-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin-blacklist.php
303 lines (257 loc) · 11.1 KB
/
plugin-blacklist.php
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/*
Plugin Name: Plugin Blacklist
Plugin URI: https://www.littlebizzy.com/plugins/plugin-blacklist
Description: Disallows bad WordPress plugins
Version: 2.1.5
Requires PHP: 7.0
Author: LittleBizzy
Author URI: https://www.littlebizzy.com
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
GitHub Plugin URI: littlebizzy/plugin-blacklist
Primary Branch: master
*/
// prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// disable wordpress.org updates for this plugin
add_filter( 'gu_override_dot_org', function( $overrides ) {
$overrides[] = 'plugin-blacklist/plugin-blacklist.php';
return $overrides;
}, 999 );
// Global to store parsed blacklist data
global $pbm_blacklist_data;
$pbm_blacklist_data = null;
// Load blacklist data from external INI file
function pbm_load_blacklist(): array {
global $pbm_blacklist_data;
// If already loaded, return the data
if ( ! is_null( $pbm_blacklist_data ) ) {
return $pbm_blacklist_data;
}
$file_path = WP_CONTENT_DIR . '/blacklist.txt'; // Path to the blacklist file
// Check if the file exists and is readable
if ( ! file_exists( $file_path ) ) {
pbm_add_admin_notice( 'Blacklist file not found: ' . wp_kses_post( $file_path ) . '. Please upload the correct file.', 'error' );
return [];
}
if ( ! is_readable( $file_path ) ) {
pbm_add_admin_notice( 'Blacklist file is not readable: ' . wp_kses_post( $file_path ) . '. Please check file permissions.', 'error' );
return [];
}
// Parse the file manually
$blacklist_data = [];
$current_section = null;
$file = fopen( $file_path, 'r' );
if ( $file ) {
while ( ( $line = fgets( $file ) ) !== false ) {
$line = trim( $line );
if ( empty( $line ) || $line[0] === ';' || $line[0] === '#' ) {
continue; // Ignore comments and empty lines
}
// Remove comments after values and trim
$line = preg_replace( '/\s*;\s*.*$/', '', $line );
$line = trim( $line );
// Parse section headers and plugin slugs
if ( preg_match( '/^\[(.*)\]$/', $line, $matches ) ) {
$current_section = strtolower( trim( $matches[1] ) );
$blacklist_data[ $current_section ] = [];
} elseif ( $current_section && ! empty( $line ) ) {
$blacklist_data[ $current_section ][] = $line;
}
}
fclose( $file );
}
// Handle empty blacklist section
if ( empty( $blacklist_data['blacklist'] ) ) {
pbm_add_admin_notice( 'No plugins listed under [blacklist] in the file. Add plugin slugs to prevent their use.', 'warning' );
}
$pbm_blacklist_data = $blacklist_data; // Cache blacklist data
return $pbm_blacklist_data;
}
// Add admin notice
function pbm_add_admin_notice( string $message, string $type = 'error' ) {
static $notices = [];
foreach ( $notices as $notice ) {
if ( $notice['message'] === $message && $notice['type'] === $type ) {
return; // Avoid duplicate notices
}
}
$notices[] = [ 'message' => $message, 'type' => $type ];
add_action( 'admin_notices', function() use ( &$notices ) {
foreach ( $notices as $notice ) {
echo '<div class="notice notice-' . esc_attr( $notice['type'] ) . '"><p>' . wp_kses_post( $notice['message'] ) . '</p></div>';
}
$notices = [];
});
}
// Force deactivate blacklisted plugins
function pbm_force_deactivate_blacklisted_plugins() {
$blacklist_data = pbm_load_blacklist();
$blacklisted_plugins = $blacklist_data['blacklist'] ?? [];
if ( empty( $blacklisted_plugins ) ) {
return; // No blacklisted plugins to deactivate
}
$active_plugins = get_option( 'active_plugins', [] );
$deactivated_plugins = [];
foreach ( $active_plugins as $plugin ) {
$plugin_slug = pbm_get_plugin_slug( $plugin );
if ( pbm_is_name_blacklisted( $plugin_slug, $blacklisted_plugins ) ) {
deactivate_plugins( $plugin ); // Deactivate the plugin
$deactivated_plugins[] = $plugin_slug;
}
}
// Display notice if any plugins were deactivated
if ( ! empty( $deactivated_plugins ) ) {
pbm_add_admin_notice(
'The following blacklisted plugins have been deactivated: <strong>' . implode( ', ', $deactivated_plugins ) . '</strong>. Please remove them.',
'error'
);
}
}
add_action( 'admin_init', 'pbm_force_deactivate_blacklisted_plugins' );
// Prevent activation of blacklisted plugins
function pbm_prevent_plugin_activation( $plugin ) {
$blacklist_data = pbm_load_blacklist();
$plugin_slug = pbm_get_plugin_slug( $plugin );
if ( pbm_is_name_blacklisted( $plugin_slug, $blacklist_data['blacklist'] ?? [] ) ) {
wp_die(
'The plugin <strong>' . wp_kses_post( $plugin_slug ) . '</strong> is blacklisted and cannot be activated. Please choose another plugin.',
'Plugin Activation Error',
[ 'back_link' => true ]
);
}
}
add_action( 'activate_plugin', 'pbm_prevent_plugin_activation' );
// Display notices for graylisted and utility plugins
function pbm_display_graylist_and_utility_notices() {
$blacklist_data = pbm_load_blacklist();
$graylisted_plugins = $blacklist_data['graylist'] ?? [];
$utility_plugins = $blacklist_data['utility'] ?? [];
$active_plugins = get_option( 'active_plugins', [] );
// Notify for graylisted plugins
$active_graylisted_plugins = array_filter( $active_plugins, function( $plugin ) use ( $graylisted_plugins ) {
return pbm_is_name_blacklisted( pbm_get_plugin_slug( $plugin ), $graylisted_plugins );
} );
if ( ! empty( $active_graylisted_plugins ) ) {
pbm_add_admin_notice(
'The following graylisted plugins are active: <strong>' . implode( ', ', array_map( 'pbm_get_plugin_slug', $active_graylisted_plugins ) ) . '</strong>. They may be blacklisted in the future.',
'warning'
);
}
// Notify for utility plugins
$active_utility_plugins = array_filter( $active_plugins, function( $plugin ) use ( $utility_plugins ) {
return pbm_is_name_blacklisted( pbm_get_plugin_slug( $plugin ), $utility_plugins );
} );
if ( ! empty( $active_utility_plugins ) ) {
pbm_add_admin_notice(
'The following utility plugins are currently active: <strong>' . implode( ', ', array_map( 'pbm_get_plugin_slug', $active_utility_plugins ) ) . '</strong>. Deactivate them when not in use for security reasons.',
'info'
);
}
}
add_action( 'admin_init', 'pbm_display_graylist_and_utility_notices' );
// Modify plugin action links for blacklisted plugins
function pbm_modify_plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
$blacklist_data = pbm_load_blacklist();
$blacklisted_plugins = $blacklist_data['blacklist'] ?? [];
$plugin_slug = pbm_get_plugin_slug( $plugin_file );
if ( pbm_is_name_blacklisted( $plugin_slug, $blacklisted_plugins ) ) {
$new_actions = [];
$new_actions['blacklisted'] = '<span style="color: #777;">Blacklisted</span>'; // Show Blacklisted label
foreach ( $actions as $key => $action ) {
if ( strpos( $key, 'delete' ) !== false ) {
$new_actions[ $key ] = $action; // Keep delete action
}
}
return $new_actions;
}
return $actions;
}
add_filter( 'plugin_action_links', 'pbm_modify_plugin_action_links', 10, 4 );
add_filter( 'network_admin_plugin_action_links', 'pbm_modify_plugin_action_links', 10, 4 );
// Disable "Install Now" button for blacklisted plugins
function pbm_enqueue_admin_scripts( $hook_suffix ) {
if ( 'plugin-install.php' !== $hook_suffix ) {
return;
}
$blacklist_data = pbm_load_blacklist();
$blacklisted_plugins = $blacklist_data['blacklist'] ?? [];
if ( empty( $blacklisted_plugins ) ) {
return;
}
// Process blacklisted plugins to identify exact matches and prefix matches
$processed_blacklisted_plugins = array_map( function( $item ) {
$item = strtolower( trim( $item ) );
if ( preg_match( '/^\/.*\/$/', $item ) ) {
// Exact match (item wrapped in slashes)
$term = trim( $item, '/' );
return array('term' => $term, 'type' => 'exact');
} else {
// Prefix match
return array('term' => $item, 'type' => 'prefix');
}
}, $blacklisted_plugins );
// Inline script to disable "Install Now" button for blacklisted plugins
wp_add_inline_script( 'jquery-core', '
jQuery(document).ready(function($) {
var blacklistedPlugins = ' . wp_json_encode( $processed_blacklisted_plugins ) . ';
function disableInstallButtons() {
$(".install-now").each(function() {
var pluginSlug = $(this).data("slug");
if (pluginSlug) {
pluginSlug = pluginSlug.toLowerCase().trim();
var isBlacklisted = blacklistedPlugins.some(function(item) {
if (item.type === "exact") {
// Exact match
return item.term === pluginSlug;
} else if (item.type === "prefix") {
// Prefix match
return pluginSlug.startsWith(item.term);
}
return false;
});
if (isBlacklisted) {
$(this).prop("disabled", true)
.css({ "opacity": "0.5", "color": "#777", "border-color": "#ccc", "background-color": "#f7f7f7", "cursor": "default" })
.text("Blacklisted")
.removeAttr("href")
.off("click")
.click(function(e) { e.preventDefault(); e.stopPropagation(); });
}
}
});
}
// Initial run on page load
disableInstallButtons();
// Listen for AJAX completion and re-run the script
$(document).ajaxComplete(function() {
disableInstallButtons();
});
});
');
}
add_action( 'admin_enqueue_scripts', 'pbm_enqueue_admin_scripts', 5 );
// Helper function to check if a plugin is blacklisted
function pbm_is_name_blacklisted( string $plugin_slug, array $list ): bool {
$plugin_slug = strtolower( trim( $plugin_slug ) );
foreach ( $list as $item ) {
$item = strtolower( trim( $item ) );
// Check for exact match (wrapped in slashes)
if ( preg_match( '/^\/.*\/$/', $item ) && trim( $item, '/' ) === $plugin_slug ) {
return true;
}
// Check for wildcard match (prefix)
elseif ( strpos( $plugin_slug, $item ) === 0 ) {
return true;
}
}
return false;
}
// Helper function to extract plugin slug from file path
function pbm_get_plugin_slug( string $plugin_file ): string {
return dirname( $plugin_file );
}
// Ref: ChatGPT