PluginProbe
Banhammer – Monitor Site Traffic, Block Bad Users and Bots / 3.5.0
Banhammer – Monitor Site Traffic, Block Bad Users and Bots v3.5.0
3.5.3 trunk 1.1 1.2 1.3 1.4 1.5 1.5.1 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.8.1 2.8.2 2.9 3.0 All 43 releases
banhammer / banhammer.php

banhammer.php in Banhammer – Monitor Site Traffic, Block Bad Users and Bots 3.5.0, at banhammer.php

442 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Banhammer
4 Plugin URI: https://perishablepress.com/banhammer/
5 Description: Monitor traffic and ban unwanted visitors.
6 Tags: monitor, security, ban, block, bots
7 Author: Jeff Starr
8 Author URI: https://plugin-planet.com/
9 Donate link: https://monzillamedia.com/donate.html
10 Contributors: specialk
11 Requires at least: 4.7
12 Tested up to: 6.9
13 Stable tag: 3.5.0
14 Version: 3.5.0
15 Requires PHP: 5.6.20
16 Text Domain: banhammer
17 Domain Path: /languages
18 License: GPL v2 or later
19 */
20
21 /*
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License
24 as published by the Free Software Foundation; either version
25 2 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 with this program. If not, visit: https://www.gnu.org/licenses/
34
35 Copyright 2025 Monzilla Media. All rights reserved.
36 */
37
38 if (!defined('ABSPATH')) die();
39
40 if (!class_exists('BanhammerWP')) {
41
42 class BanhammerWP {
43
44 function __construct() {
45
46 $this->constants();
47 $this->includes();
48
49 register_activation_hook (__FILE__, array($this, 'check_banhammer'));
50 register_activation_hook (__FILE__, array($this, 'create_table'));
51 register_activation_hook (__FILE__, 'banhammer_dismiss_notice_activate');
52 register_activation_hook (__FILE__, 'banhammer_cron_activation');
53 register_deactivation_hook(__FILE__, 'banhammer_cron_deactivation');
54
55 add_action('admin_init', array($this, 'check_version'));
56 add_action('init', array($this, 'load_i18n'));
57 add_action('upgrader_process_complete', array($this, 'private_key'), 10, 2);
58 add_filter('plugin_action_links', array($this, 'action_links'), 10, 2);
59 add_filter('plugin_row_meta', array($this, 'plugin_links'), 10, 2);
60 add_filter('admin_footer_text', array($this, 'footer_text'), 10, 1);
61
62 add_filter('removable_query_args', 'banhammer_remove_query_args');
63 add_action('admin_enqueue_scripts', 'banhammer_admin_enqueue_scripts');
64 add_action('admin_print_scripts', 'banhammer_admin_print_scripts');
65 add_action('admin_notices', 'banhammer_admin_notices');
66 add_action('admin_init', 'banhammer_register_settings');
67 add_action('admin_init', 'banhammer_reset_options');
68 add_action('admin_menu', 'banhammer_menu_pages');
69 add_action('admin_init', 'banhammer_add_target');
70 add_action('admin_init', 'banhammer_dismiss_notice_save');
71 add_action('admin_init', 'banhammer_dismiss_notice_version');
72
73 add_action('admin_init', 'banhammer_cron_update');
74 add_filter('cron_schedules', 'banhammer_cron_intervals');
75 add_action('banhammer_cron_reset', 'banhammer_cron_reset');
76
77 add_action('wp_ajax_banhammer_armory', 'banhammer_armory');
78 add_action('wp_ajax_banhammer_tower', 'banhammer_tower');
79 add_action('wp_ajax_banhammer_aux', 'banhammer_aux');
80
81 add_action('init', 'banhammer_init');
82
83 }
84
85 function constants() {
86
87 if (!defined('BANHAMMER_VERSION')) define('BANHAMMER_VERSION', '3.5.0');
88 if (!defined('BANHAMMER_REQUIRE')) define('BANHAMMER_REQUIRE', '4.7');
89 if (!defined('BANHAMMER_AUTHOR')) define('BANHAMMER_AUTHOR', 'Jeff Starr');
90 if (!defined('BANHAMMER_NAME')) define('BANHAMMER_NAME', 'Banhammer');
91 if (!defined('BANHAMMER_HOME')) define('BANHAMMER_HOME', esc_url('https://perishablepress.com/banhammer/'));
92 if (!defined('BANHAMMER_URL')) define('BANHAMMER_URL', plugin_dir_url(__FILE__));
93 if (!defined('BANHAMMER_DIR')) define('BANHAMMER_DIR', plugin_dir_path(__FILE__));
94 if (!defined('BANHAMMER_FILE')) define('BANHAMMER_FILE', plugin_basename(__FILE__));
95 if (!defined('BANHAMMER_SLUG')) define('BANHAMMER_SLUG', basename(dirname(__FILE__)));
96 if (!defined('BANHAMMER_BLANK')) define('BANHAMMER_BLANK', '[blank]');
97
98 }
99
100 function includes() {
101
102 require_once BANHAMMER_DIR .'inc/banhammer-cron.php';
103 require_once BANHAMMER_DIR .'inc/banhammer-functions.php';
104 require_once BANHAMMER_DIR .'inc/banhammer-core.php';
105
106 if (is_admin()) {
107
108 require_once BANHAMMER_DIR .'inc/status-codes.php';
109 require_once BANHAMMER_DIR .'inc/contextual-help.php';
110 require_once BANHAMMER_DIR .'inc/resources-enqueue.php';
111 require_once BANHAMMER_DIR .'inc/settings-display.php';
112 require_once BANHAMMER_DIR .'inc/settings-register.php';
113 require_once BANHAMMER_DIR .'inc/settings-reset.php';
114 require_once BANHAMMER_DIR .'inc/armory-display.php';
115 require_once BANHAMMER_DIR .'inc/armory-ajax.php';
116 require_once BANHAMMER_DIR .'inc/tower-display.php';
117 require_once BANHAMMER_DIR .'inc/tower-ajax.php';
118
119 }
120
121 }
122
123 function options() {
124
125 $options = array(
126
127 'enable_plugin' => true,
128 'ignore_logged' => true,
129 'protect_login' => false,
130 'protect_admin' => false,
131 'banned_response' => 'default',
132 'custom_message' => '<p>You has been banned!</p>',
133 'redirect_url' => 'https://example.com/',
134 'status_code' => '403',
135 'reset_interval' => 'banhammer_one_day',
136 'target_key' => banhammer_get_random_alphanumeric(),
137
138 );
139
140 return apply_filters('banhammer_settings', $options);
141
142 }
143
144 function armory() {
145
146 $options = array(
147
148 'rows' => 5,
149 'view' => 2,
150 'fx' => 0,
151
152 );
153
154 return apply_filters('banhammer_armory', $options);
155
156 }
157
158 function tower() {
159
160 $options = array(
161 array(
162 'date' => '2050-01-01 @ 01:01:01 ('. esc_html__('EXAMPLE ONLY: NOT A REAL IP ADDRESS!', 'banhammer') .')',
163 'target' => '777.888.999.000',
164 'status' => 1,
165 'hits' => 3,
166 ),
167 );
168
169 return apply_filters('banhammer_tower', $options);
170
171 }
172
173 function create_table() {
174
175 global $wpdb;
176
177 if (!current_user_can('activate_plugins')) return;
178
179 $table = $wpdb->prefix .'banhammer';
180
181 $charset = $wpdb->get_charset_collate();
182
183 $check = $wpdb->get_var("SHOW TABLES LIKE '". $table ."'");
184
185 if ($check !== $table) {
186
187 $sql = "CREATE TABLE ". $table ." (
188
189 id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
190 date VARCHAR(50) NOT NULL DEFAULT '',
191
192 status TINYINT UNSIGNED NOT NULL DEFAULT 0,
193 process TINYINT UNSIGNED NOT NULL DEFAULT 0,
194
195 user VARCHAR(50) NOT NULL DEFAULT '',
196 type VARCHAR(25) NOT NULL DEFAULT '',
197 theme VARCHAR(50) NOT NULL DEFAULT '',
198 code VARCHAR(25) NOT NULL DEFAULT '',
199 country VARCHAR(50) NOT NULL DEFAULT '',
200 region VARCHAR(50) NOT NULL DEFAULT '',
201 city VARCHAR(50) NOT NULL DEFAULT '',
202 zip VARCHAR(25) NOT NULL DEFAULT '',
203 protocol VARCHAR(25) NOT NULL DEFAULT '',
204 method VARCHAR(25) NOT NULL DEFAULT '',
205 response VARCHAR(25) NOT NULL DEFAULT '',
206 connect VARCHAR(50) NOT NULL DEFAULT '',
207 domain VARCHAR(250) NOT NULL DEFAULT '',
208 ip VARCHAR(250) NOT NULL DEFAULT '',
209 proxy VARCHAR(250) NOT NULL DEFAULT '',
210 host VARCHAR(250) NOT NULL DEFAULT '',
211 request VARCHAR(2500) NOT NULL DEFAULT '',
212 postvars VARCHAR(2500) NOT NULL DEFAULT '',
213 files VARCHAR(2500) NOT NULL DEFAULT '',
214 ua VARCHAR(500) NOT NULL DEFAULT '',
215 refer VARCHAR(500) NOT NULL DEFAULT '',
216 cookies VARCHAR(1000) NOT NULL DEFAULT '',
217 headers VARCHAR(1000) NOT NULL DEFAULT '',
218 message VARCHAR(1000) NOT NULL DEFAULT '',
219 notes VARCHAR(1000) NOT NULL DEFAULT '',
220 custom VARCHAR(500) NOT NULL DEFAULT '',
221 data VARCHAR(500) NOT NULL DEFAULT '',
222 aux VARCHAR(100) NOT NULL DEFAULT '',
223
224 PRIMARY KEY (id)
225
226 ) ". $charset .";";
227
228 require_once(ABSPATH .'wp-admin/includes/upgrade.php');
229
230 dbDelta($sql);
231
232 }
233
234 }
235
236 function private_key($upgrader_object, $options) {
237
238 if ((isset($options['action']) && $options['action'] === 'update') && (isset($options['type']) && $options['type'] === 'plugin')) {
239
240 if (isset($options['plugins'])) {
241
242 foreach($options['plugins'] as $plugin) {
243
244 if ($plugin === BANHAMMER_FILE) {
245
246 $key = banhammer_get_secret_key(30, 'plugin upgrade');
247
248 }
249
250 }
251
252 }
253
254 }
255
256 }
257
258 function action_links($links, $file) {
259
260 if ($file === BANHAMMER_FILE && current_user_can('manage_options')) {
261
262 $settings = '<a href="'. admin_url('admin.php?page=banhammer') .'">'. esc_html__('Settings', 'banhammer') .'</a>';
263
264 array_unshift($links, $settings);
265
266 }
267
268 if ($file === BANHAMMER_FILE) {
269
270 $pro_href = 'https://plugin-planet.com/banhammer-pro/';
271 $pro_title = esc_attr__('Get Banhammer Pro!', 'banhammer');
272 $pro_text = esc_html__('Go&nbsp;Pro', 'banhammer');
273 $pro_style = 'font-weight:bold;';
274
275 $pro = '<a target="_blank" rel="noopener noreferrer" href="'. $pro_href .'" title="'. $pro_title .'" style="'. $pro_style .'">'. $pro_text .'</a>';
276
277 array_unshift($links, $pro);
278
279 }
280
281 return $links;
282
283 }
284
285 function plugin_links($links, $file) {
286
287 if ($file === BANHAMMER_FILE) {
288
289 $home_href = 'https://perishablepress.com/banhammer/';
290 $home_title = esc_attr__('Plugin Homepage', 'banhammer');
291 $home_text = esc_html__('Homepage', 'banhammer');
292
293 $links[] = '<a target="_blank" rel="noopener noreferrer" href="'. $home_href .'" title="'. $home_title .'">'. $home_text .'</a>';
294
295 $rate_href = 'https://wordpress.org/support/plugin/'. BANHAMMER_SLUG .'/reviews/?rate=5#new-post';
296 $rate_title = esc_attr__('Click here to rate and review this plugin on WordPress.org', 'banhammer');
297 $rate_text = esc_html__('Rate this plugin', 'banhammer') .'&nbsp;&raquo;';
298
299 $links[] = '<a target="_blank" rel="noopener noreferrer" href="'. $rate_href .'" title="'. $rate_title .'">'. $rate_text .'</a>';
300
301 }
302
303 return $links;
304
305 }
306
307 function footer_text($text) {
308
309 $screen_id = banhammer_get_current_screen_id();
310
311 $ids = array('toplevel_page_banhammer', 'banhammer_page_banhammer-armory', 'banhammer_page_banhammer-tower');
312
313 if ($screen_id && apply_filters('banhammer_admin_footer_text', in_array($screen_id, $ids))) {
314
315 $text = __('Like Banhammer? Give it a', 'banhammer');
316
317 $text .= ' <a target="_blank" rel="noopener noreferrer" href="https://wordpress.org/support/plugin/banhammer/reviews/?rate=5#new-post">';
318
319 $text .= __('�
320
321
322
323
324 rating&nbsp;&raquo;', 'banhammer') .'</a>';
325
326 }
327
328 return $text;
329
330 }
331
332 function check_banhammer() {
333
334 if (class_exists('Banhammer_Pro')) {
335
336 if (is_plugin_active(BANHAMMER_FILE)) {
337
338 deactivate_plugins(BANHAMMER_FILE);
339
340 $msg = '<strong>'. esc_html__('Warning:', 'banhammer') .'</strong> ';
341
342 $msg .= esc_html__('Pro version of Banhammer currently active. Free and Pro versions cannot be activated at the same time. ', 'banhammer');
343
344 $msg .= esc_html__('Please return to the', 'banhammer');
345
346 $msg .= ' <a href="'. admin_url('plugins.php') .'">'. esc_html__('WP Admin Area', 'banhammer') .'</a> '. esc_html__('and try again.', 'banhammer');
347
348 wp_die($msg);
349
350 }
351
352 }
353
354 }
355
356 function check_version() {
357
358 if (current_user_can('manage_options')) {
359
360 $wp_version = get_bloginfo('version');
361
362 if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
363
364 if (version_compare($wp_version, BANHAMMER_REQUIRE, '<')) {
365
366 if (is_plugin_active(BANHAMMER_FILE)) {
367
368 deactivate_plugins(BANHAMMER_FILE);
369
370 $msg = '<strong>'. BANHAMMER_NAME .'</strong> '. esc_html__('requires WordPress ', 'banhammer') . BANHAMMER_REQUIRE;
371 $msg .= esc_html__(' or higher, and has been deactivated! ', 'banhammer');
372 $msg .= esc_html__('Please return to the', 'banhammer') .' <a href="'. admin_url() .'">';
373 $msg .= esc_html__('WP Admin Area', 'banhammer') .'</a> '. esc_html__('to upgrade WordPress and try again.', 'banhammer');
374
375 wp_die($msg);
376
377 }
378
379 }
380
381 }
382
383 }
384
385 }
386
387 function load_i18n() {
388
389 $domain = 'banhammer';
390
391 $locale = apply_filters('banhammer_locale', get_locale(), $domain);
392
393 $dir = trailingslashit(WP_LANG_DIR);
394
395 $file = $domain .'-'. $locale .'.mo';
396
397 $path_1 = $dir . $file;
398
399 $path_2 = $dir . $domain .'/'. $file;
400
401 $path_3 = $dir .'plugins/'. $file;
402
403 $path_4 = $dir .'plugins/'. $domain .'/'. $file;
404
405 $paths = array($path_1, $path_2, $path_3, $path_4);
406
407 foreach ($paths as $path) {
408
409 if ($loaded = load_textdomain($domain, $path)) {
410
411 return $loaded;
412
413 } else {
414
415 return load_plugin_textdomain($domain, false, dirname(BANHAMMER_FILE) .'/languages/');
416
417 }
418
419 }
420
421 }
422
423 function __clone() {
424
425 _doing_it_wrong(__FUNCTION__, esc_html__('Sorry, pal!', 'banhammer'), BANHAMMER_VERSION);
426
427 }
428
429 function __wakeup() {
430
431 _doing_it_wrong(__FUNCTION__, esc_html__('Sorry, pal!', 'banhammer'), BANHAMMER_VERSION);
432
433 }
434
435 }
436
437 global $Banhammer;
438
439 $GLOBALS['BanhammerWP'] = $BanhammerWP = new BanhammerWP();
440
441 }
442