Began to rewrite whole script for newly added filters, new extension stub 'network...
[mailer.git] / inc / modules / admin.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 08/31/2003 *
4  * ===============                              Last change: 07/02/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : admin.php                                        *
8  * -------------------------------------------------------------------- *
9  * Short description : Administration module                            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Administrationsmodul                             *
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 // Login is default
41 if ((empty($GLOBALS['action'])) && ($check == "admin_only")) {
42         // Redirect to right URL
43         LOAD_URL("modules.php?module=admin&amp;action=login");
44 } // END - if
45
46 // Load include file
47 require_once(PATH."inc/modules/admin/admin-inc.php");
48
49 // Fix "deleted" cookies in PHP4 (PHP5 does remove them, PHP4 sets them to deleted!)
50 FIX_DELETED_COOKIES(array('admin_login', 'admin_md5', 'admin_last', 'admin_to'));
51
52 if (!isBooleanConstantAndTrue('admin_registered')) {
53         // Admin is not registered so we have to inform the user
54         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
55         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
56                 // Hash the password with the old function because we are here in install mode
57                 $hashedPass = md5($_POST['pass']);
58
59                 // Kill maybe existing session variables
60                 DESTROY_ADMIN_SESSION(false);
61
62                 // Do registration
63                 $ret = REGISTER_ADMIN($_POST['login'], $hashedPass);
64                 switch ($ret)
65                 {
66                 case "done":
67                         admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
68                         if (!_FATAL) {
69                                 // Registering is done
70                                 LOAD_URL(URL."/modules.php?module=admin&amp;action=login&register=done");
71                         } else {
72                                 $ret = ADMIN_CANNOT_COMPLETE;
73                         }
74                         break;
75
76                 case "failed":
77                         $ret = ADMIN_REGISTER_FAILED;
78                         break;
79
80                 case "already":
81                 default:
82                         if ($ret == "already") {
83                                 // Admin does already exists!
84                                 $ret = ADMIN_LOGIN_ALREADY_REG;
85                         } else {
86                                 // Any other kind
87                                 $ret = "done";
88                         }
89
90                         // Admin still not registered?
91                         if (!isBooleanConstantAndTrue('admin_registered')) {
92                                 // Write to config that registration is done
93                                 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
94
95                                 // Load URL for login
96                                 LOAD_URL(URL."/modules.php?module=admin&amp;action=login");
97                         } // END - if
98                         break;
99                 }
100         }
101
102         // Whas that action okay?
103         if ($ret != "done") {
104                 // Fixes another "Notice"
105                 if (!empty($_POST['login'])) {
106                         define('__LOGIN_VALUE', $_POST['login']);
107                 } else {
108                         define('__LOGIN_VALUE', "");
109                 }
110
111                 // Yet-another "Notice" fix
112                 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
113                         // No login entered?
114                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
115
116                         // An error comes back from registration?
117                         if (!empty($ret)) $MSG1 = $ret;
118
119                         // No password entered?
120                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
121
122                         // Or password too short?
123                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
124
125                         // Output error messages
126                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
127                         define('__MSG_PASS',  LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
128
129                         // Reset variables
130                         $MSG1 = ""; $MSG2 = "";
131                 } else {
132                         // Reset values to nothing
133                         define('__MSG_LOGIN', "");
134                         define('__MSG_PASS' , "");
135                 }
136
137                 // Load register template
138                 LOAD_TEMPLATE("admin_reg_form");
139         }
140 } elseif (isset($_GET['reset_pass'])) {
141         // Is the form submitted?
142         if ((isset($_POST['send_link'])) && (!empty($_POST['email']))) {
143                 // Try to send the link out
144                 $OUT = ADMIN_SEND_PASSWORD_RESET_LINK($_POST['email']);
145
146                 // Output result
147                 LOAD_TEMPLATE("admin_settings_saved", false, $OUT);
148         } elseif (!empty($_GET['hash'])) {
149                 // Output form for hash validation
150                 LOAD_TEMPLATE("admin_validate_reset_hash_form", false, $_GET['hash']);
151         } elseif ((isset($_POST['validate_hash'])) && (!empty($_POST['login'])) && (!empty($_POST['hash']))) {
152                 // Validate the login data and hash
153                 $valid = ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login']);
154
155                 // Valid?
156                 if ($valid) {
157                         // Prepare content first
158                         $content = array(
159                                 'hash'  => SQL_ESCAPE($_POST['hash']),
160                                 'login' => SQL_ESCAPE($_POST['login'])
161                         );
162
163                         // Validation okay so display form for final password change
164                         LOAD_TEMPLATE("admin_reset_password_form", false, $content);
165                 } else {
166                         // Cannot validate the login data and hash
167                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED);
168                 }
169         } elseif ((isset($_POST['reset_pass'])) && (!empty($_POST['hash'])) && (!empty($_POST['login'])) && (!empty($_POST['pass1'])) && ($_POST['pass1'] == $_POST['pass2'])) {
170                 // Okay, we shall the admin password here. So first revalidate the hash
171                 if (ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN($_POST['hash'], $_POST['login'])) {
172                         // Set the password now
173                         $OUT = ADMIN_RESET_PASSWORD($_POST['login'], $_POST['pass1']);
174
175                         // Output result
176                         LOAD_TEMPLATE("admin_reset_pass_done", false, $OUT);
177                 } else {
178                         // Validation failed
179                         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_VALIDATION_RESET_LOGIN_HASH_FAILED2);
180                 }
181         } else {
182                 // Output reset password form
183                 LOAD_TEMPLATE("admin_send_reset_link");
184         }
185 } elseif ((!isSessionVariableSet('admin_login')) || (!isSessionVariableSet('admin_md5')) || (!isSessionVariableSet('admin_last')) || (!isSessionVariableSet('admin_to')) || ((get_session('admin_last') + bigintval(get_session('admin_to')) * 3600 * 24) < time())) {
186         // At leat one administrator account was created
187         if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
188                 // Timeout for last login, we have to logout first!
189                 LOAD_URL(URL."/modules.php?module=admin&amp;action=login&amp;logout=1");
190         } // END - if
191
192         if (!empty($_GET['register'])) {
193                 // Registration of first admin is done
194                 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
195         } // END - if
196
197         // Check if the admin has submitted data or not
198         $ret = "";
199         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
200         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
201                 // All required data was entered so we check his account
202                 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
203
204                 // Which status do we have?
205                 switch ($ret)
206                 {
207                 case "done": // Admin and password are okay, so we log in now
208                         // Construct URL and redirect
209                         $URL = URL."/modules.php?module=admin&amp;";
210
211                         // Rewrite overview module
212                         if ($GLOBALS['what'] == "overview") {
213                                 $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
214                         } // END - if
215
216                         // Add data to URL
217                         if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
218                          elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
219                          elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
220
221                         // Load URL
222                         LOAD_URL($URL);
223                         break;
224
225                 case "404": // Administrator login not found
226                         $_POST['ok'] = $ret;
227                         $ret = ADMIN_NOT_FOUND;
228                         DESTROY_ADMIN_SESSION();
229                         break;
230
231                 case "pass": // Wrong password
232                         $_POST['ok'] = $ret;
233                         $ret = WRONG_PASS." [<A href=\"".URL."/modules.php?module=admin&amp;reset_pass=1\">".ADMIN_RESET_PASS."</A>]\n";
234                         DESTROY_ADMIN_SESSION();
235                         break;
236                 } // END - switch
237         } // END - if
238
239         // Error detected?
240         if ($ret != "done") {
241                 if (!empty($_POST['login'])) {
242                         define('__LOGIN_VALUE', $_POST['login']);
243                 } else {
244                         define('__LOGIN_VALUE', "");
245                 }
246
247                 if (isset($_POST['ok'])) {
248                         // Set messages to zero
249                         $MSG1 = ""; $MSG2 = "";
250
251                         // No login entered?
252                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
253
254                         // An error comes back from login?
255                         if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
256
257                         // No password entered?
258                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
259
260                         // Or password too short?
261                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
262
263                         // An error comes back from login?
264                         if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
265
266                         // Load message template
267                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
268                         define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
269
270                         // Reset variables
271                         $MSG1 = ""; $MSG2 = "";
272                 } else {
273                         // Set constants to empty for hiding them
274                         define('__MSG_LOGIN', "");
275                         define('__MSG_PASS' , "");
276                 }
277
278                 // Load login form
279                 if (!empty($GLOBALS['what'])) {
280                         // Restore old what value
281                         $content = array('target' => "what", 'value' => $GLOBALS['what']);
282                 } elseif (!empty($GLOBALS['action'])) {
283                         if ($GLOBALS['action'] != "logout") {
284                                 // Restore old action value
285                                 $content = array('target' => "action", 'value' => $GLOBALS['action']);
286                         } else {
287                                 // Set default values
288                                 $content = array('target' => "action", 'value' => "login");
289                         }
290                 } elseif (!empty($_GET['area'])) {
291                         // Restore old area value
292                         $content = array('target' => "area", 'value' => $_GET['area']);
293                 } else {
294                         // Set default values
295                         $content = array('target' => "action", 'value' => "login");
296                 }
297
298                 // Load login form template
299                 LOAD_TEMPLATE("admin_login_form", false, $content);
300         } // END - if
301 } elseif (isset($_GET['logout'])) {
302         // Only try to remove cookies
303         if (DESTROY_ADMIN_SESSION()) {
304                 // Load logout template
305                 if (isset($_GET['register'])) {
306                         // Secure input
307                         $register = SQL_ESCAPE($_GET['register']);
308
309                         // Special logout redirect for installation of given extension
310                         LOAD_TEMPLATE(sprintf("admin_logout_%s_install", $register));
311                 } elseif (isset($_GET['remove'])) {
312                         // Secure input
313                         $remove = SQL_ESCAPE($_GET['remove']);
314
315                         // Special logout redirect for removal of given extension
316                         LOAD_TEMPLATE(sprintf("admin_logout_%s_remove", $remove));
317                 } else {
318                         // Logged out normally
319                         LOAD_TEMPLATE("admin_logout");
320                 }
321         } else {
322                 // Something went wrong here...
323                 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
324
325                 // Add fatal message
326                 ADD_FATAL(CANNOT_UNREG_SESS);
327         }
328 } else {
329         // Maybe an Admin want's to login?
330         $ret = CHECK_ADMIN_COOKIES(get_session('admin_login'), get_session('admin_md5'));
331         switch ($ret)
332         {
333         case "done":
334                 // Cookie-Data accepted
335                 if ((set_session("admin_md5", get_session('admin_md5'))) && (set_session("admin_login", get_session('admin_login'))) && (set_session("admin_last", time())) && (set_session("admin_to", bigintval(get_session('admin_to'))))) {
336                         // Ok, Cookie-Update done
337                         if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
338                                 // Check if action GET variable was set
339                                 $act = SQL_ESCAPE($GLOBALS['action']);
340                                 if (!empty($GLOBALS['what'])) {
341                                         // Get action value by what-value
342                                         $act = GET_ACTION("admin", $GLOBALS['what']);
343                                 }
344
345                                 // Check for access control line of current menu entry
346                                 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
347                         } else {
348                                 // Extension not installed so it's always allowed to access everywhere!
349                                 define('__ACL_ALLOW', true);
350                         }
351
352                         // When type of admin menu is not set fallback to old menu system
353                         if (getConfig('admin_menu') == null) $_CONFIG['admin_menu'] = "OLD";
354
355                         // Check for version and switch between old menu system and new "intelligent menu system"
356                         if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (FILE_READABLE(PATH."inc/modules/admin/lasys-inc.php"))) {
357                                 // Default area is the entrance, of course
358                                 $area = "entrance";
359
360                                 // Check for similar URL variable
361                                 if (!empty($_GET['area'])) $area = SQL_ESCAPE($_GET['area']);
362
363                                 // Load "logical-area menu-system" file
364                                 require_once(PATH."inc/modules/admin/lasys-inc.php");
365
366                                 // Create new-style menu system will "logical areas"
367                                 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
368                         } else {
369                                 // This little call constructs the whole default old and lacky menu system
370                                 // on left side
371                                 ADMIN_DO_ACTION($GLOBALS['what']);
372                         }
373                 } else {
374                         // Login failed (cookies enabled?)
375                         OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
376                         ADD_FATAL(CANNOT_RE_REGISTER_SESS);
377                 }
378                 break;
379
380         case "404": // Administrator login not found
381                 $_POST['ok'] = $ret;
382                 DESTROY_ADMIN_SESSION();
383                 ADD_FATAL(ADMIN_NOT_FOUND);
384                 break;
385
386         case "pass": // Wrong password
387                 $_POST['ok'] = $ret;
388                 DESTROY_ADMIN_SESSION();
389                 ADD_FATAL(WRONG_PASS);
390                 break;
391         }
392 }
393
394 //
395 ?>