]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/php/mobile-detect.php
Merge branch '3.6-release'
[friendica-addons.git] / jappixmini / jappix / php / mobile-detect.php
1 <?php
2
3 /**
4  * Mobile Detect
5  *
6  * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
7  * @version    SVN: $Id: Mobile_Detect.php 3 2009-05-21 13:06:28Z vic.stanciu $
8  */
9
10 /**
11  * MODIFIED FOR THE JAPPIX PROJECT
12  * Last revision: 20/11/10
13  *
14  * Thanks to LinkMauve for his patch!
15  */
16
17 class Mobile_Detect {
18     
19     protected $accept;
20     protected $userAgent;
21     
22     protected $isMobile     = false;
23     protected $isAndroid    = null;
24     protected $isIphone     = null;
25     protected $isBlackberry = null;
26     protected $isOpera      = null;
27     protected $isPalm       = null;
28     protected $isWindows    = null;
29     protected $isGeneric    = null;
30
31     protected $devices = array(
32         "android"       => "android",
33         "blackberry"    => "blackberry",
34         "iphone"        => "(iphone|ipod)",
35         "opera"         => "opera mini",
36         "palm"          => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
37         "windows"       => "windows ce; (iemobile|ppc|smartphone)",
38         "generic"       => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)"
39     );
40
41
42     public function __construct() {
43         $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
44
45         if (isset($_SERVER['HTTP_ACCEPT']))
46                 $this->accept = $_SERVER['HTTP_ACCEPT'];
47         else
48                 $this->accept = '';
49
50         if (isset($_SERVER['HTTP_X_WAP_PROFILE'])|| isset($_SERVER['HTTP_PROFILE'])) {
51             $this->isMobile = true;
52         } elseif (strpos($this->accept,'text/vnd.wap.wml') > 0 || strpos($this->accept,'application/vnd.wap.xhtml+xml') > 0) {
53             $this->isMobile = true;
54         } else {
55             foreach ($this->devices as $device => $regexp) {
56                 if ($this->isDevice($device)) {
57                     $this->isMobile = true;
58                 }
59             }
60         }
61     }
62
63
64     /**
65      * Overloads isAndroid() | isBlackberry() | isOpera() | isPalm() | isWindows() | isGeneric() through isDevice()
66      *
67      * @param string $name
68      * @param array $arguments
69      * @return bool
70      */
71     public function __call($name, $arguments) {
72         $device = substr($name, 2);
73         if ($name == "is" . ucfirst($device)) {
74             return $this->isDevice($device);
75         } else {
76             trigger_error("Method $name not defined", E_USER_ERROR);
77         }
78     }
79
80
81     /**
82      * Returns true if any type of mobile device detected, including special ones
83      * @return bool
84      */
85     public function isMobile() {
86         return $this->isMobile;
87     }
88
89
90     protected function isDevice($device) {
91         $var    = "is" . ucfirst($device);
92         $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[$device] . "/i", $this->userAgent) : $this->$var;
93
94         if ($device != 'generic' && $return == true) {
95             $this->isGeneric = false;
96         }
97
98         return $return;
99     }
100 }