BUGFIX: show "It's your turn in these games" menu also when you played the last card...
[e-DoKo.git] / include / preferences.php
1 <?php
2 /* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
3  *
4  *   This file is part of e-DoKo.
5  *
6  *   e-DoKo is free software: you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, either version 3 of the License, or
9  *   (at your option) any later version.
10  *
11  *   e-DoKo is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 /* make sure that we are not called from outside the scripts,
22  * use a variable defined in config.php to check this
23  */
24 if(!isset($HOST))
25   exit;
26
27 include_once('openid.php');
28
29 $name  = $_SESSION["name"];
30 $email = DB_get_email('name',$name);
31 $myid  = DB_get_userid('email',$email);
32 if(!$myid)
33   return;
34
35 /* track what got changed */
36 $changed_notify       = 0;
37 $changed_password     = 0;
38 $changed_cards        = 0;
39 $changed_timezone     = 0;
40 $changed_autosetup    = 0;
41 $changed_sorting      = 0;
42 $changed_openforgames = 0;
43 $changed_vacation     = 0;
44 $changed_openid       = 0;
45 $changed_digest       = 0;
46
47 display_user_menu($myid);
48
49 /* get old infos */
50 $PREF = DB_get_PREF($myid);
51 $timezone =  DB_get_user_timezone($myid);
52
53 DB_update_user_timestamp($myid);
54
55 /* does the user want to change some preferences?
56  * update the database and track changes with a variable, so that
57  * we can later highlight the changed value
58  */
59
60 /* check for deleted openids */
61 foreach($_REQUEST as $key=>$value)
62 {
63   if(strstr($key,"delete-openid-"))
64     {
65       /* found and openid to delete */
66       $DelOpenID = substr(str_replace("_",".",$key),14);
67       DB_DetachOpenID($DelOpenID, $myid);
68       $changed_openid = 1;
69     }
70 }
71
72
73 if(myisset('vacation_start','vacation_stop','vacation_comment') &&
74    ($_REQUEST['vacation_start']!='' || $_REQUEST['vacation_stop']!='')
75    )
76   {
77     $vacation_start   = $_REQUEST['vacation_start'].' 00:00:00';
78     $vacation_stop    = $_REQUEST['vacation_stop'].' 23:59:59';
79     $vacation_comment = $_REQUEST['vacation_comment'];
80
81     /* check if everything is valid */
82     if(!strtotime($vacation_start))
83       $changed_vacation = -1;
84     if(!strtotime($vacation_stop))
85       $changed_vacation = -1;
86
87     /* test if we should delete the entry */
88     if($vacation_start == '- 00:00:00')
89       {
90         $result = DB_query("DELETE FROM User_Prefs".
91                            " WHERE user_id='$myid' AND pref_key='vacation start'" );
92         $result = DB_query("DELETE FROM User_Prefs".
93                            " WHERE user_id='$myid' AND pref_key='vacation stop'" );
94         $result = DB_query("DELETE FROM User_Prefs".
95                            " WHERE user_id='$myid' AND pref_key='vacation comment'" );
96         $changed_vacation = 1;
97       }
98     /* change in database if format is ok */
99     else if($changed_vacation>=0)
100       {
101         /* only change if different from current value */
102         if($vacation_start!=$PREF['vacation_start'])
103           {
104             $result = DB_query("SELECT * from User_Prefs".
105                                " WHERE user_id='$myid' AND pref_key='vacation start'" );
106             if( DB_fetch_array($result))
107               $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($vacation_start).
108                                  " WHERE user_id='$myid' AND pref_key='vacation start'" );
109             else
110               $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','vacation start',".
111                                  DB_quote_smart($vacation_start).")");
112
113             $changed_vacation = 1;
114           }
115
116         /* same for the stop date */
117         if($vacation_stop!=$PREF['vacation_stop'])
118           {
119             $result = DB_query("SELECT * from User_Prefs".
120                                " WHERE user_id='$myid' AND pref_key='vacation stop'" );
121             if( DB_fetch_array($result))
122               $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($vacation_stop).
123                                  " WHERE user_id='$myid' AND pref_key='vacation stop'" );
124             else
125               $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','vacation stop',".
126                                  DB_quote_smart($vacation_stop).")");
127
128             $changed_vacation = 1;
129           }
130
131         /* does the user want to add a comment? */
132         if($vacation_comment!=$PREF['vacation_comment'])
133           {
134             $result = DB_query("SELECT * from User_Prefs".
135                                " WHERE user_id='$myid' AND pref_key='vacation comment'" );
136             if( DB_fetch_array($result))
137               $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($vacation_comment).
138                                  " WHERE user_id='$myid' AND pref_key='vacation comment'" );
139             else
140               $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','vacation comment',".
141                                  DB_quote_smart($vacation_comment).")");
142
143             $changed_vacation = 1;
144           }
145       }
146   }
147
148 if(myisset("timezone"))
149   {
150     $newtimezone = $_REQUEST['timezone'];
151     if($newtimezone != $timezone)
152       {
153         DB_query("UPDATE User SET timezone=".DB_quote_smart($newtimezone).
154                  " WHERE id=".DB_quote_smart($myid));
155         $changed_timezone = 1;
156       }
157   }
158
159 if(myisset("cards"))
160   {
161     $cards=$_REQUEST['cards'];
162     if($cards != $PREF['cardset'])
163       {
164         /* check if we already have an entry for the user, if so change it, if not create new one */
165         $result = DB_query("SELECT * from User_Prefs".
166                            " WHERE user_id='$myid' AND pref_key='cardset'" );
167         if( DB_fetch_array($result))
168           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($cards).
169                              " WHERE user_id='$myid' AND pref_key='cardset'" );
170         else
171           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
172                              DB_quote_smart($cards).")");
173         $changed_cards = 1;
174       }
175   }
176
177 if(myisset("notify"))
178   {
179     $notify=$_REQUEST['notify'];
180     if($notify != $PREF['email'])
181       {
182         /* check if we already have an entry for the user, if so change it, if not create new one */
183         $result = DB_query("SELECT * from User_Prefs".
184                            " WHERE user_id='$myid' AND pref_key='email'" );
185         if( DB_fetch_array($result))
186           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($notify).
187                              " WHERE user_id='$myid' AND pref_key='email'" );
188         else
189           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','email',".
190                              DB_quote_smart($notify).")");
191         $changed_notify=1;
192       }
193   }
194
195 if(myisset("digest"))
196   {
197     $digest=$_REQUEST['digest'];
198     if($digest != $PREF['digest'])
199       {
200         /* check if we already have an entry for the user, if so change it, if not create new one */
201         $result = DB_query("SELECT * from User_Prefs".
202                            " WHERE user_id='$myid' AND pref_key='digest'" );
203         if( DB_fetch_array($result))
204           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($digest).
205                              " WHERE user_id='$myid' AND pref_key='digest'" );
206         else
207           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','digest',".
208                              DB_quote_smart($digest).")");
209         $changed_digest=1;
210       }
211   }
212
213 if(myisset("autosetup"))
214   {
215     $autosetup = $_REQUEST['autosetup'];
216     if($autosetup != $PREF['autosetup'])
217       {
218         /* check if we already have an entry for the user, if so change it, if not create new one */
219         $result = DB_query("SELECT * from User_Prefs".
220                            " WHERE user_id='$myid' AND pref_key='autosetup'" );
221         if( DB_fetch_array($result))
222           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($autosetup).
223                              " WHERE user_id='$myid' AND pref_key='autosetup'" );
224         else
225           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','autosetup',".
226                              DB_quote_smart($autosetup).")");
227         $changed_autosetup=1;
228       }
229   }
230
231 if(myisset("sorting"))
232   {
233     $sorting = $_REQUEST['sorting'];
234     if($sorting != $PREF['sorting'])
235       {
236         /* check if we already have an entry for the user, if so change it, if not create new one */
237         $result = DB_query("SELECT * from User_Prefs".
238                            " WHERE user_id='$myid' AND pref_key='sorting'" );
239         if( DB_fetch_array($result))
240           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($sorting).
241                              " WHERE user_id='$myid' AND pref_key='sorting'" );
242         else
243           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','sorting',".
244                              DB_quote_smart($sorting).")");
245         $changed_sorting=1;
246       }
247   }
248
249 if(myisset("open_for_games"))
250   {
251     $openforgames = $_REQUEST['open_for_games'];
252     if($openforgames != $PREF['open_for_games'])
253       {
254         /* check if we already have an entry for the user, if so change it, if not create new one */
255         $result = DB_query("SELECT * from User_Prefs".
256                            " WHERE user_id='$myid' AND pref_key='open for games'" );
257         if( DB_fetch_array($result))
258           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($openforgames).
259                              " WHERE user_id='$myid' AND pref_key='open for games'" );
260         else
261           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','open for games',".
262                              DB_quote_smart($openforgames).")");
263         $changed_openforgames=1;
264       }
265   }
266
267
268 if(myisset("password0","password1","password2") &&  $_REQUEST["password0"]!="" &&  $_REQUEST["password0"]!= $_REQUEST["password1"])
269   {
270     $changed_password = 1;
271
272     /* check if old password matches */
273     $oldpasswd = md5($_REQUEST["password0"]);
274     $password  = DB_get_passwd_by_userid($myid);
275     if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
276       $changed_password = -1;
277
278     /* check if new password has been typed in correctly */
279     if($_REQUEST["password1"] != $_REQUEST["password2"] )
280       $changed_password = -2;
281
282     /* check if new password is long enough */
283     if(strlen($_REQUEST["password1"])<4)
284       $changed_password = -3;
285
286     if($changed_password==1)
287       {
288         DB_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
289                  "' WHERE id=".DB_quote_smart($myid));
290       }
291     /* error output below */
292   }
293
294 if(myisset("openid_url") && $_REQUEST['openid_url']!='')
295   {
296     $openid_url = OpenIDUrlEncode($_REQUEST['openid_url']);
297     DB_AttachOpenID($openid_url, $myid);
298   }
299
300 /* get infos again in case they have changed */
301 $PREF     = DB_get_PREF($myid);
302 $timezone = DB_get_user_timezone($myid);
303
304 /*
305  * output settings
306  */
307
308 echo "<div class=\"user\">\n";
309 echo "  <form action=\"index.php?action=prefs\" method=\"post\">\n";
310 echo "  <h2>Your settings are</h2>\n";
311 echo "    <fieldset>\n";
312 echo "    <legend>Game-related</legend>\n";
313 echo "      <table>\n";
314
315 echo "        <tr><td>Vacation:             </td>\n";
316 if($PREF['vacation_start'])
317   $value = substr($PREF['vacation_start'],0,10);
318  else
319    $value = '';
320 echo "            <td>start:<input type=\"text\" id=\"vacation_start\" name=\"vacation_start\" size=\"10\" maxlength=\"10\" value=\"$value\" /></td>\n";
321 if($PREF['vacation_stop'])
322   $value = substr($PREF['vacation_stop'],0,10);
323  else
324    $value = '';
325 echo "            <td>stop:<input type=\"text\" id=\"vacation_stop\" name=\"vacation_stop\" size=\"10\" maxlength=\"10\" value=\"$value\" /></td>\n";
326 if($PREF['vacation_comment'])
327   $value = $PREF['vacation_comment'];
328 else
329   $value = '';
330 echo "            <td>comment:<input type=\"text\" id=\"vacation_comment\" name=\"vacation_comment\" size=\"10\" maxlength=\"50\" value=\"$value\" />";
331 if($changed_vacation == 1) echo "changed";
332 if($changed_vacation == -1) echo "wrong date format";
333 echo "</td></tr>\n";
334 echo "<tr><td></td><td>use YYYY-MM-DD</td><td>use '-'  in start field to unset vacation</td></tr>\n";
335 echo "        <tr><td>Notification:          </td><td>\n";
336 echo "          <select id=\"notify\" name=\"notify\" size=\"1\">\n";
337 if($PREF['email']=="emailaddict")
338   {
339     echo "            <option value=\"emailaddict\" selected=\"selected\">less emails</option>\n";
340     echo "            <option value=\"emailnonaddict\">lots of emails</option>\n";
341   }
342 else
343   {
344     echo "            <option value=\"emailaddict\">less email</option>\n";
345     echo "            <option value=\"emailnonaddict\" selected=\"selected\">lots of email</option>\n";
346   }
347 echo "          </select>";
348 if($changed_notify) echo "changed";
349 echo " </td></tr>\n";
350
351 echo "        <tr><td>Digest:          </td><td>\n";
352 echo "          <select id=\"digest\" name=\"digest\" size=\"1\">\n";
353
354 $selected = "selected=\"selected\"";
355 echo "            <option value=\"digest-off\"";
356 if($PREF['digest']=="digest-off") echo $selected;
357 echo ">digest off</option>\n";
358
359 echo "            <option value=\"digest-1h\" ";
360 if($PREF['digest']=="digest-1h") echo $selected;
361 echo ">every hour</option>\n";
362
363 echo "            <option value=\"digest-2h\" ";
364 if($PREF['digest']=="digest-2h") echo $selected;
365 echo ">every 2h</option>\n";
366
367 echo "            <option value=\"digest-3h\" ";
368 if($PREF['digest']=="digest-3h") echo $selected;
369 echo ">every 3h</option>\n";
370
371 echo "            <option value=\"digest-4h\" ";
372 if($PREF['digest']=="digest-4h") echo $selected;
373 echo ">every 4h</option>\n";
374
375 echo "            <option value=\"digest-6h\" ";
376 if($PREF['digest']=="digest-6h") echo $selected;
377 echo ">every 6h</option>\n";
378
379 echo "            <option value=\"digest-12h\"";
380 if($PREF['digest']=="digest-12h") echo $selected;
381 echo ">every 12h</option>\n";
382
383 echo "            <option value=\"digest-24h\"";
384 if($PREF['digest']=="digest-24h") echo $selected;
385 echo ">every 24h</option>\n";
386
387 echo "          </select>";
388 if($changed_digest) echo "changed";
389 echo " </td></tr>\n";
390
391
392 echo "        <tr><td>Autosetup:          </td><td>\n";
393 echo "          <select id=\"autosetup\" name=\"autosetup\" size=\"1\">\n";
394 if($PREF['autosetup']=="yes")
395   {
396     echo "           <option value=\"yes\" selected=\"selected\">accept every game</option>\n";
397     echo "           <option value=\"no\">ask for games</option>\n";
398   }
399  else
400    {
401      echo "           <option value=\"yes\">accept every game</option>\n";
402      echo "           <option value=\"no\" selected=\"selected\">ask for games</option>\n";
403    }
404 echo "         </select>";
405 if($changed_autosetup) echo "changed";
406 echo " </td></tr>\n";
407 echo "    <tr><td>Sorting:          </td><td>\n";
408
409 echo "         <select id=\"sorting\" name=\"sorting\" size=\"1\">\n";
410 if($PREF['sorting']=="high-low")
411   {
412     echo "           <option value=\"high-low\" selected=\"selected\">high to low</option>\n";
413     echo "           <option value=\"low-high\">low to high</option>\n";
414   }
415  else
416    {
417      echo "           <option value=\"high-low\">high to low</option>\n";
418      echo "           <option value=\"low-high\" selected=\"selected\">low to high</option>\n";
419    }
420 echo "         </select>";
421 if($changed_sorting) echo "changed";
422 echo " </td></tr>\n";
423 echo "        <tr><td>Open for new games:          </td><td>\n";
424 echo "         <select id=\"open_for_games\" name=\"open_for_games\" size=\"1\">\n";
425 if($PREF['open_for_games']=="no")
426   {
427     echo "           <option value=\"yes\">yes</option>\n";
428     echo "           <option value=\"no\" selected=\"selected\">no</option>\n";
429   }
430  else /* default */
431    {
432      echo "           <option value=\"yes\" selected=\"selected\">yes</option>\n";
433      echo "           <option value=\"no\">no</option>\n";
434    }
435 echo "         </select>";
436 if($changed_openforgames) echo "changed";
437 echo " </td></tr>\n";
438
439 echo "    <tr><td>Card set:              </td><td>\n";
440 echo "         <select id=\"cards\" name=\"cards\" size=\"1\">\n";
441 if($PREF['cardset']=="altenburg")
442   {
443     echo "           <option value=\"altenburg\" selected=\"selected\">German cards</option>\n";
444     echo "           <option value=\"english\">English cards</option>\n";
445   }
446  else
447    {
448      echo "           <option value=\"altenburg\">German cards</option>\n";
449      echo "           <option value=\"english\" selected=\"selected\">English cards</option>\n";
450    }
451 echo "         </select>";
452 if($changed_cards) echo "changed";
453 echo " </td></tr>\n";
454 echo "      </table>\n";
455 echo "    </fieldset>\n";
456 echo "    <fieldset>\n";
457 echo "      <legend>Personal</legend>\n";
458 echo "      <table>\n";
459 echo "        <tr><td>Email:                 </td><td> $email    </td></tr>\n";
460 echo "        <tr><td>Timezone:              </td><td>\n";
461 output_select_timezone("timezone",$timezone);
462 if($changed_timezone) echo "changed";
463 echo "</td></tr>\n";
464
465 echo "        <tr><td>Password(old):         </td><td>",
466   "<input type=\"password\" id=\"password0\" name=\"password0\" size=\"20\" maxlength=\"30\" />";
467 switch($changed_password)
468   {
469   case '-3':
470     echo "The new passwords is not long enough (you need at least 4 characters).";
471     break;
472   case '-2':
473     echo "The new passwords don't match.";
474     break;
475   case '-1':
476     echo "The old password is not correct.";
477     break;
478   case '1':
479     echo "changed";
480     break;
481   }
482 echo " </td></tr>\n";
483 echo "        <tr><td>Password(new):         </td><td>",
484   "<input type=\"password\" id=\"password1\" name=\"password1\" size=\"20\" maxlength=\"30\" />",
485   " </td></tr>\n";
486 echo "        <tr><td>Password(new, retype): </td><td>",
487   "<input type=\"password\" id=\"password2\" name=\"password2\" size=\"20\" maxlength=\"30\" />",
488   " </td></tr>\n";
489 echo "      </table>\n";
490 echo "    </fieldset>\n";
491 echo "    <fieldset>\n";
492 echo "      <legend>OpenID</legend>\n";
493
494 $openids = array();
495 $openids = DB_GetOpenIDsByUser($myid);
496
497 if(sizeof($openids))
498   {
499     echo "     <table class=\"openid\">\n";
500     echo "     <thead><tr><th>Delete?</th><th>OpenId</th></tr></thead>\n";
501     echo "     <tbody>\n";
502     foreach ($openids as $ids)
503       {
504         $id=($ids[0]);
505         echo "        <tr><td><input type=\"checkbox\" name=\"delete-openid-$id\" /></td><td>",$id, "</td></tr>\n";
506       }
507     echo "     </tbody>\n";
508     echo "     </table>\n";
509   }
510
511 echo "        add OpenID: ",
512   "<input type=\"text\" id=\"openid_url\" name=\"openid_url\" size=\"20\" maxlength=\"50\" />";
513 if($changed_openid)
514   echo "   Deleted some OpenIDs! <br />\n";
515 echo "    </fieldset>\n";
516 echo "    <fieldset><legend>Submit</legend><input type=\"submit\"  name=\"passwd\" value=\"set\" /></fieldset>\n";
517 echo "  </form>\n";
518 echo " <p>E-DoKo uses <a href=\"http://www.gravatar.org\">gravatars</a> as icons. I</p>";
519 echo "</div>\n";
520
521 return;
522 ?>