-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbouncer.php
More file actions
283 lines (258 loc) · 8.87 KB
/
Copy pathbouncer.php
File metadata and controls
283 lines (258 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* Plugin Name: Bouncer
* Plugin URI: https://regionallyfamous.com/bouncer
* Description: A plugin behavior firewall for WordPress. Monitors what your plugins actually do — database queries, outbound HTTP, hook registrations, file changes — and uses AI to catch threats before they cause damage.
* Version: 1.0.7
* Requires at least: 7.0
* Requires PHP: 8.1
* Author: Regionally Famous
* Author URI: https://regionallyfamous.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: bouncer
* Domain Path: /languages
*
* @package Bouncer
*/
defined( 'ABSPATH' ) || exit;
// Everything below this gate uses PHP 8.1+ syntax; skip parsing it on older runtimes.
if ( version_compare( PHP_VERSION, '8.1', '<' ) ) {
if ( ! defined( 'BOUNCER_PLUGIN_FILE' ) ) {
define( 'BOUNCER_PLUGIN_FILE', __FILE__ );
}
require_once plugin_dir_path( __FILE__ ) . 'includes/bouncer-php-requirement.php';
register_activation_hook( __FILE__, 'bouncer_abort_activation_low_php' );
add_action( 'admin_notices', 'bouncer_admin_notice_low_php' );
return;
}
/**
* Whether the site meets Bouncer’s WordPress minimum (7.0), including 7.0 betas/RCs.
*
* Raw `version_compare( $wp_version, '7.0' )` treats `7.0-RC2` as below `7.0`; core’s
* `is_wp_version_compatible()` handles pre-releases correctly.
*
* @return bool
*/
function bouncer_meets_minimum_wp(): bool {
if ( function_exists( 'is_wp_version_compatible' ) ) {
return is_wp_version_compatible( '7.0' );
}
global $wp_version;
if ( ! isset( $wp_version ) || ! is_string( $wp_version ) ) {
return false;
}
if ( ! preg_match( '/^(\d+\.\d+(?:\.\d+)?)/', $wp_version, $matches ) ) {
return false;
}
return version_compare( $matches[1], '7.0', '>=' );
}
if ( ! bouncer_meets_minimum_wp() ) {
if ( ! defined( 'BOUNCER_PLUGIN_FILE' ) ) {
define( 'BOUNCER_PLUGIN_FILE', __FILE__ );
}
require_once plugin_dir_path( __FILE__ ) . 'includes/bouncer-wp-requirement.php';
register_activation_hook( __FILE__, 'bouncer_abort_activation_low_wp' );
add_action( 'admin_notices', 'bouncer_admin_notice_low_wp' );
return;
}
define( 'BOUNCER_VERSION', '1.0.7' );
define( 'BOUNCER_PLUGIN_FILE', __FILE__ );
define( 'BOUNCER_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'BOUNCER_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'BOUNCER_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
/** Capability for Bouncer admin, REST, and AJAX (mirrors core-style dedicated caps). */
define( 'BOUNCER_CAP', 'manage_bouncer' );
require_once BOUNCER_PLUGIN_DIR . 'includes/bouncer-pending-violations-lock.php';
/**
* Whether the current user may use Bouncer (dedicated cap, with manage_options fallback).
*
* @return bool
*/
function bouncer_current_user_can_manage() {
return current_user_can( BOUNCER_CAP );
}
/**
* @param string $tab Screen tab: dashboard|events|manifests|settings.
* @param array<string,mixed> $args Extra query arguments (merged; may override tab).
* @return string Full URL to a Bouncer screen under Tools.
*/
function bouncer_admin_url( string $tab = 'dashboard', array $args = array() ): string {
$query = array_merge(
array( 'page' => 'bouncer' ),
'dashboard' === $tab ? array() : array( 'tab' => $tab ),
$args
);
return add_query_arg( $query, admin_url( 'tools.php' ) );
}
/**
* Users with manage_options may use Bouncer; custom roles can hold manage_bouncer alone.
*
* Parameters intentionally untyped so a bad filter cannot trigger TypeError on PHP 8+.
*
* @param mixed $allcaps All capabilities for the user.
* @param mixed $caps Requested capabilities.
* @param mixed $args Capability check arguments.
* @param mixed $user User object.
* @return mixed
*/
function bouncer_map_capabilities_to_manage_bouncer( $allcaps, $caps, $args, $user ) { // phpcs:ignore WordPress.WP.Capabilities.Unknown
if ( ! is_array( $allcaps ) ) {
return $allcaps;
}
if ( ! is_array( $caps ) || ! in_array( BOUNCER_CAP, $caps, true ) ) {
return $allcaps;
}
if ( ! empty( $allcaps['manage_options'] ) ) {
$allcaps[ BOUNCER_CAP ] = true;
}
return $allcaps;
}
add_filter( 'user_has_cap', 'bouncer_map_capabilities_to_manage_bouncer', 10, 4 );
/**
* Grant manage_bouncer to Administrators once (covers upgrades without re-activation).
*/
function bouncer_ensure_capabilities(): void {
$rev = (int) get_option( 'bouncer_cap_revision', 0 );
if ( $rev >= 1 ) {
return;
}
$role = get_role( 'administrator' );
if ( $role && ! $role->has_cap( BOUNCER_CAP ) ) {
$role->add_cap( BOUNCER_CAP );
}
update_option( 'bouncer_cap_revision', 1 );
}
add_action( 'init', 'bouncer_ensure_capabilities', 5 );
/**
* Autoloader for Bouncer classes.
*
* @param string $class_name The class name to load.
*/
function bouncer_autoloader( $class_name ) {
if ( 'Bouncer' === $class_name ) {
require_once BOUNCER_PLUGIN_DIR . 'includes/class-bouncer.php';
return;
}
if ( 0 !== strpos( $class_name, 'Bouncer_' ) ) {
return;
}
$file = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
$path = BOUNCER_PLUGIN_DIR . 'includes/' . $file;
if ( file_exists( $path ) ) {
require_once $path;
}
}
spl_autoload_register( 'bouncer_autoloader' );
/**
* Load translations on init (WP 6.7+ requires text domains at init or later).
*/
function bouncer_load_textdomain() {
load_plugin_textdomain( 'bouncer', false, dirname( BOUNCER_PLUGIN_BASENAME ) . '/languages' );
}
add_action( 'init', 'bouncer_load_textdomain', 0 );
/**
* Log a caught exception/error (respects WP_DEBUG for stack traces).
*
* @param string $context Short label, e.g. "activation".
* @param Throwable $e The error.
*/
function bouncer_log_throwable( string $context, \Throwable $e ): void {
$msg = 'Bouncer ' . $context . ': ' . $e->getMessage();
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $msg . "\n" . $e->getTraceAsString() );
} else {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $msg );
}
}
/**
* Plugin activation handler.
*/
function bouncer_activate() {
try {
require_once BOUNCER_PLUGIN_DIR . 'includes/class-bouncer-activator.php';
Bouncer_Activator::activate();
} catch ( \Throwable $e ) {
bouncer_log_throwable( 'activation', $e );
set_transient( 'bouncer_activation_error', wp_strip_all_tags( $e->getMessage() ), 5 * MINUTE_IN_SECONDS );
if ( ! function_exists( 'deactivate_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
deactivate_plugins( plugin_basename( BOUNCER_PLUGIN_FILE ), true );
wp_die(
'<p>' . esc_html( wp_strip_all_tags( $e->getMessage() ) ) . '</p>',
esc_html__( 'Plugin could not be activated', 'bouncer' ),
array(
'response' => 500,
'back_link' => true,
)
);
}
}
register_activation_hook( __FILE__, 'bouncer_activate' );
/**
* Plugin deactivation handler.
*/
function bouncer_deactivate() {
try {
require_once BOUNCER_PLUGIN_DIR . 'includes/class-bouncer-activator.php';
Bouncer_Activator::deactivate();
} catch ( \Throwable $e ) {
bouncer_log_throwable( 'deactivation', $e );
}
}
register_deactivation_hook( __FILE__, 'bouncer_deactivate' );
/**
* Show a one-time admin notice if an activation error transient remains (edge cases).
*/
function bouncer_maybe_show_activation_error_notice(): void {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$msg = get_transient( 'bouncer_activation_error' );
if ( ! is_string( $msg ) || '' === $msg ) {
return;
}
delete_transient( 'bouncer_activation_error' );
echo '<div class="notice notice-error is-dismissible"><p><strong>' . esc_html__( 'Bouncer', 'bouncer' ) . ':</strong> ';
echo esc_html( $msg );
echo '</p></div>';
}
add_action( 'admin_notices', 'bouncer_maybe_show_activation_error_notice', 5 );
/**
* Admin notice after a runtime init failure.
*/
function bouncer_render_init_error_notice(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$msg = get_transient( 'bouncer_init_error' );
if ( ! is_string( $msg ) || '' === $msg ) {
return;
}
delete_transient( 'bouncer_init_error' );
echo '<div class="notice notice-error is-dismissible"><p><strong>' . esc_html__( 'Bouncer', 'bouncer' ) . ':</strong> ';
echo esc_html( $msg );
echo ' ' . esc_html__( 'Check debug.log for details if WP_DEBUG is enabled.', 'bouncer' );
echo '</p></div>';
}
/**
* Initialize the plugin.
*/
function bouncer_init() {
try {
$bouncer = Bouncer::get_instance();
$bouncer->init();
} catch ( \Throwable $e ) {
bouncer_log_throwable( 'init', $e );
set_transient( 'bouncer_init_error', wp_strip_all_tags( $e->getMessage() ), 5 * MINUTE_IN_SECONDS );
add_action( 'admin_notices', 'bouncer_render_init_error_notice', 1 );
}
}
add_action( 'plugins_loaded', 'bouncer_init', 5 );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once BOUNCER_PLUGIN_DIR . 'includes/class-bouncer-cli.php';
WP_CLI::add_command( 'bouncer', 'Bouncer_CLI_Command' );
}