You are here

visitors.module in Visitors 8.2

Logs visitors for your site.

File

visitors.module
View source
<?php

/**
 * @file
 * Logs visitors for your site.
 */
use Drupal\Core\Database\Database;

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

/**
 * Implements hook_theme().
 */
function visitors_theme() {
  return array(
    'visitors_jqplot' => array(
      'template' => 'visitors_jqplot',
      'variables' => array(
        'path' => null,
        'x' => null,
        'y' => null,
        'width' => null,
        'height' => null,
      ),
    ),
  );
}

/**
 * Implements of hook_cron().
 */
function visitors_cron() {
  $flush_log_timer = \Drupal::config('visitors.config')
    ->get('flush_log_timer', 0);
  if ($flush_log_timer > 0) {

    // Clean up expired access logs.
    \Drupal::database()
      ->delete('visitors')
      ->condition('visitors_date_time', time() - $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 \Drupal::database()
    ->query($query)
    ->fetchField(1);
}

/**
 * Get difference in seconds user timezone and GMT.
 *
 * @return int
 */
function visitors_timezone_diff() {
  $timezone = date_default_timezone_get();
  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;
}

/**
 * Build sql query from date filter values.
 *
 * @return string sql query.
 */
function visitors_date_filter_sql_condition(&$query) {
  visitors_set_session_date_range();
  $from = visitors_get_from_timestamp();
  $to = visitors_get_to_timestamp();
  $query
    ->condition('visitors_date_time', array(
    $from,
    $to,
  ), 'BETWEEN');
}

/**
 * Convert from date value to timestamp.
 *
 * @return returns the Unix timestamp of the session arguments given.
 *         If the arguments are invalid, the function returns FALSE
 *         (before PHP 5.1 it returned -1).
 */
function visitors_get_from_timestamp() {
  $diff = visitors_timezone_diff();
  $from = $_SESSION['visitors_from'];
  return gmmktime(0, 0, 0, $from['month'], $from['day'], $from['year']) - $diff;
}

/**
 * Convert to date value to timestamp.
 *
 * @return returns the Unix timestamp of the session arguments given.
 *         If the arguments are invalid, the function returns FALSE
 *         (before PHP 5.1 it returned -1).
 */
function visitors_get_to_timestamp() {
  $diff = visitors_timezone_diff();
  $to = $_SESSION['visitors_to'];
  return gmmktime(23, 59, 59, $to['month'], $to['day'], $to['year']) - $diff;
}

/**
 * Set to session info default values for visitors date filter.
 */
function visitors_set_session_date_range() {
  if (!isset($_SESSION['visitors_from'])) {
    $_SESSION['visitors_from'] = array(
      'day' => 1,
      'month' => date('n'),
      'year' => date('Y'),
    );
  }
  if (!isset($_SESSION['visitors_to'])) {
    $_SESSION['visitors_to'] = array(
      'day' => date('j'),
      'month' => date('n'),
      'year' => date('Y'),
    );
  }
}

Functions

Namesort descending Description
visitors_cron Implements of hook_cron().
visitors_date_filter_sql_condition Build sql query from date filter values.
visitors_date_format_sql Set date format for sql query.
visitors_get_from_timestamp Convert from date value to timestamp.
visitors_get_mysql_current_timezone Get value of MySQL system variable time_zone.
visitors_get_to_timestamp Convert to date value to timestamp.
visitors_help Implements of hook_help().
visitors_mysql_date_format_sql Set date format for mysql sql query.
visitors_pgsql_date_format_sql Set date format for pgsql sql query.
visitors_set_session_date_range Set to session info default values for visitors date filter.
visitors_sqlite_date_format_sql Set date format for sqlite sql query.
visitors_theme Implements hook_theme().
visitors_timezone_diff Get difference in seconds user timezone and GMT.