You are here

function httpbl_check in http:BL 6.2

Same name and namespace in other branches
  1. 6 httpbl.module \httpbl_check()
  2. 7 httpbl.module \httpbl_check()

Check if an IP should be banned

Return value

constant: HTTP_LIST_*

3 calls to httpbl_check()
httpbl_boot in ./httpbl.module
Implementation of hook_boot().
httpbl_comment in ./httpbl.module
Implementation of hook_comment().
httpbl_whitelist_access in ./httpbl.module
Determine whether a user has access to the session whitelist functionality.
4 string references to 'httpbl_check'
httpbl_admin_settings in ./httpbl.module
Implementation of hook_settings().
httpbl_boot in ./httpbl.module
Implementation of hook_boot().
httpbl_comment in ./httpbl.module
Implementation of hook_comment().
httpbl_requirements in ./httpbl.module
Implementation of hook_requirements().

File

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

Code

function httpbl_check() {
  static $result;

  // Result was already calculated -- return.
  if (is_int($result)) {
    return $result;
  }
  $ip = ip_address();

  // $ip = '127.1.40.1'; // simulate greylist response for testing
  // $ip = '127.1.80.1'; // simulate blacklist response for testing
  // Check if user is whitelisted in any way
  if (_httpbl_whitelisted($ip)) {
    $result = HTTPBL_LIST_SAFE;
  }
  else {
    if ($cache = variable_get('httpbl_cache', HTTPBL_CACHE_DBDRUPAL)) {
      $result = _httpbl_cache_get($ip);
    }
  }
  if (!is_numeric($result)) {

    // Do a DNS lookup, and continue if lookup was succesful
    if ($response = httpbl_dnslookup($ip)) {
      $stats = variable_get('httpbl_stats', TRUE);

      // Blacklist?
      if ($response['threat'] > variable_get('httpbl_black_threshold', HTTPBL_THRESHOLD_BLACK) && $response['type']) {
        if (variable_get('httpbl_log', FALSE)) {
          include_once "includes/bootstrap.inc";
          drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

          // This is needed for logs to work
          watchdog('httpbl', '%ip was blacklisted (%response)', array(
            '%ip' => $ip,
            '%response' => $response['raw'],
          ), WATCHDOG_WARNING, _httpbl_ipdata($ip));
        }
        if ($stats) {
          variable_set('httpbl_stat_black', variable_get('httpbl_stat_black', 0) + 1);
        }
        $result = HTTPBL_LIST_BLACK;
      }
      else {
        if ($response['threat'] > variable_get('httpbl_grey_threshold', HTTPBL_THRESHOLD_GREY) && $response['type']) {
          include_once "includes/bootstrap.inc";
          drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

          // This is needed for whitelisting and logs
          if (variable_get('httpbl_log', FALSE)) {
            watchdog('httpbl', '%ip was greylisted (%response)', array(
              '%ip' => $ip,
              '%response' => $response['raw'],
            ), WATCHDOG_WARNING, _httpbl_ipdata($ip));
          }
          if ($stats) {
            variable_set('httpbl_stat_grey', variable_get('httpbl_stat_grey', 0) + 1);
          }
          $result = HTTPBL_LIST_GREY;
        }
        else {
          $result = HTTPBL_LIST_SAFE;
        }
      }

      // Cache results - Use Blacklist offset settings (default 1 year)
      //             or Greylist offset settings (default 1 day)
      if ($cache) {
        if ($result == HTTPBL_LIST_BLACK) {
          _httpbl_cache_set($ip, $result, variable_get('httpbl_blacklist_offset', 31536000));
        }
        else {
          if ($result == HTTPBL_LIST_GREY) {
            _httpbl_cache_set($ip, $result, variable_get('httpbl_greylist_offset', 86400));
          }
        }
      }
    }
    else {
      if ($cache) {
        _httpbl_cache_set($ip, HTTPBL_LIST_SAFE, variable_get('httpbl_safe_offset', 10800));
      }
      $result = HTTPBL_LIST_SAFE;
    }
  }
  return $result;
}