2 /************************************************************************
3 * MXChange v0.2.1 Start: 08/31/2003 *
4 * =============== Last change: 07/02/2004 *
6 * -------------------------------------------------------------------- *
8 * -------------------------------------------------------------------- *
9 * Short description : Administration module *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Administrationsmodul *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
32 ************************************************************************/
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
41 if ((empty($GLOBALS['action'])) && ($check == "admin_only")) {
42 // Redirect to right URL
43 LOAD_URL("modules.php?module=admin&action=login");
47 LOAD_INC_ONCE("inc/modules/admin/admin-inc.php");
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'));
55 // Is no admin registered?
56 if (!isBooleanConstantAndTrue('admin_registered')) {
57 // Admin is not registered so we have to inform the user
58 if ((IS_FORM_SENT()) && ((!REQUEST_ISSET_POST(('login'))) || (!REQUEST_ISSET_POST(('pass'))) || (strlen(REQUEST_POST('pass')) < 4))) {
59 REQUEST_SET_POST('ok', "***");
62 if ((IS_FORM_SENT()) && (REQUEST_POST('ok') != "***")) {
63 // Hash the password with the old function because we are here in install mode
64 $hashedPass = md5(REQUEST_POST('pass'));
66 // Kill maybe existing session variables
67 destroyAdminSession(false);
70 $ret = REGISTER_ADMIN(REQUEST_POST('login'), $hashedPass);
74 changeDataInFile(constant('PATH')."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
75 if (!constant('_FATAL')) {
76 // Registering is done
77 LOAD_URL("modules.php?module=admin&action=login®ister=done");
79 $ret = getMessage('ADMIN_CANNOT_COMPLETE');
84 $ret = getMessage('ADMIN_REGISTER_FAILED');
89 if ($ret == "already") {
90 // Admin does already exists!
91 $ret = getMessage('ADMIN_LOGIN_ALREADY_REG');
93 // Any other kind will be logged and interpreted as 'done'
94 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown return code %s from CHECK_ADMIN_LOGIN()", $ret));
95 // @TODO Why is this set to 'done'?
99 // Admin still not registered?
100 if (!isBooleanConstantAndTrue('admin_registered')) {
101 // Write to config that registration is done
102 changeDataInFile(constant('PATH')."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
104 // Load URL for login
105 LOAD_URL("modules.php?module=admin&action=login");
111 // Whas that action okay?
112 if ($ret != "done") {
113 // Fixes another "Notice"
114 if (REQUEST_ISSET_POST(('login'))) {
115 define('__LOGIN_VALUE', REQUEST_POST('login'));
117 define('__LOGIN_VALUE', "");
120 // Yet-another "Notice" fix
121 if ((IS_FORM_SENT()) && (REQUEST_POST('ok') == "***")) {
123 if (!REQUEST_ISSET_POST(('login'))) $MSG1 = getMessage('ADMIN_NO_LOGIN');
125 // An error comes back from registration?
126 if (!empty($ret)) $MSG1 = $ret;
128 // No password entered?
129 if (!REQUEST_ISSET_POST(('pass'))) $MSG2 = getMessage('ADMIN_NO_PASS');
131 // Or password too short?
132 if (strlen(REQUEST_POST('pass')) < 4) $MSG2 = getMessage('ADMIN_SHORT_PASS');
134 // Output error messages
135 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
136 define('__MSG_PASS', LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
139 $MSG1 = ""; $MSG2 = "";
141 // Reset values to nothing
142 define('__MSG_LOGIN', "");
143 define('__MSG_PASS' , "");
146 // Load register template
147 LOAD_TEMPLATE("admin_reg_form");
149 } elseif (REQUEST_ISSET_GET(('reset_pass'))) {
150 // Is the form submitted?
151 if ((REQUEST_ISSET_POST(('send_link'))) && (REQUEST_ISSET_POST(('email')))) {
152 // Try to send the link out
153 $OUT = ADMIN_SEND_PASSWORD_RESET_LINK(REQUEST_POST('email'));
156 LOAD_TEMPLATE("admin_settings_saved", false, $OUT);
157 } elseif (REQUEST_ISSET_GET(('hash'))) {
158 // Output form for hash validation
159 LOAD_TEMPLATE("admin_validate_reset_hash_form", false, REQUEST_GET('hash'));
160 } elseif ((REQUEST_ISSET_POST(('validate_hash'))) && (REQUEST_ISSET_POST(('login'))) && (REQUEST_ISSET_POST(('hash')))) {
161 // Validate the login data and hash
162 $valid = ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN(REQUEST_POST('hash'), REQUEST_POST('login'));
165 if ($valid === true) {
166 // Prepare content first
168 'hash' => SQL_ESCAPE(REQUEST_POST('hash')),
169 'login' => SQL_ESCAPE(REQUEST_POST('login'))
172 // Validation okay so display form for final password change
173 LOAD_TEMPLATE("admin_reset_password_form", false, $content);
175 // Cannot validate the login data and hash
176 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED'));
178 } elseif ((REQUEST_ISSET_POST(('reset_pass'))) && (REQUEST_ISSET_POST(('hash'))) && (REQUEST_ISSET_POST(('login'))) && (REQUEST_ISSET_POST(('pass1'))) && (REQUEST_POST('pass1') == REQUEST_POST('pass2'))) {
179 // Okay, we shall the admin password here. So first revalidate the hash
180 if (ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN(REQUEST_POST('hash'), REQUEST_POST('login'))) {
181 // Set the password now
182 $OUT = ADMIN_RESET_PASSWORD(REQUEST_POST('login'), REQUEST_POST('pass1'));
185 LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
188 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED2'));
191 // Output reset password form
192 LOAD_TEMPLATE("admin_send_reset_link");
194 } 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())) {
195 // At leat one administrator account was created
196 if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
197 // Timeout for last login, we have to logout first!
198 LOAD_URL("modules.php?module=admin&action=login&logout=1");
201 if (REQUEST_ISSET_GET(('register'))) {
202 // Registration of first admin is done
203 if (REQUEST_GET('register') == "done") LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_REGISTER_DONE'));
206 // Check if the admin has submitted data or not
207 if ((IS_FORM_SENT()) && ((!REQUEST_ISSET_POST(('login'))) || (!REQUEST_ISSET_POST(('pass'))) || (strlen(REQUEST_POST('pass')) < 4))) {
208 REQUEST_SET_POST('ok', "***");
211 if ((IS_FORM_SENT()) && (REQUEST_POST('ok') != "***")) {
212 // All required data was entered so we check his account
213 $ret = CHECK_ADMIN_LOGIN(REQUEST_POST('login'), REQUEST_POST('pass'));
215 // Which status do we have?
218 case "done": // Admin and password are okay, so we log in now
219 // Construct URL and redirect
220 $URL = "modules.php?module=admin&";
222 // Rewrite overview module
223 if ($GLOBALS['what'] == "overview") {
224 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
228 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
229 elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
230 elseif (REQUEST_ISSET_GET(('area'))) $URL .= "area=".REQUEST_GET('area');
236 case "404": // Administrator login not found
237 REQUEST_SET_POST('ok', $ret);
238 $ret = getMessage('ADMIN_NOT_FOUND');
239 destroyAdminSession();
242 case "pass": // Wrong password
243 REQUEST_SET_POST('ok', $ret);
244 $ret = "{--WRONG_PASS--} [<a href=\"{!URL!}/modules.php?module=admin&reset_pass=1\">{--ADMIN_RESET_PASS--}</a>]\n";
245 destroyAdminSession();
248 default: // Others will be logged
249 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown return code %s from CHECK_ADMIN_LOGIN()", $ret));
255 if ($ret != "done") {
256 if (REQUEST_ISSET_POST(('login'))) {
257 define('__LOGIN_VALUE', REQUEST_POST('login'));
259 define('__LOGIN_VALUE', "");
262 if (IS_FORM_SENT()) {
263 // Set messages to zero
264 $MSG1 = ""; $MSG2 = "";
267 if (!REQUEST_ISSET_POST(('login'))) $MSG1 = getMessage('ADMIN_NO_LOGIN');
269 // An error comes back from login?
270 if ((!empty($ret)) && (REQUEST_POST('ok') == "404")) $MSG1 = $ret;
272 // No password entered?
273 if (!REQUEST_ISSET_POST(('pass'))) $MSG2 = getMessage('ADMIN_NO_PASS');
275 // Or password too short?
276 if (strlen(REQUEST_POST('pass')) < 4) $MSG2 = getMessage('ADMIN_SHORT_PASS');
278 // An error comes back from login?
279 if ((!empty($ret)) && (REQUEST_POST('ok') == "pass")) $MSG2 = $ret;
281 // Load message template
282 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
283 define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
286 $MSG1 = ""; $MSG2 = "";
288 // Set constants to empty for hiding them
289 define('__MSG_LOGIN', "");
290 define('__MSG_PASS' , "");
294 if (!empty($GLOBALS['what'])) {
295 // Restore old what value
296 $content = array('target' => "what", 'value' => $GLOBALS['what']);
297 } elseif (!empty($GLOBALS['action'])) {
298 if ($GLOBALS['action'] != "logout") {
299 // Restore old action value
300 $content = array('target' => "action", 'value' => $GLOBALS['action']);
302 // Set default values
303 $content = array('target' => "action", 'value' => "login");
305 } elseif (REQUEST_ISSET_GET(('area'))) {
306 // Restore old area value
307 $content = array('target' => "area", 'value' => REQUEST_GET('area'));
309 // Set default values
310 $content = array('target' => "action", 'value' => "login");
313 // Load login form template
314 LOAD_TEMPLATE("admin_login_form", false, $content);
316 } elseif (REQUEST_ISSET_GET(('logout'))) {
317 // Only try to remove cookies
318 if (destroyAdminSession()) {
319 // Load logout template
320 if (REQUEST_ISSET_GET(('register'))) {
322 $register = SQL_ESCAPE(REQUEST_GET('register'));
324 // Special logout redirect for installation of given extension
325 LOAD_TEMPLATE(sprintf("admin_logout_%s_install", $register));
326 } elseif (REQUEST_ISSET_GET(('remove'))) {
328 $remove = SQL_ESCAPE(REQUEST_GET('remove'));
330 // Special logout redirect for removal of given extension
331 LOAD_TEMPLATE(sprintf("admin_logout_%s_remove", $remove));
333 // Logged out normally
334 LOAD_TEMPLATE("admin_logout");
337 // Something went wrong here...
338 LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"admin_fatal\">{--ADMIN_LOGOUT_FAILED--}</div>");
341 addFatalMessage(getMessage('CANNOT_UNREG_SESS'));
344 // Maybe an Admin want's to login?
345 $ret = CHECK_ADMIN_COOKIES(get_session('admin_login'), get_session('admin_md5'));
349 // Check for access control line of current menu entry
350 define('__ACL_ALLOW', RUN_FILTER('check_admin_acl'));
352 // When type of admin menu is not set fallback to old menu system
353 if (!isConfigEntrySet('admin_menu')) setConfigEntry('admin_menu', "OLD");
355 // Check for version and switch between old menu system and new "intelligent menu system"
356 if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (INCLUDE_READABLE("inc/modules/admin/lasys-inc.php"))) {
357 // Default area is the entrance, of course
360 // Check for similar URL variable
361 if (REQUEST_ISSET_GET(('area'))) $area = SQL_ESCAPE(REQUEST_GET('area'));
363 // Load "logical-area menu-system" file
364 LOAD_INC_ONCE("inc/modules/admin/lasys-inc.php");
366 // Create new-style menu system will "logical areas"
367 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
369 // This little call constructs the whole default old and lacky menu system
371 ADMIN_DO_ACTION($GLOBALS['what']);
375 case "404": // Administrator login not found
376 REQUEST_SET_POST('ok', $ret);
377 destroyAdminSession();
378 addFatalMessage(getMessage('ADMIN_NOT_FOUND'));
381 case "pass": // Wrong password
382 REQUEST_SET_POST('ok', $ret);
383 destroyAdminSession();
384 addFatalMessage(getMessage('WRONG_PASS'));
387 default: // Others will be logged
388 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown return code %s from CHECK_ADMIN_COOKIES()", $ret));