uid) { $track = db_fetch_object(db_query("SELECT * FROM {troll_ip_track} WHERE uid = %d AND ip_address = '%s'", $user->uid, troll_ip_address())); if (!empty($track->uid)) { // A record for this IP exists. Update accessed timestamp. db_query("UPDATE {troll_ip_track} SET accessed = %d WHERE uid = %d AND ip_address = '%s'", time(), $user->uid, troll_ip_address()); } else { // insert new IP record for user db_query("INSERT INTO {troll_ip_track} (uid, ip_address, created, accessed) VALUES (%d, '%s', %d, %d)", $user->uid, troll_ip_address(), time(), time()); } } if (variable_get('troll_enable_ip_ban', 1)) { $ban = db_fetch_object(db_query('SELECT * FROM {troll_ip_ban} WHERE (expires > %d OR expires = 0) AND ip_address = \'%s\'', time(), troll_ip_address())); if (!empty($ban->ip_address)) { global $base_url; include_once('includes/common.inc'); watchdog('troll', t('IP Ban: !addr', array('!addr' => troll_ip_address)), WATCHDOG_NOTICE); $troll_ip_ban_redirect = variable_get('troll_ip_ban_redirect', ''); if (empty($troll_ip_ban_redirect)) { $page = drupal_get_path('module', 'troll') .'/blocked.html'; } else { $page = $troll_ip_ban_redirect; } header('Location: '. $base_url .'/'. $page); die(); } } } /** * Implementation of hook_help(). */ function troll_help($section = 'admin/help#troll') { switch ($section) { case 'admin/user/troll/ip_ban': if (!variable_get('troll_enable_ip_ban', 1)) { return theme('error', t('IP banning is currently disabled. You can enable it in the !settings page', array('!settings' => l(t('settings'), 'admin/user/troll/settings')))); } break; } } /** * Implementation of hook_settings(). * * @return array */ function troll_admin_settings() { $form['ip_settings'] = array( '#type' => 'fieldset', '#title' => 'IP Address Banning' ); $form['ip_settings']['troll_enable_ip_ban'] = array( '#type' => 'radios', '#title' => t('IP Address Banning'), '#default_value' => variable_get('troll_enable_ip_ban', 1), '#options' => array('1' => t('Enable banning by IP address'), '0' => t('Disable banning by IP address')) ); $form['ip_settings']['troll_ip_ban_redirect'] = array( '#type' => 'textfield', '#title' => t('IP Ban Relocation Page'), '#default_value' => variable_get('troll_ip_ban_redirect', ''), '#description' => t("Page for relocating users banned based on their IP address or domain name. If left blank, users will be redirected to blocked.html in the troll module's directory. Do not use a drupal path here! You will cause a loop since IP banning completely blocks all access to the site! Edit the blocked.html file, or redirect to http://localhost."), ); $roles = array(t(' -Select Role- ')) + user_roles(); $form['role_settings'] = array( '#type' => 'fieldset', '#title' => t('User Blocking') ); $form['role_settings']['troll_block_role'] = array( '#type' => 'select', '#title' => t('Troll Block Role'), '#default_value' => variable_get('troll_block_role', 0), '#options' => $roles, '#description' => t('Select the role to assign to users when blocking from the troll administration screens.'), ); return system_settings_form($form); } /** * Admin settings page validate handler. * * @see troll_admin_settings() */ function troll_admin_settings_validate($form_id, $form_values) { if ($form_values['troll_block_role'] == '0') { form_set_error('troll_block_role', t('You must choose a role to assign to users when blocking from the troll settings page.')); } } /** * Implementation of hook_perm(). */ function troll_perm() { return array('administer troll'); } /** * Implementation of hook_menu(). * * @return array */ function troll_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'admin/user/troll', 'title' => t('Troll'), 'description' => t('Manage visitor IP banning.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_search_form'), 'access' => user_access('administer troll'), 'weight' => 0, ); $items[] = array( 'path' => 'admin/user/troll/search', 'title' => t('Search Users'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_search_form'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'access' => user_access('administer troll'), 'weight' => 0 ); $items[] = array( 'path' => 'admin/user/troll/search/view', 'title' => t('Search Users'), 'callback' => 'troll_search_user_detail', 'access' => user_access('administer troll'), 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'admin/user/troll/search/block', 'title' => t('Block User'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_confirm_block_user_form'), 'access' => user_access('administer troll'), 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'admin/user/troll/ip_ban', 'title' => t('IP Banning'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_ip_ban'), 'access' => user_access('administer troll'), 'type' => MENU_LOCAL_TASK, 'weight' => 1 ); $items[] = array( 'path' => 'admin/user/troll/ip_ban/user', 'title' => t('IP Ban Form'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_confirm_ban_ip_form'), 'access' => user_access('administer troll'), 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'admin/user/troll/ip_ban/delete', 'title' => t('Remove Ban'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_confirm_delete_ip_form'), 'access' => user_access('administer troll'), 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'admin/user/troll/ip_blacklist', 'title' => t('Blacklists'), 'callback' => 'troll_blacklist_summary', 'type' => MENU_LOCAL_TASK, 'access' => user_access('administer troll'), 'weight' => 2 ); $items[] = array( 'path' => 'admin/user/troll/ip_blacklist/summary', 'title' => t('Summary'), 'callback' => 'troll_blacklist_summary', 'access' => user_access('administer troll'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => 0 ); $items[] = array( 'path' => 'admin/user/troll/ip_blacklist/punishment', 'title' => t('Visitor Punishment'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_blacklist_punishment_form'), 'access' => user_access('administer troll'), 'type' => MENU_LOCAL_TASK, 'weight' => 1 ); $items[] = array( 'path' => 'admin/user/troll/ip_blacklist/import', 'title' => t('Import Blacklist'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_blacklist_import_form'), 'access' => user_access('administer troll'), 'type' => MENU_LOCAL_TASK, 'weight' => 2 ); $items[] = array( 'path' => 'admin/user/troll/ip_blacklist/search', 'title' => t('Search Blacklisted IPs'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_blacklist_search_form'), 'access' => user_access('administer troll'), 'type' => MENU_LOCAL_TASK, 'weight' => 3 ); $items[] = array( 'path' => 'admin/user/troll/ip_blacklist/deleteblack', 'title' => t('Delete Blacklisted IPs'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_confirm_delete_black_block_form'), 'access' => user_access('administer troll'), 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'admin/user/troll/ip_blacklist/whitelist', 'title' => t('Whitelist'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_whitelist_form'), 'access' => user_access('administer troll'), 'type' => MENU_LOCAL_TASK, 'weight' => 4 ); $items[] = array( 'path' => 'admin/user/troll/settings', 'title' => t('Settings'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_admin_settings'), 'access' => user_access('administer site configuration'), 'type' => MENU_LOCAL_TASK, 'weight' => 5 ); } else { $items[] = array( 'path' => "admin/user/troll/ip_ban/edit/". arg(5), 'title' => t('IP Ban Form'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_ip_ban_form', arg(5)), 'access arguments' => user_access('administer troll'), 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'admin/user/troll/ip_blacklist/deletewhite/'. arg(5) .'/'. arg(6), 'title' => t('Delete Whitelisted IPs'), 'callback' => 'drupal_get_form', 'callback arguments' => array('troll_confirm_delete_white_block_form', arg(5), arg(6)), 'access' => user_access('administer troll'), 'type' => MENU_CALLBACK ); $items[] = array( 'path' => 'user/'. arg(1) .'/troll', 'title' => t('Troll Track'), 'callback' => 'troll_search_user_detail', 'callback arguments' => array(arg(1)), 'access' => user_access('administer troll'), 'type' => MENU_LOCAL_TASK ); } return $items; } /** * Menu callback: block a user and redirect to search page. * * @param $uid */ function troll_confirm_block_user_form($uid) { $account = user_load(array('uid' => $uid)); if (!$account) { drupal_goto('admin/user/troll'); } $form['uid'] = array( '#type' => 'value', '#value' => $uid, ); $form['#redirect'] = 'admin/user/troll'; return confirm_form($form, t('Block user %username?', array('%username' => $account->name)), 'admin/user/troll', t('Are you sure you want to block this user?')); } function troll_confirm_block_user_form_submit($form_id, $form_values /*$form, &$form_state*/) { troll_block_user($form_values['uid']); } /** * Menu callback: user IP banning. */ function troll_ip_ban() { $form['banfieldset'] = array( '#type' => 'fieldset', '#title' => t('Add IP Ban'), '#weight' => -1, '#collapsible' => TRUE, '#collapsed' => FALSE ); $form['banfieldset']['banform'] = troll_ip_ban_form(array()); $form['ipdisplay'] = array( '#type' => 'fieldset', '#title' => t('Banned IPs'), '#value' => troll_display_ip(), '#weight' => 0, '#collapsible' => TRUE, '#collapsed' => FALSE ); $form['#validate']['troll_ip_ban_form_validate'] = array(); return $form; } /** * Submit handler for IP Ban form. * * @see troll_ip_ban() * @see troll_ip_ban_form() */ function troll_ip_ban_submit($form_id, $form_values) { troll_insert_ip($form_values); } /** * IP ban confirmation form. */ function troll_confirm_ban_ip_form($uid) { $ip = db_fetch_object(db_query_range('SELECT ip_address FROM {troll_ip_track} WHERE uid = %d ORDER BY accessed DESC', $uid, 0, 1)); $form['ip_address'] = array( '#type' => 'value', '#value' => $ip->ip_address, ); $form['domain_name'] = array( '#type' => 'value', '#value' => gethostbyaddr($ip->ip_address), ); troll_ip_ban_expire_form($form, $ip); return confirm_form($form, t('Ban IP %ip?', array('%ip' => $ip->ip_address)), 'admin/user/troll/ip_ban', t('Are you sure you want to ban this IP?')); } /** * Submit handler for ban confirmation form. * * @see troll_confirm_ban_ip_form() */ function troll_confirm_ban_ip_form_submit($form_id, $form_values) { troll_insert_ip($form_values); drupal_goto('admin/user/troll'); } /** * Summary of how many IP blocks are filtered. * * @return string */ function troll_blacklist_summary() { $count = db_result(db_query('SELECT COUNT(net) FROM {troll_blacklist}')); return t('%d address blocks filtered.', array('%d' => $count)); } /** * Gives admins a choice of how to punish blacklisted visitors * * @return array */ function troll_blacklist_punishment_form() { $form['stutter'] = array( '#type' => 'checkbox', '#title' => t('Randomly stutter output'), '#default_value' => variable_get('troll_blacklist_stutter', 0), '#description' => t('While outputting content, the troll module will cause Drupal to "sleep" for random intervals of 1-5 seconds to delay output.') ); $form['mod_requests'] = array( '#type' => 'radios', '#title' => t('Page request modification'), '#options' => array(t('none'), 'silent_post_drop' => 'Silently drop form post submission data', 'notice_post_drop' => 'Drop form post submission data and give a notice'), '#default_value' => variable_get('troll_blacklist_mod_requests', '0'), '#description' => t('Modifies data sent by visitors from blacklisted IPs before Drupal even has a chance to process it.') ); $form['alt_page_output'] = array( '#type' => 'fieldset', '#title' => 'Alternate page output' ); $form['alt_page_output']['alt_page'] = array( '#type' => 'radios', '#title' => 'Alternate pages', '#options' => array(t('none'), 'blank' => t('Blank pages'), '404' => t('404 every page request'), 'redirect' => t('Redirect to alternate URL')), '#default_value' => variable_get('troll_blacklist_alt_page', '0'), '#description' => t('Sends alternate page output, whether the visitor hits a real URL or not.') ); $form['alt_page_output']['alt_url'] = array( '#type' => 'textfield', '#title' => t('Redirection URL'), '#default_value' => variable_get('troll_blacklist_alt_url', ''), '#description' => t('URL to redirect blacklisted visitors to if "Redirect to alternate URL" is selected. Start with the appropriate prefix (e.g. http://)') ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Update Blacklist Punishments'), '#weight' => 1 ); return $form; } /** * Submit handler for punishment form. * * @see troll_blacklist_punishment() */ function troll_blacklist_punishment_form_submit($form_id, $form_values) { variable_set('troll_blacklist_mod_requests', $form_values['mod_requests']); variable_set('troll_blacklist_stutter', $form_values['stutter']); variable_set('troll_blacklist_alt_page', $form_values['alt_page']); variable_set('troll_blacklist_alt_url', $form_values['alt_url']); drupal_set_message(t('The settings have been updated.')); } /** * Builds form array for admin to select how to import a new blacklist. * * @return array */ function troll_blacklist_import_form() { $form['truncate_list'] = array( '#type' => 'checkbox', '#title' => t('Truncate/delete existing blacklist before import') ); $options = array(); $options[0] = ''; if (function_exists('gzopen')) { $options['OCgz'] = 'OpenBSD mirror: okean.com China bzip archive'; $options['OKgz'] = 'OpenBSD mirror: okean.com Korea bzip archive'; } $options['OTtx'] = 'okean.com China and Korea text file'; $options['OCtx'] = 'okean.com China text file'; $options['OKtx'] = 'okean.com Korea text file'; $form['select_list'] = array( '#type' => 'select', '#title' => t('Download and import'), '#options' => $options, '#description' => t('Downloads supported blacklist from the internet. Please select a mirror when possible.'), ); $form['custom_list'] = array( '#type' => 'textfield', '#title' => t('List URL'), '#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.'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Import List'), '#weight' => 1, ); return $form; } /** * Submit handler for blacklist import. * * @see troll_blacklist_import() */ function troll_blacklist_import_form_submit($form_id, $form_values) { if (!empty($form_values['truncate_list'])) { if (db_query('TRUNCATE TABLE {troll_blacklist}')) { drupal_set_message(t('Blacklist table truncated.')); } } if (!empty($form_values['select_list'])) { troll_blacklist_import_list($form_values['select_list']); } if (!empty($form_values['custom_list'])) { troll_blacklist_import_url($form_values['custom_list']); } drupal_goto('admin/user/troll/ip_blacklist'); } /** * Decyphers what dropdown selection was chosen to send for parsing out IP blocks * * @see troll_blacklist_import_form() * @see troll_blacklist_parse_save() * @param array $edit */ function troll_blacklist_import_list($list) { $files = array( 'OCgz' => array('gz' => 'http://www.openbsd.org/spamd/chinacidr.txt.gz'), 'OKgz' => array('gz' => 'http://www.openbsd.org/spamd/koreacidr.txt.gz'), 'OTtx' => array('txt' => 'http://www.okean.com/sinokoreacidr.txt'), 'OCtx' => array('txt' => 'http://www.okean.com/chinacidr.txt'), 'OKtx' => array('txt' => 'http://www.okean.com/koreacidr.txt')); if (!isset($files[$list])) { drupal_set_message(t('Form input not valid')); return FALSE; } $file_type = array_keys($files[$list]); troll_blacklist_parse_save($files[$list][$file_type[0]], $file_type[0]); } /** * Parses a URL to send for parsing out IP blocks * * @see troll_blacklist_parse_save() * @param array $edit */ function troll_blacklist_import_url($edit) { $url_parts = parse_url($edit); $file_parts = pathinfo($url_parts['path']); troll_blacklist_parse_save($edit, $file_parts['extension']); } /** * Parses input file, line by line, searching for IP blocks in CIDR * format (x.x.x.x/x). Understands files in text, bzip, and bzip2 format, * where the PHP installation supports it. Writes what it finds to the * database as long integers. * * @todo should probably function_exists() the compression file functions * @param $file_name string * @param $file_type string */ 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); switch ($file_type) { case 'gz': $fp = gzopen($file_name, 'r'); break; default: $fp = fopen($file_name, 'r'); break; } if (is_resource($fp)) { $i = 0; while (!feof($fp)) { $buffer = fgets($fp); if ($buffer{0} != '#' && ($buffer{0} != '/' && $buffer{1} != '/') && !empty($buffer)) { preg_match("/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})/", $buffer, $matches); // $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 = %f AND bcast = %f', $net, $bcast); db_query('INSERT INTO {troll_blacklist} (net, bcast) VALUES (%f, %f)', $net, $bcast); $i++; } } switch ($file_type) { case 'gz': gzclose($fp); break; default: fclose($fp); break; } drupal_set_message(t('%i IP blocks imported', array('%i' => $i))); } else { drupal_set_message(t('Import failed! File could not be read.')); } } /** * Form to search blacklist to see if an IP matches * * @return array */ function troll_blacklist_search_form($form_values = array()) { $form['ip_address'] = array( '#type' => 'textfield', '#title' => t('IP Address'), '#size' => 15, '#maxlength' => 15, '#description' => t('Address to search for in the database of imported IP blocks.'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Search Blacklisted IPs'), '#weight' => 1 ); // If a value has been entered, display search results. if (!empty($form_values['ip_address'])) { $form['result'] = array( '#value' => troll_blacklist_search($form_values['ip_address']), ); } else { $form['result'] = array( '#value' => troll_blacklist_search(''), ); } $form['#multistep'] = TRUE; $form['#redirect'] = FALSE; return $form; } /** * Perform search to see if an IP address matches a blacklisted IP block. * * @return string */ function troll_blacklist_search($ip_address) { if (empty($ip_address)) { $sql = "SELECT net, bcast FROM {troll_blacklist}"; } else { $sql = "SELECT net, bcast FROM {troll_blacklist} WHERE net <= %f AND bcast >= %f"; $longip = _troll_longip($ip_address); } $headers = array( array('data' => t('Network Address'), 'field' => 'net', 'sort' => 'asc'), array('data' => t('Broadcast Address'), 'field' => 'bcast'), array('data' => t('Actions'), 'field' => 'delete') ); $sql .= tablesort_sql($headers); $result = pager_query($sql, 25, 0, NULL, $longip, $longip); while ($row = db_fetch_object($result)) { $printnet = long2ip($row->net); $printbcast = long2ip($row->bcast); $action = l(t('remove'), "admin/user/troll/ip_blacklist/deleteblack/{$row->net}/{$row->bcast}"); $rows[] = array($printnet, $printbcast, $action); } if (!empty($rows)) { $pager = theme('pager', NULL, 25, 0); if (!empty($pager)) { $rows[] = array(array('data' => $pager, 'colspan' => 3)); } return theme('table', $headers, $rows); } else { drupal_set_message(t('No matches found.')); } } /** * Form builder function * * @param $net string Not used, here for later implementation of whitelist editing * @param $bcast string Not used, here for later implementation of whitelist editing * @see troll_whitelist_form() * @return string */ function troll_whitelist($net = NULL, $bcast = NULL) { $sql = 'SELECT net, bcast FROM {troll_whitelist}'; $headers = array( array('data' => t('Start/Network Address'), 'field' => 'net', 'sort' => 'asc'), array('data' => t('End/Broadcast Address'), 'field' => 'bcast'), array('data' => t('Actions'), 'field' => 'delete') ); $sql .= tablesort_sql($headers); $result = pager_query($sql, 25); while ($row = db_fetch_object($result)) { $printnet = long2ip($row->net); $printbcast = long2ip($row->bcast); $action = l(t('remove'), "admin/user/troll/ip_blacklist/deletewhite/{$row->net}/{$row->bcast}"); $rows[] = array($printnet, $printbcast, $action); } $pager = theme('pager', NULL, 25, 0); if (!empty($pager)) { $rows[] = array(array('data' => $pager, 'colspan' => 3)); } return theme('table', $headers, $rows); } /** * Display form for creating new whitelist block and table of current whitelisted IPs. * * @return array */ function troll_whitelist_form($form_values = array()) { $form['whitelist_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.'), '#required' => TRUE ); $form['whitelist_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.'), '#required' => false ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Insert Whitelist IPs'), '#weight' => 1 ); $form['whitelist'] = array( '#value' => troll_whitelist(), ); return $form; } /** * Insert a new IP block into the whitelist * * @param array $edit */ function troll_whitelist_form_submit($form_id, $form_values) { $longip1 = _troll_longip($form_values['whitelist_addr1']); $longip2 = _troll_longip(empty($form_values['whitelist_addr2']) ? $form_values['whitelist_addr1'] : $form_values['whitelist_addr2']); if ($longip1 > $longip2) { $temp = $longip1; $longip1 = $longip2; $longip2 = $temp; unset($temp); } db_query("REPLACE INTO {troll_whitelist} (net, bcast) VALUES (%f, %f)", $longip1, $longip2); drupal_set_message(t('IP range %ip1 to %ip2 whitelisted.', array('%ip1' => long2ip($longip1), '%ip2' => long2ip($longip2)))); } /** * Convert dotted decimal IP to long integer and check for validity * * @param $ip string * @return integer */ function _troll_longip($ip) { $longip = ip2long($ip); if ($longip === FALSE || $longip == -1) { drupal_set_message(t('IP %ip not valid!', array('%ip' => $ip))); drupal_goto('admin/user/troll/ip_blacklist'); } return $longip; } /** * IP banning form. * * @param $iid int * @return array */ function troll_ip_ban_form($iid = NULL) { $form['submit'] = array( '#type' => 'submit', '#value' => (isset($iid)) ? t('Update Banned IP') : t('Ban IP'), '#weight' => 1, ); $ip = db_fetch_object(db_query('SELECT * FROM {troll_ip_ban} WHERE iid = %d', $iid)); $form['iid'] = array( '#type' => 'value', '#value' => isset($ip->iid) ? $ip->iid : '', ); $form['ip_address'] = array( '#type' => 'textfield', '#title' => t('IP Address'), '#default_value' => isset($ip->ip_address) ? $ip->ip_address : '', '#description' => t('The IP address to ban.'), '#required' => TRUE ); $form['domain_name'] = array( '#type' => 'textfield', '#title' => t('Domain Name'), '#default_value' => ($ip->domain_name ? $ip->domain_name : ($ip->ip_address ? gethostbyaddr($ip->ip_address) : '')), '#description' => t('The Domain Name of the IP address to ban - for reference only.') ); troll_ip_ban_expire_form($form, $ip); return $form; } /** * troll_ip_ban form validate callback function. * * @see troll_ip_ban_form() */ function troll_ip_ban_form_validate($form_id, $form_values) { if (!preg_match('([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})', $form_values['ip_address'])) { form_set_error('ip_address', t('Please include a valid IP address.')); } } /** * Submit handler for IP ban form. * * @see troll_ip_ban_form() */ function troll_ip_ban_form_submit($form_id, $form_values) { troll_update_ip($form_values); drupal_goto('admin/user/troll/ip_ban'); } /** * Helper function to return the expire form elements. */ function troll_ip_ban_expire_form(&$form, $ip) { $timestamp = ($ip->expires ? $ip->expires : time()); $date = getdate(gmmktime()); $curyear = $date['year']; $i = 0; while ($i < 10) { $years[$curyear + $i] = $curyear + $i; $i++; } $months = array(1 => t('January'), t('February'), t('March'), t('April'), t('May'), t('June'), t('July'), t('August'), t('September'), t('October'), t('November'), t('December')); for ($i = 1; $i <= 31; $i++) { $days[$i] = $i; } $form['timestamp'] = array( '#type' => 'fieldset', '#title' => t('Expires'), '#prefix' => '