4 +-----------------------------------------------------------------------+
5 | program/include/rcube_html_page.php |
7 | This file is part of the Roundcube PHP suite |
8 | Copyright (C) 2005-2011 The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Class to build XHTML page output |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_html_page.php 5135 2011-08-26 09:22:53Z alec $
23 * Class for HTML page creation
29 protected $scripts_path = '';
30 protected $script_files = array();
31 protected $css_files = array();
32 protected $scripts = array();
33 protected $charset = RCMAIL_CHARSET;
35 protected $script_tag_file = "<script type=\"text/javascript\" src=\"%s\"></script>\n";
36 protected $script_tag = "<script type=\"text/javascript\">\n/* <![CDATA[ */\n%s\n/* ]]> */\n</script>\n";
37 protected $link_css_file = "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n";
38 protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>";
40 protected $title = '';
41 protected $header = '';
42 protected $footer = '';
44 protected $base_path = '';
48 public function __construct() {}
51 * Link an external script file
53 * @param string File URL
54 * @param string Target position [head|foot]
56 public function include_script($file, $position='head')
58 static $sa_files = array();
60 if (!preg_match('|^https?://|i', $file) && $file[0] != '/')
61 $file = $this->scripts_path . $file . (($fs = @filemtime($this->scripts_path . $file)) ? '?s='.$fs : '');
63 if (in_array($file, $sa_files)) {
69 if (!is_array($this->script_files[$position])) {
70 $this->script_files[$position] = array();
72 $this->script_files[$position][] = $file;
76 * Add inline javascript code
78 * @param string JS code snippet
79 * @param string Target position [head|head_top|foot]
81 public function add_script($script, $position='head')
83 if (!isset($this->scripts[$position])) {
84 $this->scripts[$position] = "\n".rtrim($script);
86 $this->scripts[$position] .= "\n".rtrim($script);
91 * Link an external css file
93 * @param string File URL
95 public function include_css($file)
97 $this->css_files[] = $file;
101 * Add HTML code to the page header
103 * @param string $str HTML code
105 public function add_header($str)
107 $this->header .= "\n".$str;
111 * Add HTML code to the page footer
112 * To be added right befor </body>
114 * @param string $str HTML code
116 public function add_footer($str)
118 $this->footer .= "\n".$str;
122 * Setter for page title
124 * @param string $t Page title
126 public function set_title($t)
132 * Setter for output charset.
133 * To be specified in a meta tag and sent as http-header
135 * @param string $charset Charset
137 public function set_charset($charset)
139 $this->charset = $charset;
143 * Getter for output charset
145 * @return string Output charset
147 public function get_charset()
149 return $this->charset;
153 * Reset all saved properties
155 public function reset()
157 $this->script_files = array();
158 $this->scripts = array();
166 * Process template and write to stdOut
168 * @param string HTML template
169 * @param string Base for absolute paths
171 public function write($templ='', $base_path='')
173 $output = empty($templ) ? $this->default_template : trim($templ);
175 // set default page title
176 if (empty($this->title)) {
177 $this->title = 'Roundcube Mail';
180 // replace specialchars in content
181 $page_title = Q($this->title, 'show', FALSE);
185 // include meta tag with charset
186 if (!empty($this->charset)) {
187 if (!headers_sent()) {
188 header('Content-Type: text/html; charset=' . $this->charset);
190 $page_header = '<meta http-equiv="content-type"';
191 $page_header.= ' content="text/html; charset=';
192 $page_header.= $this->charset . '" />'."\n";
195 // definition of the code to be placed in the document header and footer
196 if (is_array($this->script_files['head'])) {
197 foreach ($this->script_files['head'] as $file) {
198 $page_header .= sprintf($this->script_tag_file, $file);
202 $head_script = $this->scripts['head_top'] . $this->scripts['head'];
203 if (!empty($head_script)) {
204 $page_header .= sprintf($this->script_tag, $head_script);
207 if (!empty($this->header)) {
208 $page_header .= $this->header;
211 // put docready commands into page footer
212 if (!empty($this->scripts['docready'])) {
213 $this->add_script('$(document).ready(function(){ ' . $this->scripts['docready'] . "\n});", 'foot');
216 if (is_array($this->script_files['foot'])) {
217 foreach ($this->script_files['foot'] as $file) {
218 $page_footer .= sprintf($this->script_tag_file, $file);
222 if (!empty($this->footer)) {
223 $page_footer .= $this->footer . "\n";
226 if (!empty($this->scripts['foot'])) {
227 $page_footer .= sprintf($this->script_tag, $this->scripts['foot']);
231 if ($hpos = stripos($output, '</head>')) {
232 $page_header .= "\n";
235 if (!is_numeric($hpos)) {
236 $hpos = stripos($output, '<body');
238 if (!is_numeric($hpos) && ($hpos = stripos($output, '<html'))) {
239 while ($output[$hpos] != '>') {
244 $page_header = "<head>\n<title>$page_title</title>\n$page_header\n</head>\n";
249 $output = substr_replace($output, $page_header, $hpos, 0);
252 $output = $page_header . $output;
256 if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
257 $output = substr_replace($output, $page_footer."\n", $fpos, 0);
260 $output .= "\n".$page_footer;
263 // add css files in head, before scripts, for speed up with parallel downloads
264 if (!empty($this->css_files) &&
265 (($pos = stripos($output, '<script ')) || ($pos = stripos($output, '</head>')))
268 foreach ($this->css_files as $file) {
269 $css .= sprintf($this->link_css_file, $file);
271 $output = substr_replace($output, $css, $pos, 0);
274 $this->base_path = $base_path;
276 // correct absolute paths in images and other tags
277 // add timestamp to .js and .css filename
278 $output = preg_replace_callback(
279 '!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i',
280 array($this, 'file_callback'), $output);
281 $output = str_replace('$__skin_path', $base_path, $output);
283 // trigger hook with final HTML content to be sent
284 $hook = rcmail::get_instance()->plugins->exec_hook("send_page", array('content' => $output));
285 if (!$hook['abort']) {
286 if ($this->charset != RCMAIL_CHARSET)
287 echo rcube_charset_convert($hook['content'], RCMAIL_CHARSET, $this->charset);
289 echo $hook['content'];
294 * Callback function for preg_replace_callback in write()
296 * @return string Parsed string
298 private function file_callback($matches)
302 // correct absolute paths
304 $file = $this->base_path . $file;
306 // add file modification timestamp
307 if (preg_match('/\.(js|css)$/', $file))
308 $file .= '?s=' . @filemtime($file);
310 return sprintf("%s=%s%s%s", $matches[1], $matches[2], $file, $matches[4]);