Add a functions file for file utility functions (say that 10 times fast) along with...
[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
1888b1bf 17FIXME: disabling this for now, because we now have $sm_debug_mode, but the problem with that is that we don't know what it will be until we have loaded the config file, a good 175 lines below after several important files have been included, etc. For now, we'll trust that developers have turned on E_ALL in php.ini anyway, but this can be uncommented if not.
202bcbcc 18 */
1888b1bf 19//error_reporting(E_ALL);
20
202bcbcc 21
202bcbcc 22/**
c7ebdfcf 23 * Make sure we have a page name
24 *
25 */
26if ( !defined('PAGE_NAME') ) define('PAGE_NAME', NULL);
27
28
29/**
6a2a6e44 30 * If register_globals are on, unregister globals.
a3b99374 31 * Second test covers boolean set as string (php_value register_globals off).
6a2a6e44 32 */
55dd9abf 33if ((bool) ini_get('register_globals') &&
a3b99374 34 strtolower(ini_get('register_globals'))!='off') {
6a2a6e44 35 /**
55dd9abf 36 * Remove all globals that are not reserved by PHP
37 * 'value' and 'key' are used by foreach. Don't unset them inside foreach.
6a2a6e44 38 */
55dd9abf 39 foreach ($GLOBALS as $key => $value) {
40 switch($key) {
41 case 'HTTP_POST_VARS':
42 case '_POST':
43 case 'HTTP_GET_VARS':
44 case '_GET':
45 case 'HTTP_COOKIE_VARS':
46 case '_COOKIE':
47 case 'HTTP_SERVER_VARS':
48 case '_SERVER':
49 case 'HTTP_ENV_VARS':
50 case '_ENV':
51 case 'HTTP_POST_FILES':
52 case '_FILES':
53 case '_REQUEST':
54 case 'HTTP_SESSION_VARS':
55 case '_SESSION':
56 case 'GLOBALS':
57 case 'key':
58 case 'value':
59 break;
55dd9abf 60 default:
61 unset($GLOBALS[$key]);
62 }
6a2a6e44 63 }
55dd9abf 64 // Unset variables used in foreach
65 unset($GLOBALS['key']);
66 unset($GLOBALS['value']);
6a2a6e44 67}
68
71efd1ed 69/**
d849b570 70 * Used as a dummy value, e.g., for passing as an empty
e39d00e9 71 * hook argument (where the value is passed by reference,
72 * and therefore NULL itself is not acceptable).
d849b570 73 */
086ad092 74global $null;
d849b570 75$null = NULL;
76
77/**
dbbd586e 78 * The global $server_os variable will be "windows" if
79 * we are working in a Windows environment or "*nix"
80 * otherwise.
81 */
82global $server_os;
83if (DIRECTORY_SEPARATOR == '\\') $server_os = 'windows'; else $server_os = '*nix';
84
85/**
71efd1ed 86 * [#1518885] session.use_cookies = off breaks SquirrelMail
87 *
086ad092 88 * When session cookies are not used, all http redirects, meta refreshes,
89 * src/download.php and javascript URLs are broken. Setting must be set
71efd1ed 90 * before session is started.
91 */
92if (!(bool)ini_get('session.use_cookies') ||
93 ini_get('session.use_cookies') == 'off') {
94 ini_set('session.use_cookies','1');
95}
6a2a6e44 96
79dd8c72 97/**
98 * Initialize seed of random number generator.
99 * We use a number of things to randomize input: current time in ms,
100 * info about the remote client, info about the current process, the
101 * randomness of uniqid and stat of the current file.
102 *
103 * We seed this here only once per init, not only to save cycles
104 * but also to make the result of mt_rand more random (it now also
105 * depends on the number of times mt_rand was called before in this
106 * execution.
107 */
108$seed = microtime() . $_SERVER['REMOTE_PORT'] . $_SERVER['REMOTE_ADDR'] . getmypid();
109
110if (function_exists('getrusage')) {
111 /* Avoid warnings with Win32 */
112 $dat = @getrusage();
113 if (isset($dat) && is_array($dat)) { $seed .= implode('', $dat); }
114}
115
116if(!empty($_SERVER['UNIQUE_ID'])) {
117 $seed .= $_SERVER['UNIQUE_ID'];
118}
119
120$seed .= uniqid(mt_rand(),TRUE);
121$seed .= implode( '', stat( __FILE__) );
122
123/** PHP 4.2 and up don't require seeding, but their used seed algorithm
124 * is of questionable quality, so we keep doing it ourselves. */
125mt_srand(hexdec(md5($seed)));
3f081dd0 126
6a2a6e44 127/**
202bcbcc 128 * calculate SM_PATH and calculate the base_uri
129 * assumptions made: init.php is only called from plugins or from the src dir.
130 * files in the plugin directory may not be part of a subdirectory called "src"
131 *
132 */
133if (isset($_SERVER['SCRIPT_NAME'])) {
3f081dd0 134 $a = explode('/', $_SERVER['SCRIPT_NAME']);
202bcbcc 135} elseif (isset($HTTP_SERVER_VARS['SCRIPT_NAME'])) {
3f081dd0 136 $a = explode('/', $HTTP_SERVER_VARS['SCRIPT_NAME']);
b0829edf 137} else {
3f081dd0 138 $error = 'Unable to detect script environment. Please test your PHP '
139 . 'settings and send your PHP core configuration, $_SERVER and '
140 . '$HTTP_SERVER_VARS contents to the SquirrelMail developers.';
b0829edf 141 die($error);
202bcbcc 142}
143$sSM_PATH = '';
3f081dd0 144for($i = count($a) -2; $i > -1; --$i) {
202bcbcc 145 $sSM_PATH .= '../';
146 if ($a[$i] === 'src' || $a[$i] === 'plugins') {
147 break;
148 }
149}
150
3f081dd0 151$base_uri = implode('/', array_slice($a, 0, $i)). '/';
202bcbcc 152
202bcbcc 153define('SM_PATH',$sSM_PATH);
6a2a6e44 154define('SM_BASE_URI', $base_uri);
3f081dd0 155
156
202bcbcc 157/**
158 * global var $bInit is used to check if initialisation took place.
159 * At this moment it's a workarounf for the include of addrbook_search_html
160 * inside compose.php. If we found a better way then remove this. Do only use
161 * this var if you know for sure a page can be called stand alone and be included
162 * in another file.
163 */
164$bInit = true;
165
8e1e2794 166/**
167 * This theme as a failsafe if no themes were found, or if we error
168 * out before anything could be initialised.
169 */
170$color = array();
171$color[0] = '#DCDCDC'; /* light gray TitleBar */
172$color[1] = '#800000'; /* red */
173$color[2] = '#CC0000'; /* light red Warning/Error Messages */
174$color[3] = '#A0B8C8'; /* green-blue Left Bar Background */
175$color[4] = '#FFFFFF'; /* white Normal Background */
176$color[5] = '#FFFFCC'; /* light yellow Table Headers */
177$color[6] = '#000000'; /* black Text on left bar */
178$color[7] = '#0000CC'; /* blue Links */
179$color[8] = '#000000'; /* black Normal text */
180$color[9] = '#ABABAB'; /* mid-gray Darker version of #0 */
181$color[10] = '#666666'; /* dark gray Darker version of #9 */
182$color[11] = '#770000'; /* dark red Special Folders color */
183$color[12] = '#EDEDED';
184$color[13] = '#800000'; /* (dark red) Color for quoted text -- > 1 quote */
185$color[14] = '#ff0000'; /* (red) Color for quoted text -- >> 2 or more */
186$color[15] = '#002266'; /* (dark blue) Unselectable folders */
187$color[16] = '#ff9933'; /* (orange) Highlight color */
188
1888b1bf 189require(SM_PATH . 'include/constants.php');
202bcbcc 190require(SM_PATH . 'functions/global.php');
4ffcf13a 191require(SM_PATH . 'functions/strings.php');
918fcc1d 192require(SM_PATH . 'functions/arrays.php');
67c826ce 193require(SM_PATH . 'functions/files.php');
5e68a08e 194
195/* load default configuration */
196require(SM_PATH . 'config/config_default.php');
197/* reset arrays in default configuration */
198$ldap_server = array();
199$plugins = array();
200$fontsets = array();
5e68a08e 201$aTemplateSet = array();
28294310 202$aTemplateSet[0]['ID'] = 'default';
203$aTemplateSet[0]['NAME'] = 'Default';
01fd1d1a 204
5e68a08e 205/* load site configuration */
202bcbcc 206require(SM_PATH . 'config/config.php');
5e68a08e 207/* load local configuration overrides */
208if (file_exists(SM_PATH . 'config/config_local.php')) {
209 require(SM_PATH . 'config/config_local.php');
210}
211
1888b1bf 212
213/**
214 * Set PHP error reporting level based on the SquirrelMail debug mode
215 */
216$error_level = 0;
217if ($sm_debug_mode & SM_DEBUG_MODE_SIMPLE)
218 $error_level |= E_ERROR;
219if ($sm_debug_mode & SM_DEBUG_MODE_MODERATE
220 || $sm_debug_mode & SM_DEBUG_MODE_ADVANCED)
221 $error_level |= E_ALL;
222if ($sm_debug_mode & SM_DEBUG_MODE_STRICT)
223 $error_level |= E_STRICT;
224error_reporting($error_level);
225
226
202bcbcc 227require(SM_PATH . 'functions/plugin.php');
202bcbcc 228require(SM_PATH . 'include/languages.php');
42b5e8aa 229require(SM_PATH . 'class/template/Template.class.php');
5ab684a5 230require(SM_PATH . 'class/error.class.php');
202bcbcc 231
232/**
233 * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
234 * Force magic_quotes_runtime off.
235 * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
236 * If there's a better place, please let me know.
237 */
238ini_set('magic_quotes_runtime','0');
239
240
241/* if running with magic_quotes_gpc then strip the slashes
242 from POST and GET global arrays */
430a19f3 243if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc()) {
202bcbcc 244 sqstripslashes($_GET);
245 sqstripslashes($_POST);
246}
247
202bcbcc 248
249/* strip any tags added to the url from PHP_SELF.
250This fixes hand crafted url XXS expoits for any
251 page that uses PHP_SELF as the FORM action */
252$_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
253
254$PHP_SELF = php_self();
255
256/**
257 * Initialize the session
258 */
259
e8c4e350 260/** set the name of the session cookie */
261if (!isset($session_name) || !$session_name) {
262 $session_name = 'SQMSESSID';
263}
264
265/**
319ad3c0 266 * When session.auto_start is On we want to destroy/close the session
1d537493 267 */
268$sSessionAutostartName = session_name();
ccb5faf0 269$sSessionAutostartID = session_id();
270if (!empty($sSessionAutostartID) && $sSessionAutostartName !== $session_name) {
1d537493 271 $sCookiePath = ini_get('session.cookie_path');
272 $sCookieDomain = ini_get('session.cookie_domain');
e8c4e350 273 // reset the cookie
ccb5faf0 274 sqsetcookie($sSessionAutostartName,'',1,$sCookiePath,$sCookieDomain);
e8c4e350 275 @session_destroy();
276 session_write_close();
1d537493 277}
e8c4e350 278
202bcbcc 279/**
280 * includes from classes stored in the session
281 */
282require(SM_PATH . 'class/mime.class.php');
283
202bcbcc 284ini_set('session.name' , $session_name);
285session_set_cookie_params (0, $base_uri);
286sqsession_is_active();
287
202bcbcc 288/**
319ad3c0 289 * When on login page, have to reset the user session, making
290 * sure to save session restore data first
291 */
292if (PAGE_NAME == 'login') {
293 if (!sqGetGlobalVar('session_expired_post', $sep, SQ_SESSION))
294 $sep = '';
295 if (!sqGetGlobalVar('session_expired_location', $sel, SQ_SESSION))
296 $sel = '';
297 sqsession_destroy();
298 session_write_close();
299
300 /**
301 * in some rare instances, the session seems to stick
302 * around even after destroying it (!!), so if it does,
303 * we'll manually flatten the $_SESSION data
304 */
305 if (!empty($_SESSION))
306 $_SESSION = array();
307
bc3acc5a 308 /**
309 * Allow administrators to define custom session handlers
310 * for SquirrelMail without needing to change anything in
311 * php.ini (application-level).
312 *
313 * In config_local.php, admin needs to put:
314 *
315 * $custom_session_handlers = array(
316 * 'my_open_handler',
317 * 'my_close_handler',
318 * 'my_read_handler',
319 * 'my_write_handler',
320 * 'my_destroy_handler',
321 * 'my_gc_handler',
322 * );
323 * session_module_name('user');
324 * session_set_save_handler(
325 * $custom_session_handlers[0],
326 * $custom_session_handlers[1],
327 * $custom_session_handlers[2],
328 * $custom_session_handlers[3],
329 * $custom_session_handlers[4],
330 * $custom_session_handlers[5]
331 * );
332 *
333 * We need to replicate that code once here because PHP has
334 * long had a bug that resets the session handler mechanism
335 * when the session data is also destroyed. Because of this
336 * bug, even administrators who define custom session handlers
337 * via a PHP pre-load defined in php.ini (auto_prepend_file)
338 * will still need to define the $custom_session_handlers array
339 * in config_local.php.
340 */
341 global $custom_session_handlers;
342 if (!empty($custom_session_handlers)) {
343 $open = $custom_session_handlers[0];
344 $close = $custom_session_handlers[1];
345 $read = $custom_session_handlers[2];
346 $write = $custom_session_handlers[3];
347 $destroy = $custom_session_handlers[4];
348 $gc = $custom_session_handlers[5];
349 session_module_name('user');
350 session_set_save_handler($open, $close, $read, $write, $destroy, $gc);
351 }
352
319ad3c0 353 sqsession_is_active();
354 session_regenerate_id();
ef33def6 355
356 // put session restore data back into session if necessary
357 if (!empty($sel)) {
358 sqsession_register($sel, 'session_expired_location');
359 if (!empty($sep))
360 sqsession_register($sep, 'session_expired_post');
361 }
319ad3c0 362}
363
364/**
5aed95be 365 * SquirrelMail internal version number -- DO NOT CHANGE
366 * $sm_internal_version = array (release, major, minor)
367 */
a895042a 368$SQM_INTERNAL_VERSION = explode('.', SM_VERSION, 3);
b37e457f 369$SQM_INTERNAL_VERSION[2] = intval($SQM_INTERNAL_VERSION[2]);
5aed95be 370
93d67e0d 371
6d5775db 372/* load prefs system; even when user not logged in, should be OK to do this here */
373require(SM_PATH . 'functions/prefs.php');
374
6d5775db 375
086ad092 376/* if plugins are disabled only for one user and
93d67e0d 377 * the current user is NOT that user, turn them
378 * back on
379 */
0d56053e 380sqgetGlobalVar('username', $username, SQ_SESSION);
93d67e0d 381if ($disable_plugins && !empty($disable_plugins_user)
382 && $username != $disable_plugins_user) {
383 $disable_plugins = false;
384}
385
4a9f6063 386
93d67e0d 387/* remove all plugins if they are disabled */
388if ($disable_plugins) {
389 $plugins = array();
390}
391
392
5aed95be 393/**
394 * Include Compatibility plugin if available.
395 */
93d67e0d 396if (!$disable_plugins && file_exists(SM_PATH . 'plugins/compatibility/functions.php'))
5aed95be 397 include_once(SM_PATH . 'plugins/compatibility/functions.php');
398
4a9f6063 399
5aed95be 400/**
401 * MAIN PLUGIN LOADING CODE HERE
086ad092 402 * On init, we no longer need to load all plugin setup files.
5aed95be 403 * Now, we load the statically generated hook registrations here
404 * and let the hook calls include only the plugins needed.
405 */
406$squirrelmail_plugin_hooks = array();
93d67e0d 407if (!$disable_plugins && file_exists(SM_PATH . 'config/plugin_hooks.php')) {
4a9f6063 408//FIXME: if we keep the plugin hooks array static like this, it seems like we should also keep the template files list in a static file too (when a new user session is started or the template set is changed, the code will dynamically iterate through the directory heirarchy of the template directory and catalog all the template files therein (and store the "catalog" in PHP session) -- instead, we could do that once at config-time and keep that static so SM can just include the file just like the line below)
5aed95be 409 require(SM_PATH . 'config/plugin_hooks.php');
410}
411
4a9f6063 412
5aed95be 413/**
4a9f6063 414 * Plugin authors note that the "config_override" hook used to be
415 * executed here, but please adapt your plugin to use this "prefs_backend"
416 * hook instead, making sure that it does NOT return anything, since
417 * doing so will interfere with proper prefs system functionality.
418 * Of course, otherwise, this hook may be used to do any configuration
419 * overrides as needed, as well as set up a custom preferences backend.
5aed95be 420 */
4a9f6063 421$prefs_backend = do_hook('prefs_backend', $null);
422if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH . $prefs_backend)) {
423 require(SM_PATH . $prefs_backend);
424} elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
425 require(SM_PATH . 'functions/db_prefs.php');
426} else {
427 require(SM_PATH . 'functions/file_prefs.php');
428}
429
430
5aed95be 431
432/**
3464e1f4 433 * DISABLED.
202bcbcc 434 * Remove globalized session data in rg=on setups
086ad092 435 *
3464e1f4 436 * Code can be utilized when session is started, but data is not loaded.
086ad092 437 * We have already loaded configuration and other important vars. Can't
aae60854 438 * clean session globals here, beside, the cleanout of globals at the
439 * top of this file will have removed anything this code would find anyway.
3464e1f4 440if ((bool) @ini_get('register_globals') &&
441 strtolower(ini_get('register_globals'))!='off') {
202bcbcc 442 foreach ($_SESSION as $key => $value) {
443 unset($GLOBALS[$key]);
444 }
445}
3464e1f4 446*/
6a2a6e44 447
826ddd72 448sqsession_register(SM_BASE_URI,'base_uri');
6a2a6e44 449
202bcbcc 450/**
451 * Retrieve the language cookie
452 */
453if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) {
454 $squirrelmail_language = '';
455}
456
bf3abdc3 457
202bcbcc 458/**
775a1f52 459 * In some cases, buffering all output allows more complex functionality,
460 * especially for plugins that want to add headers on hooks that are beyond
461 * the point of output having been sent to the browser otherwise.
462 *
463 * Note that we don't turn this on any earlier since we want to allow plugins
464 * to turn it on themselves via a configuration override on the prefs_backend
465 * hook.
466 *
467 */
468if ($buffer_output) ob_start(!empty($buffered_output_handler) ? $buffered_output_handler : NULL);
469
470
471/**
f0d28f44 472 * Do something special for some pages. This is based on the PAGE_NAME constant
9e06a3ea 473 * set at the top of every page.
202bcbcc 474 */
0d56053e 475$set_up_langage_after_template_setup = FALSE;
9e06a3ea 476switch (PAGE_NAME) {
086ad092 477 case 'style':
c4e5f61f 478
2b26084f 479 // need to get the right template set up
28294310 480 //
481 sqGetGlobalVar('templateid', $templateid, SQ_GET);
c4e5f61f 482
2b26084f 483 // sanitize just in case...
28294310 484 //
485 $templateid = preg_replace('/(\.\.\/){1,}/', '', $templateid);
486
487 // make sure given template actually is available
488 //
28294310 489 $found_templateset = false;
490 for ($i = 0; $i < count($aTemplateSet); ++$i) {
491 if ($aTemplateSet[$i]['ID'] == $templateid) {
492 $found_templateset = true;
493 break;
494 }
495 }
c4e5f61f 496
be155e14 497// FIXME: do we need/want to check here for actual (physical) presence of template sets?
28294310 498 // selected template not available, fall back to default template
499 //
500 if (!$found_templateset) {
42b5e8aa 501 $sTemplateID = Template::get_default_template_set();
28294310 502 } else {
503 $sTemplateID = $templateid;
c4e5f61f 504 }
505
2b26084f 506 session_write_close();
c4e5f61f 507 break;
508
f0d28f44 509 case 'mailto':
510 // nothing to do
511 break;
512
202bcbcc 513 case 'redirect':
2e616fa4 514 require(SM_PATH . 'functions/auth.php');
202bcbcc 515 //nobreak;
bf3abdc3 516
202bcbcc 517 case 'login':
518 require(SM_PATH . 'functions/display_messages.php' );
519 require(SM_PATH . 'functions/page_header.php');
520 require(SM_PATH . 'functions/html.php');
42b5e8aa 521
522 // reset template file cache
523 //
524 $sTemplateID = Template::get_default_template_set();
d81572f7 525 Template::cache_template_file_hierarchy($sTemplateID, TRUE);
42b5e8aa 526
1d537493 527 /**
01fd1d1a 528 * Make sure icon variables are setup for the login page.
529 */
530 $icon_theme = $icon_themes[$icon_theme_def]['PATH'];
531 /*
532 * NOTE: The $icon_theme_path var should contain the path to the icon
533 * theme to use. If the admin has disabled icons, or the user has
534 * set the icon theme to "None," no icons will be used.
535 */
536 $icon_theme_path = (!$use_icons || $icon_theme=='none') ? NULL : ($icon_theme == 'template' ? SM_PATH . Template::calculate_template_images_directory($sTemplateID) : $icon_theme);
537
202bcbcc 538 break;
539 default:
540 require(SM_PATH . 'functions/display_messages.php' );
541 require(SM_PATH . 'functions/page_header.php');
542 require(SM_PATH . 'functions/html.php');
202bcbcc 543
544
545 /**
546 * Check if we are logged in
547 */
548 require(SM_PATH . 'functions/auth.php');
549
550 if ( !sqsession_is_registered('user_is_logged_in') ) {
f8eb968d 551
552 // use $message to indicate what logout text the user
553 // will see... if 0, typical "You must be logged in"
554 // if 1, information that the user session was saved
555 // and will be resumed after (re)login
556 //
557 $message = 0;
558
202bcbcc 559 // First we store some information in the new session to prevent
560 // information-loss.
561 //
562 $session_expired_post = $_POST;
f8e68605 563 $session_expired_location = PAGE_NAME;
202bcbcc 564 if (!sqsession_is_registered('session_expired_post')) {
565 sqsession_register($session_expired_post,'session_expired_post');
566 }
567 if (!sqsession_is_registered('session_expired_location')) {
568 sqsession_register($session_expired_location,'session_expired_location');
f8e68605 569 if ($session_expired_location == 'compose')
f8eb968d 570 $message = 1;
202bcbcc 571 }
572 // signout page will deal with users who aren't logged
573 // in on its own; don't show error here
574 //
9e06a3ea 575 if ( PAGE_NAME == 'signout' ) {
a140422a 576 return;
202bcbcc 577 }
578
8efadc6b 579 /**
580 * Initialize the template object (logout_error uses it)
581 */
8efadc6b 582 /*
086ad092 583 * $sTemplateID is not initialized when a user is not logged in, so we
584 * will use the config file defaults here. If the neccesary variables
d81572f7 585 * are not set, force a default value.
8efadc6b 586 */
d81572f7 587 if (PAGE_NAME == 'squirrelmail_rpc') {
588 $sTemplateID = Template::get_rpc_template_set();
589 } else {
590 $sTemplateID = Template::get_default_template_set();
591 }
28294310 592 $oTemplate = Template::construct_template($sTemplateID);
8efadc6b 593
202bcbcc 594 set_up_language($squirrelmail_language, true);
f8eb968d 595 if (!$message)
596 logout_error( _("You must be logged in to access this page.") );
597 else
598 logout_error( _("Your session has expired, but will be resumed after logging in again.") );
202bcbcc 599 exit;
600 }
601
79524620 602 sqgetGlobalVar('authz',$authz,SQ_SESSION);
202bcbcc 603
604 /**
605 * Setting the prefs backend
606 */
607 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
608 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
609
610 if ( !sqsession_is_registered('prefs_are_cached') ||
611 !isset( $prefs_cache) ||
612 !is_array( $prefs_cache)) {
613 $prefs_are_cached = false;
614 $prefs_cache = false; //array();
615 }
616
202bcbcc 617 /**
618 * initializing user settings
619 */
620 require(SM_PATH . 'include/load_prefs.php');
621
202bcbcc 622 /**
623 * We'll need this to later have a noframes version
624 *
625 * Check if the user has a language preference, but no cookie.
626 * Send him a cookie with his language preference, if there is
627 * such discrepancy.
628 */
629 $my_language = getPref($data_dir, $username, 'language');
630 if ($my_language != $squirrelmail_language) {
631 sqsetcookie('squirrelmail_language', $my_language, time()+2592000, $base_uri);
632 }
202bcbcc 633
0d56053e 634 $set_up_langage_after_template_setup = TRUE;
202bcbcc 635
636 $timeZone = getPref($data_dir, $username, 'timezone');
637
638 /* Check to see if we are allowed to set the TZ environment variable.
639 * We are able to do this if ...
640 * safe_mode is disabled OR
641 * safe_mode_allowed_env_vars is empty (you are allowed to set any) OR
642 * safe_mode_allowed_env_vars contains TZ
643 */
644 $tzChangeAllowed = (!ini_get('safe_mode')) ||
645 !strcmp(ini_get('safe_mode_allowed_env_vars'),'') ||
646 preg_match('/^([\w_]+,)*TZ/', ini_get('safe_mode_allowed_env_vars'));
647
648 if ( $timeZone != SMPREF_NONE && ($timeZone != "")
649 && $tzChangeAllowed ) {
650
651 // get time zone key, if strict or custom strict timezones are used
652 if (isset($time_zone_type) &&
653 ($time_zone_type == 1 || $time_zone_type == 3)) {
654 /* load time zone functions */
655 require(SM_PATH . 'include/timezones.php');
656 $realTimeZone = sq_get_tz_key($timeZone);
657 } else {
658 $realTimeZone = $timeZone;
659 }
660
661 // set time zone
662 if ($realTimeZone) {
663 putenv("TZ=".$realTimeZone);
664 }
665 }
867fed37 666
667 /**
668 * php 5.1.0 added time zone functions. Set time zone with them in order
669 * to prevent E_STRICT notices and allow time zone modifications in safe_mode.
670 */
671 if (function_exists('date_default_timezone_set')) {
672 if ($timeZone != SMPREF_NONE && $timeZone != "") {
673 date_default_timezone_set($timeZone);
674 } else {
675 // interface runs on server's time zone. Remove php E_STRICT complains
676 $default_timezone = @date_default_timezone_get();
086ad092 677 date_default_timezone_set($default_timezone);
867fed37 678 }
679 }
202bcbcc 680 break;
681}
682
202bcbcc 683/*
086ad092 684 * $sTemplateID is not initialized when a user is not logged in, so we
685 * will use the config file defaults here. If the neccesary variables
28294310 686 * are not set, force a default value.
086ad092 687 *
688 * If the user is logged in, $sTemplateID will be set in load_prefs.php,
28294310 689 * so we shouldn't change it here.
202bcbcc 690 */
28294310 691if (!isset($sTemplateID)) {
d81572f7 692 if (PAGE_NAME == 'squirrelmail_rpc') {
693 $sTemplateID = Template::get_rpc_template_set();
694 } else {
695 $sTemplateID = Template::get_default_template_set();
696 }
28294310 697 $icon_theme_path = !$use_icons ? NULL : Template::calculate_template_images_directory($sTemplateID);
3aa46abc 698}
be155e14 699
700// template object may have already been constructed in load_prefs.php
701//
702if (empty($oTemplate)) {
703 $oTemplate = Template::construct_template($sTemplateID);
704}
202bcbcc 705
7aae649d 706// We want some variables to always be available to the template
551c7b53 707//
e39d00e9 708$oTemplate->assign('javascript_on',
709 (sqGetGlobalVar('user_is_logged_in', $user_is_logged_in, SQ_SESSION)
710 ? checkForJavascript() : 0));
fe8103c2 711$oTemplate->assign('base_uri', sqm_baseuri());
457e8593 712$always_include = array('sTemplateID', 'icon_theme_path');
7aae649d 713foreach ($always_include as $var) {
714 $oTemplate->assign($var, (isset($$var) ? $$var : NULL));
715}
716
551c7b53 717// A few output elements are used often, so just get them once here
718//
719$nbsp = $oTemplate->fetch('non_breaking_space.tpl');
720$br = $oTemplate->fetch('line_break.tpl');
721
0d56053e 722
723/**
724 * Set up the language.
725 *
726 * This code block corresponds to the *default* block of the switch
727 * statement above, but the language cannot be set up until after the
728 * template is instantiated, so we set $set_up_langage_after_template_setup
729 * above and do the linguistic stuff now.
730 */
731if ($set_up_langage_after_template_setup) {
732 $err=set_up_language(getPref($data_dir, $username, 'language'));
733
734 // Japanese translation used without mbstring support
735 if ($err==2) {
736 $sError = "<p>Your administrator needs to have PHP installed with the multibyte string extension enabled (using configure option --enable-mbstring).</p>\n"
737 . "<p>This system has assumed that you accidently switched to Japanese and has reverted your language preference to English.</p>\n"
738 . "<p>Please refresh this page in order to continue using your webmail.</p>\n";
739 error_box($sError);
740 }
741}
742
743
202bcbcc 744/**
745 * Initialize our custom error handler object
746 */
202bcbcc 747$oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
748
0d56053e 749
202bcbcc 750/**
751 * Activate custom error handling
752 */
753if (version_compare(PHP_VERSION, "4.3.0", ">=")) {
754 $oldErrorHandler = set_error_handler(array($oErrorHandler, 'SquirrelMailErrorhandler'));
755} else {
756 $oldErrorHandler = set_error_handler('SquirrelMailErrorhandler');
757}
758
f0d28f44 759
760// ============================================================================
761// ================= End of Live Code, Beginning of Functions =================
762// ============================================================================
763
764
202bcbcc 765/**
766 * Javascript support detection function
767 * @param boolean $reset recheck javascript support if set to true.
867fed37 768 * @return integer SMPREF_JS_ON or SMPREF_JS_OFF ({@see include/constants.php})
202bcbcc 769 * @since 1.5.1
770 */
202bcbcc 771function checkForJavascript($reset = FALSE) {
772 global $data_dir, $username, $javascript_on, $javascript_setting;
773
774 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
775 return $javascript_on;
776
e39d00e9 777 $user_is_logged_in = FALSE;
bf3abdc3 778 if ( $reset || !isset($javascript_setting) )
202bcbcc 779 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
780
781 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
782 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
783 $js_autodetect_results = SMPREF_JS_OFF;
784
785 if ( $javascript_setting == SMPREF_JS_AUTODETECT )
786 $javascript_on = $js_autodetect_results;
787 else
788 $javascript_on = $javascript_setting;
789
790 sqsession_register($javascript_on, 'javascript_on');
791 return $javascript_on;
792}
793
794function sqm_baseuri() {
795 global $base_uri;
796 return $base_uri;
8e1e2794 797}