|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebPage class |
|
4
|
|
|
* |
|
5
|
|
|
* This file describes an abstraction for creating a webpage |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 5 and 7 |
|
8
|
|
|
* |
|
9
|
|
|
* @author Patrick Boyd / [email protected] |
|
10
|
|
|
* @copyright Copyright (c) 2015, Austin Artistic Reconstruction |
|
11
|
|
|
* @license http://www.apache.org/licenses/ Apache 2.0 License |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* We use the Browscap abstraction to determine browser versions |
|
16
|
|
|
*/ |
|
17
|
|
|
require('libs/browscap-php/src/phpbrowscap/Browscap.php'); |
|
18
|
|
|
use phpbrowscap\Browscap; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A generic abstraction layer for creating a webpage. |
|
22
|
|
|
* |
|
23
|
|
|
* This class abstracts out some basic webpage creation |
|
24
|
|
|
*/ |
|
25
|
|
|
class WebPage |
|
26
|
|
|
{ |
|
27
|
|
|
/** The webpage title */ |
|
28
|
|
|
public $title; |
|
29
|
|
|
/** An array of tags to be added to the HTML head section */ |
|
30
|
|
|
protected $headTags; |
|
31
|
|
|
/** A string represnting the body of the page */ |
|
32
|
|
|
public $body; |
|
33
|
|
|
/** The browsecap object */ |
|
34
|
|
|
private $browscap; |
|
35
|
|
|
/** Data about the browser used to load the page */ |
|
36
|
|
|
public $browser; |
|
37
|
|
|
/** A string to add to the body open tag */ |
|
38
|
|
|
public $body_tags; |
|
39
|
|
|
/** Does the browser support import of CSS or HTML? */ |
|
40
|
|
|
public $importSupport; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create a new WebPage |
|
44
|
|
|
* |
|
45
|
|
|
* Create a new webpage abstraction object |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $title The webpage title |
|
48
|
|
|
*/ |
|
49
|
|
|
function __construct($title) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$this->title = $title; |
|
52
|
|
|
$this->headTags = array(); |
|
53
|
|
|
$this->body = ''; |
|
54
|
|
|
$this->browscap = $this->getBrowscap(); |
|
55
|
|
|
$this->browscap->doAutoUpdate = false; |
|
56
|
|
|
$this->browscap->lowercase = true; |
|
57
|
|
|
$this->browser = $this->getBrowser(); |
|
58
|
|
|
$this->importSupport = false; |
|
59
|
|
|
|
|
60
|
|
|
$browserName = $this->getBrowserName(); |
|
61
|
|
|
if($browserName === 'IE' && $this->getBrowserMajorVer() <= 7) |
|
62
|
|
|
{ |
|
63
|
|
|
header( 'Location: /badbrowser.php' ) ; |
|
64
|
|
|
} |
|
65
|
|
|
else if($browserName === 'Chrome' && $this->getBrowserMajorVer() >= 36) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->importSupport = true; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the Browscap instance for the system |
|
73
|
|
|
* |
|
74
|
|
|
* @return Browscap The Browscap instance for the system |
|
75
|
|
|
* |
|
76
|
|
|
* @SuppressWarnings("Superglobals") |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function getBrowscap() |
|
79
|
|
|
{ |
|
80
|
|
|
if(isset($GLOBALS['BROWSCAP_CACHE'])) |
|
81
|
|
|
{ |
|
82
|
|
|
return new Browscap($GLOBALS['BROWSCAP_CACHE']); |
|
83
|
|
|
} |
|
84
|
|
|
return new Browscap('/var/php_cache/browser'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Use the Browsecap library to determine what browser is being used to load this page |
|
89
|
|
|
*/ |
|
90
|
|
|
private function getBrowser() |
|
91
|
|
|
{ |
|
92
|
|
|
static $browser;//No accident can arise from depending on an unset variable. |
|
93
|
|
|
if(!isset($browser)) |
|
94
|
|
|
{ |
|
95
|
|
|
$browser = $this->browscap->getBrowser(); |
|
96
|
|
|
} |
|
97
|
|
|
return $browser; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get the name of the browser used to load this page |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getBrowserName() |
|
104
|
|
|
{ |
|
105
|
|
|
if(isset($this->browser->Browser)) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->browser->Browser; |
|
108
|
|
|
} |
|
109
|
|
|
return $this->browser->browser; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get the first part of the browser version number |
|
114
|
|
|
* |
|
115
|
|
|
* Determine what version of the browser is being used to load the page. This |
|
116
|
|
|
* is used to determine if the version of IE is too old to be used |
|
117
|
|
|
*/ |
|
118
|
|
|
private function getBrowserMajorVer() |
|
119
|
|
|
{ |
|
120
|
|
|
if(isset($this->browser->MajorVer)) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->browser->MajorVer; |
|
123
|
|
|
} |
|
124
|
|
|
return $this->browser->majorver; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Print the HTML doctype header |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function printDoctype() |
|
131
|
|
|
{ |
|
132
|
|
|
echo '<!DOCTYPE html>'; |
|
133
|
|
|
echo "\n"; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Print the opening HTML tag |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function printOpenHtml() |
|
140
|
|
|
{ |
|
141
|
|
|
echo '<HTML>'; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Print the closing HTML tag |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function printCloseHtml() |
|
148
|
|
|
{ |
|
149
|
|
|
echo '</HTML>'; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Print the page |
|
154
|
|
|
* |
|
155
|
|
|
* @deprecated 1.0.0 This funciton is deprectated and will be remoted. Please use printPage() instead |
|
156
|
|
|
*/ |
|
157
|
|
|
function print_page() |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$this->printPage(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Print the webpage to standard out |
|
164
|
|
|
*/ |
|
165
|
|
|
public function printPage() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->printDoctype(); |
|
168
|
|
|
$this->printOpenHtml(); |
|
169
|
|
|
$this->printHead(' '); |
|
170
|
|
|
$this->printBody(' '); |
|
171
|
|
|
$this->printCloseHtml(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Add a tag to the head element |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $tag The tag to add to the page header |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function addHeadTag($tag) |
|
180
|
|
|
{ |
|
181
|
|
|
array_push($this->headTags, $tag); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Create a tag to be added to the document |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $tagName The tag's name (i.e. the string right after the open sign |
|
188
|
|
|
* @param array $attribs Attributes to be added to the tag in the form key=value |
|
189
|
|
|
* @param boolean $selfClose Does this tag end with a close (/>)? |
|
190
|
|
|
* |
|
191
|
|
|
* @return string The tag as a string |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function createOpenTag($tagName, $attribs=array(), $selfClose=false) |
|
194
|
|
|
{ |
|
195
|
|
|
$tag = '<'.$tagName; |
|
196
|
|
|
$attribNames = array_keys($attribs); |
|
197
|
|
|
foreach($attribNames as $attribName) |
|
198
|
|
|
{ |
|
199
|
|
|
$tag.=' '.$attribName; |
|
200
|
|
|
if($attribs[$attribName]) |
|
201
|
|
|
{ |
|
202
|
|
|
$tag.='="'.$attribs[$attribName].'"'; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
if($selfClose) |
|
206
|
|
|
{ |
|
207
|
|
|
return $tag.'/>'; |
|
208
|
|
|
} |
|
209
|
|
|
return $tag.'>'; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Create a close tag to be added to the document |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $tagName The tag's name (i.e. the string right after the open sign |
|
216
|
|
|
* |
|
217
|
|
|
* @return string The close tag as a string |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function createCloseTag($tagName) |
|
220
|
|
|
{ |
|
221
|
|
|
return '</'.$tagName.'>'; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Create a link to be added to the document |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $linkName The text inside the link |
|
228
|
|
|
* @param string $linkTarget The location the link goes to |
|
229
|
|
|
* |
|
230
|
|
|
* @return string The link |
|
231
|
|
|
*/ |
|
232
|
|
|
public function createLink($linkName, $linkTarget='#') |
|
233
|
|
|
{ |
|
234
|
|
|
$startTag = $this->createOpenTag('a', array('href'=>$linkTarget)); |
|
235
|
|
|
$endTag = $this->createCloseTag('a'); |
|
236
|
|
|
return $startTag.$linkName.$endTag; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Add tags to the header to make the IE family of browsers behave better |
|
241
|
|
|
* |
|
242
|
|
|
* The IE family of browsers lower than version 9 do not support HTML 5 so we need |
|
243
|
|
|
* to add a polyfill for those feaures. Additionally, IE versions greater than 8 |
|
244
|
|
|
* have a compatibility mode. We need to tell them to act as the latest greatest version |
|
245
|
|
|
* |
|
246
|
|
|
* @param string $prefix The prefix to append to each line |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function printIeCompatability($prefix='') |
|
249
|
|
|
{ |
|
250
|
|
|
//IE 8 doesn't support HTML 5. Install the shim... |
|
251
|
|
|
if($this->getBrowserMajorVer() < 9) |
|
252
|
|
|
{ |
|
253
|
|
|
echo $prefix.'<script src="js/html5.js"></script>'; |
|
254
|
|
|
echo "\n"; |
|
255
|
|
|
} |
|
256
|
|
|
//Tell the browser not to use compatability mode... |
|
257
|
|
|
echo $prefix.'<meta http-equiv="X-UA-Compatible" content="IE=edge"/>'; |
|
258
|
|
|
echo "\n"; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Print the HTML HEAD section |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $prefix The prefix to append to each line |
|
265
|
|
|
*/ |
|
266
|
|
|
protected function printHead($prefix='') |
|
267
|
|
|
{ |
|
268
|
|
|
echo $prefix.'<HEAD>'; |
|
269
|
|
|
if($this->getBrowserName() === 'IE') |
|
270
|
|
|
{ |
|
271
|
|
|
$this->printIeCompatability($prefix.$prefix); |
|
272
|
|
|
} |
|
273
|
|
|
echo $prefix.$prefix.'<TITLE>'.$this->title.'</TITLE>'; |
|
274
|
|
|
echo $prefix.$prefix.'<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
|
275
|
|
|
foreach($this->headTags as $tag) |
|
276
|
|
|
{ |
|
277
|
|
|
echo $prefix.$prefix.$tag."\n"; |
|
278
|
|
|
} |
|
279
|
|
|
echo $prefix.'</HEAD>'; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Print the HTML BODY section |
|
284
|
|
|
* |
|
285
|
|
|
* @param string $prefix The prefix to append to each line |
|
286
|
|
|
*/ |
|
287
|
|
|
protected function printBody($prefix='') |
|
288
|
|
|
{ |
|
289
|
|
|
echo $prefix.'<BODY '.$this->body_tags.'>'; |
|
290
|
|
|
echo $prefix.$prefix.$this->body."\n"; |
|
291
|
|
|
echo $prefix.'</BODY>'; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Get the currently requested URL |
|
296
|
|
|
* |
|
297
|
|
|
* @return string The full URL of the requested page |
|
298
|
|
|
* |
|
299
|
|
|
* @deprecated 1.0.0 This funciton is deprectated and will be remoted. Please use currentURL() instead |
|
300
|
|
|
*/ |
|
301
|
|
|
function current_url() |
|
|
|
|
|
|
302
|
|
|
{ |
|
303
|
|
|
return $this->currentURL(); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Get the currently requested URL |
|
308
|
|
|
* |
|
309
|
|
|
* @return string The full URL of the requested page |
|
310
|
|
|
* |
|
311
|
|
|
* @SuppressWarnings("Superglobals") |
|
312
|
|
|
*/ |
|
313
|
|
|
public function currentURL() |
|
314
|
|
|
{ |
|
315
|
|
|
if(!isset($_SERVER['REQUEST_URI'])) |
|
316
|
|
|
{ |
|
317
|
|
|
return ''; |
|
318
|
|
|
} |
|
319
|
|
|
$requestURI = $_SERVER['REQUEST_URI']; |
|
320
|
|
|
if($requestURI[0] === '/') |
|
321
|
|
|
{ |
|
322
|
|
|
$requestURI = substr($requestURI, 1); |
|
323
|
|
|
} |
|
324
|
|
|
return 'http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$_SERVER['HTTP_HOST'].'/'.$requestURI; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
|
328
|
|
|
?> |
|
|
|
|
|
|
329
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.