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, $_CONFIG, $cacheInstance;
43 // If action is login or logout allow allways!
45 if (($act == "login") || ($act == "logout")) return true;
50 // Get admin's defult access right
51 if (!empty($cacheArray['admins']['def_acl'][get_session('admin_login')])) {
53 $default = $cacheArray['admins']['def_acl'][get_session('admin_login')];
56 if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
57 } elseif (!is_object($cacheInstance)) {
59 $default = GET_ADMIN_DEFAULT_ACL(get_session('admin_login'));
63 $aid = GET_ADMIN_ID(get_session('admin_login'));
66 // Check for parent menu:
67 // First get it's action value
68 $parent_action = GET_ACTION("admin", $wht);
70 // Check with this function...
71 $parent = ADMINS_CHECK_ACL($parent_action, "");
73 // Anything else is true!
77 // Shall I test for a main or sub menu? (action or what?)
78 $lines = 0; $acl_mode = "failed";
79 if (GET_EXT_VERSION("cache") >= "0.1.2") {
80 // Load only from array when there are lines!
81 if ((isset($cacheArray['admin_acls'])) && (is_array($cacheArray['admin_acls'])) && (count($cacheArray['admin_acls']) > 0)) {
82 // Load ACL from array
83 foreach ($cacheArray['admin_acls']['admin_id'] as $id => $aid_acls) {
84 if ($aid == $aid_acls) {
85 // Okay, one line was found!
86 if ((!empty($act)) && ($cacheArray['admin_acls']['action_menu'][$id] == $act)) {
87 // Main menu line found
88 $acl_mode = $cacheArray['admin_acls']['access_mode'][$id];
91 elseif ((!empty($wht)) && ($cacheArray['admin_acls']['what_menu'][$id] == $wht)) {
93 $acl_mode = $cacheArray['admin_acls']['access_mode'][$id];
98 if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
105 if ($acl_mode == "failed") {
114 // Old version, so load it from database
118 $result = SQL_QUERY_ESC("SELECT access_mode FROM "._MYSQL_PREFIX."_admins_acls WHERE admin_id=%s AND action_menu='%s' LIMIT 1",
119 array(bigintval($aid), $act), __FILE__, __LINE__);
120 } elseif (!empty($wht)) {
122 $result = SQL_QUERY_ESC("SELECT access_mode FROM "._MYSQL_PREFIX."_admins_acls WHERE admin_id=%s AND what_menu='%s' LIMIT 1",
123 array(bigintval($aid), $wht), __FILE__, __LINE__);
126 // Get number of lines
127 $lines = SQL_NUMROWS($result);
130 list($acl_mode) = SQL_FETCHROW($result);
131 SQL_FREERESULT($result);
134 // Check ACL and (maybe) allow
135 if ((($default == "allow") && ($lines == 0)) || (($default == "deny") && ($lines == "1") && ($acl_mode == "allow")) || (($lines == 0) && ($parent))) $ret = true;
141 // Create email link to admins's account
142 function ADMINS_CREATE_EMAIL_LINK($email, $mod="admin") {
143 $locked = " AND status='CONFIRMED'";
144 if (IS_ADMIN()) $locked = "";
145 if (strpos("@", $email) > 0) {
147 $result = SQL_QUERY_ESC("SELECT id
148 FROM "._MYSQL_PREFIX."_admins
149 WHERE email='%s'".$locked." LIMIT 1",
150 array($email), __FILE__, __LINE__);
152 // Is there an entry?
153 if (SQL_NUMROWS($result) == 1) {
155 list($uid) = SQL_FETCHROW($result);
157 // Rewrite email address to contact link
158 $email = URL."/modules.php?module=".$mod."&what=user_contct&u_id=".bigintval($uid);
162 SQL_FREERESULT($result);
163 } elseif (bigintval($email) > 0) {
165 $email = URL."/modules.php?module=".$mod."&what=admins_contct&admin=".bigintval($email);
168 // Return rewritten (?) email address
172 // Change a lot admin account
173 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
174 global $cacheInstance;
178 foreach ($POST['login'] as $id => $login) {
180 $id = bigintval($id);
182 // When both passwords match update admin account
183 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
184 // Save only when both passwords are the same (also when they are empty)
185 $ADD = ""; $cache_update = "1";
188 $hash = generateHash($POST['pass1'][$id]);
190 // Save password when set
191 if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
194 $salt = substr(GET_ADMIN_HASH(get_session('admin_login')), 0, -40);
195 $aid = GET_ADMIN_ID(get_session('admin_login'));
197 // Rewrite cookie when it's own account
199 // Set timeout cookie
200 set_session("admin_last", time());
202 if ($login != get_session('admin_login')) {
203 // Update login cookie
204 set_session("admin_login", $login);
206 // Update password cookie as well?
207 if (!empty($ADD)) set_session("admin_md5", $hash);
208 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
209 // Update password cookie
210 set_session("admin_md5", $hash);
214 // Get default ACL from admin to check if we can allow him to change the default ACL
215 $default = GET_ADMIN_DEFAULT_ACL(get_session('admin_login'));
217 // Update admin account
218 if ($default == "allow") {
219 // Allow changing default ACL
220 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET
225 WHERE id=%s LIMIT 1",
230 $POST['la_mode'][$id],
232 ), __FILE__, __LINE__);
234 // Do not allow it here
235 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET
239 WHERE id=%s LIMIT 1",
243 $POST['la_mode'][$id],
245 ), __FILE__, __LINE__);
249 CACHE_PURGE_ADMIN_MENU($id);
251 // Admin account saved
252 $MSG = ADMIN_ACCOUNT_SAVED;
254 // Passwords did not match
255 $MSG = ADMINS_ERROR_PASS_MISMATCH;
260 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"admin_done\">".$MSG."</SPAN>");
265 if ((EXT_IS_ACTIVE("cache")) && ($cache_update == "1")) {
266 if ($cacheInstance->cache_file("admins", true)) $cacheInstance->cache_destroy();
270 // Make admin accounts editable
271 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
272 // Begin the edit loop
274 foreach ($POST['sel'] as $id => $sel) {
276 $id = bigintval($id);
278 // Get the admin's data
279 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
280 array($id), __FILE__, __LINE__);
281 if ((SQL_NUMROWS($result) == 1) && ($sel == 1)) {
283 $content = SQL_FETCHARRAY($result);
284 SQL_FREERESULT($result);
286 // Prepare some more data for the template
287 $content['sw'] = $SW;
288 $content['id'] = $id;
290 // Shall we allow changing default ACL?
291 if ($content['mode'] == "allow") {
293 $content['mode'] = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(ADMINS_ALLOW_MODE, ADMINS_DENY_MODE), $content['mode']);
296 $content['mode'] = " ";
298 $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']);
300 // Load row template and switch color
301 $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
305 define('__ADMINS_ROWS', $OUT);
308 LOAD_TEMPLATE("admin_edit_admins");
311 // Delete given admin accounts
312 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
313 // Check if this account is the last one which cannot be deleted...
314 $result_main = SQL_QUERY("SELECT id FROM "._MYSQL_PREFIX."_admins", __FILE__, __LINE__);
315 $accounts = SQL_NUMROWS($result_main);
316 SQL_FREERESULT($result_main);
320 foreach ($POST['sel'] as $id => $sel) {
322 $id = bigintval($id);
324 // Get the admin's data
325 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
326 array($id), __FILE__, __LINE__);
327 if (SQL_NUMROWS($result) == 1) {
329 $content = SQL_FETCHARRAY($result);
330 SQL_FREERESULT($result);
331 $eval = "\$content['mode'] = ADMINS_".strtoupper($content['mode'])."_MODE;";
333 $eval = "\$content['la_mode'] = ADMINS_".strtoupper($content['la_mode'])."_LA_SETTING;";
336 // Prepare some more data
337 $content['sw'] = $SW;
338 $content['id'] = $id;
340 // Load row template and switch color
341 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
345 define('__ADMINS_ROWS', $OUT);
348 LOAD_TEMPLATE("admin_del_admins");
350 // Cannot delete last account!
351 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_ADMINS_CANNOT_DELETE_LAST);
355 // Remove the given accounts
356 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
359 foreach ($POST['sel'] as $id => $del) {
361 $id = bigintval($id);
363 // Delete only when it's not your own account!
364 if (($del == 1) && (GET_ADMIN_ID(get_session('admin_login')) != $id)) {
365 // Rewrite his tasks to all admins
366 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET assigned_admin=0 WHERE assigned_admin=%s",
367 array($id), __FILE__, __LINE__);
370 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
371 array($id), __FILE__, __LINE__);
374 CACHE_PURGE_ADMIN_MENU($id);
379 // Remove cache if cache system is activated
380 if ((EXT_IS_ACTIVE("cache")) && ($cache_update == "1")) {
381 if ($cacheInstance->cache_file("admins", true)) $cacheInstance->cache_destroy();
385 // List all admin accounts
386 function ADMINS_LIST_ADMIN_ACCOUNTS() {
387 // Select all admin accounts
388 $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins ORDER BY login ASC", __FILE__, __LINE__);
390 while ($content = SQL_FETCHARRAY($result)) {
391 // Compile some variables
392 $eval = "\$content['mode'] = ADMINS_".strtoupper($content['mode'])."_MODE;";
394 $eval = "\$content['la_mode'] = ADMINS_".strtoupper($content['la_mode'])."_LA_SETTING;";
397 // Prepare some more data
398 $content['sw'] = $SW;
399 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
401 // Load row template and switch color
402 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
407 SQL_FREERESULT($result);
408 define('__ADMINS_ROWS', $OUT);
411 LOAD_TEMPLATE("admin_list_admins");