1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Nip Framework |
5
|
|
|
* |
6
|
|
|
* @category Nip |
7
|
|
|
* @copyright 2009 Nip Framework |
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
9
|
|
|
* @version SVN: $Id: Mobile_Detect.php 115 2009-05-21 12:48:26Z victor.stanciu $ |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
class Nip_Mobile_Detect { |
|
|
|
|
13
|
|
|
|
14
|
|
|
protected $accept; |
15
|
|
|
protected $userAgent; |
16
|
|
|
|
17
|
|
|
protected $isMobile = false; |
18
|
|
|
protected $isAndroid = null; |
19
|
|
|
protected $isBlackberry = null; |
20
|
|
|
protected $isOpera = null; |
21
|
|
|
protected $isPalm = null; |
22
|
|
|
protected $isWindows = null; |
23
|
|
|
protected $isGeneric = null; |
24
|
|
|
|
25
|
|
|
protected $devices = array( |
26
|
|
|
"android" => "android", |
27
|
|
|
"blackberry" => "blackberry", |
28
|
|
|
"iphone" => "(iphone|ipod)", |
29
|
|
|
"opera" => "opera mini", |
30
|
|
|
"palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)", |
31
|
|
|
"windows" => "windows ce; (iemobile|ppc|smartphone)", |
32
|
|
|
"generic" => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)" |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function __construct() { |
|
|
|
|
37
|
|
|
$this->userAgent = $_SERVER['HTTP_USER_AGENT']; |
38
|
|
|
$this->accept = $_SERVER['HTTP_ACCEPT']; |
39
|
|
|
|
40
|
|
|
if (isset($_SERVER['HTTP_X_WAP_PROFILE'])|| isset($_SERVER['HTTP_PROFILE'])) { |
41
|
|
|
$this->isMobile = true; |
42
|
|
|
} elseif (strpos($this->accept,'text/vnd.wap.wml') > 0 || strpos($accept,'application/vnd.wap.xhtml+xml') > 0) { |
|
|
|
|
43
|
|
|
$this->isMobile = true; |
44
|
|
|
} else { |
45
|
|
|
foreach ($this->devices as $device => $regexp) { |
46
|
|
|
if ($this->isDevice($device)) { |
47
|
|
|
$this->isMobile = true; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Overloads isAndroid() | isBlackberry() | isOpera() | isPalm() | isWindows() | isGeneric() through isDevice() |
56
|
|
|
* |
57
|
|
|
* @param string $name |
58
|
|
|
* @param array $arguments |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function __call($name, $arguments) { |
62
|
|
|
$device = substr($name, 2); |
63
|
|
|
if ($name == "is" . ucfirst($device)) { |
64
|
|
|
return $this->isDevice($device); |
65
|
|
|
} else { |
66
|
|
|
trigger_error("Method $name not defined", E_USER_ERROR); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns true if any type of mobile device detected, including special ones |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function isMobile() { |
76
|
|
|
return $this->isMobile; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
protected function isDevice($device) { |
81
|
|
|
$var = "is" . ucfirst($device); |
82
|
|
|
$return = $this->$var === null ? (bool) preg_match("/" . $this->devices[$device] . "/i", $this->userAgent) : $this->$var; |
83
|
|
|
|
84
|
|
|
if ($device != 'generic' && $return == true) { |
85
|
|
|
$this->isGeneric = false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $return; |
89
|
|
|
} |
90
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.