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 * If register_globals are on, unregister globals.
37 * Code requires PHP 4.1.0 or newer.
39 if ((bool) @ini_get
('register_globals')) {
41 * Remove all globals from $_GET, $_POST, and $_COOKIE.
43 foreach ($_REQUEST as $key => $value) {
44 unset($GLOBALS[$key]);
47 * Remove globalized $_FILES variables
48 * Before 4.3.0 $_FILES are included in $_REQUEST.
49 * Unglobalize them in separate call in order to remove dependency
52 foreach ($_FILES as $key => $value) {
53 unset($GLOBALS[$key]);
54 // there are three undocumented $_FILES globals.
55 unset($GLOBALS[$key.'_type']);
56 unset($GLOBALS[$key.'_name']);
57 unset($GLOBALS[$key.'_size']);
60 * Remove globalized environment variables.
62 foreach ($_ENV as $key => $value) {
63 unset($GLOBALS[$key]);
66 * Remove globalized server variables.
68 foreach ($_SERVER as $key => $value) {
69 unset($GLOBALS[$key]);
75 * calculate SM_PATH and calculate the base_uri
76 * assumptions made: init.php is only called from plugins or from the src dir.
77 * files in the plugin directory may not be part of a subdirectory called "src"
80 if (isset($_SERVER['SCRIPT_NAME'])) {
81 $a = explode('/',$_SERVER['SCRIPT_NAME']);
82 } elseif (isset($HTTP_SERVER_VARS['SCRIPT_NAME'])) {
83 $a = explode('/',$_SERVER['SCRIPT_NAME']);
86 for($i = count($a) -2;$i > -1; --$i) {
88 if ($a[$i] === 'src' ||
$a[$i] === 'plugins') {
93 $base_uri = implode('/',array_slice($a,0,$i)). '/';
95 define('SM_PATH',$sSM_PATH);
96 define('SM_BASE_URI', $base_uri);
98 * global var $bInit is used to check if initialisation took place.
99 * At this moment it's a workarounf for the include of addrbook_search_html
100 * inside compose.php. If we found a better way then remove this. Do only use
101 * this var if you know for sure a page can be called stand alone and be included
106 require(SM_PATH
. 'functions/global.php');
107 require(SM_PATH
. 'config/config.php');
108 require(SM_PATH
. 'functions/plugin.php');
109 require(SM_PATH
. 'include/constants.php');
110 require(SM_PATH
. 'include/languages.php');
113 * If magic_quotes_runtime is on, SquirrelMail breaks in new and creative ways.
114 * Force magic_quotes_runtime off.
115 * tassium@squirrelmail.org - I put it here in the hopes that all SM code includes this.
116 * If there's a better place, please let me know.
118 ini_set('magic_quotes_runtime','0');
121 /* if running with magic_quotes_gpc then strip the slashes
122 from POST and GET global arrays */
123 if (get_magic_quotes_gpc()) {
124 sqstripslashes($_GET);
125 sqstripslashes($_POST);
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();
154 * Remove globalized session data in rg=on setups
156 if ((bool) @ini_get
('register_globals')) {
157 foreach ($_SESSION as $key => $value) {
158 unset($GLOBALS[$key]);
162 sqsession_register($base_uri, SM_BASE_URI
);
165 * Retrieve the language cookie
167 if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE
)) {
168 $squirrelmail_language = '';
173 * @var $sInitlocation From where do we include.
175 if (!isset($sInitLocation)) {
180 * MAIN PLUGIN LOADING CODE HERE
184 * Include Compatibility plugin if available.
186 if (file_exists(SM_PATH
. 'plugins/compatibility/functions.php'))
187 include_once(SM_PATH
. 'plugins/compatibility/functions.php');
188 $squirrelmail_plugin_hooks = array();
190 /* On init, register all plugins configured for use. */
191 if (isset($plugins) && is_array($plugins)) {
192 // turn on output buffering in order to prevent output of new lines
194 foreach ($plugins as $name) {
197 // get output and remove whitespace
198 $output = trim(ob_get_contents());
200 // if plugins output more than newlines and spacing, stop script execution.
201 if (!empty($output)) {
207 switch ($sInitLocation) {
208 case 'style': session_write_close(); sqsetcookieflush(); break;
210 $prefs_backend = do_hook_function('prefs_backend');
211 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH
. $prefs_backend)) {
212 require(SM_PATH
. $prefs_backend);
213 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
214 require(SM_PATH
. 'functions/db_prefs.php');
216 require(SM_PATH
. 'functions/prefs.php');
217 require(SM_PATH
. 'functions/file_prefs.php');
221 require(SM_PATH
. 'functions/display_messages.php' );
222 require(SM_PATH
. 'functions/page_header.php');
223 require(SM_PATH
. 'functions/html.php');
226 require(SM_PATH
. 'functions/display_messages.php' );
227 require(SM_PATH
. 'functions/page_header.php');
228 require(SM_PATH
. 'functions/html.php');
229 require(SM_PATH
. 'functions/strings.php');
233 * Check if we are logged in
235 require(SM_PATH
. 'functions/auth.php');
237 if ( !sqsession_is_registered('user_is_logged_in') ) {
238 // First we store some information in the new session to prevent
241 $session_expired_post = $_POST;
242 $session_expired_location = $PHP_SELF;
243 if (!sqsession_is_registered('session_expired_post')) {
244 sqsession_register($session_expired_post,'session_expired_post');
246 if (!sqsession_is_registered('session_expired_location')) {
247 sqsession_register($session_expired_location,'session_expired_location');
249 // signout page will deal with users who aren't logged
250 // in on its own; don't show error here
252 if (strpos($PHP_SELF, 'signout.php') !== FALSE) {
256 set_up_language($squirrelmail_language, true);
257 logout_error( _("You must be logged in to access this page.") );
261 sqgetGlobalVar('username',$username,SQ_SESSION
);
264 * Setting the prefs backend
266 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
267 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
269 if ( !sqsession_is_registered('prefs_are_cached') ||
270 !isset( $prefs_cache) ||
271 !is_array( $prefs_cache)) {
272 $prefs_are_cached = false;
273 $prefs_cache = false; //array();
276 $prefs_backend = do_hook_function('prefs_backend');
277 if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH
. $prefs_backend)) {
278 require(SM_PATH
. $prefs_backend);
279 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
280 require(SM_PATH
. 'functions/db_prefs.php');
282 require(SM_PATH
. 'functions/prefs.php');
283 require(SM_PATH
. 'functions/file_prefs.php');
287 * initializing user settings
289 require(SM_PATH
. 'include/load_prefs.php');
292 // i do not understand the frames language cookie story
294 * We'll need this to later have a noframes version
296 * Check if the user has a language preference, but no cookie.
297 * Send him a cookie with his language preference, if there is
300 $my_language = getPref($data_dir, $username, 'language');
301 if ($my_language != $squirrelmail_language) {
302 sqsetcookie('squirrelmail_language', $my_language, time()+
2592000, $base_uri);
307 * Set up the language.
309 $err=set_up_language(getPref($data_dir, $username, 'language'));
310 /* this is the last cookie we set so flush it. */
313 // Japanese translation used without mbstring support
316 "<p>You need to have PHP installed with the multibyte string function \n".
317 "enabled (using configure option --enable-mbstring).</p>\n".
318 "<p>System assumed that you accidently switched to Japanese translation \n".
319 "and reverted your language preference to English.</p>\n".
320 "<p>Please refresh this page in order to use webmail.</p>\n";
324 $timeZone = getPref($data_dir, $username, 'timezone');
326 /* Check to see if we are allowed to set the TZ environment variable.
327 * We are able to do this if ...
328 * safe_mode is disabled OR
329 * safe_mode_allowed_env_vars is empty (you are allowed to set any) OR
330 * safe_mode_allowed_env_vars contains TZ
332 $tzChangeAllowed = (!ini_get('safe_mode')) ||
333 !strcmp(ini_get('safe_mode_allowed_env_vars'),'') ||
334 preg_match('/^([\w_]+,)*TZ/', ini_get('safe_mode_allowed_env_vars'));
336 if ( $timeZone != SMPREF_NONE
&& ($timeZone != "")
337 && $tzChangeAllowed ) {
339 // get time zone key, if strict or custom strict timezones are used
340 if (isset($time_zone_type) &&
341 ($time_zone_type == 1 ||
$time_zone_type == 3)) {
342 /* load time zone functions */
343 require(SM_PATH
. 'include/timezones.php');
344 $realTimeZone = sq_get_tz_key($timeZone);
346 $realTimeZone = $timeZone;
351 putenv("TZ=".$realTimeZone);
358 * Initialize the template object
360 require(SM_PATH
. 'class/template/template.class.php');
362 * $sTplDir is not initialized when a user is not logged in, so we will use
363 * the config file defaults here. If the neccesary variables are net set,
364 * force a default value.
366 $aTemplateSet = ( !isset($aTemplateSet) ?
array() : $aTemplateSet );
367 $templateset_default = ( !isset($templateset_default) ?
0 : $templateset_default );
369 $sTplDir = ( !isset($aTemplateSet[$templateset_default]['PATH']) ?
370 SM_PATH
. 'templates/default/' :
371 $aTemplateSet[$templateset_default]['PATH'] );
372 $oTemplate = new Template($sTplDir);
375 * Initialize our custom error handler object
377 require(SM_PATH
. 'class/error.class.php');
378 $oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
381 * Activate custom error handling
383 if (version_compare(PHP_VERSION
, "4.3.0", ">=")) {
384 $oldErrorHandler = set_error_handler(array($oErrorHandler, 'SquirrelMailErrorhandler'));
386 $oldErrorHandler = set_error_handler('SquirrelMailErrorhandler');
390 * Javascript support detection function
391 * @param boolean $reset recheck javascript support if set to true.
392 * @return integer SMPREF_JS_ON or SMPREF_JS_OFF ({@see functions/constants.php})
396 function checkForJavascript($reset = FALSE) {
397 global $data_dir, $username, $javascript_on, $javascript_setting;
399 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION
) )
400 return $javascript_on;
402 if ( $reset ||
!isset($javascript_setting) )
403 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT
);
405 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
406 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
407 $js_autodetect_results = SMPREF_JS_OFF
;
409 if ( $javascript_setting == SMPREF_JS_AUTODETECT
)
410 $javascript_on = $js_autodetect_results;
412 $javascript_on = $javascript_setting;
414 sqsession_register($javascript_on, 'javascript_on');
415 return $javascript_on;
418 function sqm_baseuri() {