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