Password reset for admin login added (still buggy if cache is installed and sql_patch...
[mailer.git] / inc / modules / admin.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 08/31/2003 *
4  * ===============                              Last change: 07/02/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : admin.php                                        *
8  * -------------------------------------------------------------------- *
9  * Short description : Administration module                            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Administrationsmodul                             *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Login is default
41 if ((empty($GLOBALS['action'])) && ($check == "admin_only")) {
42         // Redirect to right URL
43         LOAD_URL("modules.php?module=admin&amp;action=login");
44 }
45
46 // Load include file
47 require_once(PATH."inc/modules/admin/admin-inc.php");
48
49 // Fix "deleted" cookies in PHP4 (PHP5 does remove them, PHP4 sets them to deleted!)
50 FIX_DELETED_COOKIES(array('admin_login', 'admin_md5', 'admin_last', 'admin_to'));
51
52 if (!isBooleanConstantAndTrue('admin_registered')) {
53         // Admin is not registered so we have to inform the user
54         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
55         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
56                 // Hash the password with the old function because we are here in install mode
57                 $hashedPass = md5($_POST['pass']);
58
59                 // Kill maybe existing session variables
60                 set_session('admin_login'       , "");
61                 set_session('admin_md5'         , "");
62                 set_session('admin_last'        , "");
63                 set_session('admin_to'          , "");
64
65                 // Do registration
66                 $ret = REGISTER_ADMIN($_POST['login'], $hashedPass);
67                 switch ($ret)
68                 {
69                 case "done":
70                         admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
71                         if (!_FATAL) {
72                                 // Registering is done
73                                 LOAD_URL(URL."/modules.php?module=admin&amp;action=login&register=done");
74                         } else {
75                                 $ret = ADMIN_CANNOT_COMPLETE;
76                         }
77                         break;
78
79                 case "failed":
80                         $ret = ADMIN_REGISTER_FAILED;
81                         break;
82
83                 case "already":
84                 default:
85                         if ($ret == "already") {
86                                 // Admin does already exists!
87                                 $ret = ADMIN_LOGIN_ALREADY_REG;
88                         } else {
89                                 // Any other kind
90                                 $ret = "done";
91                         }
92
93                         if (!isBooleanConstantAndTrue('admin_registered')) {
94                                 // Write to config that registration is done
95                                 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
96
97                                 // Load URL for login
98                                 $URL = URL."/modules.php?module=admin&amp;action=login";
99                                 LOAD_URL($URL);
100                         }
101                         break;
102                 }
103         }
104
105         // Whas that action okay?
106         if ($ret != "done") {
107                 // Fixes another "Notice"
108                 if (!empty($_POST['login'])) {
109                         define('__LOGIN_VALUE', $_POST['login']);
110                 } else {
111                         define('__LOGIN_VALUE', "");
112                 }
113
114                 // Yet-another "Notice" fix
115                 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
116                         // No login entered?
117                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
118
119                         // An error comes back from registration?
120                         if (!empty($ret)) $MSG1 = $ret;
121
122                         // No password entered?
123                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
124
125                         // Or password too short?
126                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
127
128                         // Output error messages
129                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
130                         define('__MSG_PASS',  LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
131
132                         // Reset variables
133                         $MSG1 = ""; $MSG2 = "";
134                 } else {
135                         // Reset values to nothing
136                         define('__MSG_LOGIN', "");
137                         define('__MSG_PASS' , "");
138                 }
139
140                 // Load register template
141                 LOAD_TEMPLATE("admin_reg_form");
142         }
143 } elseif (isset($_GET['reset_pass'])) {
144         // Is the form submitted?
145         if ((isset($_POST['send_link'])) && (!empty($_POST['email']))) {
146                 // Try to send the link out
147                 $OUT = ADMIN_SEND_PASSWORD_RESET_LINK($_POST['email']);
148
149                 // Output result
150                 LOAD_TEMPLATE("admin_settings_saved", false, $OUT);
151         } elseif (!empty($_GET['hash'])) {
152                 // Output form for hash validation
153                 LOAD_TEMPLATE("admin_validate_reset_hash_form", false, SQL_ESCAPE($_GET['hash']));
154         } elseif ((isset($_POST['validate_hash'])) && (!empty($_POST['login'])) && (!empty($_POST['hash']))) {
155                 // Validate the login data and hash
156                 $valid = ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login']);
157
158                 // Valid?
159                 if ($valid) {
160                         // Prepare content first
161                         $content = array(
162                                 'hash'  => SQL_ESCAPE($_POST['hash']),
163                                 'login' => SQL_ESCAPE($_POST['login'])
164                         );
165
166                         // Validation okay so display form for final password change
167                         LOAD_TEMPLATE("admin_reset_password_form", false, $content);
168                 } else {
169                         // Cannot validate the login data and hash
170                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED);
171                 }
172         } elseif ((isset($_POST['reset_pass'])) && (!empty($_POST['hash'])) && (!empty($_POST['login'])) && (!empty($_POST['pass1'])) && ($_POST['pass1'] == $_POST['pass2'])) {
173                 // Okay, we shall the admin password here. So first revalidate the hash
174                 if (ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login'])) {
175                         // Set the password now
176                         $OUT = ADMIN_RESET_PASSWORD($_POST['login'], $_POST['pass1']);
177
178                         // Output result
179                         LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
180                 } else {
181                         // Validation failed
182                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED2);
183                 }
184         } else {
185                 // Output reset password form
186                 LOAD_TEMPLATE("admin_send_reset_link");
187         }
188 } elseif ((!isSessionVariableSet('admin_login')) || (!isSessionVariableSet('admin_md5')) || (!isSessionVariableSet('admin_last')) || (!isSessionVariableSet('admin_to')) || ((get_session('admin_last') + bigintval(get_session('admin_to')) * 3600 * 24) < time())) {
189         // At leat one administrator account was created
190         if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
191                 // Timeout for last login, we have to logout first!
192                 $URL = URL."/modules.php?module=admin&amp;action=login&amp;logout=1";
193                 LOAD_URL($URL);
194         }
195         if (!empty($_GET['register'])) {
196                 // Registration of first admin is done
197                 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
198         } // END - if
199
200         // Check if the admin has submitted data or not
201         $ret = "";
202         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
203         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
204                 // All required data was entered so we check his account
205                 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
206                 switch ($ret)
207                 {
208                 case "done": // Admin and password are okay, so we log in now
209                         // Construct URL and redirect
210                         $URL = URL."/modules.php?module=admin&amp;";
211
212                         // Rewrite overview module
213                         if ($GLOBALS['what'] == "overview") {
214                                 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
215                         }
216
217                         // Add data to URL
218                         if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
219                          elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
220                          elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
221
222                         // Load URL
223                         LOAD_URL($URL);
224                         break;
225
226                 case "404": // Administrator login not found
227                         $_POST['ok'] = $ret;
228                         $ret = ADMIN_NOT_FOUND;
229                         break;
230
231                 case "pass": // Wrong password
232                         $_POST['ok'] = $ret;
233                         $ret = WRONG_PASS." [<A href=\"".URL."/modules.php?module=admin&amp;reset_pass=1\">".ADMIN_RESET_PASS."</A>]\n";
234                         break;
235                 }
236         }
237         if ($ret != "done") {
238                 if (!empty($_POST['login'])) {
239                         define('__LOGIN_VALUE', $_POST['login']);
240                 } else {
241                         define('__LOGIN_VALUE', "");
242                 }
243
244                 if (isset($_POST['ok'])) {
245                         // Set messages to zero
246
247                         $MSG1 = ""; $MSG2 = "";
248                         // No login entered?
249                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
250
251                         // An error comes back from login?
252                         if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
253
254                         // No password entered?
255                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
256
257                         // Or password too short?
258                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
259
260                         // An error comes back from login?
261                         if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
262
263                         // Load message template
264                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
265                         define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
266
267                         // Reset variables
268                         $MSG1 = ""; $MSG2 = "";
269                 } else {
270                         // Set constants to empty for hiding them
271                         define('__MSG_LOGIN', "");
272                         define('__MSG_PASS' , "");
273                 }
274
275                 // Load login form
276                 if (!empty($GLOBALS['what'])) {
277                         // Restore old what value
278                         $content = array('target' => "what", 'value' => $GLOBALS['what']);
279                 } elseif (!empty($GLOBALS['action'])) {
280                         if ($GLOBALS['action'] != "logout") {
281                                 // Restore old action value
282                                 $content = array('target' => "action", 'value' => $GLOBALS['action']);
283                         } else {
284                                 // Set default values
285                                 $content = array('target' => "action", 'value' => "login");
286                         }
287                 } elseif (!empty($_GET['area'])) {
288                         // Restore old area value
289                         $content = array('target' => "area", 'value' => $_GET['area']);
290                 } else {
291                         // Set default values
292                         $content = array('target' => "action", 'value' => "login");
293                 }
294
295                 // Load login form template
296                 LOAD_TEMPLATE("admin_login_form", false, $content);
297         }
298 } elseif (isset($_GET['logout'])) {
299         // Only try to remove cookies
300         if (set_session("admin_login", "") && set_session("admin_md5", "") && set_session("admin_last", "") && set_session("admin_to", "")) {
301                 // Also remove array elements
302                 set_session('admin_login'       , "");
303                 set_session('admin_md5'         , "");
304                 set_session('admin_last'        , "");
305                 set_session('admin_to'          , "");
306
307                 // Destroy session
308                 @session_destroy();
309
310                 // Load logout template
311                 if (isset($_GET['sql_patches'])) {
312                         // Special logout redirect for sql_patchrs
313                         LOAD_TEMPLATE("admin_logout_sql_patches");
314                 } else {
315                         // Logged out normally
316                         LOAD_TEMPLATE("admin_logout");
317                 }
318         } else {
319                 // Something went wrong here...
320                 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
321
322                 // Add fatal message
323                 ADD_FATAL(CANNOT_UNREG_SESS);
324         }
325 } else {
326         // Maybe an Admin want's to login?
327         $ret = CHECK_ADMIN_COOKIES(SQL_ESCAPE(get_session('admin_login')), SQL_ESCAPE(get_session('admin_md5')));
328         switch ($ret)
329         {
330         case "done":
331                 // Cookie-Data accepted
332                 if ((set_session("admin_md5", SQL_ESCAPE(get_session('admin_md5')))) && (set_session("admin_login", SQL_ESCAPE(get_session('admin_login')))) && (set_session("admin_last", time())) && (set_session("admin_to", bigintval(get_session('admin_to'))))) {
333                         // Ok, Cookie-Update done
334                         if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
335                                 // Check if action GET variable was set
336                                 $act = SQL_ESCAPE($GLOBALS['action']);
337                                 if (!empty($GLOBALS['what'])) {
338                                         // Get action value by what-value
339                                         $act = GET_ACTION("admin", $GLOBALS['what']);
340                                 }
341
342                                 // Check for access control line of current menu entry
343                                 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
344                         } else {
345                                 // Extension not installed so it's always allowed to access everywhere!
346                                 define('__ACL_ALLOW', true);
347                         }
348
349                         // When type of admin menu is not set fallback to old menu system
350                         if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
351
352                         // Check for version and switch between old menu system and new "intelligent menu system"
353                         if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (file_exists(PATH."inc/modules/admin/la_sys-inc.php"))) {
354                                 // Default area is the entrance, of course
355                                 $area = "entrance";
356
357                                 // Check for similar URL variable
358                                 if (!empty($_GET['area'])) $area = $_GET['area'];
359
360                                 // Load "logical-area menu-system" file
361                                 require_once(PATH."inc/modules/admin/la_sys-inc.php");
362
363                                 // Create new-style menu system will "logical areas"
364                                 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
365                         } else {
366                                 // This little call constructs the whole default old and lacky menu system
367                                 // on left side
368                                 ADMIN_DO_ACTION($GLOBALS['what']);
369                         }
370                 } else {
371                         // Login failed (cookies enabled?)
372                         OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
373                         ADD_FATAL(CANNOT_RE_REGISTER_SESS);
374                 }
375                 break;
376
377         case "404": // Administrator login not found
378                 $_POST['ok'] = $ret;
379                 ADD_FATAL(ADMIN_NOT_FOUND);
380                 break;
381
382         case "pass": // Wrong password
383                 $_POST['ok'] = $ret;
384                 ADD_FATAL(WRONG_PASS);
385                 break;
386         }
387 }
388
389 if (isBooleanConstantAndTrue('admin_registered'))
390 {
391         // Check config.php and inc directory for right access rights
392         if (is_INCWritable("config"))     ADD_FATAL(FATAL_CONFIG_WRITABLE);
393         if (is_INCWritable("dummy"))      ADD_FATAL(FATAL_INC_WRITABLE);
394 }
395 //
396 ?>