c0e1fdf48a4c581180ac6f9475013f3f86d9121d
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
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 }
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 // Is the logout empty?
53 if (empty($_GET['logout'])) $_GET['logout'] = "";
54
55 if (!isBooleanConstantAndTrue('admin_registered')) {
56         // Admin is not registered so we have to inform the user
57         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
58         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
59                 // Hash the password with the old function because we are here in install mode
60                 $hashedPass = md5($hashedPass);
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                         if (!isBooleanConstantAndTrue('admin_registered')) {
90                                 // Write to config that registration is done
91                                 admin_WriteData(PATH."inc/config.php", "ADMIN-SETUP", "define('admin_registered', ", ");", "true", 0);
92
93                                 // Load URL for login
94                                 $URL = URL."/modules.php?module=admin&amp;action=login";
95                                 LOAD_URL($URL);
96                         }
97                         break;
98                 }
99         }
100         if ($ret != "done") {
101                 // Fixes another "Notice"
102                 if (!empty($_POST['login'])) {
103                         define('__LOGIN_VALUE', $_POST['login']);
104                 } else {
105                         define('__LOGIN_VALUE', "");
106                 }
107
108                 // Yet-another "Notice" fix
109                 if ((!empty($_POST['ok'])) && ($_POST['ok'] == "***")) {
110                         // No login entered?
111                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
112
113                         // An error comes back from registration?
114                         if (!empty($ret)) $MSG1 = $ret;
115
116                         // No password entered?
117                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
118
119                         // Or password too short?
120                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
121
122                         // Output error messages
123                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
124                         define('__MSG_PASS',  LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
125
126                         // Reset variables
127                         $MSG1 = ""; $MSG2 = "";
128                 } else {
129                         // Reset values to nothing
130                         define('__MSG_LOGIN', "");
131                         define('__MSG_PASS' , "");
132                 }
133
134                 // Load register template
135                 LOAD_TEMPLATE("admin_reg_form");
136         }
137 } 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())) {
138         // At leat one administrator account was created
139         if ((isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5')) && (isSessionVariableSet('admin_last')) && (isSessionVariableSet('admin_to'))) {
140                 // Timeout for last login, we have to logout first!
141                 $URL = URL."/modules.php?module=admin&amp;action=login&amp;logout=1";
142                 LOAD_URL($URL);
143         }
144         if (!empty($_GET['register'])) {
145                 // Registration of first admin is done
146                 if ($_GET['register'] == "done") OUTPUT_HTML("<STRONG class=\"admin\">".ADMIN_REGISTER_DONE."</STRONG>");
147         }
148
149         // Check if the admin has submitted data or not
150         $ret = "";
151         if ((isset($_POST['ok'])) && ((empty($_POST['login'])) || (empty($_POST['pass'])) || (strlen($_POST['pass']) < 4))) $_POST['ok'] = "***";
152         if ((isset($_POST['ok'])) && ($_POST['ok'] != "***")) {
153                 // All required data was entered so we check his account
154                 $ret = CHECK_ADMIN_LOGIN($_POST['login'], $_POST['pass']);
155                 switch ($ret)
156                 {
157                 case "done": // Admin and password are okay, so we log in now
158                         // Try to register the session variables
159                         if ((set_session("admin_md5", generatePassString(generateHash($_POST['pass'], __SALT)))) && (set_session("admin_login", $_POST['login'])) && (set_session("admin_last", time())) && (set_session("admin_to", $_POST['timeout']))) {
160                                 // Construct URL and redirect
161                                 $URL = URL."/modules.php?module=admin&amp;";
162
163                                 // Rewrite overview module
164                                 if ($GLOBALS['what'] == "overview") {
165                                         $GLOBALS['action'] = GET_ACTION($GLOBALS['module'], $GLOBALS['what']);
166                                 }
167
168                                 // Add data to URL
169                                 if (!empty($GLOBALS['what'])) $URL .= "what=".$GLOBALS['what'];
170                                  elseif (!empty($GLOBALS['action'])) $URL .= "action=".$GLOBALS['action'];
171                                  elseif (!empty($_GET['area'])) $URL .= "area=".$_GET['area'];
172
173                                 // Load URL
174                                 LOAD_URL($URL);
175                         } else {
176                                 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
177                                 ADD_FATAL(CANNOT_REGISTER_SESS);
178                         }
179                         break;
180
181                 case "404": // Administrator login not found
182                         $_POST['ok'] = $ret;
183                         $ret = ADMIN_NOT_FOUND;
184                         break;
185
186                 case "pass": // Wrong password
187                         $_POST['ok'] = $ret;
188                         $ret = WRONG_PASS;
189                         break;
190                 }
191         }
192         if ($ret != "done") {
193                 if (!empty($_POST['login'])) {
194                         define('__LOGIN_VALUE', $_POST['login']);
195                 } else {
196                         define('__LOGIN_VALUE', "");
197                 }
198
199                 if (isset($_POST['ok'])) {
200                         // Set messages to zero
201
202                         $MSG1 = ""; $MSG2 = "";
203                         // No login entered?
204                         if (empty($_POST['login'])) $MSG1 = ADMIN_NO_LOGIN;
205
206                         // An error comes back from login?
207                         if ((!empty($ret)) && ($_POST['ok'] == "404")) $MSG1 = $ret;
208
209                         // No password entered?
210                         if (empty($_POST['pass'])) $MSG2 = ADMIN_NO_PASS;
211
212                         // Or password too short?
213                         if (strlen($_POST['pass']) < 4) $MSG2 = ADMIN_SHORT_PASS;
214
215                         // An error comes back from login?
216                         if ((!empty($ret)) && ($_POST['ok'] == "pass")) $MSG2 = $ret;
217
218                         // Load message template
219                         define('__MSG_LOGIN', LOAD_TEMPLATE("admin_login_msg", true, $MSG1));
220                         define('__MSG_PASS' , LOAD_TEMPLATE("admin_login_msg", true, $MSG2));
221
222                         // Reset variables
223                         $MSG1 = ""; $MSG2 = "";
224                 } else {
225                         // Set constants to empty for hiding them
226                         define('__MSG_LOGIN', "");
227                         define('__MSG_PASS' , "");
228                 }
229
230                 // Load login form
231                 if (!empty($GLOBALS['what'])) {
232                         // Restore old what value
233                         $content = array('target' => "what", 'value' => $GLOBALS['what']);
234                 } elseif (!empty($GLOBALS['action'])) {
235                         if ($GLOBALS['action'] != "logout") {
236                                 // Restore old action value
237                                 $content = array('target' => "action", 'value' => $GLOBALS['action']);
238                         } else {
239                                 // Set default values
240                                 $content = array('target' => "action", 'value' => "login");
241                         }
242                 } elseif (!empty($_GET['area'])) {
243                         // Restore old area value
244                         $content = array('target' => "area", 'value' => $_GET['area']);
245                 } else {
246                         // Set default values
247                         $content = array('target' => "action", 'value' => "login");
248                 }
249
250                 // Load login form template
251                 LOAD_TEMPLATE("admin_login_form", false, $content);
252         }
253 } elseif ($_GET['logout'] == "1") {
254         // Only try to remove cookies
255         if (set_session("admin_login", "") && set_session("admin_md5", "") && set_session("admin_last", "") && set_session("admin_to", "")) {
256                 // Also remove array elements
257                 set_session('admin_login'       , "");
258                 set_session('admin_md5'         , "");
259                 set_session('admin_last'        , "");
260                 set_session('admin_to'          , "");
261
262                 // Destroy session
263                 @session_destroy();
264
265                 // Load logout template
266                 LOAD_TEMPLATE("admin_logout");
267         } else {
268                 // Something went wrong here...
269                 OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGOUT_FAILED."</STRONG>");
270
271                 // Add fatal message
272                 ADD_FATAL(CANNOT_UNREG_SESS);
273         }
274 } else {
275         // Maybe an Admin want's to login?
276         $ret = CHECK_ADMIN_COOKIES(SQL_ESCAPE(get_session('admin_login')), SQL_ESCAPE(get_session('admin_md5')));
277         switch ($ret) {
278         case "done":
279                 // Cookie-Data accepted
280                 if ((set_session("admin_md5", SQL_ESCAPE(get_session('admin_md5')))) && (set_session("admin_login", SQL_ESCAPE(get_session('admin_login')))) && (set_session("admin_last", time())) && (set_session("admin_to", bigintval(get_session('admin_to'))))) {
281                         // Ok, Cookie-Update done
282                         if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
283                                 // Check if action GET variable was set
284                                 $act = SQL_ESCAPE($GLOBALS['action']);
285                                 if (!empty($GLOBALS['what'])) {
286                                         // Get action value by what-value
287                                         $act = GET_ACTION("admin", $GLOBALS['what']);
288                                 }
289
290                                 // Check for access control line of current menu entry
291                                 define('__ACL_ALLOW', ADMINS_CHECK_ACL($act, $GLOBALS['what']));
292                         } else {
293                                 // Extension not installed so it's always allowed to access everywhere!
294                                 define('__ACL_ALLOW', true);
295                         }
296
297                         // When type of admin menu is not set fallback to old menu system
298                         if (empty($_CONFIG['admin_menu'])) $_CONFIG['admin_menu'] = "OLD";
299
300                         // Check for version and switch between old menu system and new "intelligent menu system"
301                         if ((ADMIN_CHECK_MENU_MODE() == "NEW") && (file_exists(PATH."inc/modules/admin/la_sys-inc.php"))) {
302                                 // Default area is the entrance, of course
303                                 $area = "entrance";
304
305                                 // Check for similar URL variable
306                                 if (!empty($_GET['area'])) $area = $_GET['area'];
307
308                                 // Load "logical-area menu-system" file
309                                 require_once(PATH."inc/modules/admin/la_sys-inc.php");
310
311                                 // Create new-style menu system will "logical areas"
312                                 ADMIN_LOGICAL_AREA_SYSTEM($area, $act, $GLOBALS['what']);
313                         } else {
314                                 // This little call constructs the whole default old and lacky menu system
315                                 // on left side
316                                 ADMIN_DO_ACTION($GLOBALS['what']);
317                         }
318                 } else {
319                         // Login failed (cookies enabled?)
320                         OUTPUT_HTML("<STRONG class=\"admin_fatal\">".ADMIN_LOGIN_FAILED."</STRONG>");
321                         ADD_FATAL(CANNOT_RE_REGISTER_SESS);
322                 }
323                 break;
324
325         case "404": // Administrator login not found
326                 $_POST['ok'] = $ret;
327                 ADD_FATAL(ADMIN_NOT_FOUND);
328                 break;
329
330         case "pass": // Wrong password
331                 $_POST['ok'] = $ret;
332                 ADD_FATAL(WRONG_PASS);
333                 break;
334         }
335 }
336
337 if (isBooleanConstantAndTrue('admin_registered'))
338 {
339         // Check config.php and inc directory for right access rights
340         if (is_INCWritable("config"))     ADD_FATAL(FATAL_CONFIG_WRITABLE);
341         if (is_INCWritable("dummy"))      ADD_FATAL(FATAL_INC_WRITABLE);
342 }
343 //
344 ?>