You are here

function browscap_get_browser in Browscap 5

Same name and namespace in other branches
  1. 8 browscap.module \browscap_get_browser()
  2. 6.2 browscap.module \browscap_get_browser()
  3. 6 browscap.module \browscap_get_browser()
  4. 7.2 browscap.module \browscap_get_browser()
  5. 7 browscap.module \browscap_get_browser()
2 calls to browscap_get_browser()
browscap_exit in ./browscap.module
Implementation of hook_exit().
browscap_unmonitor in ./browscap.module

File

./browscap.module, line 232
Replacement for PHP's get_browser() function

Code

function browscap_get_browser($useragent = NULL) {
  if (!$useragent) {
    $useragent = $_SERVER['HTTP_USER_AGENT'];
  }

  // Cache the results
  $cacheid = $useragent;
  $cache = cache_get($cacheid, 'cache_browscap');
  if (!empty($cache) and $cache->created > time() - 60 * 60 * 24) {

    // Found a fresh entry in the cache
    $browserinfo = unserialize($cache->data);
  }
  else {

    // Note the 'backwards' use of LIKE - the useragent column contains
    // the wildcarded pattern to match against our full-length string
    // The ORDER BY chooses the most-specific matching pattern
    $browserinfo = db_fetch_object(db_query_range("SELECT * from {browscap} WHERE '%s' LIKE useragent ORDER BY LENGTH(useragent) DESC", $useragent, 0, 1));

    // A couple of fieldnames not in our database, provided for
    // compatibility with PHP's get_browser()

    //$browserinfo->tables = $browserinfo->htmltables;
    cache_set($cacheid, 'cache_browscap', serialize($browserinfo));
  }
  $info = unserialize($browserinfo->data);
  $info['useragent'] = $useragent;
  $info['browser_name_pattern'] = strtr($browserinfo->useragent, '%_', '*?');
  return $info;
}