Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

122 lines (107 sloc) 3.778 kB
<?php
// $Id$
/**
* @file
* Block the Buma/Stemra Copyright crawler on your site
* @author Bèr Kessels <ber@webschuur.com>
* @link http://webschuur.com
* @package bs_blocker
*/
/* -----------------------------------------------------------------
Hook Implementations
------------------------------------------------------------------ */
/**
* Implementation of hook_menu().
*/
function bs_blocker_menu() {
$items = array();
$items['admin/settings/bs_blocker'] = array(
'title' => t('BS Blocker'),
'description' => t('Sets the interval to check for new ip-addresses to block'),
'page callback' => 'drupal_get_form',
'page arguments' => array('bs_blocker_settings'),
'access arguments' => array('access administration pages'),
'menu_name' => 'Navigation',
);
return $items;
}
/** .
*
* Settings form for a.o. the cron interval.
*
* @return
* Drupal FAPI form
* @TODO: add an option to change the location where to pull the IP addresses from.
*/
function bs_blocker_settings() {
$period = drupal_map_assoc(array(21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
$form['bs_blocker_interval'] = array(
'#type' => 'select',
'#title' => t('Interval'),
'#description' => t('Select the interval on which the BS blocker must check for new IP adresses.'),
'#weight' => 0,
'#required' => TRUE,
'#default_value' => variable_get('bs_blocker_interval', 604800),
'#options' => $period,
);
return system_settings_form($form);
}
/**
* Implementation of hook_cron().
*/
function bs_blocker_cron() {
$time = time();
if (variable_get('bs_blocker_interval', 604800) >= ($time - variable_get('bs_blocker_last_ran', $time))) {
$ips = array();
$headers = array();
$last_ips = array();
if ($feed['etag']) {
$headers['If-None-Match'] = $feed['etag'];
}
if ($feed['modified']) {
$headers['If-Modified-Since'] = gmdate('D, d M Y H:i:s', $feed['modified']) .' GMT';
}
$result = drupal_http_request('http://foo.cloud.github.com/downloads/berkes/BS-Blocker/ip-addresses.txt', $headers); //Try the online file.
if ($result->code) {
switch ($result->code) {
case 404:
return;
case 304: //no change since last check.
variable_set('bs_blocker_last_ran', $time);
return;
break;
case 301: //@TODO: include redirection handling.
return;
case 200:
case 302:
case 307:
if (strlen($result->data)) {
$data = explode("\n", $result->data);
}
break;
}
}
else { //we could not open the remote file, include the local one.
$file = fopen(drupal_get_path('module', 'bs_blocker') .'/ip-addresses.txt');
while(!feof($file)) {
$data = trim(fgets($file));
}
fclose($file);
}
//remove all ips that were added last time. (aka: refresh)
//@TODO: find a better way to refresh then delete-add on each run.
foreach(variable_get('bs_blocker_last_ips', array()) as $line) {
db_query("DELETE FROM {access} WHERE mask = '%s'", $line);
watchdog('action', 'Removed IP address from bans %ip', array('%ip' => $line));
}
foreach($data as $line) {
if (!preg_match('/^\#/', $line) && (strlen($line) > 0)) { //ignore commented or empty lines.
$last_ips[] = $line;
db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $line, 'host', 0);
watchdog('action', 'Banned IP address %ip', array('%ip' => $line));
}
}
variable_set('bs_blocker_last_ips', $last_ips);
variable_get('bs_blocker_last_ran', $time);
}
}
Jump to Line
Something went wrong with that request. Please try again.