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