| 1 | <?php |
| 2 | /* |
| 3 | Bad Behavior for TightURL - detects and blocks unwanted Web accesses |
| 4 | Ron Guerin <ron@vnetworx.net> |
| 5 | |
| 6 | I wrote almost none of this, but if you're using this file, you should contact |
| 7 | me (Ron Guerin) and not Michael Hampton, the author of Bad Behavior if you |
| 8 | have questions about this file or the installation of Bad Behavior that you |
| 9 | got with TightURL (which has been modified so as to not throw some unneeded |
| 10 | warnings). |
| 11 | |
| 12 | Based on bad-behavior-generic.php |
| 13 | Copyright (C) 2005-2006 Michael Hampton badbots AT ioerror DOT us |
| 14 | |
| 15 | This program is free software; you can redistribute it and/or modify |
| 16 | it under the terms of the GNU General Public License as published by |
| 17 | the Free Software Foundation; either version 2 of the License, or |
| 18 | (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, |
| 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | GNU General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 28 | |
| 29 | */ |
| 30 | |
| 31 | ############################################################################### |
| 32 | ############################################################################### |
| 33 | |
| 34 | define('BB2_CWD', dirname(__FILE__)); |
| 35 | |
| 36 | // Settings you can adjust for Bad Behavior. |
| 37 | // Change these by changing the corresponding TightURL settings in |
| 38 | // tighturl.config.inc.php instead of editing this file. |
| 39 | |
| 40 | $bb2_settings_defaults = array( 'log_table' => $dbtable . '_bb2', |
| 41 | 'display_stats' => $BBstats, |
| 42 | 'strict' => $BBstrict, |
| 43 | 'verbose' => $BBverbose, |
| 44 | 'logging' => $BBLogging |
| 45 | ); |
| 46 | |
| 47 | // Bad Behavior callback functions. |
| 48 | |
| 49 | // Return current time in the format preferred by your database. |
| 50 | function bb2_db_date() { |
| 51 | return gmdate('Y-m-d H:i:s'); // Example is MySQL format |
| 52 | } |
| 53 | |
| 54 | // Return affected rows from most recent query. |
| 55 | function bb2_db_affected_rows() { |
| 56 | return mysql_affected_rows(); |
| 57 | } |
| 58 | |
| 59 | // Escape a string for database usage |
| 60 | function bb2_db_escape($string) { |
| 61 | return mysql_real_escape_string($string); |
| 62 | } |
| 63 | |
| 64 | // Return the number of rows in a particular query. |
| 65 | function bb2_db_num_rows($result) { |
| 66 | if ($result !== FALSE) return mysql_num_rows($result); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | // Run a query and return the results, if any. |
| 71 | // Should return FALSE if an error occurred. |
| 72 | // Bad Behavior will use the return value here in other callbacks. |
| 73 | function bb2_db_query($query) { |
| 74 | return mysql_query($query); |
| 75 | } |
| 76 | |
| 77 | // Return all rows of a particular query in an array. |
| 78 | |
| 79 | function bb2_db_rows($result) { |
| 80 | $rows = array(); |
| 81 | while ($row = mysql_fetch_assoc($result)) { |
| 82 | $rows[] = $row; |
| 83 | } |
| 84 | return $rows; |
| 85 | } |
| 86 | |
| 87 | // Return emergency contact email address. |
| 88 | function bb2_email() { |
| 89 | return $SiteAdminEmail; |
| 90 | } |
| 91 | |
| 92 | // read settings from TightURL |
| 93 | function bb2_read_settings() { |
| 94 | global $bb2_settings_defaults; |
| 95 | $settings = $bb2_settings_defaults; |
| 96 | return $settings; |
| 97 | } |
| 98 | |
| 99 | // write settings to TightURL |
| 100 | function bb2_write_settings($settings) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | // installation |
| 105 | function bb2_install() { |
| 106 | return bb2_db_query("CREATE TABLE IF NOT EXISTS `".$settings['log_table']."` ( |
| 107 | `id` INT(11) NOT NULL auto_increment, |
| 108 | `ip` TEXT NOT NULL, |
| 109 | `date` DATETIME NOT NULL default '0000-00-00 00:00:00', |
| 110 | `request_method` TEXT NOT NULL, |
| 111 | `request_uri` TEXT NOT NULL, |
| 112 | `server_protocol` TEXT NOT NULL, |
| 113 | `http_headers` TEXT NOT NULL, |
| 114 | `user_agent` TEXT NOT NULL, |
| 115 | `request_entity` TEXT NOT NULL, |
| 116 | `key` TEXT NOT NULL, |
| 117 | INDEX (`ip`(15)), |
| 118 | INDEX (`user_agent`(10)), |
| 119 | PRIMARY KEY (`id`))"); |
| 120 | } |
| 121 | |
| 122 | // Screener |
| 123 | // Insert this into the <head> section of your HTML through a template call |
| 124 | // or whatever is appropriate. This is optional we'll fall back to cookies |
| 125 | // if you don't use it. |
| 126 | function bb2_insert_head() { |
| 127 | global $bb2_javascript; |
| 128 | return $bb2_javascript; |
| 129 | } |
| 130 | |
| 131 | // Display stats |
| 132 | function bb2_insert_stats($force = false) { |
| 133 | $settings = bb2_read_settings(); |
| 134 | |
| 135 | if ($force || $settings['display_stats']) { |
| 136 | $blocked = bb2_db_query("SELECT COUNT(*) FROM " . $settings['log_table'] . " WHERE `key` NOT LIKE '00000000'"); |
| 137 | if ($blocked !== FALSE) { |
| 138 | if (! isset($blocked[0]["COUNT(*)"])) { |
| 139 | $count = 0; |
| 140 | } |
| 141 | else { |
| 142 | $count = $blocked[0]["COUNT(*)"]; |
| 143 | } |
| 144 | $ret = '<br /><a href="http://www.bad-behavior.ioerror.us/">Bad Behavior</a> has blocked ' . $count . ' access attempt(s) in the last 7 days.'; |
| 145 | } |
| 146 | else { |
| 147 | $ret = ""; |
| 148 | } |
| 149 | } |
| 150 | return($ret); |
| 151 | } |
| 152 | |
| 153 | // Return the top-level relative path of wherever we are (for cookies) |
| 154 | // You should provide in $url the top-level URL for your site. |
| 155 | function bb2_relative_path() { |
| 156 | global $self; |
| 157 | return $self; |
| 158 | } |
| 159 | |
| 160 | // Calls inward to Bad Behavior itself. |
| 161 | require_once(BB2_CWD . "/bad-behavior/version.inc.php"); |
| 162 | require_once(BB2_CWD . "/bad-behavior/core.inc.php"); |
| 163 | |
| 164 | bb2_start(bb2_read_settings()); |
| 165 | |
| 166 | ?> |
| 167 | |