Test Failed
Push — develop ( fe93c2...a526c9 )
by nguereza
05:50
created

Browser::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
	defined('ROOT_PATH') || exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
    
31
	class Browser {
32
33
		/**
34
		 * List of know platforms
35
		 * @var array
36
		 */
37
		private $platforms = array(
38
									'/windows nt 10/i'      =>  'Windows 10',
39
									'/windows phone 10/i'   =>  'Windows Phone 10',
40
									'/windows phone 8.1/i'  =>  'Windows Phone 8.1',
41
									'/windows phone 8/i'    =>  'Windows Phone 8',
42
									'/windows nt 6.3/i'     =>  'Windows 8.1',
43
									'/windows nt 6.2/i'     =>  'Windows 8',
44
									'/windows nt 6.1/i'     =>  'Windows 7',
45
									'/windows nt 6.0/i'     =>  'Windows Vista',
46
									'/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
47
									'/windows nt 5.1/i'     =>  'Windows XP',
48
									'/windows xp/i'         =>  'Windows XP',
49
									'/windows nt 5.0/i'     =>  'Windows 2000',
50
									'/windows me/i'         =>  'Windows ME',
51
									'/win98/i'              =>  'Windows 98',
52
									'/win95/i'              =>  'Windows 95',
53
									'/win16/i'              =>  'Windows 3.11',
54
									'/ipad/i'               =>  'iPad',
55
                                    '/ipod/i'               =>  'iPod',
56
                                    '/iphone/i'             =>  'iPhone',
57
                                    '/macintosh|mac os x/i' =>  'Mac OS X',
58
									'/mac_powerpc/i'        =>  'Mac OS 9',
59
									'/android/i'            =>  'Android',
60
									'/ubuntu/i'             =>  'Ubuntu',
61
									'/linux/i'              =>  'Linux',
62
									'/blackberry/i'         =>  'BlackBerry',
63
									'/webos/i'              =>  'Mobile'
64
								);
65
66
		/**
67
		 * List of know browsers
68
		 * @var array
69
		 */
70
	 	private $browsers = array(
71
									'/mobile/i'     =>  'Handheld Browser',
72
									'/msie/i'       =>  'Internet Explorer',
73
									'/firefox/i'    =>  'Firefox',
74
									'/chrome/i'     =>  'Chrome',
75
									'/safari/i'     =>  'Safari',
76
									'/edge/i'       =>  'Edge',
77
									'/opera/i'      =>  'Opera',
78
									'/netscape/i'   =>  'Netscape',
79
									'/maxthon/i'    =>  'Maxthon',
80
									'/konqueror/i'  =>  'Konqueror'
81
								);
82
83
	 	/**
84
	 	 * Agent string
85
	 	 * @var string
86
	 	 */
87
		private $agent = '';
88
89
		/**
90
		 * Browser name
91
		 * @var string
92
		 */
93
        private $browserName = '';
94
95
        /**
96
         * Browser version
97
         * @var string
98
         */
99
        private $version = '';
100
101
        /**
102
         * Platform or OS
103
         * @var string
104
         */
105
        private $platform = '';
106
107
        /**
108
         * Whether is mobile
109
         * @var boolean
110
         */
111
        private $isMobile = false;
112
113
        /**
114
         * Whether is table
115
         * @var boolean
116
         */
117
        private $isTablet = false;
118
119
        /**
120
         * Whether is robot
121
         * @var boolean
122
         */
123
        private $isRobot = false;
124
125
        /**
126
         * Whether is facebook external hit
127
         * @var boolean
128
         */
129
        private $isFacebook = false;
130
131
		/**
132
         * Class constructor
133
         */
134
        public function __construct($userAgent = '') {
135
            $this->reset();
136
            if ($userAgent != '') {
137
                $this->setUserAgent($userAgent);
138
            } else {
139
                $this->determine();
140
            }
141
        }
142
143
        /**
144
         * Reset all properties
145
         */
146
        public function reset() {
147
            $this->agent =  get_instance()->globalvar->server('HTTP_USER_AGENT');
148
            $this->browserName = 'unknown';
149
            $this->version = 'unknown';
150
            $this->platform = 'unknown';
151
            $this->isMobile = false;
152
            $this->isTablet = false;
153
            $this->isRobot = false;
154
            $this->isFacebook = false;
155
        }
156
157
        /**
158
         * Get the user agent value in use to determine the browser
159
         * @return string the user agent
160
         */
161
        public function getUserAgent() {
162
            return $this->agent;
163
        }
164
165
        /**
166
         * Set the user agent value
167
         * @param string $agentString the value for the User Agent to set
168
         */
169
        public function setUserAgent($agentString)
170
        {
171
            $this->reset();
172
            $this->agent = $agentString;
173
            $this->determine();
174
        }
175
176
        /**
177
         * The name of the browser.
178
         * @return string name of the browser
179
         */
180
        public function getBrowser() {
181
            return $this->browserName;
182
        }
183
184
        /**
185
         * The name of the platform. 
186
         * @return string name of the platform
187
         */
188
        public function getPlatform() {
189
            return $this->platform;
190
        }
191
192
        /**
193
         * The version of the browser.
194
         * @return string Version of the browser (will only contain 
195
         * alpha-numeric characters and a period)
196
         */
197
        public function getVersion() {
198
            return $this->version;
199
        }
200
201
202
        /**
203
         * Is the browser from a mobile device?
204
         * @return boolean true if the browser is from a mobile device otherwise false
205
         */
206
        public function isMobile() {
207
            return $this->isMobile;
208
        }
209
210
        /**
211
         * Is the browser from a tablet device?
212
         * @return boolean true if the browser is from a tablet device otherwise false
213
         */
214
        public function isTablet() {
215
            return $this->isTablet;
216
        }
217
218
        /**
219
         * Is the browser from a robot (ex Slurp,GoogleBot)?
220
         * @return boolean true if the browser is from a robot otherwise false
221
         */
222
        public function isRobot() {
223
            return $this->isRobot;
224
        }
225
226
        /**
227
         * Is the browser from facebook?
228
         * @return boolean true if the browser is from facebook otherwise false
229
         */
230
        public function isFacebook() {
231
            return $this->isFacebook;
232
        }
233
234
         /**
235
         * Returns a formatted string with a summary of the details of the browser.
236
         * @codeCoverageIgnore
237
         * 
238
         * @return string formatted string with a summary of the browser
239
         */
240
        public function __toString() {
241
            return "<strong>Browser Name:</strong> {$this->getBrowser()}<br/>\n" .
242
                "<strong>Browser Version:</strong> {$this->getVersion()}<br/>\n" .
243
                "<strong>Browser User Agent String:</strong> {$this->getUserAgent()}<br/>\n" .
244
                "<strong>Platform:</strong> {$this->getPlatform()}<br/>";
245
        }
246
247
248
        /**
249
         * Determine the user's platform
250
         */
251
		protected function checkPlatform() { 
252
			foreach ($this->platforms as $regex => $value) { 
253
				if (preg_match($regex, $this->agent) ) {
254
					$this->platform = $value;
255
					break;
256
				}
257
			}   
258
		}
259
260
		/**
261
         * Routine to determine the browser type
262
         */
263
		protected function checkBrowser() {
264
			foreach ($this->browsers as $regex => $value) { 
265
				if (preg_match($regex, $this->agent ) ) {
266
					$this->browserName = $value;
267
					break;
268
				}
269
			}
270
		}
271
272
		/**
273
         * Routine to determine the browser version
274
         */
275
		protected function checkBrowserVersion(){
276
			$detected = $this->getBrowser();
277
			$detect = array_search($detected, $this->browsers);
278
			$browser = str_replace(array('/i','/'), '', $detect);
279
			$regex = "/(?<browser>version|{$browser})[\/]+(?<version>[0-9.|a-zA-Z.]*)/i";
280
			if (preg_match_all($regex, $this->agent, $matches)) {
281
				$found = array_search($browser, $matches['browser']);
282
				$this->version = $matches['version'][$found];
283
			}
284
		}
285
286
		/**
287
         * Determine if the browser is Mobile or not
288
         */
289
		protected function checkMobile() {
290
			if (preg_match('/(android|avantgo|blackberry|bolt|boost|cricket'
291
                . '|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i', $this->agent) ) {
292
				$this->isMobile = true;
293
			}
294
		}
295
296
		/**
297
         * Determine if the browser is Tablet or not
298
         */
299
		protected function checkTablet() {
300
			if (preg_match('/tablet|ipad/i', $this->agent) ) {
301
				$this->isTablet = true;
302
			}
303
		}
304
305
		/**
306
         * Determine if the browser is Robot or not
307
         */
308
		protected function checkBot() {
309
			if (preg_match('/bot/i', $this->agent) ) {
310
				$this->isRobot = true;
311
			}
312
		}
313
314
		/**
315
         * Detect if URL is loaded from FacebookExternalHit
316
         */
317
        protected function checkFacebook() {
318
            if (stristr($this->agent, 'FacebookExternalHit')) {
319
                $this->isRobot = true;
320
                $this->isFacebook = true;
321
            }  else if (stristr($this->agent, 'FBIOS')) {
322
                $this->isFacebook = true;
323
            }
324
        }
325
326
327
		 /**
328
         * Protected routine to calculate and determine what
329
         *  the browser is in use (including platform)
330
         */
331
        protected function determine() {
332
            $this->checkPlatform();
333
            $this->checkBrowser();
334
            $this->checkBrowserVersion();
335
            $this->checkMobile();
336
            $this->checkTablet();
337
            $this->checkBot();
338
            $this->checkFacebook();
339
        }
340
		
341
	}
342