You are here

function comment_block_ip in Comment IP 7

Action function for comment_block_ip.

1 call to comment_block_ip()
comment_block_ip_and_delete in ./comment_ip.module
Action function for comment_block_ip_and_delete.

File

./comment_ip.module, line 182
Main hooks and functions for comment_ip module.

Code

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,
    )));
  }
}