You are here

function _httpbl_cache_set in http:BL 7

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

Write status value into httpbl cache table ** (add Grey and/or Blacklisted IPs to httpbl table)

1 call to _httpbl_cache_set()
httpbl_check in ./httpbl.module
Check if an IP should be banned

File

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

Code

function _httpbl_cache_set($ip, $status, $offset = 0) {

  // DELETE and then INSERT leads to race conditions [#723358].
  // evidence says transactions are required -- deekayen
  $txn = db_transaction();
  db_query("REPLACE {httpbl} (hostname, status, expire) VALUES (:ip, :status, :expire)", array(
    ':ip' => $ip,
    ':status' => $status,
    ':expire' => REQUEST_TIME + $offset,
  ));

  // Also, conditionally, ban blacklisted IPs in Drupal blocked_ips table
  if ($status == HTTPBL_LIST_BLACK && variable_get('httpbl_cache', HTTPBL_CACHE_DBDRUPAL) == HTTPBL_CACHE_DBDRUPAL) {
    if (_httpbl_banned_check($ip)) {
      if ($logs = variable_get('httpbl_log', HTTPBL_LOG_MIN)) {
        watchdog('httpbl', 'This IP (%ip) was already banned', array(
          '%ip' => $ip,
        ), WATCHDOG_WARNING, NULL);
      }
      drupal_set_message(t('IP address %ip currently banned from this site.', array(
        '%ip' => $ip,
      )), 'error', FALSE);

      // Note: Logically, it is theoretically impossible for the user to ever see this message because
      // they would have been banned from getting this far, but it's here for debugging purposes.  Should
      // anyone report seeing this it would mean that banning is not occurring as it should.
    }
    else {
      db_insert('blocked_ips')
        ->fields(array(
        'ip' => $ip,
      ))
        ->execute();
      if ($logs = variable_get('httpbl_log', HTTPBL_LOG_MIN)) {
        watchdog('httpbl', 'Added IP (%ip) to site banned IPs', array(
          '%ip' => $ip,
        ), WATCHDOG_WARNING, NULL);
      }
      drupal_set_message(t('Your IP address ( %ip ) has been banned from this site.', array(
        '%ip' => $ip,
      )), 'error', FALSE);

      // Note: Logically, it is theoretically impossible for the user to ever see this message because
      // they will have been banned before the see this page, but it's here for debugging purposes.  Should
      // anyone report seeing this it would mean that banning is not occurring as it should.
    }
  }
}