fsf changes, meant to be rebased on upstream
[squirrelmail.git] / functions / page_header.php
1 <?php
2
3 /**
4 * page_header.php
5 *
6 * Prints the page header (duh)
7 *
8 * @copyright 1999-2021 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 /** Include required files from SM */
15 include_once(SM_PATH . 'functions/imap_mailbox.php');
16
17 /**
18 * Output a SquirrelMail page header, from <!doctype> to </head>
19 * Always set up the language before calling these functions.
20 *
21 * Since 1.5.1 function sends http headers. Function should be called
22 * before any output is started.
23 * @param string title the page title, default SquirrelMail.
24 * @param string xtra extra HTML to insert into the header
25 * @param bool do_hook whether to execute hooks, default true
26 * @param bool frames generate html frameset doctype (since 1.5.1)
27 * @param bool $browser_cache_ok When TRUE, it's OK to leave out the
28 * no-cache browser headers (OPTIONAL;
29 * default = FALSE, send no-cache headers)
30 * @return void
31 */
32 function displayHtmlHeader( $title = 'SquirrelMail', $xtra = '', $do_hook = TRUE, $frames = FALSE, $browser_cache_ok=FALSE ) {
33 global $squirrelmail_language, $sTemplateID, $oErrorHandler, $oTemplate;
34
35 if ( !sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION) ) {
36 global $base_uri;
37 }
38 global $custom_css, $pageheader_sent, $theme, $theme_default, $text_direction,
39 $default_fontset, $chosen_fontset, $default_fontsize, $chosen_fontsize,
40 $chosen_theme, $chosen_theme_path, $user_themes, $user_theme_default,
41 $head_tag_extra;
42
43 // add no cache headers here
44 //
45 if (!$browser_cache_ok) {
46 //FIXME: should change all header() calls in SM core to use $oTemplate->header()!!
47 $oTemplate->header('Pragma: no-cache'); // http 1.0 (rfc1945)
48 $oTemplate->header('Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0'); // http 1.1 (rfc2616)
49 $oTemplate->header('Expires: Sat, 1 Jan 2000 00:00:00 GMT');
50 //TODO: is this needed? $oTemplate->header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
51 }
52 /* prevent information leakage about read emails by forbidding Firefox
53 * to do preemptive DNS requests for any links in the message body. */
54 $oTemplate->header('X-DNS-Prefetch-Control: off');
55
56 // don't show version as a security measure
57 //$oTemplate->header('X-Powered-By: SquirrelMail/' . SM_VERSION, FALSE);
58 $oTemplate->header('X-Powered-By: SquirrelMail', FALSE);
59
60 // prevent clickjack attempts
61 // FIXME: should we use DENY instead? We can also make this a configurable value, including giving the admin the option of removing this entirely in case they WANT to be framed by an external domain
62 $oTemplate->header('X-Frame-Options: SAMEORIGIN');
63
64 // prevent clickjack attempts using JavaScript for browsers that
65 // don't support the X-Frame-Options header...
66 // we check to see if we are *not* the top page, and if not, check
67 // whether or not the top page is in the same domain as we are...
68 // if not, log out immediately -- this is an attempt to do the same
69 // thing that the X-Frame-Options does using JavaScript (never a good
70 // idea to rely on JavaScript-based solutions, though)
71 //FIXME: is it a problem that we still force the clickjack protection code whether or not JavaScript is supported or desired by the user?
72 $header_tags = '<script type="text/javascript" language="JavaScript">'
73 . "\n<!--\n"
74 . 'if (self != top) { try { if (document.domain != top.document.domain) {'
75 . ' throw "Clickjacking security violation! Please log out immediately!"; /* this code should never execute - exception should already have been thrown since it\'s a security violation in this case to even try to access top.document.domain (but it\'s left here just to be extra safe) */ } } catch (e) { self.location = "'
76 . sqm_baseuri() . 'src/signout.php"; top.location = "'
77 . sqm_baseuri() . 'src/signout.php" } }'
78 . "\n// -->\n</script>\n";
79
80 $oTemplate->assign('frames', $frames);
81 $oTemplate->assign('lang', $squirrelmail_language);
82
83 $header_tags .= "<meta name=\"robots\" content=\"noindex,nofollow\" />\n"
84
85 // For adding a favicon or anything else that should be inserted in *ALL* <head> for *ALL* documents,
86 // define $head_tag_extra in config/config_local.php
87 // The string "###SM BASEURI###" will be replaced with the base URI for this SquirrelMail installation.
88 // When not defined, a default is provided that displays the default favicon.ico.
89 // If you override this and still want to use the default favicon.ico, you'll have to include the following
90 // following in your $head_tag_extra string:
91 // $head_tag_extra = '<link rel="shortcut icon" href="###SM BASEURI###favicon.ico" />...<YOUR CONTENT HERE>...';
92 //
93 . (empty($head_tag_extra) ? '<link rel="shortcut icon" href="' . sqm_baseuri() . 'favicon.ico" />'
94 : str_replace('###SM BASEURI###', sqm_baseuri(), $head_tag_extra));
95
96 $used_fontset = (!empty($chosen_fontset) ? $chosen_fontset : $default_fontset);
97 $used_fontsize = (!empty($chosen_fontsize) ? $chosen_fontsize : $default_fontsize);
98 $used_theme = !isset($chosen_theme) && $user_theme_default != 'none' && is_dir($chosen_theme) && is_readable($chosen_theme)? $user_themes[$user_theme_default]['PATH'].'/default.css' : $chosen_theme_path;
99
100 /**
101 * Stylesheets are loaded in the following order:
102 * 1) All stylesheets provided by the template. Normally, these are
103 * stylsheets in templates/<template>/css/. This is accomplished by calling
104 * $oTemplate->fetch_standard_stylesheet_links().
105 * 2) An optional user-defined stylesheet. This is set in the Display
106 * Preferences.
107 * 3) src/style.php which sets some basic font prefs.
108 * 4) If we are dealing with an RTL language, we load rtl.css from the
109 * template set.
110 */
111
112 // 1. Stylesheets from the template.
113 $header_tags .= $oTemplate->fetch_standard_stylesheet_links();
114
115 $aUserStyles = array();
116
117 // 2. Option user-defined stylesheet from preferences.
118 if (!empty($used_theme) && $used_theme != 'none') {
119 /**
120 * All styles (except "none" - ugh) just point to a directory,
121 * so we need to include all .css files in that directory.
122 */
123 //FIXME: rid ourselves of "none" strings! I didn't do it here because I think the problem is that the theme itself should never be "none" (? well, what else would it be? if "none" theme is actually OK, then is there a constant to use below in stead of a hard-coded string?)
124 $styles = $used_theme == 'none' ? array()
125 : list_files($used_theme, '.css');
126 foreach ($styles as $sheet) {
127 $aUserStyles[] = $used_theme .'/'.$sheet;
128 }
129 }
130
131 // 3. src/style.php
132 $aUserStyles[] = $base_uri .'src/style.php?'
133 . (!empty($used_fontset) ? '&amp;fontset='.$used_fontset : '')
134 . (!empty($used_fontsize) ? '&amp;fontsize='.$used_fontsize : '');
135
136 // 3.1. Load the stylesheets we have already
137 $header_tags .= $oTemplate->fetch_external_stylesheet_links($aUserStyles);
138
139 // 4. Optional rtl.css stylesheet
140 if ($text_direction == 'rtl') {
141 $header_tags .= $oTemplate->fetch_right_to_left_stylesheet_link();
142 }
143
144 // 5. Printer friendly stylesheet
145 $header_tags .= create_css_link($base_uri . 'css/print.css', 'printerfriendly', false, 'print');
146
147 if ($squirrelmail_language == 'ja_JP') {
148 /*
149 * force correct detection of charset, when browser does not follow
150 * http content-type and tries to detect charset from page content.
151 * Shooting of browser's creator can't be implemented in php.
152 * We might get rid of it, if we follow http://www.w3.org/TR/japanese-xml/
153 * recommendations and switch to unicode.
154 */
155 $header_tags .= "<!-- \xfd\xfe -->\n";
156 $header_tags .= '<meta http-equiv="Content-type" content="' . $oTemplate->get_content_type() . '; charset=euc-jp" />' . "\n";
157 }
158 if ($do_hook) {
159 // NOTE! plugins here MUST assign output to template
160 // and NOT echo anything directly!! A common
161 // approach is if a plugin decides it needs to
162 // put something at page-top after the standard
163 // SM page header, to dynamically add itself to
164 // the page_header_bottom and/or compose_header_bottom
165 // hooks for the current page request. See
166 // the Sent Confirmation v1.7 or Restrict Senders v1.2
167 // plugins for examples of this approach.
168 ob_start();
169 $temp = array(&$header_tags);
170 do_hook('generic_header', $temp);
171 $output = ob_get_contents();
172 ob_end_clean();
173 // plugin authors can debug their errors with one of the following:
174 //sm_print_r($output);
175 //echo $output;
176 if (!empty($output)) trigger_error('A plugin on the "generic_header" hook has attempted to output directly to the browser', E_USER_ERROR);
177 }
178
179 $header_tags .= $xtra;
180 $oTemplate->assign('page_title', $title);
181
182 /* work around IE6's scrollbar bug */
183 $header_tags .= <<<EOS
184 <!--[if IE 6]>
185 <style type="text/css">
186 /* avoid stupid IE6 bug with frames and scrollbars */
187 body {
188 width: expression(document.documentElement.clientWidth - 30);
189 }
190 </style>
191 <![endif]-->
192
193 EOS;
194
195 $oTemplate->assign('header_tags', $header_tags);
196 $oTemplate->display('protocol_header.tpl');
197
198 /* this is used to check elsewhere whether we should call this function */
199 $pageheader_sent = TRUE;
200 if (isset($oErrorHandler)) {
201 $oErrorHandler->HeaderSent();
202 }
203
204 }
205
206 /**
207 * Given a path to a SquirrelMail file, return a HTML link to it
208 *
209 * @param string $path The SquirrelMail file to link to
210 * (should start with something like "src/..." or
211 * "functions/..." or "plugins/..." etc.)
212 * @param string $text The link text
213 * @param string $target The target frame for this link
214 * @param string $accesskey The access key to be used, if any
215 */
216 function makeInternalLink($path, $text, $target='', $accesskey='NONE') {
217 global $base_uri, $oTemplate;
218 // sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
219
220 // This is an inefficient hook and is only used by
221 // one plugin that still needs to patch this code,
222 // plus if we are templat-izing SM, visual hooks
223 // are not needed. However, I am leaving the code
224 // here just in case we find a good (non-visual?)
225 // use for the internal_link hook.
226 //
227 //do_hook('internal_link', $text);
228
229 return create_hyperlink($base_uri . $path, $text, $target,
230 '', '', '', '',
231 ($accesskey == 'NONE'
232 ? array()
233 : array('accesskey' => $accesskey)));
234 }
235
236 /**
237 * Outputs a complete SquirrelMail page header, starting with <!doctype> and
238 * including the default menu bar. Uses displayHtmlHeader and takes
239 * JavaScript and locale settings into account.
240 *
241 * @param array color the array of theme colors
242 * @param string mailbox the current mailbox name to display
243 * @param string sHeaderJs javascipt code to be inserted in a script block in the header
244 * @param string sOnload JavaScript code to be added inside the body's onload handler
245 * as of 1.5.2, this replaces $sBodyTagJs argument
246 * @return void
247 */
248 function displayPageHeader($color, $mailbox='', $sHeaderJs='', $sOnload = '') {
249
250 global $reply_focus, $hide_sm_attributions, $frame_top,
251 $provider_name, $provider_uri, $startMessage,
252 $action, $oTemplate, $org_title, $base_uri,
253 $data_dir, $username;
254
255 if (empty($sOnload)) {
256 if (strpos($action, 'reply') !== FALSE && $reply_focus) {
257 if ($reply_focus == 'select')
258 $sOnload = 'checkForm(\'select\');';
259 else if ($reply_focus == 'focus')
260 $sOnload = 'checkForm(\'focus\');';
261 else if ($reply_focus != 'none')
262 $sOnload = 'checkForm();';
263 }
264 else
265 $sOnload = 'checkForm();';
266 }
267
268 $startMessage = (int)$startMessage;
269
270 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION );
271
272 if (!isset($frame_top)) {
273 $frame_top = '_top';
274 }
275
276 //FIXME: does checkForJavascript() make the 2nd part of the if() below unneccessary?? (that is, I think checkForJavascript() might already look for new_js_autodetect_results...(?))
277 if( checkForJavascript() || strpos($sHeaderJs, 'new_js_autodetect_results.value') ) {
278 $js_includes = $oTemplate->get_javascript_includes(TRUE);
279 $sJsBlock = '';
280 foreach ($js_includes as $js_file) {
281 $sJsBlock .= '<script src="'.$js_file.'" type="text/javascript"></script>' ."\n";
282 }
283 if ($sHeaderJs) {
284 $sJsBlock .= "\n<script type=\"text/javascript\">" .
285 "\n<!--\n" .
286 $sHeaderJs . "\n\n// -->\n</script>\n";
287 }
288 displayHtmlHeader ($org_title, $sJsBlock);
289 } else {
290 /* do not use JavaScript */
291 displayHtmlHeader ($org_title);
292 $sOnload = '';
293 }
294 if ($mailbox) {
295 /*
296 * this explains the imap_mailbox.php dependency. We should instead store
297 * the selected mailbox in the session and fallback to the session var.
298 */
299 $shortBoxName = sm_encode_html_special_chars(imap_utf7_decode_local(
300 readShortMailboxName($mailbox, $delimiter)));
301 if (getPref($data_dir, $username, 'translate_special_folders')) {
302 global $sent_folder, $trash_folder, $draft_folder;
303 if ($mailbox == $sent_folder)
304 $shortBoxName = _("Sent");
305 else if ($mailbox == $trash_folder)
306 $shortBoxName = _("Trash");
307 else if ($mailbox == $sent_folder)
308 $shortBoxName = _("Drafts");
309 }
310 $urlMailbox = urlencode($mailbox);
311 } else {
312 $shortBoxName = '';
313 $urlMailbox = '';
314 }
315
316 $provider_link = '';
317 if (!empty($provider_uri) && !empty($provider_name) && $provider_name != 'SquirrelMail') {
318 $provider_link = create_hyperlink($provider_uri, $provider_name, '_blank');
319 }
320
321 $oTemplate->assign('onload', $sOnload);
322 $oTemplate->assign('shortBoxName', $shortBoxName);
323 $oTemplate->assign('provider_link', $provider_link);
324 $oTemplate->assign('frame_top', $frame_top);
325 $oTemplate->assign('urlMailbox', $urlMailbox);
326 $oTemplate->assign('startMessage', $startMessage);
327 $oTemplate->assign('hide_sm_attributions', $hide_sm_attributions);
328
329 // access keys
330 //
331 global $accesskey_menubar_compose, $accesskey_menubar_addresses,
332 $accesskey_menubar_folders, $accesskey_menubar_options,
333 $accesskey_menubar_search, $accesskey_menubar_help,
334 $accesskey_menubar_signout;
335 $oTemplate->assign('accesskey_menubar_compose', $accesskey_menubar_compose);
336 $oTemplate->assign('accesskey_menubar_addresses', $accesskey_menubar_addresses);
337 $oTemplate->assign('accesskey_menubar_folders', $accesskey_menubar_folders);
338 $oTemplate->assign('accesskey_menubar_options', $accesskey_menubar_options);
339 $oTemplate->assign('accesskey_menubar_search', $accesskey_menubar_search);
340 $oTemplate->assign('accesskey_menubar_help', $accesskey_menubar_help);
341 $oTemplate->assign('accesskey_menubar_signout', $accesskey_menubar_signout);
342
343 $oTemplate->display('page_header.tpl');
344
345 global $null;
346 do_hook('page_header_bottom', $null);
347 }
348
349 /**
350 * Blatantly copied/truncated/modified from displayPageHeader.
351 * Outputs a page header specifically for the compose_in_new popup window
352 *
353 * @param array color the array of theme colors
354 * @param string mailbox the current mailbox name to display
355 * @param string sHeaderJs javascipt code to be inserted in a script block in the header
356 * @param string sOnload JavaScript code to be added inside the body's onload handler
357 * as of 1.5.2, this replaces $sBodyTagJs argument
358 * @return void
359 */
360 function compose_Header($color, $mailbox, $sHeaderJs='', $sOnload = '') {
361
362 global $reply_focus, $action, $oTemplate;
363
364 if (empty($sOnload)) {
365 if (strpos($action, 'reply') !== FALSE && $reply_focus) {
366 if ($reply_focus == 'select')
367 $sOnload = 'checkForm(\'select\');';
368 else if ($reply_focus == 'focus')
369 $sOnload = 'checkForm(\'focus\');';
370 else if ($reply_focus != 'none')
371 $sOnload = 'checkForm();';
372 }
373 else
374 $sOnload = 'checkForm();';
375 }
376
377
378 /*
379 * Locate the first displayable form element (only when JavaScript on)
380 */
381 if(checkForJavascript()) {
382 if ($sHeaderJs) {
383 $sJsBlock = "\n<script type=\"text/javascript\">" .
384 "\n<!--\n" .
385 $sHeaderJs . "\n\n// -->\n</script>\n";
386 } else {
387 $sJsBlock = '';
388 }
389 $sJsBlock .= "\n";
390
391 $js_includes = $oTemplate->get_javascript_includes(TRUE);
392 foreach ($js_includes as $js_file) {
393 $sJsBlock .= '<script src="'.$js_file.'" type="text/javascript"></script>' ."\n";
394 }
395
396 displayHtmlHeader (_("Compose"), $sJsBlock);
397 } else {
398 /* javascript off */
399 displayHtmlHeader(_("Compose"));
400 $sOnload = '';
401 }
402
403 // FIXME: change the colorization attributes below to a CSS class!
404 $class = '';
405 $aAttribs = array('text' => $color[8], 'bgcolor' => $color[4],
406 'link' => $color[7], 'vlink' => $color[7],
407 'alink' => $color[7]);
408
409 // this is template-safe (see create_body() function)
410 echo create_body($sOnload, $class, $aAttribs);
411
412 global $null;
413 do_hook('compose_header_bottom', $null);
414 }