Completed
Pull Request — develop (#49)
by Patrick
04:57 queued 01:49
created

WebPage::currentURL()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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