You are here

browscap.module in Browscap 6

Replacement for PHP's get_browser() function.

File

browscap.module
View source
<?php

/**
 * @file
 * Replacement for PHP's get_browser() function.
 */

// Include browscap data import and user agent recording functions
include_once 'import.inc';
include_once 'record.inc';

/**
 * Implementation of hook_perm().
 */
function browscap_perm() {
  return array(
    'administer browscap',
    'view browscap reports',
    'bypass browscap monitoring',
  );
}

/**
 * Implementation of hook_menu().
 */
function browscap_menu() {
  $items['admin/settings/browscap'] = array(
    'title' => 'Browscap',
    'description' => 'Configure user agent monitoring and browscap information settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'browscap_settings_form',
    ),
    'access arguments' => array(
      'administer browscap',
    ),
    'file' => 'browscap.admin.inc',
  );
  $items['admin/reports/browscap'] = array(
    'title' => 'Browscap',
    'description' => 'Browser-specific site statistics.',
    'page callback' => 'browscap_top_useragents',
    'page arguments' => array(
      'all',
    ),
    'access arguments' => array(
      'view browscap reports',
    ),
    'file' => 'browscap.reports.inc',
    'weight' => 5,
  );
  $items['admin/reports/browscap/useragents'] = array(
    'title' => 'All',
    'access arguments' => array(
      'view browscap reports',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 1,
  );
  $items['admin/reports/browscap/browsers'] = array(
    'title' => 'Browsers',
    'page callback' => 'browscap_top_useragents',
    'page arguments' => array(
      'browsers',
    ),
    'access arguments' => array(
      'view browscap reports',
    ),
    'file' => 'browscap.reports.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
  );
  $items['admin/reports/browscap/crawlers'] = array(
    'title' => 'Crawlers',
    'page callback' => 'browscap_top_useragents',
    'page arguments' => array(
      'crawlers',
    ),
    'access arguments' => array(
      'view browscap reports',
    ),
    'file' => 'browscap.reports.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 3,
  );
  $items['admin/reports/browscap/useragent/%'] = array(
    'title' => 'Useragent details',
    'page callback' => 'browscap_useragent_properties',
    'page arguments' => array(
      4,
    ),
    'access arguments' => array(
      'view browscap reports',
    ),
    'file' => 'browscap.user_agent.inc',
  );
  return $items;
}

/**
 * Implements hook_help().
 */
function browscap_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/browscap':
      return '<p>' . t('Settings for user agent detection and the log that Browscap will keep about user agents that visit the site. See <a href="@statistics">user agent statistics</a> for the actual information.', array(
        '@statistics' => url('admin/reports/browscap'),
      )) . '</p>';
    case 'admin/reports/browscap':
      return '<p>' . t('This page displays the most popular visiting user agents.') . '</p>';
    case 'admin/reports/browscap/browsers':
      return '<p>' . t('This page displays the most popular visiting user agents which have been classified as browsers.') . '</p>';
    case 'admin/reports/browscap/crawlers':
      return '<p>' . t('This page displays the most popular visiting user agents which have been classified as crawlers.') . '</p>';
  }
}

/**
 * Implements hook_init().
 */
function browscap_init() {

  // Record the user agent and set a flag to denote that the user agent was
  // recorded during init() so it does not need to be recorded during exit()
  _record_user_agent(TRUE, user_access('bypass browscap monitoring'));
}

/**
 * Implementation of hook_exit().
 */
function browscap_exit() {

  // Record the user agent
  _record_user_agent();
}

/**
 * Implementation of hook_cron().
 */
function browscap_cron() {
  if (variable_get('browscap_enable_automatic_updates', TRUE) == TRUE) {

    // Check the current update timer
    $automatic_update_timer = variable_get('browscap_automatic_updates_timer', 604800);

    // Check when the last update occurred
    $last_imported = variable_get('browscap_imported', time());

    // Update the browscap data if the amount of time specified by the update
    // timer has passed
    if ($last_imported + $automatic_update_timer < time()) {

      // Update the browscap information
      _browscap_import();

      // Record when the browscap information was updated
      variable_set('browscap_imported', time());
    }
  }
}

/**
 * Provide data about a user agent string or the current user agent.
 *
 * @param string $user_agent
 *   Optional user agent string to test. If empty, use the value from the current request.
 * @return array
 *   An array of information about the user agent.
 */
function browscap_get_browser($user_agent = NULL) {

  // Determine the current user agent if a user agent was not specified
  if ($user_agent != NULL) {
    $user_agent = check_plain(trim($user_agent));
  }
  elseif ($user_agent == NULL && isset($_SERVER['HTTP_USER_AGENT'])) {
    $user_agent = check_plain(trim($_SERVER['HTTP_USER_AGENT']));
  }
  else {
    $user_agent = 'Default Browser';
  }

  // Check the cache for user agent data
  $cache = cache_get($user_agent, 'cache_browscap');

  // Attempt to find a cached user agent
  // Otherwise store the user agent data in the cache
  if (!empty($cache) && $cache->created > time() - 60 * 60 * 24) {
    $user_agent_properties = $cache->data;
  }
  else {

    // Find the user agent's properties
    // The useragent column contains the wildcarded pattern to match against our
    // full-length string while the ORDER BY chooses the most-specific matching
    // pattern
    $user_agent_properties = db_fetch_object(db_query_range("SELECT * from {browscap} WHERE '%s' LIKE useragent ORDER BY LENGTH(useragent) DESC", $user_agent, 0, 1));

    // Store user agent data in the cache
    cache_set($user_agent, $user_agent_properties, 'cache_browscap');
  }

  // Create an array to hold the user agent's properties
  $properties = array();

  // Return an array of user agent properties
  if (isset($user_agent_properties) && isset($user_agent_properties->data)) {

    // Unserialize the user agent data found in the cache or the database
    $properties = unserialize($user_agent_properties->data);

    // Set the user agent name and name pattern
    $properties['useragent'] = $user_agent;
    $properties['browser_name_pattern'] = strtr($user_agent_properties->useragent, '%_', '*?');
  }
  else {

    // Set the user agent name and name pattern to 'unrecognized'
    $properties['useragent'] = 'unrecognized';
    $properties['browser_name_pattern'] = strtr('unrecognized', '%_', '*?');
  }
  return $properties;
}

Functions

Namesort descending Description
browscap_cron Implementation of hook_cron().
browscap_exit Implementation of hook_exit().
browscap_get_browser Provide data about a user agent string or the current user agent.
browscap_help Implements hook_help().
browscap_init Implements hook_init().
browscap_menu Implementation of hook_menu().
browscap_perm Implementation of hook_perm().