Refactoring JavaScript, reduces bandwidth usage on message list when fancy highlighti...
[squirrelmail.git] / functions / page_header.php
1 <?php
2
3 /**
4 * page_header.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Prints the page header (duh)
10 *
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /** Include required files from SM */
16 require_once(SM_PATH . 'functions/strings.php');
17 require_once(SM_PATH . 'functions/html.php');
18 require_once(SM_PATH . 'functions/imap_mailbox.php');
19 require_once(SM_PATH . 'functions/global.php');
20
21 /**
22 * Output a SquirrelMail page header, from <!doctype> to </head>
23 * Always set up the language before calling these functions.
24 *
25 * @param string title the page title, default SquirrelMail.
26 * @param string xtra extra HTML to insert into the header
27 * @param bool do_hook whether to execute hooks, default true
28 * @param bool frames generate html frameset doctype (since 1.5.1)
29 * @return void
30 */
31 function displayHtmlHeader( $title = 'SquirrelMail', $xtra = '', $do_hook = true, $frames = false ) {
32 global $squirrelmail_language;
33
34 if ( !sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION) ) {
35 global $base_uri;
36 }
37 global $theme_css, $custom_css, $pageheader_sent;
38
39 if ($frames) {
40 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">';
41 } else {
42 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
43 }
44 echo "\n\n" . html_tag( 'html' ,'' , '', '', 'lang="'.$squirrelmail_language.'"' ) . "\n<head>\n";
45
46 /*
47 * Add closing / to link and meta elements only after switching to xhtml 1.0 Transitional.
48 * It is not compatible with html 4.01 Transitional
49 */
50 if ( !isset( $custom_css ) || $custom_css == 'none' ) {
51 if ($theme_css != '') {
52 echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$theme_css\">";
53 }
54 } else {
55 echo '<link rel="stylesheet" type="text/css" href="' .
56 $base_uri . 'themes/css/'.$custom_css.'">';
57 }
58
59 if ($squirrelmail_language == 'ja_JP') {
60 /*
61 * force correct detection of charset, when browser does not follow
62 * http content-type and tries to detect charset from page content.
63 * Shooting of browser's creator can't be implemented in php.
64 * We might get rid of it, if we follow http://www.w3.org/TR/japanese-xml/
65 * recommendations and switch to unicode.
66 */
67 echo "<!-- \xfd\xfe -->\n";
68 echo '<meta http-equiv="Content-type" content="text/html; charset=euc-jp">' . "\n";
69 }
70
71 if ($do_hook) {
72 do_hook('generic_header');
73 }
74
75 echo "\n<title>$title</title>$xtra\n";
76
77 /* work around IE6's scrollbar bug */
78 echo <<<ECHO
79 <style type="text/css">
80 <!--
81 /* avoid stupid IE6 bug with frames and scrollbars */
82 body {
83 voice-family: "\"}\"";
84 voice-family: inherit;
85 width: expression(document.documentElement.clientWidth - 30);
86 }
87 -->
88 </style>
89
90 ECHO;
91
92 echo "\n</head>\n\n";
93
94 /* this is used to check elsewhere whether we should call this function */
95 $pageheader_sent = TRUE;
96 }
97
98 /**
99 * Given a path to a SquirrelMail file, return a HTML link to it
100 *
101 * @param string path the SquirrelMail file to link to
102 * @param string text the link text
103 * @param string target the target frame for this link
104 */
105 function makeInternalLink($path, $text, $target='') {
106 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
107 if ($target != '') {
108 $target = " target=\"$target\"";
109 }
110
111 // This is an inefficient hook and is only used by
112 // one plugin that still needs to patch this code,
113 // plus if we are templat-izing SM, visual hooks
114 // are not needed. However, I am leaving the code
115 // here just in case we find a good (non-visual?)
116 // use for the internal_link hook.
117 //
118 //$hooktext = do_hook_function('internal_link',$text);
119 //if ($hooktext != '')
120 // $text = $hooktext;
121
122 return '<a href="'.$base_uri.$path.'"'.$target.'>'.$text.'</a>';
123 }
124
125 /**
126 * Same as makeInternalLink, but echoes it too
127 */
128 function displayInternalLink($path, $text, $target='') {
129 echo makeInternalLink($path, $text, $target);
130 }
131
132 /**
133 * Outputs a complete SquirrelMail page header, starting with <!doctype> and
134 * including the default menu bar. Uses displayHtmlHeader and takes
135 * JavaScript and locale settings into account.
136 *
137 * @param array color the array of theme colors
138 * @param string mailbox the current mailbox name to display
139 * @param string xtra extra html code to add
140 * @param bool session
141 * @return void
142 */
143 function displayPageHeader($color, $mailbox, $xtra='', $session=false) {
144
145 global $hide_sm_attributions, $PHP_SELF, $frame_top,
146 $compose_new_win, $compose_width, $compose_height,
147 $provider_name, $provider_uri, $startMessage,
148 $javascript_on, $default_use_mdn, $mdn_user_support;
149
150 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION );
151 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION );
152 $module = substr( $PHP_SELF, ( strlen( $PHP_SELF ) - strlen( $base_uri ) ) * -1 );
153 if ($qmark = strpos($module, '?')) {
154 $module = substr($module, 0, $qmark);
155 }
156 if (!isset($frame_top)) {
157 $frame_top = '_top';
158 }
159
160 if ($session) {
161 $compose_uri = $base_uri.'src/compose.php?mailbox='.urlencode($mailbox).'&amp;attachedmessages=true&amp;session='."$session";
162 } else {
163 $compose_uri = $base_uri.'src/compose.php?newmessage=1';
164 $session = 0;
165 }
166
167 if( $javascript_on || strpos($xtra, 'new_js_autodetect_results.value') ) {
168
169 switch ( $module ) {
170 case 'src/read_body.php':
171 $js ='';
172
173 // compose in new window code
174 if ($compose_new_win == '1') {
175 if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
176 $compose_width = '640';
177 }
178 if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
179 $compose_height = '550';
180 }
181 $js .= "function comp_in_new_form(comp_uri, button, myform) {\n".
182 ' if (!comp_uri) {'."\n".
183 ' comp_uri = "'.$compose_uri."\";\n".
184 ' }'. "\n".
185 ' comp_uri += "&" + button.name + "=1";'."\n".
186 ' for ( var i=0; i < myform.elements.length; i++ ) {'."\n".
187 ' if ( myform.elements[i].type == "checkbox" && myform.elements[i].checked )'."\n".
188 ' comp_uri += "&" + myform.elements[i].name + "=1";'."\n".
189 ' }'."\n".
190 ' var newwin = window.open(comp_uri' .
191 ', "_blank",'.
192 '"width='.$compose_width. ',height='.$compose_height.
193 ',scrollbars=yes,resizable=yes,status=yes");'."\n".
194 "}\n\n";
195 $js .= "function comp_in_new(comp_uri) {\n".
196 " if (!comp_uri) {\n".
197 ' comp_uri = "'.$compose_uri."\";\n".
198 ' }'. "\n".
199 ' var newwin = window.open(comp_uri' .
200 ', "_blank",'.
201 '"width='.$compose_width. ',height='.$compose_height.
202 ',scrollbars=yes,resizable=yes,status=yes");'."\n".
203 "}\n\n";
204 }
205
206 // javascript for sending read receipts
207 if($default_use_mdn && $mdn_user_support) {
208 $js .= 'function sendMDN() {'."\n".
209 " mdnuri=window.location+'&sendreceipt=1'; ".
210 "var newwin = window.open(mdnuri,'right');".
211 "\n}\n\n";
212 }
213
214 // if any of the above passes, add the JS tags too.
215 if($js) {
216 $js = "\n".'<script language="JavaScript" type="text/javascript">' .
217 "\n<!--\n" . $js . "// -->\n</script>\n";
218 }
219
220 displayHtmlHeader ('SquirrelMail', $js);
221 $onload = $xtra;
222 break;
223 case 'src/compose.php':
224 $js = '<script language="JavaScript" type="text/javascript">' .
225 "\n<!--\n" .
226 "function checkForm() {\n";
227
228 global $action, $reply_focus;
229 if (strpos($action, 'reply') !== FALSE && $reply_focus)
230 {
231 if ($reply_focus == 'select') $js .= "document.forms['compose'].body.select();}\n";
232 else if ($reply_focus == 'focus') $js .= "document.forms['compose'].body.focus();}\n";
233 else if ($reply_focus == 'none') $js .= "}\n";
234 }
235 // no reply focus also applies to composing new messages
236 else if ($reply_focus == 'none')
237 {
238 $js .= "}\n";
239 }
240 else
241 $js .= "var f = document.forms.length;\n".
242 "var i = 0;\n".
243 "var pos = -1;\n".
244 "while( pos == -1 && i < f ) {\n".
245 "var e = document.forms[i].elements.length;\n".
246 "var j = 0;\n".
247 "while( pos == -1 && j < e ) {\n".
248 "if ( document.forms[i].elements[j].type == 'text' ) {\n".
249 "pos = j;\n".
250 "}\n".
251 "j++;\n".
252 "}\n".
253 "i++;\n".
254 "}\n".
255 "if( pos >= 0 ) {\n".
256 "document.forms[i-1].elements[pos].focus();\n".
257 "}\n".
258 "}\n";
259
260 $js .= "// -->\n".
261 "</script>\n";
262 $onload = 'onload="checkForm();"';
263 displayHtmlHeader ('SquirrelMail', $js);
264 break;
265
266 case 'src/right_main.php':
267 // following code graciously stolen from phpMyAdmin project at:
268 // http://www.phpmyadmin.net
269 $js = <<<EOS
270 /**
271 * This array is used to remember mark status of rows in browse mode
272 */
273 var marked_row = new Array;
274
275
276 /*
277 * (un)Checks checkbox for the row that the current table cell is in
278 * when it gets clicked.
279 *
280 * @param string the name of the checkbox that should be (un)checked
281 */
282 function row_click(chkboxName) {
283 chkbox = document.getElementById(chkboxName);
284 if (chkbox) {
285 chkbox.checked = (chkbox.checked ? false : true);
286 }
287 }
288
289
290
291 /*
292 * Sets/unsets the pointer and marker in browse mode
293 *
294 * @param object the table row
295 * @param integer the row number
296 * @param string the action calling this script (over, out or click)
297 * @param string the default background color
298 * @param string the color to use for mouseover
299 * @param string the color to use for marking a row
300 *
301 * @return boolean whether pointer is set or not
302 */
303 function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
304 {
305 var theCells = null;
306
307 // 1. Pointer and mark feature are disabled or the browser can't get the
308 // row -> exits
309 if ((thePointerColor == '' && theMarkColor == '')
310 || typeof(theRow.style) == 'undefined') {
311 return false;
312 }
313
314 // 2. Gets the current row and exits if the browser can't get it
315 if (typeof(document.getElementsByTagName) != 'undefined') {
316 theCells = theRow.getElementsByTagName('td');
317 }
318 else if (typeof(theRow.cells) != 'undefined') {
319 theCells = theRow.cells;
320 }
321 else {
322 return false;
323 }
324
325 // 3. Gets the current color...
326 var rowCellsCnt = theCells.length;
327 var domDetect = null;
328 var currentColor = null;
329 var newColor = null;
330 // 3.1 ... with DOM compatible browsers except Opera that does not return
331 // valid values with "getAttribute"
332 if (typeof(window.opera) == 'undefined'
333 && typeof(theCells[0].getAttribute) != 'undefined') {
334 currentColor = theCells[0].getAttribute('bgcolor');
335 domDetect = true;
336 }
337 // 3.2 ... with other browsers
338 else {
339 currentColor = theCells[0].style.backgroundColor;
340 domDetect = false;
341 } // end 3
342
343 // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
344 if (currentColor.indexOf("rgb") >= 0)
345 {
346 var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
347 currentColor.indexOf(')'));
348 var rgbValues = rgbStr.split(",");
349 currentColor = "#";
350 var hexChars = "0123456789ABCDEF";
351 for (var i = 0; i < 3; i++)
352 {
353 var v = rgbValues[i].valueOf();
354 currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
355 }
356 }
357
358 // 4. Defines the new color
359 // 4.1 Current color is the default one
360 if (currentColor == ''
361 || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
362 if (theAction == 'over' && thePointerColor != '') {
363 newColor = thePointerColor;
364 }
365 else if (theAction == 'click' && theMarkColor != '') {
366 newColor = theMarkColor;
367 marked_row[theRowNum] = true;
368 // deactivated onclick marking of the checkbox because it's also executed
369 // when an action (clicking on the checkbox itself) on a single item is
370 // performed. Then the checkbox would get deactived, even though we need
371 // it activated. Maybe there is a way to detect if the row was clicked,
372 // and not an item therein...
373 //document.getElementById('msg[' + theRowNum + ']').checked = true;
374 }
375 }
376 // 4.1.2 Current color is the pointer one
377 else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
378 && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
379 if (theAction == 'out') {
380 newColor = theDefaultColor;
381 }
382 else if (theAction == 'click' && theMarkColor != '') {
383 newColor = theMarkColor;
384 marked_row[theRowNum] = true;
385 //document.getElementById('msg[' + theRowNum + ']').checked = true;
386 }
387 }
388 // 4.1.3 Current color is the marker one
389 else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
390 if (theAction == 'click') {
391 newColor = (thePointerColor != '')
392 ? thePointerColor
393 : theDefaultColor;
394 marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
395 ? true
396 : null;
397 //document.getElementById('msg[' + theRowNum + ']').checked = false;
398 }
399 } // end 4
400
401 // 5. Sets the new color...
402 if (newColor) {
403 var c = null;
404 // 5.1 ... with DOM compatible browsers except Opera
405 if (domDetect) {
406 for (c = 0; c < rowCellsCnt; c++) {
407 theCells[c].setAttribute('bgcolor', newColor, 0);
408 } // end for
409 }
410 // 5.2 ... with other browsers
411 else {
412 for (c = 0; c < rowCellsCnt; c++) {
413 theCells[c].style.backgroundColor = newColor;
414 }
415 }
416 } // end 5
417
418 return true;
419 } // end of the 'setPointer()' function
420 EOS;
421 $js = "\n".'<script language="JavaScript" type="text/javascript">' .
422 "\n<!--\n" . $js;
423 if ($compose_new_win == '1') {
424 if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
425 $compose_width = '640';
426 }
427 if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
428 $compose_height = '550';
429 }
430 $js .= "\nfunction comp_in_new(comp_uri) {\n".
431 " if (!comp_uri) {\n".
432 ' comp_uri = "'.$compose_uri."\";\n".
433 ' }'. "\n".
434 ' var newwin = window.open(comp_uri' .
435 ', "_blank",'.
436 '"width='.$compose_width. ',height='.$compose_height.
437 ',scrollbars=yes,resizable=yes,status=yes");'."\n".
438 "}\n\n";
439 }
440 $js .= "// -->\n</script>\n";
441 $onload = '';
442 displayHtmlHeader ('SquirrelMail', $js);
443 break;
444
445 default:
446 $js = '<script language="JavaScript" type="text/javascript">' .
447 "\n<!--\n" .
448 "function checkForm() {\n".
449 "var f = document.forms.length;\n".
450 "var i = 0;\n".
451 "var pos = -1;\n".
452 "while( pos == -1 && i < f ) {\n".
453 "var e = document.forms[i].elements.length;\n".
454 "var j = 0;\n".
455 "while( pos == -1 && j < e ) {\n".
456 "if ( document.forms[i].elements[j].type == 'text' " .
457 "|| document.forms[i].elements[j].type == 'password' ) {\n".
458 "pos = j;\n".
459 "}\n".
460 "j++;\n".
461 "}\n".
462 "i++;\n".
463 "}\n".
464 "if( pos >= 0 ) {\n".
465 "document.forms[i-1].elements[pos].focus();\n".
466 "}\n".
467 "$xtra\n".
468 "}\n";
469
470 if ($compose_new_win == '1') {
471 if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
472 $compose_width = '640';
473 }
474 if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
475 $compose_height = '550';
476 }
477 $js .= "function comp_in_new(comp_uri) {\n".
478 " if (!comp_uri) {\n".
479 ' comp_uri = "'.$compose_uri."\";\n".
480 ' }'. "\n".
481 ' var newwin = window.open(comp_uri' .
482 ', "_blank",'.
483 '"width='.$compose_width. ',height='.$compose_height.
484 ',scrollbars=yes,resizable=yes,status=yes");'."\n".
485 "}\n\n";
486
487 }
488 $js .= "// -->\n". "</script>\n";
489
490 $onload = 'onload="checkForm();"';
491 displayHtmlHeader ('SquirrelMail', $js);
492 break;
493
494 }
495 } else {
496 /* do not use JavaScript */
497 displayHtmlHeader ('SquirrelMail');
498 $onload = '';
499 }
500
501 echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\" $onload>\n\n";
502 /** Here is the header and wrapping table **/
503 $shortBoxName = htmlspecialchars(imap_utf7_decode_local(
504 readShortMailboxName($mailbox, $delimiter)));
505 if ( $shortBoxName == 'INBOX' ) {
506 $shortBoxName = _("INBOX");
507 }
508 echo "<a name=\"pagetop\"></a>\n"
509 . html_tag( 'table', '', '', $color[4], 'border="0" width="100%" cellspacing="0" cellpadding="2"' ) ."\n"
510 . html_tag( 'tr', '', '', $color[9] ) ."\n"
511 . html_tag( 'td', '', 'left' ) ."\n";
512 if ( $shortBoxName <> '' && strtolower( $shortBoxName ) <> 'none' ) {
513 echo ' ' . _("Current Folder") . ": <b>$shortBoxName&nbsp;</b>\n";
514 } else {
515 echo '&nbsp;';
516 }
517 echo " </td>\n"
518 . html_tag( 'td', '', 'right' ) ."<b>\n";
519 displayInternalLink ('src/signout.php', _("Sign Out"), $frame_top);
520 echo "</b></td>\n"
521 . " </tr>\n"
522 . html_tag( 'tr', '', '', $color[4] ) ."\n"
523 . ($hide_sm_attributions ? html_tag( 'td', '', 'left', '', 'colspan="2"' )
524 : html_tag( 'td', '', 'left' ) )
525 . "\n";
526 $urlMailbox = urlencode($mailbox);
527 echo makeComposeLink('src/compose.php?mailbox='.$urlMailbox.'&amp;startMessage='.$startMessage);
528 echo "&nbsp;&nbsp;\n";
529 displayInternalLink ('src/addressbook.php', _("Addresses"));
530 echo "&nbsp;&nbsp;\n";
531 displayInternalLink ('src/folders.php', _("Folders"));
532 echo "&nbsp;&nbsp;\n";
533 displayInternalLink ('src/options.php', _("Options"));
534 echo "&nbsp;&nbsp;\n";
535 displayInternalLink ("src/search.php?mailbox=$urlMailbox", _("Search"));
536 echo "&nbsp;&nbsp;\n";
537 displayInternalLink ('src/help.php', _("Help"));
538 echo "&nbsp;&nbsp;\n";
539
540 do_hook('menuline');
541
542 echo " </td>\n";
543
544 if (!$hide_sm_attributions)
545 {
546 echo html_tag( 'td', '', 'right' ) ."\n";
547 if (empty($provider_uri)) {
548 echo '<a href="about.php">SquirrelMail</a>';
549 } else {
550 if (empty($provider_name)) $provider_name= 'SquirrelMail';
551 echo '<a href="'.$provider_uri.'" target="_blank">'.$provider_name.'</a>';
552 }
553 echo "</td>\n";
554 }
555 echo " </tr>\n".
556 "</table><br />\n\n";
557 }
558
559 /**
560 * Blatantly copied/truncated/modified from displayPageHeader.
561 * Outputs a page header specifically for the compose_in_new popup window
562 *
563 * @param array color the array of theme colors
564 * @param string mailbox the current mailbox name to display
565 * @return void
566 */
567 function compose_Header($color, $mailbox) {
568
569 global $javascript_on;
570
571 /*
572 * Locate the first displayable form element (only when JavaScript on)
573 */
574 if($javascript_on) {
575 global $base_uri, $PHP_SELF, $data_dir, $username;
576
577 $module = substr( $PHP_SELF, ( strlen( $PHP_SELF ) - strlen( $base_uri ) ) * -1 );
578
579 switch ( $module ) {
580 case 'src/search.php':
581 $pos = getPref($data_dir, $username, 'search_pos', 0 ) - 1;
582 $onload = "onload=\"document.forms[$pos].elements[2].focus();\"";
583 displayHtmlHeader (_("Compose"));
584 break;
585 default:
586 $js = '<script language="JavaScript" type="text/javascript">' .
587 "\n<!--\n" .
588 "function checkForm() {\n";
589
590 global $action, $reply_focus;
591 if (strpos($action, 'reply') !== FALSE && $reply_focus)
592 {
593 if ($reply_focus == 'select') $js .= "document.forms['compose'].body.select();}\n";
594 else if ($reply_focus == 'focus') $js .= "document.forms['compose'].body.focus();}\n";
595 else if ($reply_focus == 'none') $js .= "}\n";
596 }
597 // no reply focus also applies to composing new messages
598 else if ($reply_focus == 'none')
599 {
600 $js .= "}\n";
601 }
602 else
603 $js .= "var f = document.forms.length;\n".
604 "var i = 0;\n".
605 "var pos = -1;\n".
606 "while( pos == -1 && i < f ) {\n".
607 "var e = document.forms[i].elements.length;\n".
608 "var j = 0;\n".
609 "while( pos == -1 && j < e ) {\n".
610 "if ( document.forms[i].elements[j].type == 'text' ) {\n".
611 "pos = j;\n".
612 "}\n".
613 "j++;\n".
614 "}\n".
615 "i++;\n".
616 "}\n".
617 "if( pos >= 0 ) {\n".
618 "document.forms[i-1].elements[pos].focus();\n".
619 "}\n".
620 "}\n";
621 $js .= "// -->\n".
622 "</script>\n";
623 $onload = 'onload="checkForm();"';
624 displayHtmlHeader (_("Compose"), $js);
625 break;
626 }
627 } else {
628 /* javascript off */
629 displayHtmlHeader(_("Compose"));
630 $onload = '';
631 }
632
633 echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\" $onload>\n\n";
634 }
635
636 ?>