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", __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")) && (("".($_POST['id'] + 0)."") != $_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";
91 $password = ""; $uid2 = ""; $dmy = "";
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($uid2, $password, $online, $login) = SQL_FETCHROW($result);
105 // Is there an entry?
106 if ((SQL_NUMROWS($result) == 1) && ((($probe_nickname) && (!empty($uid2))) || ($uid2 == $uid))) {
108 SQL_FREERESULT($result);
110 // By default the hash is empty
113 // Check for old MD5 passwords
114 if ((strlen($password) == 32) && (md5($_POST['password']) == $password)) {
115 // Just set the hash to the password from DB... :)
118 // Hash password with improved way for comparsion
119 $hash = generateHash($_POST['password'], substr($password, 0, -40));
122 if ($hash == $password) {
123 // New hashed password found so let's generate a new one
124 $hash = generateHash($_POST['password']);
126 // ... and update database
127 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
128 array($hash, $uid), __FILE__, __LINE__);
130 // No login bonus by default
133 // Probe for last online timemark
134 $probe = time() - $online;
135 if (!empty($login)) $probe = time() - $login;
136 //if ((GET_EXT_VERSION("bonus") >= "0.2.2") && ($probe >= $_CONFIG['login_timeout'])) {
138 // Add login bonus to user's account
139 $ADD = sprintf(", login_bonus=login_bonus+%s",
140 (float)$_CONFIG['login_bonus']
144 // Subtract login bonus from userid's account or jackpot
145 if ((GET_EXT_VERSION("bonus") >= "0.3.5") && ($_CONFIG['bonus_mode'] != "ADD")) BONUS_POINTS_HANDLER('login_bonus');
149 $life = "-1"; $login = false;
151 // Secure lifetime from input form
152 $l = bigintval($_POST['lifetime']);
154 // Is the lifetime set?
156 // Calculate lifetime of cookies
159 // Calculate new hash with the secret key and master salt together
160 $hash = generatePassString($hash);
163 $login = (set_session("userid" , $uid , $life, COOKIE_PATH)
164 && set_session("u_hash" , $hash, $life, COOKIE_PATH)
165 && set_session("lifetime", $l , $life, COOKIE_PATH)
168 // Update global array
169 $GLOBALS['userid'] = $uid;
171 // Check for login data
172 $login = IS_MEMBER();
176 // Update database records
177 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET total_logins=total_logins+1".$ADD." WHERE userid=%s LIMIT 1",
178 array($uid), __FILE__, __LINE__);
179 if (SQL_AFFECTEDROWS() == 1) {
180 // Procedure to checking for login data
181 if (($BONUS) && (EXT_IS_ACTIVE("bonus"))) {
182 // Bonus added (just displaying!)
183 $URL = URL."/modules.php?module=chk_login&mode=bonus";
186 $URL = URL."/modules.php?module=chk_login&mode=login";
189 // Cannot update counter!
190 $URL = URL."/modules.php?module=index&what=login&login=".CODE_CNTR_FAILED;
193 // Cookies not setable!
194 $URL = URL."/modules.php?module=index&what=login&login=".CODE_NO_COOKIES;
196 } elseif (GET_EXT_VERSION("sql_patches") >= "0.4.7") {
197 // Update failture counter
198 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET login_failtures=login_failtures+1,last_failture=NOW() WHERE userid=%s LIMIT 1",
199 array($uid), __FILE__, __LINE__);
202 $ERROR = CODE_WRONG_PASS;
204 } elseif ((($probe_nickname) && (!empty($uid2))) || ($uid2 == $uid)) {
205 // Other account status?
206 $result = SQL_QUERY_ESC("SELECT status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
207 array($uid), __FILE__, __LINE__);
210 if (SQL_NUMROWS($result) == 1) {
212 list($status) = SQL_FETCHROW($result);
215 $ERROR = CODE_ID_LOCKED;
219 $ERROR = CODE_ID_UNCONFIRMED;
223 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown error status %s detected.", $status));
224 $ERROR = CODE_UNKNOWN_STATUS;
229 $ERROR = CODE_WRONG_ID;
233 $URL = URL."/modules.php?module=index&what=login&login=".$ERROR;
236 $ERROR = CODE_WRONG_ID;
238 } elseif ((!empty($_POST['new_pass'])) && (isset($uid))) {
239 // Compile email when found in address (only secure chars!)
240 if (!empty($_POST['email'])) $_POST['email'] = str_replace("{DOT}", '.', $_POST['email']);
242 // Set ID number when left empty
243 if (empty($_POST['id'])) $_POST['id'] = 0;
245 // Probe userid/nickname
246 $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
247 if ($probe_nickname) {
249 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' OR email='%s' LIMIT 1",
250 array($uid, $_POST['email']), __FILE__, __LINE__);
252 // Direct userid entered
253 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s OR email='%s' LIMIT 1",
254 array(bigintval($uid), $_POST['email']), __FILE__, __LINE__);
258 if (SQL_NUMROWS($result) == 1) {
259 // This data is valid, so we create a new pass... :-)
260 list($uid, $status) = SQL_FETCHROW($result);
262 if ($status == "CONFIRMED") {
263 // Ooppps, this was missing! ;-) We should update the database...
264 $NEW_PASS = GEN_PASS();
265 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%s LIMIT 1",
266 array(generateHash($NEW_PASS), $uid), __FILE__, __LINE__);
268 // Prepare data and message for email
269 $msg = LOAD_EMAIL_TEMPLATE("new-pass", array('new_pass' => $NEW_PASS), $uid);
271 // ... and send it away
272 SEND_EMAIL($uid, GUEST_NEW_PASSWORD, $msg);
274 // Output note to user
275 LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
277 // Account is locked or unconfirmed
279 case "LOCKED" : $MSG = CODE_ID_LOCKED; break;
280 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
284 LOAD_URL("modules.php?module=index&what=login&login=".$MSG);
287 // ID or email is wrong
288 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
293 if (!empty($_GET['login'])) {
295 $ERROR = SQL_ESCAPE($_GET['login']);
299 if (!empty($ERROR)) {
300 // Ok, which one now?
302 <TD width=\"10\"> </TD>
303 <TD colspan=\"7\" align=\"center\">
304 <STRONG><SPAN class=\"guest_failed\">";
307 case CODE_WRONG_PASS:
308 $MSG .= LOGIN_WRONG_PASS;
312 $MSG .= LOGIN_WRONG_ID;
316 $MSG .= LOGIN_ID_LOCKED;
319 case CODE_ID_UNCONFIRMED:
320 $MSG .= LOGIN_ID_UNCONFIRMED;
323 case CODE_NO_COOKIES:
324 $MSG .= LOGIN_NO_COOKIES;
327 case CODE_EXTENSION_PROBLEM:
329 $MSG .= sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "nickname");
331 $MSG .= LOGIN_WRONG_ID;
336 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unhandled error code %s detected.", $ERROR));
337 $MSG .= LOGIN_WRONG_ID;
340 $MSG .= "</SPAN></STRONG>
342 <TD width=\"10\"> </TD>
344 define('LOGIN_FAILURE_MSG', $MSG);
346 // No problems, no output
347 define('LOGIN_FAILURE_MSG', "");
350 // Display login form with resend-password form
351 if (EXT_IS_ACTIVE("nickname")) {
352 LOAD_TEMPLATE("guest_nickname_login");
354 LOAD_TEMPLATE("guest_login");
357 // Was an URL constructed?
359 // URL was constructed
360 if (!empty($FATAL[0])) {
362 require_once(PATH."inc/fatal_errors.php");