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