1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
<?php
/*
+-----------------------------------------------------------------------+
| program/include/rcube_html_page.php |
| |
| This file is part of the RoundCube PHP suite |
| Copyright (C) 2005-2008, RoundCube Dev. - Switzerland |
| Licensed under the GNU GPL |
| |
| CONTENTS: |
| Class to build XHTML page output |
| |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
$Id: $
*/
/**
* Class for HTML page creation
*
* @package HTML
*
* @author Thomas Bruederli <roundcube@gmail.com>
* @license http://gnu.org GPL
* @todo See about improving performance (__get, __set, sprintf)
*/
class rcube_html_page
{
protected $_store = array();
/**
* Constructor
*
* @return rcube_html_page
* @uses self::reset()
*/
public function __construct()
{
$this->reset();
}
/**
* Link an external script file
*
* @param string File URL
* @param string Target position [head|foot]
*/
public function include_script($file, $position='head')
{
static $sa_files = array();
if (in_array($file, $sa_files)) {
return;
}
if (!is_array($this->script_files[$position])) {
$this->script_files[$position] = array();
}
$this->script_files[$position][] = $file;
}
/**
* Add inline javascript code
*
* @param string JS code snippet
* @param string Target position [head|head_top|foot]
*/
public function add_script($script, $position='head')
{
if (!isset($this->scripts[$position])) {
$this->scripts[$position] = "\n".rtrim($script);
} else {
$this->scripts[$position] .= "\n".rtrim($script);
}
}
/**
* Add HTML code to the page header
*/
public function add_header($str)
{
$this->header .= "\n".$str;
}
/**
* Add HTML code to the page footer
* To be added right befor </body>
*/
public function add_footer($str)
{
$this->footer .= "\n".$str;
}
/**
* Setter for page title
*/
public function set_title($t)
{
$this->title = $t;
}
/**
* Setter for output charset.
* To be specified in a meta tag and sent as http-header
*/
public function set_charset($charset)
{
$this->charset = $charset;
}
/**
* Getter for output charset
*/
public function get_charset()
{
return $this->charset;
}
/**
* Reset all saved properties
*
* @return void
* @see self::__construct
* @uses self::$_store
*/
public function reset()
{
$this->scripts_path = '';
$this->script_files = array();
$this->external_scripts = array();
$this->scripts = array();
$this->charset = 'UTF-8';
// templates
$this->script_tag_file = "<script type=\"text/javascript\" src=\"%s%s\"></script>\n";
$this->script_tag = "<script type=\"text/javascript\">\n<!--\n%s\n\n//-->\n</script>\n";
$this->default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>";
$this->tag_format_external_script = "<script type=\"text/javascript\" src=\"%s\"></script>\n";
// page stuff
$this->title = '';
$this->header = '';
$this->footer = '';
$this->body = '';
}
/**
* Process template and write to stdOut
*
* @param string HTML template
* @param string Base for absolute paths
*/
public function write($templ='', $base_path='')
{
$output = empty($templ) ? $this->default_template : trim($templ);
// set default page title
if (empty($this->title)) {
$this->title = 'RoundCube Mail';
}
// replace specialchars in content
$__page_title = Q($this->title, 'show', FALSE);
$__page_header = $__page_body = $__page_footer = '';
// include meta tag with charset
if (!empty($this->charset)) {
if (!headers_sent()) {
header('Content-Type: text/html; charset=' . $this->charset);
}
$__page_header = '<meta http-equiv="content-type"';
$__page_header.= ' content="text/html; charset=';
$__page_header.= $this->charset . '" />'."\n";
}
// definition of the code to be placed in the document header and footer
if (is_array($this->script_files['head'])) {
foreach ($this->script_files['head'] as $file) {
$__page_header .= sprintf($this->script_tag_file, $this->scripts_path, $file);
}
}
$head_script = $this->scripts['head_top'] . $this->scripts['head'];
if (!empty($head_script)) {
$__page_header .= sprintf($this->script_tag, $head_script);
}
if (!empty($this->header)) {
$__page_header .= $this->header;
}
if (is_array($this->script_files['foot'])) {
foreach ($this->script_files['foot'] as $file) {
$__page_footer .= sprintf($this->script_tag_file, $this->scripts_path, $file);
}
}
if (!empty($this->scripts['foot'])) {
$__page_footer .= sprintf($this->script_tag, $this->scripts['foot']);
}
if (!empty($this->footer)) {
$__page_footer .= $this->footer;
}
// find page header
if ($hpos = strpos(strtolower($output), '</head>')) {
$__page_header .= "\n";
}
else {
if (!is_numeric($hpos)) {
$hpos = strpos(strtolower($output), '<body');
}
if (!is_numeric($hpos) && ($hpos = strpos(strtolower($output), '<html'))) {
while ($output[$hpos] != '>') {
$hpos++;
}
$hpos++;
}
$__page_header = "<head>\n<title>$__page_title</title>\n$__page_header\n</head>\n";
}
// add page hader
if ($hpos) {
$output = substr($output,0,$hpos) . $__page_header . substr($output,$hpos,strlen($output));
}
else {
$output = $__page_header . $output;
}
// find page body
if ($bpos = strpos(strtolower($output), '<body')) {
while ($output[$bpos] != '>') {
$bpos++;
}
$bpos++;
}
else {
$bpos = strpos(strtolower($output), '</head>')+7;
}
// add page body
if ($bpos && $__page_body) {
$output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output));
}
// find and add page footer
$output_lc = strtolower($output);
if (($fpos = strrpos($output_lc, '</body>')) || ($fpos = strrpos($output_lc, '</html>'))) {
$output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos);
}
else {
$output .= "\n".$__page_footer;
}
// reset those global vars
$__page_header = $__page_footer = '';
// correct absolute paths in images and other tags
$output = preg_replace('/(src|href|background)=(["\']?)(\/[a-z0-9_\-]+)/Ui', "\\1=\\2$base_path\\3", $output);
$output = str_replace('$__skin_path', $base_path, $output);
echo rcube_charset_convert($output, 'UTF-8', $this->charset);
}
/**
* __get
*
* @param string $var A variable name.
*
* @return mixed
* @uses self::$_store
*/
public function __get($var)
{
return $this->_store[$var];
}
/**
* __set
*
* @param string $var A variable name.
* @param mixed $value The value of the variable.
*
* @return mixed
* @uses self::$_store
*/
public function __set($var, $value)
{
return $this->_store[$var] = $value;
}
}
|