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 * -------------------------------------------------------------------- *
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";
40 // Check ACL for menu combination
41 function ADMINS_CHECK_ACL($act, $wht) {
42 // If action is login or logout allow allways!
44 if (($act == "login") || ($act == "logout")) return true;
50 $aid = GET_CURRENT_ADMIN_ID();
52 // Get admin's defult access right
53 $default = GET_ADMIN_DEFAULT_ACL($aid);
56 // Check for parent menu:
57 // First get it's action value
58 $parent_action = GET_ACTION("admin", $wht);
60 // Check with this function...
61 $parent = ADMINS_CHECK_ACL($parent_action, "");
63 // Anything else is true!
67 // Shall I test for a main or sub menu? (action or what?)
68 $lines = 0; $acl_mode = "failed";
69 if (GET_EXT_VERSION("cache") >= "0.1.2") {
70 // Load only from array when there are lines!
71 if ((isset($GLOBALS['cache_array']['admin_acls'])) && (is_array($GLOBALS['cache_array']['admin_acls'])) && (count($GLOBALS['cache_array']['admin_acls']) > 0)) {
72 // Load ACL from array
73 foreach ($GLOBALS['cache_array']['admin_acls']['admin_id'] as $id => $aid_acls) {
74 if ($aid == $aid_acls) {
75 // Okay, one line was found!
76 if ((!empty($act)) && ($GLOBALS['cache_array']['admin_acls']['action_menu'][$id] == $act)) {
77 // Main menu line found
78 $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$id];
81 elseif ((!empty($wht)) && ($GLOBALS['cache_array']['admin_acls']['what_menu'][$id] == $wht)) {
83 $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$id];
88 incrementConfigEntry('cache_hits');
95 if ($acl_mode == "failed") {
104 // Old version, so load it from database
108 $result = SQL_QUERY_ESC("SELECT access_mode FROM `{!_MYSQL_PREFIX!}_admins_acls` WHERE admin_id=%s AND action_menu='%s' LIMIT 1",
109 array(bigintval($aid), $act), __FUNCTION__, __LINE__);
110 } elseif (!empty($wht)) {
112 $result = SQL_QUERY_ESC("SELECT access_mode FROM `{!_MYSQL_PREFIX!}_admins_acls` WHERE admin_id=%s AND what_menu='%s' LIMIT 1",
113 array(bigintval($aid), $wht), __FUNCTION__, __LINE__);
116 // Get number of lines
117 $lines = SQL_NUMROWS($result);
120 list($acl_mode) = SQL_FETCHROW($result);
121 SQL_FREERESULT($result);
124 // Check ACL and (maybe) allow
125 if ((($default == "allow") && ($lines == 0)) || (($default == "deny") && ($lines == "1") && ($acl_mode == "allow")) || (($lines == 0) && ($parent))) $ret = true;
131 // Create email link to admins's account
132 function ADMINS_CREATE_EMAIL_LINK ($email, $mod="admin") {
133 if (strpos("@", $email) > 0) {
135 $result = SQL_QUERY_ESC("SELECT id
136 FROM `{!_MYSQL_PREFIX!}_admins`
137 WHERE email='%s' LIMIT 1",
138 array($email), __FUNCTION__, __LINE__);
140 // Is there an entry?
141 if (SQL_NUMROWS($result) == 1) {
143 list($uid) = SQL_FETCHROW($result);
145 // Rewrite email address to contact link
146 $email = "{!URL!}/modules.php?module=".$mod."&what=user_contct&uid=".bigintval($uid);
150 SQL_FREERESULT($result);
151 } elseif ((is_int($email)) && ($email > 0)) {
153 $email = "{!URL!}/modules.php?module=".$mod."&what=admins_contct&admin=".bigintval($email);
156 // Return rewritten (?) email address
160 // Change a lot admin account
161 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
164 foreach ($POST['login'] as $id => $login) {
166 $id = bigintval($id);
168 // When both passwords match update admin account
169 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
170 // Save only when both passwords are the same (also when they are empty)
171 $ADD = ""; $cache_update = "1";
174 $hash = generateHash($POST['pass1'][$id]);
176 // Save password when set
177 if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
180 $aid = GET_CURRENT_ADMIN_ID();
181 $salt = substr(GET_ADMIN_HASH($aid), 0, -40);
183 // Rewrite cookie when it's own account
185 // Set timeout cookie
186 set_session('admin_last', time());
188 if ($login != get_session('admin_login')) {
189 // Update login cookie
190 set_session('admin_login', $login);
192 // Update password cookie as well?
193 if (!empty($ADD)) set_session('admin_md5', $hash);
194 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
195 // Update password cookie
196 set_session('admin_md5', $hash);
200 // Get default ACL from admin to check if we can allow him to change the default ACL
201 $default = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
203 // Update admin account
204 if ($default == "allow") {
205 // Allow changing default ACL
206 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
211 WHERE id=%s LIMIT 1",
216 $POST['la_mode'][$id],
218 ), __FUNCTION__, __LINE__);
220 // Do not allow it here
221 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
225 WHERE id=%s LIMIT 1",
229 $POST['la_mode'][$id],
231 ), __FUNCTION__, __LINE__);
235 CACHE_PURGE_ADMIN_MENU($id);
237 // Admin account saved
238 $MSG = ADMIN_ACCOUNT_SAVED;
240 // Passwords did not match
241 $MSG = ADMINS_ERROR_PASS_MISMATCH;
246 LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
251 runFilterChain('post_admin_edited', REQUEST_POST_ARRAY());
254 // Make admin accounts editable
255 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
256 // "Resolve" current's admin access mode
257 $currMode = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
259 // Begin the edit loop
261 foreach ($POST['sel'] as $id => $selected) {
263 $id = bigintval($id);
265 // Get the admin's data
266 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
267 array($id), __FUNCTION__, __LINE__);
268 if ((SQL_NUMROWS($result) == 1) && ($selected == 1)) {
270 $content = SQL_FETCHARRAY($result);
271 SQL_FREERESULT($result);
273 // Prepare some more data for the template
274 $content['sw'] = $SW;
275 $content['id'] = $id;
277 // Shall we allow changing default ACL?
278 if ($currMode == "allow") {
280 $content['mode'] = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(constant('ADMINS_ALLOW_MODE'), constant('ADMINS_DENY_MODE')), $content['mode']);
283 $content['mode'] = " ";
285 $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']);
287 // Load row template and switch color
288 $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
292 define('__ADMINS_ROWS', $OUT);
295 LOAD_TEMPLATE("admin_edit_admins");
298 // Delete given admin accounts
299 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
300 // Check if this account is the last one which cannot be deleted...
301 $result_main = SQL_QUERY("SELECT id FROM `{!_MYSQL_PREFIX!}_admins`", __FUNCTION__, __LINE__);
302 $accounts = SQL_NUMROWS($result_main);
303 SQL_FREERESULT($result_main);
307 foreach ($POST['sel'] as $id => $selected) {
309 $id = bigintval($id);
311 // Get the admin's data
312 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
313 array($id), __FUNCTION__, __LINE__);
314 if (SQL_NUMROWS($result) == 1) {
316 $content = SQL_FETCHARRAY($result);
317 SQL_FREERESULT($result);
318 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
319 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
321 // Prepare some more data
322 $content['sw'] = $SW;
323 $content['id'] = $id;
325 // Load row template and switch color
326 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
330 define('__ADMINS_ROWS', $OUT);
333 LOAD_TEMPLATE("admin_del_admins");
335 // Cannot delete last account!
336 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_ADMINS_CANNOT_DELETE_LAST'));
340 // Remove the given accounts
341 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
344 foreach ($POST['sel'] as $id => $del) {
346 $id = bigintval($id);
348 // Delete only when it's not your own account!
349 if (($del == 1) && (GET_CURRENT_ADMIN_ID() != $id)) {
350 // Rewrite his tasks to all admins
351 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin=0 WHERE assigned_admin=%s",
352 array($id), __FUNCTION__, __LINE__);
355 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
356 array($id), __FUNCTION__, __LINE__);
359 CACHE_PURGE_ADMIN_MENU($id);
364 // Remove cache if cache system is activated
365 runFilterChain('post_admin_deleted', REQUEST_POST_ARRAY());
368 // List all admin accounts
369 function ADMINS_LIST_ADMIN_ACCOUNTS() {
370 // Select all admin accounts
371 $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` ORDER BY login ASC", __FUNCTION__, __LINE__);
373 while ($content = SQL_FETCHARRAY($result)) {
374 // Compile some variables
375 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
376 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
378 // Prepare some more data
379 $content['sw'] = $SW;
380 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
382 // Load row template and switch color
383 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
388 SQL_FREERESULT($result);
389 define('__ADMINS_ROWS', $OUT);
392 LOAD_TEMPLATE("admin_list_admins");
395 // Filter for adding extra data to the query
396 function FILTER_ADD_EXTRA_SQL_DATA ($ADD = "") {
397 // Is the admins extension updated? (should be!)
398 if (GET_EXT_VERSION("admins") >= "0.3") $ADD .= ", default_acl AS def_acl";
399 if (GET_EXT_VERSION("admins") >= "0.6.7") $ADD .= ", la_mode";
400 if (GET_EXT_VERSION("admins") >= "0.7.2") $ADD .= ", login_failures, UNIX_TIMESTAMP(last_failure) AS last_failure";