-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceDetect.php
127 lines (106 loc) · 2.93 KB
/
DeviceDetect.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
<?php
/**
* Device type detection (phone, tablet, desktop) based on user agent strings
*
* @author Moritz Mertinkat <moritz@mertinkat.net>
* @license http://opensource.org/licenses/LGPL-3.0
*/
class DeviceDetect
{
/**
* User agent string
*
* @var string
*/
protected $userAgent = '';
/**
* Constructor
*
* @param string $userAgent User agent string
*/
public function __construct($userAgent)
{
$this->userAgent = $userAgent;
}
/**
* Checks whether we have a tablet
*
* @return boolean
*/
public function isTablet()
{
if (preg_match('/ipad/i', $this->userAgent)) {
return true;
}
if (preg_match('/android 3/i', $this->userAgent)) {
return true;
}
if (preg_match('/android/i', $this->userAgent) && !preg_match('/mobile|opera mobi/i', $this->userAgent)) {
return true;
}
if (preg_match('/firefox/i', $this->userAgent) && preg_match('/tablet/i', $this->userAgent)) {
return true;
}
if (preg_match('/tablet pc/i', $this->userAgent)) {
return true;
}
if (preg_match('/opera tablet/i', $this->userAgent)) {
return true;
}
if (preg_match('/rim tablet/i', $this->userAgent)) {
return true;
}
if (preg_match('/kindle fire|kindle\//i', $this->userAgent)) {
return true;
}
return false;
}
/**
* Checks whether we have a phone
*
* @return boolean
*/
public function isPhone()
{
if (preg_match('/iphone/i', $this->userAgent)) {
return true;
}
if (preg_match('/android 3/i', $this->userAgent)) {
return false;
}
if (preg_match('/kindle fire|kindle\//i', $this->userAgent)) {
return false;
}
if (preg_match('/mobile safari/i', $this->userAgent)) {
return true;
}
if (preg_match('/firefox/i', $this->userAgent) && preg_match('/mobile/i', $this->userAgent)) {
return true;
}
if (preg_match('/windows phone/i', $this->userAgent)) {
return true;
}
if (preg_match('/opera mobi/i', $this->userAgent)) {
return true;
}
return false;
}
/**
* Checks whether we have a mobile device (phone or tablet)
*
* @return boolean
*/
public function isMobile()
{
return $this->isPhone() || $this->isTablet();
}
/**
* Checks whether we have a desktop device (or a mobile device in desktop mode)
*
* @return boolean
*/
public function isDesktop()
{
return !$this->isMobile();
}
}