10 defined(
'JPATH_PLATFORM') or die;
35 const WINDOWS_PHONE = 2;
55 const ANDROIDTABLET = 22;
67 protected $mobile =
false;
91 protected $languages = array();
97 protected $encodings = array();
121 protected $robot =
false;
127 protected $detection = array();
138 public function __construct($userAgent = null, $acceptEncoding = null, $acceptLanguage = null)
141 if (empty($userAgent) && isset($_SERVER[
'HTTP_USER_AGENT']))
143 $this->userAgent = $_SERVER[
'HTTP_USER_AGENT'];
147 $this->userAgent = $userAgent;
151 if (empty($acceptEncoding) && isset($_SERVER[
'HTTP_ACCEPT_ENCODING']))
153 $this->acceptEncoding = $_SERVER[
'HTTP_ACCEPT_ENCODING'];
157 $this->acceptEncoding = $acceptEncoding;
161 if (empty($acceptLanguage) && isset($_SERVER[
'HTTP_ACCEPT_LANGUAGE']))
163 $this->acceptLanguage = $_SERVER[
'HTTP_ACCEPT_LANGUAGE'];
167 $this->acceptLanguage = $acceptLanguage;
180 public function __get($name)
186 if (empty($this->detection[
'platform']))
188 $this->detectPlatform($this->userAgent);
193 if (empty($this->detection[
'engine']))
195 $this->detectEngine($this->userAgent);
200 case 'browserVersion':
201 if (empty($this->detection[
'browser']))
203 $this->detectBrowser($this->userAgent);
208 if (empty($this->detection[
'acceptLanguage']))
210 $this->detectLanguage($this->acceptLanguage);
215 if (empty($this->detection[
'acceptEncoding']))
217 $this->detectEncoding($this->acceptEncoding);
222 if (empty($this->detection[
'robot']))
224 $this->detectRobot($this->userAgent);
230 if (isset($this->$name))
245 protected function detectBrowser($userAgent)
248 if ((stripos($userAgent,
'MSIE') !==
false) && (stripos($userAgent,
'Opera') ===
false))
250 $this->browser = self::IE;
251 $patternBrowser =
'MSIE';
253 elseif ((stripos($userAgent,
'Firefox') !==
false) && (stripos($userAgent,
'like Firefox') ===
false))
255 $this->browser = self::FIREFOX;
256 $patternBrowser =
'Firefox';
258 elseif (stripos($userAgent,
'Chrome') !==
false)
260 $this->browser = self::CHROME;
261 $patternBrowser =
'Chrome';
263 elseif (stripos($userAgent,
'Safari') !==
false)
265 $this->browser = self::SAFARI;
266 $patternBrowser =
'Safari';
268 elseif (stripos($userAgent,
'Opera') !==
false)
270 $this->browser = self::OPERA;
271 $patternBrowser =
'Opera';
278 $pattern =
'#(?<browser>Version|' . $patternBrowser .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
283 if (preg_match_all($pattern, $userAgent, $matches))
286 if (count($matches[
'browser']) == 2)
289 if (strripos($userAgent,
'Version') < strripos($userAgent, $patternBrowser))
291 $this->browserVersion = $matches[
'version'][0];
295 $this->browserVersion = $matches[
'version'][1];
298 elseif (count($matches[
'browser']) > 2)
300 $key = array_search(
'Version', $matches[
'browser']);
304 $this->browserVersion = $matches[
'version'][$key];
310 $this->browserVersion = $matches[
'version'][0];
316 $this->detection[
'browser'] =
true;
328 protected function detectEncoding($acceptEncoding)
331 $this->encodings = array_map(
'trim', (array) explode(
',', $acceptEncoding));
334 $this->detection[
'acceptEncoding'] =
true;
346 protected function detectEngine($userAgent)
349 if (stripos($userAgent,
'MSIE') !==
false || stripos($userAgent,
'Trident') !==
false)
351 $this->engine = self::TRIDENT;
354 elseif (stripos($userAgent,
'AppleWebKit') !==
false || stripos($userAgent,
'blackberry') !==
false)
356 $this->engine = self::WEBKIT;
359 elseif (stripos($userAgent,
'Gecko') !==
false && stripos($userAgent,
'like Gecko') ===
false)
361 $this->engine = self::GECKO;
364 elseif (stripos($userAgent,
'Opera') !==
false || stripos($userAgent,
'Presto') !==
false)
366 $this->engine = self::PRESTO;
369 elseif (stripos($userAgent,
'KHTML') !==
false)
371 $this->engine = self::KHTML;
374 elseif (stripos($userAgent,
'Amaya') !==
false)
376 $this->engine = self::AMAYA;
380 $this->detection[
'engine'] =
true;
392 protected function detectLanguage($acceptLanguage)
395 $this->languages = array_map(
'trim', (array) explode(
',', $acceptLanguage));
398 $this->detection[
'acceptLanguage'] =
true;
410 protected function detectPlatform($userAgent)
413 if (stripos($userAgent,
'Windows') !==
false)
415 $this->platform = self::WINDOWS;
418 if (stripos($userAgent,
'Windows Phone') !==
false)
420 $this->mobile =
true;
421 $this->platform = self::WINDOWS_PHONE;
423 elseif (stripos($userAgent,
'Windows CE') !==
false)
425 $this->mobile =
true;
426 $this->platform = self::WINDOWS_CE;
430 elseif (stripos($userAgent,
'iPhone') !==
false)
432 $this->mobile =
true;
433 $this->platform = self::IPHONE;
436 if (stripos($userAgent,
'iPad') !==
false)
438 $this->platform = self::IPAD;
440 elseif (stripos($userAgent,
'iPod') !==
false)
442 $this->platform = self::IPOD;
446 elseif (stripos($userAgent,
'iPad') !==
false)
448 $this->mobile =
true;
449 $this->platform = self::IPAD;
452 elseif (stripos($userAgent,
'iPod') !==
false)
454 $this->mobile =
true;
455 $this->platform = self::IPOD;
458 elseif (preg_match(
'/macintosh|mac os x/i', $userAgent))
460 $this->platform = self::MAC;
462 elseif (stripos($userAgent,
'Blackberry') !==
false)
464 $this->mobile =
true;
465 $this->platform = self::BLACKBERRY;
467 elseif (stripos($userAgent,
'Android') !==
false)
469 $this->mobile =
true;
470 $this->platform = self::ANDROID;
479 if (stripos($userAgent,
'Android 3') !==
false || stripos($userAgent,
'Tablet') !==
false
480 || stripos($userAgent,
'Mobile') ===
false || stripos($userAgent,
'Silk') !==
false )
482 $this->platform = self::ANDROIDTABLET;
485 elseif (stripos($userAgent,
'Linux') !==
false)
487 $this->platform = self::LINUX;
491 $this->detection[
'platform'] =
true;
503 protected function detectRobot($userAgent)
505 if (preg_match(
'/http|bot|robot|spider|crawler|curl|^$/i', $userAgent))
511 $this->robot =
false;
514 $this->detection[
'robot'] =
true;