4 +-----------------------------------------------------------------------+
5 | program/include/rcube_html_page.php |
7 | This file is part of the Roundcube PHP suite |
8 | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland |
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 4469 2011-01-29 14:55:12Z thomasb $
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>";
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 if (is_array($this->script_files['foot'])) {
212 foreach ($this->script_files['foot'] as $file) {
213 $page_footer .= sprintf($this->script_tag_file, $file);
217 if (!empty($this->scripts['foot'])) {
218 $page_footer .= sprintf($this->script_tag, $this->scripts['foot']);
221 if (!empty($this->footer)) {
222 $page_footer .= $this->footer;
226 if ($hpos = stripos($output, '</head>')) {
227 $page_header .= "\n";
230 if (!is_numeric($hpos)) {
231 $hpos = stripos($output, '<body');
233 if (!is_numeric($hpos) && ($hpos = stripos($output, '<html'))) {
234 while ($output[$hpos] != '>') {
239 $page_header = "<head>\n<title>$page_title</title>\n$page_header\n</head>\n";
244 $output = substr($output,0,$hpos) . $page_header . substr($output,$hpos,strlen($output));
247 $output = $page_header . $output;
251 if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
252 $output = substr($output, 0, $fpos) . "$page_footer\n" . substr($output, $fpos);
255 $output .= "\n".$page_footer;
258 // add css files in head, before scripts, for speed up with parallel downloads
259 if (!empty($this->css_files) &&
260 (($pos = stripos($output, '<script ')) || ($pos = stripos($output, '</head>')))
263 foreach ($this->css_files as $file) {
264 $css .= sprintf($this->link_css_file, $file);
266 $output = substr($output, 0, $pos) . $css . substr($output, $pos);
269 $this->base_path = $base_path;
271 // correct absolute paths in images and other tags
272 // add timestamp to .js and .css filename
273 $output = preg_replace_callback(
274 '!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i',
275 array($this, 'file_callback'), $output);
276 $output = str_replace('$__skin_path', $base_path, $output);
278 if ($this->charset != RCMAIL_CHARSET)
279 echo rcube_charset_convert($output, RCMAIL_CHARSET, $this->charset);
285 * Callback function for preg_replace_callback in write()
287 * @return string Parsed string
289 private function file_callback($matches)
293 // correct absolute paths
295 $file = $this->base_path . $file;
297 // add file modification timestamp
298 if (preg_match('/\.(js|css)$/', $file))
299 $file .= '?s=' . @filemtime($file);
301 return sprintf("%s=%s%s%s", $matches[1], $matches[2], $file, $matches[4]);