80a17c5cd869cfbcbc6f0f6649048eba38d499fa
4 * init.php -- initialisation file
6 * File should be loaded in every file in src/ or plugins that occupate an entire frame
8 * @copyright © 2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
15 * SquirrelMail version number -- DO NOT CHANGE
18 $version = '1.5.2 [CVS]';
21 * SquirrelMail internal version number -- DO NOT CHANGE
22 * $sm_internal_version = array (release, major, minor)
25 $SQM_INTERNAL_VERSION = array(1,5,2);
28 * This is a development version so in order to track programmer mistakes we
29 * set the error reporting to E_ALL
32 error_reporting(E_ALL
);
36 * calculate SM_PATH and calculate the base_uri
37 * assumptions made: init.php is only called from plugins or from the src dir.
38 * files in the plugin directory may not be part of a subdirectory called "src"
41 if (isset($_SERVER['SCRIPT_NAME'])) {
42 $a = explode('/',$_SERVER['SCRIPT_NAME']);
43 } elseif (isset($HTTP_SERVER_VARS['SCRIPT_NAME'])) {
44 $a = explode('/',$_SERVER['SCRIPT_NAME']);
47 for($i = count($a) -2;$i > -1; --$i) {
49 if ($a[$i] === 'src' ||
$a[$i] === 'plugins') {
54 $base_uri = implode('/',array_slice($a,0,$i)). '/';
58 define('SM_PATH',$sSM_PATH);
61 * global var $bInit is used to check if initialisation took place.
62 * At this moment it's a workarounf for the include of addrbook_search_html
63 * inside compose.php. If we found a better way then remove this. Do only use
64 * this var if you know for sure a page can be called stand alone and be included
69 require(SM_PATH
. 'functions/global.php');
70 require(SM_PATH
. 'config/config.php');
71 require(SM_PATH
. 'functions/plugin.php');
72 require(SM_PATH
. 'include/constants.php');
73 require(SM_PATH
. 'include/languages.php');
76 * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
77 * Force magic_quotes_runtime off.
78 * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
79 * If there's a better place, please let me know.
81 ini_set('magic_quotes_runtime','0');
84 /* if running with magic_quotes_gpc then strip the slashes
85 from POST and GET global arrays */
86 if (get_magic_quotes_gpc()) {
87 sqstripslashes($_GET);
88 sqstripslashes($_POST);
92 * If register_globals are on, unregister globals.
93 * Code requires PHP 4.1.0 or newer.
95 if ((bool) @ini_get
('register_globals')) {
97 * Remove all globals from $_GET, $_POST, and $_COOKIE.
99 foreach ($_REQUEST as $key => $value) {
100 unset($GLOBALS[$key]);
103 * Remove globalized $_FILES variables
104 * Before 4.3.0 $_FILES are included in $_REQUEST.
105 * Unglobalize them in separate call in order to remove dependency
108 foreach ($_FILES as $key => $value) {
109 unset($GLOBALS[$key]);
110 // there are three undocumented $_FILES globals.
111 unset($GLOBALS[$key.'_type']);
112 unset($GLOBALS[$key.'_name']);
113 unset($GLOBALS[$key.'_size']);
116 * Remove globalized environment variables.
118 foreach ($_ENV as $key => $value) {
119 unset($GLOBALS[$key]);
122 * Remove globalized server variables.
124 foreach ($_SERVER as $key => $value) {
125 unset($GLOBALS[$key]);
129 /* strip any tags added to the url from PHP_SELF.
130 This fixes hand crafted url XXS expoits for any
131 page that uses PHP_SELF as the FORM action */
132 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
134 $PHP_SELF = php_self();
137 * Initialize the session
141 * includes from classes stored in the session
143 require(SM_PATH
. 'class/mime.class.php');
145 /** set the name of the session cookie */
146 if (!isset($session_name) ||
!$session_name) {
147 $session_name = 'SQMSESSID';
149 ini_set('session.name' , $session_name);
150 session_set_cookie_params (0, $base_uri);
151 sqsession_is_active();
153 sqsession_register($base_uri, 'base_uri');
156 * Remove globalized session data in rg=on setups
158 if ((bool) @ini_get
('register_globals')) {
159 foreach ($_SESSION as $key => $value) {
160 unset($GLOBALS[$key]);
164 * Retrieve the language cookie
166 if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE
)) {
167 $squirrelmail_language = '';
172 * @var $sInitlocation From where do we include.
174 if (!isset($sInitLocation)) {
179 * MAIN PLUGIN LOADING CODE HERE
183 * Include Compatibility plugin if available.
185 if (file_exists(SM_PATH
. 'plugins/compatibility/functions.php'))
186 include_once(SM_PATH
. 'plugins/compatibility/functions.php');
187 $squirrelmail_plugin_hooks = array();
189 /* On init, register all plugins configured for use. */
190 if (isset($plugins) && is_array($plugins)) {
191 // turn on output buffering in order to prevent output of new lines
193 foreach ($plugins as $name) {
196 // get output and remove whitespace
197 $output = trim(ob_get_contents());
199 // if plugins output more than newlines and spacing, stop script execution.
200 if (!empty($output)) {
206 switch ($sInitLocation) {
207 case 'style': session_write_close(); sqsetcookieflush(); break;
209 $prefs_backend = do_hook_function('prefs_backend');
210 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH
. $prefs_backend)) {
211 require(SM_PATH
. $prefs_backend);
212 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
213 require(SM_PATH
. 'functions/db_prefs.php');
215 require(SM_PATH
. 'functions/prefs.php');
216 require(SM_PATH
. 'functions/file_prefs.php');
220 require(SM_PATH
. 'functions/display_messages.php' );
221 require(SM_PATH
. 'functions/page_header.php');
222 require(SM_PATH
. 'functions/html.php');
225 require(SM_PATH
. 'functions/display_messages.php' );
226 require(SM_PATH
. 'functions/page_header.php');
227 require(SM_PATH
. 'functions/html.php');
228 require(SM_PATH
. 'functions/strings.php');
232 * Check if we are logged in
234 require(SM_PATH
. 'functions/auth.php');
236 if ( !sqsession_is_registered('user_is_logged_in') ) {
237 // First we store some information in the new session to prevent
240 $session_expired_post = $_POST;
241 $session_expired_location = $PHP_SELF;
242 if (!sqsession_is_registered('session_expired_post')) {
243 sqsession_register($session_expired_post,'session_expired_post');
245 if (!sqsession_is_registered('session_expired_location')) {
246 sqsession_register($session_expired_location,'session_expired_location');
248 // signout page will deal with users who aren't logged
249 // in on its own; don't show error here
251 if (strpos($PHP_SELF, 'signout.php') !== FALSE) {
255 set_up_language($squirrelmail_language, true);
256 logout_error( _("You must be logged in to access this page.") );
260 sqgetGlobalVar('username',$username,SQ_SESSION
);
263 * Setting the prefs backend
265 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
266 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
268 if ( !sqsession_is_registered('prefs_are_cached') ||
269 !isset( $prefs_cache) ||
270 !is_array( $prefs_cache)) {
271 $prefs_are_cached = false;
272 $prefs_cache = false; //array();
275 $prefs_backend = do_hook_function('prefs_backend');
276 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH
. $prefs_backend)) {
277 require(SM_PATH
. $prefs_backend);
278 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
279 require(SM_PATH
. 'functions/db_prefs.php');
281 require(SM_PATH
. 'functions/prefs.php');
282 require(SM_PATH
. 'functions/file_prefs.php');
286 * initializing user settings
288 require(SM_PATH
. 'include/load_prefs.php');
291 // i do not understand the frames language cookie story
293 * We'll need this to later have a noframes version
295 * Check if the user has a language preference, but no cookie.
296 * Send him a cookie with his language preference, if there is
299 $my_language = getPref($data_dir, $username, 'language');
300 if ($my_language != $squirrelmail_language) {
301 sqsetcookie('squirrelmail_language', $my_language, time()+
2592000, $base_uri);
306 * Set up the language.
308 $err=set_up_language(getPref($data_dir, $username, 'language'));
309 /* this is the last cookie we set so flush it. */
312 // Japanese translation used without mbstring support
315 "<p>You need to have PHP installed with the multibyte string function \n".
316 "enabled (using configure option --enable-mbstring).</p>\n".
317 "<p>System assumed that you accidently switched to Japanese translation \n".
318 "and reverted your language preference to English.</p>\n".
319 "<p>Please refresh this page in order to use webmail.</p>\n";
323 $timeZone = getPref($data_dir, $username, 'timezone');
325 /* Check to see if we are allowed to set the TZ environment variable.
326 * We are able to do this if ...
327 * safe_mode is disabled OR
328 * safe_mode_allowed_env_vars is empty (you are allowed to set any) OR
329 * safe_mode_allowed_env_vars contains TZ
331 $tzChangeAllowed = (!ini_get('safe_mode')) ||
332 !strcmp(ini_get('safe_mode_allowed_env_vars'),'') ||
333 preg_match('/^([\w_]+,)*TZ/', ini_get('safe_mode_allowed_env_vars'));
335 if ( $timeZone != SMPREF_NONE
&& ($timeZone != "")
336 && $tzChangeAllowed ) {
338 // get time zone key, if strict or custom strict timezones are used
339 if (isset($time_zone_type) &&
340 ($time_zone_type == 1 ||
$time_zone_type == 3)) {
341 /* load time zone functions */
342 require(SM_PATH
. 'include/timezones.php');
343 $realTimeZone = sq_get_tz_key($timeZone);
345 $realTimeZone = $timeZone;
350 putenv("TZ=".$realTimeZone);
357 * Initialize the template object
359 require(SM_PATH
. 'class/template/template.class.php');
361 * $sTplDir is not initialized when a user is not logged in, so we will use
362 * the config file defaults here. If the neccesary variables are net set,
363 * force a default value.
365 $aTemplateSet = ( !isset($aTemplateSet) ?
array() : $aTemplateSet );
366 $templateset_default = ( !isset($templateset_default) ?
0 : $templateset_default );
368 $sTplDir = ( !isset($aTemplateSet[$templateset_default]['PATH']) ?
369 SM_PATH
. 'templates/default/' :
370 $aTemplateSet[$templateset_default]['PATH'] );
371 $oTemplate = new Template($sTplDir);
374 * Initialize our custom error handler object
376 require(SM_PATH
. 'class/error.class.php');
377 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
380 * Activate custom error handling
382 if (version_compare(PHP_VERSION
, "4.3.0", ">=")) {
383 $oldErrorHandler = set_error_handler(array($oErrorHandler, 'SquirrelMailErrorhandler'));
385 $oldErrorHandler = set_error_handler('SquirrelMailErrorhandler');
389 * Javascript support detection function
390 * @param boolean $reset recheck javascript support if set to true.
391 * @return integer SMPREF_JS_ON or SMPREF_JS_OFF ({@see functions/constants.php})
395 function checkForJavascript($reset = FALSE) {
396 global $data_dir, $username, $javascript_on, $javascript_setting;
398 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION
) )
399 return $javascript_on;
401 if ( $reset ||
!isset($javascript_setting) )
402 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT
);
404 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
405 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
406 $js_autodetect_results = SMPREF_JS_OFF
;
408 if ( $javascript_setting == SMPREF_JS_AUTODETECT
)
409 $javascript_on = $js_autodetect_results;
411 $javascript_on = $javascript_setting;
413 sqsession_register($javascript_on, 'javascript_on');
414 return $javascript_on;
417 function sqm_baseuri() {