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 LOAD_INC_ONCE("inc/modules/admin/admin-inc.php");
43 // Fix "deleted" cookies in PHP4 (PHP5 does remove them, PHP4 sets them to deleted!)
44 FIX_DELETED_COOKIES(array('admin_login', 'admin_md5', 'admin_last', 'admin_to'));
49 // Is no admin registered?
50 if (!isAdminRegistered()) {
51 // Admin is not registered so we have to inform the user
52 if ((IS_FORM_SENT()) && ((!REQUEST_ISSET_POST(('login'))) || (!REQUEST_ISSET_POST(('pass'))) || (strlen(REQUEST_POST('pass')) < 4))) {
53 REQUEST_SET_POST('ok', "***");
56 if ((IS_FORM_SENT()) && (REQUEST_POST('ok') != "***")) {
57 // Hash the password with the old function because we are here in install mode
58 $hashedPass = md5(REQUEST_POST('pass'));
60 // Kill maybe existing session variables
61 destroyAdminSession(false);
64 $ret = REGISTER_ADMIN(REQUEST_POST('login'), $hashedPass, constant('WEBMASTER'));
68 $done = changeDataInFile(constant('PATH')."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
70 // Registering is done
71 LOAD_URL("modules.php?module=admin&register=done");
73 $ret = getMessage('ADMIN_CANNOT_COMPLETE');
78 $ret = getMessage('ADMIN_REGISTER_FAILED');
83 if ($ret == "already") {
84 // Admin does already exists!
85 $ret = getMessage('ADMIN_LOGIN_ALREADY_REG');
87 // Any other kind will be logged and interpreted as 'done'
88 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown return code %s from CHECK_ADMIN_LOGIN() and interpreted as 'done'!", $ret));
89 // @TODO Why is this set to 'done'?
93 // Admin still not registered?
94 if (!isAdminRegistered()) {
95 // Write to config that registration is done
96 changeDataInFile(constant('PATH')."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
99 LOAD_URL("modules.php?module=admin");
105 // Whas that action okay?
106 if ($ret != "done") {
107 // Fixes another "Notice"
108 if (REQUEST_ISSET_POST(('login'))) {
109 define('__LOGIN_VALUE', REQUEST_POST('login'));
111 define('__LOGIN_VALUE', "");
114 // Yet-another "Notice" fix
115 if ((IS_FORM_SENT()) && (REQUEST_POST('ok') == "***")) {
117 if (!REQUEST_ISSET_POST(('login'))) $MSG1 = getMessage('ADMIN_NO_LOGIN');
119 // An error comes back from registration?
120 if (!empty($ret)) $MSG1 = $ret;
122 // No password entered?
123 if (!REQUEST_ISSET_POST(('pass'))) $MSG2 = getMessage('ADMIN_NO_PASS');
125 // Or password too short?
126 if (strlen(REQUEST_POST('pass')) < 4) $MSG2 = getMessage('ADMIN_SHORT_PASS');
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));
133 $MSG1 = ""; $MSG2 = "";
135 // Reset values to nothing
136 define('__MSG_LOGIN', "");
137 define('__MSG_PASS' , "");
140 // Load register template
141 LOAD_TEMPLATE("admin_reg_form");
143 } elseif (REQUEST_ISSET_GET(('reset_pass'))) {
144 // Is the form submitted?
145 if ((REQUEST_ISSET_POST(('send_link'))) && (REQUEST_ISSET_POST(('email')))) {
146 // Try to send the link out
147 $OUT = ADMIN_SEND_PASSWORD_RESET_LINK(REQUEST_POST('email'));
150 LOAD_TEMPLATE("admin_settings_saved", false, $OUT);
151 } elseif (REQUEST_ISSET_GET(('hash'))) {
152 // Output form for hash validation
153 LOAD_TEMPLATE("admin_validate_reset_hash_form", false, REQUEST_GET('hash'));
154 } elseif ((REQUEST_ISSET_POST(('validate_hash'))) && (REQUEST_ISSET_POST(('login'))) && (REQUEST_ISSET_POST(('hash')))) {
155 // Validate the login data and hash
156 $valid = ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN(REQUEST_POST('hash'), REQUEST_POST('login'));
159 if ($valid === true) {
160 // Prepare content first
162 'hash' => SQL_ESCAPE(REQUEST_POST('hash')),
163 'login' => SQL_ESCAPE(REQUEST_POST('login'))
166 // Validation okay so display form for final password change
167 LOAD_TEMPLATE("admin_reset_password_form", false, $content);
169 // Cannot validate the login data and hash
170 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED'));
172 } elseif ((REQUEST_ISSET_POST(('reset_pass'))) && (REQUEST_ISSET_POST(('hash'))) && (REQUEST_ISSET_POST(('login'))) && (REQUEST_ISSET_POST(('pass1'))) && (REQUEST_POST('pass1') == REQUEST_POST('pass2'))) {
173 // Okay, we shall the admin password here. So first revalidate the hash
174 if (ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN(REQUEST_POST('hash'), REQUEST_POST('login'))) {
175 // Set the password now
176 $OUT = ADMIN_RESET_PASSWORD(REQUEST_POST('login'), REQUEST_POST('pass1'));
179 LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
182 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED2'));
185 // Output reset password form
186 LOAD_TEMPLATE("admin_send_reset_link");
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 LOAD_URL("modules.php?module=admin&logout=1");
195 if (REQUEST_ISSET_GET(('register'))) {
196 // Registration of first admin is done
197 if (REQUEST_GET('register') == "done") LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_REGISTER_DONE'));
200 // Check if the admin has submitted data or not
201 if ((IS_FORM_SENT()) && ((!REQUEST_ISSET_POST(('login'))) || (!REQUEST_ISSET_POST(('pass'))) || (strlen(REQUEST_POST('pass')) < 4))) {
202 REQUEST_SET_POST('ok', "***");
205 if ((IS_FORM_SENT()) && (REQUEST_POST('ok') != "***")) {
206 // All required data was entered so we check his account
207 $ret = CHECK_ADMIN_LOGIN(REQUEST_POST('login'), REQUEST_POST('pass'));
209 // Which status do we have?
212 case "done": // Admin and password are okay, so we log in now
213 // Construct URL and redirect
214 $URL = "modules.php?module=admin&";
216 // Rewrite overview module
217 if ($GLOBALS['what'] == "overview") {
218 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
222 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
223 elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
224 elseif (REQUEST_ISSET_GET(('area'))) $URL .= "area=".REQUEST_GET('area');
230 case "404": // Administrator login not found
231 REQUEST_SET_POST('ok', $ret);
232 $ret = getMessage('ADMIN_NOT_FOUND');
233 destroyAdminSession();
236 case "pass": // Wrong password
237 REQUEST_SET_POST('ok', $ret);
238 $ret = "{--WRONG_PASS--} [<a href=\"{!URL!}/modules.php?module=admin&reset_pass=1\">{--ADMIN_RESET_PASS--}</a>]\n";
239 destroyAdminSession();
242 default: // Others will be logged
243 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown return code %s from CHECK_ADMIN_LOGIN()", $ret));
249 if ($ret != "done") {
250 if (REQUEST_ISSET_POST(('login'))) {
251 define('__LOGIN_VALUE', REQUEST_POST('login'));
253 define('__LOGIN_VALUE', "");
256 if (IS_FORM_SENT()) {
257 // Set messages to zero
258 $MSG1 = ""; $MSG2 = "";
261 if (!REQUEST_ISSET_POST(('login'))) $MSG1 = getMessage('ADMIN_NO_LOGIN');
263 // An error comes back from login?
264 if ((!empty($ret)) && (REQUEST_POST('ok') == "404")) $MSG1 = $ret;
266 // No password entered?
267 if (!REQUEST_ISSET_POST(('pass'))) $MSG2 = getMessage('ADMIN_NO_PASS');
269 // Or password too short?
270 if (strlen(REQUEST_POST('pass')) < 4) $MSG2 = getMessage('ADMIN_SHORT_PASS');
272 // An error comes back from login?
273 if ((!empty($ret)) && (REQUEST_POST('ok') == "pass")) $MSG2 = $ret;
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));
280 $MSG1 = ""; $MSG2 = "";
282 // Set constants to empty for hiding them
283 define('__MSG_LOGIN', "");
284 define('__MSG_PASS' , "");
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']);
296 // Set default values
297 $content = array('target' => "action", 'value' => "login");
299 } elseif (REQUEST_ISSET_GET(('area'))) {
300 // Restore old area value
301 $content = array('target' => "area", 'value' => REQUEST_GET('area'));
303 // Set default values
304 $content = array('target' => "action", 'value' => "login");
307 // Load login form template
308 LOAD_TEMPLATE("admin_login_form", false, $content);
310 } elseif (REQUEST_ISSET_GET(('logout'))) {
311 // Only try to remove cookies
312 if (destroyAdminSession()) {
313 // Load logout template
314 if (REQUEST_ISSET_GET(('register'))) {
316 $register = REQUEST_GET(('register'));
318 // Special logout redirect for installation of given extension
319 LOAD_TEMPLATE(sprintf("admin_logout_%s_install", $register));
320 } elseif (REQUEST_ISSET_GET(('remove'))) {
322 $remove = REQUEST_GET(('remove'));
324 // Special logout redirect for removal of given extension
325 LOAD_TEMPLATE(sprintf("admin_logout_%s_remove", $remove));
327 // Logged out normally
328 LOAD_TEMPLATE("admin_logout");
331 // Something went wrong here...
332 LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"admin_fatal\">{--ADMIN_LOGOUT_FAILED--}</div>");
335 addFatalMessage(__FILE__, __LINE__, getMessage('CANNOT_UNREG_SESS'));
338 // Maybe an Admin want's to login?
339 $ret = CHECK_ADMIN_COOKIES(get_session('admin_login'), get_session('admin_md5'));
343 // Check for access control line of current menu entry
344 $GLOBALS['acl_allow'] = runFilterChain('check_admin_acl');
346 // When type of admin menu is not set fallback to old menu system
347 if (!isConfigEntrySet('admin_menu')) setConfigEntry('admin_menu', "OLD");
349 // Check for version and switch between old menu system and new "intelligent menu system"
350 if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (INCLUDE_READABLE("inc/modules/admin/lasys-inc.php"))) {
351 // Default area is the entrance, of course
354 // Check for similar URL variable
355 if (REQUEST_ISSET_GET(('area'))) $area = REQUEST_GET(('area'));
357 // Load "logical-area menu-system" file
358 LOAD_INC_ONCE("inc/modules/admin/lasys-inc.php");
360 // Create new-style menu system will "logical areas"
361 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
363 // This little call constructs the whole default old and lacky menu system
365 ADMIN_DO_ACTION($GLOBALS['what']);
369 case "404": // Administrator login not found
370 REQUEST_SET_POST('ok', $ret);
371 destroyAdminSession();
372 addFatalMessage(__FILE__, __LINE__, getMessage('ADMIN_NOT_FOUND'));
375 case "pass": // Wrong password
376 REQUEST_SET_POST('ok', $ret);
377 destroyAdminSession();
378 addFatalMessage(__FILE__, __LINE__, getMessage('WRONG_PASS'));
381 default: // Others will be logged
382 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown return code %s from CHECK_ADMIN_COOKIES()", $ret));