-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib_useragent.php
209 lines (162 loc) · 4.54 KB
/
lib_useragent.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
<?php
function useragent_decode($ua){
#
# a list of user agents, in order we'll match them.
# e.g. we put chrome before safari because chrome also
# claims it is safari (but the reverse is not true)
#
$agents = array(
'chrome',
'safari',
'konqueror',
'firefox',
'netscape',
'opera',
'msie',
'dalvik',
'blackberry',
);
$engines = array(
'webkit',
'gecko',
'trident',
'presto',
);
$ua = StrToLower($ua);
$out = array();
$temp = useragent_match($ua, $agents);
$out['agent'] = $temp['token'];
$out['agent_version'] = $temp['version'];
$temp = useragent_match($ua, $engines);
$out['engine'] = $temp['token'];
$out['engine_version'] = $temp['version'];
#
# safari does something super annoying, putting the version in the
# wrong place like: "Version/5.0.1 Safari/533.17.8"
#
# opera does the same thing:
# http://dev.opera.com/articles/view/opera-ua-string-changes/
#
if ($out['agent'] == 'safari' || $out['agent'] == 'opera'){
$temp = useragent_match($ua, array('version'));
if ($temp['token']) $out['agent_version'] = $temp['version'];
}
if ($out['agent'] == 'blackberry' && !$out['agent_version']){
if (preg_match('!blackberry(\d+)/(\S+)!', $ua, $m)){
$out['agent_version'] = $m[2];
}
}
#
# OS matching needs to do some regex transformations
#
$os = array(
'windows nt 5.1' => array('windows', 'xp'),
'windows nt 5.2' => array('windows', 'xp-x64'),
'windows nt 6.0' => array('windows', 'vista'),
'windows nt 6.1' => array('windows', '7'),
'windows nt 6.2' => array('windows', '8'),
'windows nt 6.3' => array('windows', '8.1'),
'android' => array('android', ''),
'linux i686' => array('linux', 'i686'),
'linux x86_64' => array('linux', 'x86_64'),
'(ipad; ' => array('ipad', ''),
'(ipod; ' => array('ipod', ''),
'(iphone; ' => array('iphone', ''),
'blackberry' => array('blackberry', ''),
);
$out['os'] = null;
$out['os_version'] = null;
foreach ($os as $k => $v){
if (strpos($ua, $k) !== false){
$out['os'] = $v[0];
$out['os_version'] = $v[1];
break;
}
}
if (in_array($out['os'], array('iphone', 'ipad', 'ipod'))){
if (preg_match('!os (\d+)[._](\d+)([._](\d+))? like mac os x!', $ua, $m)){
$out['os_version'] = "$m[1].$m[2]";
if ($m[4]) $out['os_version'] .= ".$m[4]";
}
}
if ($out['os'] == 'android'){
if (preg_match('!android (\d+)\.(\d+)(\.(\d+))?!', $ua, $m)){
$out['os_version'] = "$m[1].$m[2]";
if ($m[4]) $out['os_version'] .= ".$m[4]";
}
}
if ($out['os'] == 'blackberry'){
if (preg_match('!blackberry ?(\d+)!', $ua, $m)){
$out['os_version'] = $m[1];
}
}
if (is_null($out['os'])){
if (preg_match('!mac os x (\d+)[._](\d+)([._](\d+))?!', $ua, $m)){
$out['os'] = 'osx';
$out['os_version'] = "$m[1].$m[2]";
if ($m[4]) $out['os_version'] .= ".$m[4]";
}
}
return $out;
}
function useragent_match($ua, $tokens){
foreach ($tokens as $token){
if (preg_match("!{$token}[/ ]([0-9.]+\+?)!", $ua, $m)){
return array(
'token' => $token,
'version' => $m[1],
);
}
if (preg_match("!$token!", $ua)){
return array(
'token' => $token,
'version' => $null,
);
}
}
return array(
'token' => null,
'version' => null,
);
}
$GLOBALS['_useragent_cache'] = array();
$GLOBALS['_useragent_cache_max'] = 10000; # optimal cache size. when it gets 20% above this, we compact it
function useragent_decode_cached($ua){
#
# cache hit
#
if ($GLOBALS['_useragent_cache'][$ua]){
$GLOBALS['_useragent_cache'][$ua][1]++;
return $GLOBALS['_useragent_cache'][$ua][0];
}
#
# miss
#
$ret = useragent_decode($ua);
$GLOBALS['_useragent_cache'][$ua] = array($ret, 1, 0);
$compact_size = 1.2 * $GLOBALS['_useragent_cache_max'];
if (count($GLOBALS['_useragent_cache']) >= $compact_size){
#echo "compacting cache, since count is ".count($GLOBALS['_useragent_cache'])."\n";
#
# sort cache by hits-this, hits-prev DESC
#
uasort($GLOBALS['_useragent_cache'], '_useragent_sort_cache');
#print_r($GLOBALS['_useragent_cache']);
#
# trim
#
$GLOBALS['_useragent_cache'] = array_slice($GLOBALS['_useragent_cache'], 0, $GLOBALS['_useragent_cache_max']);
foreach ($GLOBALS['_useragent_cache'] as &$row){
$row[2] += $row[1];
$row[1] = 0;
}
#print_r($GLOBALS['_useragent_cache']);
#exit;
}
return $ret;
}
function _useragent_sort_cache($a, $b){
if ($a[1] == $b[1]) return $b[2] - $a[2];
return $b[1] - $a[1];
}
?>