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 require_once(PATH."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 ((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']);
63 // Kill maybe existing session variables
64 DESTROY_ADMIN_SESSION(false);
67 $ret = REGISTER_ADMIN($_POST['login'], $hashedPass);
71 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
73 // Registering is done
74 LOAD_URL("modules.php?module=admin&action=login®ister=done");
76 $ret = ADMIN_CANNOT_COMPLETE;
81 $ret = ADMIN_REGISTER_FAILED;
86 if ($ret == "already") {
87 // Admin does already exists!
88 $ret = ADMIN_LOGIN_ALREADY_REG;
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'?
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);
101 // Load URL for login
102 LOAD_URL("modules.php?module=admin&action=login");
108 // Whas that action okay?
109 if ($ret != "done") {
110 // Fixes another "Notice"
111 if (!empty($_POST['login'])) {
112 define('__LOGIN_VALUE', $_POST['login']);
114 define('__LOGIN_VALUE', "");
117 // Yet-another "Notice" fix
118 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
120 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
122 // An error comes back from registration?
123 if (!empty($ret)) $MSG1 = $ret;
125 // No password entered?
126 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
128 // Or password too short?
129 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
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));
136 $MSG1 = ""; $MSG2 = "";
138 // Reset values to nothing
139 define('__MSG_LOGIN', "");
140 define('__MSG_PASS' , "");
143 // Load register template
144 LOAD_TEMPLATE("admin_reg_form");
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']);
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']);
163 // Prepare content first
165 'hash' => SQL_ESCAPE($_POST['hash']),
166 'login' => SQL_ESCAPE($_POST['login'])
169 // Validation okay so display form for final password change
170 LOAD_TEMPLATE("admin_reset_password_form", false, $content);
172 // Cannot validate the login data and hash
173 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED);
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']);
182 LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
185 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED2);
188 // Output reset password form
189 LOAD_TEMPLATE("admin_send_reset_link");
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&action=login&logout=1");
198 if (!empty($_GET['register'])) {
199 // Registration of first admin is done
200 if ($_GET['register'] == "done") LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"admin\">".ADMIN_REGISTER_DONE."</div>");
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']);
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 (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
230 case "404": // Administrator login not found
232 $ret = ADMIN_NOT_FOUND;
233 DESTROY_ADMIN_SESSION();
236 case "pass": // Wrong password
238 $ret = WRONG_PASS." [<a href=\"".URL."/modules.php?module=admin&reset_pass=1\">".ADMIN_RESET_PASS."</a>]\n";
239 DESTROY_ADMIN_SESSION();
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 (!empty($_POST['login'])) {
251 define('__LOGIN_VALUE', $_POST['login']);
253 define('__LOGIN_VALUE', "");
256 if (isset($_POST['ok'])) {
257 // Set messages to zero
258 $MSG1 = ""; $MSG2 = "";
261 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
263 // An error comes back from login?
264 if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
266 // No password entered?
267 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
269 // Or password too short?
270 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
272 // An error comes back from login?
273 if ((!empty($ret)) && ($_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 (!empty($_GET['area'])) {
300 // Restore old area value
301 $content = array('target' => "area", 'value' => $_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 (isset($_GET['logout'])) {
311 // Only try to remove cookies
312 if (DESTROY_ADMIN_SESSION()) {
313 // Load logout template
314 if (isset($_GET['register'])) {
316 $register = SQL_ESCAPE($_GET['register']);
318 // Special logout redirect for installation of given extension
319 LOAD_TEMPLATE(sprintf("admin_logout_%s_install", $register));
320 } elseif (isset($_GET['remove'])) {
322 $remove = SQL_ESCAPE($_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(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 define('__ACL_ALLOW', RUN_FILTER('check_admin_acl'));
346 // When type of admin menu is not set fallback to old menu system
347 if (getConfig('admin_menu') == null) $_CONFIG['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") && (FILE_READABLE(PATH."inc/modules/admin/lasys-inc.php"))) {
351 // Default area is the entrance, of course
354 // Check for similar URL variable
355 if (!empty($_GET['area'])) $area = SQL_ESCAPE($_GET['area']);
357 // Load "logical-area menu-system" file
358 require_once(PATH."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
371 DESTROY_ADMIN_SESSION();
372 addFatalMessage(ADMIN_NOT_FOUND);
375 case "pass": // Wrong password
377 DESTROY_ADMIN_SESSION();
378 addFatalMessage(WRONG_PASS);
381 default: // Others will be logged
382 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown return code %s from CHECK_ADMIN_COOKIES()", $ret));