comment_ip.module in Comment IP 7
Same filename and directory in other branches
Main hooks and functions for comment_ip module.
File
comment_ip.moduleView source
<?php
/**
* @file
* Main hooks and functions for comment_ip module.
*/
/**
* Implementation of hook_action_info().
*/
function comment_ip_action_info() {
$action = array(
'comment_block_ip' => array(
'label' => t("Block the Comment Hostname(IP)"),
'type' => 'comment',
'configurable' => FALSE,
'triggers' => array(
'any',
),
'pass rows' => TRUE,
),
'comment_block_ip_and_delete' => array(
'label' => t("Block the Comment Hostname(IP) and Delete the Comment"),
'type' => 'comment',
'configurable' => FALSE,
'triggers' => array(
'any',
),
'pass rows' => TRUE,
),
);
return $action;
}
/**
* Implements hook_menu_alter().
*/
function comment_ip_menu_alter(&$items) {
// Redirect comment menu item to custom menu callback.
$items['admin/content/comment']['page callback'] = 'comment_ip_redirect';
}
/**
* Implements hook_form_alter().
*/
function comment_ip_form_alter(&$form, &$form_state, $form_id) {
// Add commenters IP address to comment view.
if ($form_id == 'comment_admin_overview') {
if (!variable_get('blocked_ips', '')) {
$form['options']['operation']['#options']['block'] = t('Delete the selected comments and block their IPs');
}
else {
drupal_set_message(t('It looks like you have used the blocked_ips config setting. To block an IP displayed below you will need to manually add that IP to your blocked_ip config.'));
}
$form['comments']['#header']['ip']['data'] = 'IP';
foreach ($form['comments']['#options'] as $id => $options) {
$comment = comment_load($id);
$form['comments']['#options'][$id]['ip'] = $comment->hostname;
}
}
}
/**
* Redirect the menu callback to specific forms based on posted data.
*/
function comment_ip_redirect($type = 'new') {
$edit = $_POST;
// Standard comment delete form.
if (isset($edit['operation']) && $edit['operation'] == 'delete' && isset($edit['comments']) && $edit['comments']) {
return drupal_get_form('comment_multiple_delete_confirm');
}
elseif (isset($edit['operation']) && $edit['operation'] == 'block' && isset($edit['comments']) && $edit['comments']) {
return drupal_get_form('comment_ip_confirm_blocked_ips');
}
else {
// Default admin overview form.
return drupal_get_form('comment_admin_overview', $type);
}
}
/**
* Altered comment overview page with IP column and block IP select option.
*/
function comment_ip_confirm_blocked_ips($form, &$form_state) {
$edit = $form_state['input'];
$form['comments'] = array(
'#prefix' => '<ul>',
'#suffix' => '</ul>',
'#tree' => TRUE,
);
// array_filter() returns only elements with actual values.
$comment_counter = 0;
foreach (array_filter($edit['comments']) as $cid => $value) {
$comment = comment_load($cid);
if (is_object($comment) && is_numeric($comment->cid)) {
$result = db_query('SELECT subject, hostname FROM {comment} WHERE cid = :cid', array(
':cid' => $cid,
))
->fetchAllKeyed();
$subject = key($result);
$ip = $result[$subject];
$warning = $ip == ip_address() ? ' <strong>(This IP address is yours, so we won\'t block it.)</strong>' : '';
$form['comments'][$cid] = array(
'#type' => 'hidden',
'#value' => $cid,
'#prefix' => '<li>',
'#suffix' => check_plain($subject) . ' - ' . check_plain($ip) . $warning . '</li>',
);
$comment_counter++;
}
}
$form['operation'] = array(
'#type' => 'hidden',
'#value' => 'block',
);
if (!$comment_counter) {
drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
drupal_goto('admin/content/comment');
}
else {
return confirm_form($form, t("Are you sure you want to delete these comments and all their children, and block the following IPs"), 'admin/content/comment', t('This action cannot be undone.'), t('Delete comments'), t('Cancel'));
}
}
/**
* Adds the deleted comments IP addresses to the blocked_ips table.
*/
function comment_ip_confirm_blocked_ips_submit(&$form, &$form_state) {
$cids = $form_state['values']['comments'];
$comments = comment_load_multiple($cids);
$ips = db_select('blocked_ips', 'bip')
->fields('bip', array(
'ip',
))
->execute()
->fetchAllKeyed(0, 0);
$comment_counter = 0;
foreach ($comments as $comment) {
$comment_counter++;
$ip = $comment->hostname;
// If already in db, skip.
if (isset($ips[$ip])) {
drupal_set_message(t('The IP address %ip was already in the blocked IP list.', array(
'%ip' => $ip,
)));
continue;
}
// If this is the admin's IP address, skip.
if ($ip == ip_address()) {
drupal_set_message(t('The IP address %ip is your current IP address, so we\'ve not blocked it.', array(
'%ip' => $ip,
)));
continue;
}
// Add IP to blocked ips table.
db_insert('blocked_ips')
->fields(array(
'ip' => $ip,
))
->execute();
$ips[$ip] = $ip;
drupal_set_message(t('The IP address %ip has been blocked.', array(
'%ip' => $ip,
)));
}
if (!$comment_counter) {
drupal_set_message(t('There do not appear to be any comments to delete and block, or your selected comment was deleted by another administrator.'));
drupal_goto('admin/content/comment');
}
// Delete all comments.
comment_delete_multiple($cids);
$form_state['values']['comments'] = array();
$form_state['redirect'] = 'admin/content/comment';
}
/**
* Action function for comment_block_ip.
*/
function comment_block_ip(&$entity, $context = array()) {
$ip = $entity->hostname;
// Fetch the current list of blocked IPs.
$ips = db_select('blocked_ips', 'bip')
->fields('bip', array(
'ip',
))
->execute()
->fetchAllKeyed(0, 0);
// Check whether the IP has already been blocked. If already in db, skip.
if (isset($ips[$ip])) {
drupal_set_message(t('The IP address %ip was already in the blocked IP list.', array(
'%ip' => $ip,
)));
}
elseif ($ip == ip_address()) {
drupal_set_message(t('The IP address %ip is your current IP address, so we\'ve not blocked it.', array(
'%ip' => $ip,
)));
}
else {
// Add IP to blocked_ips table.
db_insert('blocked_ips')
->fields(array(
'ip' => $ip,
))
->execute();
$ips[$ip] = $ip;
drupal_set_message(t('The IP address %ip has been blocked.', array(
'%ip' => $ip,
)));
}
}
/**
* Action function for comment_block_ip_and_delete.
*/
function comment_block_ip_and_delete(&$entity, $context = array()) {
// Block the IP.
comment_block_ip($entity);
// Now Delete the comment and its replies.
comment_delete($entity->cid);
}
Functions
Name | Description |
---|---|
comment_block_ip | Action function for comment_block_ip. |
comment_block_ip_and_delete | Action function for comment_block_ip_and_delete. |
comment_ip_action_info | Implementation of hook_action_info(). |
comment_ip_confirm_blocked_ips | Altered comment overview page with IP column and block IP select option. |
comment_ip_confirm_blocked_ips_submit | Adds the deleted comments IP addresses to the blocked_ips table. |
comment_ip_form_alter | Implements hook_form_alter(). |
comment_ip_menu_alter | Implements hook_menu_alter(). |
comment_ip_redirect | Redirect the menu callback to specific forms based on posted data. |