d416353943a48b21069446b62bd7d3bfbfd76d22
[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 (!defined('__SECURITY')) {
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 } // END - if
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                         // Admin still not registered?
94                         if (!isBooleanConstantAndTrue('admin_registered')) {
95                                 // Write to config that registration is done
96                                 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
97
98                                 // Load URL for login
99                                 $URL = URL."/modules.php?module=admin&amp;action=login";
100                                 LOAD_URL($URL);
101                         } // END - if
102                         break;
103                 }
104         }
105
106         // Whas that action okay?
107         if ($ret != "done") {
108                 // Fixes another "Notice"
109                 if (!empty($_POST['login'])) {
110                         define('__LOGIN_VALUE', $_POST['login']);
111                 } else {
112                         define('__LOGIN_VALUE', "");
113                 }
114
115                 // Yet-another "Notice" fix
116                 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
117                         // No login entered?
118                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
119
120                         // An error comes back from registration?
121                         if (!empty($ret)) $MSG1 = $ret;
122
123                         // No password entered?
124                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
125
126                         // Or password too short?
127                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
128
129                         // Output error messages
130                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
131                         define('__MSG_PASS',  LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
132
133                         // Reset variables
134                         $MSG1 = ""; $MSG2 = "";
135                 } else {
136                         // Reset values to nothing
137                         define('__MSG_LOGIN', "");
138                         define('__MSG_PASS' , "");
139                 }
140
141                 // Load register template
142                 LOAD_TEMPLATE("admin_reg_form");
143         }
144 } elseif (isset($_GET['reset_pass'])) {
145         // Is the form submitted?
146         if ((isset($_POST['send_link'])) && (!empty($_POST['email']))) {
147                 // Try to send the link out
148                 $OUT = ADMIN_SEND_PASSWORD_RESET_LINK($_POST['email']);
149
150                 // Output result
151                 LOAD_TEMPLATE("admin_settings_saved", false, $OUT);
152         } elseif (!empty($_GET['hash'])) {
153                 // Output form for hash validation
154                 LOAD_TEMPLATE("admin_validate_reset_hash_form", false, $_GET['hash']);
155         } elseif ((isset($_POST['validate_hash'])) && (!empty($_POST['login'])) && (!empty($_POST['hash']))) {
156                 // Validate the login data and hash
157                 $valid = ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login']);
158
159                 // Valid?
160                 if ($valid) {
161                         // Prepare content first
162                         $content = array(
163                                 'hash'  => SQL_ESCAPE($_POST['hash']),
164                                 'login' => SQL_ESCAPE($_POST['login'])
165                         );
166
167                         // Validation okay so display form for final password change
168                         LOAD_TEMPLATE("admin_reset_password_form", false, $content);
169                 } else {
170                         // Cannot validate the login data and hash
171                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED);
172                 }
173         } elseif ((isset($_POST['reset_pass'])) && (!empty($_POST['hash'])) && (!empty($_POST['login'])) && (!empty($_POST['pass1'])) && ($_POST['pass1'] == $_POST['pass2'])) {
174                 // Okay, we shall the admin password here. So first revalidate the hash
175                 if (ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login'])) {
176                         // Set the password now
177                         $OUT = ADMIN_RESET_PASSWORD($_POST['login'], $_POST['pass1']);
178
179                         // Output result
180                         LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
181                 } else {
182                         // Validation failed
183                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED2);
184                 }
185         } else {
186                 // Output reset password form
187                 LOAD_TEMPLATE("admin_send_reset_link");
188         }
189 } 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())) {
190         // At leat one administrator account was created
191         if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
192                 // Timeout for last login, we have to logout first!
193                 $URL = URL."/modules.php?module=admin&amp;action=login&amp;logout=1";
194                 LOAD_URL($URL);
195         }
196         if (!empty($_GET['register'])) {
197                 // Registration of first admin is done
198                 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
199         } // END - if
200
201         // Check if the admin has submitted data or not
202         $ret = "";
203         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
204         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
205                 // All required data was entered so we check his account
206                 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
207
208                 // Which status do we have?
209                 switch ($ret)
210                 {
211                 case "done": // Admin and password are okay, so we log in now
212                         // Construct URL and redirect
213                         $URL = URL."/modules.php?module=admin&amp;";
214
215                         // Rewrite overview module
216                         if ($GLOBALS['what'] == "overview") {
217                                 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
218                         }
219
220                         // Add data to URL
221                         if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
222                          elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
223                          elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
224
225                         // Load URL
226                         LOAD_URL($URL);
227                         break;
228
229                 case "404": // Administrator login not found
230                         $_POST['ok'] = $ret;
231                         $ret = ADMIN_NOT_FOUND;
232                         DESTROY_ADMIN_SESSION();
233                         break;
234
235                 case "pass": // Wrong password
236                         $_POST['ok'] = $ret;
237                         $ret = WRONG_PASS." [<A href=\"".URL."/modules.php?module=admin&amp;reset_pass=1\">".ADMIN_RESET_PASS."</A>]\n";
238                         DESTROY_ADMIN_SESSION();
239                         break;
240                 }
241         }
242         if ($ret != "done") {
243                 if (!empty($_POST['login'])) {
244                         define('__LOGIN_VALUE', $_POST['login']);
245                 } else {
246                         define('__LOGIN_VALUE', "");
247                 }
248
249                 if (isset($_POST['ok'])) {
250                         // Set messages to zero
251
252                         $MSG1 = ""; $MSG2 = "";
253                         // No login entered?
254                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
255
256                         // An error comes back from login?
257                         if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
258
259                         // No password entered?
260                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
261
262                         // Or password too short?
263                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
264
265                         // An error comes back from login?
266                         if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
267
268                         // Load message template
269                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
270                         define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
271
272                         // Reset variables
273                         $MSG1 = ""; $MSG2 = "";
274                 } else {
275                         // Set constants to empty for hiding them
276                         define('__MSG_LOGIN', "");
277                         define('__MSG_PASS' , "");
278                 }
279
280                 // Load login form
281                 if (!empty($GLOBALS['what'])) {
282                         // Restore old what value
283                         $content = array('target' => "what", 'value' => $GLOBALS['what']);
284                 } elseif (!empty($GLOBALS['action'])) {
285                         if ($GLOBALS['action'] != "logout") {
286                                 // Restore old action value
287                                 $content = array('target' => "action", 'value' => $GLOBALS['action']);
288                         } else {
289                                 // Set default values
290                                 $content = array('target' => "action", 'value' => "login");
291                         }
292                 } elseif (!empty($_GET['area'])) {
293                         // Restore old area value
294                         $content = array('target' => "area", 'value' => $_GET['area']);
295                 } else {
296                         // Set default values
297                         $content = array('target' => "action", 'value' => "login");
298                 }
299
300                 // Load login form template
301                 LOAD_TEMPLATE("admin_login_form", false, $content);
302         }
303 } elseif (isset($_GET['logout'])) {
304         // Only try to remove cookies
305         if (DESTROY_ADMIN_SESSION()) {
306                 // Load logout template
307                 if (isset($_GET['register'])) {
308                         // Secure input
309                         $register = SQL_ESCAPE($_GET['register']);
310
311                         // Special logout redirect for installation of given extension
312                         LOAD_TEMPLATE(sprintf("admin_logout_%s_install", $register));
313                 } elseif (isset($_GET['remove'])) {
314                         // Secure input
315                         $remove = SQL_ESCAPE($_GET['remove']);
316
317                         // Special logout redirect for removal of given extension
318                         LOAD_TEMPLATE(sprintf("admin_logout_%s_remove", $remove));
319                 } else {
320                         // Logged out normally
321                         LOAD_TEMPLATE("admin_logout");
322                 }
323         } else {
324                 // Something went wrong here...
325                 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
326
327                 // Add fatal message
328                 ADD_FATAL(CANNOT_UNREG_SESS);
329         }
330 } else {
331         // Maybe an Admin want's to login?
332         $ret = CHECK_ADMIN_COOKIES(get_session('admin_login'), get_session('admin_md5'));
333         switch ($ret)
334         {
335         case "done":
336                 // Cookie-Data accepted
337                 if ((set_session("admin_md5", get_session('admin_md5'))) && (set_session("admin_login", get_session('admin_login'))) && (set_session("admin_last", time())) && (set_session("admin_to", bigintval(get_session('admin_to'))))) {
338                         // Ok, Cookie-Update done
339                         if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
340                                 // Check if action GET variable was set
341                                 $act = SQL_ESCAPE($GLOBALS['action']);
342                                 if (!empty($GLOBALS['what'])) {
343                                         // Get action value by what-value
344                                         $act = GET_ACTION("admin", $GLOBALS['what']);
345                                 }
346
347                                 // Check for access control line of current menu entry
348                                 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
349                         } else {
350                                 // Extension not installed so it's always allowed to access everywhere!
351                                 define('__ACL_ALLOW', true);
352                         }
353
354                         // When type of admin menu is not set fallback to old menu system
355                         if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
356
357                         // Check for version and switch between old menu system and new "intelligent menu system"
358                         if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (FILE_READABLE(PATH."inc/modules/admin/lasys-inc.php"))) {
359                                 // Default area is the entrance, of course
360                                 $area = "entrance";
361
362                                 // Check for similar URL variable
363                                 if (!empty($_GET['area'])) $area = SQL_ESCAPE($_GET['area']);
364
365                                 // Load "logical-area menu-system" file
366                                 require_once(PATH."inc/modules/admin/lasys-inc.php");
367
368                                 // Create new-style menu system will "logical areas"
369                                 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
370                         } else {
371                                 // This little call constructs the whole default old and lacky menu system
372                                 // on left side
373                                 ADMIN_DO_ACTION($GLOBALS['what']);
374                         }
375                 } else {
376                         // Login failed (cookies enabled?)
377                         OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
378                         ADD_FATAL(CANNOT_RE_REGISTER_SESS);
379                 }
380                 break;
381
382         case "404": // Administrator login not found
383                 $_POST['ok'] = $ret;
384                 DESTROY_ADMIN_SESSION();
385                 ADD_FATAL(ADMIN_NOT_FOUND);
386                 break;
387
388         case "pass": // Wrong password
389                 $_POST['ok'] = $ret;
390                 DESTROY_ADMIN_SESSION();
391                 ADD_FATAL(WRONG_PASS);
392                 break;
393         }
394 }
395
396 //
397 ?>