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 +-----------------------------------------------------------------------+
23 * Class for HTML page creation
29 protected $scripts_path = '';
30 protected $script_files = array();
31 protected $scripts = array();
32 protected $charset = RCMAIL_CHARSET;
34 protected $script_tag_file = "<script type=\"text/javascript\" src=\"%s\"></script>\n";
35 protected $script_tag = "<script type=\"text/javascript\">\n/* <![CDATA[ */\n%s\n/* ]]> */\n</script>";
36 protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>";
38 protected $title = '';
39 protected $header = '';
40 protected $footer = '';
45 public function __construct() {}
48 * Link an external script file
50 * @param string File URL
51 * @param string Target position [head|foot]
53 public function include_script($file, $position='head')
55 static $sa_files = array();
57 if (!preg_match('|^https?://|i', $file) && $file[0] != '/')
58 $file = $this->scripts_path . $file . (($fs = @filemtime($this->scripts_path . $file)) ? '?s='.$fs : '');
60 if (in_array($file, $sa_files)) {
63 if (!is_array($this->script_files[$position])) {
64 $this->script_files[$position] = array();
66 $this->script_files[$position][] = $file;
70 * Add inline javascript code
72 * @param string JS code snippet
73 * @param string Target position [head|head_top|foot]
75 public function add_script($script, $position='head')
77 if (!isset($this->scripts[$position])) {
78 $this->scripts[$position] = "\n".rtrim($script);
80 $this->scripts[$position] .= "\n".rtrim($script);
85 * Add HTML code to the page header
87 public function add_header($str)
89 $this->header .= "\n".$str;
93 * Add HTML code to the page footer
94 * To be added right befor </body>
96 public function add_footer($str)
98 $this->footer .= "\n".$str;
102 * Setter for page title
104 public function set_title($t)
110 * Setter for output charset.
111 * To be specified in a meta tag and sent as http-header
113 public function set_charset($charset)
115 $this->charset = $charset;
119 * Getter for output charset
121 public function get_charset()
123 return $this->charset;
127 * Reset all saved properties
129 public function reset()
131 $this->script_files = array();
132 $this->scripts = array();
140 * Process template and write to stdOut
142 * @param string HTML template
143 * @param string Base for absolute paths
145 public function write($templ='', $base_path='')
147 $output = empty($templ) ? $this->default_template : trim($templ);
149 // set default page title
150 if (empty($this->title)) {
151 $this->title = 'RoundCube Mail';
154 // replace specialchars in content
155 $__page_title = Q($this->title, 'show', FALSE);
156 $__page_header = $__page_body = $__page_footer = '';
158 // include meta tag with charset
159 if (!empty($this->charset)) {
160 if (!headers_sent()) {
161 header('Content-Type: text/html; charset=' . $this->charset);
163 $__page_header = '<meta http-equiv="content-type"';
164 $__page_header.= ' content="text/html; charset=';
165 $__page_header.= $this->charset . '" />'."\n";
168 // definition of the code to be placed in the document header and footer
169 if (is_array($this->script_files['head'])) {
170 foreach ($this->script_files['head'] as $file) {
171 $__page_header .= sprintf($this->script_tag_file, $file);
175 $head_script = $this->scripts['head_top'] . $this->scripts['head'];
176 if (!empty($head_script)) {
177 $__page_header .= sprintf($this->script_tag, $head_script);
180 if (!empty($this->header)) {
181 $__page_header .= $this->header;
184 if (is_array($this->script_files['foot'])) {
185 foreach ($this->script_files['foot'] as $file) {
186 $__page_footer .= sprintf($this->script_tag_file, $file);
190 if (!empty($this->scripts['foot'])) {
191 $__page_footer .= sprintf($this->script_tag, $this->scripts['foot']);
194 if (!empty($this->footer)) {
195 $__page_footer .= $this->footer;
199 if ($hpos = strpos(strtolower($output), '</head>')) {
200 $__page_header .= "\n";
203 if (!is_numeric($hpos)) {
204 $hpos = strpos(strtolower($output), '<body');
206 if (!is_numeric($hpos) && ($hpos = strpos(strtolower($output), '<html'))) {
207 while ($output[$hpos] != '>') {
212 $__page_header = "<head>\n<title>$__page_title</title>\n$__page_header\n</head>\n";
217 $output = substr($output,0,$hpos) . $__page_header . substr($output,$hpos,strlen($output));
220 $output = $__page_header . $output;
224 if ($bpos = strpos(strtolower($output), '<body')) {
225 while ($output[$bpos] != '>') {
231 $bpos = strpos(strtolower($output), '</head>')+7;
235 if ($bpos && $__page_body) {
236 $output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output));
239 // find and add page footer
240 $output_lc = strtolower($output);
241 if (($fpos = strrpos($output_lc, '</body>')) || ($fpos = strrpos($output_lc, '</html>'))) {
242 $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos);
245 $output .= "\n".$__page_footer;
248 // reset those global vars
249 $__page_header = $__page_footer = '';
251 // correct absolute paths in images and other tags
252 $output = preg_replace('!(src|href|background)=(["\']?)(/[a-z0-9_-]+)!i', "\\1=\\2$base_path\\3", $output);
253 $output = preg_replace_callback('!(src|href)=(["\']?)([a-z0-9/_.-]+.(css|js))(["\'\s>])!i', array($this, 'add_filemtime'), $output);
254 $output = str_replace('$__skin_path', $base_path, $output);
256 if ($this->charset != RCMAIL_CHARSET)
257 echo rcube_charset_convert($output, RCMAIL_CHARSET, $this->charset);
263 * Callback function for preg_replace_callback in write()
265 public function add_filemtime($matches)
267 return sprintf("%s=%s%s?s=%d%s", $matches[1], $matches[2], $matches[3], @filemtime($matches[3]), $matches[5]);