1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * Copyright (C) 2003 Gary White * |
---|
4 | * -------------------------------------------- * |
---|
5 | * This program is free software; you can redistribute it and/or modify it * |
---|
6 | * under the terms of the GNU General Public License as published by the * |
---|
7 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
8 | * option) any later version. * |
---|
9 | * * |
---|
10 | * This program is distributed in the hope that it will be useful, * |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
13 | * GNU General Public License for more details at: * |
---|
14 | * http://www.gnu.org/copyleft/gpl.html * |
---|
15 | \**************************************************************************/ |
---|
16 | |
---|
17 | /** |
---|
18 | * Interpreta informações enviadas pelo navegador do usuário |
---|
19 | * @author Gary White |
---|
20 | * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com (revision) |
---|
21 | * @version 1.0 |
---|
22 | * @package Workflow |
---|
23 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
24 | */ |
---|
25 | class BrowserInfo |
---|
26 | { |
---|
27 | /** |
---|
28 | * @var string $name O nome do navegador |
---|
29 | * @access private |
---|
30 | */ |
---|
31 | private $name = "Unknown"; |
---|
32 | |
---|
33 | /** |
---|
34 | * @var string $version A versão do navegador |
---|
35 | * @access private |
---|
36 | */ |
---|
37 | private $version = "Unknown"; |
---|
38 | |
---|
39 | /** |
---|
40 | * @var string $platform O sistema operacional do usuário |
---|
41 | * @access private |
---|
42 | */ |
---|
43 | private $platform = "Unknown"; |
---|
44 | |
---|
45 | /** |
---|
46 | * @var string $userAgent O agente do navegador do usuário |
---|
47 | * @access private |
---|
48 | */ |
---|
49 | private $userAgent = "Not reported"; |
---|
50 | |
---|
51 | /** |
---|
52 | * @var bool Indica se o MSIE é uma versão modificada pela AOL |
---|
53 | * @access private |
---|
54 | */ |
---|
55 | private $AOL = false; |
---|
56 | |
---|
57 | /** |
---|
58 | * Construtor da classe |
---|
59 | * @return object |
---|
60 | * @access public |
---|
61 | */ |
---|
62 | function BrowserInfo() |
---|
63 | { |
---|
64 | $agent = $_SERVER['HTTP_USER_AGENT']; |
---|
65 | |
---|
66 | // initialize properties |
---|
67 | $bd['platform'] = "Unknown"; |
---|
68 | $bd['browser'] = "Unknown"; |
---|
69 | $bd['version'] = "Unknown"; |
---|
70 | $this->userAgent = $agent; |
---|
71 | |
---|
72 | // find operating system |
---|
73 | if (preg_match('/win/i', $agent)) |
---|
74 | $bd['platform'] = "Windows"; |
---|
75 | elseif (preg_match('/mac/i', $agent)) |
---|
76 | $bd['platform'] = "MacIntosh"; |
---|
77 | elseif (preg_match('/linux/i', $agent)) |
---|
78 | $bd['platform'] = "Linux"; |
---|
79 | elseif (preg_match('/OS\/2/i', $agent)) |
---|
80 | $bd['platform'] = "OS/2"; |
---|
81 | elseif (preg_match('/BeOS/i', $agent)) |
---|
82 | $bd['platform'] = "BeOS"; |
---|
83 | |
---|
84 | // test for Opera |
---|
85 | if (preg_match('/opera/i',$agent)) |
---|
86 | { |
---|
87 | $val = stristr($agent, "opera"); |
---|
88 | if (preg_match('/\//i', $val)) |
---|
89 | { |
---|
90 | $val = explode("/",$val); |
---|
91 | $bd['browser'] = $val[0]; |
---|
92 | $val = explode(" ",$val[1]); |
---|
93 | $bd['version'] = $val[0]; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | $val = explode(" ",stristr($val,"opera")); |
---|
98 | $bd['browser'] = $val[0]; |
---|
99 | $bd['version'] = $val[1]; |
---|
100 | } |
---|
101 | |
---|
102 | // test for WebTV |
---|
103 | } |
---|
104 | elseif (preg_match('/webtv/i',$agent)) |
---|
105 | { |
---|
106 | $val = explode("/",stristr($agent,"webtv")); |
---|
107 | $bd['browser'] = $val[0]; |
---|
108 | $bd['version'] = $val[1]; |
---|
109 | |
---|
110 | // test for MS Internet Explorer version 1 |
---|
111 | } |
---|
112 | elseif (preg_match('/microsoft internet explorer/i', $agent)) |
---|
113 | { |
---|
114 | $bd['browser'] = "MSIE"; |
---|
115 | $bd['version'] = "1.0"; |
---|
116 | $var = stristr($agent, "/"); |
---|
117 | if (preg_match('/308|425|426|474|0b1/', $var)) |
---|
118 | $bd['version'] = "1.5"; |
---|
119 | |
---|
120 | // test for NetPositive |
---|
121 | } |
---|
122 | elseif (preg_match('/NetPositive/i', $agent)) |
---|
123 | { |
---|
124 | $val = explode("/",stristr($agent,"NetPositive")); |
---|
125 | $bd['platform'] = "BeOS"; |
---|
126 | $bd['browser'] = $val[0]; |
---|
127 | $bd['version'] = $val[1]; |
---|
128 | |
---|
129 | // test for MS Internet Explorer |
---|
130 | } |
---|
131 | elseif (preg_match('/msie/i',$agent) && !preg_match('/opera/i',$agent)) |
---|
132 | { |
---|
133 | $val = explode(" ",stristr($agent,"msie")); |
---|
134 | $bd['browser'] = $val[0]; |
---|
135 | $bd['version'] = $val[1]; |
---|
136 | |
---|
137 | // test for MS Pocket Internet Explorer |
---|
138 | } |
---|
139 | elseif (preg_match('/mspie/i',$agent) || preg_match('/pocket/i', $agent)) |
---|
140 | { |
---|
141 | $val = explode(" ",stristr($agent,"mspie")); |
---|
142 | $bd['browser'] = "MSPIE"; |
---|
143 | $bd['platform'] = "WindowsCE"; |
---|
144 | if (preg_match('/mspie/i', $agent)) |
---|
145 | $bd['version'] = $val[1]; |
---|
146 | else |
---|
147 | { |
---|
148 | $val = explode("/",$agent); |
---|
149 | $bd['version'] = $val[1]; |
---|
150 | } |
---|
151 | |
---|
152 | // test for Galeon |
---|
153 | } |
---|
154 | elseif (preg_match('/galeon/i',$agent)) |
---|
155 | { |
---|
156 | $val = explode(" ",stristr($agent,"galeon")); |
---|
157 | $val = explode("/",$val[0]); |
---|
158 | $bd['browser'] = $val[0]; |
---|
159 | $bd['version'] = $val[1]; |
---|
160 | |
---|
161 | // test for Konqueror |
---|
162 | } |
---|
163 | elseif (preg_match('/Konqueror/i',$agent)) |
---|
164 | { |
---|
165 | $val = explode(" ",stristr($agent,"Konqueror")); |
---|
166 | $val = explode("/",$val[0]); |
---|
167 | $bd['browser'] = $val[0]; |
---|
168 | $bd['version'] = $val[1]; |
---|
169 | |
---|
170 | // test for iCab |
---|
171 | } |
---|
172 | elseif (preg_match('/icab/i',$agent)) |
---|
173 | { |
---|
174 | $val = explode(" ",stristr($agent,"icab")); |
---|
175 | $bd['browser'] = $val[0]; |
---|
176 | $bd['version'] = $val[1]; |
---|
177 | |
---|
178 | // test for OmniWeb |
---|
179 | } |
---|
180 | elseif (preg_match('/omniweb/i',$agent)) |
---|
181 | { |
---|
182 | $val = explode("/",stristr($agent,"omniweb")); |
---|
183 | $bd['browser'] = $val[0]; |
---|
184 | $bd['version'] = $val[1]; |
---|
185 | |
---|
186 | // test for Phoenix |
---|
187 | } |
---|
188 | elseif (preg_match('/Phoenix/i', $agent)) |
---|
189 | { |
---|
190 | $bd['browser'] = "Phoenix"; |
---|
191 | $val = explode("/", stristr($agent,"Phoenix/")); |
---|
192 | $bd['version'] = $val[1]; |
---|
193 | |
---|
194 | // test for Firebird |
---|
195 | } |
---|
196 | elseif (preg_match('/firebird/i', $agent)) |
---|
197 | { |
---|
198 | $bd['browser']="Firebird"; |
---|
199 | $val = stristr($agent, "Firebird"); |
---|
200 | $val = explode("/",$val); |
---|
201 | $bd['version'] = $val[1]; |
---|
202 | |
---|
203 | // test for Firefox |
---|
204 | } |
---|
205 | elseif (preg_match('/Firefox/i', $agent)) |
---|
206 | { |
---|
207 | $bd['browser']="Firefox"; |
---|
208 | $val = stristr($agent, "Firefox"); |
---|
209 | $val = explode("/",$val); |
---|
210 | $bd['version'] = $val[1]; |
---|
211 | |
---|
212 | // test for Mozilla Alpha/Beta Versions |
---|
213 | } |
---|
214 | elseif (preg_match('/mozilla/i',$agent) && preg_match('/rv:[0-9].[0-9][a-b]/i',$agent) && !preg_match('/netscape/i',$agent)) |
---|
215 | { |
---|
216 | $bd['browser'] = "Mozilla"; |
---|
217 | $val = explode(" ",stristr($agent,"rv:")); |
---|
218 | preg_match('/rv:[0-9].[0-9][a-b]/i',$agent,$val); |
---|
219 | $bd['version'] = str_replace("rv:","",$val[0]); |
---|
220 | |
---|
221 | // test for Mozilla Stable Versions |
---|
222 | } |
---|
223 | elseif (preg_match('/mozilla/i',$agent) && preg_match('/rv:[0-9]\.[0-9]/i',$agent) && !preg_match('/netscape/i',$agent)) |
---|
224 | { |
---|
225 | $bd['browser'] = "Mozilla"; |
---|
226 | $val = explode(" ",stristr($agent,"rv:")); |
---|
227 | preg_match('/rv:[0-9]\.[0-9]\.[0-9]/i',$agent,$val); |
---|
228 | $bd['version'] = str_replace("rv:","",$val[0]); |
---|
229 | |
---|
230 | // test for Lynx & Amaya |
---|
231 | } |
---|
232 | elseif (preg_match('/libwww/i', $agent)) |
---|
233 | { |
---|
234 | if (preg_match('/amaya/i', $agent)) |
---|
235 | { |
---|
236 | $val = explode("/",stristr($agent,"amaya")); |
---|
237 | $bd['browser'] = "Amaya"; |
---|
238 | $val = explode(" ", $val[1]); |
---|
239 | $bd['version'] = $val[0]; |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | $val = explode("/",$agent); |
---|
244 | $bd['browser'] = "Lynx"; |
---|
245 | $bd['version'] = $val[1]; |
---|
246 | } |
---|
247 | |
---|
248 | // test for Safari |
---|
249 | } |
---|
250 | elseif (preg_match('/safari/i', $agent)) |
---|
251 | { |
---|
252 | $bd['browser'] = "Safari"; |
---|
253 | $bd['version'] = ""; |
---|
254 | |
---|
255 | // remaining two tests are for Netscape |
---|
256 | } |
---|
257 | elseif (preg_match('/netscape/i',$agent)) |
---|
258 | { |
---|
259 | $val = explode(" ",stristr($agent,"netscape")); |
---|
260 | $val = explode("/",$val[0]); |
---|
261 | $bd['browser'] = $val[0]; |
---|
262 | $bd['version'] = $val[1]; |
---|
263 | } |
---|
264 | elseif (preg_match('/mozilla/i',$agent) && !preg_match('/rv:[0-9]\.[0-9]\.[0-9]/i',$agent)) |
---|
265 | { |
---|
266 | $val = explode(" ",stristr($agent,"mozilla")); |
---|
267 | $val = explode("/",$val[0]); |
---|
268 | $bd['browser'] = "Netscape"; |
---|
269 | $bd['version'] = $val[1]; |
---|
270 | } |
---|
271 | |
---|
272 | // clean up extraneous garbage that may be in the name |
---|
273 | $bd['browser'] = preg_replace('/[^a-z,A-Z]/', '', $bd['browser']); |
---|
274 | // clean up extraneous garbage that may be in the version |
---|
275 | $bd['version'] = preg_replace('/[^0-9,.,a-z,A-Z]/', '', $bd['version']); |
---|
276 | |
---|
277 | // check for AOL |
---|
278 | if (preg_match('/AOL/i', $agent)) |
---|
279 | { |
---|
280 | $var = stristr($agent, "AOL"); |
---|
281 | $var = explode(" ", $var); |
---|
282 | $bd['aol'] = preg_replace('/[^0-9,.,a-z,A-Z]/', '', $var[1]); |
---|
283 | } |
---|
284 | |
---|
285 | // finally assign our properties |
---|
286 | $this->name = $bd['browser']; |
---|
287 | $this->version = $bd['version']; |
---|
288 | $this->platform = $bd['platform']; |
---|
289 | $this->AOL = $bd['aol']; |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * Checa se a versão do navegador é igual o superior que a requisitada |
---|
294 | * @param string $version A versão requisitada |
---|
295 | * @return bool true se a versão atual é igual ou superior que a requisitada ou false caso contrário |
---|
296 | * @access private |
---|
297 | */ |
---|
298 | private function checkVersion($version) |
---|
299 | { |
---|
300 | /* in case the version is unknown, return false */ |
---|
301 | if ($this->getBrowserVersion() == 'Unknown') |
---|
302 | return false; |
---|
303 | |
---|
304 | $required = explode('.', $version); |
---|
305 | $current = explode('.', $this->getBrowserVersion()); |
---|
306 | $required_count = count($required); |
---|
307 | for ($i = 0; $i < $required_count; ++$i) |
---|
308 | { |
---|
309 | $subRequired = (int) $required[$i]; |
---|
310 | $subCurrent = (int) (isset($current[$i]) ? $current[$i] : 0); |
---|
311 | if ($subCurrent < $subRequired) |
---|
312 | return false; |
---|
313 | if ($subCurrent > $subRequired) |
---|
314 | return true; |
---|
315 | } |
---|
316 | return true; |
---|
317 | } |
---|
318 | |
---|
319 | /** |
---|
320 | * Retorna o nome do navegador |
---|
321 | * @return string O nome do navegador |
---|
322 | * @access public |
---|
323 | */ |
---|
324 | public function getBrowserName() |
---|
325 | { |
---|
326 | return $this->name; |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * Retorna a versão do navegador |
---|
331 | * @return string A versão do navegador |
---|
332 | * @access public |
---|
333 | */ |
---|
334 | public function getBrowserVersion() |
---|
335 | { |
---|
336 | return $this->version; |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | * Retorna o sistema operacional do usuário |
---|
341 | * @return string O sistema operacional do usuário |
---|
342 | * @access public |
---|
343 | */ |
---|
344 | public function getOperatingSystem() |
---|
345 | { |
---|
346 | return $this->platform; |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | * Retorna o agente do navegador do usuário |
---|
351 | * @return string O agente do navegador do usuário |
---|
352 | * @access public |
---|
353 | */ |
---|
354 | public function getUserAgent() |
---|
355 | { |
---|
356 | return $this->userAgent; |
---|
357 | } |
---|
358 | |
---|
359 | /** |
---|
360 | * Checa o tipo e a versão do navegador de acordo com as informações requisitadas |
---|
361 | * @param string $browser O tipo requisitado |
---|
362 | * @param string $version A versão requisitada (opcional) |
---|
363 | * @return bool true se o navegador atual bate com os requisitos ou false caso contrário |
---|
364 | * @access private |
---|
365 | */ |
---|
366 | private function isBrowser($browser, $version = null) |
---|
367 | { |
---|
368 | if (strpos(strtolower($browser), strtolower($this->getBrowserName())) === 0) |
---|
369 | { |
---|
370 | if ($version == null) |
---|
371 | return true; |
---|
372 | else |
---|
373 | if ($this->checkVersion($version)) |
---|
374 | return true; |
---|
375 | } |
---|
376 | return false; |
---|
377 | } |
---|
378 | |
---|
379 | /** |
---|
380 | * Checa se o navegador é o Firefox |
---|
381 | * @param string $version A versão requisitada (opcional) |
---|
382 | * @return bool true se o navegador atual bate com os requisitos ou false caso contrário |
---|
383 | * @access private |
---|
384 | */ |
---|
385 | function isFirefox($version = null) |
---|
386 | { |
---|
387 | return $this->isBrowser('firefox', $version); |
---|
388 | } |
---|
389 | |
---|
390 | /** |
---|
391 | * Checa se o navegador é o Opera |
---|
392 | * @param string $version A versão requisitada (opcional) |
---|
393 | * @param string $version The required version (optional) |
---|
394 | * @return bool true se o navegador atual bate com os requisitos ou false caso contrário |
---|
395 | * @access private |
---|
396 | */ |
---|
397 | function isOpera($version = null) |
---|
398 | { |
---|
399 | return $this->isBrowser('opera', $version); |
---|
400 | } |
---|
401 | |
---|
402 | /** |
---|
403 | * Checa se o navegador é o Internet Explorer |
---|
404 | * @param string $version A versão requisitada (opcional) |
---|
405 | * @return bool true se o navegador atual bate com os requisitos ou false caso contrário |
---|
406 | * @access private |
---|
407 | */ |
---|
408 | function isIE($version = null) |
---|
409 | { |
---|
410 | return $this->isBrowser('msie', $version); |
---|
411 | } |
---|
412 | } |
---|
413 | ?> |
---|