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

Diff of /contributions/modules/badbehavior/badbehavior.module

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

revision 1.10, Thu Apr 13 15:49:30 2006 UTC revision 1.11, Sun Apr 6 16:49:15 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  /*  define('BB2_CWD', dirname(__FILE__));
 Module Name: Bad Behavior  
 Version: 1.2.2  
 Plugin URI: http://www.ioerror.us/software/bad-behavior/  
 Description: Stop comment spam before it starts by trapping and blocking spambots before they have a chance to post comments.  
 Author: Michael Hampton (Drupal module by David Angier)  
 Author URI: http://www.ioerror.us/  
 License: GPL  
   
 Bad Behavior - detects and blocks unwanted Web accesses  
 Copyright (C) 2005 Michael Hampton  
   
 This program is free software; you can redistribute it and/or modify  
 it under the terms of the GNU General Public License as published by  
 the Free Software Foundation; either version 2 of the License, or  
 (at your option) any later version.  
   
 This program is distributed in the hope that it will be useful,  
 but WITHOUT ANY WARRANTY; without even the implied warranty of  
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
 GNU General Public License for more details.  
   
 You should have received a copy of the GNU General Public License  
 along with this program; if not, write to the Free Software  
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
   
 This is beta software so please report any problems to  
 badbots AT ioerror DOT us  
 */  
4    
5    /**
6     * Implementation of hook_help().
7     */
8  function badbehavior_help($section='') {  function badbehavior_help($section='') {
9    $output = '';    $output = '';
10    switch ($section) {    switch ($section) {
# Line 43  function badbehavior_help($section='') { Line 18  function badbehavior_help($section='') {
18    return $output;    return $output;
19  }  }
20    
21  function badbehavior_db_errortrap($errno, $string) {  /**
22  }    * Implementation of hook_menu().
23      */
24  function badbehavior_menu($may_cache) {  function badbehavior_menu($may_cache) {
25    $items = array();    $items = array();
26    
27    if ($may_cache) {    if ($may_cache) {
28      $items[] = array('path' => 'admin/logs/badbehavior', 'title' => t('bad behavior'),      $items[] = array(
29        'callback' => 'badbehavior_overview', 'access' => user_access('administer bad behavior'));                          'path' => 'admin/settings/badbehavior',
30      $items[] = array('path' => 'admin/logs/badbehavior/event', 'title' => t('details'),                          'title' => t('Bad behavior'),
31        'callback' => 'badbehavior_event', 'access' => user_access('administer bad behavior'),        'description' => t('Configure automatic spam blocking for your site.'),
32          'callback' => 'drupal_get_form',
33                            'callback arguments' => array('badbehavior_settings'),
34                            'access' => user_access('administer bad behavior')
35                    );
36        $items[] = array(
37                            'path' => 'admin/logs/badbehavior',
38                            'title' => t('Bad behavior'),
39          'description' => t('Examine the spam blocking logs for your web site.'),
40          'callback' => 'badbehavior_overview',
41                            'access' => user_access('administer bad behavior'));
42        $items[] = array(
43                            'path' => 'admin/logs/badbehavior/event',
44                            'title' => t('Details'),
45          'callback' => 'badbehavior_event',
46                            'access' => user_access('administer bad behavior'),
47        'type' => MENU_CALLBACK);        'type' => MENU_CALLBACK);
48    }    }
49    return $items;    return $items;
50  }  }
51    
52  function badbehavior_overview() {  function badbehavior_overview() {
53      if (file_exists(BB2_CWD .'/bad-behavior/core.inc.php') && file_exists(BB2_CWD .'/bad-behavior/version.inc.php') && file_exists(BB2_CWD .'/bad-behavior/responses.inc.php')) {
54        require_once(BB2_CWD .'/bad-behavior/version.inc.php');
55        require_once(BB2_CWD .'/bad-behavior/core.inc.php');
56        require_once(BB2_CWD .'/bad-behavior/responses.inc.php');
57      }
58      else {
59        return 'Bad Behavior is not installed correctly.';
60      }
61    
62    $header = array(    $header = array(
63        array('data' => t('Response'), 'field' => 'w.http_response'),
64      array('data' => t('Reason'), 'field' => 'w.denied_reason'),      array('data' => t('Reason'), 'field' => 'w.denied_reason'),
65      array('data' => t('Date'), 'field' => 'w.date', 'sort' => 'desc'),      array('data' => t('Date'), 'field' => 'w.date', 'sort' => 'desc'),
66      array('data' => t('IP'), 'field' => 'w.ip'),      array('data' => t('IP'), 'field' => 'w.ip'),
67      array('data' => t('Referrer'), 'field' => 'w.http_referrer'),      array('data' => t('Agent'), 'field' => 'w.user_agent', 'colspan' => 2)
     array('data' => t('Agent'), 'field' => 'w.http_user_agent', 'colspan' => 2)  
68    );    );
69    $sql = 'SELECT w.* FROM {bad_behavior_log} w ' . tablesort_sql($header);    if (variable_get('badbehavior_verbose_logging_enable',0)) {
70        $sql = 'SELECT w.* FROM {bad_behavior_log} w ' . tablesort_sql($header);
71      }
72      else {
73        $sql = "SELECT w.* FROM {bad_behavior_log} w WHERE w.key != '00000000' " . tablesort_sql($header);
74      }
75    
76    $result = pager_query($sql, 50);    $result = pager_query($sql, 50);
77    
78    while ($behave = db_fetch_object($result)) {    while ($behave = db_fetch_object($result)) {
79      $behave->localdate = bb_convertdate($behave->date);      $response = bb2_get_response($behave->key);
80        $behave->localdate = bb2_convertdate($behave->date);
81      $rows[] = array('data' =>      $rows[] = array('data' =>
82        array(        array(
83          // Cells          // Cells
84          $behave->denied_reason,                                  $response['response'],
85          $behave->date,                                  $response['log'],
86          $behave->ip,                                  $behave->date,
87          $behave->http_referrer,                                  $behave->ip,
88          $behave->http_user_agent,                                  $behave->user_agent,
89          l(t('details'), "admin/logs/badbehavior/event/$behave->id")          l(t('details'), "admin/logs/badbehavior/event/$behave->id")
90        )        )
91      );      );
92    }    }
93    
94    if (!$rows) {    if (!$rows) {
95      $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '7'));      $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '6'));
   }  
   
   $pager = theme('pager', NULL, 50, 0);  
   if (!empty($pager)) {  
     $rows[] = array(array('data' => $pager, 'colspan' => '7'));  
96    }    }
97    
98    $output = theme('table', $header, $rows);    $output = theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
99    
100    print theme('page', $output);    return $output;
101  }  }
102    
103  function badbehavior_event($id) {  function badbehavior_event($id = NULL) {
104      if (file_exists(BB2_CWD .'/bad-behavior/core.inc.php') && file_exists(BB2_CWD .'/bad-behavior/version.inc.php') && file_exists(BB2_CWD .'/bad-behavior/responses.inc.php')) {
105        require_once(BB2_CWD .'/bad-behavior/version.inc.php');
106        require_once(BB2_CWD .'/bad-behavior/core.inc.php');
107        require_once(BB2_CWD .'/bad-behavior/responses.inc.php');
108      }
109      else {
110        return 'Bad Behavior is not installed correctly.';
111      }
112    $output = '';    $output = '';
113    $result = db_query('SELECT w.* FROM {bad_behavior_log} w WHERE w.id = %d', $id);    $result = db_query('SELECT w.* FROM {bad_behavior_log} w WHERE w.id = %d', $id);
114    if ($behave = db_fetch_object($result)) {    if ($behave = db_fetch_object($result)) {
115      $behave->localdate = bb_convertdate($behave->date);      $response = bb2_get_response($behave->key);
116        $behave->localdate = bb2_convertdate($behave->date);
117      $output .= '<table border="1" cellpadding="2" cellspacing="2">';      $output .= '<table border="1" cellpadding="2" cellspacing="2">';
118      $output .= ' <tr><th>'. t('IP Addr') .'</th><td>' . $behave->ip . '</td></tr>';      $output .= ' <tr><th>'. t('IP Addr') .'</th><td>' . $behave->ip . '</td></tr>';
119      $output .= ' <tr><th>'. t('Hostname') .'</th><td>' . gethostbyaddr($behave->ip) . ' (' . l('whois','http://www.whois.sc/'.$behave->ip) . ')</td></tr>';      $output .= ' <tr><th>'. t('Hostname') .'</th><td>' . gethostbyaddr($behave->ip) . ' (' . l('whois','http://www.whois.sc/'.$behave->ip) . ')</td></tr>';
120      $output .= ' <tr><th>'. t('Date') .'</th><td>' . $behave->date . '</td></tr>';      $output .= ' <tr><th>'. t('Date') .'</th><td>' . $behave->date . '</td></tr>';
121      $output .= ' <tr><th>'. t('Request type') .'</th><td>' . $behave->request_method . '</td></tr>';      $output .= ' <tr><th>'. t('Request type') .'</th><td>' . $behave->request_method . '</td></tr>';
     $output .= ' <tr><th>'. t('Host') .'</th><td>' . $behave->http_host . '</td></tr>';  
122      $output .= ' <tr><th>'. t('URI') .'</th><td>' . $behave->request_uri . '</td></tr>';      $output .= ' <tr><th>'. t('URI') .'</th><td>' . $behave->request_uri . '</td></tr>';
123      $output .= ' <tr><th>'. t('Protocol') .'</th><td>' . $behave->server_protocol . '</td></tr>';      $output .= ' <tr><th>'. t('Protocol') .'</th><td>' . $behave->server_protocol . '</td></tr>';
124      $output .= ' <tr><th>'. t('Referrer') .'</th><td>' . $behave->http_referer . '</td></tr>';      $output .= ' <tr><th>'. t('User Agent') .'</th><td>' . $behave->user_agent . '</td></tr>';
     $output .= ' <tr><th>'. t('User Agent') .'</th><td>' . $behave->http_user_agent . '</td></tr>';  
125      $output .= ' <tr><th>'. t('Headers') .'</th><td>' . $behave->http_headers . '</td></tr>';      $output .= ' <tr><th>'. t('Headers') .'</th><td>' . $behave->http_headers . '</td></tr>';
126      $output .= ' <tr><th>'. t('Request Entity') .'</th><td>' . $behave->request_entity . '</td></tr>';      $output .= ' <tr><th>'. t('Request Entity') .'</th><td>' . $behave->request_entity . '</td></tr>';
127      $output .= ' <tr><th>'. t('Denied Reason') .'</th><td>' . $behave->denied_reason . '</td></tr>';      $output .= ' <tr><th>'. t('Denied Reason') .'</th><td>' . $response['log'] . '</td></tr>';
128      $output .= ' <tr><th>'. t('Response') .'</th><td>' . $behave->http_response . '</td></tr>';      $output .= ' <tr><th>'. t('Explanation') .'</th><td>' . $response['explanation'] . '</td></tr>';
129        $output .= ' <tr><th>'. t('Response') .'</th><td>' . $response['response'] . '</td></tr>';
130      $output .= '</table>';      $output .= '</table>';
131    }    }
132    print theme('page', $output);    return $output;
133  }  }
134    
135  function badbehavior_perm() {  function badbehavior_perm() {
# Line 128  function badbehavior_perm() { Line 137  function badbehavior_perm() {
137  }  }
138    
139  function badbehavior_settings() {  function badbehavior_settings() {
140    
141            // TODO: Add a checkbox to toggle between using the email provided in this
142            // textfield or the system wide contact email provided in the Site Information
143            // page settings.
144    
145    $form['badbehavior_email'] = array(    $form['badbehavior_email'] = array(
146                                       '#type' => 'textfield',      '#type' => 'textfield',
147                                       '#title' => t('Administrator Email'),      '#title' => t('Administrator Email'),
148                                       '#default_value' => variable_get('badbehavior_email','badbots@ioerror.us'),      '#default_value' => variable_get('badbehavior_email','badbots@ioerror.us'),
149                                       '#size' => 50,      '#size' => 50,
150                                       '#maxlength' => 50,      '#maxlength' => 50,
151                                       '#description' => t('Administrator email address for blocked users to contact to gain access'),      '#description' => t('Administrator email address for blocked users to contact to gain access'),
152                                       );    );
153    $form['log_settings'] = array(    $form['badbehavior_strict_mode_enable'] = array(
154                    '#type' => 'fieldset',      '#type' => 'radios',
155                    '#title' => t('Log Settings'),      '#title' => 'Enable Strict Mode',
156                    );      '#default_value' => variable_get('badbehavior_strict_mode_enable',0),
157        '#options' => array(t('Disabled'), t('Enabled')),
158    $form['log_settings']['badbehavior_logging_enable'] = array(      '#description' => t('Enable strict checking (blocks more spam but may block some people)'),
159                                                '#type' => 'radios',    );
160                                                '#title' => 'Enable Logging',    $form['badbehavior_verbose_logging_enable'] = array(
161                                                '#default_value' => variable_get('bedbehavior_logging_enable',1),      '#type' => 'radios',
162                                                '#options' => array(t('Disabled'),t('Enabled')),      '#title' => 'Enable Verbose Logging',
163                                                '#description' => t('Enables or disables logging of spam activity'),      '#default_value' => variable_get('badbehavior_verbose_logging_enable',0),
164                                                );      '#options' => array(t('Disabled'), t('Enabled')),
165        '#description' => t('Enables or disables verbose logging which includes all requests, not just failed ones'),
166    $form['log_settings']['badbehavior_verbose_logging_enable'] = array(    );
                                                       '#type' => 'radios',  
                                                       '#title' => 'Enable Verbose Logging',  
                                                       '#default_value' => variable_get('badbehavior_verbose_logging_enable',0),  
                                                       '#options' => array(t('Disabled'),  
                                                                           t('Enabled')),  
                                                       '#description' => t('Enables or disables verbose logging which includes all requests, not just failed ones '),  
                                                       );  
   
   $form['log_settings']['badbehavior_logging_duration'] = array(  
                                                 '#type' => 'textfield',  
                                                 '#title' => t('Logging Duration'),  
                                                 '#default_value' => variable_get('badbehavior_logging_duration',7),  
                                                 '#size' => 6,  
                                                 '#maxlength' => 6,  
                                                 '#description' => t('Number of days to hold logs for'),  
                                                 );  
167    
168    return $form;    return system_settings_form($form);
169  }  }
170    
171  function badbehavior_init() {  // Return current time in the format preferred by your database.
172    define('WP_BB_CWD', dirname(__FILE__));  function bb2_db_date() {
173      return gmdate('Y-m-d H:i:s'); // Example is MySQL format
174    }
175    
176    global $wp_bb_approved;  // Return affected rows from most recent query.
177    global $wp_bb_db_failure;  function bb2_db_affected_rows() {
178    global $wp_bb_remote_addr;    return db_affected_rows();
   global $wp_bb_request_method;  
   global $wp_bb_http_host;  
   global $wp_bb_request_uri;  
   global $wp_bb_server_protocol;  
   global $wp_bb_http_referer;  
   global $wp_bb_http_user_agent;  
   global $wp_bb_server_signature;  
   global $wp_bb_headers;  
   global $wp_bb_request_entity;  
   global $wp_bb_http_headers_mixed;  
   global $wp_bb_logging;  
   global $wp_bb_verbose_logging;  
   global $wp_bb_logging_duration;  
   global $wp_bb_email;  
   global $wp_bb_whitelist_ip_ranges;  
   global $wp_bb_whitelist_user_agents;  
   
   $wp_bb_logging          = variable_get('badbehavior_logging_enable', 1);  
   $wp_bb_verbose_logging  = variable_get('badbehavior_verbose_logging_enable', 0);  
   $wp_bb_logging_duration = variable_get('badbehavior_logging_duration', 7);  
   $wp_bb_email            = variable_get('badbehavior_email',"badbots@ioerror.us");  
   
   define('WP_BB_LOG', '{bad_behavior_log}');  
   
   if (file_exists(WP_BB_CWD . "/bad-behavior-core.php")) {  
     require_once(WP_BB_CWD . "/bad-behavior-core.php");  
   } else {  
     watchdog('badbehavior', t('The third-party bad-behavior files are not installed. Please consult badbehavior/README.txt for details.'), WATCHDOG_ERROR);  
   }  
179  }  }
180    
181  function bb_convertdate($bbdate) {  // Escape a string for database usage
182    $timestamp = strtotime($bbdate. ' UTC');  function bb2_db_escape($string) {
183    return format_date($timestamp,'small');    return db_escape_string($string);
184  }  }
185    
186  function wp_bb_date() {  // Return the number of rows in a particular query.
187    return gmdate('Y-m-d H:i:s');  function bb2_db_num_rows($result) {
188      if ($result != FALSE)
189        return count($result);
190      return 0;
191  }  }
192    
193  function wp_bb_db_query($query) {  function badbehavior_db_errortrap($errno, $string) {
194    }
195    
196    // Run a query and return the results, if any.
197    function bb2_db_query($query) {
198    set_error_handler('badbehavior_db_errortrap');    set_error_handler('badbehavior_db_errortrap');
199    $result = db_query($query);    $result = db_query($query);
200    restore_error_handler();    restore_error_handler();
201    if ($result == FALSE) {    if ($result == FALSE)
202      return $result;      return FALSE;
   }  
203    return db_affected_rows();    return db_affected_rows();
204  }  }
205  ?>  
206    // Return all rows in a particular query.
207    function bb2_db_rows($result) {
208      return $result;
209    }
210    
211    // Return emergency contact email address.
212    function bb2_email() {
213      return variable_get('badbehavior_email',"badbots@ioerror.us");
214    }
215    
216    // write settings to database
217    function bb2_write_settings($settings) {
218      return;
219    }
220    
221    // retrieve settings from database
222    function bb2_read_settings() {
223      return array(
224        'log_table' => db_prefix_tables('{bad_behavior_log}'),
225        'strict' => variable_get('badbehavior_strict_checking_enable', 0),
226        'verbose' => variable_get('badbehavior_verbose_logging_enable', 0));
227    }
228    
229    // installation
230    function bb2_install() {
231      if (variable_get('badbehavior_db_installed', 0) != BB2_VERSION) {
232        bb2_db_query(bb2_table_structure(db_prefix_tables('{bad_behavior_log}')));
233        variable_set('badbehavior_db_installed', BB2_VERSION);
234      }
235    }
236    
237    // Return the top-level relative path of wherever we are (for cookies)
238    function bb2_relative_path() {
239      global $base_path;
240      return $base_path;
241    }
242    
243    function badbehavior_init() {
244      if (file_exists(BB2_CWD . '/bad-behavior/core.inc.php')
245                            && file_exists(BB2_CWD . '/bad-behavior/version.inc.php')) {
246        require_once(BB2_CWD . '/bad-behavior/version.inc.php');
247        require_once(BB2_CWD . '/bad-behavior/core.inc.php');
248        bb2_install();
249        bb2_start(bb2_read_settings());
250      }
251    }
252    
253    function bb2_convertdate($bbdate) {
254      $timestamp = strtotime($bbdate. ' UTC');
255      return format_date($timestamp,'small');
256    }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11

  ViewVC Help
Powered by ViewVC 1.1.2