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