You are here

function _browscap_import in Browscap 5

Same name and namespace in other branches
  1. 8 import.inc \_browscap_import()
  2. 6.2 import.inc \_browscap_import()
  3. 6 import.inc \_browscap_import()
  4. 7.2 import.inc \_browscap_import()
  5. 7 import.inc \_browscap_import()

If there's a new version of browscap.csv, fetch it and update the database.

2 calls to _browscap_import()
browscap_cron in ./browscap.module
Implementation of hook_cron().
browscap_refresh in ./browscap.module

File

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

Code

function _browscap_import($cron = TRUE) {

  // Politely check the version for updates before fetching the file
  $versionpage = drupal_http_request('http://browsers.garykeith.com/versions/version-number.asp');
  if ($versionpage->error) {
    watchdog('browscap', t("Couldn't check version: ") . $versionpage->error);
    if (!$cron) {
      drupal_set_message(t("Couldn't check version: ") . $versionpage->error, 'error');
    }
    return;
  }
  $browscapversion = trim($versionpage->data);
  $oldversion = variable_get('browscap_version', 'Never fetched');
  if ($browscapversion == $oldversion) {

    // No update, nothing to do here
    watchdog('browscap', t('No new version of browscap to import'));
    if (!$cron) {
      drupal_set_message(t('No new version of browscap to import'));
    }
    return;
  }

  // Fetch the new version, and dump it in the temp directory
  $server = $_SERVER['SERVER_NAME'];
  $path = variable_get('file_directory_temp', '/tmp');
  $browscapfile = "{$path}/browscap_{$server}.ini";
  $browscap = drupal_http_request('http://browsers.garykeith.com/stream.asp?PHP_BrowsCapINI');
  if ($browscap->error or !trim($browscap->data)) {
    watchdog('browscap', t("Couldn't retrieve updated browscap: ") . $browscap->error);
    if (!$cron) {
      drupal_set_message(t("Couldn't retrieve updated browscap: ") . $browscap->error);
    }
    return;
  }
  $browscapfp = fopen($browscapfile, "w");
  fwrite($browscapfp, $browscap->data);
  fclose($browscapfp);
  $a = parse_ini_file($browscapfile, TRUE);
  if ($a) {

    // the first entry in the array is the version info
    $version = array_shift($a);
    foreach ($a as $key => $vals) {
      $e = $vals;

      // some recursive magic!
      $last_parent = array();
      while ($vals['Parent'] && $vals['Parent'] !== $last_parent) {
        $vals = $a[$vals['Parent']];
        $e = array_merge((array) $vals, (array) $e);
        $last_parent = $vals;
      }
      $useragent = strtr($key, '*?', '%_');
      $e = array_change_key_case($e);
      db_query("REPLACE INTO {browscap} (useragent, data) VALUES('%s','%s')", $useragent, serialize($e));
    }
    cache_clear_all('*', 'cache_browscap', TRUE);
    variable_set('browscap_version', $browscapversion);
    watchdog('browscap', t("New version of browscap imported: ") . $browscapversion);
    if (!$cron) {
      drupal_set_message(t("New version of browscap imported: ") . $browscapversion);
    }
  }
}