/[drupal]/contributions/modules/troll/troll.module
ViewVC logotype

Diff of /contributions/modules/troll/troll.module

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

revision 1.26.2.11, Sun Sep 21 12:12:48 2008 UTC revision 1.26.2.11.2.1, Sun Mar 1 03:24:10 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: troll.module,v 1.26.2.10 2008/04/29 11:23:52 jaydub Exp $  // $Id: troll.module,v 1.26.2.11 2008/09/21 12:12:48 jaydub Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 13  Line 13 
13   */   */
14    
15  /**  /**
16   * Implementation of hook_init.   * Implementation of hook_init().
17   */   */
18  function troll_init() {  function troll_init() {
19    if (troll_is_blacklisted()) {    if (troll_is_blacklisted()) {
# Line 31  function troll_init() { Line 31  function troll_init() {
31            exit;            exit;
32            break;            break;
33          case 'redirect':          case 'redirect':
34            header('Location: '. variable_get('troll_blacklist_alt_url', ''));            header('Location: '. variable_get('troll_blacklist_alt_url', 'http://127.0.0.1'));
35            exit;            exit;
36            break;            break;
37        }        }
# Line 54  function troll_init() { Line 54  function troll_init() {
54    global $user;    global $user;
55    
56    if ($user->uid) {    if ($user->uid) {
57      $track = db_fetch_object(db_query("SELECT * FROM {troll_ip_track} WHERE uid = %d AND ip_address = '%s'", $user->uid, $_SERVER['REMOTE_ADDR']));      $track = db_fetch_object(db_query("SELECT * FROM {troll_ip_track} WHERE uid = %d AND ip_address = '%s'", $user->uid, troll_ip_address()));
58      if ($track->uid) {      if (!empty($track->uid)) {
59        // record for this IP exists, update accessed timestamp        // A record for this IP exists. Update accessed timestamp.
60        db_query("UPDATE {troll_ip_track} SET accessed = %d WHERE uid = %d AND ip_address = '%s'", time(), $user->uid, $_SERVER['REMOTE_ADDR']);        db_query("UPDATE {troll_ip_track} SET accessed = %d WHERE uid = %d AND ip_address = '%s'", time(), $user->uid, troll_ip_address());
61      }      }
62      else {      else {
63        // insert new IP record for user        // insert new IP record for user
64        db_query("INSERT INTO {troll_ip_track} (uid, ip_address, created, accessed) VALUES (%d, '%s', %d, %d)", $user->uid, $_SERVER['REMOTE_ADDR'], time(), time());        db_query("INSERT INTO {troll_ip_track} (uid, ip_address, created, accessed) VALUES (%d, '%s', %d, %d)", $user->uid, troll_ip_address(), time(), time());
65      }      }
66    }    }
67    
68    if (variable_get('troll_enable_ip_ban', FALSE)) {    if (variable_get('troll_enable_ip_ban', 1)) {
69      $ban = db_fetch_object(db_query('SELECT * FROM {troll_ip_ban} WHERE (expires > %d OR expires = 0) AND ip_address = \'%s\'', time(), $_SERVER['REMOTE_ADDR']));      $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()));
70      if ($ban->ip_address) {      if (!empty($ban->ip_address)) {
71        global $base_url;        global $base_url;
72        watchdog('troll', 'IP Ban: '. $_SERVER['REMOTE_ADDR'], WATCHDOG_NOTICE);        watchdog('troll', t('IP Ban: !addr', array('!addr' => troll_ip_address)), WATCHDOG_NOTICE);
73        $troll_ip_ban_redirect = variable_get('troll_ip_ban_redirect', '');        $troll_ip_ban_redirect = variable_get('troll_ip_ban_redirect', '');
74        $page = (empty($troll_ip_ban_redirect) ? drupal_get_path('module', 'troll') .'/blocked.html' : $troll_ip_ban_redirect);        if (empty($troll_ip_ban_redirect)) {
75        header('location: '. $base_url .'/'. $page);          include_once('includes/common.inc');
76            $page = drupal_get_path('module', 'troll') .'/blocked.html';
77          }
78          else {
79            $page = $troll_ip_ban_redirect;
80          }
81          header('Location: '. $base_url .'/'. $page);
82        die();        die();
83      }      }
84    }    }
85  }  }
86    
87  /**  /**
88   * Implementation of hook_help.   * Implementation of hook_help().
  *  
  * @param $section string  
89   */   */
90  function troll_help($section = 'admin/help#legal') {  function troll_help($section = 'admin/help#troll') {
91    switch ($section) {    switch ($section) {
92      case 'admin/settings/troll/ip_ban':      case 'admin/settings/troll/ip_ban':
93        if (!variable_get('troll_enable_ip_ban', FALSE)) {        if (!variable_get('troll_enable_ip_ban', 1)) {
94          return theme('error', t('IP banning is currently disabled, you can enable it in the !settings page', array('!settings' => l(t('settings'), 'admin/settings/troll/settings'))));          return theme('error', t('IP banning is currently disabled. You can enable it in the !settings page', array('!settings' => l(t('settings'), 'admin/settings/troll/settings'))));
95        }        }
96        break;        break;
97    }    }
# Line 127  function troll_admin_settings() { Line 131  function troll_admin_settings() {
131      '#title' => t('Troll Block Role'),      '#title' => t('Troll Block Role'),
132      '#default_value' => variable_get('troll_block_role', 0),      '#default_value' => variable_get('troll_block_role', 0),
133      '#options' => $roles,      '#options' => $roles,
134      '#description' => t('Select the role to set users to when blocking from the troll adminstration screens'),      '#description' => t('Select the role to assign to users when blocking from the troll administration screens.'),
135    );    );
136    
137    return system_settings_form($form);    return system_settings_form($form);
138  }  }
139    
140  /**  /**
141   * admin settings page validate handler   * Admin settings page validate handler.
142     *
143     * @see troll_admin_settings()
144   */   */
145  function troll_admin_settings_validate($form_id, $form_values) {  function troll_admin_settings_validate($form_id, $form_values) {
146    if ($form_values['troll_block_role'] == '0') {    if ($form_values['troll_block_role'] == '0') {
147      form_set_error('troll_block_role', t('You must choose a role to set users to when blocking from the troll settings page.'));      form_set_error('troll_block_role', t('You must choose a role to assign to users when blocking from the troll settings page.'));
148    }    }
149  }  }
150    
# Line 157  function troll_perm() { Line 163  function troll_perm() {
163  function troll_menu($may_cache) {  function troll_menu($may_cache) {
164    $items = array();    $items = array();
165    
   $access = user_access('administer troll');  
166    if ($may_cache) {    if ($may_cache) {
167      $items[] = array(      $items[] = array(
168        'path' => 'admin/settings/troll',        'path' => 'admin/settings/troll',
169        'title' => t('Troll'),        'title' => t('Troll'),
170        'description' => t('Manage visitor IP banning.'),        'description' => t('Manage visitor IP banning.'),
171        'callback' => 'troll_search_users',        'callback' => 'drupal_get_form',
172        'access' => $access,        'callback arguments' => array('troll_search_form'),
173          'access' => user_access('administer troll'),
174        'weight' => 0,        'weight' => 0,
175      );      );
176      $items[] = array(      $items[] = array(
177        'path' => 'admin/settings/troll/search',        'path' => 'admin/settings/troll/search',
178        'title' => t('Search Users'),        'title' => t('Search Users'),
179        'callback' => 'troll_search_users',        'callback' => 'drupal_get_form',
180          'callback arguments' => array('troll_search_form'),
181        'type' => MENU_DEFAULT_LOCAL_TASK,        'type' => MENU_DEFAULT_LOCAL_TASK,
182        'access' => $access,        'access' => user_access('administer troll'),
183        'weight' => 0        'weight' => 0
184      );      );
185      $items[] = array(      $items[] = array(
186          'path' => 'admin/settings/troll/search/view',
187          'title' => t('Search Users'),
188          'callback' => 'troll_search_user_detail',
189          'access' => user_access('administer troll'),
190          'type' => MENU_CALLBACK
191        );
192        $items[] = array(
193          'path' => 'admin/settings/troll/search/block',
194          'title' => t('Block User'),
195          'callback' => 'drupal_get_form',
196          'callback arguments' => array('troll_confirm_block_user_form'),
197          'access' => user_access('administer troll'),
198          'type' => MENU_CALLBACK
199        );
200        $items[] = array(
201        'path' => 'admin/settings/troll/ip_ban',        'path' => 'admin/settings/troll/ip_ban',
202        'title' => t('IP Banning'),        'title' => t('IP Banning'),
203        'callback' => 'troll_ip_ban',        'callback' => 'drupal_get_form',
204          'callback arguments' => array('troll_ip_ban'),
205          'access' => user_access('administer troll'),
206        'type' => MENU_LOCAL_TASK,        'type' => MENU_LOCAL_TASK,
       'access' => $access,  
207        'weight' => 1        'weight' => 1
208      );      );
209      $items[] = array(      $items[] = array(
210          'path' => 'admin/settings/troll/ip_ban/edit',
211          'title' => t('IP Ban Form'),
212          'page callback' => 'drupal_get_form',
213          'page arguments' => array('troll_ip_ban_form'),
214          'access arguments' => user_access('administer troll'),
215          'type' => MENU_CALLBACK
216        );
217        $items[] = array(
218          'path' => 'admin/settings/troll/ip_ban/user',
219          'title' => t('IP Ban Form'),
220          'callback' => 'drupal_get_form',
221          'callback arguments' => array('troll_confirm_ban_ip_form'),
222          'access' => user_access('administer troll'),
223          'type' => MENU_CALLBACK
224        );
225        $items[] = array(
226          'path' => 'admin/settings/troll/ip_ban/delete',
227          'title' => t('Remove Ban'),
228          'callback' => 'drupal_get_form',
229          'callback arguments' => array('troll_confirm_delete_ip_form'),
230          'access' => user_access('administer troll'),
231          'type' => MENU_CALLBACK
232        );
233        $items[] = array(
234        'path' => 'admin/settings/troll/ip_blacklist',        'path' => 'admin/settings/troll/ip_blacklist',
235        'title' => t('Blacklists'),        'title' => t('Blacklists'),
236        'callback' => 'troll_blacklist',        'callback' => 'troll_blacklist_summary',
237        'type' => MENU_LOCAL_TASK,        'type' => MENU_LOCAL_TASK,
238        'access' => $access,        'access' => user_access('administer troll'),
239        'weight' => 2        'weight' => 2
240      );      );
241      $items[] = array(      $items[] = array(
242        'path' => 'admin/settings/troll/settings',        'path' => 'admin/settings/troll/ip_blacklist/summary',
243        'title' => t('Settings'),        'title' => t('Summary'),
244          'callback' => 'troll_blacklist_summary',
245          'access' => array('administer troll'),
246          'type' => MENU_DEFAULT_LOCAL_TASK,
247          'weight' => 0
248        );
249        $items[] = array(
250          'path' => 'admin/settings/troll/ip_blacklist/punishment',
251          'title' => t('Visitor Punishment'),
252        'callback' => 'drupal_get_form',        'callback' => 'drupal_get_form',
253        'callback arguments' => array('troll_admin_settings'),        'callback arguments' => array('troll_blacklist_punishment_form'),
254          'access' => user_access('administer troll'),
255          'type' => MENU_LOCAL_TASK,
256          'weight' => 1
257        );
258        $items[] = array(
259          'path' => 'admin/settings/troll/ip_blacklist/import',
260          'title' => t('Import Blacklist'),
261          'callback' => 'drupal_get_form',
262          'callback arguments' => array('troll_blacklist_import_form'),
263          'access' => user_access('administer troll'),
264          'type' => MENU_LOCAL_TASK,
265          'weight' => 2
266        );
267        $items[] = array(
268          'path' => 'admin/settings/troll/ip_blacklist/search',
269          'title' => t('Search Blacklisted IPs'),
270          'callback' => 'drupal_get_form',
271          'callback arguments' => array('troll_blacklist_search_form'),
272          'access' => user_access('administer troll'),
273        'type' => MENU_LOCAL_TASK,        'type' => MENU_LOCAL_TASK,
       'access' => user_access('administer site configuration'),  
274        'weight' => 3        'weight' => 3
275      );      );
276      $items[] = array(      $items[] = array(
277        'path' => 'admin/settings/troll/ip_ban/edit',        'path' => 'admin/settings/troll/ip_blacklist/deleteblack',
278        'title' => t('IP Ban Form'),        'title' => t('Delete Blacklisted IPs'),
279          'callback' => 'drupal_get_form',
280          'callback arguments' => array('troll_confirm_delete_black_block_form'),
281          'access' => user_access('administer troll'),
282          'type' => MENU_CALLBACK
283        );
284        $items[] = array(
285          'path' => 'admin/settings/troll/ip_blacklist/whitelist',
286          'title' => t('Whitelist'),
287          'callback' => 'drupal_get_form',
288          'callback arguments' => array('troll_whitelist_form'),
289          'access' => user_access('administer troll'),
290          'type' => MENU_LOCAL_TASK,
291          'weight' => 4
292        );
293        $items['admin/settings/troll/ip_blacklist/deletewhite'] = array(
294          'title' => t('Delete Whitelisted IPs'),
295          'page callback' => 'drupal_get_form',
296          'page arguments' => array('troll_confirm_delete_white_block_form'),
297          'access arguments' => user_access('administer troll'),
298          'type' => MENU_CALLBACK
299        );
300        $items[] = array(
301          'path' => 'admin/settings/troll/settings',
302          'title' => t('Settings'),
303        'callback' => 'drupal_get_form',        'callback' => 'drupal_get_form',
304        'callback arguments' => 'troll_ip_ban_form',        'callback arguments' => array('troll_admin_settings'),
305        'type' => MENU_CALLBACK,        'access' => user_access('administer site configuration'),
306        'access' => $access        'type' => MENU_LOCAL_TASK,
307          'weight' => 5
308      );      );
309    }    }
310    return $items;    return $items;
311  }  }
312    
313  /**  /**
314   *   * Menu callback: user IP banning.
  * MENU CALLBACKS  
  *  
 **/  
   
 /**  
  * User IP banning page callback function.  
315   */   */
316  function troll_ip_ban($op = NULL, $iid = NULL) {  function troll_ip_ban() {
317    $op   = $_POST['op'] ? $_POST['op'] : $op;    $form['banfieldset'] = array(
318    $edit = $_POST ? $_POST : $edit;      '#type' => 'fieldset',
319        '#title' => t('Add IP Ban'),
320    switch ($op) {      '#weight' => -1,
321      case 'user':      '#collapsible' => true
322        $ip = db_fetch_object(db_query('SELECT ip_address FROM {troll_ip_track} WHERE uid = %d ORDER BY accessed DESC', $iid));    );
323        $edit['ip_address'] = $ip->ip_address;    $form['banfieldset']['banform'] = troll_ip_ban_form(array());
324        troll_insert_ip($edit);    $form['#validate'] = array('troll_ip_ban_form_validate');
325        drupal_goto('admin/settings/troll/ip_ban');    $form['ipdisplay'] = array(
326        break;      '#type' => 'fieldset',
327      case t('Ban IP'):      '#title' => t('Banned IPs'),
328        troll_insert_ip($edit);      '#value' => troll_display_ip(),
329        drupal_goto('admin/settings/troll/ip_ban');      '#weight' => 0,
330        break;      '#collapsible' => true
331      case t('Update Banned IP'):    );
332        troll_update_ip($edit);    return $form;
       drupal_goto('admin/settings/troll/ip_ban');  
       break;  
     case 'delete':  
       $output = troll_confirm_delete_ip($iid);  
       break;  
     case t('Confirm'):  
       troll_remove_ip($iid);  
       drupal_goto('admin/settings/troll/ip_ban');  
       break;  
     case t('Cancel'):  
       drupal_goto('admin/settings/troll/ip_ban');  
       break;  
     default:  
       $form['banform'] = array(  
         '#type' => 'fieldset',  
         '#title' => t('Add IP Ban'),  
         '#value' => drupal_get_form('troll_ip_ban_form'),  
         '#weight' => -1,  
         '#collapsible' => true  
       );  
       $form['ipdisplay'] = array(  
         '#type' => 'fieldset',  
         '#title' => t('Banned IPs'),  
         '#value' => troll_display_ip(),  
         '#weight' => 0,  
         '#collapsible' => true  
       );  
       $output = drupal_render($form);  
       break;  
   }  
   return $output;  
333  }  }
334    
335  /**  /**
336   * Central function to display Blacklists tab and fire   * Submit handler for IP Ban form.
  * off functions for Blacklist-related form submissions  
337   *   *
338   * @param string $op   * @see troll_ip_ban()
339   * @param array $edit   * @see troll_ip_ban_form()
  * @return string  
340   */   */
341  function troll_blacklist($op = NULL, $edit = NULL) {  function troll_ip_ban_submit($form_id, $form_values) {
342    $op = $_POST['op'] ? $_POST['op'] : $op;    troll_insert_ip($form_values);
   
   switch ($op) {  
     case t('Update Blacklist Punishments'):  
       variable_set('troll_blacklist_mod_requests', $_POST['mod_requests']);  
       variable_set('troll_blacklist_stutter', $_POST['stutter']);  
       variable_set('troll_blacklist_alt_page', $_POST['alt_page']);  
       variable_set('troll_blacklist_alt_url', $_POST['alt_url']);  
       drupal_goto('admin/settings/troll/ip_blacklist');  
       break;  
     case t('Import List'):  
       if (!empty($_POST['truncate_list'])) {  
         if (db_query('TRUNCATE TABLE {troll_blacklist}')) {  
           drupal_set_message(t('Blacklist table truncated.'));  
         }  
       }  
       if (!empty($_POST['select_list'])) {  
         troll_blacklist_import_list($_POST['select_list']);  
       }  
       if (!empty($_POST['custom_list'])) {  
         troll_blacklist_import_url($_POST['custom_list']);  
       }  
       drupal_goto('admin/settings/troll/ip_blacklist');  
       break;  
     case t('Search Blacklisted IPs'):  
       return troll_blacklist_search($_POST['ip_address']);  
       break;  
     case t('Insert Whitelist IPs'):  
       troll_whitelist_insert_ips($_POST['whitelist_addr1'], $_POST['whitelist_addr2']);  
       drupal_goto('admin/settings/troll/ip_blacklist');  
       break;  
     case t('Update Whitelist IPs'):  
       troll_whitelist_insert_ips($_POST['whitelist_addr1'], $_POST['whitelist_addr2']);  
       drupal_goto('admin/settings/troll/ip_blacklist');  
       break;  
     case 'deletewhite':  
       $output = troll_confirm_delete_white_block(arg(5), arg(6));  
       break;  
     case t('Confirm Whitelist Removal'):  
       troll_remove_whitelist($_POST['net'], $_POST['bcast']);  
       drupal_goto('admin/settings/troll/ip_blacklist');  
       break;  
     case 'deleteblack':  
       $output = troll_confirm_delete_black_block(arg(5), arg(6));  
       break;  
     case t('Confirm Blacklist Removal'):  
       troll_remove_blacklist($_POST['net'], $_POST['bcast']);  
       drupal_goto('admin/settings/troll/ip_blacklist');  
       break;  
     default:  
       $form['blacklist_info'] = array(  
         '#type' => 'fieldset',  
         '#title' => t('Blacklist Summary'),  
         '#value' => troll_blacklist_summary(),  
         '#weight' => -4,  
         '#collapsible' => true,  
         '#collapsed' => false  
       );  
       $form['blacklist_punishment'] = array(  
         '#type' => 'fieldset',  
         '#title' => t('Blacklist Visitor Punishment'),  
         '#value' => troll_blacklist_punishment(),  
         '#weight' => -3,  
         '#collapsible' => true,  
         '#collapsed' => true  
       );  
       $form['blacklist_import'] = array(  
         '#type' => 'fieldset',  
         '#title' => t('Import Blacklist'),  
         '#value' => troll_blacklist_import(),  
         '#weight' => -2,  
         '#collapsible' => true,  
         '#collapsed' => true  
       );  
       $form['search_blacklist'] = array(  
         '#type' => 'fieldset',  
         '#title' => t('Search Blacklisted IPs'),  
         '#value' => troll_blacklist_search_blacklist(),  
         '#weight' => 0,  
         '#collapsible' => true,  
         '#collapsed' => true  
       );  
       $form['whitelist_form'] = array(  
         '#type' => 'fieldset',  
         '#title' => t('Whitelist IPs'),  
         '#value' => troll_whitelist(),  
         '#description' => t('Whitelisted IPs override the blacklist. Does not apply to the IP Ban feature.'),  
         '#weight' => 1,  
         '#collapsible' => true,  
         '#collapsed' => true  
       );  
       $output = drupal_render($form);  
       break;  
   }  
   return $output;  
343  }  }
344    
345    
346  /**  /**
347   * Summary of how many IP blocks are filtered   * Summary of how many IP blocks are filtered.
348   *   *
349   * @return string   * @return string
350   */   */
# Line 388  function troll_blacklist_summary() { Line 354  function troll_blacklist_summary() {
354  }  }
355    
356  /**  /**
  * Form builder function  
  *  
  * @uses troll_blacklist_punishment_form()  
  * @return string  
  */  
 function troll_blacklist_punishment() {  
   return drupal_get_form('troll_blacklist_punishment_form');  
 }  
   
 /**  
357   * Gives admins a choice of how to punish blacklisted visitors   * Gives admins a choice of how to punish blacklisted visitors
358   *   *
359   * @return array   * @return array
# Line 442  function troll_blacklist_punishment_form Line 398  function troll_blacklist_punishment_form
398  }  }
399    
400  /**  /**
  * Form builder function  
  *  
  * @uses troll_blacklist_import_form()  
  * @return string  
  */  
 function troll_blacklist_import() {  
   return drupal_get_form('troll_blacklist_import_form');  
 }  
   
 /**  
401   * Builds form array for admin to select how to import a new blacklist.   * Builds form array for admin to select how to import a new blacklist.
402   *   *
403   * @return array   * @return array
# Line 464  function troll_blacklist_import_form() { Line 410  function troll_blacklist_import_form() {
410    $options = array();    $options = array();
411    $options[0] = '';    $options[0] = '';
412    if (function_exists('gzopen')) {    if (function_exists('gzopen')) {
     $options['S1gz'] = 'OpenBSD mirror: SPEWS.org Level 1 bzip archive';  
     $options['S2gz'] = 'OpenBSD mirror: SPEWS.org Level 2 bzip archive';  
413      $options['OCgz'] = 'OpenBSD mirror: okean.com China bzip archive';      $options['OCgz'] = 'OpenBSD mirror: okean.com China bzip archive';
414      $options['OKgz'] = 'OpenBSD mirror: okean.com Korea bzip archive';      $options['OKgz'] = 'OpenBSD mirror: okean.com Korea bzip archive';
415    }    }
   $options['S1tx'] = 'SPEWS.org Level 1 text file';  
   $options['S2tx'] = 'SPEWS.org Level 2 text file';  
416    $options['OTtx'] = 'okean.com China and Korea text file';    $options['OTtx'] = 'okean.com China and Korea text file';
417    $options['OCtx'] = 'okean.com China text file';    $options['OCtx'] = 'okean.com China text file';
418    $options['OKtx'] = 'okean.com Korea text file';    $options['OKtx'] = 'okean.com Korea text file';
# Line 501  function troll_blacklist_import_form() { Line 443  function troll_blacklist_import_form() {
443   * @param array $edit   * @param array $edit
444   */   */
445  function troll_blacklist_import_list($list) {  function troll_blacklist_import_list($list) {
446    $files = array('S1gz' => array('gz' => 'http://www.openbsd.org/spamd/spews_list_level1.txt.gz'),    $files = array(
     'S2gz' => array('gz' => 'http://www.openbsd.org/spamd/spews_list_level2.txt.gz'),  
447      'OCgz' => array('gz' => 'http://www.openbsd.org/spamd/chinacidr.txt.gz'),      'OCgz' => array('gz' => 'http://www.openbsd.org/spamd/chinacidr.txt.gz'),
448      'OKgz' => array('gz' => 'http://www.openbsd.org/spamd/koreacidr.txt.gz'),      'OKgz' => array('gz' => 'http://www.openbsd.org/spamd/koreacidr.txt.gz'),
     'S1tx' => array('txt' => 'http://www.spews.org/spews_list_level1.txt'),  
     'S2tx' => array('txt' => 'http://www.spews.org/spews_list_level2.txt'),  
449      'OTtx' => array('txt' => 'http://www.okean.com/sinokoreacidr.txt'),      'OTtx' => array('txt' => 'http://www.okean.com/sinokoreacidr.txt'),
450      'OCtx' => array('txt' => 'http://www.okean.com/chinacidr.txt'),      'OCtx' => array('txt' => 'http://www.okean.com/chinacidr.txt'),
451      'OKtx' => array('txt' => 'http://www.okean.com/koreacidr.txt'));      'OKtx' => array('txt' => 'http://www.okean.com/koreacidr.txt'));
452    
453    if (!isset($files[$list])) {    if (!isset($files[$list])) {
454      drupal_set_message(t('Form input not valid'));      drupal_set_message(t('Form input not valid'));
455      return false;      return FALSE;
456    }    }
457    
458    $file_type = array_keys($files[$list]);    $file_type = array_keys($files[$list]);
# Line 599  function troll_blacklist_parse_save($fil Line 538  function troll_blacklist_parse_save($fil
538  }  }
539    
540  /**  /**
  * Form builder function  
  *  
  * @uses troll_blacklist_search_blacklist_form()  
  * @return string  
  */  
 function troll_blacklist_search_blacklist() {  
   return drupal_get_form('troll_blacklist_search_blacklist_form');  
 }  
   
 /**  
541   * Form to search blacklist to see if an IP matches   * Form to search blacklist to see if an IP matches
542   *   *
543   * @return array   * @return array
544   */   */
545  function troll_blacklist_search_blacklist_form() {  function troll_blacklist_search_form($form_values = array()) {
546    $form['ip_address'] = array(    $form['ip_address'] = array(
547      '#type' => 'textfield',      '#type' => 'textfield',
548      '#title' => t('IP Address'),      '#title' => t('IP Address'),
549      '#size' => 15,      '#size' => 15,
550      '#maxlength' => 15,      '#maxlength' => 15,
551      '#description' => t('Address to search for in the database of imported IP blocks.'),      '#description' => t('Address to search for in the database of imported IP blocks.'),
     '#required' => TRUE  
552      );      );
553    $form['submit'] = array(    $form['submit'] = array(
554      '#type' => 'submit',      '#type' => 'submit',
555      '#value' => t('Search Blacklisted IPs'),      '#value' => t('Search Blacklisted IPs'),
556      '#weight' => 1      '#weight' => 1
557    );    );
558      // If a value has been entered, display search results.
559      if (!empty($form_values['ip_address'])) {
560        $form['result'] = array(
561          '#value' => troll_blacklist_search($form_values['ip_address']),
562        );
563      }
564      else {
565        $form['result'] = array(
566          '#value' => troll_blacklist_search(''),
567        );
568      }
569    return $form;    return $form;
570  }  }
571    
572  /**  /**
573   * Perform search to see if an IP address matches a blacklisted IP block.   * Perform search to see if an IP address matches a blacklisted IP block.
  * If no results are found, get redirected to the blacklist management  
  * page instead of seeing results.  
574   *   *
  * @param $edit array  
575   * @return string   * @return string
576   */   */
577  function troll_blacklist_search($ip_address) {  function troll_blacklist_search($ip_address) {
578    $longip = _troll_longip($ip_address);    if (empty($ip_address)) {
579    $sql = "SELECT net, bcast FROM {troll_blacklist} WHERE net <= %d AND bcast >= %d";      $sql = "SELECT net, bcast FROM {troll_blacklist}";
580      }
581      else {
582        $sql = "SELECT net, bcast FROM {troll_blacklist} WHERE net <= %d AND bcast >= %d";
583        $longip = _troll_longip($ip_address);
584      }
585    $headers = array(    $headers = array(
586      array('data' => t('Network Address'), 'field' => 'net', 'sort' => 'asc'),      array('data' => t('Network Address'), 'field' => 'net', 'sort' => 'asc'),
587      array('data' => t('Broadcast Address'), 'field' => 'bcast'),      array('data' => t('Broadcast Address'), 'field' => 'bcast'),
# Line 650  function troll_blacklist_search($ip_addr Line 590  function troll_blacklist_search($ip_addr
590    
591    $sql .= tablesort_sql($headers);    $sql .= tablesort_sql($headers);
592    $result = pager_query($sql, 25, 0, NULL, $longip, $longip);    $result = pager_query($sql, 25, 0, NULL, $longip, $longip);
   if (db_num_rows($result) == 0) {  
     drupal_set_message(t('No matches found in blacklist search.'));  
     drupal_goto('admin/settings/troll/ip_blacklist');  
   }  
593    while ($row = db_fetch_object($result)) {    while ($row = db_fetch_object($result)) {
594      $printnet = long2ip($row->net);      $printnet = long2ip($row->net);
595      $printbcast = long2ip($row->bcast);      $printbcast = long2ip($row->bcast);
596      $action = l(t('remove'), "admin/settings/troll/ip_blacklist/deleteblack/{$row->net}/{$row->bcast}");      $action = l(t('remove'), "admin/settings/troll/ip_blacklist/deleteblack/{$row->net}/{$row->bcast}");
597      $rows[] = array($printnet, $printbcast, $action);      $rows[] = array($printnet, $printbcast, $action);
598    }    }
599      if (!empty($rows)) {
600    $pager = theme('pager', NULL, 25, 0);      $pager = theme('pager', NULL, 25, 0);
601    if (!empty($pager)) {      if (!empty($pager)) {
602      $rows[] = array(array('data' => $pager, 'colspan' => 3));        $rows[] = array(array('data' => $pager, 'colspan' => 3));
603        }
604        return theme('table', $headers, $rows);
605      }
606      else {
607        drupal_set_message(t('No matches found.'));
608    }    }
   
   return theme('table', $headers, $rows);  
609  }  }
610    
611  /**  /**
# Line 674  function troll_blacklist_search($ip_addr Line 613  function troll_blacklist_search($ip_addr
613   *   *
614   * @param $net string Not used, here for later implementation of whitelist editing   * @param $net string Not used, here for later implementation of whitelist editing
615   * @param $bcast string Not used, here for later implementation of whitelist editing   * @param $bcast string Not used, here for later implementation of whitelist editing
616   * @uses troll_whitelist_form()   * @see troll_whitelist_form()
617   * @return string   * @return string
618   */   */
619  function troll_whitelist($net = NULL, $bcast = NULL) {  function troll_whitelist($net = NULL, $bcast = NULL) {
   $output = drupal_get_form('troll_whitelist_form', $net, $bcast);  
620    $sql = 'SELECT net, bcast FROM {troll_whitelist}';    $sql = 'SELECT net, bcast FROM {troll_whitelist}';
621    
622    $headers = array(    $headers = array(
# Line 689  function troll_whitelist($net = NULL, $b Line 627  function troll_whitelist($net = NULL, $b
627    
628    $sql .= tablesort_sql($headers);    $sql .= tablesort_sql($headers);
629    $result = pager_query($sql, 25);    $result = pager_query($sql, 25);
630    while($row = db_fetch_object($result)) {    while ($row = db_fetch_object($result)) {
631      $printnet = long2ip($row->net);      $printnet = long2ip($row->net);
632      $printbcast = long2ip($row->bcast);      $printbcast = long2ip($row->bcast);
633      $action = l(t('remove'), "admin/settings/troll/ip_blacklist/deletewhite/{$row->net}/{$row->bcast}");      $action = l(t('remove'), "admin/settings/troll/ip_blacklist/deletewhite/{$row->net}/{$row->bcast}");
# Line 701  function troll_whitelist($net = NULL, $b Line 639  function troll_whitelist($net = NULL, $b
639      $rows[] = array(array('data' => $pager, 'colspan' => 3));      $rows[] = array(array('data' => $pager, 'colspan' => 3));
640    }    }
641    
642    return $output . theme('table', $headers, $rows);    return theme('table', $headers, $rows);
643  }  }
644    
645  /**  /**
646   * Display form for creating new whitelist block and table of current whitelisted IPs   * Display form for creating new whitelist block and table of current whitelisted IPs.
647   *   *
648   * @return array   * @return array
649   */   */
650  function troll_whitelist_form($net, $bcast) {  function troll_whitelist_form($form_values = array()) {
651    $form['whitelist_addr1'] = array(    $form['whitelist_addr1'] = array(
652      '#type' => 'textfield',      '#type' => 'textfield',
653      '#title' => t('Starting IP Address'),      '#title' => t('Starting IP Address'),
# Line 730  function troll_whitelist_form($net, $bca Line 668  function troll_whitelist_form($net, $bca
668    );    );
669    $form['submit'] = array(    $form['submit'] = array(
670      '#type' => 'submit',      '#type' => 'submit',
671      '#value' => ($net && $bcast) ? t('Update Whitelist IPs') : t('Insert Whitelist IPs'),      '#value' => t('Insert Whitelist IPs'),
672      '#weight' => 1      '#weight' => 1
673    );    );
674    $form['#action'] = url('admin/settings/troll/ip_blacklist');    $form['whitelist'] = array(
675        '#value' => troll_whitelist(),
676      );
677    return $form;    return $form;
678  }  }
679    
# Line 743  function troll_whitelist_form($net, $bca Line 682  function troll_whitelist_form($net, $bca
682   *   *
683   * @param array $edit   * @param array $edit
684   */   */
685  function troll_whitelist_insert_ips($whitelist_addr1, $whitelist_addr2) {  function troll_whitelist_form_submit($form_id, $form_values) {
686    $longip1 = _troll_longip($whitelist_addr1);    $longip1 = _troll_longip($form_values['whitelist_addr1']);
687    $longip2 = _troll_longip(empty($whitelist_addr2) ? $whitelist_addr1 : $whitelist_addr2);    $longip2 = _troll_longip(empty($form_values['whitelist_addr2']) ? $form_values['whitelist_addr1'] : $form_values['whitelist_addr2']);
688    if ($longip1 > $longip2) {    if ($longip1 > $longip2) {
689      $temp = $longip1;      $temp = $longip1;
690      $longip1 = $longip2;      $longip1 = $longip2;
# Line 764  function troll_whitelist_insert_ips($whi Line 703  function troll_whitelist_insert_ips($whi
703   */   */
704  function _troll_longip($ip) {  function _troll_longip($ip) {
705    $longip = ip2long($ip);    $longip = ip2long($ip);
706    if ($longip === false || $longip == -1) {    if ($longip === FALSE || $longip == -1) {
707      drupal_set_message(t('IP %ip not valid!', array('%ip' => $ip)));      drupal_set_message(t('IP %ip not valid!', array('%ip' => $ip)));
708      drupal_goto('admin/settings/troll/ip_blacklist');      drupal_goto('admin/settings/troll/ip_blacklist');
709    }    }
# Line 772  function _troll_longip($ip) { Line 711  function _troll_longip($ip) {
711  }  }
712    
713  /**  /**
  * User search page callback function.  
  *  
  * @param $op array  
  * @param $uid  
  * @return string  
  */  
 function troll_search_users($op = NULL, $uid = NULL) {  
   $form_values = $_POST;  
   
   $output = '';  
   switch ($op) {  
     case 'view':  
       $output = troll_search_user_detail($uid);  
       break;  
     case 'block':  
       troll_block_user($uid);  
       drupal_goto('admin/settings/troll/search');  
       break;  
     default:  
       $output = troll_user_search();  
       $output .= troll_list_users($form_values);  
       break;  
   }  
   return $output;  
 }  
   
 /**  
  *  
  * FORM FUNCTIONS  
  *  
 **/  
   
 /**  
714   * IP banning form.   * IP banning form.
715   *   *
716   * @param $iid int   * @param $iid int
# Line 813  function troll_search_users($op = NULL, Line 719  function troll_search_users($op = NULL,
719  function troll_ip_ban_form($iid = NULL) {  function troll_ip_ban_form($iid = NULL) {
720    $form['submit'] = array(    $form['submit'] = array(
721      '#type' => 'submit',      '#type' => 'submit',
722      '#value' => ($iid) ? t('Update Banned IP') : t('Ban IP'),      '#value' => (isset($iid)) ? t('Update Banned IP') : t('Ban IP'),
723      '#weight' => 1,      '#weight' => 1,
724    );    );
725    
726    $ip = db_fetch_object(db_query('SELECT * FROM {troll_ip_ban} WHERE iid = %d', $iid));    $ip = db_fetch_object(db_query('SELECT * FROM {troll_ip_ban} WHERE iid = %d', $iid));
727    $form['iid'] = array(    $form['iid'] = array(
728      '#type' => 'hidden',      '#type' => 'value',
729      '#value' => $ip->iid,      '#value' => $ip->iid,
730    );    );
731    $form['ip_address'] = array(    $form['ip_address'] = array(
# Line 836  function troll_ip_ban_form($iid = NULL) Line 742  function troll_ip_ban_form($iid = NULL)
742      '#description' => t('The Domain Name of the IP address to ban - for reference only.')      '#description' => t('The Domain Name of the IP address to ban - for reference only.')
743    );    );
744    
745      troll_ip_ban_expire_form($form, $ip);
746    
747      return $form;
748    }
749    
750    /**
751     * Helper function to return the expire form elements.
752     */
753    function troll_ip_ban_expire_form(&$form, $ip) {
754    $timestamp = ($ip->expires ? $ip->expires : time());    $timestamp = ($ip->expires ? $ip->expires : time());
755    
756    $date = getdate( gmmktime() );    $date = getdate(gmmktime());
757    $curyear = $date['year'];    $curyear = $date['year'];
758      $i = 0;
759    while ($i < 10) {    while ($i < 10) {
760      $years[$curyear+$i] = $curyear+$i;      $years[$curyear + $i] = $curyear + $i;
761      $i++;      $i++;
762    }    }
763    $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'));    $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'));
# Line 875  function troll_ip_ban_form($iid = NULL) Line 791  function troll_ip_ban_form($iid = NULL)
791      '#default_value' => date('Y', $timestamp),      '#default_value' => date('Y', $timestamp),
792      '#options' => $years      '#options' => $years
793    );    );
   
   $form['#action'] = url('admin/settings/troll/ip_ban');  
   return $form;  
794  }  }
795    
796  /**  /**
797   * troll_ip_ban form validate callback function.   * troll_ip_ban form validate callback function.
798   *   *
799   * @param $form_id   * @see troll_ip_ban_form()
  * @param $edit array  
800   */   */
801  function troll_ip_ban_validate($form_id, $edit) {  function troll_ip_ban_form_validate($form_id, $form_values) {
802    if (!preg_match('([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})', $edit['ip_address'])) {    if (!preg_match('([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})', $form_values['ip_address'])) {
803      form_set_error('ip_address', t('Please include an valid IP address'));      form_set_error('ip_address', t('Please include a valid IP address.'));
804    }    }
805  }  }
806    
# Line 911  function troll_display_ip() { Line 823  function troll_display_ip() {
823    while ($row = db_fetch_object($result)) {    while ($row = db_fetch_object($result)) {
824      $thisip = l($row->ip_address, 'admin/settings/troll/ip_ban/edit/'. $row->iid);      $thisip = l($row->ip_address, 'admin/settings/troll/ip_ban/edit/'. $row->iid);
825      $thisdom = l(($row->domain_name ? $row->domain_name : gethostbyname($row->ip_address)), 'admin/settings/troll/ip_ban/edit/'. $row->iid);      $thisdom = l(($row->domain_name ? $row->domain_name : gethostbyname($row->ip_address)), 'admin/settings/troll/ip_ban/edit/'. $row->iid);
826      $expires = ($row->expires ? date('M d, Y', $row->expires) : t('never'));      $expires = $row->expires ? date('M d, Y', $row->expires) : t('never');
827      $action = l(t('remove'), 'admin/settings/troll/ip_ban/delete/'. $row->iid);      $action = l(t('remove'), 'admin/settings/troll/ip_ban/delete/'. $row->iid);
828      $rows[] = array($thisip, $thisdom, $expires, $action);      $rows[] = array($thisip, $thisdom, $expires, $action);
829    }    }
# Line 925  function troll_display_ip() { Line 837  function troll_display_ip() {
837  }  }
838    
839  /**  /**
  * IP ban delete confirmation.  
  */  
 function troll_confirm_delete_ip($iid) {  
   return drupal_get_form('troll_confirm_delete_ip_form', $iid);  
 }  
   
 /**  
840   * IP ban delete confirmation form.   * IP ban delete confirmation form.
841   */   */
842  function troll_confirm_delete_ip_form($iid) {  function troll_confirm_delete_ip_form($iid) {
843    $ip = db_fetch_object(db_query('SELECT * FROM {troll_ip_ban} WHERE iid = %d', $iid));    $ip = db_fetch_object(db_query('SELECT ip_address FROM {troll_ip_ban} WHERE iid = %d', $iid));
844    return confirm_form(array(), t('Remove Ban for IP %ip?', array('%ip' => $ip->ip_address)), 'admin/settings/troll/ip_ban'. $edit['field_id'], t('Are you sure you want to remove the ban on this IP?'));    $form['iid'] = array(
845        '#type' => 'value',
846        '#value' => $iid,
847      );
848      return confirm_form($form, t('Remove Ban for IP %ip?', array('%ip' => $ip->ip_address)), 'admin/settings/troll/ip_ban', t('Are you sure you want to remove the ban on this IP?'));
849  }  }
850    
851  /**  /**
852   * Confirmation before deleting a blacklist IP block   * Submit handler for delete confirmation form.
853   *   *
854   * @param integer $net IP in long format   * @see troll_confirm_delete_ip_form()
  * @param integer $bcast IP in long format  
  * @return string  
855   */   */
856  function troll_confirm_delete_black_block($net, $bcast) {  function troll_confirm_delete_ip_form_submit($form_id, $form_values) {
857    $result = db_result(db_query('SELECT COUNT(net) FROM {troll_blacklist} WHERE net = %d AND bcast = %d', $net, $bcast));    troll_remove_ip($form_values['iid']);
858    if (empty($result)) {    drupal_goto('admin/settings/troll/ip_ban');
     drupal_set_message(t('No such IP range found in the database.'));  
     drupal_goto('admin/settings/troll/ip_blacklist');  
   }  
   return drupal_get_form('troll_confirm_delete_black_block_form', $net, $bcast);  
859  }  }
860    
861  /**  /**
# Line 962  function troll_confirm_delete_black_bloc Line 865  function troll_confirm_delete_black_bloc
865   * @param integer $bcast IP in long format   * @param integer $bcast IP in long format
866   */   */
867  function troll_confirm_delete_black_block_form($net, $bcast) {  function troll_confirm_delete_black_block_form($net, $bcast) {
868    return confirm_form(array(    $result = db_result(db_query('SELECT COUNT(net) FROM {troll_blacklist} WHERE net = %d AND bcast = %d', $net, $bcast));
869      'net' => array(    if (empty($result)) {
870        '#type' => 'hidden',      drupal_set_message(t('No such IP range found in the database.'));
871        '#value' => $net),      drupal_goto('admin/settings/troll/ip_blacklist/search');
872        'bcast' => array(    }
873          '#type' => 'hidden',    $form['net'] = array(
874          '#value' => $bcast)      '#type' => 'value',
875        ),      '#value' => $net
876        t('Remove listing for IP block %ip1 to %ip2?', array('%ip1' => long2ip($net), '%ip2' => long2ip($bcast))    );
877      ),    $form['bcast'] = array(
878      'admin/settings/troll/ip_blacklist',      '#type' => 'value',
879        '#value' => $bcast
880      );
881      return confirm_form(
882        $form,
883        t('Remove listing for IP block %ip1 to %ip2?', array('%ip1' => long2ip($net), '%ip2' => long2ip($bcast))),
884        'admin/settings/troll/ip_blacklist/search',
885      t('Are you sure you want to remove this IP block from the blacklist?'),      t('Are you sure you want to remove this IP block from the blacklist?'),
886      t('Confirm Blacklist Removal')      t('Confirm Blacklist Removal')
887    );    );
888  }  }
889    
890  /**  /**
891   * Confirmation before deleting a whitelist IP block   * Submit handler for deletion of IP from blacklist.
892   *   *
893   * @param integer $net IP in long format   * @see troll_confirm_delete_black_block_form()
  * @param integer $bcast IP in long format  
  * @return string  
894   */   */
895  function troll_confirm_delete_white_block($net, $bcast) {  function troll_confirm_delete_black_block_form_submit($form_id, $form_values) {
896    $result = db_result(db_query('SELECT COUNT(net) FROM {troll_whitelist} WHERE net = %d AND bcast = %d', $net, $bcast));    troll_remove_blacklist($form_values['net'], $form_values['bcast']);
897    if (empty($result)) {    drupal_goto('admin/settings/troll/ip_blacklist/search');
     drupal_set_message(t('No such IP range found in the database.'));  
     drupal_goto('admin/settings/troll/ip_blacklist');  
   }  
   return drupal_get_form('troll_confirm_delete_white_block_form', $net, $bcast);  
898  }  }
899    
900  /**  /**
# Line 1000  function troll_confirm_delete_white_bloc Line 903  function troll_confirm_delete_white_bloc
903   * @param integer $net IP in long format   * @param integer $net IP in long format
904   * @param integer $bcast IP in long format   * @param integer $bcast IP in long format
905   */   */
906  function troll_confirm_delete_white_block_form($net, $bcast) {  function troll_confirm_delete_white_block_form($form_values, $net, $bcast) {
907    return confirm_form(array(    $result = db_result(db_query('SELECT COUNT(net) FROM {troll_whitelist} WHERE net = %d AND bcast = %d', $net, $bcast));
908      'net' => array(    if (empty($result)) {
909        '#type' => 'hidden',      drupal_set_message(t('No such IP range found in the database.'));
910        '#value' => $net),      drupal_goto('admin/settings/troll/ip_blacklist/whitelist');
911        'bcast' => array(    }
912          '#type' => 'hidden',    $form['net'] = array(
913          '#value' => $bcast)      '#type' => 'value',
914      ),      '#value' => $net
915      );
916      $form['bcast'] = array(
917        '#type' => 'value',
918        '#value' => $bcast
919      );
920    
921      return confirm_form(
922        $form,
923      t('Remove listing for IP block %ip1 to %ip2?', array('%ip1' => long2ip($net), '%ip2' => long2ip($bcast))),      t('Remove listing for IP block %ip1 to %ip2?', array('%ip1' => long2ip($net), '%ip2' => long2ip($bcast))),
924      'admin/settings/troll/ip_blacklist',      'admin/settings/troll/ip_blacklist/whitelist',
925      t('Are you sure you want to remove this IP block from the whitelist?'),      t('Are you sure you want to remove this IP block from the whitelist?'),
926      t('Confirm Whitelist Removal')      t('Confirm Whitelist Removal')
927    );    );
928  }  }
929    
930  /**  /**
931   * Form builder function   * Submit handler for deletion of IP from whitelist.
932   *   *
933   * @uses troll_search_form()   * @see troll_confirm_delete_white_block_form()
  * @return sring  
934   */   */
935  function troll_user_search() {  function troll_confirm_delete_white_block_form_submit($form_id, $form_values) {
936    return drupal_get_form('troll_search_form');    troll_remove_whitelist($form_values['net'], $form_values['bcast']);
937      drupal_goto('admin/settings/troll/ip_blacklist/whitelist');
938  }  }
939    
940  /**  /**
# Line 1032  function troll_user_search() { Line 943  function troll_user_search() {
943   * @return array   * @return array
944   */   */
945  function troll_search_form() {  function troll_search_form() {
   $form['#action'] = url('admin/settings/troll/search');  
   $form['#redirect'] = FALSE;  
946    $form['search'] = array(    $form['search'] = array(
947      '#type' => 'fieldset',      '#type' => 'fieldset',
948      '#title' => t('Search Users'),      '#title' => t('Search Users'),
949      '#collapsible' => true      '#collapsible' => TRUE
950    );    );
951    $form['search']['username'] = array(    $form['search']['username'] = array(
952      '#type' => 'textfield',      '#type' => 'textfield',
# Line 1074  function troll_list_users($edit) { Line 983  function troll_list_users($edit) {
983    
984    $where[] = 'u.uid <> 0';    $where[] = 'u.uid <> 0';
985    
986    if (strlen($edit['username']) > 0) {    if (!empty($edit['username'])) {
987      $where[] = "LOWER(u.name) LIKE '%%%s%%' ";      $where[] = "LOWER(u.name) LIKE '%%%s%%' ";
988      $args[] = drupal_strtolower($edit['username']);      $args[] = drupal_strtolower($edit['username']);
989    }    }
990    if (strlen($edit['mail']) > 0) {    if (!empty($edit['mail'])) {
991      $where[] = "LOWER(u.mail) LIKE '%%%s%%' ";      $where[] = "LOWER(u.mail) LIKE '%%%s%%' ";
992      $args[] = drupal_strtolower($edit['mail']);      $args[] = drupal_strtolower($edit['mail']);
993    }    }
994    if (strlen($edit['ip_address']) > 0) {    if (!empty($edit['ip_address'])) {
995      $where[] = "LOWER(t.ip_address) LIKE '%%%s%%' ";      $where[] = "LOWER(t.ip_address) LIKE '%%%s%%' ";
996      $args[] = drupal_strtolower($edit['ip_address']);      $args[] = drupal_strtolower($edit['ip_address']);
997    }    }
998    if (strlen($edit['date_created']) > 0) {    if (!empty($edit['date_created'])) {
999      $where[] = "u.created > %d ";      $where[] = "u.created > %d ";
1000      $args[] = drupal_strtotime($edit['date_created']);      $args[] = drupal_strtotime($edit['date_created']);
1001    }    }
# Line 1111  function troll_list_users($edit) { Line 1020  function troll_list_users($edit) {
1020    $count = 'SELECT COUNT(*) FROM {users} u LEFT JOIN {troll_ip_track} t ON u.uid = t.uid WHERE '. implode(' AND ', $where) .' AND u.uid <> 0';    $count = 'SELECT COUNT(*) FROM {users} u LEFT JOIN {troll_ip_track} t ON u.uid = t.uid WHERE '. implode(' AND ', $where) .' AND u.uid <> 0';
1021    $result = pager_query($sql, 25, 0, $count, $args);    $result = pager_query($sql, 25, 0, $count, $args);
1022    
1023    while($user = db_fetch_object($result)) {    while ($user = db_fetch_object($result)) {
1024      $name = l($user->name, 'admin/settings/troll/search/view/'. $user->uid, array('title' => t('View detailed user information')));      $name = l($user->name, 'admin/settings/troll/search/view/'. $user->uid, array('title' => t('View detailed user information')));
1025      $email = $user->mail;      $email = $user->mail;
1026      $status = ($user->status ? t('Active') : t('Blocked'));      $status = ($user->status ? t('Active') : t('Blocked'));
# Line 1127  function troll_list_users($edit) { Line 1036  function troll_list_users($edit) {
1036        $actions[] = array('title' => t('Block User'), 'href' => 'admin/settings/troll/search/block/'. $user->uid);        $actions[] = array('title' => t('Block User'), 'href' => 'admin/settings/troll/search/block/'. $user->uid);
1037      }      }
1038      else {      else {
1039        $actions[] = array('title' => t('Setup Block Role'), 'href' => 'admin/settings/troll/settings');        $actions[] = array('title' => t('Set up Block Role'), 'href' => 'admin/settings/troll/settings');
1040      }      }
1041      if ($user->ip_address) {      if ($user->ip_address) {
1042        $actions[] = array('title' => t('Ban IP'), 'href' => 'admin/settings/troll/ip_ban/user/'. $user->uid);        $actions[] = array('title' => t('Ban IP'), 'href' => 'admin/settings/troll/ip_ban/user/'. $user->uid);
# Line 1148  function troll_list_users($edit) { Line 1057  function troll_list_users($edit) {
1057  /**  /**
1058   * User detail form.   * User detail form.
1059   *   *
1060     * This is not an actual form; the form API is just being used for easy formatting.
1061     *
1062   * @param $uid int   * @param $uid int
1063   * @return string   * @return string
1064   */   */
# Line 1155  function troll_search_user_detail($uid) Line 1066  function troll_search_user_detail($uid)
1066    $u = user_load(array('uid' => $uid));    $u = user_load(array('uid' => $uid));
1067    $u->ip = db_fetch_object(db_query('SELECT t.ip_address FROM {troll_ip_track} t WHERE t.uid = %d GROUP BY t.uid, t.ip_address', $uid));    $u->ip = db_fetch_object(db_query('SELECT t.ip_address FROM {troll_ip_track} t WHERE t.uid = %d GROUP BY t.uid, t.ip_address', $uid));
1068    
   $roles = $u->roles;  
   
1069    $form['details'] = array(    $form['details'] = array(
1070      '#type' => 'fieldset',      '#type' => 'fieldset',
1071      '#title' => t('Account Details for %username', array('%username' => $u->name))      '#title' => t('Account Details for %username', array('%username' => $u->name))
# Line 1185  function troll_search_user_detail($uid) Line 1094  function troll_search_user_detail($uid)
1094    $form['details'][] = array(    $form['details'][] = array(
1095      '#type' => 'item',      '#type' => 'item',
1096      '#title' => t('Last Access'),      '#title' => t('Last Access'),
1097      '#value' => format_date($u->changed, 'long')      '#value' => format_date($u->access, 'long')
1098    );    );
1099    $form['details'][] = array(    $form['details'][] = array(
1100      '#type' => 'item',      '#type' => 'item',
# Line 1214  function troll_search_user_detail($uid) Line 1123  function troll_search_user_detail($uid)
1123    
1124    $content = drupal_render($form);    $content = drupal_render($form);
1125    
1126    // get IP history    // Get IP history.
1127    $results = db_query('SELECT * FROM {troll_ip_track} WHERE uid = %d ORDER BY created DESC', $uid);    $results = db_query('SELECT * FROM {troll_ip_track} WHERE uid = %d ORDER BY created DESC', $uid);
1128    while ($ip = db_fetch_object($results)) {    while ($ip = db_fetch_object($results)) {
1129      $banned = db_fetch_object(db_query('SELECT * FROM {troll_ip_ban} WHERE ip_address = \'%s\'', $ip->ip_address));      $banned = db_fetch_object(db_query('SELECT * FROM {troll_ip_ban} WHERE ip_address = \'%s\'', $ip->ip_address));
# Line 1227  function troll_search_user_detail($uid) Line 1136  function troll_search_user_detail($uid)
1136        }        }
1137      }      }
1138      else {      else {
1139        $status = l(t('not banned'), 'admin/settings/troll/ip_ban/ip/'. $ip->ip_address, array('title' => t('Ban this IP')));        $status = l(t('not banned'), 'admin/settings/troll/ip_ban/user/'. $uid, array('title' => t('Ban this IP')));
1140      }      }
1141      $rows[] = array($ip->ip_address, $status, format_date($ip->accessed, 'long'), format_date($ip->created, 'long'), exec('host '. $ip->ip_address));      $rows[] = array($ip->ip_address, $status, format_date($ip->accessed, 'long'), format_date($ip->created, 'long'), exec('host '. $ip->ip_address));
1142    }    }
# Line 1238  function troll_search_user_detail($uid) Line 1147  function troll_search_user_detail($uid)
1147    }    }
1148    $content .= theme('box', t('IP History'), ($posts ? $posts : t('No ip history')));    $content .= theme('box', t('IP History'), ($posts ? $posts : t('No ip history')));
1149    
1150    // get recent posts    // Get recent posts.
1151    $rdat = db_query("SELECT * FROM {node} WHERE uid = %d ORDER BY created DESC LIMIT 5", $uid);    $rdat = db_query_range("SELECT * FROM {node} WHERE uid = %d ORDER BY created DESC", $uid, 0, 5);
   
1152    $posts = NULL;    $posts = NULL;
1153    $rows = array();    $rows = array();
1154    while ($node = db_fetch_object($rdat)) {    while ($node = db_fetch_object($rdat)) {
1155      $rows[] = array(l($node->title, "node/{$node->nid}") .' '. theme('mark', node_mark($node->nid, $node->changed)), $node->type, date('M d, Y', $node->created), ($node->status ? t('published') : t('not published')));      $rows[] = array(l($node->title, "node/$node->nid") .' '. theme('mark', node_mark($node->nid, $node->changed)), $node->type, date('M d, Y', $node->created), ($node->status ? t('published') : t('not published')));
1156    }    }
1157    if ($rows) {    if ($rows) {
1158      $pheader = array(t('Title'), t('Type'), t('Created'), t('Status'));      $pheader = array(t('Title'), t('Type'), t('Created'), t('Status'));
# Line 1253  function troll_search_user_detail($uid) Line 1161  function troll_search_user_detail($uid)
1161    
1162    $content .= theme('box', t('Recent Posts'), ($posts ? $posts : t('No recent posts')));    $content .= theme('box', t('Recent Posts'), ($posts ? $posts : t('No recent posts')));
1163    
1164    // Get recent comments    // Get recent comments.
1165    $cp = 'SELECT * FROM {comments} WHERE uid = %d ORDER BY timestamp DESC';    $cp = 'SELECT * FROM {comments} WHERE uid = %d ORDER BY timestamp DESC';
1166    $cdat = db_query($cp, $uid);    $cdat = db_query($cp, $uid);
1167    
# Line 1273  function troll_search_user_detail($uid) Line 1181  function troll_search_user_detail($uid)
1181  }  }
1182    
1183  /**  /**
  *  
  * DATABASE FUNCTIONS  
  *  
 **/  
   
 /**  
1184   * Removes an IP ban from the database.   * Removes an IP ban from the database.
1185   */   */
1186  function troll_remove_ip($iid) {  function troll_remove_ip($iid) {
1187    if (db_query('DELETE FROM {troll_ip_ban} WHERE iid = %d', $iid)) {    if (db_query('DELETE FROM {troll_ip_ban} WHERE iid = %d', $iid)) {
1188      drupal_set_message(t('IP ban removed'));      drupal_set_message(t('IP ban removed.'));
1189    }    }
1190    else {    else {
1191      drupal_set_message(t('An error occurred, IP ban not removed'));      drupal_set_message(t('An error occurred, IP ban not removed.'));
1192    }    }
1193  }  }
1194    
# Line 1297  function troll_remove_ip($iid) { Line 1199  function troll_remove_ip($iid) {
1199   */   */
1200  function troll_remove_blacklist($net, $bcast) {  function troll_remove_blacklist($net, $bcast) {
1201    if (db_query('DELETE FROM {troll_blacklist} WHERE net = %d AND bcast = %d', $net, $bcast)) {    if (db_query('DELETE FROM {troll_blacklist} WHERE net = %d AND bcast = %d', $net, $bcast)) {
1202      drupal_set_message(t('Blacklist block removed'));      drupal_set_message(t('Blacklist block removed.'));
1203    }    }
1204    else {    else {
1205      drupal_set_message(t('An error occurred, blacklist block not removed'));      drupal_set_message(t('An error occurred, blacklist block not removed.'));
1206    }    }
1207  }  }
1208    
1209  /**  /**
1210   * Removes IP block from the whitelist   * Removes IP block from the whitelist.
1211   *   *
1212   * @param $edit array   * @param $edit array
1213   */   */
1214  function troll_remove_whitelist($net, $bcast) {  function troll_remove_whitelist($net, $bcast) {
1215    if (db_query('DELETE FROM {troll_whitelist} WHERE net = %d AND bcast = %d', $net, $bcast)) {    if (db_query('DELETE FROM {troll_whitelist} WHERE net = %d AND bcast = %d', $net, $bcast)) {
1216      drupal_set_message(t('IP whitelist removed'));      drupal_set_message(t('IP whitelist removed.'));
1217    }    }
1218    else {    else {
1219      drupal_set_message(t('An error occurred, IP whitelist not removed'));      drupal_set_message(t('An error occurred, IP whitelist not removed.'));
1220    }    }
1221  }  }
1222    
# Line 1326  function troll_remove_whitelist($net, $b Line 1228  function troll_remove_whitelist($net, $b
1228  function troll_insert_ip($edit) {  function troll_insert_ip($edit) {
1229    global $user;    global $user;
1230    
1231    $expires = ($edit['expires'] ? mktime(23, 59, 0, $edit['month'], $edit['day'], $edit['year']) : 0);    $expires = ($edit['expires'] == 1) ? mktime(23, 59, 0, $edit['month'], $edit['day'], $edit['year']) : 0;
1232    
1233    db_query("DELETE FROM {troll_ip_ban} WHERE ip_address = '%s'", $edit['ip_address']);    db_query("DELETE FROM {troll_ip_ban} WHERE ip_address = '%s'", $edit['ip_address']);
1234    if (db_query("INSERT INTO {troll_ip_ban} (ip_address, domain_name, created, expires, uid) VALUES ('%s', '%s', %d, %d, %d)", $edit['ip_address'], $edit['domain_name'], time(), $expires, $user->uid)) {    if (db_query("INSERT INTO {troll_ip_ban} (ip_address, domain_name, created, expires, uid) VALUES ('%s', '%s', %d, %d, %d)", $edit['ip_address'], $edit['domain_name'], time(), $expires, $user->uid)) {
1235      drupal_set_message(t('IP ban added: %ip', array('%ip' => $edit['ip_address'])));      drupal_set_message(t('IP ban added: %ip', array('%ip' => $edit['ip_address'])));
1236    }    }
1237    else {    else {
1238      drupal_set_message(t('An error occurred, IP ban not created'));      drupal_set_message(t('An error occurred. IP ban not created.'));
1239    }    }
1240    drupal_goto('admin/settings/troll/ip_ban');    drupal_goto('admin/settings/troll/ip_ban');
1241  }  }
# Line 1346  function troll_insert_ip($edit) { Line 1248  function troll_insert_ip($edit) {
1248  function troll_update_ip($edit) {  function troll_update_ip($edit) {
1249    global $user;    global $user;
1250    
1251    $expires = ($edit['expires'] ? mktime(23, 59, 0, $edit['month'], $edit['day'], $edit['year']) : 0);    $expires = ($edit['expires'] == 1) ? mktime(23, 59, 0, $edit['month'], $edit['day'], $edit['year']) : 0;
1252    
1253    if (db_query("UPDATE {troll_ip_ban} SET ip_address = '%s', domain_name = '%s', expires = %d, uid = %d WHERE iid = %d", $edit['ip_address'], $edit['domain_name'], $expires, $user->uid, $edit['iid'])) {    if (db_query("UPDATE {troll_ip_ban} SET ip_address = '%s', domain_name = '%s', expires = %d, uid = %d WHERE iid = %d", $edit['ip_address'], $edit['domain_name'], $expires, $user->uid, $edit['iid'])) {
1254      drupal_set_message(t('IP ban updated: %ip', array('%ip' => $edit['ip_address'])));      drupal_set_message(t('IP ban updated: %ip', array('%ip' => $edit['ip_address'])));
1255    }    }
1256    else {    else {
1257      drupal_set_message(t('An error occurred, IP ban not updated'));      drupal_set_message(t('An error occurred. IP ban not updated.'));
1258    }    }
1259  }  }
1260    
# Line 1360  function troll_update_ip($edit) { Line 1262  function troll_update_ip($edit) {
1262  /**  /**
1263   * Logs IP information for users in the database.   * Logs IP information for users in the database.
1264   */   */
1265  function troll_check_ip(){  function troll_check_ip() {
1266    global $user;    global $user;
1267    $sql = "SELECT uid FROM {troll_ip_track} WHERE ip_address = '%s' AND uid = %d";    $sql = "SELECT uid FROM {troll_ip_track} WHERE ip_address = '%s' AND uid = %d";
1268    $chk = db_query($sql, $_SERVER['REMOTE_ADDR'], $user->uid);    $chk = db_query($sql, troll_ip_address, $user->uid);
1269    $check = db_fetch_array($chk);    $check = db_fetch_array($chk);
1270    if (!isset($check['uid'])){    if (!isset($check['uid'])) {
1271      $isql = "INSERT INTO {troll_ip_track} (uid, ip_address, created) VALUES (%d, '%s', %d)";      $isql = "INSERT INTO {troll_ip_track} (uid, ip_address, created) VALUES (%d, '%s', %d)";
1272      db_query($isql, $user->uid, $_SERVER['REMOTE_ADDR'], time());      db_query($isql, $user->uid, troll_ip_address, time());
1273      }
1274    }
1275    
1276    /**
1277     * Copy of the ip_address() function in Drupal 6.
1278     */
1279    function troll_ip_address() {
1280      static $ip_address = NULL;
1281    
1282      if (!isset($ip_address)) {
1283        $ip_address = $_SERVER['REMOTE_ADDR'];
1284        if (variable_get('reverse_proxy', 0) && array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
1285          // If an array of known reverse proxy IPs is provided, then trust
1286          // the XFF header if request really comes from one of them.
1287          $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
1288          if (!empty($reverse_proxy_addresses) && in_array($ip_address, $reverse_proxy_addresses, TRUE)) {
1289            // If there are several arguments, we need to check the most
1290            // recently added one, i.e. the last one.
1291            $ip_address = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
1292          }
1293        }
1294    }    }
1295    
1296      return $ip_address;
1297  }  }
1298    
1299  /**  /**
# Line 1378  function troll_check_ip(){ Line 1303  function troll_check_ip(){
1303   * @return integer zero if whitelisted or not blacklisted, otherwise the number of IP blacklist block matches   * @return integer zero if whitelisted or not blacklisted, otherwise the number of IP blacklist block matches
1304   */   */
1305  function troll_is_blacklisted() {  function troll_is_blacklisted() {
1306    $longip = ip2long($_SERVER['REMOTE_ADDR']);    $longip = ip2long(troll_ip_address());
1307    if ($longip === FALSE || $longip == -1) {    if ($longip === FALSE || $longip == -1) {
1308      return FALSE;      return FALSE;
1309    }    }
# Line 1395  function troll_is_blacklisted() { Line 1320  function troll_is_blacklisted() {
1320   * @param $uid int   * @param $uid int
1321   */   */
1322  function troll_block_user($uid) {  function troll_block_user($uid) {
1323      $name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $uid));
1324    db_query('UPDATE {users} SET status = 0 WHERE uid = %d', $uid);    db_query('UPDATE {users} SET status = 0 WHERE uid = %d', $uid);
1325    db_query('DELETE FROM {users_roles} WHERE uid = %d', $uid);    db_query('DELETE FROM {users_roles} WHERE uid = %d', $uid);
1326    sess_destroy_uid($uid);    sess_destroy_uid($uid);
1327    if (variable_get('troll_block_role', NULL)) {    if (variable_get('troll_block_role', NULL)) {
1328        $role = db_result(db_query("SELECT name from {role} WHERE rid = %d", variable_get('troll_block_role', 0)));
1329      db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $uid, variable_get('troll_block_role', '0'));      db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $uid, variable_get('troll_block_role', '0'));
1330        drupal_set_message(t('Blocked user !link and assigned role %role.', array('!link' => l($name, "admin/settings/troll/search/view/$uid"), '%role' => $role)));
1331      }
1332      else {
1333        drupal_set_message(t('Blocked user !link.', array('!link' => l($name, "admin/settings/troll/search/view/$uid"))));
1334    }    }
   drupal_set_message(t('Blocked user !link', array('!link' => l($uid, "admin/settings/troll/search/view/$uid"))));  
1335  }  }
1336    
1337  /**  /**

Legend:
Removed from v.1.26.2.11  
changed lines
  Added in v.1.26.2.11.2.1

  ViewVC Help
Powered by ViewVC 1.1.2