| 1 |
<?php |
<?php |
| 2 |
// $Id: troll.admin.inc,v 1.1.2.10 2009/04/24 18:31:08 deekayen Exp $ |
// $Id: troll.admin.inc,v 1.1.2.11 2009/05/04 19:06:09 deekayen Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 192 |
'#title' => t('Download and import'), |
'#title' => t('Download and import'), |
| 193 |
'#options' => $options, |
'#options' => $options, |
| 194 |
'#description' => t('Downloads supported blacklist from the internet. Please select a mirror when possible.'), |
'#description' => t('Downloads supported blacklist from the internet. Please select a mirror when possible.'), |
| 195 |
); |
); |
| 196 |
$form['custom_list'] = array( |
$form['url_list'] = array( |
| 197 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 198 |
'#title' => t('List URL'), |
'#title' => t('List URL'), |
| 199 |
'#description' => t('URL of a list to import. List files should have one address per line in Classless Internet Domain Routing CIDR format (x.x.x.x/x). Individual IPs should still have /32.'), |
'#description' => t('URL of a list to import. List files should have one address per line in Classless Internet Domain Routing CIDR format (x.x.x.x/x). Individual IPs should still have /32.'), |
| 200 |
); |
); |
| 201 |
|
$form['custom_list'] = array( |
| 202 |
|
'#type' => 'textarea', |
| 203 |
|
'#rows' => 10, |
| 204 |
|
'#title' => t('List'), |
| 205 |
|
'#description' => t('List one address per line in Classless Internet Domain Routing CIDR format (x.x.x.x/x). Individual IPs should still have /32.'), |
| 206 |
|
); |
| 207 |
$form['submit'] = array( |
$form['submit'] = array( |
| 208 |
'#type' => 'submit', |
'#type' => 'submit', |
| 209 |
'#value' => t('Import List'), |
'#value' => t('Import List'), |
| 224 |
} |
} |
| 225 |
} |
} |
| 226 |
if (!empty($form_state['values']['select_list'])) { |
if (!empty($form_state['values']['select_list'])) { |
| 227 |
troll_blacklist_import_list($form_state['values']['select_list']); |
troll_blacklist_import_remote($form_state['values']['select_list']); |
| 228 |
|
} |
| 229 |
|
if (!empty($form_state['values']['url_list'])) { |
| 230 |
|
troll_blacklist_import_url($form_state['values']['url_list']); |
| 231 |
} |
} |
| 232 |
if (!empty($form_state['values']['custom_list'])) { |
if (!empty($form_state['values']['custom_list'])) { |
| 233 |
troll_blacklist_import_url($form_state['values']['custom_list']); |
troll_blacklist_import_list($form_state['values']['custom_list']); |
| 234 |
} |
} |
| 235 |
$form_state['redirect'] = 'admin/user/troll/ip_blacklist'; |
$form_state['redirect'] = 'admin/user/troll/ip_blacklist'; |
| 236 |
} |
} |
| 242 |
* @see troll_blacklist_parse_save() |
* @see troll_blacklist_parse_save() |
| 243 |
* @param array $edit |
* @param array $edit |
| 244 |
*/ |
*/ |
| 245 |
function troll_blacklist_import_list($list) { |
function troll_blacklist_import_remote($list) { |
| 246 |
$files = array( |
$files = array( |
| 247 |
'OCgz' => array('gz' => 'http://www.openbsd.org/spamd/chinacidr.txt.gz'), |
'OCgz' => array('gz' => 'http://www.openbsd.org/spamd/chinacidr.txt.gz'), |
| 248 |
'OKgz' => array('gz' => 'http://www.openbsd.org/spamd/koreacidr.txt.gz'), |
'OKgz' => array('gz' => 'http://www.openbsd.org/spamd/koreacidr.txt.gz'), |
| 271 |
troll_blacklist_parse_save($edit['custom_list'], $file_parts['extension']); |
troll_blacklist_parse_save($edit['custom_list'], $file_parts['extension']); |
| 272 |
} |
} |
| 273 |
|
|
| 274 |
|
function troll_blacklist_import_list($list) { |
| 275 |
|
$list = preg_split("/\s+/", $list); |
| 276 |
|
foreach ($list as $ip) { |
| 277 |
|
_troll_blacklist_insert_ip($ip); |
| 278 |
|
} |
| 279 |
|
} |
| 280 |
|
|
| 281 |
/** |
/** |
| 282 |
* Parses input file, line by line, searching for IP blocks in CIDR |
* Parses input file, line by line, searching for IP blocks in CIDR |
| 283 |
* format (x.x.x.x/x). Understands files in text, bzip, and bzip2 format, |
* format (x.x.x.x/x). Understands files in text, bzip, and bzip2 format, |
| 289 |
* @param $file_type string |
* @param $file_type string |
| 290 |
*/ |
*/ |
| 291 |
function troll_blacklist_parse_save($file_name, $file_type) { |
function troll_blacklist_parse_save($file_name, $file_type) { |
|
$netmasks = array( |
|
|
0, -2147483648, -1073741824, -536870912, -268435456, -134217728, |
|
|
-67108864, -33554432, -16777216, -8388608, -4194304, -2097152, -1048576, |
|
|
-524288, -262144, -131072, -65536, -32768, -16384, -8192, -4096, -2048, |
|
|
-1024, -512, -256, -128, -64, -32, -16, -8, -4, -2, -1); |
|
|
|
|
| 292 |
switch ($file_type) { |
switch ($file_type) { |
| 293 |
case 'gz': |
case 'gz': |
| 294 |
$fp = gzopen($file_name, 'r'); |
$fp = gzopen($file_name, 'r'); |
| 303 |
while (!feof($fp)) { |
while (!feof($fp)) { |
| 304 |
$buffer = fgets($fp); |
$buffer = fgets($fp); |
| 305 |
if ($buffer{0} != '#' && ($buffer{0} != '/' && $buffer{1} != '/') && !empty($buffer)) { |
if ($buffer{0} != '#' && ($buffer{0} != '/' && $buffer{1} != '/') && !empty($buffer)) { |
| 306 |
preg_match("/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})/", $buffer, $matches); |
_troll_blacklist_insert_ip($buffer); |
|
// $matches array |
|
|
// 0 = whole string |
|
|
// 1 = i.i.i.i |
|
|
// 2 = s |
|
|
$longip = ip2long($matches[1]); |
|
|
unset($buffer, $matches[0], $matches[1]); |
|
|
|
|
|
if ($matches[2] == 32) { |
|
|
$net = $bcast = $longip; |
|
|
} |
|
|
else { |
|
|
$net = $longip & $netmasks[$matches[2]]; |
|
|
$bcast = $longip | ($netmasks[$matches[2]] ^ -1); |
|
|
} |
|
|
db_query('DELETE FROM {troll_blacklist} WHERE net = %d AND bcast = %d', $net, $bcast); |
|
|
db_query('INSERT INTO {troll_blacklist} (net, bcast) VALUES (%d, %d)', $net, $bcast); |
|
| 307 |
$i++; |
$i++; |
| 308 |
} |
} |
| 309 |
} |
} |
| 322 |
} |
} |
| 323 |
} |
} |
| 324 |
|
|
| 325 |
|
function _troll_blacklist_insert_ip($ip) { |
| 326 |
|
$netmasks = array( |
| 327 |
|
0, -2147483648, -1073741824, -536870912, -268435456, -134217728, |
| 328 |
|
-67108864, -33554432, -16777216, -8388608, -4194304, -2097152, -1048576, |
| 329 |
|
-524288, -262144, -131072, -65536, -32768, -16384, -8192, -4096, -2048, |
| 330 |
|
-1024, -512, -256, -128, -64, -32, -16, -8, -4, -2, -1); |
| 331 |
|
|
| 332 |
|
preg_match("/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})/", $ip, $matches); |
| 333 |
|
// $matches array |
| 334 |
|
// 0 = whole string |
| 335 |
|
// 1 = i.i.i.i |
| 336 |
|
// 2 = s |
| 337 |
|
$longip = ip2long($matches[1]); |
| 338 |
|
unset($ip, $matches[0], $matches[1]); |
| 339 |
|
|
| 340 |
|
if ($matches[2] == 32) { |
| 341 |
|
$net = $bcast = $longip; |
| 342 |
|
} |
| 343 |
|
else { |
| 344 |
|
$net = $longip & $netmasks[$matches[2]]; |
| 345 |
|
$bcast = $longip | ($netmasks[$matches[2]] ^ -1); |
| 346 |
|
} |
| 347 |
|
if (!empty($net)) { |
| 348 |
|
db_query('DELETE FROM {troll_blacklist} WHERE net = %f AND bcast = %f', $net, $bcast); |
| 349 |
|
return db_query('INSERT INTO {troll_blacklist} (net, bcast) VALUES (%f, %f)', $net, $bcast); |
| 350 |
|
} |
| 351 |
|
watchdog('troll', 'Malformed %ip not imported', array('%ip' => $ip), WATCHDOG_DEBUG); |
| 352 |
|
return FALSE; |
| 353 |
|
} |
| 354 |
|
|
| 355 |
/** |
/** |
| 356 |
* Form to search blacklist to see if an IP matches |
* Form to search blacklist to see if an IP matches |
| 357 |
* |
* |
| 406 |
$result = pager_query('SELECT net, bcast FROM {troll_blacklist}', 25, 0, 'SELECT COUNT(*) FROM {troll_blacklist}'); |
$result = pager_query('SELECT net, bcast FROM {troll_blacklist}', 25, 0, 'SELECT COUNT(*) FROM {troll_blacklist}'); |
| 407 |
} |
} |
| 408 |
else { |
else { |
| 409 |
$sql = "SELECT net, bcast FROM {troll_blacklist} WHERE net <= %d AND bcast >= %d"; |
$sql = "SELECT net, bcast FROM {troll_blacklist} WHERE net <= %f AND bcast >= %f"; |
| 410 |
$count_sql = 'SELECT COUNT(*) FROM {troll_blacklist} WHERE net <= %d AND bcast >= %d'; |
$count_sql = 'SELECT COUNT(*) FROM {troll_blacklist} WHERE net <= %f AND bcast >= %f'; |
| 411 |
$longip = _troll_longip($ip_address); |
$longip = _troll_longip($ip_address); |
| 412 |
$sql .= tablesort_sql($headers); |
$sql .= tablesort_sql($headers); |
| 413 |
$result = pager_query($sql, 25, 0, $count_sql, $longip, $longip); |
$result = pager_query($sql, 25, 0, $count_sql, $longip, $longip); |
| 524 |
$longip2 = $temp; |
$longip2 = $temp; |
| 525 |
unset($temp); |
unset($temp); |
| 526 |
} |
} |
| 527 |
db_query("REPLACE INTO {troll_whitelist} (net, bcast) VALUES (%d, %d)", $longip1, $longip2); |
db_query("REPLACE INTO {troll_whitelist} (net, bcast) VALUES (%f, %f)", $longip1, $longip2); |
| 528 |
drupal_set_message(t('IP range %ip1 to %ip2 whitelisted.', array('%ip1' => long2ip($longip1), '%ip2' => long2ip($longip2)))); |
drupal_set_message(t('IP range %ip1 to %ip2 whitelisted.', array('%ip1' => long2ip($longip1), '%ip2' => long2ip($longip2)))); |
| 529 |
} |
} |
| 530 |
|
|
| 745 |
* @param integer $bcast IP in long format |
* @param integer $bcast IP in long format |
| 746 |
*/ |
*/ |
| 747 |
function troll_confirm_delete_black_block_form($form_state, $net, $bcast) { |
function troll_confirm_delete_black_block_form($form_state, $net, $bcast) { |
| 748 |
$result = db_result(db_query('SELECT COUNT(net) FROM {troll_blacklist} WHERE net = %d AND bcast = %d', $net, $bcast)); |
$result = db_result(db_query('SELECT COUNT(net) FROM {troll_blacklist} WHERE net = %f AND bcast = %f', $net, $bcast)); |
| 749 |
if (empty($result)) { |
if (empty($result)) { |
| 750 |
drupal_set_message(t('No such IP range found in the database.')); |
drupal_set_message(t('No such IP range found in the database.')); |
| 751 |
drupal_goto('admin/user/troll/ip_blacklist/search'); |
drupal_goto('admin/user/troll/ip_blacklist/search'); |
| 784 |
* @param integer $bcast IP in long format |
* @param integer $bcast IP in long format |
| 785 |
*/ |
*/ |
| 786 |
function troll_confirm_delete_white_block_form($form_state, $net, $bcast) { |
function troll_confirm_delete_white_block_form($form_state, $net, $bcast) { |
| 787 |
$result = db_result(db_query('SELECT COUNT(net) FROM {troll_whitelist} WHERE net = %d AND bcast = %d', $net, $bcast)); |
$result = db_result(db_query('SELECT COUNT(net) FROM {troll_whitelist} WHERE net = %f AND bcast = %f', $net, $bcast)); |
| 788 |
if (empty($result)) { |
if (empty($result)) { |
| 789 |
drupal_set_message(t('No such IP range found in the database.')); |
drupal_set_message(t('No such IP range found in the database.')); |
| 790 |
drupal_goto('admin/user/troll/ip_blacklist/whitelist'); |
drupal_goto('admin/user/troll/ip_blacklist/whitelist'); |