c0d4c13023df4398210569cb391dad0960c30404
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
42         require($INC);
43 }
44
45 // Check ACL for menu combination
46 function ADMINS_CHECK_ACL($act, $wht) {
47         // If action is login or logout allow allways!
48         $default = "allow";
49         if (($act == "login") || ($act == "logout")) return true;
50
51         // Default is deny
52         $ret = false;
53
54         // Get admin's ID
55         $aid = GET_CURRENT_ADMIN_ID();
56
57         // Get admin's defult access right
58         $default = GET_ADMIN_DEFAULT_ACL($aid);
59
60         if (!empty($wht)) {
61                 // Check for parent menu:
62                 // First get it's action value
63                 $parent_action = GET_ACTION("admin", $wht);
64
65                 // Check with this function...
66                 $parent = ADMINS_CHECK_ACL($parent_action, "");
67         } else {
68                 // Anything else is true!
69                 $parent = false;
70         }
71
72         // Shall I test for a main or sub menu? (action or what?)
73         $acl_mode = "failed";
74         if ((GET_EXT_VERSION("cache") >= "0.1.2") && (count($GLOBALS['cache_array']['admin_acls']) > 0)) {
75                 // Lookup in cache
76                 if ((!empty($act)) && (isset($GLOBALS['cache_array']['admin_acls']['action_menu'][$aid])) & ($GLOBALS['cache_array']['admin_acls']['action_menu'][$aid] == $act)) {
77                         // Main menu line found
78                         $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$aid];
79
80                         // Count cache hits
81                         incrementConfigEntry('cache_hits');
82                 } elseif ((!empty($wht)) && (isset($GLOBALS['cache_array']['admin_acls']['what_menu'][$aid])) && ($GLOBALS['cache_array']['admin_acls']['what_menu'][$aid] == $wht)) {
83                         // Check sub menu
84                         $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$aid];
85
86                         // Count cache hits
87                         incrementConfigEntry('cache_hits');
88                 }
89         } elseif (!EXT_IS_ACTIVE("cache")) {
90                 // Old version, so load it from database
91                 if (!empty($act)) {
92                         // Main menu
93                         $result = SQL_QUERY_ESC("SELECT access_mode FROM `{!_MYSQL_PREFIX!}_admins_acls` WHERE admin_id=%s AND action_menu='%s' LIMIT 1",
94                                 array(bigintval($aid), $act), __FUNCTION__, __LINE__);
95                 } elseif (!empty($wht)) {
96                         // Sub menu
97                         $result = SQL_QUERY_ESC("SELECT access_mode FROM `{!_MYSQL_PREFIX!}_admins_acls` WHERE admin_id=%s AND what_menu='%s' LIMIT 1",
98                                 array(bigintval($aid), $wht), __FUNCTION__, __LINE__);
99                 }
100
101                 // Is an entry found?
102                 if (SQL_NUMROWS($result) == 1) {
103                         // Load ACL
104                         list($acl_mode) = SQL_FETCHROW($result);
105                 } // END - if
106
107                 // Free memory
108                 SQL_FREERESULT($result);
109         }
110
111         // Check ACL and (maybe) allow
112         if (($default == "allow") || (($default == "deny") && ($acl_mode == "allow")) || ($parent === true)) {
113                 // Access is granted
114                 $ret = true;
115         } // END - if
116
117         // Return value
118         return $ret;
119 }
120
121 // Create email link to admins's account
122 function ADMINS_CREATE_EMAIL_LINK ($email, $mod="admin") {
123         if (strpos("@", $email) > 0) {
124                 // Create email link
125                 $result = SQL_QUERY_ESC("SELECT id
126 FROM `{!_MYSQL_PREFIX!}_admins`
127 WHERE email='%s' LIMIT 1",
128                         array($email), __FUNCTION__, __LINE__);
129
130                 // Is there an entry?
131                 if (SQL_NUMROWS($result) == 1) {
132                         // Load userid
133                         list($uid) = SQL_FETCHROW($result);
134
135                         // Rewrite email address to contact link
136                         $email = "{!URL!}/modules.php?module=".$mod."&amp;what=user_contct&amp;uid=".bigintval($uid);
137                 }
138
139                 // Free memory
140                 SQL_FREERESULT($result);
141         } elseif ((is_int($email)) && ($email > 0)) {
142                 // Direct ID given
143                 $email = "{!URL!}/modules.php?module=".$mod."&amp;what=admins_contct&amp;admin=".bigintval($email);
144         }
145
146         // Return rewritten (?) email address
147         return $email;
148 }
149
150 // Change a lot admin account
151 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
152         // Begin the update
153         $cache_update = 0;
154         foreach ($POST['login'] as $id => $login) {
155                 // Secure ID number
156                 $id = bigintval($id);
157
158                 // When both passwords match update admin account
159                 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
160                         // Save only when both passwords are the same (also when they are empty)
161                         $add = ""; $cache_update = "1";
162
163                         // Generate hash
164                         $hash = generateHash($POST['pass1'][$id]);
165
166                         // Save password when set
167                         if (!empty($POST['pass1'][$id])) $add = sprintf(", password='%s'", SQL_ESCAPE($hash));
168
169                         // Get admin's ID
170                         $aid = GET_CURRENT_ADMIN_ID();
171                         $salt = substr(GET_ADMIN_HASH($aid), 0, -40);
172
173                         // Rewrite cookie when it's own account
174                         if ($aid == $id) {
175                                 // Set timeout cookie
176                                 set_session('admin_last', time());
177
178                                 if ($login != get_session('admin_login')) {
179                                         // Update login cookie
180                                         set_session('admin_login', $login);
181
182                                         // Update password cookie as well?
183                                         if (!empty($add)) set_session('admin_md5', $hash);
184                                 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
185                                         // Update password cookie
186                                         set_session('admin_md5', $hash);
187                                 }
188                         } // END - if
189
190                         // Get default ACL from admin to check if we can allow him to change the default ACL
191                         $default = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
192
193                         // Update admin account
194                         if ($default == "allow") {
195                                 // Allow changing default ACL
196                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
197 login='%s'".$add.",
198 email='%s',
199 default_acl='%s',
200 la_mode='%s'
201 WHERE id=%s LIMIT 1",
202                                         array(
203                                                 $login,
204                                                 $POST['email'][$id],
205                                                 $POST['mode'][$id],
206                                                 $POST['la_mode'][$id],
207                                                 $id
208                                         ), __FUNCTION__, __LINE__);
209                         } else {
210                                 // Do not allow it here
211                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
212 login='%s'".$add.",
213 email='%s',
214 la_mode='%s'
215 WHERE id=%s LIMIT 1",
216                                         array(
217                                                 $login,
218                                                 $POST['email'][$id],
219                                                 $POST['la_mode'][$id],
220                                                 $id
221                                         ), __FUNCTION__, __LINE__);
222                         }
223
224                         // Purge cache
225                         CACHE_PURGE_ADMIN_MENU($id);
226
227                         // Admin account saved
228                         $message = ADMIN_ACCOUNT_SAVED;
229                 } else {
230                         // Passwords did not match
231                         $message = ADMINS_ERROR_PASS_MISMATCH;
232                 }
233
234                 // Display message
235                 if (!empty($message)) {
236                         LOAD_TEMPLATE("admin_settings_saved", false, $message);
237                 }
238         }
239
240         // Remove cache file
241         runFilterChain('post_admin_edited', REQUEST_POST_ARRAY());
242 }
243
244 // Make admin accounts editable
245 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
246         // "Resolve" current's admin access mode
247         $currMode = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
248
249         // Begin the edit loop
250         $OUT = ""; $SW = 2;
251         foreach ($POST['sel'] as $id => $selected) {
252                 // Secure ID number
253                 $id = bigintval($id);
254
255                 // Get the admin's data
256                 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
257                         array($id), __FUNCTION__, __LINE__);
258                 if ((SQL_NUMROWS($result) == 1) && ($selected == 1)) {
259                         // Entry found
260                         $content = SQL_FETCHARRAY($result);
261                         SQL_FREERESULT($result);
262
263                         // Prepare some more data for the template
264                         $content['sw'] = $SW;
265                         $content['id'] = $id;
266
267                         // Shall we allow changing default ACL?
268                         if ($currMode == "allow") {
269                                 // Allow chaning it
270                                 $content['mode']    = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(constant('ADMINS_ALLOW_MODE'), constant('ADMINS_DENY_MODE')), $content['mode']);
271                         } else {
272                                 // Don't allow it
273                                 $content['mode'] = "&nbsp;";
274                         }
275                         $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']);
276
277                         // Load row template and switch color
278                         $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
279                         $SW = 3 - $SW;
280                 }
281         }
282         define('__ADMINS_ROWS', $OUT);
283
284         // Load template
285         LOAD_TEMPLATE("admin_edit_admins");
286 }
287
288 // Delete given admin accounts
289 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
290         // Check if this account is the last one which cannot be deleted...
291         $result_main = SQL_QUERY("SELECT id FROM `{!_MYSQL_PREFIX!}_admins`", __FUNCTION__, __LINE__);
292         $accounts = SQL_NUMROWS($result_main);
293         SQL_FREERESULT($result_main);
294         if ($accounts > 1) {
295                 // Delete accounts
296                 $OUT = ""; $SW = 2;
297                 foreach ($POST['sel'] as $id => $selected) {
298                         // Secure ID number
299                         $id = bigintval($id);
300
301                         // Get the admin's data
302                         $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
303                                 array($id), __FUNCTION__, __LINE__);
304                         if (SQL_NUMROWS($result) == 1) {
305                                 // Entry found
306                                 $content = SQL_FETCHARRAY($result);
307                                 SQL_FREERESULT($result);
308                                 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
309                                 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
310
311                                 // Prepare some more data
312                                 $content['sw'] = $SW;
313                                 $content['id'] = $id;
314
315                                 // Load row template and switch color
316                                 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
317                                 $SW = 3 - $SW;
318                         }
319                 }
320                 define('__ADMINS_ROWS', $OUT);
321
322                 // Load template
323                 LOAD_TEMPLATE("admin_del_admins");
324         } else {
325                 // Cannot delete last account!
326                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_ADMINS_CANNOT_DELETE_LAST'));
327         }
328 }
329
330 // Remove the given accounts
331 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
332         // Begin removal
333         $cache_update = 0;
334         foreach ($POST['sel'] as $id => $del) {
335                 // Secure ID number
336                 $id = bigintval($id);
337
338                 // Delete only when it's not your own account!
339                 if (($del == 1) && (GET_CURRENT_ADMIN_ID() != $id)) {
340                         // Rewrite his tasks to all admins
341                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin=0 WHERE assigned_admin=%s",
342                          array($id), __FUNCTION__, __LINE__);
343
344                         // Remove account
345                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
346                                 array($id), __FUNCTION__, __LINE__);
347
348                         // Purge cache
349                         CACHE_PURGE_ADMIN_MENU($id);
350                         $cache_update = "1";
351                 }
352         }
353
354         // Remove cache if cache system is activated
355         runFilterChain('post_admin_deleted', REQUEST_POST_ARRAY());
356 }
357
358 // List all admin accounts
359 function ADMINS_LIST_ADMIN_ACCOUNTS() {
360         // Select all admin accounts
361         $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` ORDER BY login ASC", __FUNCTION__, __LINE__);
362         $OUT = ""; $SW = 2;
363         while ($content = SQL_FETCHARRAY($result)) {
364                 // Compile some variables
365                 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
366                 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
367
368                 // Prepare some more data
369                 $content['sw']         = $SW;
370                 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
371
372                 // Load row template and switch color
373                 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
374                 $SW = 3 - $SW;
375         }
376
377         // Free memory
378         SQL_FREERESULT($result);
379         define('__ADMINS_ROWS', $OUT);
380
381         // Load template
382         LOAD_TEMPLATE("admin_list_admins");
383 }
384
385 // Filter for adding extra data to the query
386 function FILTER_ADD_EXTRA_SQL_DATA ($add = "") {
387         // Is the admins extension updated? (should be!)
388         if (GET_EXT_VERSION("admins") >= "0.3")   $add .= ", default_acl AS def_acl";
389         if (GET_EXT_VERSION("admins") >= "0.6.7") $add .= ", la_mode";
390         if (GET_EXT_VERSION("admins") >= "0.7.2") $add .= ", login_failures, UNIX_TIMESTAMP(last_failure) AS last_failure";
391
392         // Return it
393         return $add;
394 }
395
396 //
397 ?>