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