forked from daemsa/synathina
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mobile_detect.php
483 lines (216 loc) · 8.74 KB
/
mobile_detect.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* Mobile Detect
* $Id: Mobile_Detect.php 37 2012-03-29 21:45:15Z serbanghita@gmail.com $
*
* @usage require_once 'Mobile_Detect.php';
* $detect = new Mobile_Detect();
* $detect->isMobile() or $detect->isTablet()
*
* For more specific usage see the documentation inside the class.
* $detect->isAndroidOS() or $detect->isiPhone() ...
*
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class Mobile_Detect {
protected $detectionRules;
protected $userAgent = null;
protected $accept = null;
// Assume the visitor has a desktop environment.
protected $isMobile = false;
protected $isTablet = false;
protected $phoneDeviceName = null;
protected $tabletDevicename = null;
protected $operatingSystemName = null;
protected $userAgentName = null;
// List of mobile devices (phones)
protected $phoneDevices = array(
'iPhone' => '(iPhone.*Mobile|iPod|iTunes)',
'BlackBerry' => 'BlackBerry|rim[0-9]+',
'HTC' => 'HTC|Desire',
'Nexus' => 'Nexus One|Nexus S',
'DellStreak' => 'Dell Streak',
'Motorola' => '\bDroid\b.*Build|HRI39|MOT\-',
'Samsung' => 'Samsung|GT\-P1000|SGH\-T959D|GT\-I9100|GT\-I9000',
'Sony' => 'E10i',
'Asus' => 'Asus.*Galaxy',
'Palm' => 'PalmSource|Palm', // avantgo|blazer|elaine|hiptop|plucker|xiino
'GenericPhone' => '(mmp|pocket|psp|symbian|Smartphone|smartfon|treo|up.browser|up.link|vodafone|wap|nokia|Series40|Series60|S60|SonyEricsson|N900|\bPPC\b|MAUI.*WAP.*Browser|LG\-P500)'
);
// List of tablet devices.
protected $tabletDevices = array(
'BlackBerryTablet' => 'PlayBook|RIM Tablet',
'iPad' => 'iPad.*Mobile',
'Kindle' => 'Kindle|Silk.*Accelerated',
'SamsungTablet' => 'SCH\-I800|GT\-P1000|Galaxy.*Tab',
'MotorolaTablet' => 'xoom|sholest',
'AsusTablet' => 'Transformer|TF101',
'GenericTablet' => 'Tablet|ViewPad7|LG\-V909|MID7015|BNTV250A|LogicPD Zoom2|\bA7EB\b|CatNova8|A1_07|CT704|CT1002|\bM721\b',
);
// List of mobile Operating Systems.
protected $operatingSystems = array(
'AndroidOS' => '(android.*mobile|android(?!.*mobile))',
'BlackBerryOS' => '(blackberry|rim tablet os)',
'PalmOS' => '(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)',
'SymbianOS' => 'Symbian|SymbOS|Series60|Series40|\bS60\b',
'WindowsMobileOS' => 'IEMobile|Windows Phone|Windows CE.*(PPC|Smartphone)|MSIEMobile|Window Mobile|XBLWP7',
'iOS' => '(iphone|ipod|ipad)',
'FlashLiteOS' => '',
'JavaOS' => '',
'NokiaOS' => '',
'webOS' => '',
'badaOS' => '\bBada\b',
'BREWOS' => '',
);
// List of mobile User Agents.
protected $userAgents = array(
'Chrome' => '\bCrMo\b',
'Dolfin' => '\bDolfin\b',
'Opera' => '(Opera.*Mini|Opera.*Mobi)',
'Skyfire' => 'skyfire',
'IE' => 'ie*mobile',
'Firefox' => 'fennec|firefox.*maemo',
'Bolt' => 'bolt',
'TeaShark' => 'teashark',
'Blazer' => 'Blazer',
'Safari' => 'Mobile*Safari',
'Midori' => 'midori',
'GenericBrowser' => 'NokiaBrowser|OviBrowser'
);
function __construct(){
// Merge all rules together.
$this->detectionRules = array_merge(
$this->phoneDevices,
$this->tabletDevices,
$this->operatingSystems,
$this->userAgents
);
$this->userAgent = $_SERVER['HTTP_USER_AGENT'];
$this->accept = @$_SERVER['HTTP_ACCEPT'];
if (
isset($_SERVER['HTTP_X_WAP_PROFILE']) ||
isset($_SERVER['HTTP_X_WAP_CLIENTID']) ||
isset($_SERVER['HTTP_WAP_CONNECTION']) ||
isset($_SERVER['HTTP_PROFILE']) ||
isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) || // Reported by Nokia devices (eg. C3)
isset($_SERVER['HTTP_X_NOKIA_IPADDRESS']) ||
isset($_SERVER['HTTP_X_NOKIA_GATEWAY_ID']) ||
isset($_SERVER['HTTP_X_ORANGE_ID']) ||
isset($_SERVER['HTTP_X_VODAFONE_3GPDPCONTEXT']) ||
isset($_SERVER['HTTP_X_HUAWEI_USERID']) ||
isset($_SERVER['HTTP_UA_OS']) || // Reported by Windows Smartphones
(isset($_SERVER['HTTP_UA_CPU']) && $_SERVER['HTTP_UA_CPU'] == 'ARM') // Seen this on a HTC
) {
$this->isMobile = true;
} elseif (!empty($this->accept) && (strpos($this->accept, 'text/vnd.wap.wml') !== false || strpos($this->accept, 'application/vnd.wap.xhtml+xml') !== false)) {
$this->isMobile = true;
} else {
$this->_detect();
}
}
public function getRules()
{
return $this->detectionRules;
}
/**
*
* @method boolean isiPhone()
* @method boolean isBlackBerry()
* @method boolean isHTC()
* @method boolean isNexus()
* @method boolean isDellStreak()
* @method boolean isMotorola()
* @method boolean isSamsung()
* @method boolean isSony()
* @method boolean isAsus()
* @method boolean isPalm()
*
* @method boolean isBlackBerryTablet()
* @method boolean isiPad()
* @method boolean isKindle()
* @method boolean isSamsungTablet()
* @method boolean isMotorolaTablet()
* @method boolean isAsusTablet()
*
* @method boolean isAndroidOS()
* @method boolean isBlackBerryOS()
* @method boolean isPalmOS()
* @method boolean isSymbianOS()
* @method boolean isWindowsMobileOS()
* @method boolean isiOS()
*
* @method boolean isChrome()
* @method boolean isDolfin()
* @method boolean isOpera()
* @method boolean isSkyfire()
* @method boolean isIE()
* @method boolean isFirefox()
* @method boolean isBolt()
* @method boolean isTeaShark()
* @method boolean isBlazer()
* @method boolean isSafari()
* @method boolean isMidori()
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
$key = substr($name, 2);
return $this->_detect($key);
}
private function _detect($key='')
{
if(empty($key)){
// Begin general search.
foreach($this->detectionRules as $_key => $_regex){
if(empty($_regex)){ continue; }
if(preg_match('/'.$_regex.'/is', $this->userAgent)){
$this->isMobile = true;
return true;
}
}
return false;
} else {
// Search for a certain key.
// Make the keys lowecase so we can match: isIphone(), isiPhone(), isiphone(), etc.
$key = strtolower($key);
$_rules = array_change_key_case($this->detectionRules);
if(array_key_exists($key, $_rules)){
if(empty($_rules[$key])){ return null; }
if(preg_match('/'.$_rules[$key].'/is', $this->userAgent)){
$this->isMobile = true;
return true;
} else {
return false;
}
} else {
trigger_error("Method $key is not defined", E_USER_WARNING);
}
return false;
}
}
/**
* Returns true if any type of mobile device detected, including special ones
* @return bool
*/
public function isMobile()
{
return $this->isMobile;
}
/**
* Return true if any type of tablet device is detected.
* @return boolean
*/
public function isTablet()
{
foreach($this->tabletDevices as $_key => $_regex){
if(preg_match('/'.$_regex.'/is', $this->userAgent)){
$this->isTablet = true;
return true;
}
}
return false;
}
}