Centralized init
[squirrelmail.git] / include / init.php
CommitLineData
202bcbcc 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 * SquirrelMail version number -- DO NOT CHANGE
16 */
17
18$version = '1.5.2 [CVS]';
19
20/**
21 * SquirrelMail internal version number -- DO NOT CHANGE
22 * $sm_internal_version = array (release, major, minor)
23 */
24
25$SQM_INTERNAL_VERSION = array(1,5,2);
26
27/**
28 * This is a development version so in order to track programmer mistakes we
29 * set the error reporting to E_ALL
30 */
31
32error_reporting(E_ALL);
33
34
35/**
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"
39 *
40 */
41if (isset($_SERVER['SCRIPT_NAME'])) {
42 $a = explode('/',$_SERVER['SCRIPT_NAME']);
43} elseif (isset($HTTP_SERVER_VARS['SCRIPT_NAME'])) {
44 $a = explode('/',$_SERVER['SCRIPT_NAME']);
45}
46$sSM_PATH = '';
47for($i = count($a) -2;$i > -1; --$i) {
48 $sSM_PATH .= '../';
49 if ($a[$i] === 'src' || $a[$i] === 'plugins') {
50 break;
51 }
52}
53
54$base_uri = implode('/',array_slice($a,0,$i)). '/';
55
56
57
58define('SM_PATH',$sSM_PATH);
59
60/**
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
65 * in another file.
66 */
67$bInit = true;
68
69require(SM_PATH . 'functions/global.php');
70require(SM_PATH . 'config/config.php');
71require(SM_PATH . 'functions/plugin.php');
72require(SM_PATH . 'include/constants.php');
73require(SM_PATH . 'include/languages.php');
74
75/**
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.
80 */
81ini_set('magic_quotes_runtime','0');
82
83
84/* if running with magic_quotes_gpc then strip the slashes
85 from POST and GET global arrays */
86if (get_magic_quotes_gpc()) {
87 sqstripslashes($_GET);
88 sqstripslashes($_POST);
89}
90
91/**
92 * If register_globals are on, unregister globals.
93 * Code requires PHP 4.1.0 or newer.
94 */
95if ((bool) @ini_get('register_globals')) {
96 /**
97 * Remove all globals from $_GET, $_POST, and $_COOKIE.
98 */
99 foreach ($_REQUEST as $key => $value) {
100 unset($GLOBALS[$key]);
101 }
102 /**
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
106 * on PHP version.
107 */
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']);
114 }
115 /**
116 * Remove globalized environment variables.
117 */
118 foreach ($_ENV as $key => $value) {
119 unset($GLOBALS[$key]);
120 }
121 /**
122 * Remove globalized server variables.
123 */
124 foreach ($_SERVER as $key => $value) {
125 unset($GLOBALS[$key]);
126 }
127}
128
129/* strip any tags added to the url from PHP_SELF.
130This 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']);
133
134$PHP_SELF = php_self();
135
136/**
137 * Initialize the session
138 */
139
140/**
141 * includes from classes stored in the session
142 */
143require(SM_PATH . 'class/mime.class.php');
144
145/** set the name of the session cookie */
146if (!isset($session_name) || !$session_name) {
147 $session_name = 'SQMSESSID';
148}
149ini_set('session.name' , $session_name);
150session_set_cookie_params (0, $base_uri);
151sqsession_is_active();
152
153sqsession_register($base_uri, 'base_uri');
154
155/**
156 * Remove globalized session data in rg=on setups
157 */
158if ((bool) @ini_get('register_globals')) {
159 foreach ($_SESSION as $key => $value) {
160 unset($GLOBALS[$key]);
161 }
162}
163/**
164 * Retrieve the language cookie
165 */
166if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) {
167 $squirrelmail_language = '';
168}
169
170
171/**
172 * @var $sInitlocation From where do we include.
173 */
174if (!isset($sInitLocation)) {
175 $sInitLocation=NULL;
176}
177
178/**
179 * MAIN PLUGIN LOADING CODE HERE
180 */
181
182/**
183 * Include Compatibility plugin if available.
184 */
185if (file_exists(SM_PATH . 'plugins/compatibility/functions.php'))
186 include_once(SM_PATH . 'plugins/compatibility/functions.php');
187$squirrelmail_plugin_hooks = array();
188
189/* On init, register all plugins configured for use. */
190if (isset($plugins) && is_array($plugins)) {
191 // turn on output buffering in order to prevent output of new lines
192 ob_start();
193 foreach ($plugins as $name) {
194 use_plugin($name);
195 }
196 // get output and remove whitespace
197 $output = trim(ob_get_contents());
198 ob_end_clean();
199 // if plugins output more than newlines and spacing, stop script execution.
200 if (!empty($output)) {
201 die($output);
202 }
203}
204
205
206switch ($sInitLocation) {
207 case 'style': session_write_close(); sqsetcookieflush(); break;
208 case 'redirect':
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');
214 } else {
215 require(SM_PATH . 'functions/prefs.php');
216 require(SM_PATH . 'functions/file_prefs.php');
217 }
218 //nobreak;
219 case 'login':
220 require(SM_PATH . 'functions/display_messages.php' );
221 require(SM_PATH . 'functions/page_header.php');
222 require(SM_PATH . 'functions/html.php');
223 break;
224 default:
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');
229
230
231 /**
232 * Check if we are logged in
233 */
234 require(SM_PATH . 'functions/auth.php');
235
236 if ( !sqsession_is_registered('user_is_logged_in') ) {
237 // First we store some information in the new session to prevent
238 // information-loss.
239 //
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');
244 }
245 if (!sqsession_is_registered('session_expired_location')) {
246 sqsession_register($session_expired_location,'session_expired_location');
247 }
248 // signout page will deal with users who aren't logged
249 // in on its own; don't show error here
250 //
251 if (strpos($PHP_SELF, 'signout.php') !== FALSE) {
252 return;
253 }
254
255 set_up_language($squirrelmail_language, true);
256 logout_error( _("You must be logged in to access this page.") );
257 exit;
258 }
259
260 sqgetGlobalVar('username',$username,SQ_SESSION);
261
262 /**
263 * Setting the prefs backend
264 */
265 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
266 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
267
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();
273 }
274
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');
280 } else {
281 require(SM_PATH . 'functions/prefs.php');
282 require(SM_PATH . 'functions/file_prefs.php');
283 }
284
285 /**
286 * initializing user settings
287 */
288 require(SM_PATH . 'include/load_prefs.php');
289
290
291// i do not understand the frames language cookie story
292 /**
293 * We'll need this to later have a noframes version
294 *
295 * Check if the user has a language preference, but no cookie.
296 * Send him a cookie with his language preference, if there is
297 * such discrepancy.
298 */
299 $my_language = getPref($data_dir, $username, 'language');
300 if ($my_language != $squirrelmail_language) {
301 sqsetcookie('squirrelmail_language', $my_language, time()+2592000, $base_uri);
302 }
303// /dont understand
304
305 /**
306 * Set up the language.
307 */
308 $err=set_up_language(getPref($data_dir, $username, 'language'));
309 /* this is the last cookie we set so flush it. */
310 sqsetcookieflush();
311
312 // Japanese translation used without mbstring support
313 if ($err==2) {
314 $sError =
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";
320 error_box($sError);
321 }
322
323 $timeZone = getPref($data_dir, $username, 'timezone');
324
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
330 */
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'));
334
335 if ( $timeZone != SMPREF_NONE && ($timeZone != "")
336 && $tzChangeAllowed ) {
337
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);
344 } else {
345 $realTimeZone = $timeZone;
346 }
347
348 // set time zone
349 if ($realTimeZone) {
350 putenv("TZ=".$realTimeZone);
351 }
352 }
353 break;
354}
355
356/**
357 * Initialize the template object
358 */
359require(SM_PATH . 'class/template/template.class.php');
360/*
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.
364 */
365$aTemplateSet = ( !isset($aTemplateSet) ? array() : $aTemplateSet );
366$templateset_default = ( !isset($templateset_default) ? 0 : $templateset_default );
367
368$sTplDir = ( !isset($aTemplateSet[$templateset_default]['PATH']) ?
369 SM_PATH . 'templates/default/' :
370 $aTemplateSet[$templateset_default]['PATH'] );
371$oTemplate = new Template($sTplDir);
372
373/**
374 * Initialize our custom error handler object
375 */
376require(SM_PATH . 'class/error.class.php');
377$oErrorHandler = new ErrorHandler($oTemplate,'error_message.tpl');
378
379/**
380 * Activate custom error handling
381 */
382if (version_compare(PHP_VERSION, "4.3.0", ">=")) {
383 $oldErrorHandler = set_error_handler(array($oErrorHandler, 'SquirrelMailErrorhandler'));
384} else {
385 $oldErrorHandler = set_error_handler('SquirrelMailErrorhandler');
386}
387
388/**
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})
392 * @since 1.5.1
393 */
394
395function checkForJavascript($reset = FALSE) {
396 global $data_dir, $username, $javascript_on, $javascript_setting;
397
398 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
399 return $javascript_on;
400
401 if ( $reset || !isset($javascript_setting) )
402 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
403
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;
407
408 if ( $javascript_setting == SMPREF_JS_AUTODETECT )
409 $javascript_on = $js_autodetect_results;
410 else
411 $javascript_on = $javascript_setting;
412
413 sqsession_register($javascript_on, 'javascript_on');
414 return $javascript_on;
415}
416
417function sqm_baseuri() {
418 global $base_uri;
419 return $base_uri;
420}