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