]> git.mxchange.org Git - mailer.git/blob - inc/libs/admins_functions.php
Fix for 'no admin assigned' bug. Resolves #108
[mailer.git] / inc / libs / admins_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 06/30/2003 *
4  * ===============                              Last change: 11/27/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : admins_functions.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for the admins extension               *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer die admins-Erweiterung           *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
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.                                  *
22  *                                                                      *
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.                         *
27  *                                                                      *
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,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Check ACL for menu combination
41 function ADMINS_CHECK_ACL($act, $wht) {
42         // If action is login or logout allow allways!
43         $default = "allow";
44         if (($act == "login") || ($act == "logout")) return true;
45
46         // Default is deny
47         $ret = false;
48
49         // Get admin's ID
50         $aid = GET_CURRENT_ADMIN_ID();
51
52         // Get admin's defult access right
53         $default = GET_ADMIN_DEFAULT_ACL($aid);
54
55         if (!empty($wht)) {
56                 // Check for parent menu:
57                 // First get it's action value
58                 $parent_action = GET_ACTION("admin", $wht);
59
60                 // Check with this function...
61                 $parent = ADMINS_CHECK_ACL($parent_action, "");
62         } else {
63                 // Anything else is true!
64                 $parent = false;
65         }
66
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];
79                                                 $lines = 1;
80                                         }
81                                          elseif ((!empty($wht)) && ($GLOBALS['cache_array']['admin_acls']['what_menu'][$id] == $wht)) {
82                                                 // Check sub menu
83                                                 $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$id];
84                                                 $lines = 1;
85                                         }
86                                         if ($lines == 1) {
87                                                 // Count cache hits
88                                                 incrementConfigEntry('cache_hits');
89                                                 break;
90                                         }
91                                 }
92                         }
93
94                         // No ACL found?
95                         if ($acl_mode == "failed") {
96                                 $acl_mode = "";
97                                 $lines = 0;
98                         }
99                 } else {
100                         // No lines here
101                         $lines = 0;
102                 }
103         } else {
104                 // Old version, so load it from database
105                 if (!empty($act))
106                 {
107                         // Main menu
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)) {
111                         // Sub menu
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__);
114                 }
115
116                 // Get number of lines
117                 $lines = SQL_NUMROWS($result);
118
119                 // Load ACL
120                 list($acl_mode) = SQL_FETCHROW($result);
121                 SQL_FREERESULT($result);
122         }
123
124         // Check ACL and (maybe) allow
125         if ((($default == "allow") && ($lines == 0)) || (($default == "deny") && ($lines == "1") && ($acl_mode == "allow")) || (($lines == 0) && ($parent))) $ret = true;
126
127         // Return value
128         return $ret;
129 }
130
131 // Create email link to admins's account
132 function ADMINS_CREATE_EMAIL_LINK ($email, $mod="admin") {
133         if (strpos("@", $email) > 0) {
134                 // Create email link
135                 $result = SQL_QUERY_ESC("SELECT id
136 FROM `{!_MYSQL_PREFIX!}_admins`
137 WHERE email='%s' LIMIT 1",
138                         array($email), __FUNCTION__, __LINE__);
139
140                 // Is there an entry?
141                 if (SQL_NUMROWS($result) == 1) {
142                         // Load userid
143                         list($uid) = SQL_FETCHROW($result);
144
145                         // Rewrite email address to contact link
146                         $email = "{!URL!}/modules.php?module=".$mod."&amp;what=user_contct&amp;uid=".bigintval($uid);
147                 }
148
149                 // Free memory
150                 SQL_FREERESULT($result);
151         } elseif ((is_int($email)) && ($email > 0)) {
152                 // Direct ID given
153                 $email = "{!URL!}/modules.php?module=".$mod."&amp;what=admins_contct&amp;admin=".bigintval($email);
154         }
155
156         // Return rewritten (?) email address
157         return $email;
158 }
159
160 // Change a lot admin account
161 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
162         // Begin the update
163         $cache_update = 0;
164         foreach ($POST['login'] as $id => $login) {
165                 // Secure ID number
166                 $id = bigintval($id);
167
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";
172
173                         // Generate hash
174                         $hash = generateHash($POST['pass1'][$id]);
175
176                         // Save password when set
177                         if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
178
179                         // Get admin's ID
180                         $aid = GET_CURRENT_ADMIN_ID();
181                         $salt = substr(GET_ADMIN_HASH($aid), 0, -40);
182
183                         // Rewrite cookie when it's own account
184                         if ($aid == $id) {
185                                 // Set timeout cookie
186                                 set_session('admin_last', time());
187
188                                 if ($login != get_session('admin_login')) {
189                                         // Update login cookie
190                                         set_session('admin_login', $login);
191
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);
197                                 }
198                         } // END - if
199
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());
202
203                         // Update admin account
204                         if ($default == "allow") {
205                                 // Allow changing default ACL
206                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
207 login='%s'".$ADD.",
208 email='%s',
209 default_acl='%s',
210 la_mode='%s'
211 WHERE id=%s LIMIT 1",
212                                         array(
213                                                 $login,
214                                                 $POST['email'][$id],
215                                                 $POST['mode'][$id],
216                                                 $POST['la_mode'][$id],
217                                                 $id
218                                         ), __FUNCTION__, __LINE__);
219                         } else {
220                                 // Do not allow it here
221                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
222 login='%s'".$ADD.",
223 email='%s',
224 la_mode='%s'
225 WHERE id=%s LIMIT 1",
226                                         array(
227                                                 $login,
228                                                 $POST['email'][$id],
229                                                 $POST['la_mode'][$id],
230                                                 $id
231                                         ), __FUNCTION__, __LINE__);
232                         }
233
234                         // Purge cache
235                         CACHE_PURGE_ADMIN_MENU($id);
236
237                         // Admin account saved
238                         $MSG = ADMIN_ACCOUNT_SAVED;
239                 } else {
240                         // Passwords did not match
241                         $MSG = ADMINS_ERROR_PASS_MISMATCH;
242                 }
243
244                 // Display message
245                 if (!empty($MSG)) {
246                         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
247                 }
248         }
249
250         // Remove cache file
251         runFilterChain('post_admin_edited', REQUEST_POST_ARRAY());
252 }
253
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());
258
259         // Begin the edit loop
260         $OUT = ""; $SW = 2;
261         foreach ($POST['sel'] as $id => $selected) {
262                 // Secure ID number
263                 $id = bigintval($id);
264
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)) {
269                         // Entry found
270                         $content = SQL_FETCHARRAY($result);
271                         SQL_FREERESULT($result);
272
273                         // Prepare some more data for the template
274                         $content['sw'] = $SW;
275                         $content['id'] = $id;
276
277                         // Shall we allow changing default ACL?
278                         if ($currMode == "allow") {
279                                 // Allow chaning it
280                                 $content['mode']    = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(constant('ADMINS_ALLOW_MODE'), constant('ADMINS_DENY_MODE')), $content['mode']);
281                         } else {
282                                 // Don't allow it
283                                 $content['mode'] = "&nbsp;";
284                         }
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']);
286
287                         // Load row template and switch color
288                         $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
289                         $SW = 3 - $SW;
290                 }
291         }
292         define('__ADMINS_ROWS', $OUT);
293
294         // Load template
295         LOAD_TEMPLATE("admin_edit_admins");
296 }
297
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);
304         if ($accounts > 1) {
305                 // Delete accounts
306                 $OUT = ""; $SW = 2;
307                 foreach ($POST['sel'] as $id => $selected) {
308                         // Secure ID number
309                         $id = bigintval($id);
310
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) {
315                                 // Entry found
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');
320
321                                 // Prepare some more data
322                                 $content['sw'] = $SW;
323                                 $content['id'] = $id;
324
325                                 // Load row template and switch color
326                                 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
327                                 $SW = 3 - $SW;
328                         }
329                 }
330                 define('__ADMINS_ROWS', $OUT);
331
332                 // Load template
333                 LOAD_TEMPLATE("admin_del_admins");
334         } else {
335                 // Cannot delete last account!
336                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_ADMINS_CANNOT_DELETE_LAST'));
337         }
338 }
339
340 // Remove the given accounts
341 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
342         // Begin removal
343         $cache_update = 0;
344         foreach ($POST['sel'] as $id => $del) {
345                 // Secure ID number
346                 $id = bigintval($id);
347
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__);
353
354                         // Remove account
355                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
356                                 array($id), __FUNCTION__, __LINE__);
357
358                         // Purge cache
359                         CACHE_PURGE_ADMIN_MENU($id);
360                         $cache_update = "1";
361                 }
362         }
363
364         // Remove cache if cache system is activated
365         runFilterChain('post_admin_deleted', REQUEST_POST_ARRAY());
366 }
367
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__);
372         $OUT = ""; $SW = 2;
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');
377
378                 // Prepare some more data
379                 $content['sw']         = $SW;
380                 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
381
382                 // Load row template and switch color
383                 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
384                 $SW = 3 - $SW;
385         }
386
387         // Free memory
388         SQL_FREERESULT($result);
389         define('__ADMINS_ROWS', $OUT);
390
391         // Load template
392         LOAD_TEMPLATE("admin_list_admins");
393 }
394
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";
401
402         // Return it
403         return $ADD;
404 }
405
406 //
407 ?>