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 (!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 (!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 ((empty($_SESSION['admin_login'])) || (empty($_SESSION['admin_md5'])) || (empty($_SESSION['admin_last'])) || (empty($_SESSION['admin_to'])) || (($_SESSION['admin_last'] + bigintval($_SESSION['admin_to']) * 3600 * 24) < time())) {
141 // At leat one administrator account was created
142 if ((!empty($_SESSION['admin_login'])) && (!empty($_SESSION['admin_md5'])) && (!empty($_SESSION['admin_last'])) && (!empty($_SESSION['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
164 $URL = URL."/modules.php?module=admin&";
166 // Rewrite overview module
167 if ($GLOBALS['what'] == "overview") {
168 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
172 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
173 elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
174 elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
179 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
180 ADD_FATAL(CANNOT_REGISTER_SESS);
184 case "404": // Administrator login not found
186 $ret = ADMIN_NOT_FOUND;
189 case "pass": // Wrong password
195 if ($ret != "done") {
196 if (!empty($_POST['login'])) {
197 define('__LOGIN_VALUE', $_POST['login']);
199 define('__LOGIN_VALUE', "");
202 if (isset($_POST['ok'])) {
203 // Set messages to zero
205 $MSG1 = ""; $MSG2 = "";
207 if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
209 // An error comes back from login?
210 if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
212 // No password entered?
213 if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
215 // Or password too short?
216 if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
218 // An error comes back from login?
219 if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
221 // Load message template
222 define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
223 define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
226 $MSG1 = ""; $MSG2 = "";
228 // Set constants to empty for hiding them
229 define('__MSG_LOGIN', "");
230 define('__MSG_PASS' , "");
234 if (!empty($GLOBALS['what'])) {
235 // Restore old what value
236 $content = array('target' => "what", 'value' => $GLOBALS['what']);
237 } elseif (!empty($GLOBALS['action'])) {
238 if ($GLOBALS['action'] != "logout") {
239 // Restore old action value
240 $content = array('target' => "action", 'value' => $GLOBALS['action']);
242 // Set default values
243 $content = array('target' => "action", 'value' => "login");
245 } elseif (!empty($_GET['area'])) {
246 // Restore old area value
247 $content = array('target' => "area", 'value' => $_GET['area']);
249 // Set default values
250 $content = array('target' => "action", 'value' => "login");
253 // Load login form template
254 LOAD_TEMPLATE("admin_login_form", false, $content);
256 } elseif ($_GET['logout'] == "1") {
257 // Only try to remove cookies
258 if (set_session("admin_login", "", (time() - 3600), COOKIE_PATH) && set_session("admin_md5", "", (time() - 3600), COOKIE_PATH) && set_session("admin_last", "", (time() - 3600), COOKIE_PATH) && set_session("admin_to", "", (time() - 3600), COOKIE_PATH)) {
259 // Also remove array elements
260 unset($_SESSION['admin_login']);
261 unset($_SESSION['admin_md5']);
262 unset($_SESSION['admin_last']);
263 unset($_SESSION['admin_to']);
268 // Load logout template
269 LOAD_TEMPLATE("admin_logout");
271 // Something went wrong here...
272 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
275 ADD_FATAL(CANNOT_UNREG_SESS);
278 // Maybe an Admin want's to login?
279 $ret = CHECK_ADMIN_COOKIES(SQL_ESCAPE($_SESSION['admin_login']), SQL_ESCAPE($_SESSION['admin_md5']));
282 // Cookie-Data accepted
283 if ((set_session("admin_md5", SQL_ESCAPE($_SESSION['admin_md5']))) && (set_session("admin_login", SQL_ESCAPE($_SESSION['admin_login']))) && (set_session("admin_last", time())) && (set_session("admin_to", bigintval($_SESSION['admin_to'])))) {
284 // Ok, Cookie-Update done
285 if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
286 // Check if action GET variable was set
287 $act = SQL_ESCAPE($GLOBALS['action']);
288 if (!empty($GLOBALS['what'])) {
289 // Get action value by what-value
290 $act = GET_ACTION("admin", $GLOBALS['what']);
293 // Check for access control line of current menu entry
294 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
296 // Extension not installed so it's always allowed to access everywhere!
297 define('__ACL_ALLOW', true);
300 // When type of admin menu is not set fallback to old menu system
301 if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
303 // Check for version and switch between old menu system and new "intelligent menu system"
304 if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (file_exists(PATH."inc/modules/admin/la_sys-inc.php"))) {
305 // Default area is the entrance, of course
308 // Check for similar URL variable
309 if (!empty($_GET['area'])) $area = $_GET['area'];
311 // Load "logical-area menu-system" file
312 require_once(PATH."inc/modules/admin/la_sys-inc.php");
314 // Create new-style menu system will "logical areas"
315 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
317 // This little call constructs the whole default old and lacky menu system
319 ADMIN_DO_ACTION($GLOBALS['what']);
322 // Login failed (cookies enabled?)
323 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
324 ADD_FATAL(CANNOT_RE_REGISTER_SESS);
328 case "404": // Administrator login not found
330 ADD_FATAL(ADMIN_NOT_FOUND);
333 case "pass": // Wrong password
335 ADD_FATAL(WRONG_PASS);
340 if (admin_registered)
342 // Check config.php and inc directory for right access rights
343 if (is_INCWritable("config")) ADD_FATAL(FATAL_CONFIG_WRITABLE);
344 if (is_INCWritable("dummy")) ADD_FATAL(FATAL_INC_WRITABLE);