]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_html_page.php
Imported Upstream version 0.5
[roundcube.git] / program / include / rcube_html_page.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_html_page.php                                   |
6  |                                                                       |
7  | This file is part of the Roundcube PHP suite                          |
8  | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | CONTENTS:                                                             |
12  |   Class to build XHTML page output                                    |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: rcube_html_page.php 4015 2010-09-29 16:47:05Z alec $
19
20 */
21
22 /**
23  * Class for HTML page creation
24  *
25  * @package HTML
26  */
27 class rcube_html_page
28 {
29     protected $scripts_path = '';
30     protected $script_files = array();
31     protected $scripts = array();
32     protected $charset = RCMAIL_CHARSET;
33
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>";
37
38     protected $title = '';
39     protected $header = '';
40     protected $footer = '';
41     protected $body = '';
42     protected $base_path = '';
43
44
45     /** Constructor */
46     public function __construct() {}
47
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();
57         
58         if (!preg_match('|^https?://|i', $file) && $file[0] != '/')
59             $file = $this->scripts_path . $file . (($fs = @filemtime($this->scripts_path . $file)) ? '?s='.$fs : '');
60
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
87      *
88      * @param string $str HTML code
89      */
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>
98      *
99      * @param string $str HTML code
100      */
101     public function add_footer($str)
102     {
103         $this->footer .= "\n".$str;
104     }
105
106     /**
107      * Setter for page title
108      *
109      * @param string $t Page title
110      */
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
119      *
120      * @param string $charset Charset
121      */
122     public function set_charset($charset)
123     {
124         $this->charset = $charset;
125     }
126
127     /**
128      * Getter for output charset
129      *
130      * @return string Output charset
131      */
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();
143         $this->scripts      = array();
144         $this->title        = '';
145         $this->header       = '';
146         $this->footer       = '';
147         $this->body         = '';
148     }
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)) {
162             $this->title = 'Roundcube Mail';
163         }
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) {
182                 $__page_header .= sprintf($this->script_tag_file, $file);
183             }
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) {
197                 $__page_footer .= sprintf($this->script_tag_file, $file);
198             }
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
210         if ($hpos = stripos($output, '</head>')) {
211             $__page_header .= "\n";
212         }
213         else {
214             if (!is_numeric($hpos)) {
215                 $hpos = stripos($output, '<body');
216             }
217             if (!is_numeric($hpos) && ($hpos = stripos($output, '<html'))) {
218                 while ($output[$hpos] != '>') {
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
235         if ($bpos = stripos($output, '<body')) {
236             while ($output[$bpos] != '>') {
237                 $bpos++;
238             }
239             $bpos++;
240         }
241         else {
242             $bpos = stripos($output, '</head>')+7;
243         }
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
251         if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
252             $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos);
253         }
254         else {
255             $output .= "\n".$__page_footer;
256         }
257
258         // reset those global vars
259         $__page_header = $__page_footer = '';
260
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);
267
268         if ($this->charset != RCMAIL_CHARSET)
269                 echo rcube_charset_convert($output, RCMAIL_CHARSET, $this->charset);
270             else
271                 echo $output;
272     }
273     
274     /**
275      * Callback function for preg_replace_callback in write()
276      *
277      * @return string Parsed string
278      */
279     private function file_callback($matches)
280     {
281             $file = $matches[3];
282
283         // correct absolute paths
284             if ($file[0] == '/')
285                 $file = $this->base_path . $file;
286
287         // add file modification timestamp
288             if (preg_match('/\.(js|css)$/', $file))
289             $file .= '?s=' . @filemtime($file);
290
291             return sprintf("%s=%s%s%s", $matches[1], $matches[2], $file, $matches[4]);
292     }
293 }
294