2 /************************************************************************
3 * MXChange v0.2.1 Start: 10/14/2003 *
4 * =============== Last change: 04/28/2004 *
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 *
13 * -------------------------------------------------------------------- *
15 * -------------------------------------------------------------------- *
16 * Copyright (c) 2003 - 2008 by Roland Haeder *
17 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
33 ************************************************************************/
35 // Some security stuff...
36 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
38 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
42 // Add description as navigation point
43 ADD_DESCR("guest", basename(__FILE__));
45 OPEN_TABLE("100%", "guest_content_align", "");
49 $probe_nickname = false; $UID = false; $hash = "";
50 unset($login); unset($online);
52 if ((!empty($GLOBALS['userid'])) && (!empty($_SESSION['u_hash'])))
55 $UID = $GLOBALS['userid'];
57 elseif ((!empty($_POST['id'])) && (!empty($_POST['password'])) && (isset($_POST['ok'])))
59 // Set userid and crypt password when login data was submitted
60 $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
64 $UID = SQL_ESCAPE($_POST['id']);
68 // Direct userid entered
69 $UID = bigintval($_POST['id']);
72 elseif (!empty($_POST['new_pass']))
74 // New password requested
76 if (!empty($_POST['id'])) $UID = $_POST['id'];
81 $UID = "0"; $hash = "";
85 // Set unset variables
86 if (empty($_POST['new_pass'])) $_POST['new_pass'] = "";
87 if (empty($_GET['login'])) $_GET['login'] = "";
91 // Login immidiately...
92 $URL = URL."/modules.php?module=login";
94 elseif (isset($_POST['ok']))
96 // Add last_login if available
98 if (GET_EXT_VERSION("sql_patches") >= "0.2.8")
100 $LAST = ", last_login";
108 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$LAST." FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' AND status='CONFIRMED' LIMIT 1",
109 array($UID), __FILE__, __LINE__);
110 list($UID2, $password, $online, $login) = SQL_FETCHROW($result);
111 if (!empty($UID2)) $UID = $UID2;
115 // Direct userid entered
116 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$LAST." FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d AND status='CONFIRMED' LIMIT 1",
117 array(bigintval($UID), $hash), __FILE__, __LINE__);
118 list($dmy, $password, $online, $login) = SQL_FETCHROW($result);
120 if (SQL_NUMROWS($result) == 1)
122 // Valid data found so let's load the last login data
123 if (isset($_POST['ok']))
125 // By default the hash is empty
128 // Check for old MD5 passwords
129 if ((strlen($password) == 32) && (md5($_POST['password']) == $password))
131 // Just set the hash to the password from DB... :)
136 // Encrypt hash for comparsion
137 $hash = generateHash($_POST['password'], substr($password, 0, -40));
140 if ($hash == $password)
142 // New hashed password found so let's generate a new one
143 $hash = generateHash($_POST['password']);
145 // ... and update database
146 $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%d AND status='CONFIRMED' LIMIT 1",
147 array($hash, $UID), __FILE__, __LINE__);
149 // No login bonus by default
152 // Probe for last online timemark
153 $probe = time() - $online;
154 if (!empty($login)) $probe = time() - $login;
155 if ((GET_EXT_VERSION("bonus") >= "0.2.2") && ($probe >= $_CONFIG['login_timeout']))
157 // Add login bonus to user's account
158 $ADD = ", login_bonus=login_bonus+'".$_CONFIG['login_bonus']."'";
161 // Subtract login bonus from userid's account or jackpot
162 if ((GET_EXT_VERSION("bonus") >= "0.3.5") && ($_CONFIG['bonus_mode'] != "ADD")) BONUS_POINTS_HANDLER('login_bonus');
166 // Secure lifetime from input form
167 $l = bigintval($_POST['lifetime']);
171 // Calculate lifetime of cookies
174 // Calculate new hash with the secret key and master salt together
175 $hash = generatePassString($hash);
178 $login = (set_session("userid" , $UID , $life, COOKIE_PATH)
179 && set_session("u_hash" , $hash, $life, COOKIE_PATH)
180 && set_session("lifetime", $l , $life, COOKIE_PATH));
182 // Update global array
183 $GLOBALS['userid'] = $UID;
184 $_SESSION['u_hash'] = $hash;
185 $_SESSION['lifetime'] = $l;
189 // Check for login data
190 $login = IS_LOGGED_IN();
195 // Update database records
196 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET total_logins=total_logins+1".$ADD." WHERE userid=%d LIMIT 1",
197 array(bigintval($UID)), __FILE__, __LINE__);
198 if (SQL_AFFECTEDROWS($link) == 1)
200 // Procedure to checking for login data
201 if (($BONUS) && (EXT_IS_ACTIVE("bonus")))
203 // Bonus added (just displaying!)
204 $URL = URL."/modules.php?module=chk_login&mode=bonus";
209 $URL = URL."/modules.php?module=chk_login&mode=login";
214 // Cannot update counter!
215 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".CODE_CNTR_FAILED;
220 // Cookies not setable!
221 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".CODE_NO_COOKIES;
227 $ERROR = CODE_WRONG_PASS;
233 $ERROR = CODE_LOGIN_FAILED;
238 // Other account status?
239 $result = SQL_QUERY_ESC("SELECT status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1",
240 array(bigintval($UID)), __FILE__, __LINE__);
241 if (SQL_NUMROWS($result) == 1)
244 list($status) = SQL_FETCHROW($result);
248 $ERROR = CODE_ID_LOCKED;
252 $ERROR = CODE_ID_UNCONFIRMED;
256 $ERROR = CODE_UNKNOWN_STATUS;
263 $ERROR = CODE_WRONG_ID;
267 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".$ERROR;
270 elseif ((!empty($_POST['new_pass'])) && (isset($UID)))
272 // Compile email when found in address (only secure chars!)
273 if (!empty($_POST['email'])) $_POST['email'] = str_replace("{DOT}", '.', $_POST['email']);
275 // Set ID number when left empty
276 if (empty($_POST['id'])) $_POST['id'] = "0";
278 // Probe userid/nickname
279 $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
283 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' OR email='%s' LIMIT 1",
284 array(addslashes($UID), $_POST['email']), __FILE__, __LINE__);
288 // Direct userid entered
289 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d OR email='%s' LIMIT 1",
290 array(bigintval($UID), $_POST['email']), __FILE__, __LINE__);
292 if (SQL_NUMROWS($result) == 1)
294 // This data is valid, so we create a new pass... :-)
295 list($UID, $status) = SQL_FETCHROW($result);
297 if ($status == "CONFIRMED")
299 // Ooppps, this was missing! ;-) We should update the database...
300 $NEW_PASS = GEN_PASS();
301 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%d LIMIT 1",
302 array(generateHash($NEW_PASS), bigintval($UID)), __FILE__, __LINE__);
304 // Prepare data and message for email
305 $DATA = array($NEW_PASS, getenv('REMOTE_ADDR'));
306 $msg = LOAD_EMAIL_TEMPLATE("new-pass", "", bigintval($UID));
308 // ... and send it away
309 SEND_EMAIL(bigintval($UID), GUEST_NEW_PASSWORD, $msg);
311 // Output note to user
312 LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
316 // Account is locked or unconfirmed
319 case "LOCKED" : $MSG = CODE_ID_LOCKED; break;
320 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
324 LOAD_URL("modules.php?module=".$GLOBALS['module']."&what=login&login=".$MSG);
329 // ID or email is wrong
330 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
336 if (!empty($_GET['login']))
338 // Ok, which one now?
340 <TD width=\"10\"> </TD>
341 <TD colspan=\"7\" align=\"center\">
342 <STRONG><SPAN class=\"guest_failed\">";
343 switch ($_GET['login'])
345 case CODE_WRONG_PASS:
346 $MSG .= LOGIN_WRONG_PASS;
350 $MSG .= LOGIN_WRONG_ID;
354 $MSG .= LOGIN_ID_LOCKED;
357 case CODE_ID_UNCONFIRMED:
358 $MSG .= LOGIN_ID_UNCONFIRMED;
361 case CODE_NO_COOKIES:
362 $MSG .= LOGIN_NO_COOKIES;
366 $MSG .= LOGIN_WRONG_ID;
369 $MSG .= "</SPAN></STRONG>
371 <TD width=\"10\"> </TD>
373 define ('LOGIN_FAILURE_MSG', $MSG);
377 // No problems, no output
378 define ('LOGIN_FAILURE_MSG', "");
380 // Display login form with resend-password form
381 if (EXT_IS_ACTIVE("nickname"))
383 LOAD_TEMPLATE("guest_nickname_login");
387 LOAD_TEMPLATE("guest_login");
391 // Was an URL constructed?
394 // URL was constructed
395 if (!empty($FATAL[0]))
398 require_once(PATH."inc/fatal_errors.php");