thomascube
2011-03-01 715c7961ba8ff72fe40720bb4feaa7865e57e8b9
commit | author | age
47124c 1 <?php
4c6b66 2
47124c 3 /*
T 4  +-----------------------------------------------------------------------+
5  | program/include/rcube_html_page.php                                   |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube PHP suite                          |
f5e7b3 8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
47124c 9  | Licensed under the GNU GPL                                            |
T 10  |                                                                       |
11  | CONTENTS:                                                             |
12  |   Class to build XHTML page output                                    |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
1d786c 18  $Id$
47124c 19
T 20 */
21
22 /**
23  * Class for HTML page creation
24  *
25  * @package HTML
26  */
27 class rcube_html_page
28 {
4c6b66 29     protected $scripts_path = '';
T 30     protected $script_files = array();
ba3377 31     protected $css_files = array();
4c6b66 32     protected $scripts = array();
ecb9fb 33     protected $charset = RCMAIL_CHARSET;
47124c 34
cc97ea 35     protected $script_tag_file = "<script type=\"text/javascript\" src=\"%s\"></script>\n";
T 36     protected $script_tag  =  "<script type=\"text/javascript\">\n/* <![CDATA[ */\n%s\n/* ]]> */\n</script>";
ba3377 37     protected $link_css_file = "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n";
4c6b66 38     protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>";
T 39
40     protected $title = '';
41     protected $header = '';
42     protected $footer = '';
43     protected $body = '';
74be73 44     protected $base_path = '';
4c6b66 45
T 46
47     /** Constructor */
48     public function __construct() {}
47124c 49
T 50     /**
51      * Link an external script file
52      *
53      * @param string File URL
54      * @param string Target position [head|foot]
55      */
56     public function include_script($file, $position='head')
57     {
58         static $sa_files = array();
cc97ea 59         
23a2ee 60         if (!preg_match('|^https?://|i', $file) && $file[0] != '/')
2eb794 61             $file = $this->scripts_path . $file . (($fs = @filemtime($this->scripts_path . $file)) ? '?s='.$fs : '');
47124c 62
T 63         if (in_array($file, $sa_files)) {
64             return;
65         }
ba3377 66
A 67         $sa_files[] = $file;
68
47124c 69         if (!is_array($this->script_files[$position])) {
T 70             $this->script_files[$position] = array();
71         }
72         $this->script_files[$position][] = $file;
73     }
74
75     /**
76      * Add inline javascript code
77      *
78      * @param string JS code snippet
79      * @param string Target position [head|head_top|foot]
80      */
81     public function add_script($script, $position='head')
82     {
83         if (!isset($this->scripts[$position])) {
84             $this->scripts[$position] = "\n".rtrim($script);
85         } else {
86             $this->scripts[$position] .= "\n".rtrim($script);
87         }
ba3377 88     }
A 89
90     /**
91      * Link an external css file
92      *
93      * @param string File URL
94      */
95     public function include_css($file)
96     {
97         $this->css_files[] = $file;
47124c 98     }
T 99
100     /**
101      * Add HTML code to the page header
5c461b 102      *
A 103      * @param string $str HTML code
47124c 104      */
T 105     public function add_header($str)
106     {
107         $this->header .= "\n".$str;
108     }
109
110     /**
111      * Add HTML code to the page footer
112      * To be added right befor </body>
5c461b 113      *
A 114      * @param string $str HTML code
47124c 115      */
T 116     public function add_footer($str)
117     {
118         $this->footer .= "\n".$str;
119     }
120
121     /**
122      * Setter for page title
5c461b 123      *
A 124      * @param string $t Page title
47124c 125      */
T 126     public function set_title($t)
127     {
128         $this->title = $t;
129     }
130
131     /**
132      * Setter for output charset.
133      * To be specified in a meta tag and sent as http-header
5c461b 134      *
A 135      * @param string $charset Charset
47124c 136      */
T 137     public function set_charset($charset)
138     {
139         $this->charset = $charset;
140     }
141
142     /**
143      * Getter for output charset
5c461b 144      *
A 145      * @return string Output charset
47124c 146      */
T 147     public function get_charset()
148     {
149         return $this->charset;
150     }
151
152     /**
153      * Reset all saved properties
154      */
155     public function reset()
156     {
157         $this->script_files = array();
2eb794 158         $this->scripts      = array();
A 159         $this->title        = '';
160         $this->header       = '';
161         $this->footer       = '';
162         $this->body         = '';
47124c 163     }
T 164
165     /**
166      * Process template and write to stdOut
167      *
168      * @param string HTML template
169      * @param string Base for absolute paths
170      */
171     public function write($templ='', $base_path='')
172     {
173         $output = empty($templ) ? $this->default_template : trim($templ);
174
175         // set default page title
176         if (empty($this->title)) {
e019f2 177             $this->title = 'Roundcube Mail';
47124c 178         }
T 179
180         // replace specialchars in content
ba3377 181         $page_title  = Q($this->title, 'show', FALSE);
A 182         $page_header = '';
183         $page_footer = '';
47124c 184
T 185         // include meta tag with charset
186         if (!empty($this->charset)) {
187             if (!headers_sent()) {
188                 header('Content-Type: text/html; charset=' . $this->charset);
189             }
ba3377 190             $page_header = '<meta http-equiv="content-type"';
A 191             $page_header.= ' content="text/html; charset=';
192             $page_header.= $this->charset . '" />'."\n";
47124c 193         }
T 194
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) {
ba3377 198                 $page_header .= sprintf($this->script_tag_file, $file);
47124c 199             }
T 200         }
201
202         $head_script = $this->scripts['head_top'] . $this->scripts['head'];
203         if (!empty($head_script)) {
ba3377 204             $page_header .= sprintf($this->script_tag, $head_script);
47124c 205         }
T 206
207         if (!empty($this->header)) {
ba3377 208             $page_header .= $this->header;
47124c 209         }
T 210
211         if (is_array($this->script_files['foot'])) {
212             foreach ($this->script_files['foot'] as $file) {
ba3377 213                 $page_footer .= sprintf($this->script_tag_file, $file);
47124c 214             }
T 215         }
216
217         if (!empty($this->scripts['foot'])) {
ba3377 218             $page_footer .= sprintf($this->script_tag, $this->scripts['foot']);
47124c 219         }
T 220
221         if (!empty($this->footer)) {
ba3377 222             $page_footer .= $this->footer;
47124c 223         }
T 224
225         // find page header
74be73 226         if ($hpos = stripos($output, '</head>')) {
ba3377 227             $page_header .= "\n";
47124c 228         }
T 229         else {
230             if (!is_numeric($hpos)) {
74be73 231                 $hpos = stripos($output, '<body');
47124c 232             }
74be73 233             if (!is_numeric($hpos) && ($hpos = stripos($output, '<html'))) {
47124c 234                 while ($output[$hpos] != '>') {
T 235                     $hpos++;
236                 }
237                 $hpos++;
238             }
ba3377 239             $page_header = "<head>\n<title>$page_title</title>\n$page_header\n</head>\n";
47124c 240         }
T 241
242         // add page hader
243         if ($hpos) {
ba3377 244             $output = substr($output,0,$hpos) . $page_header . substr($output,$hpos,strlen($output));
47124c 245         }
T 246         else {
ba3377 247             $output = $page_header . $output;
47124c 248         }
T 249
ba3377 250         // add page footer
74be73 251         if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
ba3377 252             $output = substr($output, 0, $fpos) . "$page_footer\n" . substr($output, $fpos);
47124c 253         }
T 254         else {
ba3377 255             $output .= "\n".$page_footer;
47124c 256         }
T 257
ba3377 258         // add css files in head, before scripts, for speed up with parallel downloads
A 259         if (!empty($this->css_files) && 
260             (($pos = stripos($output, '<script ')) || ($pos = stripos($output, '</head>')))
261         ) {
262             $css = '';
263             foreach ($this->css_files as $file) {
264                 $css .= sprintf($this->link_css_file, $file);
265             }
266             $output = substr($output, 0, $pos) . $css . substr($output, $pos);
267         }
47124c 268
798764 269         $this->base_path = $base_path;
ba3377 270
47124c 271         // correct absolute paths in images and other tags
798764 272         // add timestamp to .js and .css filename
ba3377 273         $output = preg_replace_callback(
A 274             '!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i',
798764 275             array($this, 'file_callback'), $output);
47124c 276         $output = str_replace('$__skin_path', $base_path, $output);
T 277
798764 278         // trigger hook with final HTML content to be sent
T 279         $hook = rcmail::get_instance()->plugins->exec_hook("send_page", array('content' => $output));
280         if (!$hook['abort']) {
281             if ($this->charset != RCMAIL_CHARSET)
282                 echo rcube_charset_convert($hook['content'], RCMAIL_CHARSET, $this->charset);
283             else
284                 echo $hook['content'];
285         }
3510d7 286     }
533e86 287     
T 288     /**
289      * Callback function for preg_replace_callback in write()
5c461b 290      *
A 291      * @return string Parsed string
533e86 292      */
74be73 293     private function file_callback($matches)
533e86 294     {
2eb794 295         $file = $matches[3];
74be73 296
A 297         // correct absolute paths
2eb794 298         if ($file[0] == '/')
A 299             $file = $this->base_path . $file;
74be73 300
A 301         // add file modification timestamp
2eb794 302         if (preg_match('/\.(js|css)$/', $file))
74be73 303             $file .= '?s=' . @filemtime($file);
A 304
2eb794 305         return sprintf("%s=%s%s%s", $matches[1], $matches[2], $file, $matches[4]);
533e86 306     }
47124c 307 }
4c6b66 308