62450c81b6b2a728bc45617ef4ca97928feb88fa
[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 {
38         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
39         require($INC);
40 }
41
42 // Add description as navigation point
43 ADD_DESCR("guest", basename(__FILE__));
44
45 OPEN_TABLE("100%", "guest_content_align", "");
46 global $DATA, $FATAL;
47
48 // Initialize data
49 $probe_nickname = false; $UID = false; $hash = "";
50 unset($login); unset($online);
51
52 if ((!empty($GLOBALS['userid'])) && (isSessionVariableSet('u_hash')))
53 {
54         // Already logged in?
55         $UID = $GLOBALS['userid'];
56 } elseif ((!empty($_POST['id'])) && (!empty($_POST['password'])) && (isset($_POST['ok']))) {
57         // Set userid and crypt password when login data was submitted
58         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
59         if ($probe_nickname)
60         {
61                 // Nickname entered
62                 $UID = SQL_ESCAPE($_POST['id']);
63         } else {
64                 // Direct userid entered
65                 $UID  = bigintval($_POST['id']);
66         }
67 } elseif (!empty($_POST['new_pass'])) {
68         // New password requested
69         $UID = "0";
70         if (!empty($_POST['id'])) $UID = $_POST['id'];
71 } else {
72         // Not logged in
73         $UID = "0"; $hash = "";
74 }
75
76 $URL = ""; $ADD = "";
77 // Set unset variables
78 if (empty($_POST['new_pass'])) $_POST['new_pass'] = "";
79 if (empty($_GET['login']))     $_GET['login']     = "";
80
81 if (IS_LOGGED_IN()) {
82         // Login immidiately...
83         $URL = URL."/modules.php?module=login";
84 } elseif (isset($_POST['ok'])) {
85         // Add last_login if available
86         $LAST = "";
87         if (GET_EXT_VERSION("sql_patches") >= "0.2.8") {
88                 $LAST = ", last_login";
89         }
90
91         // Check login data
92         $password = "";
93         if ($probe_nickname) {
94                 // Nickname entered
95                 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$LAST." FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' AND status='CONFIRMED' LIMIT 1",
96                  array($UID), __FILE__, __LINE__);
97                 list($UID2, $password, $online, $login) = SQL_FETCHROW($result);
98                 if (!empty($UID2)) $UID = $UID2;
99         } else {
100                 // Direct userid entered
101                 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$LAST." FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d AND status='CONFIRMED' LIMIT 1",
102                  array(bigintval($UID), $hash), __FILE__, __LINE__);
103                 list($dmy, $password, $online, $login) = SQL_FETCHROW($result);
104         }
105         if (SQL_NUMROWS($result) == 1) {
106                 // Valid data found so let's load the last login data
107                 if (isset($_POST['ok'])) {
108                         // By default the hash is empty
109                         $hash = "";
110
111                         // Check for old MD5 passwords
112                         if ((strlen($password) == 32) && (md5($_POST['password']) == $password)) {
113                                 // Just set the hash to the password from DB... :)
114                                 $hash = $password;
115                         } else {
116                                 // Encrypt hash for comparsion
117                                 $hash = generateHash($_POST['password'], substr($password, 0, -40));
118                         }
119
120                         if ($hash == $password) {
121                                 // New hashed password found so let's generate a new one
122                                 $hash = generateHash($_POST['password']);
123
124                                 // ... and update database
125                                 $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%d AND status='CONFIRMED' LIMIT 1",
126                                  array($hash, $UID), __FILE__, __LINE__);
127
128                                 // No login bonus by default
129                                 $BONUS = false;
130
131                                 // Probe for last online timemark
132                                 $probe = time() -  $online;
133                                 if (!empty($login)) $probe = time() - $login;
134                                 if ((GET_EXT_VERSION("bonus") >= "0.2.2") && ($probe >= $_CONFIG['login_timeout'])) {
135                                         // Add login bonus to user's account
136                                         $ADD = ", login_bonus=login_bonus+'".$_CONFIG['login_bonus']."'";
137                                         $BONUS = true;
138
139                                         // Subtract login bonus from userid's account or jackpot
140                                         if ((GET_EXT_VERSION("bonus") >= "0.3.5") && ($_CONFIG['bonus_mode'] != "ADD")) BONUS_POINTS_HANDLER('login_bonus');
141                                 }
142
143
144                                 // Secure lifetime from input form
145                                 $l = bigintval($_POST['lifetime']);
146                                 $life = "-1";
147                                 if ($l > 0) {
148                                         // Calculate lifetime of cookies
149                                         $life = time() + $l;
150
151                                         // Calculate new hash with the secret key and master salt together
152                                         $hash = generatePassString($hash);
153
154                                         // Update cookies
155                                         $login = (set_session("userid"  , $UID , $life, COOKIE_PATH)
156                                                && set_session("u_hash"  , $hash, $life, COOKIE_PATH)
157                                                && set_session("lifetime", $l   , $life, COOKIE_PATH));
158
159                                         // Update global array
160                                         $GLOBALS['userid'] = $UID;
161                                 } else {
162                                         // Check for login data
163                                         $login = IS_LOGGED_IN();
164                                 }
165
166                                 if ($login) {
167                                         // Update database records
168                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET total_logins=total_logins+1".$ADD." WHERE userid=%d LIMIT 1",
169                                          array(bigintval($UID)), __FILE__, __LINE__);
170                                         if (SQL_AFFECTEDROWS($link) == 1) {
171                                                 // Procedure to checking for login data
172                                                 if (($BONUS) && (EXT_IS_ACTIVE("bonus"))) {
173                                                         // Bonus added (just displaying!)
174                                                         $URL = URL."/modules.php?module=chk_login&mode=bonus";
175                                                 } else {
176                                                         // Bonus not added
177                                                         $URL = URL."/modules.php?module=chk_login&mode=login";
178                                                 }
179                                         } else {
180                                                 // Cannot update counter!
181                                                 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".CODE_CNTR_FAILED;
182                                         }
183                                 } else {
184                                         // Cookies not setable!
185                                         $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".CODE_NO_COOKIES;
186                                 }
187                         } else {
188                                 // Wrong password!
189                                 $ERROR = CODE_WRONG_PASS;
190                         }
191                 } else {
192                         // Fatal error!
193                         $ERROR = CODE_LOGIN_FAILED;
194                 }
195         } else {
196                 // Other account status?
197                 $result = SQL_QUERY_ESC("SELECT status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1",
198                  array(bigintval($UID)), __FILE__, __LINE__);
199                 if (SQL_NUMROWS($result) == 1)
200                 {
201                         // Load status
202                         list($status) = SQL_FETCHROW($result);
203                         switch ($status)
204                         {
205                         case "LOCKED":
206                                 $ERROR = CODE_ID_LOCKED;
207                                 break;
208
209                         case "UNCONFIRMED":
210                                 $ERROR = CODE_ID_UNCONFIRMED;
211                                 break;
212
213                         default:
214                                 $ERROR = CODE_UNKNOWN_STATUS;
215                                 break;
216                         }
217                 }
218                  else
219                 {
220                         // ID not found!
221                         $ERROR = CODE_WRONG_ID;
222                 }
223
224                 // Construct URL
225                 $URL = URL."/modules.php?module=".$GLOBALS['module']."&what=login&login=".$ERROR;
226         }
227 }
228  elseif ((!empty($_POST['new_pass'])) && (isset($UID)))
229 {
230         // Compile email when found in address (only secure chars!)
231         if (!empty($_POST['email'])) $_POST['email'] = str_replace("{DOT}", '.', $_POST['email']);
232
233         // Set ID number when left empty
234         if (empty($_POST['id'])) $_POST['id'] = "0";
235
236         // Probe userid/nickname
237         $probe_nickname = ((EXT_IS_ACTIVE("nickname")) && (("".round($_POST['id'])."") != $_POST['id']));
238         if ($probe_nickname)
239         {
240                 // Nickname entered
241                 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE nickname='%s' OR email='%s' LIMIT 1",
242                  array(addslashes($UID), $_POST['email']), __FILE__, __LINE__);
243         }
244          else
245         {
246                 // Direct userid entered
247                 $result = SQL_QUERY_ESC("SELECT userid, status FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d OR email='%s' LIMIT 1",
248                  array(bigintval($UID), $_POST['email']), __FILE__, __LINE__);
249         }
250         if (SQL_NUMROWS($result) == 1)
251         {
252                 // This data is valid, so we create a new pass... :-)
253                 list($UID, $status) = SQL_FETCHROW($result);
254
255                 if ($status == "CONFIRMED")
256                 {
257                         // Ooppps, this was missing! ;-) We should update the database...
258                         $NEW_PASS = GEN_PASS();
259                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET password='%s' WHERE userid=%d LIMIT 1",
260                          array(generateHash($NEW_PASS), bigintval($UID)), __FILE__, __LINE__);
261
262                         // Prepare data and message for email
263                         $DATA = array($NEW_PASS, getenv('REMOTE_ADDR'));
264                         $msg = LOAD_EMAIL_TEMPLATE("new-pass", "", bigintval($UID));
265
266                         // ... and send it away
267                         SEND_EMAIL(bigintval($UID), GUEST_NEW_PASSWORD, $msg);
268
269                         // Output note to user
270                         LOAD_TEMPLATE("admin_settings_saved", false, GUEST_NEW_PASSWORD_SEND);
271                 }
272                  else
273                 {
274                         // Account is locked or unconfirmed
275                         switch ($status)
276                         {
277                                 case "LOCKED"     : $MSG = CODE_ID_LOCKED;      break;
278                                 case "UNCONFIRMED": $MSG = CODE_ID_UNCONFIRMED; break;
279                         }
280
281                         // Load URL
282                         LOAD_URL("modules.php?module=".$GLOBALS['module']."&amp;what=login&login=".$MSG);
283                 }
284         }
285          else
286         {
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  else
292 {
293         // Login problems?
294         if (!empty($_GET['login']))
295         {
296                 // Ok, which one now?
297                 $MSG = "<TR>
298   <TD width=\"10\">&nbsp;</TD>
299   <TD colspan=\"7\" align=\"center\">
300     <STRONG><SPAN class=\"guest_failed\">";
301                 switch ($_GET['login'])
302                 {
303                 case CODE_WRONG_PASS:
304                         $MSG .= LOGIN_WRONG_PASS;
305                         break;
306
307                 case CODE_WRONG_ID:
308                         $MSG .= LOGIN_WRONG_ID;
309                         break;
310
311                 case CODE_ID_LOCKED:
312                         $MSG .= LOGIN_ID_LOCKED;
313                         break;
314
315                 case CODE_ID_UNCONFIRMED:
316                         $MSG .= LOGIN_ID_UNCONFIRMED;
317                         break;
318
319                 case CODE_NO_COOKIES:
320                         $MSG .= LOGIN_NO_COOKIES;
321                         break;
322
323                 default:
324                         $MSG .= LOGIN_WRONG_ID;
325                         break;
326                 }
327                 $MSG .= "</SPAN></STRONG>
328   </TD>
329   <TD width=\"10\">&nbsp;</TD>
330 </TR>\n";
331                 define('LOGIN_FAILURE_MSG', $MSG);
332         }
333          else
334         {
335                 // No problems, no output
336                 define('LOGIN_FAILURE_MSG', "");
337         }
338         // Display login form with resend-password form
339         if (EXT_IS_ACTIVE("nickname"))
340         {
341                 LOAD_TEMPLATE("guest_nickname_login");
342         }
343          else
344         {
345                 LOAD_TEMPLATE("guest_login");
346         }
347 }
348
349 // Was an URL constructed?
350 if (!empty($URL))
351 {
352         // URL was constructed
353         if (!empty($FATAL[0]))
354         {
355                 // Fatal errors!
356                 require_once(PATH."inc/fatal_errors.php");
357         }
358          else
359         {
360                 // Load URL
361                 LOAD_URL($URL);
362         }
363 }
364
365 CLOSE_TABLE();
366 //
367 ?>