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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
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 if (!isBooleanConstantAndTrue('admin_registered')) {
94 // Write to config that registration is done
95 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
98 $URL = URL."/modules.php?module=admin&action=login";
105 // Whas that action okay?
106 if ($ret != "done") {
107 // Fixes another "Notice"
108 if (!empty($_POST['login'])) {
109 define('__LOGIN_VALUE', $_POST['login']);
111 define('__LOGIN_VALUE', "");
114 // Yet-another "Notice" fix
115 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
117 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
119 // An error comes back from registration?
120 if (!empty($ret)) $MSG1 = $ret;
122 // No password entered?
123 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
125 // Or password too short?
126 if (strlen($_POST['pass']) < 4) $MSG2 = 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 (isset($_GET['reset_pass'])) {
144 // Is the form submitted?
145 if ((isset($_POST['send_link'])) && (!empty($_POST['email']))) {
146 // Try to send the link out
147 $OUT = ADMIN_SEND_PASSWORD_RESET_LINK($_POST['email']);
150 LOAD_TEMPLATE("admin_settings_saved", false, $OUT);
151 } elseif (!empty($_GET['hash'])) {
152 // Output form for hash validation
153 LOAD_TEMPLATE("admin_validate_reset_hash_form", false, SQL_ESCAPE($_GET['hash']));
154 } elseif ((isset($_POST['validate_hash'])) && (!empty($_POST['login'])) && (!empty($_POST['hash']))) {
155 // Validate the login data and hash
156 $valid = ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login']);
160 // Prepare content first
162 'hash' => SQL_ESCAPE($_POST['hash']),
163 'login' => SQL_ESCAPE($_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, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED);
172 } elseif ((isset($_POST['reset_pass'])) && (!empty($_POST['hash'])) && (!empty($_POST['login'])) && (!empty($_POST['pass1'])) && ($_POST['pass1'] == $_POST['pass2'])) {
173 // Okay, we shall the admin password here. So first revalidate the hash
174 if (ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login'])) {
175 // Set the password now
176 $OUT = ADMIN_RESET_PASSWORD($_POST['login'], $_POST['pass1']);
179 LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
182 LOAD_TEMPLATE("admin_settings_saved", false, 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 $URL = URL."/modules.php?module=admin&action=login&logout=1";
195 if (!empty($_GET['register'])) {
196 // Registration of first admin is done
197 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
200 // Check if the admin has submitted data or not
202 if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
203 if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
204 // All required data was entered so we check his account
205 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
208 case "done": // Admin and password are okay, so we log in now
209 // Construct URL and redirect
210 $URL = URL."/modules.php?module=admin&";
212 // Rewrite overview module
213 if ($GLOBALS['what'] == "overview") {
214 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
218 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
219 elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
220 elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
226 case "404": // Administrator login not found
228 $ret = ADMIN_NOT_FOUND;
231 case "pass": // Wrong password
233 $ret = WRONG_PASS." [<A href=\"".URL."/modules.php?module=admin&reset_pass=1\">".ADMIN_RESET_PASS."</A>]\n";
237 if ($ret != "done") {
238 if (!empty($_POST['login'])) {
239 define('__LOGIN_VALUE', $_POST['login']);
241 define('__LOGIN_VALUE', "");
244 if (isset($_POST['ok'])) {
245 // Set messages to zero
247 $MSG1 = ""; $MSG2 = "";
249 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
251 // An error comes back from login?
252 if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
254 // No password entered?
255 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
257 // Or password too short?
258 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
260 // An error comes back from login?
261 if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
263 // Load message template
264 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
265 define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
268 $MSG1 = ""; $MSG2 = "";
270 // Set constants to empty for hiding them
271 define('__MSG_LOGIN', "");
272 define('__MSG_PASS' , "");
276 if (!empty($GLOBALS['what'])) {
277 // Restore old what value
278 $content = array('target' => "what", 'value' => $GLOBALS['what']);
279 } elseif (!empty($GLOBALS['action'])) {
280 if ($GLOBALS['action'] != "logout") {
281 // Restore old action value
282 $content = array('target' => "action", 'value' => $GLOBALS['action']);
284 // Set default values
285 $content = array('target' => "action", 'value' => "login");
287 } elseif (!empty($_GET['area'])) {
288 // Restore old area value
289 $content = array('target' => "area", 'value' => $_GET['area']);
291 // Set default values
292 $content = array('target' => "action", 'value' => "login");
295 // Load login form template
296 LOAD_TEMPLATE("admin_login_form", false, $content);
298 } elseif (isset($_GET['logout'])) {
299 // Only try to remove cookies
300 if (set_session("admin_login", "") && set_session("admin_md5", "") && set_session("admin_last", "") && set_session("admin_to", "")) {
301 // Also remove array elements
302 set_session('admin_login', "");
303 set_session('admin_md5' , "");
304 set_session('admin_last' , "");
305 set_session('admin_to' , "");
310 // Load logout template
311 if (isset($_GET['register'])) {
313 $register = secureString(SQL_ESCAPE($_GET['register']));
315 // Special logout redirect for installation of given extension
316 LOAD_TEMPLATE(sprintf("admin_logout_%s_install", $register));
317 } elseif (isset($_GET['remove'])) {
319 $remove = secureString(SQL_ESCAPE($_GET['remove']));
321 // Special logout redirect for removal of given extension
322 LOAD_TEMPLATE(sprintf("admin_logout_%s_remove", $remove));
324 // Logged out normally
325 LOAD_TEMPLATE("admin_logout");
328 // Something went wrong here...
329 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
332 ADD_FATAL(CANNOT_UNREG_SESS);
335 // Maybe an Admin want's to login?
336 $ret = CHECK_ADMIN_COOKIES(SQL_ESCAPE(get_session('admin_login')), SQL_ESCAPE(get_session('admin_md5')));
340 // Cookie-Data accepted
341 if ((set_session("admin_md5", SQL_ESCAPE(get_session('admin_md5')))) && (set_session("admin_login", SQL_ESCAPE(get_session('admin_login')))) && (set_session("admin_last", time())) && (set_session("admin_to", bigintval(get_session('admin_to'))))) {
342 // Ok, Cookie-Update done
343 if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
344 // Check if action GET variable was set
345 $act = SQL_ESCAPE($GLOBALS['action']);
346 if (!empty($GLOBALS['what'])) {
347 // Get action value by what-value
348 $act = GET_ACTION("admin", $GLOBALS['what']);
351 // Check for access control line of current menu entry
352 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
354 // Extension not installed so it's always allowed to access everywhere!
355 define('__ACL_ALLOW', true);
358 // When type of admin menu is not set fallback to old menu system
359 if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
361 // Check for version and switch between old menu system and new "intelligent menu system"
362 if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (file_exists(PATH."inc/modules/admin/la_sys-inc.php"))) {
363 // Default area is the entrance, of course
366 // Check for similar URL variable
367 if (!empty($_GET['area'])) $area = $_GET['area'];
369 // Load "logical-area menu-system" file
370 require_once(PATH."inc/modules/admin/la_sys-inc.php");
372 // Create new-style menu system will "logical areas"
373 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
375 // This little call constructs the whole default old and lacky menu system
377 ADMIN_DO_ACTION($GLOBALS['what']);
380 // Login failed (cookies enabled?)
381 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
382 ADD_FATAL(CANNOT_RE_REGISTER_SESS);
386 case "404": // Administrator login not found
388 ADD_FATAL(ADMIN_NOT_FOUND);
391 case "pass": // Wrong password
393 ADD_FATAL(WRONG_PASS);
398 if (isBooleanConstantAndTrue('admin_registered'))
400 // Check config.php and inc directory for right access rights
401 if (is_INCWritable("config")) ADD_FATAL(FATAL_CONFIG_WRITABLE);
402 if (is_INCWritable("dummy")) ADD_FATAL(FATAL_INC_WRITABLE);