-
Notifications
You must be signed in to change notification settings - Fork 1
/
Page.class.php
274 lines (238 loc) · 8.91 KB
/
Page.class.php
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
<?php
/*
PHP-Mesh - A page meshing framework for PHP.
Copyright (C) 2003-2009 Trejkaz
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
You can contact the author by electronic mail, which is presently
at the following address: trejkaz@trypticon.org
*/
/**
* This class parses the HTML page which is to be decorated, and provides
* various convenience methods for getting and printing data from the page.
*
* @author Trejkaz <trejkaz@trypticon.org>
*/
class Page
{
// The page title.
// This variable is prone to change, so don't use it directly.
var $_title;
// The remainder of the page head contents, after the title is removed.
// This variable is prone to change, so don't use it directly.
var $_head;
// The page properties.
// This variable is prone to change, so don't use it directly.
var $_properties = array();
// Maps property name to "name" or "http-equiv" depending on where a meta property came from.
var $_meta_property_types = array();
// The page body.
// This variable is prone to change, so don't use it directly.
var $_body;
/**
* Constructs a Page object.
*
* @param $page_content The entire page content, as a single string.
*/
function Page($page_content)
{
// Match the <head/> element.
if (preg_match("#<head.*?>(.*?)</head>#si", $page_content, $match))
{
$this->_head = $match[1];
// Match the <title/> element.
if (preg_match("#<title.*?>(.*?)</title>#si", $this->_head, $match))
{
// Store away the title.
$this->_title = trim($match[1]);
// Match <meta/> tags.
if (preg_match_all("#<meta.*?>#si", $this->_head, $matches, PREG_SET_ORDER))
{
foreach ($matches as $match)
{
$meta_tag = $match[0];
// Match the key. (name and http-equiv are treated equally.)
if (preg_match("#\b(name)\s*=\s*\"(.*?)\"#si", $meta_tag, $match) ||
preg_match("#\b(http-equiv)\s*=\s*\"(.*?)\"#si", $meta_tag, $match))
{
$meta_type = $match[1];
$meta_name = $match[2];
}
// Match the value.
if (preg_match("#\bcontent\s*=\s*\"(.*?)\"#si", $meta_tag, $match))
{
$meta_value = $match[1];
}
// Store away the meta key and value.
$this->_properties['meta.' . $meta_name] = $meta_value;
$this->_meta_property_types['meta.' . $meta_name] = $meta_type;
}
}
// Store away the header with the title removed.
$this->_head = preg_replace("#<title.*?>.*?</title>#si", "", $this->_head);
$this->_head = trim($this->_head);
}
}
// Match the <body/> element.
if (preg_match("#(<body.*?>)(.*?)</body>#si", $page_content, $match))
{
// Store away the body.
$this->_body = trim($match[2]) . "\n";
// Match the attributes in the body tag.
$body_start_tag = $match[1];
if (preg_match_all("#\b(\S+)\s*=\s*\"(.*?)\"#s", $body_start_tag, $matches, PREG_SET_ORDER))
{
foreach ($matches as $match)
{
// Store away the body attribute key and value.
$this->_properties['body.' . $match[1]] = $match[2];
}
}
}
}
/**
* Print the page title.
*
* @param $default The default value of the title, to use if no property is defined.
*/
function title()
{
print($this->_title);
}
/**
* Print the remainder of the head contents, after the title is removed.
*/
function head()
{
print($this->_head);
}
/**
* Gets the URL of the page which is being viewed.
*
* @return the URL of the page which is being viewed.
*/
function get_url()
{
return $_SERVER['REQUEST_URI'];
}
/**
* Get a single property.
*
* @param $property_name The name of the property.
* @param $default The default value of the property, to use if the property is not defined.
* @return the value of that property, or the default if the property was not defined.
*/
function get_property($property_name, $default = NULL)
{
$property_value = $this->_properties[$property_name];
if ($property_value == NULL)
{
$property_value = $default;
}
return $property_value;
}
/**
* Print a single property.
*
* Properties found in <meta> tags are named meta.[propertyName].
* Properties found on the <body> tag as attributes are named body.[propertyName].
*
* @param $property_name The name of the property.
* @param $default The default value of the property, to use if the property is not defined.
* @param $formatted Whether to format the property as it originally appeared.
*/
function property($property_name, $default = NULL, $formatted = false)
{
$property_value = $this->get_property($property_name, $default);
if ($property_value != NULL)
{
if ($formatted)
{
if (preg_match("/^(body|meta).(.*)$/", $property_name, $match))
{
$property_prefix = $match[1];
$property_name_cut = $match[2];
}
else
{
die("Invalid property name: $property_name.\n");
}
if ($property_prefix == "meta")
{
$meta_type = $this->_meta_property_types[$property_name];
$before = "<meta $meta_type=\"$property_name_cut\" content=\"";
$after = "\" />";
}
else if ($property_prefix == "body")
{
$before = " $property_name_cut=\"";
$after = "\"";
}
else
{
die("Impossible branch: $property_name => $property_prefix $property_name_cut");
}
}
else
{
$before = "";
$after = "";
}
print "$before$property_value$after";
}
}
/**
* Print the page body.
*/
function body()
{
print($this->_body);
}
/**
* Includes the specified page as decorated content.
*
* @param $page_location the location of the page to fetch and apply the
* decorator to, which may be relative to the decorator.
* @param $decorator_name the name of the decorator to decorate the page with.
*/
function apply_decorator($page_location, $decorator_name)
{
global $decorator_selector;
global $_GET;
global $_POST;
// Store the original GET and POST variables.
$OLD_GET = $_GET;
$OLD_POST = $_POST;
// Does the page location have a query string? If it does, we need to parse it!
$_GET = array();
$_POST = array();
$parsed_location = parse_url($page_location);
if (isset($parsed_location['query']))
{
parse_str($parsed_location['query'], $_GET);
}
// Capture the output from requiring the page.
ob_start();
require($decorator_selector->get_path($parsed_location['path']));
$page_contents = ob_get_clean();
// Restore the original GET and POST variables.
$_GET = $OLD_GET;
$_POST = $OLD_POST;
// Create the page object. This guy does all the parsing work.
$page = new Page($page_contents);
// Create the decorator.
$decorator = $decorator_selector->get_decorator($decorator_name);
// Perform the decoration.
$decorator->decorate($page);
}
} // class Page
?>