Completed
Pull Request — master (#109)
by Patrick
10:35
created

class.WebPage.php (4 issues)

Upgrade to new PHP Analysis Engine

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;
0 ignored issues
show
The property doAutoUpdate does not seem to exist in BrowscapPHP\Browscap.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
56
        $this->browscap->lowercase = true;
0 ignored issues
show
The property lowercase does not seem to exist in BrowscapPHP\Browscap.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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
$adapter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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 = '#', $class=false)
240
    {
241
        $data =  array('href'=>$linkTarget);
242
        if($class !== false)
243
        {
244
            $data['class'] = $class;
245
        }
246
        $startTag = $this->createOpenTag('a', $data);
247
        $endTag = $this->createCloseTag('a');
248
        return $startTag.$linkName.$endTag;
249
    }
250
251
    /**
252
     * Add tags to the header to make the IE family of browsers behave better
253
     *
254
     * The IE family of browsers lower than version 9 do not support HTML 5 so we need
255
     * to add a polyfill for those feaures. Additionally, IE versions greater than 8
256
     * have a compatibility mode. We need to tell them to act as the latest greatest version
257
     *
258
     * @param string $prefix The prefix to append to each line
259
     */
260
    protected function printIeCompatability($prefix = '')
261
    {
262
        //IE 8 doesn't support HTML 5. Install the shim...
263
        if($this->getBrowserMajorVer() < 9)
264
        {
265
            echo $prefix.'<script src="js/html5.js"></script>';
266
            echo "\n";
267
        }
268
        //Tell the browser not to use compatability mode...
269
        echo $prefix.'<meta http-equiv="X-UA-Compatible" content="IE=edge"/>';
270
        echo "\n";
271
    }
272
273
    /**
274
     * Print the HTML HEAD section
275
     *
276
     * @param string $prefix The prefix to append to each line
277
     */
278
    protected function printHead($prefix = '')
279
    {
280
        echo $prefix.'<HEAD>';
281
        if($this->getBrowserName() === 'IE')
282
        {
283
            $this->printIeCompatability($prefix.$prefix);
284
        }
285
        echo $prefix.$prefix.'<TITLE>'.$this->title.'</TITLE>';
286
        echo $prefix.$prefix.'<meta name="viewport" content="width=device-width, initial-scale=1.0">';
287
        foreach($this->headTags as $tag)
288
        {
289
            echo $prefix.$prefix.$tag."\n";
290
        }
291
        echo $prefix.'</HEAD>';
292
    }
293
294
    /**
295
     * Print the HTML BODY section
296
     *
297
     * @param string $prefix The prefix to append to each line
298
     */
299
    protected function printBody($prefix = '')
300
    {
301
        echo $prefix.'<BODY '.$this->body_tags.'>';
302
        echo $prefix.$prefix.$this->body."\n";
303
        echo $prefix.'</BODY>';
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 View Code Duplication
    public function currentURL()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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