Fix body onload per FIXME
[squirrelmail.git] / functions / page_header.php
1 <?php
2
3 /**
4 * page_header.php
5 *
6 * Prints the page header (duh)
7 *
8 * @copyright &copy; 1999-2007 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 * @return void
28 */
29 function displayHtmlHeader( $title = 'SquirrelMail', $xtra = '', $do_hook = TRUE, $frames = FALSE ) {
30 global $squirrelmail_language, $sTemplateID, $oErrorHandler, $oTemplate;
31
32 if ( !sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION) ) {
33 global $base_uri;
34 }
35 global $custom_css, $pageheader_sent, $theme, $theme_default, $text_direction,
36 $default_fontset, $chosen_fontset, $default_fontsize, $chosen_fontsize,
37 $chosen_theme, $chosen_theme_path, $user_themes, $user_theme_default;
38
39 /* add no cache headers here */
40 //FIXME: should change all header() calls in SM core to use $oTemplate->header()!!
41 $oTemplate->header('Pragma: no-cache'); // http 1.0 (rfc1945)
42 $oTemplate->header('Cache-Control: private, no-cache, no-store'); // http 1.1 (rfc2616)
43
44 // don't show version as a security measure
45 //$oTemplate->header('X-Powered-By: SquirrelMail/' . SM_VERSION, FALSE);
46 $oTemplate->header('X-Powered-By: SquirrelMail', FALSE);
47
48 $oTemplate->assign('frames', $frames);
49 $oTemplate->assign('lang', $squirrelmail_language);
50
51 $header_tags = '';
52
53 $header_tags .= "<meta name=\"robots\" content=\"noindex,nofollow\" />\n";
54
55 $used_fontset = (!empty($chosen_fontset) ? $chosen_fontset : $default_fontset);
56 $used_fontsize = (!empty($chosen_fontsize) ? $chosen_fontsize : $default_fontsize);
57 $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;
58
59 /**
60 * Stylesheets are loaded in the following order:
61 * 1) All stylesheets provided by the template. Normally, these are
62 * stylsheets in templates/<template>/css/. This is accomplished by calling
63 * $oTemplate->fetch_standard_stylesheet_links().
64 * 2) An optional user-defined stylesheet. This is set in the Display
65 * Preferences.
66 * 3) src/style.php which sets some basic font prefs.
67 * 4) If we are dealing with an RTL language, we load rtl.css from the
68 * template set.
69 */
70
71 // 1. Stylesheets from the template.
72 $header_tags .= $oTemplate->fetch_standard_stylesheet_links();
73
74 $aUserStyles = array();
75
76 // 2. Option user-defined stylesheet from preferences.
77 if (!empty($used_theme) && $used_theme != 'none') {
78 /**
79 * All styles (except "none" - ugh) just point to a directory,
80 * so we need to include all .css files in that directory.
81 */
82 //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?)
83 $styles = $used_theme == 'none' ? array()
84 : list_files($used_theme, '.css');
85 foreach ($styles as $sheet) {
86 $aUserStyles[] = $used_theme .'/'.$sheet;
87 }
88 }
89
90 // 3. src/style.php
91 $aUserStyles[] = $base_uri .'src/style.php?'
92 . (!empty($used_fontset) ? '&amp;fontset='.$used_fontset : '')
93 . (!empty($used_fontsize) ? '&amp;fontsize='.$used_fontsize : '');
94
95 // 3.1. Load the stylesheets we have already
96 $header_tags .= $oTemplate->fetch_external_stylesheet_links($aUserStyles);
97
98 // 4. Optional rtl.css stylesheet
99 if ($text_direction == 'rtl') {
100 $header_tags .= $oTemplate->fetch_right_to_left_stylesheet_link();
101 }
102
103 // 5. Printer friendly stylesheet
104 $header_tags .= create_css_link($base_uri . 'css/print.css', 'printerfriendly', false, 'print');
105
106 if ($squirrelmail_language == 'ja_JP') {
107 /*
108 * force correct detection of charset, when browser does not follow
109 * http content-type and tries to detect charset from page content.
110 * Shooting of browser's creator can't be implemented in php.
111 * We might get rid of it, if we follow http://www.w3.org/TR/japanese-xml/
112 * recommendations and switch to unicode.
113 */
114 $header_tags .= "<!-- \xfd\xfe -->\n";
115 $header_tags .= '<meta http-equiv="Content-type" content="' . $oTemplate->get_content_type() . '; charset=euc-jp" />' . "\n";
116 }
117 if ($do_hook) {
118 // NOTE! plugins here MUST assign output to template
119 // and NOT echo anything directly!! A common
120 // approach is if a plugin decides it needs to
121 // put something at page-top after the standard
122 // SM page header, to dynamically add itself to
123 // the page_header_bottom and/or compose_header_bottom
124 // hooks for the current page request. See
125 // the Sent Confirmation v1.7 or Restrict Senders v1.2
126 // plugins for examples of this approach.
127 ob_start();
128 $temp = array(&$header_tags);
129 do_hook('generic_header', $temp);
130 $output = ob_get_contents();
131 ob_end_clean();
132 // plugin authors can debug their errors with one of the following:
133 //sm_print_r($output);
134 //echo $output;
135 if (!empty($output)) trigger_error('A plugin on the "generic_header" hook has attempted to output directly to the browser', E_USER_ERROR);
136 }
137
138 $header_tags .= $xtra;
139 $oTemplate->assign('page_title', $title);
140
141 /* work around IE6's scrollbar bug */
142 $header_tags .= <<<EOS
143 <!--[if IE 6]>
144 <style type="text/css">
145 /* avoid stupid IE6 bug with frames and scrollbars */
146 body {
147 width: expression(document.documentElement.clientWidth - 30);
148 }
149 </style>
150 <![endif]-->
151
152 EOS;
153
154 $oTemplate->assign('header_tags', $header_tags);
155 $oTemplate->display('protocol_header.tpl');
156
157 /* this is used to check elsewhere whether we should call this function */
158 $pageheader_sent = TRUE;
159 if (isset($oErrorHandler)) {
160 $oErrorHandler->HeaderSent();
161 }
162
163 }
164
165 /**
166 * Given a path to a SquirrelMail file, return a HTML link to it
167 *
168 * @param string path the SquirrelMail file to link to
169 * (should start with something like "src/..." or
170 * "functions/..." or "plugins/..." etc.)
171 * @param string text the link text
172 * @param string target the target frame for this link
173 */
174 function makeInternalLink($path, $text, $target='') {
175 global $base_uri, $oTemplate;
176 // sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
177
178 // This is an inefficient hook and is only used by
179 // one plugin that still needs to patch this code,
180 // plus if we are templat-izing SM, visual hooks
181 // are not needed. However, I am leaving the code
182 // here just in case we find a good (non-visual?)
183 // use for the internal_link hook.
184 //
185 //do_hook('internal_link', $text);
186
187 return create_hyperlink($base_uri . $path, $text, $target);
188 }
189
190 /**
191 * Outputs a complete SquirrelMail page header, starting with <!doctype> and
192 * including the default menu bar. Uses displayHtmlHeader and takes
193 * JavaScript and locale settings into account.
194 *
195 * @param array color the array of theme colors
196 * @param string mailbox the current mailbox name to display
197 * @param string sHeaderJs javascipt code to be inserted in a script block in the header
198 * @param string sOnload JavaScript code to be added inside the body's onload handler
199 * as of 1.5.2, this replaces $sBodyTagJs argument
200 * @return void
201 */
202 function displayPageHeader($color, $mailbox='', $sHeaderJs='', $sOnload = '') {
203
204 global $reply_focus, $hide_sm_attributions, $frame_top,
205 $provider_name, $provider_uri, $startMessage,
206 $action, $oTemplate, $org_title, $base_uri,
207 $data_dir, $username;
208
209 if (empty($sOnload)) {
210 if (strpos($action, 'reply') !== FALSE && $reply_focus) {
211 if ($reply_focus == 'select')
212 $sOnload = 'checkForm(\'select\');';
213 else if ($reply_focus == 'focus')
214 $sOnload = 'checkForm(\'focus\');';
215 else if ($reply_focus != 'none')
216 $sOnload = 'checkForm();';
217 }
218 else
219 $sOnload = 'checkForm();';
220 }
221
222 $startMessage = (int)$startMessage;
223
224 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION );
225
226 if (!isset($frame_top)) {
227 $frame_top = '_top';
228 }
229
230 //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...(?))
231 if( checkForJavascript() || strpos($sHeaderJs, 'new_js_autodetect_results.value') ) {
232 $js_includes = $oTemplate->get_javascript_includes(TRUE);
233 $sJsBlock = '';
234 foreach ($js_includes as $js_file) {
235 $sJsBlock .= '<script src="'.$js_file.'" type="text/javascript"></script>' ."\n";
236 }
237 if ($sHeaderJs) {
238 $sJsBlock .= "\n<script type=\"text/javascript\">" .
239 "\n<!--\n" .
240 $sHeaderJs . "\n\n// -->\n</script>\n";
241 }
242 displayHtmlHeader ($org_title, $sJsBlock);
243 } else {
244 /* do not use JavaScript */
245 displayHtmlHeader ($org_title);
246 $sOnload = '';
247 }
248 if ($mailbox) {
249 /*
250 * this explains the imap_mailbox.php dependency. We should instead store
251 * the selected mailbox in the session and fallback to the session var.
252 */
253 $shortBoxName = htmlspecialchars(imap_utf7_decode_local(
254 readShortMailboxName($mailbox, $delimiter)));
255 if (getPref($data_dir, $username, 'translate_special_folders')) {
256 $shortBoxName = _($shortBoxName);
257 }
258 $urlMailbox = urlencode($mailbox);
259 } else {
260 $shortBoxName = '';
261 $urlMailbox = '';
262 }
263
264 $provider_link = '';
265 if (!empty($provider_uri) && !empty($provider_name) && $provider_name != 'SquirrelMail') {
266 $provider_link = create_hyperlink($provider_uri, $provider_name, '_blank');
267 }
268
269 $oTemplate->assign('onload', $sOnload);
270 $oTemplate->assign('shortBoxName', $shortBoxName);
271 $oTemplate->assign('provider_link', $provider_link);
272 $oTemplate->assign('frame_top', $frame_top);
273 $oTemplate->assign('urlMailbox', $urlMailbox);
274 $oTemplate->assign('startMessage', $startMessage);
275 $oTemplate->assign('hide_sm_attributions', $hide_sm_attributions);
276 $oTemplate->display('page_header.tpl');
277
278 global $null;
279 do_hook('page_header_bottom', $null);
280 }
281
282 /**
283 * Blatantly copied/truncated/modified from displayPageHeader.
284 * Outputs a page header specifically for the compose_in_new popup window
285 *
286 * @param array color the array of theme colors
287 * @param string mailbox the current mailbox name to display
288 * @param string sHeaderJs javascipt code to be inserted in a script block in the header
289 * @param string sOnload JavaScript code to be added inside the body's onload handler
290 * as of 1.5.2, this replaces $sBodyTagJs argument
291 * @return void
292 */
293 function compose_Header($color, $mailbox, $sHeaderJs='', $sOnload = '') {
294
295 global $reply_focus, $action, $oTemplate;
296
297 if (empty($sOnload)) {
298 if (strpos($action, 'reply') !== FALSE && $reply_focus) {
299 if ($reply_focus == 'select')
300 $sOnload = 'checkForm(\'select\');';
301 else if ($reply_focus == 'focus')
302 $sOnload = 'checkForm(\'focus\');';
303 else if ($reply_focus != 'none')
304 $sOnload = 'checkForm();';
305 }
306 else
307 $sOnload = 'checkForm();';
308 }
309
310
311 /*
312 * Locate the first displayable form element (only when JavaScript on)
313 */
314 if(checkForJavascript()) {
315 if ($sHeaderJs) {
316 $sJsBlock = "\n<script type=\"text/javascript\">" .
317 "\n<!--\n" .
318 $sHeaderJs . "\n\n// -->\n</script>\n";
319 } else {
320 $sJsBlock = '';
321 }
322 $sJsBlock .= "\n";
323
324 $js_includes = $oTemplate->get_javascript_includes(TRUE);
325 foreach ($js_includes as $js_file) {
326 $sJsBlock .= '<script src="'.$js_file.'" type="text/javascript"></script>' ."\n";
327 }
328
329 displayHtmlHeader (_("Compose"), $sJsBlock);
330 } else {
331 /* javascript off */
332 displayHtmlHeader(_("Compose"));
333 $sOnload = '';
334 }
335
336 // FIXME: change the colorization attributes below to a CSS class!
337 $class = '';
338 $aAttribs = array('text' => $color[8], 'bgcolor' => $color[4],
339 'link' => $color[7], 'vlink' => $color[7],
340 'alink' => $color[7]);
341
342 // this is template-safe (see create_body() function)
343 echo create_body($sOnload, $class, $aAttribs);
344
345 global $null;
346 do_hook('compose_header_bottom', $null);
347 }