You are here

httpbl.module in http:BL 5

Same filename and directory in other branches
  1. 8 httpbl.module
  2. 6.2 httpbl.module
  3. 6 httpbl.module
  4. 7 httpbl.module

Implementation of http:BL for Drupal. It provides IP-based blacklisting through http:BL and allows linking to a honeypot.

@author Mark Janssen (praseodym) @link http://drupal.org/project/httpbl @link http://httpbl.org/

File

httpbl.module
View source
<?php

/**
 * @file
 * Implementation of http:BL for Drupal. It provides IP-based blacklisting
 * through http:BL and allows linking to a honeypot.
 *
 * @author Mark Janssen (praseodym)
 * @link http://drupal.org/project/httpbl
 * @link http://httpbl.org/
 */

/**
 * 
 * Version 2.0, 29/04/2008
 * Contact: praseodym (at) gmail (dot) com
 *
 * Feel free to improve this module, but please contact the author with any
 * changes you make so they can be implemented into the 'official' version.
 *
 */

/**
 * Implementation of hook_menu().
 *
 * @return array
 */
function httpbl_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'httpbl/whitelist',
      'title' => t('Request whitelisting'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'httpbl_request_whitelist',
      ),
      'type' => MENU_CALLBACK,
      'access' => TRUE,
    );
    $items[] = array(
      'path' => 'admin/settings/httpbl',
      'title' => t('http:BL'),
      'description' => t('Manage http:BL settings.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'httpbl_admin_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  else {
    _httpbl_check();
  }
  return $items;
}

/**
 * Implementation of hook_comment().
 */
function httpbl_comment($comment, $op) {
  if (variable_get('httpbl_check', 0) != 1) {
    return;
  }
  switch ($op) {
    case 'insert':
    case 'update':
      $comment = (object) $comment;
      $blacklisted = httpbl_blacklisted();
      if ($blacklisted != 0) {

        // Unpublish and inform the user.
        $operation = comment_operations('unpublish');
        $query = $operation['unpublish'][1];
        db_query($query, $comment->cid);
        drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));

        // Add to our statistics
        if (variable_get('httpbl_stats', TRUE)) {
          variable_set('httpbl_stat_comment', variable_get('httpbl_stat_comment', 0) + 1);
        }
      }
      return;
  }
}

/**
 * Implementation of hook_settings().
 *
 * @return array
 */
function httpbl_admin_settings() {
  if (!$_POST && (!variable_get('httpbl_accesskey', NULL) || !variable_get('httpbl_check', 0))) {
    drupal_set_message(t('IP blacklist lookups are currently disabled; enter your access key below and enable checks to enable blacklist lookups.'), 'error');
  }
  $form['core'] = array(
    '#type' => 'fieldset',
    '#title' => t('http:BL'),
    '#description' => t('For more information about http:BL, see the <a href="http://www.projecthoneypot.org/httpbl.php">http:BL homepage</a>.'),
    '#collapsible' => TRUE,
  );
  $form['core']['httpbl_accesskey'] = array(
    '#type' => 'textfield',
    '#title' => t('http:BL Access Key'),
    '#default_value' => variable_get('httpbl_accesskey', NULL),
    '#description' => t('Your http:BL <a href="http://www.projecthoneypot.org/httpbl_configure.php">Access Key</a>.'),
    '#size' => 20,
    '#maxlength' => 12,
  );
  $form['core']['httpbl_check'] = array(
    '#type' => 'select',
    '#title' => t('Check blacklist status'),
    '#default_value' => variable_get('httpbl_check', 0),
    '#options' => array(
      t('Nowhere (disabled)'),
      t('On comment forms'),
      t('For anonymous users'),
      t('For all users'),
    ),
    '#description' => t('At what times the blacklist should be checked.'),
  );
  $form['core']['httpbl_message_black'] = array(
    '#type' => 'textarea',
    '#title' => t('Blacklist message'),
    '#default_value' => variable_get('httpbl_message_black', "Sorry, %ip has been blacklisted by <a href=\"%ipurl\">http:BL</a>.\n%honeypot"),
    '#description' => t("The message visitors will see when their IP is blacklisted. <em>%ip</em> will be replaced with the visitor's IP, <em>%ipurl</em> with a link to the Project Honeypot information page for that IP, <em>%honeypot</em> with your Honeypot link."),
  );
  $form['core']['httpbl_message_grey'] = array(
    '#type' => 'textarea',
    '#title' => t('Greylist message'),
    '#default_value' => variable_get('httpbl_message_grey', "Sorry, %ip has been greylisted by <a href=\"%ipurl\">http:BL</a>.\nYou may try whitelisting on <a href=\"%whitelisturl\">%whitelisturl</a>.\n%honeypot"),
    '#description' => t("The message visitors will see when their IP is greylisted. <em>%ip</em> will be replaced with the visitor's IP, <em>%ipurl</em> with a link to the Project Honeypot information page for that IP, <em>%honeypot</em> with your Honeypot link, <em>%whitelisturl</em> with the internal whitelist request URL."),
  );
  $form['honeypot'] = array(
    '#type' => 'fieldset',
    '#title' => t('Honeypot'),
    '#description' => t('Your Honeypot (spam trap) settings. For more information, see the <a href="http://www.projecthoneypot.org/">Project Honey Pot homepage</a>.'),
    '#collapsible' => TRUE,
  );
  $form['honeypot']['httpbl_footer'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add link to footer'),
    '#default_value' => variable_get('httpbl_footer', FALSE),
    '#description' => t('Whether to add your Honeypot link to the footer of every page.'),
  );
  $form['honeypot']['httpbl_link'] = array(
    '#type' => 'textfield',
    '#title' => t('Project Honey Pot Link'),
    '#default_value' => variable_get('httpbl_link', NULL),
    '#description' => t('Your Honeypot (spam trap) link. This can be one of your own <a href="http://www.projecthoneypot.org/manage_honey_pots.php">Honey Pots</a> or a <a href="http://www.projecthoneypot.org/manage_quicklink.php">QuickLink</a>.'),
  );
  $form['honeypot']['httpbl_word'] = array(
    '#type' => 'textfield',
    '#title' => t('Link word'),
    '#default_value' => variable_get('httpbl_word', 'randomness'),
    '#description' => t('A random word which will be used as a link.'),
  );
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['advanced']['httpbl_threatlevel'] = array(
    '#type' => 'textfield',
    '#title' => t('Greylisting threshold'),
    '#default_value' => variable_get('httpbl_threatlevel', 50),
    '#description' => t('Threshold for the greylisting threat level (1-255, 0 to disable greylisting)'),
    '#size' => 5,
    '#maxlength' => 3,
  );
  $form['advanced']['httpbl_log'] = array(
    '#type' => 'select',
    '#title' => t('Log level'),
    '#default_value' => variable_get('httpbl_log', 1),
    '#options' => array(
      t('Only errors'),
      t('Temporary whitelist requests'),
      t('Grey- and blacklisted IPs'),
      t('All requests (debugging)'),
    ),
    '#description' => t('Log level for http:BL requests. Every item contains all items above it.'),
  );
  $form['advanced']['httpbl_stats'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable statistics'),
    '#default_value' => variable_get('httpbl_stats', TRUE),
    '#description' => t('Whether to enable counting of grey- and blacklisted requests.'),
  );
  $form['advanced']['httpbl_dbcache'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable database cache'),
    '#default_value' => variable_get('httpbl_dbcache', TRUE),
    '#description' => t('Whether to enable database-based caching. Note that when this is disabled, IPs that fail the temporary whitelist test cannot be banned.'),
  );
  return system_settings_form($form);
}

/**
 * Form API callback to validate the httpbl settings form.
 */
function httpbl_admin_settings_validate($form_id, $form_values) {
  $key = $form_values['httpbl_accesskey'];
  if ($form_values['httpbl_check'] && !$key) {
    form_set_error('httpbl_accesskey', t('You must enter an access key to enable blacklist checks.'));
  }
  if ($form_values['httpbl_footer'] && !$form_values['httpbl_link']) {
    form_set_error('httpbl_link', t('You must enter a link to be able to add it to the footer.'));
  }
  if (!is_numeric($form_values['httpbl_threatlevel']) || $form_values['httpbl_threatlevel'] > 255 || $form_values['httpbl_threatlevel'] < 0) {
    form_set_error('httpbl_threatlevel', t('Threat level threshold should be a numeric value between 0 to 255.'));
  }
  if ($key) {

    // key should be 12 lowercase alpha characters
    if (ereg('[^a-z]', $key) || strlen($key) != 12) {
      form_set_error('httpbl_accesskey', t('Your access key is formatted incorrectly.'));
    }
    else {
      if (!count(form_get_errors())) {

        // look up 127.80.1.1 as a test
        $lookup = httpbl_dnslookup('127.1.80.1', $key);
        if (!$lookup && $lookup['threat'] == 80) {
          form_set_error('httpbl_accesskey', t('Testcase failed. This either means that your access key is incorrect or that there is a problem in your DNS system.'));
        }
        else {
          drupal_set_message('http:BL test completed succesfully.');
        }
      }
    }
  }
}

/**
 * Implementation of hook_footer().
 *
 * Adds a Project Honeypot link to the footer.
 */
function httpbl_footer($main = 0) {
  if (variable_get('httpbl_footer', FALSE)) {
    $link = variable_get('httpbl_link', NULL);
    $word = variable_get('httpbl_word', 'randomness');
    return httpbl_honeylink($link, $word);
  }
}

/**
 * Implementation of hook_requirements().
 *
 * Shows some basic usage statistics for the module.
 */
function httpbl_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {
    if (!variable_get('httpbl_accesskey', NULL) || !variable_get('httpbl_check', 0)) {
      $requirements['httpbl'] = array(
        'description' => t('IP blacklist lookups are currently disabled; enter your access key <a href="@settings">on the settings page</a> and enable checks to enable blacklist lookups.', array(
          '@settings' => url('admin/settings/httpbl'),
        )),
        'severity' => REQUIREMENT_ERROR,
        'value' => t('Disabled'),
      );
      if (variable_get('httpbl_footer', FALSE)) {
        $requirements['httpbl']['severity'] = REQUIREMENT_WARNING;
      }
    }
    else {
      $stat_black = variable_get('httpbl_stat_black', 0);
      $stat_comment = variable_get('httpbl_stat_comment', 0);
      $stat_grey = variable_get('httpbl_stat_grey', 0);
      if (!variable_get('httpbl_stats', TRUE)) {
        $requirements['httpbl'] = array(
          'description' => t('http:BL is enabled.'),
          'severity' => REQUIREMENT_OK,
          'value' => t('Enabled'),
        );
      }
      else {
        if (variable_get('httpbl_check', 0) == 1) {
          $requirements['httpbl'] = array(
            'description' => t('http:BL is enabled and has blocked %c comments.', array(
              '%c' => $stat_comment,
            )),
            'severity' => REQUIREMENT_OK,
            'value' => t('Enabled'),
          );
        }
        else {
          if (variable_get('httpbl_check', 0)) {
            $requirements['httpbl'] = array(
              'description' => t('http:BL is enabled and has blocked %t visits (%b blacklisted and %g greylisted).', array(
                '%t' => $stat_grey + $stat_black,
                '%b' => $stat_black,
                '%g' => $stat_grey,
              )),
              'severity' => REQUIREMENT_OK,
              'value' => t('Enabled'),
            );
          }
        }
      }
    }
    $requirements['httpbl']['title'] = t('http:BL');
  }
  return $requirements;
}

/**
 * Implementation of hook_cron().
 *
 * Cleans up cache table.
 */
function httpbl_cron() {
  if (variable_get('httpbl_dbcache', TRUE)) {
    db_query("DELETE FROM {httpbl} WHERE expire <= %d", time());
  }
}

/**
 * Return HTML code with hidden Honeypot link
 * in one of the many styles.
 *
 * @param string $link
 * @param string $word
 * @return string
 */
function httpbl_honeylink($link, $word) {
  if (!$link) {
    return;
  }
  $rand = mt_rand(0, 7);
  if ($rand == 0) {
    return '<div><a href="' . $link . '"><!-- ' . $word . ' --></a></div>';
  }
  else {
    if ($rand == 1) {
      return '<div><a href="' . $link . '" style="display: none;">' . $word . '</a></div>';
    }
    else {
      if ($rand == 2) {
        return '<div style="display: none;"><a href="' . $link . '">' . $word . '</a></div>';
      }
      else {
        if ($rand == 3) {
          return '<div><a href="' . $link . '"></a></div>';
        }
        else {
          if ($rand == 4) {
            return '<!-- <a href="' . $link . '">' . $word . '</a> -->';
          }
          else {
            if ($rand == 5) {
              return '<div style="position: absolute; top: -250px; left: -250px;"><a href="' . $link . '">' . $word . '</a></div>';
            }
            else {
              if ($rand == 6) {
                return '<div><a href="' . $link . '"><span style="display: none;">' . $word . '</span></a></div>';
              }
              else {
                return '<div><a href="' . $link . '"><div style="height: 0px; width: 0px;"></div></a></div>';
              }
            }
          }
        }
      }
    }
  }
}

/**
 * Check if an IP should be banned
 * 
 * @param string $ip
 * @param boolean $enable_cache
 * @param boolean $enable_stats
 * @return int
 */
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;
  }
}

/**
 * Check if an IP is known to belong to a search engine
 * 
 * @param string $ip
 * @return bool
 */
function httpbl_se($ip = NULL) {
  if ($response = httpbl_dnslookup($ip)) {
    $iplink = _httpbl_iplink($ip);

    // positive threat, type is search engine (0)
    if ($response['threat'] && $response['type'] == 0) {
      if (variable_get('httpbl_log', 1) > 2) {
        watchdog('httpbl', t('Search engine lookup for %ip was positive (%response)', array(
          '%ip' => $ip,
          '%response' => $response['raw'],
        )), WATCHDOG_NOTICE, $iplink);
      }
      return TRUE;
    }
    else {
      if (variable_get('httpbl_log', 1) > 2) {
        watchdog('httpbl', t('Search engine lookup for %ip was negative (%response)', array(
          '%ip' => $ip,
          '%response' => $response['raw'],
        )), WATCHDOG_NOTICE, $iplink);
      }
      return FALSE;
    }
  }
  else {
    return FALSE;
  }
}

/**
 * Reverse IP octets
 * 
 * @param string $ip
 * @return string
 */
function httpbl_reverse_ip($ip) {
  if (!is_numeric(str_replace('.', '', $ip))) {
    return NULL;
  }
  $ip = explode('.', $ip);
  if (count($ip) != 4) {
    return NULL;
  }
  return $ip[3] . '.' . $ip[2] . '.' . $ip[1] . '.' . $ip[0];
}
function httpbl_request_whitelist() {
  if (httpbl_blacklisted() != 2 || $_SESSION['httpbl_status'] != 'grey') {
    drupal_access_denied();
    exit;
  }
  $form['#redirect'] = 'node';
  $form['reason'] = array(
    '#type' => 'textarea',
    '#title' => t('Reason for block'),
    '#size' => 60,
    '#required' => TRUE,
  );
  $form['block'] = array(
    '#type' => 'textfield',
    '#title' => t('Leave this blank'),
    '#size' => 15,
  );
  $form['leave'] = array(
    '#type' => 'textfield',
    '#size' => 30,
    '#attributes' => array(
      'style' => 'display: none',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Whitelist request'),
  );
  return $form;
}
function httpbl_request_whitelist_validate($form_id, $form_values) {
  $ip = _httpbl_ip_address();
  $iplink = _httpbl_iplink($ip);
  if ($form_values['block'] || $form_values['leave']) {
    if (variable_get('httpbl_dbcache', TRUE)) {
      _httpbl_cache_update($ip, 1, 86400);
      if (variable_get('httpbl_log', 1)) {
        watchdog('httpbl', t('%ip failed session whitelist request, blacklisted for 24 hours.', array(
          '%ip' => $ip,
        )), WATCHDOG_WARNING, $iplink);
      }
      print t('Whitelist request failed; your IP has been blacklisted for 24 hours.');
      exit;
    }
    else {
      if (variable_get('httpbl_log', 1)) {
        watchdog('httpbl', t('%ip failed session whitelist request.', array(
          '%ip' => $ip,
        )), WATCHDOG_WARNING, $iplink);
      }
      print t('Whitelist request failed.');
      exit;
    }
  }
  if (variable_get('httpbl_log', 1)) {
    watchdog('httpbl', t('%ip tried a whitelist request', array(
      '%ip' => $ip,
    )), WATCHDOG_NOTICE, $iplink);
  }
}
function httpbl_request_whitelist_submit($form_id, $form_values) {
  $ip = _httpbl_ip_address();
  $iplink = _httpbl_iplink($ip);
  if (variable_get('httpbl_log', 1)) {
    watchdog('httpbl', t('Session from %ip whitelisted. Reason: @reason', array(
      '%ip' => $ip,
      '@reason' => $form_values['reason'],
    )), WATCHDOG_WARNING, $iplink);
  }
  drupal_set_message(t('The current session has been whitelisted.'));
  $_SESSION['httpbl_status'] = 'white';
}

/**
 * Do http:BL DNS lookup
 * 
 * @param string $ip
 * @param string $key
 * @return array
 */
function httpbl_dnslookup($ip, $key = NULL) {

  // Thanks to J.Wesley2 at
  // http://www.projecthoneypot.org/board/read.php?f=10&i=1&t=1
  if (!($ip = httpbl_reverse_ip($ip))) {
    return FALSE;
  }
  if (!$key) {
    if (!($key = variable_get('httpbl_accesskey', NULL))) {
      return FALSE;
    }
  }
  $query = $key . '.' . $ip . '.dnsbl.httpbl.org.';
  $response = gethostbyname($query);
  if ($response == $query) {

    // if the domain does not resolve then it will be the same thing we passed to gethostbyname
    return FALSE;
  }
  $values = array();
  $values['raw'] = $response;
  $response = explode('.', $response);
  if ($response[0] != '127') {

    // if the first octet is not 127, the response should be considered invalid
    watchdog('httpbl', t('Lookup failed for %ip, response was %response', array(
      '%ip' => $ip,
      '%response' => $values['raw'],
    )), WATCHDOG_ERROR);
    return FALSE;
  }
  $values['last_activity'] = $response[1];
  $values['threat'] = $response[2];
  $values['type'] = $response[3];
  if ($response[3] == 0) {

    //if it's 0 then there's only one thing it can be
    $values['search_engine'] = TRUE;
  }
  if ($response[3] & 1) {

    //does it have the same bits as 1 set
    $values['suspicious'] = TRUE;
  }
  if ($response[3] & 2) {

    //does it have the same bits as 2 set
    $values['harvester'] = TRUE;
  }
  if ($response[3] & 4) {

    //does it have the same bits as 4 set
    $values['comment_spammer'] = TRUE;
  }
  return $values;
}

/**
 * The actual checking function.
 */
function _httpbl_check() {
  global $user;
  if (variable_get('httpbl_check', 0) < 2 || variable_get('httpbl_check', 0) == 2 && $user->uid) {

    // Only check when 'for all users' or 'for all anonymous users' is set
    return;
  }

  // Testcases at http://www.projecthoneypot.org/httpbl_api.php
  // $ip = '127.1.40.1';
  $ip = _httpbl_ip_address();
  $blacklisted = httpbl_blacklisted($ip, TRUE, variable_get('httpbl_stats', TRUE));
  if ($blacklisted && !_httpbl_whitelisted($ip) && !($blacklisted == 2 && $_GET['q'] == 'httpbl/whitelist')) {
    if ($blacklisted == 2) {

      // Greylisted
      $message = variable_get('httpbl_message_grey', "Sorry, %ip has been greylisted by <a href=\"%ipurl\">http:BL</a>.\nYou may try whitelisting on <a href=\"%whitelisturl\">%whitelisturl</a>.\n%honeypot");
      $_SESSION['httpbl_status'] = 'grey';
    }
    else {
      if ($blacklisted == 1) {

        // blacklisted
        $message = variable_get('httpbl_message_black', "Sorry, %ip has been blacklisted by <a href=\"%ipurl\">http:BL</a>.\n%honeypot");
      }
    }
    if ($link = variable_get('httpbl_link', NULL)) {
      $word = variable_get('httpbl_word', 'randomness');
      $link = httpbl_honeylink($link, $word);
    }
    $message = strtr($message, array(
      '%ip' => $ip,
      '%ipurl' => _httpbl_iplink($ip, FALSE),
      '%honeypot' => $link,
      '%whitelisturl' => url('httpbl/whitelist'),
    ));
    header('HTTP/1.1 403 Forbidden');
    print "<html>\n<body>\n";
    print $message . "\n";
    print "</body>\n</html>";
    exit;
  }
}

/**
 * Check if an IP is whitelisted through the access table
 */
function _httpbl_whitelisted($ip) {
  return $_SESSION['httpbl_status'] == 'white' || db_result(db_query_range("SELECT status FROM {access} WHERE type = 'host' AND LOWER('%s') LIKE LOWER(mask) ORDER BY status DESC", $ip, 0, 1)) == '1';
}

/**
 * Write status value into cache table
 */
function _httpbl_cache_set($ip, $status, $offset = 0) {
  db_query("DELETE FROM {httpbl} WHERE hostname = '%s'", $ip);
  db_query("INSERT INTO {httpbl} (hostname, status, expire) VALUES ('%s', %d, %d)", $ip, $status, time() + $offset);
}

/**
 * Update cache table
 */
function _httpbl_cache_update($ip, $status, $offset = 0) {
  db_query("UPDATE {httpbl} SET status = %d, expire = %d WHERE hostname = '%s'", $status, time() + $offset, $ip);
}

/**
 * Get status value from cache table
 */
function _httpbl_cache_get($ip) {
  $result = db_result(db_query("SELECT status FROM {httpbl} WHERE hostname = '%s'", $ip));
  if ($result === '1') {
    return TRUE;
  }
  else {
    if ($result === '0') {
      return FALSE;
    }
    else {
      if ($result === '2') {
        return 2;
      }
    }
  }
}

/**
 * Generate a link with Project Honeypot information for a given IP address.
 */
function _httpbl_iplink($ip, $anchor = TRUE) {
  if ($anchor) {
    return '<a href="http://www.projecthoneypot.org/search_ip.php?ip=' . $ip . '">IP data</a>';
  }
  else {
    return 'http://www.projecthoneypot.org/search_ip.php?ip=' . $ip;
  }
}

/**
 * Backport of Drupal 6's ip_address() function.
 */
function _httpbl_ip_address() {
  static $ip_address = NULL;
  if (!isset($ip_address)) {
    $ip_address = $_SERVER['REMOTE_ADDR'];
    if (variable_get('reverse_proxy', 0) && array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {

      // If an array of known reverse proxy IPs is provided, then trust
      // the XFF header if request really comes from one of them.
      $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
      if (!empty($reverse_proxy_addresses) && in_array($ip_address, $reverse_proxy_addresses, TRUE)) {

        // If there are several arguments, we need to check the most
        // recently added one, i.e. the last one.
        $ip_address = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
      }
    }
  }
  return $ip_address;
}

Functions

Namesort descending Description
httpbl_admin_settings Implementation of hook_settings().
httpbl_admin_settings_validate Form API callback to validate the httpbl settings form.
httpbl_blacklisted Check if an IP should be banned
httpbl_comment Implementation of hook_comment().
httpbl_cron Implementation of hook_cron().
httpbl_dnslookup Do http:BL DNS lookup
httpbl_footer Implementation of hook_footer().
httpbl_honeylink Return HTML code with hidden Honeypot link in one of the many styles.
httpbl_menu Implementation of hook_menu().
httpbl_request_whitelist
httpbl_request_whitelist_submit
httpbl_request_whitelist_validate
httpbl_requirements Implementation of hook_requirements().
httpbl_reverse_ip Reverse IP octets
httpbl_se Check if an IP is known to belong to a search engine
_httpbl_cache_get Get status value from cache table
_httpbl_cache_set Write status value into cache table
_httpbl_cache_update Update cache table
_httpbl_check The actual checking function.
_httpbl_iplink Generate a link with Project Honeypot information for a given IP address.
_httpbl_ip_address Backport of Drupal 6's ip_address() function.
_httpbl_whitelisted Check if an IP is whitelisted through the access table