-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAgent.php
More file actions
144 lines (133 loc) · 3.04 KB
/
Agent.php
File metadata and controls
144 lines (133 loc) · 3.04 KB
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
<?php
/**
* @package Flextype Components
*
* @author Sergey Romanenko <awilum@yandex.ru>
* @link http://components.flextype.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype\Component\Agent;
class Agent
{
/**
* Mobiles
*
* @var array
*/
public static $mobiles = [
'ipad',
'iphone',
'ipod',
'android',
'windows ce',
'windows phone',
'mobileexplorer',
'opera mobi',
'opera mini',
'fennec',
'blackberry',
'nokia',
'kindle',
'ericsson',
'motorola',
'minimo',
'iemobile',
'symbian',
'webos',
'hiptop',
'palmos',
'palmsource',
'xiino',
'avantgo',
'docomo',
'up.browser',
'vodafone',
'portable',
'pocket',
'mobile',
'phone',
];
/**
* Robots
*
* @var array
*/
public static $robots = [
'googlebot',
'msnbot',
'slurp',
'yahoo',
'askjeeves',
'fastcrawler',
'infoseek',
'lycos',
'ia_archiver',
];
/**
* Searches for a string in the user agent string.
*
* @param array $agents Array of strings to look for
* @return boolean
*/
protected static function find(array $agents) : bool
{
// If isset HTTP_USER_AGENT ?
if (isset($_SERVER['HTTP_USER_AGENT'])) {
// Loop through $agents array
foreach ($agents as $agent) {
// If current user agent == agents[agent] then return true
if (stripos($_SERVER['HTTP_USER_AGENT'], $agent) !== false) {
return true;
}
}
}
// Else return false
return false;
}
/**
* Returns true if the user agent that made the request is identified as a mobile device.
*
* if (Agent::isMobile()) {
* // Do something...
* }
*
* @return boolean
*/
public static function isMobile() : bool
{
return Agent::find(Agent::$mobiles);
}
/**
* Returns true if the user agent that made the request is identified as a robot/crawler.
*
* if (Agent::isRobot()) {
* // Do something...
* }
*
* @return boolean
*/
public static function isRobot() : bool
{
return Agent::find(Agent::$robots);
}
/**
* Returns TRUE if the string you're looking for exists in the user agent string and FALSE if not.
*
* if (Agent::is('iphone')) {
* // Do something...
* }
*
* if (Agent::is(array('iphone', 'ipod'))) {
* // Do something...
* }
*
* @param mixed $device String or array of strings you're looking for
* @return boolean
*/
public static function is($device) : bool
{
return Agent::find((array) $device);
}
}