NEW FEATURE: new sorting algorithm
[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             mymail($email,$EmailName."recovery ",$message);
58
59             /* we save these in the database */
60             DB_set_recovery_password($myid,md5($newpw));
61           }
62         else
63           {
64             /* make it so that people (or a robot) can request thousands of passwords within a short time
65              * and spam a user this way */
66             echo "Sorry you already tried 5 times during the last 24h.<br />".
67               "You need to use one of those passwords or wait to get a new one.<br />";
68             echo "Back to the <a href=\"$INDEX\">main page</a>.";
69           }
70       }
71     else
72       {/* can't find user id in the database */
73
74         /* no email given? */
75         if($email=="")
76           echo "You need to give me an email address! <br />".
77             "Please try <a href=\"$INDEX\">again</a>.";
78         else /* default error message */
79           echo "Couldn't find a player with this email! <br />".
80             "Please contact Arun, if you think this is a mistake <br />".
81             "or else try <a href=\"$INDEX\">again</a>.";
82       }
83   }
84 else
85   { /* normal user page */
86
87     /* verify password and email */
88     if(strlen($password)!=32)
89       $password = md5($password);
90
91     $ok  = 1;
92     $myid = DB_get_userid('email-password',$email,$password);
93     if(!$myid)
94       $ok = 0;
95
96     if($ok)
97       {
98         /* user information is ok */
99         $myname = DB_get_name('email',$email);
100         $_SESSION["name"] = $myname;
101
102         $PREF = DB_get_PREF($myid);
103
104         DB_update_user_timestamp($myid);
105
106         display_user_menu($myid);
107
108         /* display all games the user has played */
109         echo "<div class=\"user\">";
110         echo "<h4>These are all your games:</h4>\n";
111         echo "<p>Session: <br />\n";
112         echo "<span class=\"gamestatuspre\"> p </span> =  pre-game phase ";
113         echo "<span class=\"gamestatusplay\">P </span> =  game in progess ";
114         echo "<span class=\"gamestatusover\">F </span> =  game finished ";
115         echo "<span class=\"gamestatusover multi\"><a> N</a> </span> =  game finished, hand played by others too <br />";
116         echo "</p>\n";
117
118         $output = array();
119         $result = DB_query("SELECT Hand.hash,Hand.game_id,G.mod_date,G.player,G.status, ".
120                            " (SELECT count(H.randomnumbers) FROM Game H WHERE H.randomnumbers=G.randomnumbers) AS count ".
121                            " FROM Hand".
122                            " LEFT JOIN Game G ON G.id=Hand.game_id".
123                            " WHERE user_id='$myid'".
124                            " ORDER BY G.session,G.create_date" );
125
126         $gamenrold = -1;
127         echo "<table>\n <tr><td>\n";
128         while( $r = DB_fetch_array($result))
129           {
130             $game = DB_format_gameid($r[1]);
131             $gamenr = (int) $game;
132             if($gamenrold < $gamenr)
133               {
134                 if($gamenrold!=-1)
135                   echo "</td></tr>\n <tr> <td>$gamenr:</td>\n";
136                 else
137                   echo "$gamenr:</td>\n";
138                 $gamenrold = $gamenr;
139                 echo "<td class=\"usergames\">\n";
140               }
141             $Multi = ($r[5]>1) ? "multi" : "";
142             if($r[4]=='pre')
143               echo "   <span class=\"gamestatuspre $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">p </a></span>\n";
144             else if ($r[4]=='gameover')
145             {
146               echo "   <span class=\"gamestatusover $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">";
147               if($r[5]<2)
148                 echo "F ";
149               else
150                 echo $r[5];
151               echo "</a></span>\n";
152             }
153             else
154               echo "   <span class=\"gamestatusplay $Multi\"><a href=\"".$INDEX."?action=game&amp;me=".$r[0]."\">P </a></span>\n";
155             if($r[4] != 'gameover')
156               {
157                 echo "</td>\n<td>\n    ";
158                 if($r[3]==$myid || !$r[3])
159                   echo "(it's <strong>your</strong> turn)\n";
160                 else
161                   {
162                     $name = DB_get_name('userid',$r[3]);
163                     $gameid = $r[1];
164                     if(DB_get_reminder($r[3],$gameid)==0)
165                       if(time()-strtotime($r[2]) > 60*60*24*7)
166                         echo "<a href=\"$INDEX?action=reminder&amp;me=".$r[0]."\">Send a reminder.</a>";
167                     echo "(it's $name's turn)\n";
168                   };
169                 if(time()-strtotime($r[2]) > 60*60*24*30)
170                   echo "<a href=\"$INDEX?action=cancel&amp;me=".$r[0]."\">Cancel?</a>".
171                     " (clicking here is final and can't be restored)";
172               }
173           }
174         echo "</td></tr>\n</table>\n";
175
176         /* display last 5 users that have signed up to e-DoKo */
177         $names = DB_get_names_of_new_logins(5);
178         echo "<h4>New Players:</h4>\n<p>\n";
179         echo implode(", ",$names).",...\n";
180         echo "</p>\n";
181
182         /* display last 5 users that logged on */
183         $names = DB_get_names_of_last_logins(5);
184         echo "<h4>Players last logged in:</h4>\n<p>\n";
185         echo implode(", ",$names).",...\n";
186         echo "</p>\n";
187
188         echo "</div>\n";
189       }
190     else
191       {
192         echo "<div class=\"message\">Sorry email and password don't match. Please <a href=\"$INDEX\">try again</a>. </div>";
193       }
194   };
195 ?>