init.php 20 KB

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