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