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