commit | author | age
|
47124c
|
1 |
<?php |
4c6b66
|
2 |
|
47124c
|
3 |
/* |
T |
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcube_html_page.php | |
|
6 |
| | |
|
7 |
| This file is part of the RoundCube PHP suite | |
|
8 |
| Copyright (C) 2005-2008, 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: $ |
|
19 |
|
|
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(); |
|
32 |
protected $charset = 'UTF-8'; |
47124c
|
33 |
|
4c6b66
|
34 |
protected $script_tag_file = "<script type=\"text/javascript\" src=\"%s%s\"></script>\n"; |
T |
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>"; |
|
37 |
|
|
38 |
protected $title = ''; |
|
39 |
protected $header = ''; |
|
40 |
protected $footer = ''; |
|
41 |
protected $body = ''; |
|
42 |
|
|
43 |
|
|
44 |
/** Constructor */ |
|
45 |
public function __construct() {} |
47124c
|
46 |
|
T |
47 |
/** |
|
48 |
* Link an external script file |
|
49 |
* |
|
50 |
* @param string File URL |
|
51 |
* @param string Target position [head|foot] |
|
52 |
*/ |
|
53 |
public function include_script($file, $position='head') |
|
54 |
{ |
|
55 |
static $sa_files = array(); |
|
56 |
|
|
57 |
if (in_array($file, $sa_files)) { |
|
58 |
return; |
|
59 |
} |
|
60 |
if (!is_array($this->script_files[$position])) { |
|
61 |
$this->script_files[$position] = array(); |
|
62 |
} |
|
63 |
$this->script_files[$position][] = $file; |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* Add inline javascript code |
|
68 |
* |
|
69 |
* @param string JS code snippet |
|
70 |
* @param string Target position [head|head_top|foot] |
|
71 |
*/ |
|
72 |
public function add_script($script, $position='head') |
|
73 |
{ |
|
74 |
if (!isset($this->scripts[$position])) { |
|
75 |
$this->scripts[$position] = "\n".rtrim($script); |
|
76 |
} else { |
|
77 |
$this->scripts[$position] .= "\n".rtrim($script); |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Add HTML code to the page header |
|
83 |
*/ |
|
84 |
public function add_header($str) |
|
85 |
{ |
|
86 |
$this->header .= "\n".$str; |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* Add HTML code to the page footer |
|
91 |
* To be added right befor </body> |
|
92 |
*/ |
|
93 |
public function add_footer($str) |
|
94 |
{ |
|
95 |
$this->footer .= "\n".$str; |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* Setter for page title |
|
100 |
*/ |
|
101 |
public function set_title($t) |
|
102 |
{ |
|
103 |
$this->title = $t; |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* Setter for output charset. |
|
108 |
* To be specified in a meta tag and sent as http-header |
|
109 |
*/ |
|
110 |
public function set_charset($charset) |
|
111 |
{ |
|
112 |
$this->charset = $charset; |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Getter for output charset |
|
117 |
*/ |
|
118 |
public function get_charset() |
|
119 |
{ |
|
120 |
return $this->charset; |
|
121 |
} |
|
122 |
|
|
123 |
/** |
|
124 |
* Reset all saved properties |
|
125 |
*/ |
|
126 |
public function reset() |
|
127 |
{ |
|
128 |
$this->script_files = array(); |
|
129 |
$this->scripts = array(); |
|
130 |
$this->title = ''; |
|
131 |
$this->header = ''; |
|
132 |
$this->footer = ''; |
3510d7
|
133 |
$this->body = ''; |
47124c
|
134 |
} |
T |
135 |
|
|
136 |
/** |
|
137 |
* Process template and write to stdOut |
|
138 |
* |
|
139 |
* @param string HTML template |
|
140 |
* @param string Base for absolute paths |
|
141 |
*/ |
|
142 |
public function write($templ='', $base_path='') |
|
143 |
{ |
|
144 |
$output = empty($templ) ? $this->default_template : trim($templ); |
|
145 |
|
|
146 |
// set default page title |
|
147 |
if (empty($this->title)) { |
|
148 |
$this->title = 'RoundCube Mail'; |
|
149 |
} |
|
150 |
|
|
151 |
// replace specialchars in content |
|
152 |
$__page_title = Q($this->title, 'show', FALSE); |
|
153 |
$__page_header = $__page_body = $__page_footer = ''; |
|
154 |
|
|
155 |
// include meta tag with charset |
|
156 |
if (!empty($this->charset)) { |
|
157 |
if (!headers_sent()) { |
|
158 |
header('Content-Type: text/html; charset=' . $this->charset); |
|
159 |
} |
|
160 |
$__page_header = '<meta http-equiv="content-type"'; |
|
161 |
$__page_header.= ' content="text/html; charset='; |
|
162 |
$__page_header.= $this->charset . '" />'."\n"; |
|
163 |
} |
|
164 |
|
|
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); |
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 |
$head_script = $this->scripts['head_top'] . $this->scripts['head']; |
|
173 |
if (!empty($head_script)) { |
|
174 |
$__page_header .= sprintf($this->script_tag, $head_script); |
|
175 |
} |
|
176 |
|
|
177 |
if (!empty($this->header)) { |
|
178 |
$__page_header .= $this->header; |
|
179 |
} |
|
180 |
|
|
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); |
|
184 |
} |
|
185 |
} |
|
186 |
|
|
187 |
if (!empty($this->scripts['foot'])) { |
|
188 |
$__page_footer .= sprintf($this->script_tag, $this->scripts['foot']); |
|
189 |
} |
|
190 |
|
|
191 |
if (!empty($this->footer)) { |
|
192 |
$__page_footer .= $this->footer; |
|
193 |
} |
|
194 |
|
|
195 |
// find page header |
|
196 |
if ($hpos = strpos(strtolower($output), '</head>')) { |
|
197 |
$__page_header .= "\n"; |
|
198 |
} |
|
199 |
else { |
|
200 |
if (!is_numeric($hpos)) { |
|
201 |
$hpos = strpos(strtolower($output), '<body'); |
|
202 |
} |
|
203 |
if (!is_numeric($hpos) && ($hpos = strpos(strtolower($output), '<html'))) { |
|
204 |
while ($output[$hpos] != '>') { |
|
205 |
$hpos++; |
|
206 |
} |
|
207 |
$hpos++; |
|
208 |
} |
|
209 |
$__page_header = "<head>\n<title>$__page_title</title>\n$__page_header\n</head>\n"; |
|
210 |
} |
|
211 |
|
|
212 |
// add page hader |
|
213 |
if ($hpos) { |
|
214 |
$output = substr($output,0,$hpos) . $__page_header . substr($output,$hpos,strlen($output)); |
|
215 |
} |
|
216 |
else { |
|
217 |
$output = $__page_header . $output; |
|
218 |
} |
|
219 |
|
|
220 |
// find page body |
|
221 |
if ($bpos = strpos(strtolower($output), '<body')) { |
|
222 |
while ($output[$bpos] != '>') { |
|
223 |
$bpos++; |
|
224 |
} |
|
225 |
$bpos++; |
|
226 |
} |
|
227 |
else { |
|
228 |
$bpos = strpos(strtolower($output), '</head>')+7; |
|
229 |
} |
|
230 |
|
|
231 |
// add page body |
|
232 |
if ($bpos && $__page_body) { |
|
233 |
$output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output)); |
|
234 |
} |
|
235 |
|
|
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); |
|
240 |
} |
|
241 |
else { |
|
242 |
$output .= "\n".$__page_footer; |
|
243 |
} |
|
244 |
|
|
245 |
// reset those global vars |
|
246 |
$__page_header = $__page_footer = ''; |
|
247 |
|
|
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); |
|
251 |
|
3510d7
|
252 |
echo rcube_charset_convert($output, 'UTF-8', $this->charset); |
T |
253 |
} |
47124c
|
254 |
} |
4c6b66
|
255 |
|