…Yet Another Browser Detector
First I thought about calling this script ‹ExtrAgent›, then I thought, hey,
there’s a whole bunch of other browser detection scripts around, so actually
it’s just yet another browser detector!
I have just updated this code and replaced the old function with the new class Sd_Yabd. It takes part of my upcoming PHP framework «Sourdough» which will be released in april/may 2004.
This class should be capable to recognize all major browser/systems. Pass the agent string to extract_agent() and get the browser information by one of the follwing functions: getAgent(), getName(), getFullname(), getSystem(), getFullsystem(), isBot(), getVersion()
sample usage:
1 2 3 4 5 6 7 8 9 10 11 |
include_once('Sd_Yabd.class.php'); $YABD = new Sd_Yabd(); // if no agent string specified, get the current agent $YABD->printInfo(); // parse another agent string... $YABD->parse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"); $browserName = $YABD->getName(); // this should return 'IE' $browserFull = $YABD->getFullname(); // this should return 'Internet Explorer' $browserVer = $YABD->getVersion(); // this should return 6.0 $YABD->printInfo(); |
Sd_Yabd.class.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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
/** * SOURDOUGH Shared Library * YABD - Yet Another Browser Detector * * @final * @package sourdough * @author Philip Iezzi <pipo> * @copyright Copyright (c) 2000-2004 PHPEE.COM * * @version $Id: Sd_Yabd.class.php,v 1.9 2004/02/07 14:57:54 cvs_iezzi Exp $ */</pipo>class Sd_Yabd { /** * Agent * * @var string $agent * @access public */ var $agent = ''; /** * Agent name * * @var string $name * @access public */ var $name = ''; /** * Agent is a bot * * @var string $isBot * @access public */ var $isBot = false; /** * Browser version * * @var integer $v * @access public */ var $v = 0; /** * Browser version as a string * * @var string $v_str * @access public */ var $v_str = ''; /** * Operating system * * @var string $sys * @access public */ var $sys = ''; /** * Browser mapping array * * @var array $browserMap * @access public */ var $browserMap = array( 'IE' => 'Internet Explorer', 'NS' => 'Netscape', 'MZ' => 'Mozilla', 'OP' => 'Opera', 'KONQ' => 'Konqueror', 'OMNI' => 'OmniWeb', 'ICAB' => 'iCab', 'LX' => 'Lynx', 'NPOS' => 'NetPositive', 'FASTWC' => 'FAST-WebCrawler', 'W3C' => 'W3C Validator' ); /** * System mapping array * * @var array $systemMap * @access public */ var $systemMap = array( 'Win' => 'Windows', 'Win2000' => 'Windows 2000', 'Win95' => 'Windows 95', 'Win98' => 'Windows 98', 'WinMe' => 'Windows Me', 'WinXP' => 'Windows XP', 'WinServ2003' => 'Windows Server 2003', 'WinNT' => 'Windows NT', 'WinNT4.0' => 'Windows NT4', 'Mac' => 'Mac OS', 'MacOSX' => 'Mac OS X' ); /** * Agents * * This contains agent detection patterns in the following syntax: * array(pattern, is_bot, name, version, [system]) * * offline browsers: w3m, TeleportPro, WebCopier, WebStripper, WebZIP, WWWOFFLE * text-based: Lynx, w3m * file retrieval: Wget * web analytics: WebTrends * * @var string $_agents * @access private */ var $_agents = array( array("(opera) ([0-9]{1,2}.[0-9]{1,3}){0,1}" , false , "OP" , "%f"), array("(opera/)([0-9]{1,2}.[0-9]{1,3}){0,1}" , false , "OP" , "%f"), array("(konqueror)/([0-9]{1,2}.[0-9]{1,3})" , false , "KONQ" , "%f", "Linux"), array("(konqueror)/([0-9]{1,2})" , false , "KONQ" , "%f", "Linux"), array("(NetPositive)/([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})" , false , "NPOS" , "%s"), array("(iCab)/([0-9]{1,2}.[0-9]{1,3})" , false , "ICAB" , "%f"), array("(lynx)/([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})" , false , "LX" , "%s"), array("(links) (([0-9]{1,2}.[0-9]{1,3})" , false , "Links" , "%f"), array("(omniweb/)([0-9]{1,2}.[0-9]{1,3})" , false , "OMNI" , "%f"), array("(webtv/)([0-9]{1,2}.[0-9]{1,3})" , false , "WebTV" , "%f"), array("(msie) ([0-9]{1,2}.[0-9]{1,3})" , false , "IE" , "%f"), array("(Netscape)/([0-9]{1,2}.[0-9]{1,3})" , false , "NS" , "%f"), array("(netscape6)/(6.[0-9]{1,3})" , false , "NS" , "%f"), array("Mozilla/5.+(rv:)([0-9]{1,2}.[0-9]{1,3})" , false , "MZ" , "%f"), array("mozilla/5" , false , "NS" , 6.0), array("(mozilla)/([0-9]{1,2}.[0-9]{1,3})" , false , "NS" , "%f"), array("w3m" , false , "w3m" , 0), array("(scooter)-([0-9]{1,2}.[0-9]{1,3})" , false , "Scooter" , "%f"), array("(Teleport Pro)/([0-9]{1,2}.[0-9]{1,2})" , false , "TeleportPro", "%f"), array("(WebCopier) v([0-9]{1,2}.[0-9]{1,2})" , false , "WebCopier" , "%f"), array("(WebStripper)/([0-9]{1,2}.[0-9]{1,2})" , false , "WebStripper", "%f"), array("(WebZIP)/([0-9]{1,2}.[0-9]{1,2})" , false , "WebZIP" , "%f"), array("(WWWOFFLE)/([0-9]{1,2}.[0-9]{1,2})" , false , "WWWOFFLE" , "%f"), array("(Wget)/([0-9]{1,2}.[0-9]{1,2})" , false , "Wget" , "%f"), array("(w3c_validator)/([0-9]{1,2}.[0-9]{1,3})" , true , "W3C" , "%f"), array("(WebTrends)/([0-9]{1,2}.[0-9]{1,2})" , true , "WebTrends" , "%f"), array("(googlebot)/([0-9]{1,2}.[0-9]{1,3})" , true , "Google" , "%f"), array("(FAST-WebCrawler)/([0-9]{1,2}.[0-9]{1,2})" , true , "FASTWC" , "%f"), array("(search.ch) V([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})" , true , "Search.ch" , "%s"), array("(SpaceBison)/([0-9]{1,2}.[0-9]{1,2})" , true , "SpaceBison" , "%f"), array("(SuperBot)/([0-9]{1,2}.[0-9]{1,2})" , true , "SuperBot" , "%f") ); /** * Systems * * @var string $_systems * @access private */ var $_systems = array( // array(pattern, name) array("linux" , "Linux"), array("Win 9x 4.90" , "WinMe"), array("win32" , "Win"), array("windows 2000" , "Win2000"), array("(win)([9][5,8])" , "Win%s"), array("(windows) ([9][5,8])" , "Win%s"), array("(windows nt)( ){0,1}(5.0)" , "Win2000"), array("(windows nt)( ){0,1}(5.1)" , "WinXP"), array("(windows nt)( ){0,1}(5.2)" , "WinServ2003"), array("windows XP" , "WinXP"), array("(winnt)([0-4]{1,2}.[0-9]{1,2}){0,1}" , "WinNT%s"), array("windows nt( ){0,1}([0-4]{1,2}.[0-9]{1,2}){0,1}" , "WinNT%s"), array("PPC Mac OS X" , "MacOSX"), array("PPC" , "MacPPC"), array("Mac_PowerPC" , "MacPPC"), array("mac" , "Mac"), array("(sunos) ([0-9]{1,2}.[0-9]{1,2}){0,1}" , "SunOS%s"), array("(beos) r([0-9]{1,2}.[0-9]{1,2}){0,1}" , "BeOS%s"), array("freebsd" , "FreeBSD"), array("openbsd" , "OpenBSD"), array("irix" , "IRIX"), array("os/2" , "OS/2"), array("plan9" , "Plan9"), array("unix" , "Unix"), array("hp-ux" , "Unix"), array("osf" , "OSF"), array("X11" , "Unix") );/** * Sd_Yabd object constructor * * @access public * @param string agent string * @return void */ function Sd_Yabd($agt = '') { $this->parse($agt); } /** * object variable initialization * * @access private * @return void */ function _init() { $this->agent = ''; $this->name = ''; $this->isBot = false; $this->v = 0; $this->v_str = ''; $this->sys = ''; } /** * Extract browser, version, and OS from a given agent string * * If no agent string specified, try to get the current agent information * from $_SERVER["HTTP_USER_AGENT"]. * * @access public * @param string $agt agent string * @return array agent properties array */ function parse($agt = '') { $this->_init(); if(!$agt) $agt = $_SERVER["HTTP_USER_AGENT"]; $this->agent = $agt; // agent detection foreach($this->_agents as $arrAgent) { if(eregi($arrAgent[0], $this->agent, $regs)) { $this->isBot = $arrAgent[1]; $this->name = $arrAgent[2]; if (is_string($arrAgent[3])) { $this->v_str = sprintf($arrAgent[3], $regs[2]); $this->v = (float) $this->v_str; } else { $this->v = (float) $arrAgent[3]; } if(isset($arrAgent[4])) $this->sys = $arrAgent[4]; break; // agent detected, done. Jump out of the foreach loop } } // system detection if(!$this->sys) { foreach($this->_systems as $arrSystem) { if(eregi($arrSystem[0], $this->agent, $regs)) { $this->sys = (isset($regs[2])) ? sprintf($arrSystem[1], $regs[2]) : $arrSystem[1]; break; // system detected, done. Jump out of the foreach loop } } } $outp = array( $this->name, // agent name $this->isBot, // is a bot? $this->v, // version as float $this->v_str, // version as string $this->sys // system ); return($outp); } /** * gets the agent * * @access public * @return string */ function getAgent() { return($this->agent); } /** * gets the agent name * * @access public * @return string */ function getName() { return($this->name); } /** * gets the full agent name * * @access public * @return string */ function getFullname($name = '') { if(!$name) $name = $this->name; return (@$this->browserMap[$name]) ? $this->browserMap[$name] : $name; } /** * gets the operating system * * @access public * @return string */ function getSystem() { return($this->sys); } /** * gets the full system name * * @access public * @return string */ function getFullsystem() { return (@$this->systemMap[$this->sys]) ? $this->systemMap[$this->sys] : $this->sys; } /** * is agent a bot? * * @access public * @return boolean */ function isBot() { return($this->isBot); } /** * gets the browser version * * @access public * @return string */ function getVersion($asString = FALSE) { if ($asString && ($this->v_str > '')) { return($this->v_str); } else { return($this->v); } } /** * Get all agent information * for debugging purposes * * @access public * @return string * @see printInfo() */ function getInfo() { $str = 'agent................:'.htmlspecialchars($this->getAgent())."n" . 'name.................:'.$this->getName()."n" . 'full name............:'.$this->getFullname()."n" . 'is a bot.............:'.(($this->isBot()) ? 'true' : 'false')."n" . 'version..............:'.$this->getVersion()."n" . 'system...............:'.$this->getSystem()."n" . 'full system..........:'.$this->getFullsystem()."n"; return $str; } /** * Print all agent information * for debugging purposes * * @access public * @return void * @see getInfo() */ function printInfo($infoStr = '') { echo ($infoStr) ? $infoStr : $this->getInfo(); } } |