You are here

function drush_httpbl_sos in http:BL 8

Command handler. Unban IP address and stop/stop checking/blocking of all page requests or on comment submissions only.

File

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

Code

function drush_httpbl_sos($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 SOS 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 whitelist this IP.
  $httpblEvaluator = \Drupal::service('httpbl.evaluator');
  $status = $httpblEvaluator
    ->getIpLocalStatus($ip);
  if ($status != 0) {
    $httpblEvaluator
      ->drushWhitelist($ip);
    $logMessage = t('Drush SOS whitelisted @ip.', [
      '@ip' => $ip,
    ]);
    drush_log(dt($logMessage), 'ok');
    \Drupal::logger('httpbl')
      ->notice($logMessage);
  }
  else {
    drush_log(dt('@ip was already white-listed or was not found.', [
      '@ip' => $ip,
    ]), 'ok');
  }

  // Stop option:
  if (drush_get_option('stop', FALSE)) {
    $command = drush_state_set($key = 'httpbl.check', $value = 0);
    $logMessage = t('Drush SOS stopped Httpbl page checking/blocking.');
    \Drupal::logger('httpbl')
      ->warning($logMessage);
    return $command;
  }
  elseif (drush_get_option('start', FALSE)) {
    $command = drush_state_set($key = 'httpbl.check', $value = 2);
    $logMessage = t('Drush SOS started or restarted Httpbl page checking/blocking.');
    drush_log(dt($logMessage), 'ok');
    \Drupal::logger('httpbl')
      ->notice($logMessage);
    return $command;
  }
  elseif (drush_get_option('comment', FALSE)) {
    $command = drush_state_set($key = 'httpbl.check', $value = 1);
    $logMessage = t('Drush SOS started checking/blocking on comment submissions only.');

    // Drush log only needed for feedback when the drupal logger is below warning level.

    //drush_log(dt($logMessage), 'ok');
    \Drupal::logger('httpbl')
      ->warning($logMessage);
    return $command;
  }
  else {
    return;
  }
}