These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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(dirname(__FILE__).'/vendor/autoload.php'); |
||
18 | use BrowscapPHP\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 | 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() |
||
79 | { |
||
80 | $bc = new Browscap(); |
||
81 | $adapter = false; |
||
0 ignored issues
–
show
|
|||
82 | if(isset($GLOBALS['BROWSCAP_CACHE'])) |
||
83 | { |
||
84 | $adapter = new \WurflCache\Adapter\File([\WurflCache\Adapter\File::DIR => $GLOBALS['BROWSCAP_CACHE']]); |
||
85 | } |
||
86 | else |
||
87 | { |
||
88 | $adapter = new \WurflCache\Adapter\File([\WurflCache\Adapter\File::DIR => '/var/php_cache/browser']); |
||
89 | } |
||
90 | $bc->setCache($adapter); |
||
91 | return $bc; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Use the Browsecap library to determine what browser is being used to load this page |
||
96 | */ |
||
97 | private function getBrowser() |
||
98 | { |
||
99 | static $browser; //No accident can arise from depending on an unset variable. |
||
100 | if(!isset($browser)) |
||
101 | { |
||
102 | $browser = $this->browscap->getBrowser(); |
||
103 | } |
||
104 | return $browser; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get the name of the browser used to load this page |
||
109 | */ |
||
110 | private function getBrowserName() |
||
111 | { |
||
112 | if(isset($this->browser->Browser)) |
||
113 | { |
||
114 | return $this->browser->Browser; |
||
115 | } |
||
116 | return $this->browser->browser; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get the first part of the browser version number |
||
121 | * |
||
122 | * Determine what version of the browser is being used to load the page. This |
||
123 | * is used to determine if the version of IE is too old to be used |
||
124 | */ |
||
125 | private function getBrowserMajorVer() |
||
126 | { |
||
127 | if(isset($this->browser->MajorVer)) |
||
128 | { |
||
129 | return $this->browser->MajorVer; |
||
130 | } |
||
131 | return $this->browser->majorver; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Print the HTML doctype header |
||
136 | */ |
||
137 | protected function printDoctype() |
||
138 | { |
||
139 | echo '<!DOCTYPE html>'; |
||
140 | echo "\n"; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Print the opening HTML tag |
||
145 | */ |
||
146 | protected function printOpenHtml() |
||
147 | { |
||
148 | echo '<HTML lang="en">'; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Print the closing HTML tag |
||
153 | */ |
||
154 | protected function printCloseHtml() |
||
155 | { |
||
156 | echo '</HTML>'; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Print the page |
||
161 | * |
||
162 | * @deprecated 1.0.0 This funciton is deprectated and will be remoted. Please use printPage() instead |
||
163 | */ |
||
164 | public function print_page() |
||
165 | { |
||
166 | $this->printPage(); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Print the webpage to standard out |
||
171 | */ |
||
172 | public function printPage() |
||
173 | { |
||
174 | $this->printDoctype(); |
||
175 | $this->printOpenHtml(); |
||
176 | $this->printHead(' '); |
||
177 | $this->printBody(' '); |
||
178 | $this->printCloseHtml(); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Add a tag to the head element |
||
183 | * |
||
184 | * @param string $tag The tag to add to the page header |
||
185 | */ |
||
186 | public function addHeadTag($tag) |
||
187 | { |
||
188 | array_push($this->headTags, $tag); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Create a tag to be added to the document |
||
193 | * |
||
194 | * @param string $tagName The tag's name (i.e. the string right after the open sign |
||
195 | * @param array $attribs Attributes to be added to the tag in the form key=value |
||
196 | * @param boolean $selfClose Does this tag end with a close (/>)? |
||
197 | * |
||
198 | * @return string The tag as a string |
||
199 | */ |
||
200 | protected function createOpenTag($tagName, $attribs = array(), $selfClose = false) |
||
201 | { |
||
202 | $tag = '<'.$tagName; |
||
203 | $attribNames = array_keys($attribs); |
||
204 | foreach($attribNames as $attribName) |
||
205 | { |
||
206 | $tag .= ' '.$attribName; |
||
207 | if($attribs[$attribName]) |
||
208 | { |
||
209 | $tag .= '="'.$attribs[$attribName].'"'; |
||
210 | } |
||
211 | } |
||
212 | if($selfClose) |
||
213 | { |
||
214 | return $tag.'/>'; |
||
215 | } |
||
216 | return $tag.'>'; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Create a close tag to be added to the document |
||
221 | * |
||
222 | * @param string $tagName The tag's name (i.e. the string right after the open sign |
||
223 | * |
||
224 | * @return string The close tag as a string |
||
225 | */ |
||
226 | protected function createCloseTag($tagName) |
||
227 | { |
||
228 | return '</'.$tagName.'>'; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Create a link to be added to the document |
||
233 | * |
||
234 | * @param string $linkName The text inside the link |
||
235 | * @param string $linkTarget The location the link goes to |
||
236 | * |
||
237 | * @return string The link |
||
238 | */ |
||
239 | public function createLink($linkName, $linkTarget = '#') |
||
240 | { |
||
241 | $startTag = $this->createOpenTag('a', array('href'=>$linkTarget)); |
||
242 | $endTag = $this->createCloseTag('a'); |
||
243 | return $startTag.$linkName.$endTag; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Add tags to the header to make the IE family of browsers behave better |
||
248 | * |
||
249 | * The IE family of browsers lower than version 9 do not support HTML 5 so we need |
||
250 | * to add a polyfill for those feaures. Additionally, IE versions greater than 8 |
||
251 | * have a compatibility mode. We need to tell them to act as the latest greatest version |
||
252 | * |
||
253 | * @param string $prefix The prefix to append to each line |
||
254 | */ |
||
255 | protected function printIeCompatability($prefix = '') |
||
256 | { |
||
257 | //IE 8 doesn't support HTML 5. Install the shim... |
||
258 | if($this->getBrowserMajorVer() < 9) |
||
259 | { |
||
260 | echo $prefix.'<script src="js/html5.js"></script>'; |
||
261 | echo "\n"; |
||
262 | } |
||
263 | //Tell the browser not to use compatability mode... |
||
264 | echo $prefix.'<meta http-equiv="X-UA-Compatible" content="IE=edge"/>'; |
||
265 | echo "\n"; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Print the HTML HEAD section |
||
270 | * |
||
271 | * @param string $prefix The prefix to append to each line |
||
272 | */ |
||
273 | protected function printHead($prefix = '') |
||
274 | { |
||
275 | echo $prefix.'<HEAD>'; |
||
276 | if($this->getBrowserName() === 'IE') |
||
277 | { |
||
278 | $this->printIeCompatability($prefix.$prefix); |
||
279 | } |
||
280 | echo $prefix.$prefix.'<TITLE>'.$this->title.'</TITLE>'; |
||
281 | echo $prefix.$prefix.'<meta name="viewport" content="width=device-width, initial-scale=1.0">'; |
||
282 | foreach($this->headTags as $tag) |
||
283 | { |
||
284 | echo $prefix.$prefix.$tag."\n"; |
||
285 | } |
||
286 | echo $prefix.'</HEAD>'; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Print the HTML BODY section |
||
291 | * |
||
292 | * @param string $prefix The prefix to append to each line |
||
293 | */ |
||
294 | protected function printBody($prefix = '') |
||
295 | { |
||
296 | echo $prefix.'<BODY '.$this->body_tags.'>'; |
||
297 | echo $prefix.$prefix.$this->body."\n"; |
||
298 | echo $prefix.'</BODY>'; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Get the currently requested URL |
||
303 | * |
||
304 | * @return string The full URL of the requested page |
||
305 | * |
||
306 | * @SuppressWarnings("Superglobals") |
||
307 | */ |
||
308 | public function currentURL() |
||
309 | { |
||
310 | if(!isset($_SERVER['REQUEST_URI'])) |
||
311 | { |
||
312 | return ''; |
||
313 | } |
||
314 | $requestURI = $_SERVER['REQUEST_URI']; |
||
315 | if($requestURI[0] === '/') |
||
316 | { |
||
317 | $requestURI = substr($requestURI, 1); |
||
318 | } |
||
319 | return 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].'/'.$requestURI; |
||
320 | } |
||
321 | } |
||
322 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
323 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.