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 set_session('admin_login' , "");
61 set_session('admin_md5' , "");
62 set_session('admin_last' , "");
63 set_session('admin_to' , "");
66 $ret = REGISTER_ADMIN($_POST['login'], $hashedPass);
70 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
72 // Registering is done
73 LOAD_URL(URL."/modules.php?module=admin&action=login®ister=done");
75 $ret = ADMIN_CANNOT_COMPLETE;
80 $ret = ADMIN_REGISTER_FAILED;
85 if ($ret == "already") {
86 // Admin does already exists!
87 $ret = ADMIN_LOGIN_ALREADY_REG;
93 // Admin still not registered?
94 if (!isBooleanConstantAndTrue('admin_registered')) {
95 // Write to config that registration is done
96 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
99 $URL = URL."/modules.php?module=admin&action=login";
106 // Whas that action okay?
107 if ($ret != "done") {
108 // Fixes another "Notice"
109 if (!empty($_POST['login'])) {
110 define('__LOGIN_VALUE', $_POST['login']);
112 define('__LOGIN_VALUE', "");
115 // Yet-another "Notice" fix
116 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
118 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
120 // An error comes back from registration?
121 if (!empty($ret)) $MSG1 = $ret;
123 // No password entered?
124 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
126 // Or password too short?
127 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
129 // Output error messages
130 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
131 define('__MSG_PASS', LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
134 $MSG1 = ""; $MSG2 = "";
136 // Reset values to nothing
137 define('__MSG_LOGIN', "");
138 define('__MSG_PASS' , "");
141 // Load register template
142 LOAD_TEMPLATE("admin_reg_form");
144 } elseif (isset($_GET['reset_pass'])) {
145 // Is the form submitted?
146 if ((isset($_POST['send_link'])) && (!empty($_POST['email']))) {
147 // Try to send the link out
148 $OUT = ADMIN_SEND_PASSWORD_RESET_LINK($_POST['email']);
151 LOAD_TEMPLATE("admin_settings_saved", false, $OUT);
152 } elseif (!empty($_GET['hash'])) {
153 // Output form for hash validation
154 LOAD_TEMPLATE("admin_validate_reset_hash_form", false, $_GET['hash']);
155 } elseif ((isset($_POST['validate_hash'])) && (!empty($_POST['login'])) && (!empty($_POST['hash']))) {
156 // Validate the login data and hash
157 $valid = ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login']);
161 // Prepare content first
163 'hash' => SQL_ESCAPE($_POST['hash']),
164 'login' => SQL_ESCAPE($_POST['login'])
167 // Validation okay so display form for final password change
168 LOAD_TEMPLATE("admin_reset_password_form", false, $content);
170 // Cannot validate the login data and hash
171 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED);
173 } elseif ((isset($_POST['reset_pass'])) && (!empty($_POST['hash'])) && (!empty($_POST['login'])) && (!empty($_POST['pass1'])) && ($_POST['pass1'] == $_POST['pass2'])) {
174 // Okay, we shall the admin password here. So first revalidate the hash
175 if (ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login'])) {
176 // Set the password now
177 $OUT = ADMIN_RESET_PASSWORD($_POST['login'], $_POST['pass1']);
180 LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
183 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED2);
186 // Output reset password form
187 LOAD_TEMPLATE("admin_send_reset_link");
189 } 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())) {
190 // At leat one administrator account was created
191 if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
192 // Timeout for last login, we have to logout first!
193 $URL = URL."/modules.php?module=admin&action=login&logout=1";
196 if (!empty($_GET['register'])) {
197 // Registration of first admin is done
198 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
201 // Check if the admin has submitted data or not
203 if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
204 if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
205 // All required data was entered so we check his account
206 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
208 // Which status do we have?
211 case "done": // Admin and password are okay, so we log in now
212 // Construct URL and redirect
213 $URL = URL."/modules.php?module=admin&";
215 // Rewrite overview module
216 if ($GLOBALS['what'] == "overview") {
217 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
221 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
222 elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
223 elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
229 case "404": // Administrator login not found
231 $ret = ADMIN_NOT_FOUND;
232 DESTROY_ADMIN_SESSION();
235 case "pass": // Wrong password
237 $ret = WRONG_PASS." [<A href=\"".URL."/modules.php?module=admin&reset_pass=1\">".ADMIN_RESET_PASS."</A>]\n";
238 DESTROY_ADMIN_SESSION();
242 if ($ret != "done") {
243 if (!empty($_POST['login'])) {
244 define('__LOGIN_VALUE', $_POST['login']);
246 define('__LOGIN_VALUE', "");
249 if (isset($_POST['ok'])) {
250 // Set messages to zero
252 $MSG1 = ""; $MSG2 = "";
254 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
256 // An error comes back from login?
257 if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
259 // No password entered?
260 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
262 // Or password too short?
263 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
265 // An error comes back from login?
266 if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
268 // Load message template
269 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
270 define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
273 $MSG1 = ""; $MSG2 = "";
275 // Set constants to empty for hiding them
276 define('__MSG_LOGIN', "");
277 define('__MSG_PASS' , "");
281 if (!empty($GLOBALS['what'])) {
282 // Restore old what value
283 $content = array('target' => "what", 'value' => $GLOBALS['what']);
284 } elseif (!empty($GLOBALS['action'])) {
285 if ($GLOBALS['action'] != "logout") {
286 // Restore old action value
287 $content = array('target' => "action", 'value' => $GLOBALS['action']);
289 // Set default values
290 $content = array('target' => "action", 'value' => "login");
292 } elseif (!empty($_GET['area'])) {
293 // Restore old area value
294 $content = array('target' => "area", 'value' => $_GET['area']);
296 // Set default values
297 $content = array('target' => "action", 'value' => "login");
300 // Load login form template
301 LOAD_TEMPLATE("admin_login_form", false, $content);
303 } elseif (isset($_GET['logout'])) {
304 // Only try to remove cookies
305 if (DESTROY_ADMIN_SESSION()) {
306 // Load logout template
307 if (isset($_GET['register'])) {
309 $register = SQL_ESCAPE($_GET['register']);
311 // Special logout redirect for installation of given extension
312 LOAD_TEMPLATE(sprintf("admin_logout_%s_install", $register));
313 } elseif (isset($_GET['remove'])) {
315 $remove = SQL_ESCAPE($_GET['remove']);
317 // Special logout redirect for removal of given extension
318 LOAD_TEMPLATE(sprintf("admin_logout_%s_remove", $remove));
320 // Logged out normally
321 LOAD_TEMPLATE("admin_logout");
324 // Something went wrong here...
325 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
328 ADD_FATAL(CANNOT_UNREG_SESS);
331 // Maybe an Admin want's to login?
332 $ret = CHECK_ADMIN_COOKIES(get_session('admin_login'), get_session('admin_md5'));
336 // Cookie-Data accepted
337 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'))))) {
338 // Ok, Cookie-Update done
339 if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
340 // Check if action GET variable was set
341 $act = SQL_ESCAPE($GLOBALS['action']);
342 if (!empty($GLOBALS['what'])) {
343 // Get action value by what-value
344 $act = GET_ACTION("admin", $GLOBALS['what']);
347 // Check for access control line of current menu entry
348 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
350 // Extension not installed so it's always allowed to access everywhere!
351 define('__ACL_ALLOW', true);
354 // When type of admin menu is not set fallback to old menu system
355 if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
357 // Check for version and switch between old menu system and new "intelligent menu system"
358 if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (FILE_READABLE(PATH."inc/modules/admin/lasys-inc.php"))) {
359 // Default area is the entrance, of course
362 // Check for similar URL variable
363 if (!empty($_GET['area'])) $area = SQL_ESCAPE($_GET['area']);
365 // Load "logical-area menu-system" file
366 require_once(PATH."inc/modules/admin/lasys-inc.php");
368 // Create new-style menu system will "logical areas"
369 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
371 // This little call constructs the whole default old and lacky menu system
373 ADMIN_DO_ACTION($GLOBALS['what']);
376 // Login failed (cookies enabled?)
377 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
378 ADD_FATAL(CANNOT_RE_REGISTER_SESS);
382 case "404": // Administrator login not found
384 DESTROY_ADMIN_SESSION();
385 ADD_FATAL(ADMIN_NOT_FOUND);
388 case "pass": // Wrong password
390 DESTROY_ADMIN_SESSION();
391 ADD_FATAL(WRONG_PASS);