| 1 | <?php |
| 2 | /* |
| 3 | http://www.bad-behavior.ioerror.us/ |
| 4 | |
| 5 | Bad Behavior - detects and blocks unwanted Web accesses |
| 6 | Copyright (C) 2005 Michael Hampton |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | // This file is the entry point for Bad Behavior. |
| 24 | |
| 25 | if (!defined('MEDIAWIKI')) die(); |
| 26 | |
| 27 | // Settings you can adjust for Bad Behavior. |
| 28 | $bb2_settings_defaults = array( |
| 29 | 'log_table' => $wgDBprefix . 'bad_behavior', |
| 30 | 'display_stats' => true, |
| 31 | 'strict' => false, |
| 32 | 'verbose' => false, |
| 33 | 'logging' => true, |
| 34 | 'httpbl_key' => '', |
| 35 | 'httpbl_threat' => '25', |
| 36 | 'httpbl_maxage' => '30', |
| 37 | ); |
| 38 | |
| 39 | define('BB2_CWD', dirname(__FILE__)); |
| 40 | |
| 41 | // Bad Behavior callback functions. |
| 42 | |
| 43 | // Return current time in the format preferred by your database. |
| 44 | function bb2_db_date() { |
| 45 | return gmdate('Y-m-d H:i:s'); |
| 46 | } |
| 47 | |
| 48 | // Return affected rows from most recent query. |
| 49 | function bb2_db_affected_rows($result) { |
| 50 | return wfAffectedRows($result); |
| 51 | } |
| 52 | |
| 53 | // Escape a string for database usage |
| 54 | function bb2_db_escape($string) { |
| 55 | // FIXME SECURITY: Get a straight answer from somebody on how MW escapes stuff |
| 56 | return addslashes($string); |
| 57 | } |
| 58 | |
| 59 | // Return the number of rows in a particular query. |
| 60 | function bb2_db_num_rows($result) { |
| 61 | return wfNumRows($result); |
| 62 | } |
| 63 | |
| 64 | // Run a query and return the results, if any. |
| 65 | // Should return FALSE if an error occurred. |
| 66 | function bb2_db_query($query) { |
| 67 | $bb2_last_query = wfQuery($query, DB_WRITE); |
| 68 | return $bb2_last_query; |
| 69 | } |
| 70 | |
| 71 | // Return all rows in a particular query. |
| 72 | // Should contain an array of all rows generated by calling mysql_fetch_assoc() |
| 73 | // or equivalent and appending the result of each call to an array. |
| 74 | function bb2_db_rows($result) { |
| 75 | $rows = array(); |
| 76 | while ($row = wfFetchRow($result)) { |
| 77 | $rows[] = $row; |
| 78 | } |
| 79 | return $rows; |
| 80 | } |
| 81 | |
| 82 | // Return emergency contact email address. |
| 83 | function bb2_email() { |
| 84 | global $wgEmergencyContact; |
| 85 | return $wgEmergencyContact; |
| 86 | } |
| 87 | |
| 88 | // This Bad Behavior-related function is a stub. You can help MediaWiki by expanding it. |
| 89 | // retrieve settings from database |
| 90 | function bb2_read_settings() { |
| 91 | global $bb2_settings_defaults; |
| 92 | return $bb2_settings_defaults; |
| 93 | } |
| 94 | |
| 95 | // This Bad Behavior-related function is a stub. You can help MediaWiki by expanding it. |
| 96 | // write settings to database |
| 97 | function bb2_write_settings($settings) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // In some configurations automatic table creation may fail with the message |
| 102 | // You must update your load-balancing configuration. |
| 103 | // You can create the table manually (see query in |
| 104 | // bad-behavior/database.inc.php) and add this line to your LocalSettings.php: |
| 105 | // |
| 106 | // define('BB2_NO_CREATE', true); |
| 107 | |
| 108 | // installation |
| 109 | function bb2_install() { |
| 110 | $settings = bb2_read_settings(); |
| 111 | if (defined('BB2_NO_CREATE')) return; |
| 112 | if (!$settings['logging']) return; |
| 113 | bb2_db_query(bb2_table_structure($settings['log_table'])); |
| 114 | } |
| 115 | |
| 116 | // Return the top-level relative path of wherever we are (for cookies) |
| 117 | function bb2_relative_path() { |
| 118 | // TODO: This might not be the best way, but it seems to work |
| 119 | global $wgScript; |
| 120 | return dirname($wgScript) . "/"; |
| 121 | } |
| 122 | |
| 123 | // Cute timer display |
| 124 | function bb2_mediawiki_timer(&$parser, &$text) { |
| 125 | global $bb2_timer_total; |
| 126 | $text = "<!-- Bad Behavior " . BB2_VERSION . " run time: " . number_format(1000 * $bb2_timer_total, 3) . " ms -->" . $text; |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | function bb2_mediawiki_entry() { |
| 131 | global $bb2_timer_total; |
| 132 | |
| 133 | $bb2_mtime = explode(" ", microtime()); |
| 134 | $bb2_timer_start = $bb2_mtime[1] + $bb2_mtime[0]; |
| 135 | |
| 136 | if (php_sapi_name() != 'cli') { |
| 137 | require_once(BB2_CWD . "/bad-behavior/core.inc.php"); |
| 138 | bb2_install(); // FIXME: see above |
| 139 | $settings = bb2_read_settings(); |
| 140 | bb2_start($settings); |
| 141 | } |
| 142 | |
| 143 | $bb2_mtime = explode(" ", microtime()); |
| 144 | $bb2_timer_stop = $bb2_mtime[1] + $bb2_mtime[0]; |
| 145 | $bb2_timer_total = $bb2_timer_stop - $bb2_timer_start; |
| 146 | } |
| 147 | |
| 148 | require_once(BB2_CWD . "/bad-behavior/version.inc.php"); |
| 149 | $wgExtensionCredits['other'][] = array( |
| 150 | 'name' => 'Bad Behavior', |
| 151 | 'version' => BB2_VERSION, |
| 152 | 'author' => 'Michael Hampton', |
| 153 | 'description' => 'Detects and blocks unwanted Web accesses', |
| 154 | 'url' => 'http://www.bad-behavior.ioerror.us/' |
| 155 | ); |
| 156 | |
| 157 | #$wgHooks['ParserAfterTidy'][] = 'bb2_mediawiki_timer'; |
| 158 | $wgExtensionFunctions[] = 'bb2_mediawiki_entry'; |
| 159 | |
| 160 | ?> |
| 161 | |