Fix in CREATE_EXTENSION_DEACTIVATION_TASK() and coding-style updated
[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 (!defined('__SECURITY')) {
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", __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")) && (("".($_POST['id'] + 0)."") != $_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 = ""; $uid2 = ""; $dmy = "";
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($uid2, $password, $online, $login) = SQL_FETCHROW($result);
103         }
104
105         // Is there an entry?
106         if ((SQL_NUMROWS($result) == 1) && ((($probe_nickname) && (!empty($uid2))) || ($uid2 == $uid))) {
107                 // Free result
108                 SQL_FREERESULT($result);
109
110                 // By default the hash is empty
111                 $hash = "";
112
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... :)
116                         $hash = $password;
117                 } else {
118                         // Hash password with improved way for comparsion
119                         $hash = generateHash($_POST['password'], substr($password, 0, -40));
120                 }
121
122                 if ($hash == $password) {
123                         // New hashed password found so let's generate a new one
124                         $hash = generateHash($_POST['password']);
125
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__);
129
130                         // No login bonus by default
131                         $BONUS = false;
132
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')
140                                 );
141                                 $BONUS = true;
142
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');
145                         } // END - if
146
147                         // Init variables
148                         $life = "-1"; $login = false;
149
150                         // Secure lifetime from input form
151                         $l = bigintval($_POST['lifetime']);
152
153                         // Is the lifetime set?
154                         if ($l > 0) {
155                                 // Calculate lifetime of cookies
156                                 $life = time() + $l;
157
158                                 // Calculate new hash with the secret key and master salt together
159                                 $hash = generatePassString($hash);
160
161                                 // Update cookies
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)
165                                 );
166
167                                 // Update global array
168                                 $GLOBALS['userid'] = $uid;
169                         } else {
170                                 // Check for login data
171                                 $login = IS_MEMBER();
172                         }
173
174                         if ($login) {
175                                 // Update database records
176                                 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&amp;mode=bonus";
183                                         } else {
184                                                 // Bonus not added
185                                                 $URL = URL."/modules.php?module=chk_login&amp;mode=login";
186                                         }
187                                 } else {
188                                         // Cannot update counter!
189                                         $URL = URL."/modules.php?module=index&amp;what=login&amp;login=".CODE_CNTR_FAILED;
190                                 }
191                         } else {
192                                 // Cookies not setable!
193                                 $URL = URL."/modules.php?module=index&amp;what=login&amp;login=".CODE_NO_COOKIES;
194                         }
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__);
199
200                         // Wrong password!
201                         $ERROR = CODE_WRONG_PASS;
202                 }
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__);
207
208                 // Entry found?
209                 if (SQL_NUMROWS($result) == 1) {
210                         // Load status
211                         list($status) = SQL_FETCHROW($result);
212                         switch ($status) {
213                                 case "LOCKED":
214                                         $ERROR = CODE_ID_LOCKED;
215                                         break;
216
217                                 case "UNCONFIRMED":
218                                         $ERROR = CODE_ID_UNCONFIRMED;
219                                         break;
220
221                                 default:
222                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown error status %s detected.", $status));
223                                         $ERROR = CODE_UNKNOWN_STATUS;
224                                         break;
225                         }
226                 } else {
227                         // ID not found!
228                         $ERROR = CODE_WRONG_ID;
229                 }
230
231                 // Construct URL
232                 $URL = URL."/modules.php?module=index&amp;what=login&amp;login=".$ERROR;
233         } else {
234                 // ID not found!
235                 $ERROR = CODE_WRONG_ID;
236         }
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']);
240
241         // Set ID number when left empty
242         if (empty($_POST['id'])) $_POST['id'] = 0;
243
244         // Init result
245         $result = false;
246
247         // Probe userid/nickname
248         if ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id'])) {
249                 // Nickname entered
250                 $result = SQL_QUERY_ESC("SELECT userid, status FROM `"._MYSQL_PREFIX."_user_data` WHERE nickname='%s' OR email='%s' LIMIT 1",
251                         array($uid, $_POST['email']), __FILE__, __LINE__);
252         } elseif (($uid > 0) && (empty($_POST['email']))) {
253                 // Direct userid entered
254                 $result = SQL_QUERY_ESC("SELECT userid, status FROM `"._MYSQL_PREFIX."_user_data` WHERE userid=%s LIMIT 1",
255                         array(bigintval($uid)), __FILE__, __LINE__);
256         } elseif (!empty($_POST['email'])) {
257                 // Email entered
258                 $result = SQL_QUERY_ESC("SELECT userid, status FROM `"._MYSQL_PREFIX."_user_data` WHERE email='%s' LIMIT 1",
259                         array($_POST['email']), __FILE__, __LINE__);
260         } else {
261                 // Userid not set!
262                 DEBUG_LOG(__FILE__, __LINE__, "Userid is not set! BUG!");
263                 $ERROR = CODE_WRONG_ID;
264         }
265
266         // Any entry found?
267         if (SQL_NUMROWS($result) == 1) {
268                 // This data is valid, so we create a new pass... :-)
269                 list($uid, $status) = SQL_FETCHROW($result);
270
271                 if ($status == "CONFIRMED") {
272                         // Ooppps, this was missing! ;-) We should update the database...
273                         $NEW_PASS = GEN_PASS();
274                         SQL_QUERY_ESC("UPDATE `"._MYSQL_PREFIX."_user_data` SET password='%s' WHERE userid=%s LIMIT 1",
275                                 array(generateHash($NEW_PASS), $uid), __FILE__, __LINE__);
276
277                         // Prepare data and message for email
278                         $msg = LOAD_EMAIL_TEMPLATE("new-pass", array('new_pass' => $NEW_PASS), $uid);
279
280                         // ... and send it away
281                         SEND_EMAIL($uid, GUEST_NEW_PASSWORD, $msg);
282
283                         // Output note to user
284                         LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
285                 } else {
286                         // Account is locked or unconfirmed
287                         switch ($status) {
288                                 case "LOCKED"     : $ERROR = CODE_ID_LOCKED;      break;
289                                 case "UNCONFIRMED": $ERROR = CODE_ID_UNCONFIRMED; break;
290                                 default: // Unhandled account status!
291                                         $ERROR = CODE_UNHANDLED_STATUS;
292                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Undhandled account status %s detected.", $status));
293                                         break;
294                         }
295
296                         // Load URL
297                         LOAD_URL("modules.php?module=index&amp;what=login&amp;login=".$MSG);
298                 }
299         } else {
300                 // ID or email is wrong
301                 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
302         }
303 }
304
305 // Login problems?
306 if (!empty($_GET['login'])) {
307         // Use code from URL
308         $ERROR = SQL_ESCAPE($_GET['login']);
309 } // END  - if
310
311 // Login problems?
312 if (!empty($ERROR)) {
313         // Ok, which one now?
314         $MSG = "<TR>
315   <TD width=\"10\">&nbsp;</TD>
316   <TD colspan=\"7\" align=\"center\">
317     <STRONG><SPAN class=\"guest_failed\">";
318
319         switch ($ERROR) {
320                 case CODE_WRONG_PASS:
321                         $MSG .= LOGIN_WRONG_PASS;
322                         break;
323
324                 case CODE_WRONG_ID:
325                         $MSG .= LOGIN_WRONG_ID;
326                         break;
327
328                 case CODE_ID_LOCKED:
329                         $MSG .= LOGIN_ID_LOCKED;
330                         break;
331
332                 case CODE_ID_UNCONFIRMED:
333                         $MSG .= LOGIN_ID_UNCONFIRMED;
334                         break;
335
336                 case CODE_NO_COOKIES:
337                         $MSG .= LOGIN_NO_COOKIES;
338                         break;
339
340                 case CODE_EXTENSION_PROBLEM:
341                         if (IS_ADMIN()) {
342                                 $MSG .= sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "nickname");
343                         } else {
344                                 $MSG .= LOGIN_WRONG_ID;
345                         }
346                         break;
347
348                 default:
349                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unhandled error code %s detected.", $ERROR));
350                         $MSG .= LOGIN_WRONG_ID;
351                         break;
352                 }
353                 $MSG .= "</SPAN></STRONG>
354   </TD>
355   <TD width=\"10\">&nbsp;</TD>
356 </TR>\n";
357                 define('LOGIN_FAILURE_MSG', $MSG);
358 } else {
359         // No problems, no output
360         define('LOGIN_FAILURE_MSG', "");
361 }
362
363 // Display login form with resend-password form
364 if (EXT_IS_ACTIVE("nickname")) {
365         LOAD_TEMPLATE("guest_nickname_login");
366 } else {
367         LOAD_TEMPLATE("guest_login");
368 }
369
370 // Was an URL constructed?
371 if (!empty($URL)) {
372         // URL was constructed
373         if (!empty($FATAL[0])) {
374                 // Fatal errors!
375                 require_once(PATH."inc/fatal_errors.php");
376         } else {
377                 // Load URL
378                 LOAD_URL($URL);
379         }
380 } // END - if
381
382 //
383 ?>