]> git.mxchange.org Git - mailer.git/blob - inc/modules/guest/what-login.php
fc66b2470816587d91a7acf5b4953a667316af07
[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 >= $_CONFIG['login_timeout'])) {
137                         if (true) {
138                                 // Add login bonus to user's account
139                                 $ADD = sprintf(", login_bonus=login_bonus+%s",
140                                         (float)$_CONFIG['login_bonus']
141                                 );
142                                 $BONUS = true;
143
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');
146                         } // END - if
147
148                         // Init variables
149                         $life = "-1"; $login = false;
150
151                         // Secure lifetime from input form
152                         $l = bigintval($_POST['lifetime']);
153
154                         // Is the lifetime set?
155                         if ($l > 0) {
156                                 // Calculate lifetime of cookies
157                                 $life = time() + $l;
158
159                                 // Calculate new hash with the secret key and master salt together
160                                 $hash = generatePassString($hash);
161
162                                 // Update cookies
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)
166                                 );
167
168                                 // Update global array
169                                 $GLOBALS['userid'] = $uid;
170                         } else {
171                                 // Check for login data
172                                 $login = IS_MEMBER();
173                         }
174
175                         if ($login) {
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&amp;mode=bonus";
184                                         } else {
185                                                 // Bonus not added
186                                                 $URL = URL."/modules.php?module=chk_login&amp;mode=login";
187                                         }
188                                 } else {
189                                         // Cannot update counter!
190                                         $URL = URL."/modules.php?module=index&amp;what=login&amp;login=".CODE_CNTR_FAILED;
191                                 }
192                         } else {
193                                 // Cookies not setable!
194                                 $URL = URL."/modules.php?module=index&amp;what=login&amp;login=".CODE_NO_COOKIES;
195                         }
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__);
200
201                         // Wrong password!
202                         $ERROR = CODE_WRONG_PASS;
203                 }
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__);
208
209                 // Entry found?
210                 if (SQL_NUMROWS($result) == 1) {
211                         // Load status
212                         list($status) = SQL_FETCHROW($result);
213                         switch ($status) {
214                                 case "LOCKED":
215                                         $ERROR = CODE_ID_LOCKED;
216                                         break;
217
218                                 case "UNCONFIRMED":
219                                         $ERROR = CODE_ID_UNCONFIRMED;
220                                         break;
221
222                                 default:
223                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown error status %s detected.", $status));
224                                         $ERROR = CODE_UNKNOWN_STATUS;
225                                         break;
226                         }
227                 } else {
228                         // ID not found!
229                         $ERROR = CODE_WRONG_ID;
230                 }
231
232                 // Construct URL
233                 $URL = URL."/modules.php?module=index&amp;what=login&amp;login=".$ERROR;
234         } else {
235                 // ID not found!
236                 $ERROR = CODE_WRONG_ID;
237         }
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']);
241
242         // Set ID number when left empty
243         if (empty($_POST['id'])) $_POST['id'] = 0;
244
245         // Probe userid/nickname
246         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
247         if ($probe_nickname) {
248                 // Nickname entered
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__);
251         } else {
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__);
255         }
256
257         // Any entry found?
258         if (SQL_NUMROWS($result) == 1) {
259                 // This data is valid, so we create a new pass... :-)
260                 list($uid, $status) = SQL_FETCHROW($result);
261
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__);
267
268                         // Prepare data and message for email
269                         $msg = LOAD_EMAIL_TEMPLATE("new-pass", array('new_pass' => $NEW_PASS), $uid);
270
271                         // ... and send it away
272                         SEND_EMAIL($uid, GUEST_NEW_PASSWORD, $msg);
273
274                         // Output note to user
275                         LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
276                 } else {
277                         // Account is locked or unconfirmed
278                         switch ($status) {
279                                 case "LOCKED"     : $MSG = CODE_ID_LOCKED;      break;
280                                 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
281                         }
282
283                         // Load URL
284                         LOAD_URL("modules.php?module=index&amp;what=login&amp;login=".$MSG);
285                 }
286         } else {
287                 // ID or email is wrong
288                 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"guest_failed\">".GUEST_WRONG_ID_EMAIL."</SPAN>");
289         }
290 }
291
292 // Login problems?
293 if (!empty($_GET['login'])) {
294         // Use code from URL
295         $ERROR = SQL_ESCAPE($_GET['login']);
296 } // END  - if
297
298 // Login problems?
299 if (!empty($ERROR)) {
300         // Ok, which one now?
301         $MSG = "<TR>
302   <TD width=\"10\">&nbsp;</TD>
303   <TD colspan=\"7\" align=\"center\">
304     <STRONG><SPAN class=\"guest_failed\">";
305
306         switch ($ERROR) {
307                 case CODE_WRONG_PASS:
308                         $MSG .= LOGIN_WRONG_PASS;
309                         break;
310
311                 case CODE_WRONG_ID:
312                         $MSG .= LOGIN_WRONG_ID;
313                         break;
314
315                 case CODE_ID_LOCKED:
316                         $MSG .= LOGIN_ID_LOCKED;
317                         break;
318
319                 case CODE_ID_UNCONFIRMED:
320                         $MSG .= LOGIN_ID_UNCONFIRMED;
321                         break;
322
323                 case CODE_NO_COOKIES:
324                         $MSG .= LOGIN_NO_COOKIES;
325                         break;
326
327                 case CODE_EXTENSION_PROBLEM:
328                         if (IS_ADMIN()) {
329                                 $MSG .= sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "nickname");
330                         } else {
331                                 $MSG .= LOGIN_WRONG_ID;
332                         }
333                         break;
334
335                 default:
336                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unhandled error code %s detected.", $ERROR));
337                         $MSG .= LOGIN_WRONG_ID;
338                         break;
339                 }
340                 $MSG .= "</SPAN></STRONG>
341   </TD>
342   <TD width=\"10\">&nbsp;</TD>
343 </TR>\n";
344                 define('LOGIN_FAILURE_MSG', $MSG);
345 } else {
346         // No problems, no output
347         define('LOGIN_FAILURE_MSG', "");
348 }
349
350 // Display login form with resend-password form
351 if (EXT_IS_ACTIVE("nickname")) {
352         LOAD_TEMPLATE("guest_nickname_login");
353 } else {
354         LOAD_TEMPLATE("guest_login");
355 }
356
357 // Was an URL constructed?
358 if (!empty($URL)) {
359         // URL was constructed
360         if (!empty($FATAL[0])) {
361                 // Fatal errors!
362                 require_once(PATH."inc/fatal_errors.php");
363         } else {
364                 // Load URL
365                 LOAD_URL($URL);
366         }
367 } // END - if
368
369 //
370 ?>