| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Bad Behavior |
| 4 | Version: 2.0.23 |
| 5 | Description: Deny automated spambots access to your PHP-based Web site. |
| 6 | Plugin URI: http://www.bad-behavior.ioerror.us/ |
| 7 | Author: Michael Hampton |
| 8 | Author URI: http://www.homelandstupidity.us/ |
| 9 | License: GPL |
| 10 | |
| 11 | Bad Behavior - detects and blocks unwanted Web accesses |
| 12 | Copyright (C) 2005 Michael Hampton |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or modify |
| 15 | it under the terms of the GNU General Public License as published by |
| 16 | the Free Software Foundation; either version 2 of the License, or |
| 17 | (at your option) any later version. |
| 18 | |
| 19 | As a special exemption, you may link this program with any of the |
| 20 | programs listed below, regardless of the license terms of those |
| 21 | programs, and distribute the resulting program, without including the |
| 22 | source code for such programs: ExpressionEngine |
| 23 | |
| 24 | This program is distributed in the hope that it will be useful, |
| 25 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | GNU General Public License for more details. |
| 28 | |
| 29 | You should have received a copy of the GNU General Public License |
| 30 | along with this program; if not, write to the Free Software |
| 31 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 32 | |
| 33 | Please report any problems to badbots AT ioerror DOT us |
| 34 | */ |
| 35 | |
| 36 | ############################################################################### |
| 37 | ############################################################################### |
| 38 | |
| 39 | if (!defined('ABSPATH')) die("No cheating!"); |
| 40 | |
| 41 | $bb2_mtime = explode(" ", microtime()); |
| 42 | $bb2_timer_start = $bb2_mtime[1] + $bb2_mtime[0]; |
| 43 | |
| 44 | define('BB2_CWD', dirname(__FILE__)); |
| 45 | |
| 46 | // Bad Behavior callback functions. |
| 47 | |
| 48 | // Return current time in the format preferred by your database. |
| 49 | function bb2_db_date() { |
| 50 | return get_gmt_from_date(current_time('mysql')); |
| 51 | } |
| 52 | |
| 53 | // Return affected rows from most recent query. |
| 54 | function bb2_db_affected_rows() { |
| 55 | global $wpdb; |
| 56 | |
| 57 | return $wpdb->rows_affected; |
| 58 | } |
| 59 | |
| 60 | // Escape a string for database usage |
| 61 | function bb2_db_escape($string) { |
| 62 | global $wpdb; |
| 63 | |
| 64 | return $wpdb->escape($string); |
| 65 | } |
| 66 | |
| 67 | // Return the number of rows in a particular query. |
| 68 | function bb2_db_num_rows($result) { |
| 69 | if ($result !== FALSE) |
| 70 | return count($result); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | // Run a query and return the results, if any. |
| 75 | // Should return FALSE if an error occurred. |
| 76 | // Bad Behavior will use the return value here in other callbacks. |
| 77 | function bb2_db_query($query) { |
| 78 | global $wpdb; |
| 79 | |
| 80 | $wpdb->hide_errors(); |
| 81 | $result = $wpdb->get_results($query, ARRAY_A); |
| 82 | $wpdb->show_errors(); |
| 83 | if (mysql_error()) { |
| 84 | return FALSE; |
| 85 | } |
| 86 | return $result; |
| 87 | } |
| 88 | |
| 89 | // Return all rows in a particular query. |
| 90 | // Should contain an array of all rows generated by calling mysql_fetch_assoc() |
| 91 | // or equivalent and appending the result of each call to an array. |
| 92 | // For WP this is pretty much a no-op. |
| 93 | function bb2_db_rows($result) { |
| 94 | return $result; |
| 95 | } |
| 96 | |
| 97 | // Return emergency contact email address. |
| 98 | function bb2_email() { |
| 99 | return get_bloginfo('admin_email'); |
| 100 | } |
| 101 | |
| 102 | // retrieve settings from database |
| 103 | function bb2_read_settings() { |
| 104 | global $wpdb; |
| 105 | |
| 106 | // Add in default settings when they aren't yet present in WP |
| 107 | $settings = get_settings('bad_behavior_settings'); |
| 108 | if (!$settings) $settings = array(); |
| 109 | return array_merge(array('log_table' => $wpdb->prefix . 'bad_behavior', 'display_stats' => true, 'strict' => false, 'verbose' => false, 'logging' => true, 'httpbl_key' => '', 'httpbl_threat' => '25', 'httpbl_maxage' => '30',), $settings); |
| 110 | } |
| 111 | |
| 112 | // write settings to database |
| 113 | function bb2_write_settings($settings) { |
| 114 | update_option('bad_behavior_settings', $settings); |
| 115 | } |
| 116 | |
| 117 | // installation |
| 118 | function bb2_install() { |
| 119 | $settings = bb2_read_settings(); |
| 120 | if (!$settings['logging']) return; |
| 121 | bb2_db_query(bb2_table_structure($settings['log_table'])); |
| 122 | } |
| 123 | |
| 124 | // Cute timer display; screener |
| 125 | function bb2_insert_head() { |
| 126 | global $bb2_timer_total; |
| 127 | global $bb2_javascript; |
| 128 | echo "\n<!-- Bad Behavior " . BB2_VERSION . " run time: " . number_format(1000 * $bb2_timer_total, 3) . " ms -->\n"; |
| 129 | echo $bb2_javascript; |
| 130 | } |
| 131 | |
| 132 | // Display stats? |
| 133 | function bb2_insert_stats($force = false) { |
| 134 | $settings = bb2_read_settings(); |
| 135 | |
| 136 | if ($force || $settings['display_stats']) { |
| 137 | $blocked = bb2_db_query("SELECT COUNT(*) FROM " . $settings['log_table'] . " WHERE `key` NOT LIKE '00000000'"); |
| 138 | if ($blocked !== FALSE) { |
| 139 | echo sprintf('<p><a href="http://www.bad-behavior.ioerror.us/">%1$s</a> %2$s <strong>%3$s</strong> %4$s</p>', __('Bad Behavior'), __('has blocked'), $blocked[0]["COUNT(*)"], __('access attempts in the last 7 days.')); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Return the top-level relative path of wherever we are (for cookies) |
| 145 | function bb2_relative_path() { |
| 146 | $url = parse_url(get_bloginfo('url')); |
| 147 | return $url['path'] . '/'; |
| 148 | } |
| 149 | |
| 150 | // FIXME: some sort of hack to run install on 1.5 (and older?) blogs |
| 151 | // FIXME: figure out what's wrong on 2.0 that this doesn't work |
| 152 | // register_activation_hook(__FILE__, 'bb2_install'); |
| 153 | //add_action('activate_bb2/bad-behavior-wordpress.php', 'bb2_install'); |
| 154 | add_action('wp_head', 'bb2_insert_head'); |
| 155 | add_action('wp_footer', 'bb2_insert_stats'); |
| 156 | |
| 157 | // Calls inward to Bad Behavor itself. |
| 158 | require_once(BB2_CWD . "/bad-behavior/version.inc.php"); |
| 159 | require_once(BB2_CWD . "/bad-behavior/core.inc.php"); |
| 160 | bb2_install(); // FIXME: see above |
| 161 | |
| 162 | if (is_admin() || strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { // 1.5 kludge |
| 163 | #wp_enqueue_script("admin-forms"); |
| 164 | require_once(BB2_CWD . "/bad-behavior-wordpress-admin.php"); |
| 165 | } |
| 166 | |
| 167 | bb2_start(bb2_read_settings()); |
| 168 | |
| 169 | $bb2_mtime = explode(" ", microtime()); |
| 170 | $bb2_timer_stop = $bb2_mtime[1] + $bb2_mtime[0]; |
| 171 | $bb2_timer_total = $bb2_timer_stop - $bb2_timer_start; |
| 172 | |
| 173 | ?> |
| 174 | |