page_header.php 17 KB

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