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 $_CONFIG['cache_hits']++;
57 } elseif (!is_object($cacheInstance)) {
59 $result = SQL_QUERY_ESC("SELECT default_acl FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
60 array(get_session('admin_login')), __FILE__, __LINE__);
61 list($default) = SQL_FETCHROW($result);
62 SQL_FREERESULT($result);
66 $aid = GET_ADMIN_ID(get_session('admin_login'));
69 // Check for parent menu:
70 // First get it's action value
71 $parent_action = GET_ACTION("admin", $wht);
73 // Check with this function...
74 $parent = ADMINS_CHECK_ACL($parent_action, "");
76 // Anything else is true!
80 // Shall I test for a main or sub menu? (action or what?)
81 $lines = 0; $acl_mode = "failed";
82 if (GET_EXT_VERSION("cache") >= "0.1.2") {
83 // Load only from array when there are lines!
84 if ((isset($cacheArray['admin_acls'])) && (is_array($cacheArray['admin_acls'])) && (count($cacheArray['admin_acls']) > 0)) {
85 // Load ACL from array
86 foreach ($cacheArray['admin_acls']['admin_id'] as $id => $aid_acls) {
87 if ($aid == $aid_acls) {
88 // Okay, one line was found!
89 if ((!empty($act)) && ($cacheArray['admin_acls']['action_menu'][$id] == $act)) {
90 // Main menu line found
91 $acl_mode = $cacheArray['admin_acls']['access_mode'][$id];
94 elseif ((!empty($wht)) && ($cacheArray['admin_acls']['what_menu'][$id] == $wht)) {
96 $acl_mode = $cacheArray['admin_acls']['access_mode'][$id];
101 $_CONFIG['cache_hits']++;
108 if ($acl_mode == "failed") {
117 // Old version, so load it from database
121 $result = SQL_QUERY_ESC("SELECT access_mode FROM "._MYSQL_PREFIX."_admins_acls WHERE admin_id=%s AND action_menu='%s' LIMIT 1",
122 array(bigintval($aid), $act), __FILE__, __LINE__);
123 } elseif (!empty($wht)) {
125 $result = SQL_QUERY_ESC("SELECT access_mode FROM "._MYSQL_PREFIX."_admins_acls WHERE admin_id=%s AND what_menu='%s' LIMIT 1",
126 array(bigintval($aid), $wht), __FILE__, __LINE__);
129 // Get number of lines
130 $lines = SQL_NUMROWS($result);
133 list($acl_mode) = SQL_FETCHROW($result);
134 SQL_FREERESULT($result);
137 // Check ACL and (maybe) allow
138 if ((($default == "allow") && ($lines == 0)) || (($default == "deny") && ($lines == "1") && ($acl_mode == "allow")) || (($lines == 0) && ($parent))) $ret = true;
144 // Create email link to admins's account
145 function ADMINS_CREATE_EMAIL_LINK($email, $mod="admin") {
146 $locked = " AND status='CONFIRMED'";
147 if (IS_ADMIN()) $locked = "";
148 if (strpos("@", $email) > 0) {
150 $result = SQL_QUERY_ESC("SELECT id
151 FROM "._MYSQL_PREFIX."_admins
152 WHERE email='%s'".$locked." LIMIT 1",
153 array($email), __FILE__, __LINE__);
155 // Is there an entry?
156 if (SQL_NUMROWS($result) == 1) {
158 list($uid) = SQL_FETCHROW($result);
160 // Rewrite email address to contact link
161 $email = URL."/modules.php?module=".$mod."&what=user_contct&u_id=".bigintval($uid);
165 SQL_FREERESULT($result);
166 } elseif (bigintval($email) > 0) {
168 $email = URL."/modules.php?module=".$mod."&what=admins_contct&admin=".bigintval($email);
171 // Return rewritten (?) email address
175 // Change a lot admin account
176 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
177 global $cacheInstance;
181 foreach ($POST['login'] as $id => $login) {
183 $id = bigintval($id);
185 // When both passwords match update admin account
186 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
187 // Save only when both passwords are the same (also when they are empty)
188 $ADD = ""; $cache_update = "1";
191 $hash = generateHash($POST['pass1'][$id]);
193 // Save password when set
194 if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
197 $salt = substr(GET_ADMIN_HASH(get_session('admin_login')), 0, -40);
198 $aid = GET_ADMIN_ID(get_session('admin_login'));
200 // Rewrite cookie when it's own account
202 // Set timeout cookie
203 set_session("admin_last", time());
205 if ($login != get_session('admin_login')) {
206 // Update login cookie
207 set_session("admin_login", $login);
209 // Update password cookie as well?
210 if (!empty($ADD)) set_session("admin_md5", $hash);
211 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
212 // Update password cookie
213 set_session("admin_md5", $hash);
218 // Get default ACL from admin to check if we can allow him to change the default ACL
219 $result = SQL_QUERY_ESC("SELECT default_acl FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
220 array(get_session('admin_login')), __FILE__, __LINE__);
221 list($default) = SQL_FETCHROW($result);
224 SQL_FREERESULT($result);
226 // Update admin account
227 if ($default == "allow") {
228 // Allow changing default ACL
229 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET
234 WHERE id=%s LIMIT 1",
239 $POST['la_mode'][$id],
241 ), __FILE__, __LINE__);
243 // Do not allow it here
244 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET
248 WHERE id=%s LIMIT 1",
252 $POST['la_mode'][$id],
254 ), __FILE__, __LINE__);
258 CACHE_PURGE_ADMIN_MENU($id);
260 // Admin account saved
261 $MSG = ADMIN_ACCOUNT_SAVED;
263 // Passwords did not match
264 $MSG = ADMINS_ERROR_PASS_MISMATCH;
269 LOAD_TEMPLATE("admin_settings_saved", false, "<SPAN class=\"admin_done\">".$MSG."</SPAN>");
274 if ((EXT_IS_ACTIVE("cache")) && ($cache_update == "1")) {
275 if ($cacheInstance->cache_file("admins", true)) $cacheInstance->cache_destroy();
279 // Make admin accounts editable
280 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
281 // Begin the edit loop
283 foreach ($POST['sel'] as $id => $sel) {
285 $id = bigintval($id);
287 // Get the admin's data
288 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
289 array($id), __FILE__, __LINE__);
290 if ((SQL_NUMROWS($result) == 1) && ($sel == 1)) {
292 $content = SQL_FETCHARRAY($result);
293 SQL_FREERESULT($result);
295 // Prepare some more data for the template
296 $content['sw'] = $SW;
297 $content['id'] = $id;
299 // Shall we allow changing default ACL?
300 if ($content['mode'] == "allow") {
302 $content['mode'] = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(ADMINS_ALLOW_MODE, ADMINS_DENY_MODE), $content['mode']);
305 $content['mode'] = " ";
307 $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']);
309 // Load row template and switch color
310 $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
314 define('__ADMINS_ROWS', $OUT);
317 LOAD_TEMPLATE("admin_edit_admins");
320 // Delete given admin accounts
321 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
322 // Check if this account is the last one which cannot be deleted...
323 $result_main = SQL_QUERY("SELECT id FROM "._MYSQL_PREFIX."_admins", __FILE__, __LINE__);
324 $accounts = SQL_NUMROWS($result_main);
325 SQL_FREERESULT($result_main);
329 foreach ($POST['sel'] as $id => $sel) {
331 $id = bigintval($id);
333 // Get the admin's data
334 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
335 array($id), __FILE__, __LINE__);
336 if (SQL_NUMROWS($result) == 1) {
338 $content = SQL_FETCHARRAY($result);
339 SQL_FREERESULT($result);
340 $eval = "\$content['mode'] = ADMINS_".strtoupper($content['mode'])."_MODE;";
342 $eval = "\$content['la_mode'] = ADMINS_".strtoupper($content['la_mode'])."_LA_SETTING;";
345 // Prepare some more data
346 $content['sw'] = $SW;
347 $content['id'] = $id;
349 // Load row template and switch color
350 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
354 define('__ADMINS_ROWS', $OUT);
357 LOAD_TEMPLATE("admin_del_admins");
359 // Cannot delete last account!
360 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_ADMINS_CANNOT_DELETE_LAST);
364 // Remove the given accounts
365 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
368 foreach ($POST['sel'] as $id => $del) {
370 $id = bigintval($id);
372 // Delete only when it's not your own account!
373 if (($del == 1) && (GET_ADMIN_ID(get_session('admin_login')) != $id)) {
374 // Rewrite his tasks to all admins
375 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET assigned_admin=0 WHERE assigned_admin=%s",
376 array($id), __FILE__, __LINE__);
379 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
380 array($id), __FILE__, __LINE__);
383 CACHE_PURGE_ADMIN_MENU($id);
388 // Remove cache if cache system is activated
389 if ((EXT_IS_ACTIVE("cache")) && ($cache_update == "1")) {
390 if ($cacheInstance->cache_file("admins", true)) $cacheInstance->cache_destroy();
394 // List all admin accounts
395 function ADMINS_LIST_ADMIN_ACCOUNTS() {
396 // Select all admin accounts
397 $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM "._MYSQL_PREFIX."_admins ORDER BY login ASC", __FILE__, __LINE__);
399 while ($content = SQL_FETCHARRAY($result)) {
400 // Compile some variables
401 $eval = "\$content['mode'] = ADMINS_".strtoupper($content['mode'])."_MODE;";
403 $eval = "\$content['la_mode'] = ADMINS_".strtoupper($content['la_mode'])."_LA_SETTING;";
406 // Prepare some more data
407 $content['sw'] = $SW;
408 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
410 // Load row template and switch color
411 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
416 SQL_FREERESULT($result);
417 define('__ADMINS_ROWS', $OUT);
420 LOAD_TEMPLATE("admin_list_admins");