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 >= getConfig('login_timeout'))) {
137 // Add login bonus to user's account
138 $ADD = sprintf(", login_bonus=login_bonus+%s",
139 (float)getConfig('login_bonus')
143 // Subtract login bonus from userid's account or jackpot
144 if ((GET_EXT_VERSION("bonus") >= "0.3.5") && (getConfig('bonus_mode') != "ADD")) BONUS_POINTS_HANDLER('login_bonus');
148 $life = "-1"; $login = false;
150 // Secure lifetime from input form
151 $l = bigintval($_POST['lifetime']);
153 // Is the lifetime set?
155 // Calculate lifetime of cookies
158 // Calculate new hash with the secret key and master salt together
159 $hash = generatePassString($hash);
162 $login = (set_session("userid" , $uid , $life, COOKIE_PATH)
163 && set_session("u_hash" , $hash, $life, COOKIE_PATH)
164 && set_session("lifetime", $l , $life, COOKIE_PATH)
167 // Update global array
168 $GLOBALS['userid'] = $uid;
170 // Check for login data
171 $login = IS_MEMBER();
175 // Update database records
176 $result = SQL_QUERY_ESC("UPDATE `"._MYSQL_PREFIX."_user_data` SET total_logins=total_logins+1".$ADD." WHERE userid=%s LIMIT 1",
177 array($uid), __FILE__, __LINE__);
178 if (SQL_AFFECTEDROWS() == 1) {
179 // Procedure to checking for login data
180 if (($BONUS) && (EXT_IS_ACTIVE("bonus"))) {
181 // Bonus added (just displaying!)
182 $URL = URL."/modules.php?module=chk_login&mode=bonus";
185 $URL = URL."/modules.php?module=chk_login&mode=login";
188 // Cannot update counter!
189 $URL = URL."/modules.php?module=index&what=login&login=".CODE_CNTR_FAILED;
192 // Cookies not setable!
193 $URL = URL."/modules.php?module=index&what=login&login=".CODE_NO_COOKIES;
195 } elseif (GET_EXT_VERSION("sql_patches") >= "0.4.7") {
196 // Update failture counter
197 SQL_QUERY_ESC("UPDATE `"._MYSQL_PREFIX."_user_data` SET login_failtures=login_failtures+1,last_failture=NOW() WHERE userid=%s LIMIT 1",
198 array($uid), __FILE__, __LINE__);
201 $ERROR = CODE_WRONG_PASS;
203 } elseif ((($probe_nickname) && (!empty($uid2))) || ($uid2 == $uid)) {
204 // Other account status?
205 $result = SQL_QUERY_ESC("SELECT status FROM `"._MYSQL_PREFIX."_user_data` WHERE userid=%s LIMIT 1",
206 array($uid), __FILE__, __LINE__);
209 if (SQL_NUMROWS($result) == 1) {
211 list($status) = SQL_FETCHROW($result);
214 $ERROR = CODE_ID_LOCKED;
218 $ERROR = CODE_ID_UNCONFIRMED;
222 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown error status %s detected.", $status));
223 $ERROR = CODE_UNKNOWN_STATUS;
228 $ERROR = CODE_WRONG_ID;
232 $URL = URL."/modules.php?module=index&what=login&login=".$ERROR;
235 $ERROR = CODE_WRONG_ID;
237 } elseif ((!empty($_POST['new_pass'])) && (isset($uid))) {
238 // Compile email when found in address (only secure chars!)
239 if (!empty($_POST['email'])) $_POST['email'] = str_replace("{DOT}", '.', $_POST['email']);
241 // Set ID number when left empty
242 if (empty($_POST['id'])) $_POST['id'] = 0;
244 // Probe userid/nickname
245 $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
246 if ($probe_nickname) {
248 $result = SQL_QUERY_ESC("SELECT userid, status FROM `"._MYSQL_PREFIX."_user_data` WHERE nickname='%s' OR email='%s' LIMIT 1",
249 array($uid, $_POST['email']), __FILE__, __LINE__);
251 // Direct userid entered
252 $result = SQL_QUERY_ESC("SELECT userid, status FROM `"._MYSQL_PREFIX."_user_data` WHERE userid=%s OR email='%s' LIMIT 1",
253 array(bigintval($uid), $_POST['email']), __FILE__, __LINE__);
257 if (SQL_NUMROWS($result) == 1) {
258 // This data is valid, so we create a new pass... :-)
259 list($uid, $status) = SQL_FETCHROW($result);
261 if ($status == "CONFIRMED") {
262 // Ooppps, this was missing! ;-) We should update the database...
263 $NEW_PASS = GEN_PASS();
264 $result = SQL_QUERY_ESC("UPDATE `"._MYSQL_PREFIX."_user_data` SET password='%s' WHERE userid=%s LIMIT 1",
265 array(generateHash($NEW_PASS), $uid), __FILE__, __LINE__);
267 // Prepare data and message for email
268 $msg = LOAD_EMAIL_TEMPLATE("new-pass", array('new_pass' => $NEW_PASS), $uid);
270 // ... and send it away
271 SEND_EMAIL($uid, GUEST_NEW_PASSWORD, $msg);
273 // Output note to user
274 LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
276 // Account is locked or unconfirmed
278 case "LOCKED" : $MSG = CODE_ID_LOCKED; break;
279 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
283 LOAD_URL("modules.php?module=index&what=login&login=".$MSG);
286 // ID or email is wrong
287 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
292 if (!empty($_GET['login'])) {
294 $ERROR = SQL_ESCAPE($_GET['login']);
298 if (!empty($ERROR)) {
299 // Ok, which one now?
301 <TD width=\"10\"> </TD>
302 <TD colspan=\"7\" align=\"center\">
303 <STRONG><SPAN class=\"guest_failed\">";
306 case CODE_WRONG_PASS:
307 $MSG .= LOGIN_WRONG_PASS;
311 $MSG .= LOGIN_WRONG_ID;
315 $MSG .= LOGIN_ID_LOCKED;
318 case CODE_ID_UNCONFIRMED:
319 $MSG .= LOGIN_ID_UNCONFIRMED;
322 case CODE_NO_COOKIES:
323 $MSG .= LOGIN_NO_COOKIES;
326 case CODE_EXTENSION_PROBLEM:
328 $MSG .= sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "nickname");
330 $MSG .= LOGIN_WRONG_ID;
335 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unhandled error code %s detected.", $ERROR));
336 $MSG .= LOGIN_WRONG_ID;
339 $MSG .= "</SPAN></STRONG>
341 <TD width=\"10\"> </TD>
343 define('LOGIN_FAILURE_MSG', $MSG);
345 // No problems, no output
346 define('LOGIN_FAILURE_MSG', "");
349 // Display login form with resend-password form
350 if (EXT_IS_ACTIVE("nickname")) {
351 LOAD_TEMPLATE("guest_nickname_login");
353 LOAD_TEMPLATE("guest_login");
356 // Was an URL constructed?
358 // URL was constructed
359 if (!empty($FATAL[0])) {
361 require_once(PATH."inc/fatal_errors.php");