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 global $cacheArray, $cacheInstance;
43 // If action is login or logout allow allways!
45 if (($act == "login") || ($act == "logout")) return true;
51 $aid = GET_CURRENT_ADMIN_ID();
53 // Get admin's defult access right
54 $default = GET_ADMIN_DEFAULT_ACL($aid);
57 // Check for parent menu:
58 // First get it's action value
59 $parent_action = GET_ACTION("admin", $wht);
61 // Check with this function...
62 $parent = ADMINS_CHECK_ACL($parent_action, "");
64 // Anything else is true!
68 // Shall I test for a main or sub menu? (action or what?)
69 $lines = 0; $acl_mode = "failed";
70 if (GET_EXT_VERSION("cache") >= "0.1.2") {
71 // Load only from array when there are lines!
72 if ((isset($cacheArray['admin_acls'])) && (is_array($cacheArray['admin_acls'])) && (count($cacheArray['admin_acls']) > 0)) {
73 // Load ACL from array
74 foreach ($cacheArray['admin_acls']['admin_id'] as $id => $aid_acls) {
75 if ($aid == $aid_acls) {
76 // Okay, one line was found!
77 if ((!empty($act)) && ($cacheArray['admin_acls']['action_menu'][$id] == $act)) {
78 // Main menu line found
79 $acl_mode = $cacheArray['admin_acls']['access_mode'][$id];
82 elseif ((!empty($wht)) && ($cacheArray['admin_acls']['what_menu'][$id] == $wht)) {
84 $acl_mode = $cacheArray['admin_acls']['access_mode'][$id];
89 incrementConfigEntry('cache_hits');
96 if ($acl_mode == "failed") {
105 // Old version, so load it from database
109 $result = SQL_QUERY_ESC("SELECT access_mode FROM "._MYSQL_PREFIX."_admins_acls WHERE admin_id=%s AND action_menu='%s' LIMIT 1",
110 array(bigintval($aid), $act), __FILE__, __LINE__);
111 } elseif (!empty($wht)) {
113 $result = SQL_QUERY_ESC("SELECT access_mode FROM "._MYSQL_PREFIX."_admins_acls WHERE admin_id=%s AND what_menu='%s' LIMIT 1",
114 array(bigintval($aid), $wht), __FILE__, __LINE__);
117 // Get number of lines
118 $lines = SQL_NUMROWS($result);
121 list($acl_mode) = SQL_FETCHROW($result);
122 SQL_FREERESULT($result);
125 // Check ACL and (maybe) allow
126 if ((($default == "allow") && ($lines == 0)) || (($default == "deny") && ($lines == "1") && ($acl_mode == "allow")) || (($lines == 0) && ($parent))) $ret = true;
132 // Create email link to admins's account
133 function ADMINS_CREATE_EMAIL_LINK($email, $mod="admin") {
134 $locked = " AND status='CONFIRMED'";
135 if (IS_ADMIN()) $locked = "";
136 if (strpos("@", $email) > 0) {
138 $result = SQL_QUERY_ESC("SELECT id
139 FROM "._MYSQL_PREFIX."_admins
140 WHERE email='%s'".$locked." LIMIT 1",
141 array($email), __FILE__, __LINE__);
143 // Is there an entry?
144 if (SQL_NUMROWS($result) == 1) {
146 list($uid) = SQL_FETCHROW($result);
148 // Rewrite email address to contact link
149 $email = URL."/modules.php?module=".$mod."&what=user_contct&u_id=".bigintval($uid);
153 SQL_FREERESULT($result);
154 } elseif (bigintval($email) > 0) {
156 $email = URL."/modules.php?module=".$mod."&what=admins_contct&admin=".bigintval($email);
159 // Return rewritten (?) email address
163 // Change a lot admin account
164 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
165 global $cacheInstance;
169 foreach ($POST['login'] as $id => $login) {
171 $id = bigintval($id);
173 // When both passwords match update admin account
174 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
175 // Save only when both passwords are the same (also when they are empty)
176 $ADD = ""; $cache_update = "1";
179 $hash = generateHash($POST['pass1'][$id]);
181 // Save password when set
182 if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
185 $aid = GET_CURRENT_ADMIN_ID();
186 $salt = substr(GET_ADMIN_HASH($aid), 0, -40);
188 // Rewrite cookie when it's own account
190 // Set timeout cookie
191 set_session('admin_last', time());
193 if ($login != get_session('admin_login')) {
194 // Update login cookie
195 set_session('admin_login', $login);
197 // Update password cookie as well?
198 if (!empty($ADD)) set_session('admin_md5', $hash);
199 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
200 // Update password cookie
201 set_session('admin_md5', $hash);
205 // Get default ACL from admin to check if we can allow him to change the default ACL
206 $default = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
208 // Update admin account
209 if ($default == "allow") {
210 // Allow changing default ACL
211 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET
216 WHERE id=%s LIMIT 1",
221 $POST['la_mode'][$id],
223 ), __FILE__, __LINE__);
225 // Do not allow it here
226 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET
230 WHERE id=%s LIMIT 1",
234 $POST['la_mode'][$id],
236 ), __FILE__, __LINE__);
240 CACHE_PURGE_ADMIN_MENU($id);
242 // Admin account saved
243 $MSG = ADMIN_ACCOUNT_SAVED;
245 // Passwords did not match
246 $MSG = ADMINS_ERROR_PASS_MISMATCH;
251 LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
256 RUN_FILTER('post_admin_edited', $_POST);
259 // Make admin accounts editable
260 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
261 // "Resolve" current's admin access mode
262 $currMode = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
264 // Begin the edit loop
266 foreach ($POST['sel'] as $id => $sel) {
268 $id = bigintval($id);
270 // Get the admin's data
271 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
272 array($id), __FILE__, __LINE__);
273 if ((SQL_NUMROWS($result) == 1) && ($sel == 1)) {
275 $content = SQL_FETCHARRAY($result);
276 SQL_FREERESULT($result);
278 // Prepare some more data for the template
279 $content['sw'] = $SW;
280 $content['id'] = $id;
282 // Shall we allow changing default ACL?
283 if ($currMode == "allow") {
285 $content['mode'] = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(ADMINS_ALLOW_MODE, ADMINS_DENY_MODE), $content['mode']);
288 $content['mode'] = " ";
290 $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']);
292 // Load row template and switch color
293 $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
297 define('__ADMINS_ROWS', $OUT);
300 LOAD_TEMPLATE("admin_edit_admins");
303 // Delete given admin accounts
304 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
305 // Check if this account is the last one which cannot be deleted...
306 $result_main = SQL_QUERY("SELECT id FROM "._MYSQL_PREFIX."_admins", __FILE__, __LINE__);
307 $accounts = SQL_NUMROWS($result_main);
308 SQL_FREERESULT($result_main);
312 foreach ($POST['sel'] as $id => $sel) {
314 $id = bigintval($id);
316 // Get the admin's data
317 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
318 array($id), __FILE__, __LINE__);
319 if (SQL_NUMROWS($result) == 1) {
321 $content = SQL_FETCHARRAY($result);
322 SQL_FREERESULT($result);
323 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
324 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
326 // Prepare some more data
327 $content['sw'] = $SW;
328 $content['id'] = $id;
330 // Load row template and switch color
331 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
335 define('__ADMINS_ROWS', $OUT);
338 LOAD_TEMPLATE("admin_del_admins");
340 // Cannot delete last account!
341 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_ADMINS_CANNOT_DELETE_LAST);
345 // Remove the given accounts
346 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
349 foreach ($POST['sel'] as $id => $del) {
351 $id = bigintval($id);
353 // Delete only when it's not your own account!
354 if (($del == 1) && (GET_CURRENT_ADMIN_ID() != $id)) {
355 // Rewrite his tasks to all admins
356 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET assigned_admin=0 WHERE assigned_admin=%s",
357 array($id), __FILE__, __LINE__);
360 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
361 array($id), __FILE__, __LINE__);
364 CACHE_PURGE_ADMIN_MENU($id);
369 // Remove cache if cache system is activated
370 RUN_FILTER('post_admin_deleted', $_POST);
373 // List all admin accounts
374 function ADMINS_LIST_ADMIN_ACCOUNTS() {
375 // Select all admin accounts
376 $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins ORDER BY login ASC", __FILE__, __LINE__);
378 while ($content = SQL_FETCHARRAY($result)) {
379 // Compile some variables
380 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
381 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
383 // Prepare some more data
384 $content['sw'] = $SW;
385 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
387 // Load row template and switch color
388 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
393 SQL_FREERESULT($result);
394 define('__ADMINS_ROWS', $OUT);
397 LOAD_TEMPLATE("admin_list_admins");
400 // Filter for adding extra data to the query
401 function FILTER_ADD_EXTRA_SQL_DATA ($ADD = "") {
402 // Is the admins extension updated? (should be!)
403 if (GET_EXT_VERSION("admins") >= "0.3") $ADD .= ", default_acl AS def_acl";
404 if (GET_EXT_VERSION("admins") >= "0.6.7") $ADD .= ", la_mode";
405 if (GET_EXT_VERSION("admins") >= "0.7.2") $ADD .= ", login_failures, UNIX_TIMESTAMP(last_failure) AS last_failure";