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