You are here

function better_statistics_better_statistics_prelog in Better Statistics 7

Implements hook_better_statistics_prelog().

Better Statistics provides inclusion/exclusion restrictions for collecting access log data based on request paths, user roles and cache status.

File

./better_statistics.statistics.inc, line 156
Statistics API functions and hooks for the Better Statistics module.

Code

function better_statistics_better_statistics_prelog() {

  // Skip if page is served from cache and Better Statistics is set to
  // exclude logging cached requests.
  $cache_status = better_statistics_served_from_cache();
  if ($cache_status && !variable_get('statistics_access_log_restrictions_cache', TRUE)) {
    better_statistics_request_is_loggable(FALSE);
    return;
  }

  // If there are page-based restrictions, check the page.
  $restrict_pages = variable_get('statistics_access_log_restrictions_page', 0);
  $pages = drupal_strtolower(variable_get('statistics_access_log_restrictions_pages', ''));
  if ($restrict_pages || !empty($pages)) {

    // Make sure drupal_match_path() is available.
    require_once DRUPAL_ROOT . '/' . variable_get('path_inc', 'includes/path.inc');

    // Make sure language is initialized for drupal_get_path_alias().
    global $language_url;
    if (!isset($language_url)) {
      drupal_language_initialize();
    }

    // Convert the Drupal path to lowercase.
    $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));

    // Compare the lowercase internal and lowercase path alias (if any).
    $page_match = drupal_match_path($path, $pages);
    if ($path != $_GET['q']) {
      $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
    }
    if ($page_match xor $restrict_pages) {
      better_statistics_request_is_loggable(FALSE);
      return;
    }
  }

  // If role restrictions are specified, check the array.
  $roles = variable_get('statistics_access_log_restrictions_roles', array());
  $restrictions_count = array_count_values($roles);
  if (isset($restrictions_count[0]) && $restrictions_count[0] < count($roles)) {

    // Skip if user is not part of an included role.
    global $user;
    $roles_check = array_intersect($roles, array_keys($user->roles));
    if (empty($roles_check)) {
      better_statistics_request_is_loggable(FALSE);
      return;
    }
  }

  // If DNT restrictions are specified, check headers.
  if (variable_get('statistics_access_log_restrictions_dnt', FALSE)) {

    // Respect the "do not track" header.
    if (isset($_SERVER['HTTP_DNT']) && $_SERVER['HTTP_DNT']) {
      better_statistics_request_is_loggable(FALSE);
    }
  }

  // If mixed-mode logging is enabled, check the page cache status.
  $mixed_mode = variable_get('statistics_enable_access_log', BETTER_STATISTICS_ACCESSLOG_DISABLED) == BETTER_STATISTICS_ACCESSLOG_MIXED;
  if ($mixed_mode && $cache_status) {
    better_statistics_request_is_loggable(FALSE);
    return;
  }
}