You are here

ip.module in IP address manager 6

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

IP address manager module.

File

ip.module
View source
<?php

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

/**
 * Implementation of hook_perm().
 */
function ip_perm() {
  return array(
    'manage ip addresses',
  );
}

/**
 * Implementation of 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 arguments' => array(
      'manage ip addresses',
    ),
    'type' => MENU_LOCAL_TASK,
    'description' => t("Manage a user's IP addresses."),
  );
  $items['user/ip/%'] = array(
    'title' => 'Manage IP addresses',
    'page callback' => 'ip_manage',
    'page arguments' => array(
      2,
    ),
    'access arguments' => array(
      'manage ip addresses',
    ),
    'type' => MENU_LOCAL_TASK,
    'description' => t("Manage a user's IP addresses."),
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function ip_theme(&$existing) {
  return array(
    'ip_tracker_user_records' => array(
      'arguments' => array(
        'account' => NULL,
      ),
    ),
    'ip_tracker_ip_records' => array(
      'arguments' => array(
        'ip' => NULL,
      ),
    ),
    'ip_username' => array(
      'arguments' => array(
        'account' => NULL,
      ),
    ),
  );
}

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

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

/**
 * Implementation of hook_user().
 */
function ip_user($op, &$edit, &$account, $category = NULL) {
  if ($op == 'login') {
    ip_tracker_user_save($account->uid);
  }
}

/**
 * Save a record into the ip_tracker table.
 */
function ip_tracker_user_save($uid) {

  // Check to see if a row exists for this uid/ip combination.
  $sql = "SELECT visits FROM {ip_tracker} WHERE uid = %d AND ip = %d";
  $args = array(
    $uid,
    ip2long(ip_address()),
  );
  $visits = db_result(db_query($sql, $args));
  if ($visits) {

    // Update.
    $sql = "UPDATE {ip_tracker} SET visits = %d, last_visit = %d WHERE uid = %d AND ip = %d";
    $args = array(
      $visits + 1,
      time(),
      $uid,
      ip2long(ip_address()),
    );
    db_query($sql, $args);
  }
  else {

    // Insert.
    $sql = "INSERT INTO {ip_tracker} (uid, ip, visits, first_visit, last_visit) VALUES (%d, %d, 1, %d, %d)";
    $args = array(
      $uid,
      ip2long(ip_address()),
      time(),
      time(),
    );
    db_query($sql, $args);
  }
}

/**
 * Remove records in the ip_tracker table for a certain user.
 */
function ip_tracker_user_remove($uid) {
  $sql = "DELETE FROM {ip_tracker} WHERE uid = %d";
  $args = array(
    $uid,
  );
  db_query($sql, $args);
}

/**
 * Get the records from the tracker for a user.
 */
function ip_tracker_user_records($uid) {
  $sql = "SELECT * FROM {ip_tracker} WHERE uid = %d";
  $args = array(
    $uid,
  );
  $result = db_query($sql, $args);
  $rows = array();
  while ($row = db_fetch_array($result)) {
    $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} WHERE ip = '%s'";
  $args = array(
    ip2long($ip),
  );
  $result = db_query($sql, $args);
  $rows = array();
  while ($row = db_fetch_array($result)) {
    $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 = '%s'";
  $args = array(
    ip2long($ip),
  );
  $count = db_result(db_query($sql, $args));
  return $count;
}

/**
 * Theme the IP tracker records for a user.
 */
function theme_ip_tracker_user_records($account) {

  // Set the page title.
  drupal_set_title(t('Manage IP addresses for %user', array(
    '%user' => $account->name,
  )));

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

  // Create a table header.
  $header = array(
    t('IP address'),
    t('Visits'),
    t('First visit'),
    t('Last visit'),
    t('User count'),
    t('Operations'),
  );
  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,
      )), 'user/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'], 'user/ip/' . $record['ip']),
      $record['visits'],
      format_date($record['first_visit']),
      format_date($record['last_visit']),
      $count_field,
      l(t('Ban IP'), 'admin/user/rules/add/' . $record['ip'] . '/host'),
    );
  }
  $out = theme('table', $header, $rows);
  return $out;
}

/**
 * Theme the IP tracker records for an IP.
 */
function theme_ip_tracker_ip_records($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'),
  );
  foreach ($records as $record) {
    $account = user_load($record['uid']);

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

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

/**
 * Theme a username for display in the IP tracker.
 *
 * A modified version of theme_username().
 */
function theme_ip_username($account) {

  // Shorten the name when it is too long or it will break many tables.
  if (drupal_strlen($account->name) > 20) {
    $name = drupal_substr($account->name, 0, 15) . '...';
  }
  else {
    $name = $account->name;
  }
  $output = l($name, 'user/' . $account->uid . '/ip', array(
    'attributes' => array(
      'title' => t('Manage user IP addresses.'),
    ),
  ));
  return $output;
}

/**
 * Implementation of hook_nodeapi().
 */
function ip_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'insert') {
    ip_posts_insert('node', $node->nid);
  }
}

/**
 * Implementation of hook_comment().
 */
function ip_comment(&$a1, $op) {
  if ($op == 'insert') {
    ip_posts_insert('comment', $a1['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;
  $sql = "INSERT INTO {ip_posts} (type, id, ip) VALUES ('%s', %d, %d)";
  $args = array(
    $type,
    $id,
    ip2long($ip_address),
  );
  db_query($sql, $args);
}

/**
 * Implementation of 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) {
    $content_message = '@type: added %title.';
    $result = db_query_range("SELECT wid, hostname, link" . " FROM {watchdog}" . " WHERE type = 'content'" . " AND message = '%s'" . " AND wid < " . $ip_backlog_nodes . " ORDER BY wid DESC", array(
      $content_message,
    ), 0, 20);
    $row_count = 0;
    while ($row = db_fetch_object($result)) {
      $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() {
  $result = db_query_range("SELECT c.cid AS cid, c.hostname AS hostname" . " FROM {comments} 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);
  while ($row = db_fetch_object($result)) {
    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 Implementation of hook_comment().
ip_cron Implementation of hook_cron().
ip_manage Page callback for IP address management.
ip_menu Implementation of hook_menu().
ip_nodeapi Implementation of hook_nodeapi().
ip_perm Implementation of hook_perm().
ip_posts_insert Insert a record into the cave_tracker table.
ip_theme Implementation of 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.
ip_user Implementation of hook_user().
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.
theme_ip_username Theme a username for display in the IP tracker.