Reset rewritten, SQL fixed, zeros are now numeric
[mailer.git] / inc / modules / guest / what-login.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/14/2003 *
4  * ===============                              Last change: 04/28/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-login.php                                   *
8  * -------------------------------------------------------------------- *
9  * Short description : Login area (redirects to the real login module)  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Loginbereich (leitet an das richtige Lgin-Modul  *
12  *                     weiter)                                          *
13  * -------------------------------------------------------------------- *
14  *                                                                      *
15  * -------------------------------------------------------------------- *
16  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
17  * For more information visit: http://www.mxchange.org                  *
18  *                                                                      *
19  * This program is free software; you can redistribute it and/or modify *
20  * it under the terms of the GNU General Public License as published by *
21  * the Free Software Foundation; either version 2 of the License, or    *
22  * (at your option) any later version.                                  *
23  *                                                                      *
24  * This program is distributed in the hope that it will be useful,      *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
27  * GNU General Public License for more details.                         *
28  *                                                                      *
29  * You should have received a copy of the GNU General Public License    *
30  * along with this program; if not, write to the Free Software          *
31  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
32  * MA  02110-1301  USA                                                  *
33  ************************************************************************/
34
35 // Some security stuff...
36 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 // Add description as navigation point
42 ADD_DESCR("guest", basename(__FILE__));
43
44 global $DATA, $FATAL;
45
46 // Initialize data
47 $probe_nickname = false; $UID = false; $hash = "";
48 unset($login); unset($online);
49
50 if ((!empty($GLOBALS['userid'])) && (isSessionVariableSet('u_hash')))
51 {
52         // Already logged in?
53         $UID = $GLOBALS['userid'];
54 } elseif ((!empty($_POST['id'])) && (!empty($_POST['password'])) && (isset($_POST['ok']))) {
55         // Set userid and crypt password when login data was submitted
56         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
57         if ($probe_nickname)
58         {
59                 // Nickname entered
60                 $UID = SQL_ESCAPE($_POST['id']);
61         } else {
62                 // Direct userid entered
63                 $UID  = bigintval($_POST['id']);
64         }
65 } elseif (!empty($_POST['new_pass'])) {
66         // New password requested
67         $UID = 0;
68         if (!empty($_POST['id'])) $UID = $_POST['id'];
69 } else {
70         // Not logged in
71         $UID = 0; $hash = "";
72 }
73
74 $URL = ""; $ADD = "";
75 // Set unset variables
76 if (empty($_POST['new_pass'])) $_POST['new_pass'] = "";
77 if (empty($_GET['login']))     $_GET['login']     = "";
78
79 if (IS_MEMBER()) {
80         // Login immidiately...
81         $URL = URL."/modules.php?module=login";
82 } elseif (isset($_POST['ok'])) {
83         // Add last_login if available
84         $LAST = "";
85         if (GET_EXT_VERSION("sql_patches") >= "0.2.8") {
86                 $LAST = ", last_login";
87         }
88
89         // Check login data
90         $password = "";
91         if ($probe_nickname) {
92                 // Nickname entered
93                 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$LAST." FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' AND status='CONFIRMED' LIMIT 1",
94                  array($UID), __FILE__, __LINE__);
95                 list($UID2, $password, $online, $login) = SQL_FETCHROW($result);
96                 if (!empty($UID2)) $UID = $UID2;
97         } else {
98                 // Direct userid entered
99                 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$LAST." FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
100                  array(bigintval($UID), $hash), __FILE__, __LINE__);
101                 list($dmy, $password, $online, $login) = SQL_FETCHROW($result);
102         }
103         if (SQL_NUMROWS($result) == 1) {
104                 // Valid data found so let's load the last login data
105                 if (isset($_POST['ok'])) {
106                         // By default the hash is empty
107                         $hash = "";
108
109                         // Check for old MD5 passwords
110                         if ((strlen($password) == 32) && (md5($_POST['password']) == $password)) {
111                                 // Just set the hash to the password from DB... :)
112                                 $hash = $password;
113                         } else {
114                                 // Encrypt hash for comparsion
115                                 $hash = generateHash($_POST['password'], substr($password, 0, -40));
116                         }
117
118                         if ($hash == $password) {
119                                 // New hashed password found so let's generate a new one
120                                 $hash = generateHash($_POST['password']);
121
122                                 // ... and update database
123                                 $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
124                                  array($hash, $UID), __FILE__, __LINE__);
125
126                                 // No login bonus by default
127                                 $BONUS = false;
128
129                                 // Probe for last online timemark
130                                 $probe = time() -  $online;
131                                 if (!empty($login)) $probe = time() - $login;
132                                 if ((GET_EXT_VERSION("bonus") >= "0.2.2") && ($probe >= $_CONFIG['login_timeout'])) {
133                                         // Add login bonus to user's account
134                                         $ADD = ", login_bonus=login_bonus+'".$_CONFIG['login_bonus']."'";
135                                         $BONUS = true;
136
137                                         // Subtract login bonus from userid's account or jackpot
138                                         if ((GET_EXT_VERSION("bonus") >= "0.3.5") && ($_CONFIG['bonus_mode'] != "ADD")) BONUS_POINTS_HANDLER('login_bonus');
139                                 }
140
141
142                                 // Secure lifetime from input form
143                                 $l = bigintval($_POST['lifetime']);
144                                 $life = "-1";
145                                 if ($l > 0) {
146                                         // Calculate lifetime of cookies
147                                         $life = time() + $l;
148
149                                         // Calculate new hash with the secret key and master salt together
150                                         $hash = generatePassString($hash);
151
152                                         // Update cookies
153                                         $login = (set_session("userid"  , $UID , $life, COOKIE_PATH)
154                                                && set_session("u_hash"  , $hash, $life, COOKIE_PATH)
155                                                && set_session("lifetime", $l   , $life, COOKIE_PATH));
156
157                                         // Update global array
158                                         $GLOBALS['userid'] = bigintval($UID);
159                                 } else {
160                                         // Check for login data
161                                         $login = IS_MEMBER();
162                                 }
163
164                                 if ($login) {
165                                         // Update database records
166                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET total_logins=total_logins+1".$ADD." WHERE userid=%s LIMIT 1",
167                                          array(bigintval($UID)), __FILE__, __LINE__);
168                                         if (SQL_AFFECTEDROWS() == 1) {
169                                                 // Procedure to checking for login data
170                                                 if (($BONUS) && (EXT_IS_ACTIVE("bonus"))) {
171                                                         // Bonus added (just displaying!)
172                                                         $URL = URL."/modules.php?module=chk_login&mode=bonus";
173                                                 } else {
174                                                         // Bonus not added
175                                                         $URL = URL."/modules.php?module=chk_login&mode=login";
176                                                 }
177                                         } else {
178                                                 // Cannot update counter!
179                                                 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".CODE_CNTR_FAILED;
180                                         }
181                                 } else {
182                                         // Cookies not setable!
183                                         $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".CODE_NO_COOKIES;
184                                 }
185                         } else {
186                                 // Wrong password!
187                                 $ERROR = CODE_WRONG_PASS;
188                         }
189                 } else {
190                         // Fatal error!
191                         $ERROR = CODE_LOGIN_FAILED;
192                 }
193         } else {
194                 // Other account status?
195                 $result = SQL_QUERY_ESC("SELECT status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
196                  array(bigintval($UID)), __FILE__, __LINE__);
197                 if (SQL_NUMROWS($result) == 1)
198                 {
199                         // Load status
200                         list($status) = SQL_FETCHROW($result);
201                         switch ($status)
202                         {
203                         case "LOCKED":
204                                 $ERROR = CODE_ID_LOCKED;
205                                 break;
206
207                         case "UNCONFIRMED":
208                                 $ERROR = CODE_ID_UNCONFIRMED;
209                                 break;
210
211                         default:
212                                 $ERROR = CODE_UNKNOWN_STATUS;
213                                 break;
214                         }
215                 }
216                  else
217                 {
218                         // ID not found!
219                         $ERROR = CODE_WRONG_ID;
220                 }
221
222                 // Construct URL
223                 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".$ERROR;
224         }
225 }
226  elseif ((!empty($_POST['new_pass'])) && (isset($UID)))
227 {
228         // Compile email when found in address (only secure chars!)
229         if (!empty($_POST['email'])) $_POST['email'] = str_replace("{DOT}", '.', $_POST['email']);
230
231         // Set ID number when left empty
232         if (empty($_POST['id'])) $_POST['id'] = 0;
233
234         // Probe userid/nickname
235         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
236         if ($probe_nickname)
237         {
238                 // Nickname entered
239                 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' OR email='%s' LIMIT 1",
240                  array(addslashes($UID), $_POST['email']), __FILE__, __LINE__);
241         }
242          else
243         {
244                 // Direct userid entered
245                 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s OR email='%s' LIMIT 1",
246                  array(bigintval($UID), $_POST['email']), __FILE__, __LINE__);
247         }
248         if (SQL_NUMROWS($result) == 1)
249         {
250                 // This data is valid, so we create a new pass... :-)
251                 list($UID, $status) = SQL_FETCHROW($result);
252
253                 if ($status == "CONFIRMED")
254                 {
255                         // Ooppps, this was missing! ;-) We should update the database...
256                         $NEW_PASS = GEN_PASS();
257                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%s LIMIT 1",
258                          array(generateHash($NEW_PASS), bigintval($UID)), __FILE__, __LINE__);
259
260                         // Prepare data and message for email
261                         $DATA = array($NEW_PASS, getenv('REMOTE_ADDR'));
262                         $msg = LOAD_EMAIL_TEMPLATE("new-pass", "", bigintval($UID));
263
264                         // ... and send it away
265                         SEND_EMAIL(bigintval($UID), GUEST_NEW_PASSWORD, $msg);
266
267                         // Output note to user
268                         LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
269                 }
270                  else
271                 {
272                         // Account is locked or unconfirmed
273                         switch ($status)
274                         {
275                                 case "LOCKED"     : $MSG = CODE_ID_LOCKED;      break;
276                                 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
277                         }
278
279                         // Load URL
280                         LOAD_URL("modules.php?module=".$GLOBALS['module']."&amp;what=login&login=".$MSG);
281                 }
282         }
283          else
284         {
285                 // ID or email is wrong
286                 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
287         }
288 }
289  else
290 {
291         // Login problems?
292         if (!empty($_GET['login']))
293         {
294                 // Ok, which one now?
295                 $MSG = "<TR>
296   <TD width=\"10\">&nbsp;</TD>
297   <TD colspan=\"7\" align=\"center\">
298     <STRONG><SPAN class=\"guest_failed\">";
299                 switch ($_GET['login'])
300                 {
301                 case CODE_WRONG_PASS:
302                         $MSG .= LOGIN_WRONG_PASS;
303                         break;
304
305                 case CODE_WRONG_ID:
306                         $MSG .= LOGIN_WRONG_ID;
307                         break;
308
309                 case CODE_ID_LOCKED:
310                         $MSG .= LOGIN_ID_LOCKED;
311                         break;
312
313                 case CODE_ID_UNCONFIRMED:
314                         $MSG .= LOGIN_ID_UNCONFIRMED;
315                         break;
316
317                 case CODE_NO_COOKIES:
318                         $MSG .= LOGIN_NO_COOKIES;
319                         break;
320
321                 default:
322                         $MSG .= LOGIN_WRONG_ID;
323                         break;
324                 }
325                 $MSG .= "</SPAN></STRONG>
326   </TD>
327   <TD width=\"10\">&nbsp;</TD>
328 </TR>\n";
329                 define('LOGIN_FAILURE_MSG', $MSG);
330         }
331          else
332         {
333                 // No problems, no output
334                 define('LOGIN_FAILURE_MSG', "");
335         }
336         // Display login form with resend-password form
337         if (EXT_IS_ACTIVE("nickname"))
338         {
339                 LOAD_TEMPLATE("guest_nickname_login");
340         }
341          else
342         {
343                 LOAD_TEMPLATE("guest_login");
344         }
345 }
346
347 // Was an URL constructed?
348 if (!empty($URL)) {
349         // URL was constructed
350         if (!empty($FATAL[0]))
351         {
352                 // Fatal errors!
353                 require_once(PATH."inc/fatal_errors.php");
354         } else {
355                 // Load URL
356                 LOAD_URL($URL);
357         }
358 }
359
360 //
361 ?>