LAYOUT: fixed a typo and made clock icon smaller
[e-DoKo.git] / include / user.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 /* test id and password, should really be done in one step */
9 if(!isset($_SESSION["name"]))
10   {
11     $email     = $_REQUEST["email"];
12     $password  = $_REQUEST["password"];
13   }
14 else
15   {
16     $name = $_SESSION["name"];
17     $email     = DB_get_email('name',$name);
18     $password  = DB_get_passwd_by_name($name);
19   };
20
21 /* user has forgotten his password */
22 if(myisset("forgot"))
23   {
24     /* check if player is in the database */
25     $ok = 1;
26
27     $myid = DB_get_userid('email',$email);
28     if(!$myid)
29       $ok = 0;
30
31     if($ok)
32       {
33         /* check how many entries in recovery table */
34         $number = DB_get_number_of_passwords_recovery($myid);
35
36         /* if less than N recent ones, add a new one and send out email */
37         if( $number < 5 )
38           {
39             echo "Ok, I send you a new password. <br />";
40             if($number >1)
41               echo "N.B. You tried this already $number times during the last day and it will only work ".
42                 " 5 times during a day.<br />";
43             echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
44             echo "Back to the  <a href=\"$INDEX\">main page</a>.";
45
46             /* create temporary password, use the fist 8 letters of a md5 hash */
47             $TIME  = (string) time(); /* to avoid collisions */
48             $hash  = md5("Anewpassword".$email.$TIME);
49             $newpw = substr($hash,1,8);
50
51             $message = "Someone (hopefully you) requested a new password. \n".
52               "You can use this email and the following password: \n".
53               "   $newpw    \n".
54               "to log into the server. The new password is valid for 24h, so make\n".
55               "sure you reset your password to something new. Your old password will\n".
56               "also still be valid until you set a new one.\n";
57             $subject = $EmailName.' Recovery';
58             sendmail($email,$subject,$message);
59
60             /* we save these in the database */
61             DB_set_recovery_password($myid,md5($newpw));
62           }
63         else
64           {
65             /* make it so that people (or a robot) can request thousands of passwords within a short time
66              * and spam a user this way */
67             echo "Sorry you already tried 5 times during the last 24h.<br />".
68               "You need to use one of those passwords or wait to get a new one.<br />";
69             echo "Back to the <a href=\"$INDEX\">main page</a>.";
70           }
71       }
72     else
73       {/* can't find user id in the database */
74
75         /* no email given? */
76         if($email=="")
77           echo "You need to give me an email address! <br />".
78             "Please try <a href=\"$INDEX\">again</a>.";
79         else /* default error message */
80           echo "Couldn't find a player with this email! <br />".
81             "Please contact Arun, if you think this is a mistake <br />".
82             "or else try <a href=\"$INDEX\">again</a>.";
83       }
84   }
85 else
86   { /* normal user page */
87
88     /* verify password and email */
89     if(strlen($password)!=32)
90       $password = md5($password);
91
92     $ok  = 1;
93     $myid = DB_get_userid('email-password',$email,$password);
94     if(!$myid)
95       $ok = 0;
96
97     if($ok)
98       {
99         /* user information is ok */
100         $myname = DB_get_name('email',$email);
101         $_SESSION["name"] = $myname;
102
103         $PREF = DB_get_PREF($myid);
104
105         DB_update_user_timestamp($myid);
106
107         display_user_menu($myid);
108
109         /* display all games the user has played */
110         echo "<div class=\"user\">";
111
112         if($myvacation = check_vacation($myid))
113           {
114             $vac_start   = $myvacation[0];
115             $vac_stop    = $myvacation[1];
116             $vac_comment = $myvacation[2];
117             echo "<p class=\"vacation\">Enjoy your vacation (don't forgot to change your settings once you're back). Between $vac_start and $vac_stop other users will see the following message: $vac_comment.</p>\n";
118           }
119
120         echo "<h4>These are all your games:</h4>\n";
121         /* output legend */
122         echo "<p>Session: <br />\n";
123         echo "<span class=\"gamestatuspre\"> p </span> =  pre-game phase ";
124         echo "<span class=\"gamestatusplay\">P </span> =  game in progess ";
125         echo "<span class=\"gamestatusover\">E </span> =  game ended ";
126         echo "<span class=\"gamestatusover multi\"><a>N</a> </span> =  N games with same hand <br />";
127         echo "</p>\n";
128
129         $output = array();
130         $result = DB_query("SELECT Hand.hash,Hand.game_id,G.mod_date,G.player,G.status, ".
131                            " (SELECT count(H.randomnumbers) FROM Game H WHERE H.randomnumbers=G.randomnumbers) AS count ".
132                            " FROM Hand".
133                            " LEFT JOIN Game G ON G.id=Hand.game_id".
134                            " WHERE user_id='$myid'".
135                            " ORDER BY G.session,G.create_date" );
136
137         $gamenrold = -1;
138         $count = 0;
139         echo "<table>\n <tr><td>\n";
140         while( $r = DB_fetch_array($result))
141           {
142             $count++;
143             $game = DB_format_gameid($r[1]);
144             $gamenr = (int) $game;
145             if($gamenrold < $gamenr)
146               {
147                 if($gamenrold!=-1)
148                   echo "</td></tr>\n <tr> <td>$gamenr:</td>\n";
149                 else
150                   echo "$gamenr:</td>\n";
151                 $gamenrold = $gamenr;
152                 echo "<td class=\"usergames\">\n";
153               }
154             $Multi = ($r[5]>1) ? "multi" : "";
155             if($r[4]=='pre')
156               echo "   <span class=\"gamestatuspre $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">p </a></span>\n";
157             else if (in_array($r[4],array('gameover','cancel-timedout','cancel-nines','cancel-noplay','cancel-trump')))
158             {
159               echo "   <span class=\"gamestatusover $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">";
160               if($r[5]<2)
161                 echo "E ";
162               else
163                 echo $r[5];
164               echo "</a></span>\n";
165             }
166             else
167               echo "   <span class=\"gamestatusplay $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">P </a></span>\n";
168             if($r[4] == 'pre' || $r[4] == 'play')
169               {
170                 echo "</td>\n<td>\n    ";
171                 if($r[3]==$myid || !$r[3])
172                   echo "(it's <strong>your</strong> turn)\n";
173                 else
174                   {
175                     $name = DB_get_name('userid',$r[3]);
176                     $gameid = $r[1];
177                     /* check if we need to send out a reminder */
178                     if(DB_get_reminder($r[3],$gameid)==0)
179                       if(time()-strtotime($r[2]) > 60*60*24*7)
180                         echo "<a href=\"$INDEX?action=reminder&amp;me=".$r[0]."\">Send a reminder.</a>";
181
182                     /* check vacaction status of this user */
183                     if($vacation=check_vacation($r[3]))
184                       {
185                         $stop = substr($vacation[1],0,10);
186                         $title = 'begin:'.substr($vacation[0],0,10).' end:'.$vacation[1].' '.$vacation[2];
187                         echo "(it's <span class=\"vacation\" title=\"$title\">$name's (on vacation until $stop)</span> turn)\n";
188                       }
189                     else
190                       echo "(it's $name's turn)\n";
191                   };
192                 if(time()-strtotime($r[2]) > 60*60*24*30)
193                   echo "<a href=\"$INDEX?action=cancel&amp;me=".$r[0]."\">Cancel?</a> ";
194               }
195           }
196         echo "</td></tr>\n</table>\n";
197
198         /* give a hint for new players */
199         if($count<10)
200           echo "<p class=\"newbiehint\">You can start new games using the link in the top right corner!</p>\n";
201
202         /* display last 5 users that have signed up to e-DoKo */
203         $names = DB_get_names_of_new_logins(5);
204         echo "<h4>New Players:</h4>\n<p>\n";
205         echo implode(", ",$names).",...\n";
206         echo "</p>\n";
207
208         /* display last 5 users that logged on */
209         $names = DB_get_names_of_last_logins(5);
210         echo "<h4>Players last logged in:</h4>\n<p>\n";
211         echo implode(", ",$names).",...\n";
212         echo "</p>\n";
213
214         echo "</div>\n";
215       }
216     else
217       {
218         echo "<div class=\"message\">Sorry email and password don't match. Please <a href=\"$INDEX\">try again</a>. </div>";
219       }
220   };
221 ?>