2 /* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 Arun Persaud <arun@nubati.net>
4 * This file is part of e-DoKo.
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.
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.
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/>.
21 /* make sure that we are not called from outside the scripts,
22 * use a variable defined in config.php to check this
27 include_once('openid.php');
29 $name = $_SESSION["name"];
30 $email = DB_get_email('name',$name);
31 $myid = DB_get_userid('email',$email);
35 /* track what got changed */
37 $changed_password = 0;
39 $changed_timezone = 0;
40 $changed_autosetup = 0;
42 $changed_openforgames = 0;
43 $changed_vacation = 0;
46 $changed_language = 0;
48 display_user_menu($myid);
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);
56 DB_update_user_timestamp($myid);
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
63 /* check for deleted openids */
64 foreach($_REQUEST as $key=>$value)
66 if(strstr($key,"delete-openid-"))
68 /* found and openid to delete */
69 $DelOpenID = substr(str_replace("_",".",$key),14);
70 DB_DetachOpenID($DelOpenID, $myid);
76 if(myisset('vacation_start','vacation_stop','vacation_comment') &&
77 ($_REQUEST['vacation_start']!='' || $_REQUEST['vacation_stop']!='')
80 $vacation_start = $_REQUEST['vacation_start'].' 00:00:00';
81 $vacation_stop = $_REQUEST['vacation_stop'].' 23:59:59';
82 $vacation_comment = $_REQUEST['vacation_comment'];
84 /* check if everything is valid */
85 if(!strtotime($vacation_start))
86 $changed_vacation = -1;
87 if(!strtotime($vacation_stop))
88 $changed_vacation = -1;
90 /* test if we should delete the entry */
91 if($_REQUEST['vacation_start'] == $_REQUEST['vacation_stop'])
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;
101 /* change in database if format is ok */
102 else if($changed_vacation>=0)
104 /* only change if different from current value */
105 if($vacation_start!=$PREF['vacation_start'])
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'" );
113 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','vacation start',".
114 DB_quote_smart($vacation_start).")");
116 $changed_vacation = 1;
119 /* same for the stop date */
120 if($vacation_stop!=$PREF['vacation_stop'])
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'" );
128 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','vacation stop',".
129 DB_quote_smart($vacation_stop).")");
131 $changed_vacation = 1;
134 /* does the user want to add a comment? */
135 if($vacation_comment!=$PREF['vacation_comment'])
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'" );
143 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','vacation comment',".
144 DB_quote_smart($vacation_comment).")");
146 $changed_vacation = 1;
151 if(myisset("timezone"))
153 $newtimezone = $_REQUEST['timezone'];
154 if($newtimezone != $timezone)
156 DB_query("UPDATE User SET timezone=".DB_quote_smart($newtimezone).
157 " WHERE id=".DB_quote_smart($myid));
158 $changed_timezone = 1;
164 $cards=$_REQUEST['cards'];
165 if($cards != $PREF['cardset'])
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'" );
174 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
175 DB_quote_smart($cards).")");
180 if(myisset("notify"))
182 $notify=$_REQUEST['notify'];
183 if($notify != $PREF['email'])
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'" );
192 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','email',".
193 DB_quote_smart($notify).")");
198 if(myisset("digest"))
200 $digest=$_REQUEST['digest'];
201 if($digest != $PREF['digest'])
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'" );
210 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','digest',".
211 DB_quote_smart($digest).")");
216 if(myisset("autosetup"))
218 $autosetup = $_REQUEST['autosetup'];
219 if($autosetup != $PREF['autosetup'])
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'" );
228 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','autosetup',".
229 DB_quote_smart($autosetup).")");
230 $changed_autosetup=1;
234 if(myisset("sorting"))
236 $sorting = $_REQUEST['sorting'];
237 if($sorting != $PREF['sorting'])
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'" );
246 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','sorting',".
247 DB_quote_smart($sorting).")");
252 if(myisset("open_for_games"))
254 $openforgames = $_REQUEST['open_for_games'];
255 if($openforgames != $PREF['open_for_games'])
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'" );
264 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','open for games',".
265 DB_quote_smart($openforgames).")");
266 $changed_openforgames=1;
271 if(myisset("password0","password1","password2") && $_REQUEST["password0"]!="" && $_REQUEST["password0"]!= $_REQUEST["password1"])
273 $changed_password = 1;
275 /* check if old password matches */
276 $result = verify_password($email, $_REQUEST["password0"]);
279 $changed_password = -1;
281 /* check if new password has been typed in correctly */
282 if($_REQUEST["password1"] != $_REQUEST["password2"] )
283 $changed_password = -2;
285 /* check if new password is long enough */
286 if(strlen($_REQUEST["password1"])<4)
287 $changed_password = -3;
289 if($changed_password==1)
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);
297 DB_query("UPDATE User SET password='".$hash.
298 "' WHERE id=".DB_quote_smart($myid));
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);
305 /* error output below */
308 if(myisset("openid_url") && $_REQUEST['openid_url']!='')
310 $openid_url = OpenIDUrlEncode($_REQUEST['openid_url']);
311 DB_AttachOpenID($openid_url, $myid);
314 if(myisset("language"))
316 $language = $_REQUEST['language'];
317 if($language != $PREF['language'])
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'" );
326 $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','language',".
327 DB_quote_smart($language).")");
328 $changed_language = 1;
333 /* get infos again in case they have changed */
334 $PREF = DB_get_PREF($myid);
335 $timezone = DB_get_user_timezone($myid);
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";
348 echo ' <tr><td>'._('Vacation').": </td>\n";
349 if($PREF['vacation_start'])
350 $value = substr($PREF['vacation_start'],0,10);
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);
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'];
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');
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")
372 echo " <option value=\"emailaddict\" selected=\"selected\">"._('less emails')."</option>\n";
373 echo " <option value=\"emailnonaddict\">"._('lots of emails')."</option>\n";
377 echo " <option value=\"emailaddict\">"._('less emails')."</option>\n";
378 echo " <option value=\"emailnonaddict\" selected=\"selected\">"._('lots of emails')."</option>\n";
381 if($changed_notify) echo _('changed');
382 echo " </td></tr>\n";
384 echo ' <tr><td>'._('Digest').": </td><td>\n";
385 echo " <select id=\"digest\" name=\"digest\" size=\"1\">\n";
387 $selected = "selected=\"selected\"";
388 echo " <option value=\"digest-off\"";
389 if($PREF['digest']=="digest-off") echo $selected;
390 echo '>'._('digest off')."</option>\n";
392 echo " <option value=\"digest-1h\" ";
393 if($PREF['digest']=="digest-1h") echo $selected;
394 echo ">"._('every hour')."</option>\n";
396 echo " <option value=\"digest-2h\" ";
397 if($PREF['digest']=="digest-2h") echo $selected;
398 echo ">"._('every 2h')."</option>\n";
400 echo " <option value=\"digest-3h\" ";
401 if($PREF['digest']=="digest-3h") echo $selected;
402 echo ">"._('every 3h')."</option>\n";
404 echo " <option value=\"digest-4h\" ";
405 if($PREF['digest']=="digest-4h") echo $selected;
406 echo ">"._('every 4h')."</option>\n";
408 echo " <option value=\"digest-6h\" ";
409 if($PREF['digest']=="digest-6h") echo $selected;
410 echo ">"._('every 6h')."</option>\n";
412 echo " <option value=\"digest-12h\"";
413 if($PREF['digest']=="digest-12h") echo $selected;
414 echo ">"._('every 12h')."</option>\n";
416 echo " <option value=\"digest-24h\"";
417 if($PREF['digest']=="digest-24h") echo $selected;
418 echo ">"._('every 24h')."</option>\n";
421 if($changed_digest) echo _('changed');
422 echo " </td></tr>\n";
425 echo ' <tr><td>'._('Autosetup').": </td><td>\n";
426 echo " <select id=\"autosetup\" name=\"autosetup\" size=\"1\">\n";
427 if($PREF['autosetup']=="yes")
429 echo " <option value=\"yes\" selected=\"selected\">"._('accept every game')."</option>\n";
430 echo " <option value=\"no\">"._('ask for games')."</option>\n";
434 echo " <option value=\"yes\">"._('accept every game')."</option>\n";
435 echo " <option value=\"no\" selected=\"selected\">"._('ask for games')."</option>\n";
438 if($changed_autosetup) echo _('changed');
439 echo " </td></tr>\n";
440 echo ' <tr><td>'._('Sorting').": </td><td>\n";
442 echo " <select id=\"sorting\" name=\"sorting\" size=\"1\">\n";
443 if($PREF['sorting']=="high-low")
445 echo " <option value=\"high-low\" selected=\"selected\">"._('high to low')."</option>\n";
446 echo " <option value=\"low-high\">"._('low to high')."</option>\n";
450 echo " <option value=\"high-low\">"._('high to low')."</option>\n";
451 echo " <option value=\"low-high\" selected=\"selected\">"._('low to high')."</option>\n";
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")
460 echo ' <option value="yes">'._('yes')."</option>\n";
461 echo ' <option value="no" selected="selected">'._('no')."</option>\n";
465 echo ' <option value="yes" selected="selected">'._('yes')."</option>\n";
466 echo ' <option value="no">'._('no')."</option>\n";
469 if($changed_openforgames) echo _('changed');
470 echo " </td></tr>\n";
472 echo ' <tr><td>'.('Card set').": </td><td>\n";
473 echo " <select id=\"cards\" name=\"cards\" size=\"1\">\n";
474 if($PREF['cardset']=="altenburg")
476 echo " <option value=\"altenburg\" selected=\"selected\">"._('German cards')."</option>\n";
477 echo " <option value=\"english\">"._('English cards')."</option>\n";
481 echo " <option value=\"altenburg\">"._('German cards')."</option>\n";
482 echo " <option value=\"english\" selected=\"selected\">"._('English cards')."</option>\n";
485 if($changed_cards) echo _('changed');
486 echo " </td></tr>\n";
488 echo " </fieldset>\n";
489 echo " <fieldset>\n";
490 echo ' <legend>'._('Personal')."</legend>\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');
497 echo ' <tr><td>'._('Language').": </td><td>\n";
498 output_select_language("language",$PREF['language']);
499 if($changed_language == 1) echo _('changed');
501 echo ' <tr><td>'._('Password(old)').": </td><td>",
502 "<input type=\"password\" id=\"password0\" name=\"password0\" size=\"20\" maxlength=\"30\" />";
503 switch($changed_password)
506 echo _('The new passwords is not long enough (you need at least 4 characters).');
509 echo _('The new passwords don\'t match.');
512 echo _('The old password is not correct.');
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\" />",
522 echo ' <tr><td>'._('Password(new, retype)').": </td><td>",
523 "<input type=\"password\" id=\"password2\" name=\"password2\" size=\"20\" maxlength=\"30\" />",
526 echo " </fieldset>\n";
527 echo " <fieldset>\n";
528 echo ' <legend>'._('OpenID')."</legend>\n";
531 $openids = DB_GetOpenIDsByUser($myid);
535 echo " <table class=\"openid\">\n";
536 echo ' <thead><tr><th>'._('Delete')."?</th><th>OpenId</th></tr></thead>\n";
538 foreach ($openids as $ids)
541 echo " <tr><td><input type=\"checkbox\" name=\"delete-openid-$id\" /></td><td>",$id, "</td></tr>\n";
547 echo ' '._('add OpenID').': ',
548 "<input type=\"text\" id=\"openid_url\" name=\"openid_url\" size=\"20\" maxlength=\"50\" />";
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";
554 echo ' <p>'._('E-DoKo uses <a href="http://www.gravatar.org">gravatars</a> as icons.').'</p>';
557 // add jquery date picker if html5 is not available
560 $(".date").dateinput({ format: 'yyyy-mm-dd' });