You are here

ip.module in IP address manager 7

Same filename and directory in other branches
  1. 8.2 ip.module
  2. 6 ip.module
  3. 7.2 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'),
    ),
  );
}

/**
 * Callback for menu user ip access.
 */
function ip_menu_access($arg) {
  return !empty($arg) && user_access('manage ip addresses');
}

/**
 * Implements hook_menu().
 */
function ip_menu() {
  $items = array();
  $items['user/%user/ip'] = array(
    'title' => 'Manage IP addresses',
    'page callback' => 'ip_user_manage',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'ip_menu_access',
    'access arguments' => array(
      1,
    ),
    'type' => MENU_LOCAL_TASK,
    'description' => "Manage a user's IP addresses.",
  );

  /*
  // This page hasn't been made yet, but it should be a list of IP addresses.

  $items['admin/people/ip'] = array(
    'title' => 'Manage IP addresses',
    'page callback' => 'ip_manage',
    'access arguments' => array('manage ip addresses'),
    'type' => MENU_LOCAL_TASK,
    'description' => "Manage all IP addresses.",
  );
  */
  $items['admin/people/ip/%'] = array(
    'title' => 'Manage IP addresses',
    'page callback' => 'ip_manage',
    'page arguments' => array(
      3,
    ),
    'access callback' => 'ip_menu_access',
    'access arguments' => array(
      3,
    ),
    'type' => MENU_CALLBACK,
    'description' => "Manage IP addresses.",
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function ip_theme() {
  return array(
    'ip_tracker_user_records' => array(
      'variables' => array(
        'account' => NULL,
      ),
    ),
    'ip_tracker_ip_records' => array(
      'variables' => array(
        'ip' => NULL,
      ),
    ),
  );
}

/**
 * Page callback for user IP address management.
 */
function ip_user_manage($account) {
  $out = theme('ip_tracker_user_records', array(
    'account' => $account,
  ));
  return $out;
}

/**
 * Page callback for IP address management.
 */
function ip_manage($ip = NULL) {
  $out = theme('ip_tracker_ip_records', array(
    'ip' => $ip,
  ));
  return $out;
}

/**
 * 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;
}

/**
 * Theme the IP tracker records for a user.
 */
function theme_ip_tracker_user_records($variables) {
  $account =& $variables['account'];

  // Set the page title.
  drupal_set_title(t('Manage IP addresses for @user', array(
    '@user' => format_username($account),
  )));
  $out = '';

  // Get the record data.
  $records = ip_tracker_user_records($account->uid);
  if (!empty($records)) {

    // Create a table header.
    $header = array(
      t('IP address'),
      t('Visits'),
      t('First visit'),
      t('Last visit'),
      t('User count'),
    );
    foreach ($records as $record) {

      // Count the users tracked using this IP.
      $count = ip_tracker_ip_user_count($record->ip);
      if ($count > 1) {

        // There are multiple users recorded with this IP - link to the IP page.
        $count_field = l(t('!count users', array(
          '!count' => $count,
        )), 'admin/people/ip/' . $record->ip);
      }
      else {

        // There is only this user recorded with this IP.
        $count_field = t('1 user');
      }

      // Build a table row.
      $rows[] = array(
        l($record->ip, 'admin/people/ip/' . $record->ip),
        $record->visits,
        format_date($record->first_visit),
        format_date($record->last_visit),
        $count_field,
      );
    }
    $out = theme('table', array(
      'header' => $header,
      'rows' => $rows,
    ));
  }
  return $out;
}

/**
 * Theme the IP tracker records for an IP.
 */
function theme_ip_tracker_ip_records($variables) {
  $ip =& $variables['ip'];
  if (!empty($ip)) {

    // Set the page title.
    drupal_set_title(t('Manage IP address @ip', array(
      '@ip' => $ip,
    )));

    // Get the record data.
    $records = ip_tracker_ip_records($ip);

    // Create a table header.
    $header = array(
      t('User'),
      t('Visits'),
      t('First visit'),
      t('Last visit'),
    );
    $rows = array();
    foreach ($records as $record) {
      $account = user_load($record->uid);

      // Check that account is loaded.
      if ($account) {

        // Build a table row.
        $rows[] = array(
          format_username($account),
          $record->visits,
          format_date($record->first_visit),
          format_date($record->last_visit),
        );
      }
    }
    $out = theme('table', array(
      'header' => $header,
      'rows' => $rows,
    ));
  }
  return $out;
}

/**
 * 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 cave_tracker 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();
  }
}

/**
 * Implements hook_cron().
 */
function ip_cron() {

  // Process backlog of nodes.
  ip_backlog_nodes();

  // If nodes are done, process comments.
  if (!variable_get('ip_backlog_nodes', PHP_INT_MAX)) {
    ip_backlog_comments();
  }
}

/**
 * Handle backlog of nodes.
 */
function ip_backlog_nodes() {

  // This variable tracks the last wid.
  $ip_backlog_nodes = variable_get('ip_backlog_nodes', PHP_INT_MAX);
  if ($ip_backlog_nodes) {
    $result = db_query_range("SELECT wid, hostname, link" . " FROM {watchdog}" . " WHERE type = 'content'" . " AND message = :msg" . " AND wid < :backlog_nodes" . " ORDER BY wid DESC", 0, 20, array(
      ':msg' => '@type: added %title.',
      ':backlog_nodes' => $ip_backlog_nodes,
    ));
    $row_count = 0;
    foreach ($result as $row) {
      $nid = str_replace(array(
        '<a href="' . base_path() . 'node/',
        '">view</a>',
      ), array(
        '',
        '',
      ), $row->link);

      // Test the node.
      $node = node_load($nid);
      if (!empty($node)) {
        ip_posts_insert('node', $nid, $row->hostname);
      }
      $row_count++;
      variable_set('ip_backlog_nodes', $row->wid);
    }
    if (!$row_count) {

      // Mark as finished.
      variable_set('ip_backlog_nodes', 0);
    }
  }
}

/**
 * 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);
    }
  }
}

// @todo an IP address search.
// @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_backlog_nodes Handle backlog of nodes.
ip_comment_insert Implements hook_comment_insert().
ip_cron Implements hook_cron().
ip_manage Page callback for IP address management.
ip_menu Implements hook_menu().
ip_menu_access Callback for menu user ip access.
ip_node_insert Implements hook_node_insert().
ip_permission Implements hook_permission().
ip_posts_insert Insert a record into the cave_tracker table.
ip_theme Implements hook_theme().
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_user_manage Page callback for user IP address management.
theme_ip_tracker_ip_records Theme the IP tracker records for an IP.
theme_ip_tracker_user_records Theme the IP tracker records for a user.