statistics_filter.module in Statistics Filter 7
Same filename and directory in other branches
Filter specified hits out from your statistics.
File
statistics_filter.moduleView source
<?php
/**
* @file
* Filter specified hits out from your statistics.
*/
/**
* Implements hook_help().
*/
function statistics_filter_help($path, $arg) {
switch ($path) {
case 'admin/modules#description':
return t('Filter out admin and search engine hits on your site.');
case 'admin/settings/statistics_filter':
return t('To ignore crawlers, you must install and enable the <strong><a href="@link">browscap</a></strong> module.', array(
'@link' => 'https://drupal.org/project/browscap',
));
}
}
/**
* Implements hook_perm().
*/
function statistics_filter_permission() {
return array(
'administer statistics filter' => array(
'title' => t('Administer Statistics Filter'),
'description' => t('Set the roles which should be filtered from site statistics'),
),
);
}
/**
* Implements hook_exit().
*
* Undo what statistics_exit() did for particular users.
*/
function statistics_filter_exit() {
global $user;
$do_filtering = statistics_filter_do_filter();
if (!$do_filtering) {
return;
}
// We don't want these stats to count, precisely undo what statistics.module
// and browscap.module did.
if (variable_get('statistics_count_content_views', 0)) {
// We are counting content views.
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
// A node has been viewed (and logged by the statistics module),
// so decrement what statistics.module incremented.
db_update('node_counter')
->expression('daycount', 'daycount - 1')
->expression('totalcount', 'totalcount - 1')
->condition('nid', arg(1))
->condition('daycount', 0, '>')
->execute();
}
}
if (variable_get('statistics_enable_access_log', 0)) {
// Statistical logs are enabled.
$title = truncate_utf8(strip_tags(drupal_get_title()), 255);
$path = truncate_utf8($_GET['q'], 255);
$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$hostname = ip_address();
$uid = $user->uid;
$sid = session_id();
$timestamp = REQUEST_TIME;
// Unlog this page access.
db_delete('accesslog')
->condition('title', $title)
->condition('path', $path)
->condition('url', $url)
->condition('hostname', $hostname)
->condition('uid', $uid)
->condition('sid', $sid)
->condition('timestamp', $timestamp)
->execute();
}
// Log the filter.
if (variable_get('statistics_filter_log', FALSE)) {
$browser = empty($_SERVER['HTTP_USER_AGENT']) ? 'Unknown' : $_SERVER['HTTP_USER_AGENT'];
$crawler = statistics_filter_is_crawler() | 0;
db_merge('statistics_filter_browsers')
->key(array(
'browser' => $browser,
))
->fields(array(
'browser' => $browser,
'counter' => 1,
'is_crawler' => $crawler,
'timestamp' => REQUEST_TIME,
))
->expression('counter', 'counter + 1')
->expression('is_crawler', $crawler)
->expression('timestamp', REQUEST_TIME)
->execute();
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see statistics_settings_form()
* @see statistics_filter_form_statistics_settings_validate()
*/
function statistics_filter_form_statistics_settings_form_alter(&$form, &$form_state) {
$roles = user_roles();
$default_roles = variable_get('statistics_filter_roles', array());
$form['filter'] = array(
'#type' => 'fieldset',
'#title' => t('Filter Settings'),
);
$form['filter']['statistics_filter_noadmin'] = array(
'#type' => 'checkbox',
'#title' => t('Ignore administrator (User-1)'),
'#description' => t("Ignore the special administrator account's accesses to this site."),
'#default_value' => variable_get('statistics_filter_noadmin', FALSE),
);
$form['filter']['statistics_filter_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Roles to ignore'),
'#description' => t('Ignore accesses by users in any of the checked roles.'),
'#default_value' => $default_roles,
'#options' => $roles,
);
if (module_exists('browscap')) {
$form['filter']['statistics_filter_crawlers'] = array(
'#type' => 'checkbox',
'#title' => t('Ignore crawlers'),
'#description' => t("Ignore search engine crawlers' accesses to this site."),
'#default_value' => variable_get('statistics_filter_crawlers', FALSE),
);
}
$form['filter']['statistics_filter_log'] = array(
'#type' => 'checkbox',
'#title' => t('Log filtered browser'),
'#prefix' => '<br/>',
'#default_value' => variable_get('statistics_filter_log', FALSE),
);
$form['#validate'][] = 'statistics_filter_form_statistics_settings_validate';
}
/**
* Validate statistics settings form.
*
* @see statistics_settings_form()
*/
function statistics_filter_form_statistics_settings_validate(&$form, &$form_state) {
$values =& $form_state['values'];
$values['statistics_filter_roles'] = array_filter($values['statistics_filter_roles']);
}
/**
* Determine request is from crawler or not.
*
* @return bool
* Crawler or not.
*/
function statistics_filter_is_crawler() {
if (module_exists('browscap')) {
// Cache result.
static $crawler = NULL;
if (is_null($crawler)) {
$browser = browscap_get_browser();
$crawler = !empty($browser['crawler']);
}
return $crawler;
}
return FALSE;
}
/**
* Determine we should do the filter or not.
*
* @return bool
* Filter or not.
*/
function statistics_filter_do_filter() {
global $user;
// Who are we ignoring?
$filter_admin = variable_get('statistics_filter_noadmin', FALSE);
if ($filter_admin && $user->uid == 1) {
return TRUE;
}
if (is_array($user->roles)) {
$filtered_roles = variable_get('statistics_filter_roles', FALSE);
if (is_array($filtered_roles) && !empty($filtered_roles)) {
foreach ($user->roles as $rid => $role) {
if (isset($filtered_roles[$rid])) {
return TRUE;
}
}
}
}
$filter_crawlers = variable_get('statistics_filter_crawlers', FALSE);
if ($filter_crawlers && statistics_filter_is_crawler()) {
return TRUE;
}
return FALSE;
}
Functions
Name![]() |
Description |
---|---|
statistics_filter_do_filter | Determine we should do the filter or not. |
statistics_filter_exit | Implements hook_exit(). |
statistics_filter_form_statistics_settings_form_alter | Implements hook_form_FORM_ID_alter(). |
statistics_filter_form_statistics_settings_validate | Validate statistics settings form. |
statistics_filter_help | Implements hook_help(). |
statistics_filter_is_crawler | Determine request is from crawler or not. |
statistics_filter_permission | Implements hook_perm(). |