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'));
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']);
59 // Kill maybe existing session variables
60 DESTROY_ADMIN_SESSION(false);
63 $ret = REGISTER_ADMIN($_POST['login'], $hashedPass);
67 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
69 // Registering is done
70 LOAD_URL(URL."/modules.php?module=admin&action=login®ister=done");
72 $ret = ADMIN_CANNOT_COMPLETE;
77 $ret = ADMIN_REGISTER_FAILED;
82 if ($ret == "already") {
83 // Admin does already exists!
84 $ret = ADMIN_LOGIN_ALREADY_REG;
90 // Admin still not registered?
91 if (!isBooleanConstantAndTrue('admin_registered')) {
92 // Write to config that registration is done
93 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
96 LOAD_URL(URL."/modules.php?module=admin&action=login");
102 // Whas that action okay?
103 if ($ret != "done") {
104 // Fixes another "Notice"
105 if (!empty($_POST['login'])) {
106 define('__LOGIN_VALUE', $_POST['login']);
108 define('__LOGIN_VALUE', "");
111 // Yet-another "Notice" fix
112 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
114 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
116 // An error comes back from registration?
117 if (!empty($ret)) $MSG1 = $ret;
119 // No password entered?
120 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
122 // Or password too short?
123 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
125 // Output error messages
126 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
127 define('__MSG_PASS', LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
130 $MSG1 = ""; $MSG2 = "";
132 // Reset values to nothing
133 define('__MSG_LOGIN', "");
134 define('__MSG_PASS' , "");
137 // Load register template
138 LOAD_TEMPLATE("admin_reg_form");
140 } elseif (isset($_GET['reset_pass'])) {
141 // Is the form submitted?
142 if ((isset($_POST['send_link'])) && (!empty($_POST['email']))) {
143 // Try to send the link out
144 $OUT = ADMIN_SEND_PASSWORD_RESET_LINK($_POST['email']);
147 LOAD_TEMPLATE("admin_settings_saved", false, $OUT);
148 } elseif (!empty($_GET['hash'])) {
149 // Output form for hash validation
150 LOAD_TEMPLATE("admin_validate_reset_hash_form", false, $_GET['hash']);
151 } elseif ((isset($_POST['validate_hash'])) && (!empty($_POST['login'])) && (!empty($_POST['hash']))) {
152 // Validate the login data and hash
153 $valid = ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login']);
157 // Prepare content first
159 'hash' => SQL_ESCAPE($_POST['hash']),
160 'login' => SQL_ESCAPE($_POST['login'])
163 // Validation okay so display form for final password change
164 LOAD_TEMPLATE("admin_reset_password_form", false, $content);
166 // Cannot validate the login data and hash
167 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED);
169 } elseif ((isset($_POST['reset_pass'])) && (!empty($_POST['hash'])) && (!empty($_POST['login'])) && (!empty($_POST['pass1'])) && ($_POST['pass1'] == $_POST['pass2'])) {
170 // Okay, we shall the admin password here. So first revalidate the hash
171 if (ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login'])) {
172 // Set the password now
173 $OUT = ADMIN_RESET_PASSWORD($_POST['login'], $_POST['pass1']);
176 LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
179 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED2);
182 // Output reset password form
183 LOAD_TEMPLATE("admin_send_reset_link");
185 } 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())) {
186 // At leat one administrator account was created
187 if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
188 // Timeout for last login, we have to logout first!
189 LOAD_URL(URL."/modules.php?module=admin&action=login&logout=1");
192 if (!empty($_GET['register'])) {
193 // Registration of first admin is done
194 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
197 // Check if the admin has submitted data or not
199 if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
200 if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
201 // All required data was entered so we check his account
202 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
204 // Which status do we have?
207 case "done": // Admin and password are okay, so we log in now
208 // Construct URL and redirect
209 $URL = URL."/modules.php?module=admin&";
211 // Rewrite overview module
212 if ($GLOBALS['what'] == "overview") {
213 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
217 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
218 elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
219 elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
225 case "404": // Administrator login not found
227 $ret = ADMIN_NOT_FOUND;
228 DESTROY_ADMIN_SESSION();
231 case "pass": // Wrong password
233 $ret = WRONG_PASS." [<A href=\"".URL."/modules.php?module=admin&reset_pass=1\">".ADMIN_RESET_PASS."</A>]\n";
234 DESTROY_ADMIN_SESSION();
240 if ($ret != "done") {
241 if (!empty($_POST['login'])) {
242 define('__LOGIN_VALUE', $_POST['login']);
244 define('__LOGIN_VALUE', "");
247 if (isset($_POST['ok'])) {
248 // Set messages to zero
249 $MSG1 = ""; $MSG2 = "";
252 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
254 // An error comes back from login?
255 if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
257 // No password entered?
258 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
260 // Or password too short?
261 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
263 // An error comes back from login?
264 if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
266 // Load message template
267 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
268 define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
271 $MSG1 = ""; $MSG2 = "";
273 // Set constants to empty for hiding them
274 define('__MSG_LOGIN', "");
275 define('__MSG_PASS' , "");
279 if (!empty($GLOBALS['what'])) {
280 // Restore old what value
281 $content = array('target' => "what", 'value' => $GLOBALS['what']);
282 } elseif (!empty($GLOBALS['action'])) {
283 if ($GLOBALS['action'] != "logout") {
284 // Restore old action value
285 $content = array('target' => "action", 'value' => $GLOBALS['action']);
287 // Set default values
288 $content = array('target' => "action", 'value' => "login");
290 } elseif (!empty($_GET['area'])) {
291 // Restore old area value
292 $content = array('target' => "area", 'value' => $_GET['area']);
294 // Set default values
295 $content = array('target' => "action", 'value' => "login");
298 // Load login form template
299 LOAD_TEMPLATE("admin_login_form", false, $content);
301 } elseif (isset($_GET['logout'])) {
302 // Only try to remove cookies
303 if (DESTROY_ADMIN_SESSION()) {
304 // Load logout template
305 if (isset($_GET['register'])) {
307 $register = SQL_ESCAPE($_GET['register']);
309 // Special logout redirect for installation of given extension
310 LOAD_TEMPLATE(sprintf("admin_logout_%s_install", $register));
311 } elseif (isset($_GET['remove'])) {
313 $remove = SQL_ESCAPE($_GET['remove']);
315 // Special logout redirect for removal of given extension
316 LOAD_TEMPLATE(sprintf("admin_logout_%s_remove", $remove));
318 // Logged out normally
319 LOAD_TEMPLATE("admin_logout");
322 // Something went wrong here...
323 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
326 ADD_FATAL(CANNOT_UNREG_SESS);
329 // Maybe an Admin want's to login?
330 $ret = CHECK_ADMIN_COOKIES(get_session('admin_login'), get_session('admin_md5'));
334 // Cookie-Data accepted
335 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'))))) {
336 // Ok, Cookie-Update done
337 // Check for access control line of current menu entry
338 define('__ACL_ALLOW', RUN_FILTER('check_admin_acl'));
340 // When type of admin menu is not set fallback to old menu system
341 if (getConfig('admin_menu') == null) $_CONFIG['admin_menu'] = "OLD";
343 // Check for version and switch between old menu system and new "intelligent menu system"
344 if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (FILE_READABLE(PATH."inc/modules/admin/lasys-inc.php"))) {
345 // Default area is the entrance, of course
348 // Check for similar URL variable
349 if (!empty($_GET['area'])) $area = SQL_ESCAPE($_GET['area']);
351 // Load "logical-area menu-system" file
352 require_once(PATH."inc/modules/admin/lasys-inc.php");
354 // Create new-style menu system will "logical areas"
355 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
357 // This little call constructs the whole default old and lacky menu system
359 ADMIN_DO_ACTION($GLOBALS['what']);
362 // Login failed (cookies enabled?)
363 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
364 ADD_FATAL(CANNOT_RE_REGISTER_SESS);
368 case "404": // Administrator login not found
370 DESTROY_ADMIN_SESSION();
371 ADD_FATAL(ADMIN_NOT_FOUND);
374 case "pass": // Wrong password
376 DESTROY_ADMIN_SESSION();
377 ADD_FATAL(WRONG_PASS);