-
Notifications
You must be signed in to change notification settings - Fork 4
/
Holmes.php
138 lines (121 loc) · 3.65 KB
/
Holmes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Holmes;
use BadMethodCallException;
class DeviceNotDetectedException extends \Exception {}
/**
* Holmes
* Based On http://code.google.com/p/php-mobile-detect/
* @modified Zack Kitzmiller
*/
class Holmes
{
/**
* @var array $devices regex and patterns from php-mobile-detect
*/
protected static $devices = array(
"android" => "android.*mobile",
"androidtablet" => "android.*chrome\/[.0-9]* (mobile)?",
"blackberry" => "blackberry",
"blackberrytablet" => "rim tablet os",
"iphone" => "(iphone|ipod)",
"ipad" => "(ipad)",
"ios" => "(iphone|ipod|ipad)",
"chromeios" => "CriOS",
'nintendo' => "(nintendo dsi|nintendo ds)",
"palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
"windows" => "windows ce; (iemobile|ppc|smartphone)",
"windowsphone" => "windows phone os",
"generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)"
);
/**
* Magic tester method.
*
* @param string $name method name
* @param array $arguments method arguments
* @return boolean static::isDevice result
*/
public static function __callStatic($name, $arguments)
{
$parts = preg_split('/(?=[A-Z])/', $name);
$device = strtolower(array_pop($parts));
if (array_key_exists($device, static::$devices))
{
return static::isDevice($device);
}
else
{
throw new BadMethodCallException('Invalid method called.');
}
}
/**
* Detect wether it's a tablet device
*
* @return boolean wether it's a tablet device
*/
public static function isTablet()
{
return static::isAndroidtablet() || static::isIpad();
}
/**
* Detect wether it's a mobile device
*
* @return boolean wether it's a mobile device
*/
public static function isMobile()
{
if ( empty($_SERVER['HTTP_ACCEPT']) )
{
return false;
}
$accept = $_SERVER['HTTP_ACCEPT'];
if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))
{
return true;
}
elseif (strpos($accept, 'text/vnd.wap.wml') > 0 || strpos($accept, 'application/vnd.wap.xhtml+xml') > 0)
{
return true;
}
foreach (array_keys(static::$devices) as $device)
{
if (static::isDevice($device))
{
return true;
}
}
return false;
}
/**
* Retrieve the device type
*
* @param boolean $default default device
* @return string device name
* @throws Holmes\DeviceNotDetectedException
*/
public static function getDevice($default = false)
{
foreach (array_keys(static::$devices) as $device)
{
if (static::isDevice($device))
{
return $device;
}
}
if ($default === false)
{
throw new DeviceNotDetectedException('Could not detect device.');
}
return $default;
}
/**
* Detect wether it's a certaim device
*
* @param string $device device name
* @return boolean wether it's a device
*/
protected static function isDevice($device)
{
$ua = $_SERVER['HTTP_USER_AGENT'];
return (bool) preg_match('/' . static::$devices[$device] . '/i', $ua);
}
}