4 +-----------------------------------------------------------------------+
5 | program/include/rcube_html_page.php |
7 | This file is part of the RoundCube PHP suite |
8 | Copyright (C) 2005-2008, 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 = 'UTF-8';
34 protected $script_tag_file = "<script type=\"text/javascript\" src=\"%s%s\"></script>\n";
35 protected $script_tag = "<script type=\"text/javascript\">\n<!--\n%s\n\n//-->\n</script>\n";
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 (in_array($file, $sa_files)) {
60 if (!is_array($this->script_files[$position])) {
61 $this->script_files[$position] = array();
63 $this->script_files[$position][] = $file;
67 * Add inline javascript code
69 * @param string JS code snippet
70 * @param string Target position [head|head_top|foot]
72 public function add_script($script, $position='head')
74 if (!isset($this->scripts[$position])) {
75 $this->scripts[$position] = "\n".rtrim($script);
77 $this->scripts[$position] .= "\n".rtrim($script);
82 * Add HTML code to the page header
84 public function add_header($str)
86 $this->header .= "\n".$str;
90 * Add HTML code to the page footer
91 * To be added right befor </body>
93 public function add_footer($str)
95 $this->footer .= "\n".$str;
99 * Setter for page title
101 public function set_title($t)
107 * Setter for output charset.
108 * To be specified in a meta tag and sent as http-header
110 public function set_charset($charset)
112 $this->charset = $charset;
116 * Getter for output charset
118 public function get_charset()
120 return $this->charset;
124 * Reset all saved properties
126 public function reset()
128 $this->script_files = array();
129 $this->scripts = array();
137 * Process template and write to stdOut
139 * @param string HTML template
140 * @param string Base for absolute paths
142 public function write($templ='', $base_path='')
144 $output = empty($templ) ? $this->default_template : trim($templ);
146 // set default page title
147 if (empty($this->title)) {
148 $this->title = 'RoundCube Mail';
151 // replace specialchars in content
152 $__page_title = Q($this->title, 'show', FALSE);
153 $__page_header = $__page_body = $__page_footer = '';
155 // include meta tag with charset
156 if (!empty($this->charset)) {
157 if (!headers_sent()) {
158 header('Content-Type: text/html; charset=' . $this->charset);
160 $__page_header = '<meta http-equiv="content-type"';
161 $__page_header.= ' content="text/html; charset=';
162 $__page_header.= $this->charset . '" />'."\n";
165 // definition of the code to be placed in the document header and footer
166 if (is_array($this->script_files['head'])) {
167 foreach ($this->script_files['head'] as $file) {
168 $__page_header .= sprintf($this->script_tag_file, $this->scripts_path, $file);
172 $head_script = $this->scripts['head_top'] . $this->scripts['head'];
173 if (!empty($head_script)) {
174 $__page_header .= sprintf($this->script_tag, $head_script);
177 if (!empty($this->header)) {
178 $__page_header .= $this->header;
181 if (is_array($this->script_files['foot'])) {
182 foreach ($this->script_files['foot'] as $file) {
183 $__page_footer .= sprintf($this->script_tag_file, $this->scripts_path, $file);
187 if (!empty($this->scripts['foot'])) {
188 $__page_footer .= sprintf($this->script_tag, $this->scripts['foot']);
191 if (!empty($this->footer)) {
192 $__page_footer .= $this->footer;
196 if ($hpos = strpos(strtolower($output), '</head>')) {
197 $__page_header .= "\n";
200 if (!is_numeric($hpos)) {
201 $hpos = strpos(strtolower($output), '<body');
203 if (!is_numeric($hpos) && ($hpos = strpos(strtolower($output), '<html'))) {
204 while ($output[$hpos] != '>') {
209 $__page_header = "<head>\n<title>$__page_title</title>\n$__page_header\n</head>\n";
214 $output = substr($output,0,$hpos) . $__page_header . substr($output,$hpos,strlen($output));
217 $output = $__page_header . $output;
221 if ($bpos = strpos(strtolower($output), '<body')) {
222 while ($output[$bpos] != '>') {
228 $bpos = strpos(strtolower($output), '</head>')+7;
232 if ($bpos && $__page_body) {
233 $output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output));
236 // find and add page footer
237 $output_lc = strtolower($output);
238 if (($fpos = strrpos($output_lc, '</body>')) || ($fpos = strrpos($output_lc, '</html>'))) {
239 $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos);
242 $output .= "\n".$__page_footer;
245 // reset those global vars
246 $__page_header = $__page_footer = '';
248 // correct absolute paths in images and other tags
249 $output = preg_replace('/(src|href|background)=(["\']?)(\/[a-z0-9_\-]+)/Ui', "\\1=\\2$base_path\\3", $output);
250 $output = str_replace('$__skin_path', $base_path, $output);
252 echo rcube_charset_convert($output, 'UTF-8', $this->charset);