Passed
Push — 1.0.0-dev ( cf37bc...0aa539 )
by nguereza
02:20
created

Browser::checkBrowserEdge()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
nc 4
nop 0
dl 0
loc 15
rs 9.6111
c 1
b 0
f 0
1
<?php
2
	defined('ROOT_PATH') || exit('Access denied');
3
    /**
4
     * File: Browser.php
5
     * Author: Chris Schuld (http://chrisschuld.com/)
6
     * Last Modified: July 22nd, 2016
7
     * @version 2.0
8
     * @package PegasusPHP
9
     *
10
     * Copyright (C) 2008-2010 Chris Schuld  ([email protected])
11
     *
12
     * This program is free software; you can redistribute it and/or
13
     * modify it under the terms of the GNU General Public License as
14
     * published by the Free Software Foundation; either version 2 of
15
     * the License, or (at your option) any later version.
16
     *
17
     * This program is distributed in the hope that it will be useful,
18
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
     * GNU General Public License for more details at:
21
     * http://www.gnu.org/copyleft/gpl.html
22
     *
23
     */
24
	class Browser {
25
26
		/**
27
		 * List of know platforms
28
		 * @var array
29
		 */
30
		private $_platforms = array(
31
									'/windows nt 10/i'      =>  'Windows 10',
32
									'/windows phone 10/i'   =>  'Windows Phone 10',
33
									'/windows phone 8.1/i'  =>  'Windows Phone 8.1',
34
									'/windows phone 8/i'    =>  'Windows Phone 8',
35
									'/windows nt 6.3/i'     =>  'Windows 8.1',
36
									'/windows nt 6.2/i'     =>  'Windows 8',
37
									'/windows nt 6.1/i'     =>  'Windows 7',
38
									'/windows nt 6.0/i'     =>  'Windows Vista',
39
									'/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
40
									'/windows nt 5.1/i'     =>  'Windows XP',
41
									'/windows xp/i'         =>  'Windows XP',
42
									'/windows nt 5.0/i'     =>  'Windows 2000',
43
									'/windows me/i'         =>  'Windows ME',
44
									'/win98/i'              =>  'Windows 98',
45
									'/win95/i'              =>  'Windows 95',
46
									'/win16/i'              =>  'Windows 3.11',
47
									'/macintosh|mac os x/i' =>  'Mac OS X',
48
									'/mac_powerpc/i'        =>  'Mac OS 9',
49
									'/iphone/i'             =>  'iPhone',
50
									'/ipod/i'               =>  'iPod',
51
									'/ipad/i'               =>  'iPad',
52
									'/android/i'            =>  'Android',
53
									'/ubuntu/i'             =>  'Ubuntu',
54
									'/linux/i'              =>  'Linux',
55
									'/blackberry/i'         =>  'BlackBerry',
56
									'/webos/i'              =>  'Mobile'
57
								);
58
59
		/**
60
		 * List of know browsers
61
		 * @var array
62
		 */
63
	 	private $_browsers = array(
64
									'/mobile/i'     =>  'Handheld Browser',
65
									'/msie/i'       =>  'Internet Explorer',
66
									'/firefox/i'    =>  'Firefox',
67
									'/chrome/i'     =>  'Chrome',
68
									'/safari/i'     =>  'Safari',
69
									'/edge/i'       =>  'Edge',
70
									'/opera/i'      =>  'Opera',
71
									'/netscape/i'   =>  'Netscape',
72
									'/maxthon/i'    =>  'Maxthon',
73
									'/konqueror/i'  =>  'Konqueror'
74
								);
75
76
	 	/**
77
	 	 * Agent string
78
	 	 * @var string
79
	 	 */
80
		private $_agent = '';
81
82
		/**
83
		 * Browser name
84
		 * @var string
85
		 */
86
        private $_browser_name = '';
87
88
        /**
89
         * Browser version
90
         * @var string
91
         */
92
        private $_version = '';
93
94
        /**
95
         * Platform or OS
96
         * @var string
97
         */
98
        private $_platform = '';
99
100
        /**
101
         * Whether is mobile
102
         * @var boolean
103
         */
104
        private $_is_mobile = false;
105
106
        /**
107
         * Whether is table
108
         * @var boolean
109
         */
110
        private $_is_tablet = false;
111
112
        /**
113
         * Whether is robot
114
         * @var boolean
115
         */
116
        private $_is_robot = false;
117
118
        /**
119
         * Whether is facebook external hit
120
         * @var boolean
121
         */
122
        private $_is_facebook = false;
123
124
		/**
125
         * Class constructor
126
         */
127
        public function __construct($userAgent = '') {
128
            $this->reset();
129
            if ($userAgent != '') {
130
                $this->setUserAgent($userAgent);
131
            } else {
132
                $this->determine();
133
            }
134
        }
135
136
        /**
137
         * Reset all properties
138
         */
139
        public function reset() {
140
            $this->_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
141
            $this->_browser_name = 'unknown';
142
            $this->_version = 'unknown';
143
            $this->_platform = 'unknown';
144
            $this->_is_mobile = false;
145
            $this->_is_tablet = false;
146
            $this->_is_robot = false;
147
            $this->_is_facebook = false;
148
        }
149
150
        /**
151
         * Get the user agent value in use to determine the browser
152
         * @return string The user agent
153
         */
154
        public function getUserAgent() {
155
            return $this->_agent;
156
        }
157
158
        /**
159
         * Set the user agent value
160
         * @param string $agent_string The value for the User Agent to set
161
         */
162
        public function setUserAgent($agent_string)
163
        {
164
            $this->reset();
165
            $this->_agent = $agent_string;
166
            $this->determine();
167
        }
168
169
        /**
170
         * The name of the browser.
171
         * @return string name of the browser
172
         */
173
        public function getBrowser() {
174
            return $this->_browser_name;
175
        }
176
177
        /**
178
         * Set the name of the browser
179
         * @param $browser string The name of the browser
180
         */
181
        public function setBrowser($browser) {
182
            $this->_browser_name = $browser;
183
        }
184
185
        /**
186
         * The name of the platform. 
187
         * @return string name of the platform
188
         */
189
        public function getPlatform() {
190
            return $this->_platform;
191
        }
192
193
        /**
194
         * Set the name of the platform
195
         * @param string $platform the name of the platform
196
         */
197
        public function setPlatform($platform) {
198
            $this->_platform = $platform;
199
        }
200
201
        /**
202
         * The version of the browser.
203
         * @return string Version of the browser (will only contain 
204
         * alpha-numeric characters and a period)
205
         */
206
        public function getVersion() {
207
            return $this->_version;
208
        }
209
210
        /**
211
         * Set the version of the browser
212
         * @param string $version the version of the browser
213
         */
214
        public function setVersion($version) {
215
            $this->_version = $version;
216
        }
217
218
219
        /**
220
         * Is the browser from a mobile device?
221
         * @return boolean true if the browser is from a mobile device otherwise false
222
         */
223
        public function isMobile() {
224
            return $this->_is_mobile;
225
        }
226
227
        /**
228
         * Is the browser from a tablet device?
229
         * @return boolean true if the browser is from a tablet device otherwise false
230
         */
231
        public function isTablet() {
232
            return $this->_is_tablet;
233
        }
234
235
        /**
236
         * Is the browser from a robot (ex Slurp,GoogleBot)?
237
         * @return boolean true if the browser is from a robot otherwise false
238
         */
239
        public function isRobot() {
240
            return $this->_is_robot;
241
        }
242
243
        /**
244
         * Is the browser from facebook?
245
         * @return boolean true if the browser is from facebook otherwise false
246
         */
247
        public function isFacebook() {
248
            return $this->_is_facebook;
249
        }
250
251
         /**
252
         * Returns a formatted string with a summary of the details of the browser.
253
         * @return string formatted string with a summary of the browser
254
         */
255
        public function __toString() {
256
            return "<strong>Browser Name:</strong> {$this->getBrowser()}<br/>\n" .
257
                "<strong>Browser Version:</strong> {$this->getVersion()}<br/>\n" .
258
                "<strong>Browser User Agent String:</strong> {$this->getUserAgent()}<br/>\n" .
259
                "<strong>Platform:</strong> {$this->getPlatform()}<br/>";
260
        }
261
262
263
        /**
264
         * Set the browser to be mobile
265
         * @param boolean $value is the browser a mobile browser or not
266
         */
267
        protected function setMobile($value = true) {
268
            $this->_is_mobile = $value;
269
        }
270
271
        /**
272
         * Set the browser to be tablet
273
         * @param boolean $value is the browser a tablet browser or not
274
         */
275
        protected function setTablet($value = true) {
276
            $this->_is_tablet = $value;
277
        }
278
279
        /**
280
         * Set the browser to be a robot
281
         * @param boolean $value is the browser a robot or not
282
         */
283
        protected function setRobot($value = true) {
284
            $this->_is_robot = $value;
285
        }
286
287
        /**
288
         * Set the browser to be a facebook request
289
         * @param boolean $value is the browser a robot or not
290
         */
291
        protected function setFacebook($value = true) {
292
            $this->_is_facebook = $value;
293
        }
294
295
        /**
296
         * Determine the user's platform
297
         */
298
		protected function checkPlatform() { 
299
			foreach ($this->_platforms as $regex => $value) { 
300
				if (preg_match($regex, $this->_agent) ) {
301
					$this->setPlatform($value);
302
					break;
303
				}
304
			}   
305
		}
306
307
		/**
308
         * Routine to determine the browser type
309
         */
310
		protected function checkBrowser() {
311
			foreach ($this->_browsers as $regex => $value) { 
312
				if (preg_match($regex, $this->_agent ) ) {
313
					$this->setBrowser($value);
314
					break;
315
				}
316
			}
317
		}
318
319
		/**
320
         * Routine to determine the browser version
321
         */
322
		protected function checkBrowserVersion(){
323
			$detected = $this->getBrowser();
324
			$d = array_search($detected, $this->_browsers);
325
			$browser = str_replace(array("/i","/"), "", $d);
326
			$regex = "/(?<browser>version|{$browser})[\/]+(?<version>[0-9.|a-zA-Z.]*)/i";
327
			if (preg_match_all($regex, $this->_agent, $matches)) {
328
				$found = array_search($browser, $matches["browser"]);
329
				$this->setVersion($matches["version"][$found]);
330
			}
331
		}
332
333
		/**
334
         * Determine if the browser is Mobile or not
335
         */
336
		protected function checkMobile() {
337
			if (preg_match('/mobile|phone|ipod/i', $this->_agent) ) {
338
				$this->setMobile(true);
339
			}
340
		}
341
342
		/**
343
         * Determine if the browser is Tablet or not
344
         */
345
		protected function checkTablet() {
346
			if (preg_match('/tablet|ipad/i', $this->_agent) ) {
347
				$this->setTablet(true);
348
			}
349
		}
350
351
		/**
352
         * Determine if the browser is Robot or not
353
         */
354
		protected function checkBot() {
355
			if (preg_match('/bot/i', $this->_agent) ) {
356
				$this->setTablet(true);
357
			}
358
		}
359
360
		/**
361
         * Detect if URL is loaded from FacebookExternalHit
362
         */
363
        protected function checkFacebook() {
364
            if (stristr($this->_agent, 'FacebookExternalHit')) {
365
                $this->setRobot(true);
366
                $this->setFacebook(true);
367
            }  else if (stristr($this->_agent, 'FBIOS')) {
368
                $this->setFacebook(true);
369
            }
370
        }
371
372
373
		 /**
374
         * Protected routine to calculate and determine what
375
         *  the browser is in use (including platform)
376
         */
377
        protected function determine() {
378
            $this->checkPlatform();
379
            $this->checkBrowser();
380
            $this->checkBrowserVersion();
381
            $this->checkMobile();
382
            $this->checkTablet();
383
            $this->checkBot();
384
            $this->checkFacebook();
385
        }
386
		
387
	}
388