]> git.mxchange.org Git - mailer.git/blob - inc/modules/guest/what-login.php
6035504fb80e6f0470f99f945b1965d267384e95
[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                                 $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&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         // Probe userid/nickname
245         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
246         if ($probe_nickname) {
247                 // Nickname entered
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__);
250         } else {
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__);
254         }
255
256         // Any entry found?
257         if (SQL_NUMROWS($result) == 1) {
258                 // This data is valid, so we create a new pass... :-)
259                 list($uid, $status) = SQL_FETCHROW($result);
260
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__);
266
267                         // Prepare data and message for email
268                         $msg = LOAD_EMAIL_TEMPLATE("new-pass", array('new_pass' => $NEW_PASS), $uid);
269
270                         // ... and send it away
271                         SEND_EMAIL($uid, GUEST_NEW_PASSWORD, $msg);
272
273                         // Output note to user
274                         LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
275                 } else {
276                         // Account is locked or unconfirmed
277                         switch ($status) {
278                                 case "LOCKED"     : $MSG = CODE_ID_LOCKED;      break;
279                                 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
280                         }
281
282                         // Load URL
283                         LOAD_URL("modules.php?module=index&amp;what=login&amp;login=".$MSG);
284                 }
285         } else {
286                 // ID or email is wrong
287                 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
288         }
289 }
290
291 // Login problems?
292 if (!empty($_GET['login'])) {
293         // Use code from URL
294         $ERROR = SQL_ESCAPE($_GET['login']);
295 } // END  - if
296
297 // Login problems?
298 if (!empty($ERROR)) {
299         // Ok, which one now?
300         $MSG = "<TR>
301   <TD width=\"10\">&nbsp;</TD>
302   <TD colspan=\"7\" align=\"center\">
303     <STRONG><SPAN class=\"guest_failed\">";
304
305         switch ($ERROR) {
306                 case CODE_WRONG_PASS:
307                         $MSG .= LOGIN_WRONG_PASS;
308                         break;
309
310                 case CODE_WRONG_ID:
311                         $MSG .= LOGIN_WRONG_ID;
312                         break;
313
314                 case CODE_ID_LOCKED:
315                         $MSG .= LOGIN_ID_LOCKED;
316                         break;
317
318                 case CODE_ID_UNCONFIRMED:
319                         $MSG .= LOGIN_ID_UNCONFIRMED;
320                         break;
321
322                 case CODE_NO_COOKIES:
323                         $MSG .= LOGIN_NO_COOKIES;
324                         break;
325
326                 case CODE_EXTENSION_PROBLEM:
327                         if (IS_ADMIN()) {
328                                 $MSG .= sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "nickname");
329                         } else {
330                                 $MSG .= LOGIN_WRONG_ID;
331                         }
332                         break;
333
334                 default:
335                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unhandled error code %s detected.", $ERROR));
336                         $MSG .= LOGIN_WRONG_ID;
337                         break;
338                 }
339                 $MSG .= "</SPAN></STRONG>
340   </TD>
341   <TD width=\"10\">&nbsp;</TD>
342 </TR>\n";
343                 define('LOGIN_FAILURE_MSG', $MSG);
344 } else {
345         // No problems, no output
346         define('LOGIN_FAILURE_MSG', "");
347 }
348
349 // Display login form with resend-password form
350 if (EXT_IS_ACTIVE("nickname")) {
351         LOAD_TEMPLATE("guest_nickname_login");
352 } else {
353         LOAD_TEMPLATE("guest_login");
354 }
355
356 // Was an URL constructed?
357 if (!empty($URL)) {
358         // URL was constructed
359         if (!empty($FATAL[0])) {
360                 // Fatal errors!
361                 require_once(PATH."inc/fatal_errors.php");
362         } else {
363                 // Load URL
364                 LOAD_URL($URL);
365         }
366 } // END - if
367
368 //
369 ?>