--- troll.module 2007-03-08 23:07:32.000000000 +0800 +++ troll.module 2008-03-12 17:39:13.000000000 +0800 @@ -302,12 +302,20 @@ function troll_blacklist($op = NULL, $ed case t('Search Blacklisted IPs'): return troll_blacklist_search($_POST['ip_address']); break; + case t('Insert Blacklist IPs'): + troll_list_insert_ips($_POST['blacklist_addr1'], $_POST['blacklist_addr2'], 'blacklist'); + drupal_goto('admin/settings/troll/ip_blacklist'); + break; + case t('Update Blacklist IPs'): + troll_list_insert_ips($_POST['blacklist_addr1'], $_POST['blacklist_addr2'], 'blacklist'); + drupal_goto('admin/settings/troll/ip_blacklist'); + break; case t('Insert Whitelist IPs'): - troll_whitelist_insert_ips($_POST['whitelist_addr1'], $_POST['whitelist_addr2']); + troll_list_insert_ips($_POST['whitelist_addr1'], $_POST['whitelist_addr2'], 'whitelist'); drupal_goto('admin/settings/troll/ip_blacklist'); break; case t('Update Whitelist IPs'): - troll_whitelist_insert_ips($_POST['whitelist_addr1'], $_POST['whitelist_addr2']); + troll_list_insert_ips($_POST['whitelist_addr1'], $_POST['whitelist_addr2'], 'whitelist'); drupal_goto('admin/settings/troll/ip_blacklist'); break; case 'deletewhite': @@ -357,12 +365,20 @@ function troll_blacklist($op = NULL, $ed '#collapsible' => true, '#collapsed' => true ); + $form['blacklist_form'] = array( + '#type' => 'fieldset', + '#title' => t('Blacklist IPs'), + '#value' => troll_list(NULL, NULL, 'blacklist'), + '#weight' => 1, + '#collapsible' => true, + '#collapsed' => true + ); $form['whitelist_form'] = array( '#type' => 'fieldset', '#title' => t('Whitelist IPs'), - '#value' => troll_whitelist(), + '#value' => troll_list(NULL, NULL, 'whitelist'), '#description' => t('Whitelisted IPs override the blacklist. Does not apply to the IP Ban feature.'), - '#weight' => 1, + '#weight' => 2, '#collapsible' => true, '#collapsed' => true ); @@ -669,9 +685,9 @@ function troll_blacklist_search($ip_addr * @uses troll_whitelist_form() * @return string */ -function troll_whitelist($net = NULL, $bcast = NULL) { - $output = drupal_get_form('troll_whitelist_form', $net, $bcast); - $sql = 'SELECT net, bcast FROM {troll_whitelist}'; +function troll_list($net = NULL, $bcast = NULL, $type) { + $output = drupal_get_form('troll_list_form', $net, $bcast, $type); + $sql = 'SELECT net, bcast FROM {troll_'. $type .'}'; $headers = array( array('data' => t('Start/Network Address'), 'field' => 'net', 'sort' => 'asc'), @@ -684,7 +700,7 @@ function troll_whitelist($net = NULL, $b while($row = db_fetch_object($result)) { $printnet = long2ip($row->net); $printbcast = long2ip($row->bcast); - $action = l(t('remove'), "admin/settings/troll/ip_blacklist/deletewhite/{$row->net}/{$row->bcast}"); + $action = l(t('remove'), "admin/settings/troll/ip_blacklist/". (($type == 'whitelist') ? 'deletewhite' : 'deleteblack') ."/{$row->net}/{$row->bcast}"); $rows[] = array($printnet, $printbcast, $action); } @@ -697,32 +713,33 @@ function troll_whitelist($net = NULL, $b } /** - * Display form for creating new whitelist block and table of current whitelisted IPs + * Display form for creating new whitelist or blacklist block and table + * of current whitelisted or blacklisted IPs * * @return array */ -function troll_whitelist_form($net, $bcast) { - $form['whitelist_addr1'] = array( +function troll_list_form($net, $bcast, $type) { + $form[$type .'_addr1'] = array( '#type' => 'textfield', '#title' => t('Starting IP Address'), '#size' => 15, '#maxlength' => 15, '#default_value' => $net ? long2ip($net) : '', - '#description' => t('IP or start to range of IPs to whitelist.'), + '#description' => t('IP or start to range of IPs to '. $type), '#required' => TRUE ); - $form['whitelist_addr2'] = array( + $form[$type .'_addr2'] = array( '#type' => 'textfield', '#title' => t('Ending IP Address'), '#size' => 15, '#maxlength' => 15, '#default_value' => $bcast ? long2ip($bcast) : '', - '#description' => t('End of IP range to whitelist. If whitelisting a single IP, leave this blank.'), + '#description' => t('End of IP range to '. $type .'. If '. $type .'ing a single IP, leave this blank.'), '#required' => false ); $form['submit'] = array( '#type' => 'submit', - '#value' => ($net && $bcast) ? t('Update Whitelist IPs') : t('Insert Whitelist IPs'), + '#value' => ($net && $bcast) ? ($type == 'whitelist' ? t('Update Whitelist IPs') : t('Update Blacklist IPs')) : ($type == 'whitelist' ? t('Insert Whitelist IPs') : t('Insert Blacklist IPs')), '#weight' => 1 ); $form['#action'] = url('admin/settings/troll/ip_blacklist'); @@ -731,21 +748,21 @@ function troll_whitelist_form($net, $bca } /** - * Insert a new IP block into the whitelist + * Insert a new IP block into the whitelist or whitelist * * @param array $edit */ -function troll_whitelist_insert_ips($whitelist_addr1, $whitelist_addr2) { - $longip1 = _troll_longip($whitelist_addr1); - $longip2 = _troll_longip(empty($whitelist_addr2) ? $whitelist_addr1 : $whitelist_addr2); +function troll_list_insert_ips($addr1, $addr2, $type) { + $longip1 = _troll_longip($addr1); + $longip2 = _troll_longip(empty($addr2) ? $addr1 : $addr2); if ($longip1 > $longip2) { $temp = $longip1; $longip1 = $longip2; $longip2 = $temp; unset($temp); } - db_query("REPLACE INTO {troll_whitelist} (net, bcast) VALUES (%d, %d)", $longip1, $longip2); - drupal_set_message(t('IP range %ip1 to %ip2 whitelisted.', array('%ip1' => long2ip($longip1), '%ip2' => long2ip($longip2)))); + db_query("REPLACE INTO {troll_". $type ."} (net, bcast) VALUES (%d, %d)", $longip1, $longip2); + drupal_set_message(t('IP range %ip1 to %ip2 '. $type .'ed.', array('%ip1' => long2ip($longip1), '%ip2' => long2ip($longip2)))); } /**