ip.module in IP address manager 8.2
Same filename and directory in other branches
IP address manager module.
File
ip.moduleView source
<?php
/**
* @file
* IP address manager module.
*/
use Drupal\Core\Database\Database;
use Drupal\ip\IpPosts;
use Drupal\ip\IpTracker;
/**
* Implements hook_user_login().
*/
function ip_user_login($account) {
$ipTracker = new IpTracker(Database::getConnection(), Drupal::request(), $account);
$ipTracker
->save();
}
/**
* Implements hook_entity_insert().
*/
function ip_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
switch ($entity
->getEntityTypeId()) {
case 'user':
// Track anonymous registration (Do not track administration account creation).
if (Drupal::currentUser()
->id() == 0 || Drupal::currentUser()
->id() == $entity
->id()) {
$ipTracker = new IpTracker(Database::getConnection(), Drupal::request(), $entity);
$ipTracker
->save();
}
break;
default:
// @TODO: allow selection of entities to be tracked.
$ipTracker = new IpPosts(Database::getConnection(), Drupal::request(), $entity);
$ipTracker
->save();
}
}
/**
* 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_cron().
*/
function ip_cron() {
/* @TODO port to D8: maybe a better approach is to move this to a drush/batch command.
// 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.
* @TODO port to D8.
*/
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.
* @TODO port to D8.
*/
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);
}
}
}
Functions
Name | Description |
---|---|
ip_backlog_comments | Handle backlog of comments. @TODO port to D8. |
ip_backlog_nodes | Handle backlog of nodes. @TODO port to D8. |
ip_cron | Implements hook_cron(). |
ip_entity_insert | Implements hook_entity_insert(). |
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_user_login | Implements hook_user_login(). |