You are here

function drush_httpbl_sol in http:BL 8

Command handler. Quickly remove localhost (or any given IP) from ban & httpbl tabled. Useful for testing.

File

drush/httpbl.drush.inc, line 182
Drush integration for the httpbl module.

Code

function drush_httpbl_sol($ip = '127.0.0.1') {

  // Get Ban manager service and unBan if banned.
  $banManager = \Drupal::service('ban.ip_manager');
  if ($banManager
    ->isBanned($ip)) {
    $banManager
      ->unbanIp($ip);
    $logMessage = t('Drush SOL unbanned @ip.', [
      '@ip' => $ip,
    ]);
    drush_log(dt($logMessage), 'ok');
    \Drupal::logger('httpbl')
      ->notice($logMessage);
  }
  else {
    drush_log(dt('@ip was not banned.', [
      '@ip' => $ip,
    ]), 'ok');
  }

  // Get Evaluator service and delete this IP.
  $httpblEvaluator = \Drupal::service('httpbl.evaluator');
  $status = $httpblEvaluator
    ->getIpLocalStatus($ip);
  if ($status != NULL) {

    // Gather all hosts with this IP.
    $hosts = HostQuery::loadHostsByIp($ip);

    // If we have some, count them.
    if (isset($hosts) && !empty($hosts)) {
      $count = count($hosts);

      // As long as there's more than one...
      while ($count > 0) {

        // Sort them in order by index.
        ksort($hosts);

        // Get that host and delete it.
        $id = key($hosts);
        $host = Host::load($id);
        $host
          ->delete();

        // Reverse sort the array and remove the last one.
        arsort($hosts);
        array_pop($hosts);

        // Rinse and repeat.
        $count--;
      }
      drush_log(dt('@ip was removed from evaluated hosts.', [
        '@ip' => $ip,
      ]), 'ok');
    }
    else {
      drush_log(dt('@ip was not an evaluated host.', [
        '@ip' => $ip,
      ]), 'ok');
    }
  }
  else {
    drush_log(dt('@ip was not an evaluated host.', [
      '@ip' => $ip,
    ]), 'ok');
  }
  return;
}