You are here

function comment_ip_confirm_blocked_ips_submit in Comment IP 7

Adds the deleted comments IP addresses to the blocked_ips table.

File

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

Code

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';
}