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 (!defined('__SECURITY')) {
37 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
41 // Add description as navigation point
42 ADD_DESCR("guest", basename(__FILE__));
47 $probe_nickname = false; $uid = false; $hash = "";
48 unset($login); unset($online);
50 if ((!empty($GLOBALS['userid'])) && (isSessionVariableSet('u_hash'))) {
52 $uid = $GLOBALS['userid'];
53 } elseif ((!empty($_POST['id'])) && (!empty($_POST['password'])) && (isset($_POST['ok']))) {
54 // Set userid and crypt password when login data was submitted
55 $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".bigintval($_POST['id'])."") != $_POST['id']));
56 if ($probe_nickname === true) {
58 $uid = SQL_ESCAPE($_POST['id']);
60 // Direct userid entered
61 $uid = bigintval($_POST['id']);
63 } elseif (!empty($_POST['new_pass'])) {
64 // New password requested
66 if (!empty($_POST['id'])) $uid = $_POST['id'];
73 // Set unset variables
74 if (empty($_POST['new_pass'])) $_POST['new_pass'] = "";
75 if (empty($_GET['login'])) $_GET['login'] = "";
78 // Login immidiately...
79 $URL = URL."/modules.php?module=login";
80 } elseif ((isset($_POST['ok'])) && ("".$uid."" != "".$_POST['id']."")) {
81 // Invalid input (no nickname extension installed but nickname entered)
82 $ERROR = CODE_EXTENSION_PROBLEM;
83 } elseif (isset($_POST['ok'])) {
84 // Add last_login if available
86 if (GET_EXT_VERSION("sql_patches") >= "0.2.8") {
87 $LAST = ", last_login";
92 if ($probe_nickname === true) {
94 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$LAST." FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' AND status='CONFIRMED' LIMIT 1",
95 array($uid), __FILE__, __LINE__);
96 list($uid2, $password, $online, $login) = SQL_FETCHROW($result);
97 if (!empty($uid2)) $uid = bigintval($uid2);
99 // Direct userid entered
100 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$LAST." FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
101 array($uid, $hash), __FILE__, __LINE__);
102 list($dmy, $password, $online, $login) = SQL_FETCHROW($result);
105 // Is there an entry?
106 if (SQL_NUMROWS($result) == 1) {
107 // By default the hash is empty
110 // Check for old MD5 passwords
111 if ((strlen($password) == 32) && (md5($_POST['password']) == $password)) {
112 // Just set the hash to the password from DB... :)
115 // Encrypt hash for comparsion
116 $hash = generateHash($_POST['password'], substr($password, 0, -40));
119 if ($hash == $password) {
120 // New hashed password found so let's generate a new one
121 $hash = generateHash($_POST['password']);
123 // ... and update database
124 $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
125 array($hash, $uid), __FILE__, __LINE__);
127 // No login bonus by default
130 // Probe for last online timemark
131 $probe = time() - $online;
132 if (!empty($login)) $probe = time() - $login;
133 if ((GET_EXT_VERSION("bonus") >= "0.2.2") && ($probe >= $_CONFIG['login_timeout'])) {
134 // Add login bonus to user's account
135 $ADD = ", login_bonus=login_bonus+'".$_CONFIG['login_bonus']."'";
138 // Subtract login bonus from userid's account or jackpot
139 if ((GET_EXT_VERSION("bonus") >= "0.3.5") && ($_CONFIG['bonus_mode'] != "ADD")) BONUS_POINTS_HANDLER('login_bonus');
143 // Secure lifetime from input form
144 $l = bigintval($_POST['lifetime']);
147 // Calculate lifetime of cookies
150 // Calculate new hash with the secret key and master salt together
151 $hash = generatePassString($hash);
154 $login = (set_session("userid" , $uid , $life, COOKIE_PATH)
155 && set_session("u_hash" , $hash, $life, COOKIE_PATH)
156 && set_session("lifetime", $l , $life, COOKIE_PATH));
158 // Update global array
159 $GLOBALS['userid'] = $uid;
161 // Check for login data
162 $login = IS_MEMBER();
166 // Update database records
167 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET total_logins=total_logins+1".$ADD." WHERE userid=%s LIMIT 1",
168 array($uid), __FILE__, __LINE__);
169 if (SQL_AFFECTEDROWS() == 1) {
170 // Procedure to checking for login data
171 if (($BONUS) && (EXT_IS_ACTIVE("bonus"))) {
172 // Bonus added (just displaying!)
173 $URL = URL."/modules.php?module=chk_login&mode=bonus";
176 $URL = URL."/modules.php?module=chk_login&mode=login";
179 // Cannot update counter!
180 $URL = URL."/modules.php?module=index&what=login&login=".CODE_CNTR_FAILED;
183 // Cookies not setable!
184 $URL = URL."/modules.php?module=index&what=login&login=".CODE_NO_COOKIES;
187 // Update failture counter
188 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET login_failtures=login_failtures+1,last_failture=NOW() WHERE userid=%s LIMIT 1",
189 array($uid), __FILE__, __LINE__);
192 $ERROR = CODE_WRONG_PASS;
195 // Other account status?
196 $result = SQL_QUERY_ESC("SELECT status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
197 array($uid), __FILE__, __LINE__);
198 if (SQL_NUMROWS($result) == 1)
201 list($status) = SQL_FETCHROW($result);
205 $ERROR = CODE_ID_LOCKED;
209 $ERROR = CODE_ID_UNCONFIRMED;
213 $ERROR = CODE_UNKNOWN_STATUS;
218 $ERROR = CODE_WRONG_ID;
222 $URL = URL."/modules.php?module=index&what=login&login=".$ERROR;
224 } elseif ((!empty($_POST['new_pass'])) && (isset($uid))) {
225 // Compile email when found in address (only secure chars!)
226 if (!empty($_POST['email'])) $_POST['email'] = str_replace("{DOT}", '.', $_POST['email']);
228 // Set ID number when left empty
229 if (empty($_POST['id'])) $_POST['id'] = 0;
231 // Probe userid/nickname
232 $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
233 if ($probe_nickname) {
235 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' OR email='%s' LIMIT 1",
236 array(addslashes($uid), $_POST['email']), __FILE__, __LINE__);
238 // Direct userid entered
239 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s OR email='%s' LIMIT 1",
240 array($uid, $_POST['email']), __FILE__, __LINE__);
244 if (SQL_NUMROWS($result) == 1) {
245 // This data is valid, so we create a new pass... :-)
246 list($uid, $status) = SQL_FETCHROW($result);
248 if ($status == "CONFIRMED") {
249 // Ooppps, this was missing! ;-) We should update the database...
250 $NEW_PASS = GEN_PASS();
251 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%s LIMIT 1",
252 array(generateHash($NEW_PASS), $uid), __FILE__, __LINE__);
254 // Prepare data and message for email
255 $msg = LOAD_EMAIL_TEMPLATE("new-pass", array('new_pass' => $NEW_PASS), $uid);
257 // ... and send it away
258 SEND_EMAIL($uid, GUEST_NEW_PASSWORD, $msg);
260 // Output note to user
261 LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
263 // Account is locked or unconfirmed
265 case "LOCKED" : $MSG = CODE_ID_LOCKED; break;
266 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
270 LOAD_URL("modules.php?module=index&what=login&login=".$MSG);
273 // ID or email is wrong
274 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
279 if (!empty($_GET['login'])) {
281 $ERROR = SQL_ESCAPE($_GET['login']);
285 if (!empty($ERROR)) {
286 // Ok, which one now?
288 <TD width=\"10\"> </TD>
289 <TD colspan=\"7\" align=\"center\">
290 <STRONG><SPAN class=\"guest_failed\">";
293 case CODE_WRONG_PASS:
294 $MSG .= LOGIN_WRONG_PASS;
298 $MSG .= LOGIN_WRONG_ID;
302 $MSG .= LOGIN_ID_LOCKED;
305 case CODE_ID_UNCONFIRMED:
306 $MSG .= LOGIN_ID_UNCONFIRMED;
309 case CODE_NO_COOKIES:
310 $MSG .= LOGIN_NO_COOKIES;
313 case CODE_EXTENSION_PROBLEM:
315 $MSG .= sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "nickname");
317 $MSG .= LOGIN_WRONG_ID;
322 $MSG .= LOGIN_WRONG_ID;
325 $MSG .= "</SPAN></STRONG>
327 <TD width=\"10\"> </TD>
329 define('LOGIN_FAILURE_MSG', $MSG);
331 // No problems, no output
332 define('LOGIN_FAILURE_MSG', "");
335 // Display login form with resend-password form
336 if (EXT_IS_ACTIVE("nickname")) {
337 LOAD_TEMPLATE("guest_nickname_login");
339 LOAD_TEMPLATE("guest_login");
342 // Was an URL constructed?
344 // URL was constructed
345 if (!empty($FATAL[0])) {
347 require_once(PATH."inc/fatal_errors.php");