/[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.11, Sun Apr 6 16:49:15 2008 UTC revision 1.12, Tue Sep 1 21:33:58 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2    // $Id: badbehavior.module,v 1.11.4.5 2009/09/01 21:33:28 seanr Exp $
3  define('BB2_CWD', dirname(__FILE__));  
4    define('BB2_CWD', './sites/all/libraries');
5  /**  
6   * Implementation of hook_help().  /**
7   */   * Implementation of hook_help().
8  function badbehavior_help($section='') {   */
9    $output = '';  function badbehavior_help($path, $arg) {
10    switch ($section) {    $output = '';
11      case "admin/logs/badbehavior":    switch ($path) {
12        $output .= t("<p>The badbehavior module examines HTTP requests of visits to your web site, and any suspicious requests are logged for later review.  The suspicious visit is shown an error page with instructions on how to view the site without triggering the bad behavior error message.");      case "admin/reports/badbehavior":
13        break;        $output .= t("<p>The badbehavior module examines HTTP requests of visits to your web site, and any suspicious requests are logged for later review.  The suspicious visit is shown an error page with instructions on how to view the site without triggering the bad behavior error message.");
14      case "admin/modules#description":        break;
15        $output .= t("Stop comment spam before it starts by trapping and blocking spambots before they have a chance to post comments.");    }
16        break;    return $output;
17    }  }
18    return $output;  
19  }  /**
20      * Implementation of hook_menu().
21  /**    */
22    * Implementation of hook_menu().  function badbehavior_menu() {
23    */    $items = array();
24  function badbehavior_menu($may_cache) {  
25    $items = array();    $items['admin/settings/badbehavior'] = array(
26        'title' => 'Bad behavior',
27    if ($may_cache) {      'description' => 'Configure automatic spam blocking for your site.',
28      $items[] = array(      'page callback' => 'drupal_get_form',
29                          'path' => 'admin/settings/badbehavior',      'page arguments' => array('badbehavior_settings'),
30                          'title' => t('Bad behavior'),      'access arguments' => array('administer bad behavior')
31        'description' => t('Configure automatic spam blocking for your site.'),    );
32        'callback' => 'drupal_get_form',    $items['admin/reports/badbehavior'] = array(
33                          'callback arguments' => array('badbehavior_settings'),      'title' => 'Bad behavior',
34                          'access' => user_access('administer bad behavior')      'description' => 'Examine the spam blocking logs for your web site.',
35                  );      'page callback' => 'badbehavior_overview',
36      $items[] = array(      'access arguments' => array('administer bad behavior')
37                          'path' => 'admin/logs/badbehavior',    );
38                          'title' => t('Bad behavior'),    $items['admin/reports/badbehavior/event'] = array(
39        'description' => t('Examine the spam blocking logs for your web site.'),      'title' => 'Details',
40        'callback' => 'badbehavior_overview',      'page callback' => 'badbehavior_event',
41                          'access' => user_access('administer bad behavior'));      'access arguments' => array('administer bad behavior'),
42      $items[] = array(      'type' => MENU_CALLBACK);
43                          'path' => 'admin/logs/badbehavior/event',  
44                          'title' => t('Details'),    return $items;
45        'callback' => 'badbehavior_event',  }
46                          'access' => user_access('administer bad behavior'),  
47        'type' => MENU_CALLBACK);  function badbehavior_overview() {
48    }    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')) {
49    return $items;      require_once(BB2_CWD .'/bad-behavior/version.inc.php');
50  }      require_once(BB2_CWD .'/bad-behavior/core.inc.php');
51        require_once(BB2_CWD .'/bad-behavior/responses.inc.php');
52  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')) {    else {
54      require_once(BB2_CWD .'/bad-behavior/version.inc.php');      return 'Bad Behavior is not installed correctly.  Please download Bad Behavior and extract /bad-behavior/bad-behavior from the zip to sites/all/libraries/bad-behavior';
55      require_once(BB2_CWD .'/bad-behavior/core.inc.php');    }
56      require_once(BB2_CWD .'/bad-behavior/responses.inc.php');  
57    }    $header = array(
58    else {      array('data' => t('Response'), 'field' => 'w.http_response'),
59      return 'Bad Behavior is not installed correctly.';      array('data' => t('Reason'), 'field' => 'w.denied_reason'),
60    }      array('data' => t('Date'), 'field' => 'w.date', 'sort' => 'desc'),
61        array('data' => t('IP'), 'field' => 'w.ip'),
62    $header = array(      array('data' => t('Agent'), 'field' => 'w.user_agent', 'colspan' => 2)
63      array('data' => t('Response'), 'field' => 'w.http_response'),    );
64      array('data' => t('Reason'), 'field' => 'w.denied_reason'),    if (variable_get('badbehavior_verbose_logging_enable', 0)) {
65      array('data' => t('Date'), 'field' => 'w.date', 'sort' => 'desc'),      $sql = 'SELECT w.* FROM {bad_behavior_log} w '. tablesort_sql($header);
66      array('data' => t('IP'), 'field' => 'w.ip'),    }
67      array('data' => t('Agent'), 'field' => 'w.user_agent', 'colspan' => 2)    else {
68    );      $sql = "SELECT w.* FROM {bad_behavior_log} w WHERE w.key <> '00000000' " . tablesort_sql($header);
69    if (variable_get('badbehavior_verbose_logging_enable',0)) {    }
70      $sql = 'SELECT w.* FROM {bad_behavior_log} w ' . tablesort_sql($header);    $result = pager_query($sql, 50);
71    }    while ($behave = db_fetch_object($result)) {
72    else {      $response = bb2_get_response($behave->key);
73      $sql = "SELECT w.* FROM {bad_behavior_log} w WHERE w.key != '00000000' " . tablesort_sql($header);      $behave->localdate = bb2_convertdate($behave->date);
74    }      $rows[] = array('data' => array($response['response'], $response['log'], $behave->date, $behave->ip, $behave->user_agent, l(t('details'), "admin/reports/badbehavior/event/$behave->id")));
75      }
76    $result = pager_query($sql, 50);  
77      if (!$rows) {
78    while ($behave = db_fetch_object($result)) {      $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '6'));
79      $response = bb2_get_response($behave->key);    }
80      $behave->localdate = bb2_convertdate($behave->date);  
81      $rows[] = array('data' =>    $output = theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
82        array(  
83          // Cells    return $output;
84                                  $response['response'],  }
85                                  $response['log'],  
86                                  $behave->date,  function badbehavior_event($id = NULL) {
87                                  $behave->ip,    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')) {
88                                  $behave->user_agent,      require_once(BB2_CWD .'/bad-behavior/version.inc.php');
89          l(t('details'), "admin/logs/badbehavior/event/$behave->id")      require_once(BB2_CWD .'/bad-behavior/core.inc.php');
90        )      require_once(BB2_CWD .'/bad-behavior/responses.inc.php');
91      );    }
92    }    else {
93        return 'Bad Behavior is not installed correctly.  Please download Bad Behavior and extract /bad-behavior/bad-behavior from the zip to sites/all/libraries/bad-behavior';
94    if (!$rows) {    }
95      $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '6'));  
96    }    $output = '';
97      $result = db_query('SELECT w.* FROM {bad_behavior_log} w WHERE w.id = %d', $id);
98    $output = theme('table', $header, $rows) . theme('pager', NULL, 50, 0);    if ($behave = db_fetch_object($result)) {
99        $response = bb2_get_response($behave->key);
100    return $output;      $behave->localdate = bb2_convertdate($behave->date);
101  }      $output .= '<table border="1" cellpadding="2" cellspacing="2">';
102        $output .= ' <tr><th>'. t('IP Addr') .'</th><td>'. $behave->ip .'</td></tr>';
103  function badbehavior_event($id = NULL) {      $output .= ' <tr><th>'. t('Hostname') .'</th><td>'. gethostbyaddr($behave->ip) .' ('. l('whois', 'http://www.whois.sc/'. $behave->ip) .')</td></tr>';
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')) {      $output .= ' <tr><th>'. t('Date') .'</th><td>'. $behave->date .'</td></tr>';
105      require_once(BB2_CWD .'/bad-behavior/version.inc.php');      $output .= ' <tr><th>'. t('Request type') .'</th><td>'. $behave->request_method .'</td></tr>';
106      require_once(BB2_CWD .'/bad-behavior/core.inc.php');      $output .= ' <tr><th>'. t('URI') .'</th><td>'. $behave->request_uri .'</td></tr>';
107      require_once(BB2_CWD .'/bad-behavior/responses.inc.php');      $output .= ' <tr><th>'. t('Protocol') .'</th><td>'. $behave->server_protocol .'</td></tr>';
108    }      $output .= ' <tr><th>'. t('User Agent') .'</th><td>'. $behave->user_agent .'</td></tr>';
109    else {      $output .= ' <tr><th>'. t('Headers') .'</th><td>'. $behave->http_headers .'</td></tr>';
110      return 'Bad Behavior is not installed correctly.';      $output .= ' <tr><th>'. t('Request Entity') .'</th><td>'. $behave->request_entity .'</td></tr>';
111    }      $output .= ' <tr><th>'. t('Denied Reason') .'</th><td>'. $response['log'] .'</td></tr>';
112    $output = '';      $output .= ' <tr><th>'. t('Explanation') .'</th><td>'. $response['explanation'] .'</td></tr>';
113    $result = db_query('SELECT w.* FROM {bad_behavior_log} w WHERE w.id = %d', $id);      $output .= ' <tr><th>'. t('Response') .'</th><td>'. $response['response'] .'</td></tr>';
114    if ($behave = db_fetch_object($result)) {      $output .= '</table>';
115      $response = bb2_get_response($behave->key);    }
116      $behave->localdate = bb2_convertdate($behave->date);    return $output;
117      $output .= '<table border="1" cellpadding="2" cellspacing="2">';  }
118      $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>';  function badbehavior_perm() {
120      $output .= ' <tr><th>'. t('Date') .'</th><td>' . $behave->date . '</td></tr>';    return array('administer bad behavior');
121      $output .= ' <tr><th>'. t('Request type') .'</th><td>' . $behave->request_method . '</td></tr>';  }
122      $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>';  function badbehavior_settings() {
124      $output .= ' <tr><th>'. t('User Agent') .'</th><td>' . $behave->user_agent . '</td></tr>';    $form['badbehavior_email'] = array(
125      $output .= ' <tr><th>'. t('Headers') .'</th><td>' . $behave->http_headers . '</td></tr>';      '#type' => 'textfield',
126      $output .= ' <tr><th>'. t('Request Entity') .'</th><td>' . $behave->request_entity . '</td></tr>';      '#title' => t('Administrator Email'),
127      $output .= ' <tr><th>'. t('Denied Reason') .'</th><td>' . $response['log'] . '</td></tr>';      '#default_value' => variable_get('badbehavior_email', 'badbots@ioerror.us'),
128      $output .= ' <tr><th>'. t('Explanation') .'</th><td>' . $response['explanation'] . '</td></tr>';      '#size' => 50,
129      $output .= ' <tr><th>'. t('Response') .'</th><td>' . $response['response'] . '</td></tr>';      '#maxlength' => 50,
130      $output .= '</table>';      '#description' => t('Administrator email address for blocked users to contact to gain access'),
131    }    );
132    return $output;    $form['badbehavior_strict_mode_enable'] = array(
133  }      '#type' => 'radios',
134        '#title' => 'Enable Strict Mode',
135  function badbehavior_perm() {      '#default_value' => variable_get('badbehavior_strict_mode_enable', 0),
136    return array('administer bad behavior');      '#options' => array(t('Disabled'), t('Enabled')),
137  }      '#description' => t('Enable strict checking (blocks more spam but may block some people)'),
138      );
139  function badbehavior_settings() {    $form['badbehavior_verbose_logging_enable'] = array(
140        '#type' => 'radios',
141          // TODO: Add a checkbox to toggle between using the email provided in this      '#title' => 'Enable Verbose Logging',
142          // textfield or the system wide contact email provided in the Site Information      '#default_value' => variable_get('badbehavior_verbose_logging_enable', 0),
143          // page settings.      '#options' => array(t('Disabled'), t('Enabled')),
144        '#description' => t('Enables or disables verbose logging which includes all requests, not just failed ones'),
145    $form['badbehavior_email'] = array(    );
146      '#type' => 'textfield',  
147      '#title' => t('Administrator Email'),    return system_settings_form($form);
148      '#default_value' => variable_get('badbehavior_email','badbots@ioerror.us'),  }
149      '#size' => 50,  
150      '#maxlength' => 50,  // Return current time in the format preferred by your database.
151      '#description' => t('Administrator email address for blocked users to contact to gain access'),  function bb2_db_date() {
152    );    return gmdate('Y-m-d H:i:s'); // Example is MySQL format
153    $form['badbehavior_strict_mode_enable'] = array(  }
154      '#type' => 'radios',  
155      '#title' => 'Enable Strict Mode',  // Return affected rows from most recent query.
156      '#default_value' => variable_get('badbehavior_strict_mode_enable',0),  function bb2_db_affected_rows() {
157      '#options' => array(t('Disabled'), t('Enabled')),    return db_affected_rows();
158      '#description' => t('Enable strict checking (blocks more spam but may block some people)'),  }
159    );  
160    $form['badbehavior_verbose_logging_enable'] = array(  // Escape a string for database usage
161      '#type' => 'radios',  function bb2_db_escape($string) {
162      '#title' => 'Enable Verbose Logging',    return db_escape_string($string);
163      '#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'),  // Return the number of rows in a particular query.
166    );  function bb2_db_num_rows($result) {
167      if ($result != FALSE)
168    return system_settings_form($form);      return count($result);
169  }    return 0;
170    }
171  // Return current time in the format preferred by your database.  
172  function bb2_db_date() {  function badbehavior_db_errortrap($errno, $string) {
173    return gmdate('Y-m-d H:i:s'); // Example is MySQL format  }
174  }  
175    // Run a query and return the results, if any.
176  // Return affected rows from most recent query.  function bb2_db_query($query) {
177  function bb2_db_affected_rows() {    set_error_handler('badbehavior_db_errortrap');
178    return db_affected_rows();    $result = db_query($query);
179  }    restore_error_handler();
180      if ($result == FALSE)
181  // Escape a string for database usage      return FALSE;
182  function bb2_db_escape($string) {    return db_affected_rows();
183    return db_escape_string($string);  }
184  }  
185    // Return all rows in a particular query.
186  // Return the number of rows in a particular query.  function bb2_db_rows($result) {
187  function bb2_db_num_rows($result) {    return $result;
188    if ($result != FALSE)  }
189      return count($result);  
190    return 0;  // Return emergency contact email address.
191  }  function bb2_email() {
192      return variable_get('badbehavior_email', "badbots@ioerror.us");
193  function badbehavior_db_errortrap($errno, $string) {  }
194  }  
195    // write settings to database
196  // Run a query and return the results, if any.  function bb2_write_settings($settings) {
197  function bb2_db_query($query) {    return;
198    set_error_handler('badbehavior_db_errortrap');  }
199    $result = db_query($query);  
200    restore_error_handler();  // retrieve settings from database
201    if ($result == FALSE)  function bb2_read_settings() {
202      return FALSE;    return array(
203    return db_affected_rows();      'log_table' => 'bad_behavior_log',
204  }      'strict' => variable_get('badbehavior_strict_checking_enable', 0),
205        'verbose' => variable_get('badbehavior_verbose_logging_enable', 0));
206  // Return all rows in a particular query.  }
207  function bb2_db_rows($result) {  
208    return $result;  // installation
209  }  function bb2_install() {
210      if (variable_get('badbehavior_db_installed', 0) != BB2_VERSION) {
211  // Return emergency contact email address.      bb2_db_query(bb2_table_structure('bad_behavior_log'));
212  function bb2_email() {      variable_set('badbehavior_db_installed', BB2_VERSION);
213    return variable_get('badbehavior_email',"badbots@ioerror.us");    }
214  }  }
215    
216  // write settings to database  // Return the top-level relative path of wherever we are (for cookies)
217  function bb2_write_settings($settings) {  function bb2_relative_path() {
218    return;    global $base_path;
219  }    return $base_path;
220    }
221  // retrieve settings from database  
222  function bb2_read_settings() {  function badbehavior_boot() {
223    return array(    if (file_exists(BB2_CWD .'/bad-behavior/core.inc.php') && file_exists(BB2_CWD .'/bad-behavior/version.inc.php')) {
224      'log_table' => db_prefix_tables('{bad_behavior_log}'),      require_once(BB2_CWD .'/bad-behavior/version.inc.php');
225      'strict' => variable_get('badbehavior_strict_checking_enable', 0),      require_once(BB2_CWD .'/bad-behavior/core.inc.php');
226      'verbose' => variable_get('badbehavior_verbose_logging_enable', 0));      bb2_install();
227  }      bb2_start(bb2_read_settings());
228      }
229  // installation  }
230  function bb2_install() {  
231    if (variable_get('badbehavior_db_installed', 0) != BB2_VERSION) {  function bb2_convertdate($bbdate) {
232      bb2_db_query(bb2_table_structure(db_prefix_tables('{bad_behavior_log}')));    $timestamp = strtotime($bbdate .' UTC');
233      variable_set('badbehavior_db_installed', BB2_VERSION);    return format_date($timestamp, 'small');
234    }  }
 }  
   
 // Return the top-level relative path of wherever we are (for cookies)  
 function bb2_relative_path() {  
   global $base_path;  
   return $base_path;  
 }  
   
 function badbehavior_init() {  
   if (file_exists(BB2_CWD . '/bad-behavior/core.inc.php')  
                         && file_exists(BB2_CWD . '/bad-behavior/version.inc.php')) {  
     require_once(BB2_CWD . '/bad-behavior/version.inc.php');  
     require_once(BB2_CWD . '/bad-behavior/core.inc.php');  
     bb2_install();  
     bb2_start(bb2_read_settings());  
   }  
 }  
   
 function bb2_convertdate($bbdate) {  
   $timestamp = strtotime($bbdate. ' UTC');  
   return format_date($timestamp,'small');  
 }  

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

  ViewVC Help
Powered by ViewVC 1.1.2