You are here

public static function HttpblEvaluator::updateIpLocalStatus in http:BL 8

Update stored status of Host IP.

(legacy name was "_httpbl_cache_update".)

Overrides HttpblEvaluatorInterface::updateIpLocalStatus

File

src/HttpblEvaluator.php, line 550

Class

HttpblEvaluator
HttpblEvaluator evaluates visitor/host page requests.

Namespace

Drupal\httpbl

Code

public static function updateIpLocalStatus($ip, $status, $offset = 0) {

  // Collect needed services.
  $banManager = \Drupal::service('ban.ip_manager');
  $logTrapper = \Drupal::service('httpbl.logtrapper');
  $hosts = HostQuery::loadHostsByIp($ip);
  if (isset($hosts) && !empty($hosts)) {
    foreach ($hosts as $host) {
      $host
        ->setHostStatus($status);
      $host
        ->setExpiry(\Drupal::time()
        ->getRequestTime() + $offset);
      $host
        ->setSource(HTTPBL_CHALLENGE_FAILURE);
      $host
        ->save();
    }
  }
  else {

    // Error.  Something could be broken.
    $logTrapper
      ->trapError('Cannot blacklist non-existing IP (@ip).', [
      '@ip' => $ip,
    ]);
  }

  // If blacklisted host and using "Auto-banning"...
  if ($status == HTTPBL_LIST_BLACK && \Drupal::state()
    ->get('httpbl.storage') == HTTPBL_DB_HH_DRUPAL) {

    // Check if host is already banned.
    if ($banManager
      ->isBanned($ip)) {

      // Warning.  This shouldn't be happening.
      $logTrapper
        ->trapWarning('This host (@ip) is already banned', [
        '@ip' => $ip,
      ]);

      // This message should never be seen by anyone who has really been banned.
      drupal_set_message(t('IP address ( @ip ) currently banned from this site.', array(
        '@ip' => $ip,
      )), 'error', FALSE);
    }
    else {
      $banManager
        ->banIp($ip);

      // Warning.   Most likely a white-list challenge failure.
      $logTrapper
        ->trapWarning('Host (@ip) has been banned from this site.', [
        '@ip' => $ip,
      ]);

      // Possible to see this message once after failing challenge, but never
      // again after a page refresh.
      drupal_set_message(t('Your IP address ( @ip ) has been banned from this site.', array(
        '@ip' => $ip,
      )), 'error', FALSE);
    }
  }
}