- Login failtures now counted and displayed after login in admin/member area
[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         // Already logged in?
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) {
57                 // Nickname entered
58                 $uid = SQL_ESCAPE($_POST['id']);
59         } else {
60                 // Direct userid entered
61                 $uid  = bigintval($_POST['id']);
62         }
63 } elseif (!empty($_POST['new_pass'])) {
64         // New password requested
65         $uid = 0;
66         if (!empty($_POST['id'])) $uid = $_POST['id'];
67 } else {
68         // Not logged in
69         $uid = 0; $hash = "";
70 }
71
72 $URL = ""; $ADD = "";
73 // Set unset variables
74 if (empty($_POST['new_pass'])) $_POST['new_pass'] = "";
75 if (empty($_GET['login']))     $_GET['login']     = "";
76
77 if (IS_MEMBER()) {
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
85         $LAST = "";
86         if (GET_EXT_VERSION("sql_patches") >= "0.2.8") {
87                 $LAST = ", last_login";
88         } // END - if
89
90         // Check login data
91         $password = "";
92         if ($probe_nickname === true) {
93                 // Nickname entered
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);
98         } else {
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);
103         }
104
105         // Is there an entry?
106         if (SQL_NUMROWS($result) == 1) {
107                 // By default the hash is empty
108                 $hash = "";
109
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... :)
113                         $hash = $password;
114                 } else {
115                         // Encrypt hash for comparsion
116                         $hash = generateHash($_POST['password'], substr($password, 0, -40));
117                 }
118
119                 if ($hash == $password) {
120                         // New hashed password found so let's generate a new one
121                         $hash = generateHash($_POST['password']);
122
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__);
126
127                         // No login bonus by default
128                         $BONUS = false;
129
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']."'";
136                                 $BONUS = true;
137
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');
140                         } // END - if
141
142
143                         // Secure lifetime from input form
144                         $l = bigintval($_POST['lifetime']);
145                         $life = "-1";
146                         if ($l > 0) {
147                                 // Calculate lifetime of cookies
148                                 $life = time() + $l;
149
150                                 // Calculate new hash with the secret key and master salt together
151                                 $hash = generatePassString($hash);
152
153                                 // Update cookies
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));
157
158                                 // Update global array
159                                 $GLOBALS['userid'] = $uid;
160                         } else {
161                                 // Check for login data
162                                 $login = IS_MEMBER();
163                         }
164
165                         if ($login) {
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";
174                                         } else {
175                                                 // Bonus not added
176                                                 $URL = URL."/modules.php?module=chk_login&mode=login";
177                                         }
178                                 } else {
179                                         // Cannot update counter!
180                                         $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".CODE_CNTR_FAILED;
181                                 }
182                         } else {
183                                 // Cookies not setable!
184                                 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".CODE_NO_COOKIES;
185                         }
186                 } else {
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__);
190
191                         // Wrong password!
192                         $ERROR = CODE_WRONG_PASS;
193                 }
194         } else {
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)
199                 {
200                         // Load status
201                         list($status) = SQL_FETCHROW($result);
202                         switch ($status)
203                         {
204                         case "LOCKED":
205                                 $ERROR = CODE_ID_LOCKED;
206                                 break;
207
208                         case "UNCONFIRMED":
209                                 $ERROR = CODE_ID_UNCONFIRMED;
210                                 break;
211
212                         default:
213                                 $ERROR = CODE_UNKNOWN_STATUS;
214                                 break;
215                         }
216                 } else {
217                         // ID not found!
218                         $ERROR = CODE_WRONG_ID;
219                 }
220
221                 // Construct URL
222                 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".$ERROR;
223         }
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']);
227
228         // Set ID number when left empty
229         if (empty($_POST['id'])) $_POST['id'] = 0;
230
231         // Probe userid/nickname
232         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
233         if ($probe_nickname) {
234                 // Nickname entered
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__);
237         } else {
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__);
241         }
242
243         // Any entry found?
244         if (SQL_NUMROWS($result) == 1) {
245                 // This data is valid, so we create a new pass... :-)
246                 list($uid, $status) = SQL_FETCHROW($result);
247
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__);
253
254                         // Prepare data and message for email
255                         $msg = LOAD_EMAIL_TEMPLATE("new-pass", array('new_pass' => $NEW_PASS), $uid);
256
257                         // ... and send it away
258                         SEND_EMAIL($uid, GUEST_NEW_PASSWORD, $msg);
259
260                         // Output note to user
261                         LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
262                 } else {
263                         // Account is locked or unconfirmed
264                         switch ($status) {
265                                 case "LOCKED"     : $MSG = CODE_ID_LOCKED;      break;
266                                 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
267                         }
268
269                         // Load URL
270                         LOAD_URL("modules.php?module=".$GLOBALS['module']."&amp;what=login&login=".$MSG);
271                 }
272         } else {
273                 // ID or email is wrong
274                 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
275         }
276 }
277
278 // Login problems?
279 if (!empty($_GET['login'])) {
280         // Use code from URL
281         $ERROR = SQL_ESCAPE($_GET['login']);
282 } // END  - if
283
284 // Login problems?
285 if (!empty($ERROR)) {
286         // Ok, which one now?
287         $MSG = "<TR>
288   <TD width=\"10\">&nbsp;</TD>
289   <TD colspan=\"7\" align=\"center\">
290     <STRONG><SPAN class=\"guest_failed\">";
291
292         switch ($ERROR) {
293                 case CODE_WRONG_PASS:
294                         $MSG .= LOGIN_WRONG_PASS;
295                         break;
296
297                 case CODE_WRONG_ID:
298                         $MSG .= LOGIN_WRONG_ID;
299                         break;
300
301                 case CODE_ID_LOCKED:
302                         $MSG .= LOGIN_ID_LOCKED;
303                         break;
304
305                 case CODE_ID_UNCONFIRMED:
306                         $MSG .= LOGIN_ID_UNCONFIRMED;
307                         break;
308
309                 case CODE_NO_COOKIES:
310                         $MSG .= LOGIN_NO_COOKIES;
311                         break;
312
313                 case CODE_EXTENSION_PROBLEM:
314                         if (IS_ADMIN()) {
315                                 $MSG .= sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "nickname");
316                         } else {
317                                 $MSG .= LOGIN_WRONG_ID;
318                         }
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 } else {
331         // No problems, no output
332         define('LOGIN_FAILURE_MSG', "");
333 }
334
335 // Display login form with resend-password form
336 if (EXT_IS_ACTIVE("nickname")) {
337         LOAD_TEMPLATE("guest_nickname_login");
338 } else {
339         LOAD_TEMPLATE("guest_login");
340 }
341
342 // Was an URL constructed?
343 if (!empty($URL)) {
344         // URL was constructed
345         if (!empty($FATAL[0])) {
346                 // Fatal errors!
347                 require_once(PATH."inc/fatal_errors.php");
348         } else {
349                 // Load URL
350                 LOAD_URL($URL);
351         }
352 } // END - if
353
354 //
355 ?>