page_header.php 18 KB

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