placeholder
Last active
December 11, 2015 01:09
-
-
Save hron84/4521299 to your computer and use it in GitHub Desktop.
Blocked IPs Drupal 6 module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function block_ips_overview($keys = NULL) { | |
if ($keys) { | |
// Replace wildcards with MySQL/PostgreSQL wildcards. | |
$keys = preg_replace('!\*+!', '%', $keys); | |
$sql = "SELECT * FROM {url_alias} WHERE dst LIKE '%%%s%%'"; | |
} | |
$header = array( | |
array('data' => t('IP address'), 'field' => 'ip', 'sort' => 'asc'), | |
array('data' => t('Operations')), | |
); | |
$sql = "SELECT * FROM {blocked_ips}"; | |
$sql .= tablesort_sql($header); | |
$result = pager_query($sql, 50, 0 , NULL, $keys); | |
$rows = array(); | |
while($data = db_fetch_array($result)) { | |
$rows []= array( | |
l(check_plain($data['ip']), "admin/settings/blocked_ips/edit/{$data['iid']}"), | |
l(t('delete'), "admin/settings/blocked_ips/delete/{$data['iid']}") | |
); | |
} | |
if (empty($rows)) { | |
$empty_message = $keys ? t('No IP addresses found.') : t('No IP addresses available.') ; | |
$rows[] = array(array('data' => $empty_message, 'colspan' => 2)); | |
} | |
$output .= theme('table', $header, $rows); | |
$output .= theme('pager', NULL, 50, 0); | |
return $output; | |
} | |
function blocked_ips_edit($iid = NULL) { | |
if($iid) { | |
$blocked_ip = blocked_ip_load($iid); | |
drupal_set_title(check_plain($blocked_ip['ip'])); | |
$output = drupal_get_form('blocked_ips_form', $blocked_ip); | |
} else { | |
$output = drupal_get_form('blocked_ips_form'); | |
} | |
return $output; | |
} | |
function blocked_ips_form(&$form_state, $edit = array('ip' => NULL, 'iid' => NULL)) { | |
$form['#ip'] = $edit; | |
$form['ip'] = array( | |
'#type' => 'textfield', | |
'#title' => t('IP address'), | |
'#description' => t('Specify the IP address what you wish to block out. For example: 1.2.3.4'), | |
'#default_value' => $edit['ip'], | |
'#maxlength' => 128, | |
'#size' => 45, | |
'#required' => TRUE, | |
); | |
if ($edit['iid']) { | |
$form['iid'] = array('#type' => 'hidden', '#value' => $edit['iid']); | |
$form['submit'] = array('#type' => 'submit', '#value' => t('Update IP')); | |
} else { | |
$form['submit'] = array('#type' => 'submit', '#value' => t('Add')); | |
} | |
return $form; | |
} | |
function blocked_ips_form_validate($form, &$form_state) { | |
$ip = $form_state['values']['ip']; | |
$iid = isset($form_state['values']['iid']) ? $form_state['values']['iid'] : 0; | |
if($iid == 0 AND db_result(db_query("SELECT COUNT(ip) FROM {blocked_ips} WHERE ip = '%s'", $ip))) { | |
form_set_error('ip', t('IP address is already blocked')); | |
} | |
} | |
function blocked_ips_form_submit($form, &$form_state) { | |
$ip = $form_state['values']['ip']; | |
$iid = isset($form_state['values']['iid']) ? $form_state['values']['iid'] : 0; | |
if($iid > 0) { | |
db_query("UPDATE {blocked_ips} SET ip = '%s' WHERE iid = %s", $ip, $iid); | |
} else { | |
db_query("INSERT INTO {blocked_ips} (ip) VALUES ('%s')", $ip); | |
} | |
drupal_set_message(t('The IP address has been saved')); | |
$form_state['redirect'] = 'admin/settings/blocked_ips'; | |
return; | |
} | |
function blocked_ips_delete_confirm($form_state, $iid) { | |
$ip = blocked_ip_load($iid); | |
if(user_access('adminster site configuration')) { | |
$form['iid'] = array('#type' => 'value', '#value' => $iid); | |
$output = confirm_form($form, | |
t('Are you sure you want to unblock IP address %ip?', array('%ip' => $ip['ip'])), | |
isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/blocked_ips'); | |
} | |
return $output; | |
} | |
function blocked_ips_delete_confirm_submit($form, &$form_state) { | |
if ($form_state['values']['confirm']) { | |
db_query("DELETE FROM {blocked_ips} WHERE iid = '%s'", $form_state['values']['iid']); | |
$form_state['redirect'] = 'admin/settings/blocked_ips'; | |
return; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = Blocked IPs | |
description = Block requests from specific IP address | |
core = 6.x | |
version = "6.x-dev" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function blocked_ips_schema() { | |
$schema['blocked_ips'] = array( | |
'description' => 'Stores blocked IP addresses.', | |
'fields' => array( | |
'iid' => array( | |
'description' => 'Primary Key: unique ID for IP addresses.', | |
'type' => 'serial', | |
'unsigned' => TRUE, | |
'not null' => TRUE, | |
), | |
'ip' => array( | |
'description' => 'IP address', | |
'type' => 'varchar', | |
'length' => 40, | |
'not null' => TRUE, | |
'default' => '', | |
), | |
), | |
'indexes' => array( | |
'blocked_ip' => array('ip'), | |
), | |
'primary key' => array('iid'), | |
); | |
return $schema; | |
} | |
/** | |
* Implementation of hook_install() | |
*/ | |
function blocked_ips_install() { | |
drupal_install_schema('blocked_ips'); | |
} | |
/** | |
* Implementation of hook_uninstall() | |
*/ | |
function blocked_ips_uninstall() { | |
drupal_uninstall_schema('blocked_ips'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function blocked_ip_load($iid) { | |
return db_fetch_array(db_query("SELECT * FROM {blocked_ips} WHERE iid = '%s'", $iid)); | |
} | |
/** | |
* Checks to see if an IP address has been blocked. | |
* | |
* Blocked IP addresses are stored in the database by default. However for | |
* performance reasons we allow an override in settings.php. This allows us | |
* to avoid querying the database at this critical stage of the bootstrap if | |
* an administrative interface for IP address blocking is not required. | |
* | |
* Copied and ported from Drupal 7 | |
* | |
* @param $ip | |
* IP address to check. | |
* | |
* @return bool | |
* TRUE if access is denied, FALSE if access is allowed. | |
*/ | |
function blocked_ips_is_denied($ip) { | |
// Because this function is called on every page request, we first check | |
// for an array of IP addresses in settings.php before querying the | |
// database. | |
$blocked_ips = variable_get('blocked_ips', NULL); | |
$denied = FALSE; | |
if (isset($blocked_ips) && is_array($blocked_ips)) { | |
$denied = in_array($ip, $blocked_ips); | |
} else { | |
$denied = (bool)db_result(db_query("SELECT 1 FROM {blocked_ips} WHERE ip = '%s'",$ip)); | |
} | |
return $denied; | |
} | |
/** | |
* Handles denied users. | |
* | |
* Implementation of hook_init() | |
*/ | |
function blocked_ips_init() { | |
$users_ip = ip_address(); | |
if(blocked_ips_is_denied($users_ip)) { | |
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); | |
print 'Sorry, ' . check_plain($users_ip) . ' has been banned.'; | |
exit(); | |
} | |
} | |
function blocked_ips_menu() { | |
$items['admin/settings/blocked_ips'] = array( | |
'title' => 'Blocked IP addresses', | |
'description' => 'Block access of site from specific IP addresses', | |
'page callback' => 'block_ips_overview', | |
'access arguments' => array('adminster site configuration'), | |
'file' => 'blocked_ips.admin.inc', | |
); | |
$items['admin/settings/blocked_ips/add'] = array( | |
'title' => 'Add IP for blocking', | |
'page callback' => 'blocked_ips_edit', | |
'access arguments' => array('adminster site configuration'), | |
'type' => MENU_LOCAL_TASK, | |
'file' => 'blocked_ips.admin.inc', | |
); | |
$items['admin/settings/blocked_ips/edit'] = array( | |
'title' => 'Add IP for blocking', | |
'page callback' => 'blocked_ips_edit', | |
'access arguments' => array('adminster site configuration'), | |
'type' => MENU_CALLBACK, | |
'file' => 'blocked_ips.admin.inc', | |
); | |
$items['admin/settings/blocked_ips/delete'] = array( | |
'title' => 'Delete blocked IP address', | |
'type' => MENU_CALLBACK, | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('blocked_ips_delete_confirm'), | |
'access arguments' => array('adminster site configuration'), | |
'file' => 'blocked_ips.admin.inc', | |
); | |
$items['admin/settings/blocked_ips/list'] = array( | |
'title' => 'List', | |
'type' => MENU_DEFAULT_LOCAL_TASK, | |
'weight' => -10, | |
); | |
return $items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment