You are here

visitors.module in Visitors 7.0

File

visitors.module
View source
<?php

/**
 * @file
 * Logs visitors for your site.
 */
require_once dirname(__FILE__) . '/visitors.block.inc';
require_once dirname(__FILE__) . '/visitors.exit.inc';

/**
 * Implements of hook_help().
 */
function visitors_help($section) {
  switch ($section) {
    case 'visitors':
      return t('Visitors info.');
    default:
      return '';
  }
}

/**
 * Implements hook_permission().
 */
function visitors_permission() {
  return array(
    'administer visitors' => array(
      'title' => t('Administer visitors module'),
    ),
    'access visitors' => array(
      'title' => t('View reports'),
    ),
  );
}

/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function visitors_menu() {
  $items = array();
  $items['admin/config/system/visitors'] = array(
    'title' => 'Visitors settings',
    'description' => 'Configure visitors settings.',
    'access arguments' => array(
      'administer visitors',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'visitors_admin_settings',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'forms/admin_settings.inc',
  );
  $items['visitors'] = array(
    'title' => 'Visitors',
    'description' => "Visitors info.",
    'position' => 'left',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['visitors/hits'] = array(
    'title' => 'Recent hits',
    'description' => 'View pages that have recently been visited.',
    'page callback' => 'visitors_recent_hits',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['visitors/pages'] = array(
    'title' => 'Top pages',
    'description' => 'View pages that have been hit frequently.',
    'page callback' => 'visitors_top_pages',
    'access arguments' => array(
      'access visitors',
    ),
    'weight' => 1,
    'file' => 'visitors.admin.inc',
  );
  $items['visitors/monthly_history'] = array(
    'title' => 'Monthly history',
    'description' => 'View pages count per month.',
    'page callback' => 'visitors_monthly_history',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['visitors/monthly_history/chart'] = array(
    'title' => 'Monthly history',
    'description' => 'View pages count per month.',
    'page callback' => 'chart_visitors_monthly_history',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['visitors/days_of_month'] = array(
    'title' => 'Days of month',
    'description' => 'View pages count per day of month.',
    'page callback' => 'visitors_days_of_month',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['visitors/days_of_month/chart'] = array(
    'title' => 'Days of month',
    'description' => 'View pages count per day of month.',
    'page callback' => 'chart_visitors_days_of_month',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['visitors/days_of_week'] = array(
    'title' => 'Days of week',
    'description' => 'View pages count per day of week.',
    'page callback' => 'visitors_days_of_week',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
  );
  $items['visitors/days_of_week/chart'] = array(
    'title' => 'Days of week',
    'description' => 'View pages count per day of week.',
    'page callback' => 'chart_visitors_days_of_week',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['visitors/hours'] = array(
    'title' => 'Hours',
    'description' => 'View pages count per hour.',
    'page callback' => 'visitors_hours',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['visitors/hours/chart'] = array(
    'title' => 'Hours',
    'description' => 'View pages count per hour.',
    'page callback' => 'chart_visitors_hours',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['visitors/hosts'] = array(
    'title' => 'Hosts',
    'description' => 'View pages count per host.',
    'page callback' => 'visitors_hosts',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['visitors/user_activity'] = array(
    'title' => 'User activity',
    'description' => 'View user activity report (hits, nodes, comments).',
    'page callback' => 'visitors_user_activity',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['visitors/referers'] = array(
    'title' => 'Referers',
    'description' => 'View referer list.',
    'page callback' => 'visitors_referer_list',
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['visitors/hosts/%'] = array(
    'title' => 'Hits',
    'description' => 'View host access log.',
    'page callback' => 'visitors_host_hits',
    'page arguments' => array(
      2,
    ),
    'access arguments' => array(
      'access visitors',
    ),
    'file' => 'visitors.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['visitors/hits/%'] = array(
    'title' => 'Details',
    'description' => 'View access log.',
    'page callback' => 'visitors_hit_details',
    'page arguments' => array(
      2,
    ),
    'access arguments' => array(
      'access visitors',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'visitors.admin.inc',
  );
  $items['node/%node/visitors'] = array(
    'title' => 'Visitors',
    'page callback' => 'visitors_node',
    'access callback' => 'user_access',
    'access arguments' => array(
      'access visitors',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'visitors.admin.inc',
  );
  return $items;
}

/**
 * Implements of hook_theme().
 */
function visitors_theme() {
  return array(
    'visitors_block_view' => array(
      'arguments' => array(),
    ),
  );
}

/**
 * Implements of hook_cron().
 */
function visitors_cron() {
  $visitors_flush_log_timer = variable_get('visitors_flush_log_timer', 0);
  if ($visitors_flush_log_timer > 0) {

    // Clean up expired access logs.
    db_delete('visitors')
      ->condition('visitors_date_time', time() - $visitors_flush_log_timer, '<')
      ->execute();
  }
}

/**
 * Get value of MySQL system variable time_zone.
 *
 * @return string
 */
function visitors_get_mysql_current_timezone() {
  $query = 'SHOW variables LIKE \'time_zone\'';
  $data = db_query($query)
    ->fetchAssoc();
  return $data['value'];
}

/**
 * Get user timezone.
 *
 * @return
 *   string timezone
 */
function visitors_get_timezone() {
  $timezone = variable_get('date_default_timezone', 0);
  if (variable_get('configurable_timezones', 0) == '1') {
    global $user;
    if ($user->timezone !== NULL) {
      $timezone = $user->timezone;
    }
  }
  return $timezone;
}

/**
 * Get difference in seconds user timezone and GMT.
 *
 * @return int
 */
function visitors_timezone_diff() {
  $timezone = visitors_get_timezone();
  return timezone_offset_get(timezone_open($timezone), date_create());
}

/**
 * Set date format for sql query.
 *
 * @param $field_name string field name
 * @param $format string date format
 *
 * @return string date format
 */
function visitors_date_format_sql($field_name, $format) {
  switch (Database::getConnection()
    ->driver()) {
    case 'pgsql':
      return visitors_pgsql_date_format_sql($field_name, $format);
      break;
    default:
      return visitors_mysql_date_format_sql($field_name, $format);
  }
}

/**
 * Set date format for mysql sql query.
 *
 * @param $field_name string field name
 * @param $format string date format
 *
 * @return string date format
 */
function visitors_mysql_date_format_sql($field_name, $format) {
  $mysql_current_timezone = visitors_get_mysql_current_timezone();
  $diff = visitors_timezone_diff();
  $timezone = (int) ($diff / 60 / 60);
  $timezone .= sprintf(':%02d', abs($diff) / 60 % 60);
  if ($timezone >= 0) {
    $timezone = '+' . $timezone;
  }
  return sprintf("date_format(convert_tz(from_unixtime(%s), '%s', '%s'), '%s')", $field_name, $mysql_current_timezone, $timezone, $format);
}

/**
 * Set date format for pgsql sql query.
 *
 * @param $field_name string field name
 * @param $format string date format
 *
 * @return string date format
 */
function visitors_pgsql_date_format_sql($field_name, $format) {
  static $format_array = array(
    '%H' => "to_char(TIMESTAMP 'epoch' + (%s + (%d)) * INTERVAL '1 second', 'HH24')",
    '%a' => "to_char(TIMESTAMP 'epoch' + (%s + (%d)) * INTERVAL '1 second', 'dy')",
    '%w' => "cast(to_char(TIMESTAMP 'epoch' + (%s + (%d)) * INTERVAL '1 second', 'D') as integer) - 1",
    '%d' => "to_char(TIMESTAMP 'epoch' + (%s + (%d)) * INTERVAL '1 second', 'DD')",
    '%Y %M' => "to_char(TIMESTAMP 'epoch' + (%s + (%d)) * INTERVAL '1 second', 'YYYY Month')",
    '%Y%m' => "to_char(TIMESTAMP 'epoch' + (%s + (%d)) * INTERVAL '1 second', 'YYYYMM')",
  );
  if (isset($format_array[$format])) {
    $diff = visitors_timezone_diff();
    $result = sprintf($format_array[$format], $field_name, $diff);
  }
  else {
    $result = '';
  }
  return $result;
}

Functions

Namesort descending Description
visitors_cron Implements of hook_cron().
visitors_date_format_sql Set date format for sql query.
visitors_get_mysql_current_timezone Get value of MySQL system variable time_zone.
visitors_get_timezone Get user timezone.
visitors_help Implements of hook_help().
visitors_menu Menu callback. Prints a listing of active nodes on the site.
visitors_mysql_date_format_sql Set date format for mysql sql query.
visitors_permission Implements hook_permission().
visitors_pgsql_date_format_sql Set date format for pgsql sql query.
visitors_theme Implements of hook_theme().
visitors_timezone_diff Get difference in seconds user timezone and GMT.