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