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 4015 2010-09-29 16:47:05Z alec $
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 = '';
42 protected $base_path = '';
46 public function __construct() {}
49 * Link an external script file
51 * @param string File URL
52 * @param string Target position [head|foot]
54 public function include_script($file, $position='head')
56 static $sa_files = array();
58 if (!preg_match('|^https?://|i', $file) && $file[0] != '/')
59 $file = $this->scripts_path . $file . (($fs = @filemtime($this->scripts_path . $file)) ? '?s='.$fs : '');
61 if (in_array($file, $sa_files)) {
64 if (!is_array($this->script_files[$position])) {
65 $this->script_files[$position] = array();
67 $this->script_files[$position][] = $file;
71 * Add inline javascript code
73 * @param string JS code snippet
74 * @param string Target position [head|head_top|foot]
76 public function add_script($script, $position='head')
78 if (!isset($this->scripts[$position])) {
79 $this->scripts[$position] = "\n".rtrim($script);
81 $this->scripts[$position] .= "\n".rtrim($script);
86 * Add HTML code to the page header
88 * @param string $str HTML code
90 public function add_header($str)
92 $this->header .= "\n".$str;
96 * Add HTML code to the page footer
97 * To be added right befor </body>
99 * @param string $str HTML code
101 public function add_footer($str)
103 $this->footer .= "\n".$str;
107 * Setter for page title
109 * @param string $t Page title
111 public function set_title($t)
117 * Setter for output charset.
118 * To be specified in a meta tag and sent as http-header
120 * @param string $charset Charset
122 public function set_charset($charset)
124 $this->charset = $charset;
128 * Getter for output charset
130 * @return string Output charset
132 public function get_charset()
134 return $this->charset;
138 * Reset all saved properties
140 public function reset()
142 $this->script_files = array();
143 $this->scripts = array();
151 * Process template and write to stdOut
153 * @param string HTML template
154 * @param string Base for absolute paths
156 public function write($templ='', $base_path='')
158 $output = empty($templ) ? $this->default_template : trim($templ);
160 // set default page title
161 if (empty($this->title)) {
162 $this->title = 'Roundcube Mail';
165 // replace specialchars in content
166 $__page_title = Q($this->title, 'show', FALSE);
167 $__page_header = $__page_body = $__page_footer = '';
169 // include meta tag with charset
170 if (!empty($this->charset)) {
171 if (!headers_sent()) {
172 header('Content-Type: text/html; charset=' . $this->charset);
174 $__page_header = '<meta http-equiv="content-type"';
175 $__page_header.= ' content="text/html; charset=';
176 $__page_header.= $this->charset . '" />'."\n";
179 // definition of the code to be placed in the document header and footer
180 if (is_array($this->script_files['head'])) {
181 foreach ($this->script_files['head'] as $file) {
182 $__page_header .= sprintf($this->script_tag_file, $file);
186 $head_script = $this->scripts['head_top'] . $this->scripts['head'];
187 if (!empty($head_script)) {
188 $__page_header .= sprintf($this->script_tag, $head_script);
191 if (!empty($this->header)) {
192 $__page_header .= $this->header;
195 if (is_array($this->script_files['foot'])) {
196 foreach ($this->script_files['foot'] as $file) {
197 $__page_footer .= sprintf($this->script_tag_file, $file);
201 if (!empty($this->scripts['foot'])) {
202 $__page_footer .= sprintf($this->script_tag, $this->scripts['foot']);
205 if (!empty($this->footer)) {
206 $__page_footer .= $this->footer;
210 if ($hpos = stripos($output, '</head>')) {
211 $__page_header .= "\n";
214 if (!is_numeric($hpos)) {
215 $hpos = stripos($output, '<body');
217 if (!is_numeric($hpos) && ($hpos = stripos($output, '<html'))) {
218 while ($output[$hpos] != '>') {
223 $__page_header = "<head>\n<title>$__page_title</title>\n$__page_header\n</head>\n";
228 $output = substr($output,0,$hpos) . $__page_header . substr($output,$hpos,strlen($output));
231 $output = $__page_header . $output;
235 if ($bpos = stripos($output, '<body')) {
236 while ($output[$bpos] != '>') {
242 $bpos = stripos($output, '</head>')+7;
246 if ($bpos && $__page_body) {
247 $output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output));
250 // find and add page footer
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 // reset those global vars
259 $__page_header = $__page_footer = '';
261 $this->base_path = $base_path;
262 // correct absolute paths in images and other tags
263 // add timestamp to .js and .css filename
264 $output = preg_replace_callback('!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i',
265 array($this, 'file_callback'), $output);
266 $output = str_replace('$__skin_path', $base_path, $output);
268 if ($this->charset != RCMAIL_CHARSET)
269 echo rcube_charset_convert($output, RCMAIL_CHARSET, $this->charset);
275 * Callback function for preg_replace_callback in write()
277 * @return string Parsed string
279 private function file_callback($matches)
283 // correct absolute paths
285 $file = $this->base_path . $file;
287 // add file modification timestamp
288 if (preg_match('/\.(js|css)$/', $file))
289 $file .= '?s=' . @filemtime($file);
291 return sprintf("%s=%s%s%s", $matches[1], $matches[2], $file, $matches[4]);