You are here

visitors.module in Visitors 7

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\'';
  return db_query($query)
    ->fetchField(1);
}

/**
 * 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;
    case 'sqlite':
      return visitors_sqlite_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;
}

/**
 * Set date format for sqlite sql query.
 *
 * @param $field_name string field name
 * @param $format string date format
 *
 * @return string date format
 */
function visitors_sqlite_date_format_sql($field_name, $format) {
  static $format_array = array(
    '%H' => "strftime('%%H', %s + (%d), 'unixepoch')",
    '%a' => "case strftime('%%w', %s + (%d), 'unixepoch')\n                when '0' then 'Sun'\n                when '1' then 'Mon'\n                when '2' then 'Tue'\n                when '3' then 'Wed'\n                when '4' then 'Thu'\n                when '5' then 'Fri'\n                when '6' then 'Sat'\n                else '' end",
    '%w' => "strftime('%%w', %s + (%d), 'unixepoch')",
    '%d' => "strftime('%%d', %s + (%d), 'unixepoch')",
    '%Y %M' => "strftime('%%Y ', %1\$s + (%2\$d), 'unixepoch') ||\n                case strftime('%%m', %1\$s + (%2\$d), 'unixepoch')\n                when '01' then 'January'\n                when '02' then 'Febuary'\n                when '03' then 'March'\n                when '04' then 'April'\n                when '05' then 'May'\n                when '06' then 'June'\n                when '07' then 'July'\n                when '08' then 'August'\n                when '09' then 'September'\n                when '10' then 'October'\n                when '11' then 'November'\n                when '12' then 'December'\n                else '' end",
    '%Y%m' => "strftime('%%Y%%m', %s + (%d), 'unixepoch')",
  );
  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_sqlite_date_format_sql Set date format for sqlite sql query.
visitors_theme Implements of hook_theme().
visitors_timezone_diff Get difference in seconds user timezone and GMT.