You are here

function httpbl_boot in http:BL 7

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

Implementation of hook_boot()

Blocks all access from nuisance IPs on every page request

File

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

Code

function httpbl_boot() {

  // Only continue when checks on "all requests" is enabled
  if (variable_get('httpbl_check', HTTPBL_CHECK_NONE) != HTTPBL_CHECK_ALL) {
    return;
  }

  // If visitor about to be white-list challenged (for the first time this session),
  // determine and save original destination before we check access, so
  // redirect can occur if visitor successfully completes white list challenge.
  // Note: if visitor re-loads the white list form, this will not work.
  if (!isset($_SESSION['httpbl_destination']) && request_uri() == '/httpbl/whitelist') {
    $_SESSION['httpbl_destination'] = ltrim($_SERVER['HTTP_REFERER'], $GLOBALS['base_url']);
  }
  $result = httpbl_check();

  // Check IP immediately upon access
  if ($result) {
    if ($result == HTTPBL_LIST_GREY) {
      if (isset($_GET['q']) && $_GET['q'] == 'httpbl/whitelist') {
        return;
      }

      // If IP is found in Project Honeypot at Greylist threat level
      // we notify user and offer a challenge to get white-listed (see if they are human).
      $message = variable_get('httpbl_message_grey', 'Sorry, %ip has been greylisted by <a href="%ipurl">http:BL</a>.<br>You may try whitelisting on <a href="%whitelisturl">%whitelisturl</a>.%honeypot');
    }
    elseif ($result == HTTPBL_LIST_BLACK) {
      $message = variable_get('httpbl_message_black', 'Sorry, %ip has been blacklisted by <a href="%ipurl">http:BL</a>.%honeypot');
    }

    // Place hidden link that spiders cannot resist
    if ($link = variable_get('httpbl_link', NULL)) {
      $word = variable_get('httpbl_word', 'randomness');
      $link = httpbl_honeylink($link, $word);
    }
    $message = strtr($message, array(
      '%ip' => ip_address(),
      '%ipurl' => _httpbl_ipdata(ip_address(), FALSE),
      '%honeypot' => $link,
      '%whitelisturl' => _httpbl_url('/httpbl/whitelist'),
    ));
    header('HTTP/1.1 403 Forbidden');
    echo $message;
    exit;
  }
}