/[drupal]/contributions/modules/troll/troll.admin.inc
ViewVC logotype

Diff of /contributions/modules/troll/troll.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.11, Mon May 4 19:06:09 2009 UTC revision 1.1.2.12, Sat May 9 05:47:46 2009 UTC
# Line 1  Line 1 
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
# Line 192  function troll_blacklist_import_form() { Line 192  function troll_blacklist_import_form() {
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'),
# Line 218  function troll_blacklist_import_form_sub Line 224  function troll_blacklist_import_form_sub
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  }  }
# Line 233  function troll_blacklist_import_form_sub Line 242  function troll_blacklist_import_form_sub
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'),
# Line 262  function troll_blacklist_import_url($edi Line 271  function troll_blacklist_import_url($edi
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,
# Line 273  function troll_blacklist_import_url($edi Line 289  function troll_blacklist_import_url($edi
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');
# Line 293  function troll_blacklist_parse_save($fil Line 303  function troll_blacklist_parse_save($fil
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      }      }
# Line 328  function troll_blacklist_parse_save($fil Line 322  function troll_blacklist_parse_save($fil
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   *   *
# Line 382  function troll_blacklist_search($ip_addr Line 406  function troll_blacklist_search($ip_addr
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);
# Line 500  function troll_whitelist_form_submit($fo Line 524  function troll_whitelist_form_submit($fo
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    
# Line 721  function troll_confirm_ban_ip_form_submi Line 745  function troll_confirm_ban_ip_form_submi
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');
# Line 760  function troll_confirm_delete_black_bloc Line 784  function troll_confirm_delete_black_bloc
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');

Legend:
Removed from v.1.1.2.11  
changed lines
  Added in v.1.1.2.12

  ViewVC Help
Powered by ViewVC 1.1.2