init.php 28 KB

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