2 /************************************************************************
3 * MXChange v0.2.1 Start: 06/30/2003 *
4 * =============== Last change: 11/27/2004 *
6 * -------------------------------------------------------------------- *
7 * File : admins_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for the admins extension *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer die admins-Erweiterung *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2008 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
45 // Check ACL for menu combination
46 function ADMINS_CHECK_ACL($act, $wht) {
47 // If action is login or logout allow allways!
49 if (($act == "login") || ($act == "logout")) return true;
55 $aid = GET_CURRENT_ADMIN_ID();
57 // Get admin's defult access right
58 $default = GET_ADMIN_DEFAULT_ACL($aid);
61 // Check for parent menu:
62 // First get it's action value
63 $parent_action = GET_ACTION('admin', $wht);
65 // Check with this function...
66 $parent = ADMINS_CHECK_ACL($parent_action, '');
68 // Anything else is true!
72 // Shall I test for a main or sub menu? (action or what?)
74 if ((GET_EXT_VERSION('cache') >= '0.1.2') && (count($GLOBALS['cache_array']['admin_acls']) > 0)) {
76 if ((!empty($act)) && (isset($GLOBALS['cache_array']['admin_acls']['action_menu'][$aid])) & ($GLOBALS['cache_array']['admin_acls']['action_menu'][$aid] == $act)) {
77 // Main menu line found
78 $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$aid];
81 incrementConfigEntry('cache_hits');
82 } elseif ((!empty($wht)) && (isset($GLOBALS['cache_array']['admin_acls']['what_menu'][$aid])) && ($GLOBALS['cache_array']['admin_acls']['what_menu'][$aid] == $wht)) {
84 $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$aid];
87 incrementConfigEntry('cache_hits');
89 } elseif (!EXT_IS_ACTIVE('cache')) {
90 // Old version, so load it from database
93 $result = SQL_QUERY_ESC("SELECT access_mode FROM `{!_MYSQL_PREFIX!}_admins_acls` WHERE admin_id=%s AND action_menu='%s' LIMIT 1",
94 array(bigintval($aid), $act), __FUNCTION__, __LINE__);
95 } elseif (!empty($wht)) {
97 $result = SQL_QUERY_ESC("SELECT access_mode FROM `{!_MYSQL_PREFIX!}_admins_acls` WHERE admin_id=%s AND what_menu='%s' LIMIT 1",
98 array(bigintval($aid), $wht), __FUNCTION__, __LINE__);
101 // Is an entry found?
102 if (SQL_NUMROWS($result) == 1) {
104 list($acl_mode) = SQL_FETCHROW($result);
108 SQL_FREERESULT($result);
111 // Check ACL and (maybe) allow
112 if (($default == "allow") || (($default == "deny") && ($acl_mode == "allow")) || ($parent === true)) {
118 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:act={$act},wht={$wht},default={$default},acl_mode={$acl_mode}<br />\n";
122 // Create email link to admins's account
123 function ADMINS_CREATE_EMAIL_LINK ($email, $mod='admin') {
125 if (strpos($email, "@") !== false) {
127 $result = SQL_QUERY_ESC("SELECT `id`
128 FROM `{!_MYSQL_PREFIX!}_admins`
129 WHERE `email`='%s' LIMIT 1",
130 array($email), __FUNCTION__, __LINE__);
132 // Is there an entry?
133 if (SQL_NUMROWS($result) == 1) {
135 list($aid) = SQL_FETCHROW($result);
137 // Rewrite email address to contact link
138 $email = "{!URL!}/modules.php?module=".$mod."&what=admins_contct&admin=".bigintval($aid);
142 SQL_FREERESULT($result);
143 } elseif ((is_int($email)) && ($email > 0)) {
145 $email = "{!URL!}/modules.php?module=".$mod."&what=admins_contct&admin=".bigintval($email);
148 // Return rewritten (?) email address
152 // Change a lot admin account
153 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
156 foreach ($POST['login'] as $id => $login) {
158 $id = bigintval($id);
160 // When both passwords match update admin account
161 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
162 // Save only when both passwords are the same (also when they are empty)
163 $add = ''; $cache_update = "1";
166 $hash = generateHash($POST['pass1'][$id]);
168 // Save password when set
169 if (!empty($POST['pass1'][$id])) $add = sprintf(", password='%s'", SQL_ESCAPE($hash));
172 $aid = GET_CURRENT_ADMIN_ID();
173 $salt = substr(GET_ADMIN_HASH($aid), 0, -40);
175 // Rewrite cookie when it's own account
177 // Set timeout cookie
178 set_session('admin_last', time());
180 if ($login != get_session('admin_login')) {
181 // Update login cookie
182 set_session('admin_login', $login);
184 // Update password cookie as well?
185 if (!empty($add)) set_session('admin_md5', $hash);
186 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
187 // Update password cookie
188 set_session('admin_md5', $hash);
192 // Get default ACL from admin to check if we can allow him to change the default ACL
193 $default = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
195 // Update admin account
196 if ($default == "allow") {
197 // Allow changing default ACL
198 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
203 WHERE id=%s LIMIT 1",
208 $POST['la_mode'][$id],
210 ), __FUNCTION__, __LINE__);
212 // Do not allow it here
213 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
217 WHERE id=%s LIMIT 1",
221 $POST['la_mode'][$id],
223 ), __FUNCTION__, __LINE__);
227 CACHE_PURGE_ADMIN_MENU($id);
229 // Admin account saved
230 $message = ADMIN_ACCOUNT_SAVED;
232 // Passwords did not match
233 $message = ADMINS_ERROR_PASS_MISMATCH;
237 if (!empty($message)) {
238 LOAD_TEMPLATE('admin_settings_saved', false, $message);
243 runFilterChain('post_admin_edited', REQUEST_POST_ARRAY());
246 // Make admin accounts editable
247 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
248 // "Resolve" current's admin access mode
249 $currMode = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
251 // Begin the edit loop
253 foreach ($POST['sel'] as $id => $selected) {
255 $id = bigintval($id);
257 // Get the admin's data
258 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
259 array($id), __FUNCTION__, __LINE__);
260 if ((SQL_NUMROWS($result) == 1) && ($selected == 1)) {
262 $content = SQL_FETCHARRAY($result);
263 SQL_FREERESULT($result);
265 // Prepare some more data for the template
266 $content['sw'] = $SW;
267 $content['id'] = $id;
269 // Shall we allow changing default ACL?
270 if ($currMode == "allow") {
272 $content['mode'] = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(constant('ADMINS_ALLOW_MODE'), constant('ADMINS_DENY_MODE')), $content['mode']);
275 $content['mode'] = " ";
277 $content['la_mode'] = ADD_OPTION_LINES("/ARRAY/", array("global", "OLD", "NEW"), array(ADMINS_GLOBAL_LA_SETTING, ADMINS_OLD_LA_SETTING, ADMINS_NEW_LA_SETTING), $content['la_mode']);
279 // Load row template and switch color
280 $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
284 define('__ADMINS_ROWS', $OUT);
287 LOAD_TEMPLATE("admin_edit_admins");
290 // Delete given admin accounts
291 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
292 // Check if this account is the last one which cannot be deleted...
293 $result_main = SQL_QUERY("SELECT id FROM `{!_MYSQL_PREFIX!}_admins`", __FUNCTION__, __LINE__);
294 $accounts = SQL_NUMROWS($result_main);
295 SQL_FREERESULT($result_main);
299 foreach ($POST['sel'] as $id => $selected) {
301 $id = bigintval($id);
303 // Get the admin's data
304 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
305 array($id), __FUNCTION__, __LINE__);
306 if (SQL_NUMROWS($result) == 1) {
308 $content = SQL_FETCHARRAY($result);
309 SQL_FREERESULT($result);
310 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
311 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
313 // Prepare some more data
314 $content['sw'] = $SW;
315 $content['id'] = $id;
317 // Load row template and switch color
318 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
322 define('__ADMINS_ROWS', $OUT);
325 LOAD_TEMPLATE("admin_del_admins");
327 // Cannot delete last account!
328 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_ADMINS_CANNOT_DELETE_LAST'));
332 // Remove the given accounts
333 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
336 foreach ($POST['sel'] as $id => $del) {
338 $id = bigintval($id);
340 // Delete only when it's not your own account!
341 if (($del == 1) && (GET_CURRENT_ADMIN_ID() != $id)) {
342 // Rewrite his tasks to all admins
343 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin=0 WHERE assigned_admin=%s",
344 array($id), __FUNCTION__, __LINE__);
347 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
348 array($id), __FUNCTION__, __LINE__);
351 CACHE_PURGE_ADMIN_MENU($id);
356 // Remove cache if cache system is activated
357 runFilterChain('post_admin_deleted', REQUEST_POST_ARRAY());
360 // List all admin accounts
361 function ADMINS_LIST_ADMIN_ACCOUNTS() {
362 // Select all admin accounts
363 $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` ORDER BY login ASC", __FUNCTION__, __LINE__);
365 while ($content = SQL_FETCHARRAY($result)) {
366 // Compile some variables
367 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
368 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
370 // Prepare some more data
371 $content['sw'] = $SW;
372 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
374 // Load row template and switch color
375 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
380 SQL_FREERESULT($result);
381 define('__ADMINS_ROWS', $OUT);
384 LOAD_TEMPLATE("admin_list_admins");
387 // Filter for adding extra data to the query
388 function FILTER_ADD_EXTRA_SQL_DATA ($add = "") {
389 // Is the admins extension updated? (should be!)
390 if (GET_EXT_VERSION('admins') >= '0.3.0') $add .= ", default_acl AS def_acl";
391 if (GET_EXT_VERSION('admins') >= '0.6.7') $add .= ", la_mode";
392 if (GET_EXT_VERSION('admins') >= '0.7.2') $add .= ", login_failures, UNIX_TIMESTAMP(last_failure) AS last_failure";