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 $locked = " AND `status`='CONFIRMED'";
134 if (IS_ADMIN()) $locked = "";
135 if (strpos("@", $email) > 0) {
137 $result = SQL_QUERY_ESC("SELECT id
138 FROM `{!_MYSQL_PREFIX!}_admins`
139 WHERE email='%s'".$locked." LIMIT 1",
140 array($email), __FUNCTION__, __LINE__);
142 // Is there an entry?
143 if (SQL_NUMROWS($result) == 1) {
145 list($uid) = SQL_FETCHROW($result);
147 // Rewrite email address to contact link
148 $email = "{!URL!}/modules.php?module=".$mod."&what=user_contct&uid=".bigintval($uid);
152 SQL_FREERESULT($result);
153 } elseif (bigintval($email) > 0) {
155 $email = "{!URL!}/modules.php?module=".$mod."&what=admins_contct&admin=".bigintval($email);
158 // Return rewritten (?) email address
162 // Change a lot admin account
163 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
166 foreach ($POST['login'] as $id => $login) {
168 $id = bigintval($id);
170 // When both passwords match update admin account
171 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
172 // Save only when both passwords are the same (also when they are empty)
173 $ADD = ""; $cache_update = "1";
176 $hash = generateHash($POST['pass1'][$id]);
178 // Save password when set
179 if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
182 $aid = GET_CURRENT_ADMIN_ID();
183 $salt = substr(GET_ADMIN_HASH($aid), 0, -40);
185 // Rewrite cookie when it's own account
187 // Set timeout cookie
188 set_session('admin_last', time());
190 if ($login != get_session('admin_login')) {
191 // Update login cookie
192 set_session('admin_login', $login);
194 // Update password cookie as well?
195 if (!empty($ADD)) set_session('admin_md5', $hash);
196 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
197 // Update password cookie
198 set_session('admin_md5', $hash);
202 // Get default ACL from admin to check if we can allow him to change the default ACL
203 $default = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
205 // Update admin account
206 if ($default == "allow") {
207 // Allow changing default ACL
208 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
213 WHERE id=%s LIMIT 1",
218 $POST['la_mode'][$id],
220 ), __FUNCTION__, __LINE__);
222 // Do not allow it here
223 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
227 WHERE id=%s LIMIT 1",
231 $POST['la_mode'][$id],
233 ), __FUNCTION__, __LINE__);
237 CACHE_PURGE_ADMIN_MENU($id);
239 // Admin account saved
240 $MSG = ADMIN_ACCOUNT_SAVED;
242 // Passwords did not match
243 $MSG = ADMINS_ERROR_PASS_MISMATCH;
248 LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
253 RUN_FILTER('post_admin_edited', REQUEST_POST_ARRAY());
256 // Make admin accounts editable
257 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
258 // "Resolve" current's admin access mode
259 $currMode = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
261 // Begin the edit loop
263 foreach ($POST['sel'] as $id => $sel) {
265 $id = bigintval($id);
267 // Get the admin's data
268 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
269 array($id), __FUNCTION__, __LINE__);
270 if ((SQL_NUMROWS($result) == 1) && ($sel == 1)) {
272 $content = SQL_FETCHARRAY($result);
273 SQL_FREERESULT($result);
275 // Prepare some more data for the template
276 $content['sw'] = $SW;
277 $content['id'] = $id;
279 // Shall we allow changing default ACL?
280 if ($currMode == "allow") {
282 $content['mode'] = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(constant('ADMINS_ALLOW_MODE'), constant('ADMINS_DENY_MODE')), $content['mode']);
285 $content['mode'] = " ";
287 $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']);
289 // Load row template and switch color
290 $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
294 define('__ADMINS_ROWS', $OUT);
297 LOAD_TEMPLATE("admin_edit_admins");
300 // Delete given admin accounts
301 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
302 // Check if this account is the last one which cannot be deleted...
303 $result_main = SQL_QUERY("SELECT id FROM `{!_MYSQL_PREFIX!}_admins`", __FUNCTION__, __LINE__);
304 $accounts = SQL_NUMROWS($result_main);
305 SQL_FREERESULT($result_main);
309 foreach ($POST['sel'] as $id => $sel) {
311 $id = bigintval($id);
313 // Get the admin's data
314 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
315 array($id), __FUNCTION__, __LINE__);
316 if (SQL_NUMROWS($result) == 1) {
318 $content = SQL_FETCHARRAY($result);
319 SQL_FREERESULT($result);
320 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
321 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
323 // Prepare some more data
324 $content['sw'] = $SW;
325 $content['id'] = $id;
327 // Load row template and switch color
328 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
332 define('__ADMINS_ROWS', $OUT);
335 LOAD_TEMPLATE("admin_del_admins");
337 // Cannot delete last account!
338 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_ADMINS_CANNOT_DELETE_LAST'));
342 // Remove the given accounts
343 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
346 foreach ($POST['sel'] as $id => $del) {
348 $id = bigintval($id);
350 // Delete only when it's not your own account!
351 if (($del == 1) && (GET_CURRENT_ADMIN_ID() != $id)) {
352 // Rewrite his tasks to all admins
353 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin=0 WHERE assigned_admin=%s",
354 array($id), __FUNCTION__, __LINE__);
357 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
358 array($id), __FUNCTION__, __LINE__);
361 CACHE_PURGE_ADMIN_MENU($id);
366 // Remove cache if cache system is activated
367 RUN_FILTER('post_admin_deleted', REQUEST_POST_ARRAY());
370 // List all admin accounts
371 function ADMINS_LIST_ADMIN_ACCOUNTS() {
372 // Select all admin accounts
373 $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` ORDER BY login ASC", __FUNCTION__, __LINE__);
375 while ($content = SQL_FETCHARRAY($result)) {
376 // Compile some variables
377 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
378 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
380 // Prepare some more data
381 $content['sw'] = $SW;
382 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
384 // Load row template and switch color
385 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
390 SQL_FREERESULT($result);
391 define('__ADMINS_ROWS', $OUT);
394 LOAD_TEMPLATE("admin_list_admins");
397 // Filter for adding extra data to the query
398 function FILTER_ADD_EXTRA_SQL_DATA ($ADD = "") {
399 // Is the admins extension updated? (should be!)
400 if (GET_EXT_VERSION("admins") >= "0.3") $ADD .= ", default_acl AS def_acl";
401 if (GET_EXT_VERSION("admins") >= "0.6.7") $ADD .= ", la_mode";
402 if (GET_EXT_VERSION("admins") >= "0.7.2") $ADD .= ", login_failures, UNIX_TIMESTAMP(last_failure) AS last_failure";