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

Contents of /contributions/modules/badbehavior/badbehavior.module

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


Revision 1.12 - (show annotations) (download) (as text)
Tue Sep 1 21:33:58 2009 UTC (2 months, 3 weeks ago) by seanr
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +234 -256 lines
File MIME type: text/x-php
Catch head up to Drupal 6
1 <?php
2 // $Id: badbehavior.module,v 1.11.4.5 2009/09/01 21:33:28 seanr Exp $
3
4 define('BB2_CWD', './sites/all/libraries');
5
6 /**
7 * Implementation of hook_help().
8 */
9 function badbehavior_help($path, $arg) {
10 $output = '';
11 switch ($path) {
12 case "admin/reports/badbehavior":
13 $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 break;
15 }
16 return $output;
17 }
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function badbehavior_menu() {
23 $items = array();
24
25 $items['admin/settings/badbehavior'] = array(
26 'title' => 'Bad behavior',
27 'description' => 'Configure automatic spam blocking for your site.',
28 'page callback' => 'drupal_get_form',
29 'page arguments' => array('badbehavior_settings'),
30 'access arguments' => array('administer bad behavior')
31 );
32 $items['admin/reports/badbehavior'] = array(
33 'title' => 'Bad behavior',
34 'description' => 'Examine the spam blocking logs for your web site.',
35 'page callback' => 'badbehavior_overview',
36 'access arguments' => array('administer bad behavior')
37 );
38 $items['admin/reports/badbehavior/event'] = array(
39 'title' => 'Details',
40 'page callback' => 'badbehavior_event',
41 'access arguments' => array('administer bad behavior'),
42 'type' => MENU_CALLBACK);
43
44 return $items;
45 }
46
47 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 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 }
53 else {
54 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 }
56
57 $header = array(
58 array('data' => t('Response'), 'field' => 'w.http_response'),
59 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 array('data' => t('Agent'), 'field' => 'w.user_agent', 'colspan' => 2)
63 );
64 if (variable_get('badbehavior_verbose_logging_enable', 0)) {
65 $sql = 'SELECT w.* FROM {bad_behavior_log} w '. tablesort_sql($header);
66 }
67 else {
68 $sql = "SELECT w.* FROM {bad_behavior_log} w WHERE w.key <> '00000000' " . tablesort_sql($header);
69 }
70 $result = pager_query($sql, 50);
71 while ($behave = db_fetch_object($result)) {
72 $response = bb2_get_response($behave->key);
73 $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
77 if (!$rows) {
78 $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '6'));
79 }
80
81 $output = theme('table', $header, $rows) . theme('pager', NULL, 50, 0);
82
83 return $output;
84 }
85
86 function badbehavior_event($id = NULL) {
87 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 require_once(BB2_CWD .'/bad-behavior/version.inc.php');
89 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 }
95
96 $output = '';
97 $result = db_query('SELECT w.* FROM {bad_behavior_log} w WHERE w.id = %d', $id);
98 if ($behave = db_fetch_object($result)) {
99 $response = bb2_get_response($behave->key);
100 $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 $output .= ' <tr><th>'. t('Hostname') .'</th><td>'. gethostbyaddr($behave->ip) .' ('. l('whois', 'http://www.whois.sc/'. $behave->ip) .')</td></tr>';
104 $output .= ' <tr><th>'. t('Date') .'</th><td>'. $behave->date .'</td></tr>';
105 $output .= ' <tr><th>'. t('Request type') .'</th><td>'. $behave->request_method .'</td></tr>';
106 $output .= ' <tr><th>'. t('URI') .'</th><td>'. $behave->request_uri .'</td></tr>';
107 $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 $output .= ' <tr><th>'. t('Headers') .'</th><td>'. $behave->http_headers .'</td></tr>';
110 $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 .= ' <tr><th>'. t('Explanation') .'</th><td>'. $response['explanation'] .'</td></tr>';
113 $output .= ' <tr><th>'. t('Response') .'</th><td>'. $response['response'] .'</td></tr>';
114 $output .= '</table>';
115 }
116 return $output;
117 }
118
119 function badbehavior_perm() {
120 return array('administer bad behavior');
121 }
122
123 function badbehavior_settings() {
124 $form['badbehavior_email'] = array(
125 '#type' => 'textfield',
126 '#title' => t('Administrator Email'),
127 '#default_value' => variable_get('badbehavior_email', 'badbots@ioerror.us'),
128 '#size' => 50,
129 '#maxlength' => 50,
130 '#description' => t('Administrator email address for blocked users to contact to gain access'),
131 );
132 $form['badbehavior_strict_mode_enable'] = array(
133 '#type' => 'radios',
134 '#title' => 'Enable Strict Mode',
135 '#default_value' => variable_get('badbehavior_strict_mode_enable', 0),
136 '#options' => array(t('Disabled'), t('Enabled')),
137 '#description' => t('Enable strict checking (blocks more spam but may block some people)'),
138 );
139 $form['badbehavior_verbose_logging_enable'] = array(
140 '#type' => 'radios',
141 '#title' => 'Enable Verbose Logging',
142 '#default_value' => variable_get('badbehavior_verbose_logging_enable', 0),
143 '#options' => array(t('Disabled'), t('Enabled')),
144 '#description' => t('Enables or disables verbose logging which includes all requests, not just failed ones'),
145 );
146
147 return system_settings_form($form);
148 }
149
150 // Return current time in the format preferred by your database.
151 function bb2_db_date() {
152 return gmdate('Y-m-d H:i:s'); // Example is MySQL format
153 }
154
155 // Return affected rows from most recent query.
156 function bb2_db_affected_rows() {
157 return db_affected_rows();
158 }
159
160 // Escape a string for database usage
161 function bb2_db_escape($string) {
162 return db_escape_string($string);
163 }
164
165 // Return the number of rows in a particular query.
166 function bb2_db_num_rows($result) {
167 if ($result != FALSE)
168 return count($result);
169 return 0;
170 }
171
172 function badbehavior_db_errortrap($errno, $string) {
173 }
174
175 // Run a query and return the results, if any.
176 function bb2_db_query($query) {
177 set_error_handler('badbehavior_db_errortrap');
178 $result = db_query($query);
179 restore_error_handler();
180 if ($result == FALSE)
181 return FALSE;
182 return db_affected_rows();
183 }
184
185 // Return all rows in a particular query.
186 function bb2_db_rows($result) {
187 return $result;
188 }
189
190 // Return emergency contact email address.
191 function bb2_email() {
192 return variable_get('badbehavior_email', "badbots@ioerror.us");
193 }
194
195 // write settings to database
196 function bb2_write_settings($settings) {
197 return;
198 }
199
200 // retrieve settings from database
201 function bb2_read_settings() {
202 return array(
203 'log_table' => 'bad_behavior_log',
204 'strict' => variable_get('badbehavior_strict_checking_enable', 0),
205 'verbose' => variable_get('badbehavior_verbose_logging_enable', 0));
206 }
207
208 // installation
209 function bb2_install() {
210 if (variable_get('badbehavior_db_installed', 0) != BB2_VERSION) {
211 bb2_db_query(bb2_table_structure('bad_behavior_log'));
212 variable_set('badbehavior_db_installed', BB2_VERSION);
213 }
214 }
215
216 // Return the top-level relative path of wherever we are (for cookies)
217 function bb2_relative_path() {
218 global $base_path;
219 return $base_path;
220 }
221
222 function badbehavior_boot() {
223 if (file_exists(BB2_CWD .'/bad-behavior/core.inc.php') && file_exists(BB2_CWD .'/bad-behavior/version.inc.php')) {
224 require_once(BB2_CWD .'/bad-behavior/version.inc.php');
225 require_once(BB2_CWD .'/bad-behavior/core.inc.php');
226 bb2_install();
227 bb2_start(bb2_read_settings());
228 }
229 }
230
231 function bb2_convertdate($bbdate) {
232 $timestamp = strtotime($bbdate .' UTC');
233 return format_date($timestamp, 'small');
234 }

  ViewVC Help
Powered by ViewVC 1.1.2