Fixed logfile writing in installation phase, .revision is now ignored
[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:: 856                                                    $ *
14  * $Date:: 2009-03-06 20:24:32 +0100 (Fr, 06. Mär 2009)              $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author:: stelzi                                                   $ *
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         $lines = 0; $acl_mode = "failed";
74         if (GET_EXT_VERSION("cache") >= "0.1.2") {
75                 // Load only from array when there are lines!
76                 if ((isset($GLOBALS['cache_array']['admin_acls'])) && (is_array($GLOBALS['cache_array']['admin_acls'])) && (count($GLOBALS['cache_array']['admin_acls']) > 0)) {
77                         // Load ACL from array
78                         foreach ($GLOBALS['cache_array']['admin_acls']['admin_id'] as $id => $aid_acls) {
79                                 if ($aid == $aid_acls) {
80                                         // Okay, one line was found!
81                                         if ((!empty($act)) && ($GLOBALS['cache_array']['admin_acls']['action_menu'][$id] == $act)) {
82                                                 // Main menu line found
83                                                 $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$id];
84                                                 $lines = 1;
85                                         }
86                                          elseif ((!empty($wht)) && ($GLOBALS['cache_array']['admin_acls']['what_menu'][$id] == $wht)) {
87                                                 // Check sub menu
88                                                 $acl_mode = $GLOBALS['cache_array']['admin_acls']['access_mode'][$id];
89                                                 $lines = 1;
90                                         }
91                                         if ($lines == 1) {
92                                                 // Count cache hits
93                                                 incrementConfigEntry('cache_hits');
94                                                 break;
95                                         }
96                                 }
97                         }
98
99                         // No ACL found?
100                         if ($acl_mode == "failed") {
101                                 $acl_mode = "";
102                                 $lines = 0;
103                         }
104                 } else {
105                         // No lines here
106                         $lines = 0;
107                 }
108         } else {
109                 // Old version, so load it from database
110                 if (!empty($act))
111                 {
112                         // Main menu
113                         $result = SQL_QUERY_ESC("SELECT access_mode FROM `{!_MYSQL_PREFIX!}_admins_acls` WHERE admin_id=%s AND action_menu='%s' LIMIT 1",
114                          array(bigintval($aid), $act), __FUNCTION__, __LINE__);
115                 } elseif (!empty($wht)) {
116                         // Sub menu
117                         $result = SQL_QUERY_ESC("SELECT access_mode FROM `{!_MYSQL_PREFIX!}_admins_acls` WHERE admin_id=%s AND what_menu='%s' LIMIT 1",
118                          array(bigintval($aid), $wht), __FUNCTION__, __LINE__);
119                 }
120
121                 // Get number of lines
122                 $lines = SQL_NUMROWS($result);
123
124                 // Load ACL
125                 list($acl_mode) = SQL_FETCHROW($result);
126                 SQL_FREERESULT($result);
127         }
128
129         // Check ACL and (maybe) allow
130         if ((($default == "allow") && ($lines == 0)) || (($default == "deny") && ($lines == "1") && ($acl_mode == "allow")) || (($lines == 0) && ($parent))) $ret = true;
131
132         // Return value
133         return $ret;
134 }
135
136 // Create email link to admins's account
137 function ADMINS_CREATE_EMAIL_LINK ($email, $mod="admin") {
138         if (strpos("@", $email) > 0) {
139                 // Create email link
140                 $result = SQL_QUERY_ESC("SELECT id
141 FROM `{!_MYSQL_PREFIX!}_admins`
142 WHERE email='%s' LIMIT 1",
143                         array($email), __FUNCTION__, __LINE__);
144
145                 // Is there an entry?
146                 if (SQL_NUMROWS($result) == 1) {
147                         // Load userid
148                         list($uid) = SQL_FETCHROW($result);
149
150                         // Rewrite email address to contact link
151                         $email = "{!URL!}/modules.php?module=".$mod."&amp;what=user_contct&amp;uid=".bigintval($uid);
152                 }
153
154                 // Free memory
155                 SQL_FREERESULT($result);
156         } elseif ((is_int($email)) && ($email > 0)) {
157                 // Direct ID given
158                 $email = "{!URL!}/modules.php?module=".$mod."&amp;what=admins_contct&amp;admin=".bigintval($email);
159         }
160
161         // Return rewritten (?) email address
162         return $email;
163 }
164
165 // Change a lot admin account
166 function ADMINS_CHANGE_ADMIN_ACCOUNT($POST) {
167         // Begin the update
168         $cache_update = 0;
169         foreach ($POST['login'] as $id => $login) {
170                 // Secure ID number
171                 $id = bigintval($id);
172
173                 // When both passwords match update admin account
174                 if ($POST['pass1'][$id] == $POST['pass2'][$id]) {
175                         // Save only when both passwords are the same (also when they are empty)
176                         $ADD = ""; $cache_update = "1";
177
178                         // Generate hash
179                         $hash = generateHash($POST['pass1'][$id]);
180
181                         // Save password when set
182                         if (!empty($POST['pass1'][$id])) $ADD = sprintf(", password='%s'", SQL_ESCAPE($hash));
183
184                         // Get admin's ID
185                         $aid = GET_CURRENT_ADMIN_ID();
186                         $salt = substr(GET_ADMIN_HASH($aid), 0, -40);
187
188                         // Rewrite cookie when it's own account
189                         if ($aid == $id) {
190                                 // Set timeout cookie
191                                 set_session('admin_last', time());
192
193                                 if ($login != get_session('admin_login')) {
194                                         // Update login cookie
195                                         set_session('admin_login', $login);
196
197                                         // Update password cookie as well?
198                                         if (!empty($ADD)) set_session('admin_md5', $hash);
199                                 } elseif (generateHash($POST['pass1'][$id], $salt) != get_session('admin_md5')) {
200                                         // Update password cookie
201                                         set_session('admin_md5', $hash);
202                                 }
203                         } // END - if
204
205                         // Get default ACL from admin to check if we can allow him to change the default ACL
206                         $default = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
207
208                         // Update admin account
209                         if ($default == "allow") {
210                                 // Allow changing default ACL
211                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
212 login='%s'".$ADD.",
213 email='%s',
214 default_acl='%s',
215 la_mode='%s'
216 WHERE id=%s LIMIT 1",
217                                         array(
218                                                 $login,
219                                                 $POST['email'][$id],
220                                                 $POST['mode'][$id],
221                                                 $POST['la_mode'][$id],
222                                                 $id
223                                         ), __FUNCTION__, __LINE__);
224                         } else {
225                                 // Do not allow it here
226                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins` SET
227 login='%s'".$ADD.",
228 email='%s',
229 la_mode='%s'
230 WHERE id=%s LIMIT 1",
231                                         array(
232                                                 $login,
233                                                 $POST['email'][$id],
234                                                 $POST['la_mode'][$id],
235                                                 $id
236                                         ), __FUNCTION__, __LINE__);
237                         }
238
239                         // Purge cache
240                         CACHE_PURGE_ADMIN_MENU($id);
241
242                         // Admin account saved
243                         $MSG = ADMIN_ACCOUNT_SAVED;
244                 } else {
245                         // Passwords did not match
246                         $MSG = ADMINS_ERROR_PASS_MISMATCH;
247                 }
248
249                 // Display message
250                 if (!empty($MSG)) {
251                         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
252                 }
253         }
254
255         // Remove cache file
256         runFilterChain('post_admin_edited', REQUEST_POST_ARRAY());
257 }
258
259 // Make admin accounts editable
260 function ADMINS_EDIT_ADMIN_ACCOUNTS ($POST) {
261         // "Resolve" current's admin access mode
262         $currMode = GET_ADMIN_DEFAULT_ACL(GET_CURRENT_ADMIN_ID());
263
264         // Begin the edit loop
265         $OUT = ""; $SW = 2;
266         foreach ($POST['sel'] as $id => $selected) {
267                 // Secure ID number
268                 $id = bigintval($id);
269
270                 // Get the admin's data
271                 $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
272                         array($id), __FUNCTION__, __LINE__);
273                 if ((SQL_NUMROWS($result) == 1) && ($selected == 1)) {
274                         // Entry found
275                         $content = SQL_FETCHARRAY($result);
276                         SQL_FREERESULT($result);
277
278                         // Prepare some more data for the template
279                         $content['sw'] = $SW;
280                         $content['id'] = $id;
281
282                         // Shall we allow changing default ACL?
283                         if ($currMode == "allow") {
284                                 // Allow chaning it
285                                 $content['mode']    = ADD_OPTION_LINES("/ARRAY/", array("allow", "deny"), array(constant('ADMINS_ALLOW_MODE'), constant('ADMINS_DENY_MODE')), $content['mode']);
286                         } else {
287                                 // Don't allow it
288                                 $content['mode'] = "&nbsp;";
289                         }
290                         $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']);
291
292                         // Load row template and switch color
293                         $OUT .= LOAD_TEMPLATE("admin_edit_admins_row", true, $content);
294                         $SW = 3 - $SW;
295                 }
296         }
297         define('__ADMINS_ROWS', $OUT);
298
299         // Load template
300         LOAD_TEMPLATE("admin_edit_admins");
301 }
302
303 // Delete given admin accounts
304 function ADMINS_DELETE_ADMIN_ACCOUNTS ($POST) {
305         // Check if this account is the last one which cannot be deleted...
306         $result_main = SQL_QUERY("SELECT id FROM `{!_MYSQL_PREFIX!}_admins`", __FUNCTION__, __LINE__);
307         $accounts = SQL_NUMROWS($result_main);
308         SQL_FREERESULT($result_main);
309         if ($accounts > 1) {
310                 // Delete accounts
311                 $OUT = ""; $SW = 2;
312                 foreach ($POST['sel'] as $id => $selected) {
313                         // Secure ID number
314                         $id = bigintval($id);
315
316                         // Get the admin's data
317                         $result = SQL_QUERY_ESC("SELECT login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
318                                 array($id), __FUNCTION__, __LINE__);
319                         if (SQL_NUMROWS($result) == 1) {
320                                 // Entry found
321                                 $content = SQL_FETCHARRAY($result);
322                                 SQL_FREERESULT($result);
323                                 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
324                                 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
325
326                                 // Prepare some more data
327                                 $content['sw'] = $SW;
328                                 $content['id'] = $id;
329
330                                 // Load row template and switch color
331                                 $OUT .= LOAD_TEMPLATE("admin_del_admins_row", true, $content);
332                                 $SW = 3 - $SW;
333                         }
334                 }
335                 define('__ADMINS_ROWS', $OUT);
336
337                 // Load template
338                 LOAD_TEMPLATE("admin_del_admins");
339         } else {
340                 // Cannot delete last account!
341                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_ADMINS_CANNOT_DELETE_LAST'));
342         }
343 }
344
345 // Remove the given accounts
346 function ADMINS_REMOVE_ADMIN_ACCOUNTS ($POST) {
347         // Begin removal
348         $cache_update = 0;
349         foreach ($POST['sel'] as $id => $del) {
350                 // Secure ID number
351                 $id = bigintval($id);
352
353                 // Delete only when it's not your own account!
354                 if (($del == 1) && (GET_CURRENT_ADMIN_ID() != $id)) {
355                         // Rewrite his tasks to all admins
356                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin=0 WHERE assigned_admin=%s",
357                          array($id), __FUNCTION__, __LINE__);
358
359                         // Remove account
360                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admins` WHERE id=%s LIMIT 1",
361                                 array($id), __FUNCTION__, __LINE__);
362
363                         // Purge cache
364                         CACHE_PURGE_ADMIN_MENU($id);
365                         $cache_update = "1";
366                 }
367         }
368
369         // Remove cache if cache system is activated
370         runFilterChain('post_admin_deleted', REQUEST_POST_ARRAY());
371 }
372
373 // List all admin accounts
374 function ADMINS_LIST_ADMIN_ACCOUNTS() {
375         // Select all admin accounts
376         $result = SQL_QUERY("SELECT id, login, email, default_acl AS mode, la_mode FROM `{!_MYSQL_PREFIX!}_admins` ORDER BY login ASC", __FUNCTION__, __LINE__);
377         $OUT = ""; $SW = 2;
378         while ($content = SQL_FETCHARRAY($result)) {
379                 // Compile some variables
380                 $content['mode'] = constant('ADMINS_'.strtoupper($content['mode']).'_MODE');
381                 $content['la_mode'] = constant('ADMINS_'.strtoupper($content['la_mode']).'_LA_SETTING');
382
383                 // Prepare some more data
384                 $content['sw']         = $SW;
385                 $content['email_link'] = CREATE_EMAIL_LINK($content['id']);
386
387                 // Load row template and switch color
388                 $OUT .= LOAD_TEMPLATE("admin_list_admins_row", true, $content);
389                 $SW = 3 - $SW;
390         }
391
392         // Free memory
393         SQL_FREERESULT($result);
394         define('__ADMINS_ROWS', $OUT);
395
396         // Load template
397         LOAD_TEMPLATE("admin_list_admins");
398 }
399
400 // Filter for adding extra data to the query
401 function FILTER_ADD_EXTRA_SQL_DATA ($ADD = "") {
402         // Is the admins extension updated? (should be!)
403         if (GET_EXT_VERSION("admins") >= "0.3")   $ADD .= ", default_acl AS def_acl";
404         if (GET_EXT_VERSION("admins") >= "0.6.7") $ADD .= ", la_mode";
405         if (GET_EXT_VERSION("admins") >= "0.7.2") $ADD .= ", login_failures, UNIX_TIMESTAMP(last_failure) AS last_failure";
406
407         // Return it
408         return $ADD;
409 }
410
411 //
412 ?>