A lot fixes to templates and missing functions added, more rewrites
[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         $locked = " AND `status`='CONFIRMED'";
134         if (IS_ADMIN()) $locked = "";
135         if (strpos("@", $email) > 0) {
136                 // Create email link
137                 $result = SQL_QUERY_ESC("SELECT id
138 FROM `{!_MYSQL_PREFIX!}_admins`
139 WHERE email='%s'".$locked." LIMIT 1",
140  array($email), __FUNCTION__, __LINE__);
141
142                 // Is there an entry?
143                 if (SQL_NUMROWS($result) == 1) {
144                         // Load userid
145                         list($uid) = SQL_FETCHROW($result);
146
147                         // Rewrite email address to contact link
148                         $email = "{!URL!}/modules.php?module=".$mod."&amp;what=user_contct&amp;uid=".bigintval($uid);
149                 }
150
151                 // Free memory
152                 SQL_FREERESULT($result);
153         } elseif (bigintval($email) > 0) {
154                 // Direct ID given
155                 $email = "{!URL!}/modules.php?module=".$mod."&amp;what=admins_contct&amp;admin=".bigintval($email);
156         }
157
158         // Return rewritten (?) email address
159         return $email;
160 }
161
162 // Change a lot admin account
163 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
164         // Begin the update
165         $cache_update = 0;
166         foreach ($POST['login'] as $id => $login) {
167                 // Secure ID number
168                 $id = bigintval($id);
169
170                 // When both passwords match update admin account
171                 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
172                         // Save only when both passwords are the same (also when they are empty)
173                         $ADD = ""; $cache_update = "1";
174
175                         // Generate hash
176                         $hash = generateHash($POST['pass1'][$id]);
177
178                         // Save password when set
179                         if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
180
181                         // Get admin's ID
182                         $aid = GET_CURRENT_ADMIN_ID();
183                         $salt = substr(GET_ADMIN_HASH($aid), 0, -40);
184
185                         // Rewrite cookie when it's own account
186                         if ($aid == $id) {
187                                 // Set timeout cookie
188                                 set_session('admin_last', time());
189
190                                 if ($login != get_session('admin_login')) {
191                                         // Update login cookie
192                                         set_session('admin_login', $login);
193
194                                         // Update password cookie as well?
195                                         if (!empty($ADD)) set_session('admin_md5', $hash);
196                                 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
197                                         // Update password cookie
198                                         set_session('admin_md5', $hash);
199                                 }
200                         } // END - if
201
202                         // Get default ACL from admin to check if we can allow him to change the default ACL
203                         $default = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
204
205                         // Update admin account
206                         if ($default == "allow") {
207                                 // Allow changing default ACL
208                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
209 login='%s'".$ADD.",
210 email='%s',
211 default_acl='%s',
212 la_mode='%s'
213 WHERE id=%s LIMIT 1",
214                                         array(
215                                                 $login,
216                                                 $POST['email'][$id],
217                                                 $POST['mode'][$id],
218                                                 $POST['la_mode'][$id],
219                                                 $id
220                                         ), __FUNCTION__, __LINE__);
221                         } else {
222                                 // Do not allow it here
223                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
224 login='%s'".$ADD.",
225 email='%s',
226 la_mode='%s'
227 WHERE id=%s LIMIT 1",
228                                         array(
229                                                 $login,
230                                                 $POST['email'][$id],
231                                                 $POST['la_mode'][$id],
232                                                 $id
233                                         ), __FUNCTION__, __LINE__);
234                         }
235
236                         // Purge cache
237                         CACHE_PURGE_ADMIN_MENU($id);
238
239                         // Admin account saved
240                         $MSG = ADMIN_ACCOUNT_SAVED;
241                 } else {
242                         // Passwords did not match
243                         $MSG = ADMINS_ERROR_PASS_MISMATCH;
244                 }
245
246                 // Display message
247                 if (!empty($MSG)) {
248                         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
249                 }
250         }
251
252         // Remove cache file
253         RUN_FILTER('post_admin_edited', REQUEST_POST_ARRAY());
254 }
255
256 // Make admin accounts editable
257 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
258         // "Resolve" current's admin access mode
259         $currMode = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
260
261         // Begin the edit loop
262         $SW = 2; $OUT = "";
263         foreach ($POST['sel'] as $id => $sel) {
264                 // Secure ID number
265                 $id = bigintval($id);
266
267                 // Get the admin's data
268                 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
269                         array($id), __FUNCTION__, __LINE__);
270                 if ((SQL_NUMROWS($result) == 1) && ($sel == 1)) {
271                         // Entry found
272                         $content = SQL_FETCHARRAY($result);
273                         SQL_FREERESULT($result);
274
275                         // Prepare some more data for the template
276                         $content['sw'] = $SW;
277                         $content['id'] = $id;
278
279                         // Shall we allow changing default ACL?
280                         if ($currMode == "allow") {
281                                 // Allow chaning it
282                                 $content['mode']    = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(constant('ADMINS_ALLOW_MODE'), constant('ADMINS_DENY_MODE')), $content['mode']);
283                         } else {
284                                 // Don't allow it
285                                 $content['mode'] = "&nbsp;";
286                         }
287                         $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']);
288
289                         // Load row template and switch color
290                         $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
291                         $SW = 3 - $SW;
292                 }
293         }
294         define('__ADMINS_ROWS', $OUT);
295
296         // Load template
297         LOAD_TEMPLATE("admin_edit_admins");
298 }
299
300 // Delete given admin accounts
301 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
302         // Check if this account is the last one which cannot be deleted...
303         $result_main = SQL_QUERY("SELECT id FROM `{!_MYSQL_PREFIX!}_admins`", __FUNCTION__, __LINE__);
304         $accounts = SQL_NUMROWS($result_main);
305         SQL_FREERESULT($result_main);
306         if ($accounts > 1) {
307                 // Delete accounts
308                 $SW = 2; $OUT = "";
309                 foreach ($POST['sel'] as $id => $sel) {
310                         // Secure ID number
311                         $id = bigintval($id);
312
313                         // Get the admin's data
314                         $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
315                          array($id), __FUNCTION__, __LINE__);
316                         if (SQL_NUMROWS($result) == 1) {
317                                 // Entry found
318                                 $content = SQL_FETCHARRAY($result);
319                                 SQL_FREERESULT($result);
320                                 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
321                                 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
322
323                                 // Prepare some more data
324                                 $content['sw'] = $SW;
325                                 $content['id'] = $id;
326
327                                 // Load row template and switch color
328                                 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
329                                 $SW = 3 - $SW;
330                         }
331                 }
332                 define('__ADMINS_ROWS', $OUT);
333
334                 // Load template
335                 LOAD_TEMPLATE("admin_del_admins");
336         } else {
337                 // Cannot delete last account!
338                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_ADMINS_CANNOT_DELETE_LAST'));
339         }
340 }
341
342 // Remove the given accounts
343 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
344         // Begin removal
345         $cache_update = 0;
346         foreach ($POST['sel'] as $id => $del) {
347                 // Secure ID number
348                 $id = bigintval($id);
349
350                 // Delete only when it's not your own account!
351                 if (($del == 1) && (GET_CURRENT_ADMIN_ID() != $id)) {
352                         // Rewrite his tasks to all admins
353                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin=0 WHERE assigned_admin=%s",
354                          array($id), __FUNCTION__, __LINE__);
355
356                         // Remove account
357                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
358                                 array($id), __FUNCTION__, __LINE__);
359
360                         // Purge cache
361                         CACHE_PURGE_ADMIN_MENU($id);
362                         $cache_update = "1";
363                 }
364         }
365
366         // Remove cache if cache system is activated
367         RUN_FILTER('post_admin_deleted', REQUEST_POST_ARRAY());
368 }
369
370 // List all admin accounts
371 function ADMINS_LIST_ADMIN_ACCOUNTS() {
372         // Select all admin accounts
373         $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` ORDER BY login ASC", __FUNCTION__, __LINE__);
374         $SW = 2; $OUT = "";
375         while ($content = SQL_FETCHARRAY($result)) {
376                 // Compile some variables
377                 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
378                 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
379
380                 // Prepare some more data
381                 $content['sw']         = $SW;
382                 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
383
384                 // Load row template and switch color
385                 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
386                 $SW = 3 - $SW;
387         }
388
389         // Free memory
390         SQL_FREERESULT($result);
391         define('__ADMINS_ROWS', $OUT);
392
393         // Load template
394         LOAD_TEMPLATE("admin_list_admins");
395 }
396
397 // Filter for adding extra data to the query
398 function FILTER_ADD_EXTRA_SQL_DATA ($ADD = "") {
399         // Is the admins extension updated? (should be!)
400         if (GET_EXT_VERSION("admins") >= "0.3")   $ADD .= ", default_acl AS def_acl";
401         if (GET_EXT_VERSION("admins") >= "0.6.7") $ADD .= ", la_mode";
402         if (GET_EXT_VERSION("admins") >= "0.7.2") $ADD .= ", login_failures, UNIX_TIMESTAMP(last_failure) AS last_failure";
403
404         // Return it
405         return $ADD;
406 }
407
408 //
409 ?>