You are here

ip.module in IP address manager 7.2

Same filename and directory in other branches
  1. 8.2 ip.module
  2. 6 ip.module
  3. 7 ip.module

IP address manager module.

File

ip.module
View source
<?php

/**
 * @file
 * IP address manager module.
 */

/**
 * Implements hook_permission().
 */
function ip_permission() {
  return array(
    'manage ip addresses' => array(
      'title' => t('Manage IP addresses'),
    ),
  );
}

/**
 * Implements hook_user_login().
 */
function ip_user_login(&$edit, $account) {
  ip_tracker_user_save($account->uid);
}

/**
 * Implements hook_user_insert().
 */
function ip_user_insert(&$edit, $account) {
  global $user;

  // Track anonymous registration (Do not track administration account creation).
  if (empty($user->uid) || $user->uid == $account->uid) {
    ip_tracker_user_save($account->uid);
  }
}

/**
 * Save a record into the ip_tracker table.
 * @NOTE: Maybe you want to check the user uid vs $uid before calling this function.
 */
function ip_tracker_user_save($uid) {
  $iplong = ip2long(ip_address());
  if (!empty($iplong)) {

    // Check to see if a row exists for this uid/ip combination.
    $sql = "SELECT visits FROM {ip_tracker} WHERE uid = :uid AND ip = :ip";
    $args = array(
      ':uid' => $uid,
      ':ip' => $iplong,
    );
    $visits = db_query($sql, $args)
      ->fetchField();
    if ($visits) {

      // Update.
      return db_update('ip_tracker')
        ->fields(array(
        'visits' => $visits + 1,
        'last_visit' => REQUEST_TIME,
      ))
        ->condition('uid', $uid)
        ->condition('ip', $iplong)
        ->execute();
    }
    else {

      // Insert.
      return db_insert('ip_tracker')
        ->fields(array(
        'uid' => $uid,
        'ip' => $iplong,
        'visits' => 1,
        'first_visit' => REQUEST_TIME,
        'last_visit' => REQUEST_TIME,
      ))
        ->execute();
    }
  }
}

/**
 * Remove records in the ip_tracker table for a certain user.
 */
function ip_tracker_user_remove($uid) {
  return db_delete('ip_tracker')
    ->condition('uid', $uid)
    ->execute();
}

/**
 * Get the records from the tracker for a user.
 */
function ip_tracker_user_records($uid) {
  $sql = "SELECT * FROM {ip_tracker} WHERE uid = :uid";
  $args = array(
    ':uid' => $uid,
  );
  $result = db_query($sql, $args);
  $rows = array();
  foreach ($result as $row) {
    $row->ip = long2ip($row->ip);
    $rows[] = $row;
  }
  return $rows;
}

/**
 * Get the records from the tracker for an IP.
 */
function ip_tracker_ip_records($ip) {
  $sql = "SELECT * FROM {ip_tracker}";
  $args = array();
  if (!empty($ip)) {
    $sql .= " WHERE ip = :ip";
    $args[':ip'] = ip2long($ip);
  }
  $result = db_query($sql, $args);
  $rows = array();
  foreach ($result as $row) {
    $row->ip = long2ip($row->ip);
    $rows[] = $row;
  }
  return $rows;
}

/**
 * Count how many users have been tracked against an IP.
 */
function ip_tracker_ip_user_count($ip) {
  $sql = "SELECT COUNT(uid) FROM {ip_tracker} WHERE ip = :ip";
  $args = array(
    ':ip' => ip2long($ip),
  );
  $count = db_query($sql, $args)
    ->fetchField();
  return $count;
}

/**
 * Implements hook_node_insert().
 */
function ip_node_insert($node) {
  ip_posts_insert('node', $node->nid);
}

/**
 * Implements hook_comment_insert().
 */
function ip_comment_insert($comment) {
  ip_posts_insert('comment', $comment->cid);
}

/**
 * Insert a record into the ip_posts table.
 */
function ip_posts_insert($type, $id, $ip_address = NULL) {
  $ip_address = is_null($ip_address) ? ip_address() : $ip_address;
  $iplong = ip2long($ip_address);
  if (!empty($iplong)) {
    return db_insert('ip_posts')
      ->fields(array(
      'type' => $type,
      'id' => $id,
      'ip' => $iplong,
    ))
      ->execute();
  }
}

/**
 * Loads a record into the ip_posts table.
 */
function ip_posts_load($type, $id) {
  return db_query('SELECT * FROM {ip_posts} WHERE type = :type AND id = :id', array(
    'type' => $type,
    'id' => $id,
  ))
    ->fetch();
}

/**
 * Handle backlog of comments.
 */
function ip_backlog_comments() {
  if (module_exists('comment')) {
    $result = db_query_range("SELECT c.cid AS cid, c.hostname AS hostname" . " FROM {comment} c" . " LEFT JOIN {ip_posts} i" . " ON i.type = 'comment'" . " AND (c.cid = i.id OR i.id IS NULL)" . " WHERE i.id IS NULL" . " ORDER BY c.cid DESC", 0, 50);
    foreach ($result as $row) {
      ip_posts_insert('comment', $row->cid, $row->hostname);
    }
  }
}

/**
 * Implements hook_views_api()
 */
function ip_views_api() {
  return array(
    'api' => views_api_version(),
  );
}

// @todo duplicate finder.
// @todo attach our module's data to nodes/comments ?
// @todo delete from posts table when object (node/comment) is deleted.
// @todo something like hook_user_operations, but hook_ip_operations
// @todo handle deleting of comments and nodes.

Functions

Namesort descending Description
ip_backlog_comments Handle backlog of comments.
ip_comment_insert Implements hook_comment_insert().
ip_node_insert Implements hook_node_insert().
ip_permission Implements hook_permission().
ip_posts_insert Insert a record into the ip_posts table.
ip_posts_load Loads a record into the ip_posts table.
ip_tracker_ip_records Get the records from the tracker for an IP.
ip_tracker_ip_user_count Count how many users have been tracked against an IP.
ip_tracker_user_records Get the records from the tracker for a user.
ip_tracker_user_remove Remove records in the ip_tracker table for a certain user.
ip_tracker_user_save Save a record into the ip_tracker table. @NOTE: Maybe you want to check the user uid vs $uid before calling this function.
ip_user_insert Implements hook_user_insert().
ip_user_login Implements hook_user_login().
ip_views_api Implements hook_views_api()