You are here

visitors.exit.inc in Visitors 7.0

Same filename and directory in other branches
  1. 8 visitors.exit.inc
  2. 7.2 visitors.exit.inc
  3. 7 visitors.exit.inc

Implements of hook_exit().

File

visitors.exit.inc
View source
<?php

/* vim: set filetype=php: */

/**
 * @file
 * Implements of hook_exit().
 */

/**
 * Verify the syntax of the given ip address.
 *
 * @param ip
 *   A string containing an ip address.
 * @return
 *   TRUE if the ip is in a valid format, FALSE on failure.
 */
function visitors_is_ip_valid($ip) {
  $result = preg_match('/^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/', $ip, $matches);
  return $result && isset($matches[0]) && $matches[0] === $ip && $matches[1] >= 1 && $matches[1] <= 255 && $matches[2] >= 0 && $matches[2] <= 255 && $matches[3] >= 0 && $matches[3] <= 255 && $matches[4] >= 0 && $matches[4] <= 255;
}

/**
 * Get visitors ip address.
 *
 * @return
 *   A string containing an ip address ('0.0.0.0' on failure).
 */
function visitors_get_ip() {
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip_array = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $ip = trim(reset($ip_array));
  }
  else {
    $ip = $_SERVER['REMOTE_ADDR'];
  }
  return visitors_is_ip_valid($ip) ? $ip : '0.0.0.0';
}

/**
 * Converts a string containing an visitors (IPv4) Internet Protocol dotted
 * address into a proper address.
 *
 * @return
 *   string
 */
function visitors_get_ip_str() {
  return sprintf("%u", ip2long(visitors_get_ip()));
}

/**
 * Get full path request uri.
 *
 * @return
 *   string full path
 */
function visitors_get_url() {
  return urldecode(sprintf('http://%s%s', $_SERVER['HTTP_HOST'], request_uri()));
}

/**
 * Get internal path.
 *
 * @return
 *   string internal path
 */
function visitors_get_path() {
  return $_GET['q'];
}

/**
 * Get the address of the page (if any) which referred the user agent to the
 * current page.
 *
 * @return
 *   string referer, or empty string if referer does not exist
 */
function visitors_get_referer() {
  return isset($_SERVER['HTTP_REFERER']) ? urldecode($_SERVER['HTTP_REFERER']) : '';
}

/**
 * Get the title of the current page.
 *
 * @return
 *   string title of the current page
 */
function visitors_get_title() {
  return htmlspecialchars_decode(drupal_get_title(), ENT_QUOTES);
}

/**
 * Get visitor user agent.
 *
 * @return
 *   string user agent, or empty string if user agent does not exist
 */
function visitors_get_user_agent() {
  return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
}

/**
 * Implements of hook_exit().
 */
function visitors_exit() {
  drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  drupal_load('module', 'user');
  global $user;
  $not_admin = !in_array('administrator', $user->roles);
  $log_admin = !variable_get('visitors_exclude_administer_users', 0);
  if ($log_admin || $not_admin) {
    db_insert('visitors')
      ->fields(array(
      'visitors_uid' => $user->uid,
      'visitors_ip' => visitors_get_ip_str(),
      'visitors_date_time' => time(),
      'visitors_url' => visitors_get_url(),
      'visitors_referer' => visitors_get_referer(),
      'visitors_path' => visitors_get_path(),
      'visitors_title' => visitors_get_title(),
      'visitors_user_agent' => visitors_get_user_agent(),
    ))
      ->execute();
  }
}

Functions

Namesort descending Description
visitors_exit Implements of hook_exit().
visitors_get_ip Get visitors ip address.
visitors_get_ip_str Converts a string containing an visitors (IPv4) Internet Protocol dotted address into a proper address.
visitors_get_path Get internal path.
visitors_get_referer Get the address of the page (if any) which referred the user agent to the current page.
visitors_get_title Get the title of the current page.
visitors_get_url Get full path request uri.
visitors_get_user_agent Get visitor user agent.
visitors_is_ip_valid Verify the syntax of the given ip address.