LAYOUT: fix score table layout
[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     $result = verify_password($email, $_REQUEST["password0"]);
277
278     if( $result!=0 )
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         // create a password hash using the crypt function, need php 5.3 for this
292         // create and random salt
293         $salt = substr(str_replace('+', '.', base64_encode(sha1(microtime(true), true))), 0, 22);
294         // hash incoming password using 12 rounds of blowfish
295         $hash = crypt($_REQUEST["password1"], '$2y$12$' . $salt);
296
297         DB_query("UPDATE User SET password='".$hash.
298                  "' WHERE id=".DB_quote_smart($myid));
299
300         /* in case this was done using a recovery password delete that password */
301         $tmppasswd = md5($_REQUEST["password0"]);
302         if(DB_check_recovery_passwords($tmppasswd,$email))
303           DB_delete_recovery_passwords($myid);
304       }
305     /* error output below */
306   }
307
308 if(myisset("openid_url") && $_REQUEST['openid_url']!='')
309   {
310     $openid_url = OpenIDUrlEncode($_REQUEST['openid_url']);
311     DB_AttachOpenID($openid_url, $myid);
312   }
313
314 if(myisset("language"))
315   {
316     $language = $_REQUEST['language'];
317     if($language != $PREF['language'])
318       {
319         /* check if we already have an entry for the user, if so change it, if not create new one */
320         $result = DB_query("SELECT * from User_Prefs".
321                            " WHERE user_id='$myid' AND pref_key='language'" );
322         if( DB_fetch_array($result))
323           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($language).
324                              " WHERE user_id='$myid' AND pref_key='language'" );
325         else
326           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','language',".
327                              DB_quote_smart($language).")");
328         $changed_language = 1;
329       }
330   }
331
332
333 /* get infos again in case they have changed */
334 $PREF     = DB_get_PREF($myid);
335 $timezone = DB_get_user_timezone($myid);
336
337 /*
338  * output settings
339  */
340
341 echo "<div class=\"user\">\n";
342 echo "  <form action=\"index.php?action=prefs\" method=\"post\">\n";
343 echo '  <h2>'._('Your settings are')."</h2>\n";
344 echo "    <fieldset>\n";
345 echo '    <legend>'._('Game-related')."</legend>\n";
346 echo "      <table>\n";
347
348 echo '        <tr><td>'._('Vacation').":             </td>\n";
349 if($PREF['vacation_start'])
350   $value = substr($PREF['vacation_start'],0,10);
351  else
352    $value = '';
353 echo "            <td>"._('start').":<input type=\"date\" class=\"date\" name=\"vacation_start\" value=\"$value\" /></td>\n";
354 if($PREF['vacation_stop'])
355   $value = substr($PREF['vacation_stop'],0,10);
356  else
357    $value = '';
358 echo "            <td>"._('stop').":<input type=\"date\" class=\"date\" name=\"vacation_stop\" value=\"$value\" /></td>\n";
359 if($PREF['vacation_comment'])
360   $value = $PREF['vacation_comment'];
361 else
362   $value = '';
363 echo '            <td>'._('comment:')."<input type=\"text\" id=\"vacation_comment\" name=\"vacation_comment\" size=\"10\" maxlength=\"50\" value=\"$value\" />";
364 if($changed_vacation == 1) echo _('changed');
365 if($changed_vacation == -1) echo _('wrong date format');
366 echo "</td></tr>\n";
367 echo '<tr><td></td><td colspan="2">'._("set both dates to the same day to end vacation")."</td></tr>\n";
368 echo '        <tr><td>'._('Notification').":          </td><td>\n";
369 echo "          <select id=\"notify\" name=\"notify\" size=\"1\">\n";
370 if($PREF['email']=="emailaddict")
371   {
372     echo "            <option value=\"emailaddict\" selected=\"selected\">"._('less emails')."</option>\n";
373     echo "            <option value=\"emailnonaddict\">"._('lots of emails')."</option>\n";
374   }
375 else
376   {
377     echo "            <option value=\"emailaddict\">"._('less emails')."</option>\n";
378     echo "            <option value=\"emailnonaddict\" selected=\"selected\">"._('lots of emails')."</option>\n";
379   }
380 echo "          </select>";
381 if($changed_notify) echo _('changed');
382 echo " </td></tr>\n";
383
384 echo '        <tr><td>'._('Digest').":          </td><td>\n";
385 echo "          <select id=\"digest\" name=\"digest\" size=\"1\">\n";
386
387 $selected = "selected=\"selected\"";
388 echo "            <option value=\"digest-off\"";
389 if($PREF['digest']=="digest-off") echo $selected;
390 echo '>'._('digest off')."</option>\n";
391
392 echo "            <option value=\"digest-1h\" ";
393 if($PREF['digest']=="digest-1h") echo $selected;
394 echo ">"._('every hour')."</option>\n";
395
396 echo "            <option value=\"digest-2h\" ";
397 if($PREF['digest']=="digest-2h") echo $selected;
398 echo ">"._('every 2h')."</option>\n";
399
400 echo "            <option value=\"digest-3h\" ";
401 if($PREF['digest']=="digest-3h") echo $selected;
402 echo ">"._('every 3h')."</option>\n";
403
404 echo "            <option value=\"digest-4h\" ";
405 if($PREF['digest']=="digest-4h") echo $selected;
406 echo ">"._('every 4h')."</option>\n";
407
408 echo "            <option value=\"digest-6h\" ";
409 if($PREF['digest']=="digest-6h") echo $selected;
410 echo ">"._('every 6h')."</option>\n";
411
412 echo "            <option value=\"digest-12h\"";
413 if($PREF['digest']=="digest-12h") echo $selected;
414 echo ">"._('every 12h')."</option>\n";
415
416 echo "            <option value=\"digest-24h\"";
417 if($PREF['digest']=="digest-24h") echo $selected;
418 echo ">"._('every 24h')."</option>\n";
419
420 echo "          </select>";
421 if($changed_digest) echo _('changed');
422 echo " </td></tr>\n";
423
424
425 echo '        <tr><td>'._('Autosetup').":          </td><td>\n";
426 echo "          <select id=\"autosetup\" name=\"autosetup\" size=\"1\">\n";
427 if($PREF['autosetup']=="yes")
428   {
429     echo "           <option value=\"yes\" selected=\"selected\">"._('accept every game')."</option>\n";
430     echo "           <option value=\"no\">"._('ask for games')."</option>\n";
431   }
432  else
433    {
434      echo "           <option value=\"yes\">"._('accept every game')."</option>\n";
435      echo "           <option value=\"no\" selected=\"selected\">"._('ask for games')."</option>\n";
436    }
437 echo "         </select>";
438 if($changed_autosetup) echo _('changed');
439 echo " </td></tr>\n";
440 echo '    <tr><td>'._('Sorting').":          </td><td>\n";
441
442 echo "         <select id=\"sorting\" name=\"sorting\" size=\"1\">\n";
443 if($PREF['sorting']=="high-low")
444   {
445     echo "           <option value=\"high-low\" selected=\"selected\">"._('high to low')."</option>\n";
446     echo "           <option value=\"low-high\">"._('low to high')."</option>\n";
447   }
448  else
449    {
450      echo "           <option value=\"high-low\">"._('high to low')."</option>\n";
451      echo "           <option value=\"low-high\" selected=\"selected\">"._('low to high')."</option>\n";
452    }
453 echo "         </select>";
454 if($changed_sorting) echo _('changed');
455 echo " </td></tr>\n";
456 echo '        <tr><td>'._('Open for new games').":          </td><td>\n";
457 echo "         <select id=\"open_for_games\" name=\"open_for_games\" size=\"1\">\n";
458 if($PREF['open_for_games']=="no")
459   {
460     echo '           <option value="yes">'._('yes')."</option>\n";
461     echo '           <option value="no" selected="selected">'._('no')."</option>\n";
462   }
463  else /* default */
464    {
465      echo '           <option value="yes" selected="selected">'._('yes')."</option>\n";
466      echo '           <option value="no">'._('no')."</option>\n";
467    }
468 echo "         </select>";
469 if($changed_openforgames) echo _('changed');
470 echo " </td></tr>\n";
471
472 echo '    <tr><td>'.('Card set').":              </td><td>\n";
473 echo "         <select id=\"cards\" name=\"cards\" size=\"1\">\n";
474 if($PREF['cardset']=="altenburg")
475   {
476     echo "           <option value=\"altenburg\" selected=\"selected\">"._('German cards')."</option>\n";
477     echo "           <option value=\"english\">"._('English cards')."</option>\n";
478   }
479  else
480    {
481      echo "           <option value=\"altenburg\">"._('German cards')."</option>\n";
482      echo "           <option value=\"english\" selected=\"selected\">"._('English cards')."</option>\n";
483    }
484 echo "         </select>";
485 if($changed_cards) echo _('changed');
486 echo " </td></tr>\n";
487 echo "      </table>\n";
488 echo "    </fieldset>\n";
489 echo "    <fieldset>\n";
490 echo '      <legend>'._('Personal')."</legend>\n";
491 echo "      <table>\n";
492 echo '        <tr><td>'._('Email').":                 </td><td> $email    </td></tr>\n";
493 echo '        <tr><td>'._('Timezone').":              </td><td>\n";
494 output_select_timezone("timezone",$timezone);
495 if($changed_timezone) echo _('changed');
496 echo "</td></tr>\n";
497 echo '        <tr><td>'._('Language').":              </td><td>\n";
498 output_select_language("language",$PREF['language']);
499 if($changed_language == 1) echo _('changed');
500 echo "</td></tr>\n";
501 echo '        <tr><td>'._('Password(old)').":         </td><td>",
502   "<input type=\"password\" id=\"password0\" name=\"password0\" size=\"20\" maxlength=\"30\" />";
503 switch($changed_password)
504   {
505   case '-3':
506     echo _('The new passwords is not long enough (you need at least 4 characters).');
507     break;
508   case '-2':
509     echo _('The new passwords don\'t match.');
510     break;
511   case '-1':
512     echo _('The old password is not correct.');
513     break;
514   case '1':
515     echo _('changed');
516     break;
517   }
518 echo " </td></tr>\n";
519 echo '        <tr><td>'._('Password(new)').":         </td><td>",
520   "<input type=\"password\" id=\"password1\" name=\"password1\" size=\"20\" maxlength=\"30\" />",
521   " </td></tr>\n";
522 echo '        <tr><td>'._('Password(new, retype)').": </td><td>",
523   "<input type=\"password\" id=\"password2\" name=\"password2\" size=\"20\" maxlength=\"30\" />",
524   " </td></tr>\n";
525 echo "      </table>\n";
526 echo "    </fieldset>\n";
527 echo "    <fieldset>\n";
528 echo '      <legend>'._('OpenID')."</legend>\n";
529
530 $openids = array();
531 $openids = DB_GetOpenIDsByUser($myid);
532
533 if(sizeof($openids))
534   {
535     echo "     <table class=\"openid\">\n";
536     echo '     <thead><tr><th>'._('Delete')."?</th><th>OpenId</th></tr></thead>\n";
537     echo "     <tbody>\n";
538     foreach ($openids as $ids)
539       {
540         $id=($ids[0]);
541         echo "        <tr><td><input type=\"checkbox\" name=\"delete-openid-$id\" /></td><td>",$id, "</td></tr>\n";
542       }
543     echo "     </tbody>\n";
544     echo "     </table>\n";
545   }
546
547 echo '        '._('add OpenID').': ',
548   "<input type=\"text\" id=\"openid_url\" name=\"openid_url\" size=\"20\" maxlength=\"50\" />";
549 if($changed_openid)
550   echo '   '._('Deleted some OpenIDs!')." <br />\n";
551 echo "    </fieldset>\n";
552 echo '    <fieldset><legend>'._('Submit')."</legend><input type=\"submit\"  name=\"passwd\" value=\"set\" /></fieldset>\n";
553 echo "  </form>\n";
554 echo ' <p>'._('E-DoKo uses <a href="http://www.gravatar.org">gravatars</a> as icons.').'</p>';
555 echo "</div>\n";
556
557 // add jquery date picker if html5 is not available
558 ?>
559 <script>
560   $(".date").dateinput({  format: 'yyyy-mm-dd'  });
561 </script>
562 <?php
563
564
565 return;
566 ?>