1 | <?php |
||
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 | public 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() |
||
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() |
||
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 | public function print_page() |
||
161 | |||
162 | /** |
||
163 | * Print the webpage to standard out |
||
164 | */ |
||
165 | public function printPage() |
||
166 | { |
||
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) |
||
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) |
||
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 = '') |
||
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 = '') |
||
293 | |||
294 | /** |
||
295 | * Get the currently requested URL |
||
296 | * |
||
297 | * @return string The full URL of the requested page |
||
298 | * |
||
299 | * @SuppressWarnings("Superglobals") |
||
300 | */ |
||
301 | public function currentURL() |
||
314 | } |
||
315 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
316 |