the register globals check was deleting $base_uri from the global scope.
[squirrelmail.git] / include / init.php
1 <?php
2
3 /**
4 * init.php -- initialisation file
5 *
6 * File should be loaded in every file in src/ or plugins that occupate an entire frame
7 *
8 * @copyright &copy; 2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 */
13
14 /**
15 * SquirrelMail version number -- DO NOT CHANGE
16 */
17
18 $version = '1.5.2 [CVS]';
19
20 /**
21 * SquirrelMail internal version number -- DO NOT CHANGE
22 * $sm_internal_version = array (release, major, minor)
23 */
24
25 $SQM_INTERNAL_VERSION = array(1,5,2);
26
27 /**
28 * This is a development version so in order to track programmer mistakes we
29 * set the error reporting to E_ALL
30 */
31
32 error_reporting(E_ALL);
33
34
35 /**
36 * If register_globals are on, unregister globals.
37 * Code requires PHP 4.1.0 or newer.
38 */
39 if ((bool) @ini_get('register_globals')) {
40 /**
41 * Remove all globals from $_GET, $_POST, and $_COOKIE.
42 */
43 foreach ($_REQUEST as $key => $value) {
44 unset($GLOBALS[$key]);
45 }
46 /**
47 * Remove globalized $_FILES variables
48 * Before 4.3.0 $_FILES are included in $_REQUEST.
49 * Unglobalize them in separate call in order to remove dependency
50 * on PHP version.
51 */
52 foreach ($_FILES as $key => $value) {
53 unset($GLOBALS[$key]);
54 // there are three undocumented $_FILES globals.
55 unset($GLOBALS[$key.'_type']);
56 unset($GLOBALS[$key.'_name']);
57 unset($GLOBALS[$key.'_size']);
58 }
59 /**
60 * Remove globalized environment variables.
61 */
62 foreach ($_ENV as $key => $value) {
63 unset($GLOBALS[$key]);
64 }
65 /**
66 * Remove globalized server variables.
67 */
68 foreach ($_SERVER as $key => $value) {
69 unset($GLOBALS[$key]);
70 }
71 }
72
73
74 /**
75 * calculate SM_PATH and calculate the base_uri
76 * assumptions made: init.php is only called from plugins or from the src dir.
77 * files in the plugin directory may not be part of a subdirectory called "src"
78 *
79 */
80 if (isset($_SERVER['SCRIPT_NAME'])) {
81 $a = explode('/',$_SERVER['SCRIPT_NAME']);
82 } elseif (isset($HTTP_SERVER_VARS['SCRIPT_NAME'])) {
83 $a = explode('/',$_SERVER['SCRIPT_NAME']);
84 }
85 $sSM_PATH = '';
86 for($i = count($a) -2;$i > -1; --$i) {
87 $sSM_PATH .= '../';
88 if ($a[$i] === 'src' || $a[$i] === 'plugins') {
89 break;
90 }
91 }
92
93 $base_uri = implode('/',array_slice($a,0,$i)). '/';
94
95 define('SM_PATH',$sSM_PATH);
96 define('SM_BASE_URI', $base_uri);
97 /**
98 * global var $bInit is used to check if initialisation took place.
99 * At this moment it's a workarounf for the include of addrbook_search_html
100 * inside compose.php. If we found a better way then remove this. Do only use
101 * this var if you know for sure a page can be called stand alone and be included
102 * in another file.
103 */
104 $bInit = true;
105
106 require(SM_PATH . 'functions/global.php');
107 require(SM_PATH . 'config/config.php');
108 require(SM_PATH . 'functions/plugin.php');
109 require(SM_PATH . 'include/constants.php');
110 require(SM_PATH . 'include/languages.php');
111
112 /**
113 * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
114 * Force magic_quotes_runtime off.
115 * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
116 * If there's a better place, please let me know.
117 */
118 ini_set('magic_quotes_runtime','0');
119
120
121 /* if running with magic_quotes_gpc then strip the slashes
122 from POST and GET global arrays */
123 if (get_magic_quotes_gpc()) {
124 sqstripslashes($_GET);
125 sqstripslashes($_POST);
126 }
127
128
129 /* strip any tags added to the url from PHP_SELF.
130 This fixes hand crafted url XXS expoits for any
131 page that uses PHP_SELF as the FORM action */
132 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
133
134 $PHP_SELF = php_self();
135
136 /**
137 * Initialize the session
138 */
139
140 /**
141 * includes from classes stored in the session
142 */
143 require(SM_PATH . 'class/mime.class.php');
144
145 /** set the name of the session cookie */
146 if (!isset($session_name) || !$session_name) {
147 $session_name = 'SQMSESSID';
148 }
149 ini_set('session.name' , $session_name);
150 session_set_cookie_params (0, $base_uri);
151 sqsession_is_active();
152
153 /**
154 * Remove globalized session data in rg=on setups
155 */
156 if ((bool) @ini_get('register_globals')) {
157 foreach ($_SESSION as $key => $value) {
158 unset($GLOBALS[$key]);
159 }
160 }
161
162 sqsession_register($base_uri, SM_BASE_URI);
163
164 /**
165 * Retrieve the language cookie
166 */
167 if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) {
168 $squirrelmail_language = '';
169 }
170
171
172 /**
173 * @var $sInitlocation From where do we include.
174 */
175 if (!isset($sInitLocation)) {
176 $sInitLocation=NULL;
177 }
178
179 /**
180 * MAIN PLUGIN LOADING CODE HERE
181 */
182
183 /**
184 * Include Compatibility plugin if available.
185 */
186 if (file_exists(SM_PATH . 'plugins/compatibility/functions.php'))
187 include_once(SM_PATH . 'plugins/compatibility/functions.php');
188 $squirrelmail_plugin_hooks = array();
189
190 /* On init, register all plugins configured for use. */
191 if (isset($plugins) && is_array($plugins)) {
192 // turn on output buffering in order to prevent output of new lines
193 ob_start();
194 foreach ($plugins as $name) {
195 use_plugin($name);
196 }
197 // get output and remove whitespace
198 $output = trim(ob_get_contents());
199 ob_end_clean();
200 // if plugins output more than newlines and spacing, stop script execution.
201 if (!empty($output)) {
202 die($output);
203 }
204 }
205
206
207 switch ($sInitLocation) {
208 case 'style': session_write_close(); sqsetcookieflush(); break;
209 case 'redirect':
210 $prefs_backend = do_hook_function('prefs_backend');
211 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH . $prefs_backend)) {
212 require(SM_PATH . $prefs_backend);
213 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
214 require(SM_PATH . 'functions/db_prefs.php');
215 } else {
216 require(SM_PATH . 'functions/prefs.php');
217 require(SM_PATH . 'functions/file_prefs.php');
218 }
219 //nobreak;
220 case 'login':
221 require(SM_PATH . 'functions/display_messages.php' );
222 require(SM_PATH . 'functions/page_header.php');
223 require(SM_PATH . 'functions/html.php');
224 break;
225 default:
226 require(SM_PATH . 'functions/display_messages.php' );
227 require(SM_PATH . 'functions/page_header.php');
228 require(SM_PATH . 'functions/html.php');
229 require(SM_PATH . 'functions/strings.php');
230
231
232 /**
233 * Check if we are logged in
234 */
235 require(SM_PATH . 'functions/auth.php');
236
237 if ( !sqsession_is_registered('user_is_logged_in') ) {
238 // First we store some information in the new session to prevent
239 // information-loss.
240 //
241 $session_expired_post = $_POST;
242 $session_expired_location = $PHP_SELF;
243 if (!sqsession_is_registered('session_expired_post')) {
244 sqsession_register($session_expired_post,'session_expired_post');
245 }
246 if (!sqsession_is_registered('session_expired_location')) {
247 sqsession_register($session_expired_location,'session_expired_location');
248 }
249 // signout page will deal with users who aren't logged
250 // in on its own; don't show error here
251 //
252 if (strpos($PHP_SELF, 'signout.php') !== FALSE) {
253 return;
254 }
255
256 set_up_language($squirrelmail_language, true);
257 logout_error( _("You must be logged in to access this page.") );
258 exit;
259 }
260
261 sqgetGlobalVar('username',$username,SQ_SESSION);
262
263 /**
264 * Setting the prefs backend
265 */
266 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
267 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
268
269 if ( !sqsession_is_registered('prefs_are_cached') ||
270 !isset( $prefs_cache) ||
271 !is_array( $prefs_cache)) {
272 $prefs_are_cached = false;
273 $prefs_cache = false; //array();
274 }
275
276 $prefs_backend = do_hook_function('prefs_backend');
277 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH . $prefs_backend)) {
278 require(SM_PATH . $prefs_backend);
279 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
280 require(SM_PATH . 'functions/db_prefs.php');
281 } else {
282 require(SM_PATH . 'functions/prefs.php');
283 require(SM_PATH . 'functions/file_prefs.php');
284 }
285
286 /**
287 * initializing user settings
288 */
289 require(SM_PATH . 'include/load_prefs.php');
290
291
292 // i do not understand the frames language cookie story
293 /**
294 * We'll need this to later have a noframes version
295 *
296 * Check if the user has a language preference, but no cookie.
297 * Send him a cookie with his language preference, if there is
298 * such discrepancy.
299 */
300 $my_language = getPref($data_dir, $username, 'language');
301 if ($my_language != $squirrelmail_language) {
302 sqsetcookie('squirrelmail_language', $my_language, time()+2592000, $base_uri);
303 }
304 // /dont understand
305
306 /**
307 * Set up the language.
308 */
309 $err=set_up_language(getPref($data_dir, $username, 'language'));
310 /* this is the last cookie we set so flush it. */
311 sqsetcookieflush();
312
313 // Japanese translation used without mbstring support
314 if ($err==2) {
315 $sError =
316 "<p>You need to have PHP installed with the multibyte string function \n".
317 "enabled (using configure option --enable-mbstring).</p>\n".
318 "<p>System assumed that you accidently switched to Japanese translation \n".
319 "and reverted your language preference to English.</p>\n".
320 "<p>Please refresh this page in order to use webmail.</p>\n";
321 error_box($sError);
322 }
323
324 $timeZone = getPref($data_dir, $username, 'timezone');
325
326 /* Check to see if we are allowed to set the TZ environment variable.
327 * We are able to do this if ...
328 * safe_mode is disabled OR
329 * safe_mode_allowed_env_vars is empty (you are allowed to set any) OR
330 * safe_mode_allowed_env_vars contains TZ
331 */
332 $tzChangeAllowed = (!ini_get('safe_mode')) ||
333 !strcmp(ini_get('safe_mode_allowed_env_vars'),'') ||
334 preg_match('/^([\w_]+,)*TZ/', ini_get('safe_mode_allowed_env_vars'));
335
336 if ( $timeZone != SMPREF_NONE && ($timeZone != "")
337 && $tzChangeAllowed ) {
338
339 // get time zone key, if strict or custom strict timezones are used
340 if (isset($time_zone_type) &&
341 ($time_zone_type == 1 || $time_zone_type == 3)) {
342 /* load time zone functions */
343 require(SM_PATH . 'include/timezones.php');
344 $realTimeZone = sq_get_tz_key($timeZone);
345 } else {
346 $realTimeZone = $timeZone;
347 }
348
349 // set time zone
350 if ($realTimeZone) {
351 putenv("TZ=".$realTimeZone);
352 }
353 }
354 break;
355 }
356
357 /**
358 * Initialize the template object
359 */
360 require(SM_PATH . 'class/template/template.class.php');
361 /*
362 * $sTplDir is not initialized when a user is not logged in, so we will use
363 * the config file defaults here. If the neccesary variables are net set,
364 * force a default value.
365 */
366 $aTemplateSet = ( !isset($aTemplateSet) ? array() : $aTemplateSet );
367 $templateset_default = ( !isset($templateset_default) ? 0 : $templateset_default );
368
369 $sTplDir = ( !isset($aTemplateSet[$templateset_default]['PATH']) ?
370 SM_PATH . 'templates/default/' :
371 $aTemplateSet[$templateset_default]['PATH'] );
372 $oTemplate = new Template($sTplDir);
373
374 /**
375 * Initialize our custom error handler object
376 */
377 require(SM_PATH . 'class/error.class.php');
378 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
379
380 /**
381 * Activate custom error handling
382 */
383 if (version_compare(PHP_VERSION, "4.3.0", ">=")) {
384 $oldErrorHandler = set_error_handler(array($oErrorHandler, 'SquirrelMailErrorhandler'));
385 } else {
386 $oldErrorHandler = set_error_handler('SquirrelMailErrorhandler');
387 }
388
389 /**
390 * Javascript support detection function
391 * @param boolean $reset recheck javascript support if set to true.
392 * @return integer SMPREF_JS_ON or SMPREF_JS_OFF ({@see functions/constants.php})
393 * @since 1.5.1
394 */
395
396 function checkForJavascript($reset = FALSE) {
397 global $data_dir, $username, $javascript_on, $javascript_setting;
398
399 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
400 return $javascript_on;
401
402 if ( $reset || !isset($javascript_setting) )
403 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
404
405 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
406 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
407 $js_autodetect_results = SMPREF_JS_OFF;
408
409 if ( $javascript_setting == SMPREF_JS_AUTODETECT )
410 $javascript_on = $js_autodetect_results;
411 else
412 $javascript_on = $javascript_setting;
413
414 sqsession_register($javascript_on, 'javascript_on');
415 return $javascript_on;
416 }
417
418 function sqm_baseuri() {
419 global $base_uri;
420 return $base_uri;
421 }