You are here

function httpbl_blacklisted in http:BL 5

Check if an IP should be banned

Parameters

string $ip:

boolean $enable_cache:

boolean $enable_stats:

Return value

int

3 calls to httpbl_blacklisted()
httpbl_comment in ./httpbl.module
Implementation of hook_comment().
httpbl_request_whitelist in ./httpbl.module
_httpbl_check in ./httpbl.module
The actual checking function.

File

./httpbl.module, line 378
Implementation of http:BL for Drupal. It provides IP-based blacklisting through http:BL and allows linking to a honeypot.

Code

function httpbl_blacklisted($ip = NULL, $enable_cache = TRUE, $enable_stats = FALSE) {
  if (!$ip) {
    $ip = _httpbl_ip_address();
  }
  if ($cache = variable_get('httpbl_dbcache', TRUE) && $enable_cache) {
    $result = _httpbl_cache_get($ip);
  }
  if ($result === NULL) {
    if ($response = httpbl_dnslookup($ip)) {

      // positive threat, type is positive (not search engine)
      if ($response['threat'] > variable_get('httpbl_threatlevel', 50) && $response['type']) {
        if (variable_get('httpbl_log', 1) > 1) {
          watchdog('httpbl', t('%ip was blacklisted (%response)', array(
            '%ip' => $ip,
            '%response' => $response['raw'],
          )), WATCHDOG_WARNING, _httpbl_iplink($ip));
        }
        if ($cache) {
          _httpbl_cache_set($ip, 1, 7200);
        }
        if ($enable_stats) {
          variable_set('httpbl_stat_black', variable_get('httpbl_stat_black', 0) + 1);
        }
        return 1;
      }
      else {
        if ($response['threat'] && $response['type']) {
          if (variable_get('httpbl_log', 1) > 1) {
            watchdog('httpbl', t('%ip was greylisted (%response)', array(
              '%ip' => $ip,
              '%response' => $response['raw'],
            )), WATCHDOG_WARNING, _httpbl_iplink($ip));
          }
          if ($cache) {
            _httpbl_cache_set($ip, 2, 7200);
          }
          if ($enable_stats) {
            variable_set('httpbl_stat_grey', variable_get('httpbl_stat_grey', 0) + 1);
          }
          return 2;
        }
        else {
          if (variable_get('httpbl_log', 1) > 2) {
            watchdog('httpbl', t('Lookup for %ip was negative (%response)', array(
              '%ip' => $ip,
              '%response' => $response['raw'],
            )), WATCHDOG_NOTICE, _httpbl_iplink($ip));
          }
          if ($cache) {
            _httpbl_cache_set($ip, 0, 10800);
          }
          return 0;
        }
      }
    }
    else {
      if (variable_get('httpbl_log', 1) > 2) {
        watchdog('httpbl', t('No results for %ip', array(
          '%ip' => $ip,
        )), WATCHDOG_NOTICE);
      }
      if ($cache) {
        _httpbl_cache_set($ip, 0, 21600);
      }
      return FALSE;
    }
  }
  else {
    return $result;
  }
}