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 // Is the logout empty?
53 if (empty($_GET['logout'])) $_GET['logout'] = "";
55 if (!isBooleanConstantAndTrue('admin_registered')) {
56 // Admin is not registered so we have to inform the user
57 if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
58 if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
59 // Hash the password with our new generateHash() function
60 $hashedPass = generateHash($_POST['pass']);
62 // If the password has not been hashed we have to fall-back to md5()
63 if ($hashedPass == $_POST['pass']) $hashedPass = md5($hashedPass);
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;
92 if (!isBooleanConstantAndTrue('admin_registered')) {
93 // Write to config that registration is done
94 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define ('admin_registered', ", ");", "true", 0);
97 $URL = URL."/modules.php?module=admin&action=login";
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 ((!isSessionVariableSet('admin_login')) || (!isSessionVariableSet('admin_md5')) || (!isSessionVariableSet('admin_last')) || (!isSessionVariableSet('admin_to')) || ((get_session('admin_last') + bigintval(get_session('admin_to')) * 3600 * 24) < time())) {
141 // At leat one administrator account was created
142 if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
143 // Timeout for last login, we have to logout first!
144 $URL = URL."/modules.php?module=admin&action=login&logout=1";
147 if (!empty($_GET['register'])) {
148 // Registration of first admin is done
149 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
152 // Check if the admin has submitted data or not
154 if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
155 if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
156 // All required data was entered so we check his account
157 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
160 case "done": // Admin and password are okay, so we log in now
161 // Try to register the session variables
162 if ((set_session("admin_md5", generatePassString(generateHash($_POST['pass'], __SALT)))) && (set_session("admin_login", $_POST['login'])) && (set_session("admin_last", time())) && (set_session("admin_to", $_POST['timeout']))) {
163 // Construct URL and redirect
165 $URL = URL."/modules.php?module=admin&";
167 // Rewrite overview module
168 if ($GLOBALS['what'] == "overview") {
169 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
173 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
174 elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
175 elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
180 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
181 ADD_FATAL(CANNOT_REGISTER_SESS);
185 case "404": // Administrator login not found
187 $ret = ADMIN_NOT_FOUND;
190 case "pass": // Wrong password
196 if ($ret != "done") {
197 if (!empty($_POST['login'])) {
198 define('__LOGIN_VALUE', $_POST['login']);
200 define('__LOGIN_VALUE', "");
203 if (isset($_POST['ok'])) {
204 // Set messages to zero
206 $MSG1 = ""; $MSG2 = "";
208 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
210 // An error comes back from login?
211 if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
213 // No password entered?
214 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
216 // Or password too short?
217 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
219 // An error comes back from login?
220 if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
222 // Load message template
223 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
224 define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
227 $MSG1 = ""; $MSG2 = "";
229 // Set constants to empty for hiding them
230 define('__MSG_LOGIN', "");
231 define('__MSG_PASS' , "");
235 if (!empty($GLOBALS['what'])) {
236 // Restore old what value
237 $content = array('target' => "what", 'value' => $GLOBALS['what']);
238 } elseif (!empty($GLOBALS['action'])) {
239 if ($GLOBALS['action'] != "logout") {
240 // Restore old action value
241 $content = array('target' => "action", 'value' => $GLOBALS['action']);
243 // Set default values
244 $content = array('target' => "action", 'value' => "login");
246 } elseif (!empty($_GET['area'])) {
247 // Restore old area value
248 $content = array('target' => "area", 'value' => $_GET['area']);
250 // Set default values
251 $content = array('target' => "action", 'value' => "login");
254 // Load login form template
255 LOAD_TEMPLATE("admin_login_form", false, $content);
257 } elseif ($_GET['logout'] == "1") {
258 // Only try to remove cookies
259 if (set_session("admin_login", "") && set_session("admin_md5", "") && set_session("admin_last", "") && set_session("admin_to", "")) {
260 // Also remove array elements
261 set_session('admin_login' , "");
262 set_session('admin_md5' , "");
263 set_session('admin_last' , "");
264 set_session('admin_to' , "");
269 // Load logout template
270 LOAD_TEMPLATE("admin_logout");
272 // Something went wrong here...
273 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
276 ADD_FATAL(CANNOT_UNREG_SESS);
279 // Maybe an Admin want's to login?
280 $ret = CHECK_ADMIN_COOKIES('admin_login', 'admin_md5');
283 // Cookie-Data accepted
284 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'))))) {
285 // Ok, Cookie-Update done
286 if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
287 // Check if action GET variable was set
288 $act = SQL_ESCAPE($GLOBALS['action']);
289 if (!empty($GLOBALS['what'])) {
290 // Get action value by what-value
291 $act = GET_ACTION("admin", $GLOBALS['what']);
294 // Check for access control line of current menu entry
295 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
297 // Extension not installed so it's always allowed to access everywhere!
298 define('__ACL_ALLOW', true);
301 // When type of admin menu is not set fallback to old menu system
302 if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
304 // Check for version and switch between old menu system and new "intelligent menu system"
305 if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (file_exists(PATH."inc/modules/admin/la_sys-inc.php"))) {
306 // Default area is the entrance, of course
309 // Check for similar URL variable
310 if (!empty($_GET['area'])) $area = $_GET['area'];
312 // Load "logical-area menu-system" file
313 require_once(PATH."inc/modules/admin/la_sys-inc.php");
315 // Create new-style menu system will "logical areas"
316 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
318 // This little call constructs the whole default old and lacky menu system
320 ADMIN_DO_ACTION($GLOBALS['what']);
323 // Login failed (cookies enabled?)
324 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
325 ADD_FATAL(CANNOT_RE_REGISTER_SESS);
329 case "404": // Administrator login not found
331 ADD_FATAL(ADMIN_NOT_FOUND);
334 case "pass": // Wrong password
336 ADD_FATAL(WRONG_PASS);
341 if (isBooleanConstantAndTrue('admin_registered'))
343 // Check config.php and inc directory for right access rights
344 if (is_INCWritable("config")) ADD_FATAL(FATAL_CONFIG_WRITABLE);
345 if (is_INCWritable("dummy")) ADD_FATAL(FATAL_INC_WRITABLE);