PluginProbe
Vigilant – 100% Free Security Suite: Firewall, 2FA, Login, Headers, Scanner… / 2.10.3
Vigilant – 100% Free Security Suite: Firewall, 2FA, Login, Headers, Scanner… v2.10.3
3.0.0 2.11.12 2.11.11 2.11.10 2.11.9 2.11.7 2.11.8 2.11.6 2.11.5 2.11.4 2.11.3 2.11.1 2.11.2 2.11.0 2.10.5 2.10.4 2.10.3 2.10.2 2.10.1 2.10.0 2.9.9 2.9.8 2.9.6 2.9.7 2.9.5 All 88 releases
vigilante / vigilante.php

vigilante.php in Vigilant – 100% Free Security Suite: Firewall, 2FA, Login, Headers, Scanner… 2.10.3, at vigilante.php

863 lines 33.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Vigilant - 100% Free Security Suite: Firewall, 2FA, Login, Headers, Scanner…
4 * Plugin URI: https://servicios.ayudawp.com
5 * Description: Complete security solution for WordPress. Firewall, 2FA, security headers, login protection, file integrity monitoring, activity logging and more.
6 * Version: 2.10.3
7 * Author: Fernando Tellado
8 * Author URI: https://ayudawp.com
9 * Text Domain: vigilante
10 * Requires at least: 6.2
11 * Tested up to: 7.1
12 * Requires PHP: 7.4
13 * License: GPL v2 or later
14 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
15 *
16 * @package Vigilante
17 */
18
19 // Prevent direct access
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 /**
25 * Plugin constants
26 */
27 define( 'VIGILANTE_VERSION', '2.10.3' );
28 define( 'VIGILANTE_PLUGIN_FILE', __FILE__ );
29 define( 'VIGILANTE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
30 define( 'VIGILANTE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
31 define( 'VIGILANTE_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
32 define( 'VIGILANTE_INCLUDES_DIR', VIGILANTE_PLUGIN_DIR . 'includes/' );
33 define( 'VIGILANTE_ADMIN_DIR', VIGILANTE_PLUGIN_DIR . 'admin/' );
34 define( 'VIGILANTE_ASSETS_URL', VIGILANTE_PLUGIN_URL . 'assets/' );
35
36 // Backup directory outside plugin folder (persists through updates)
37 define( 'VIGILANTE_BACKUP_DIR', WP_CONTENT_DIR . '/vigilante-backups/' );
38
39 // Minimum requirements
40 define( 'VIGILANTE_MIN_PHP_VERSION', '7.4' );
41 define( 'VIGILANTE_MIN_WP_VERSION', '5.0' );
42
43 /**
44 * Check minimum requirements before loading
45 *
46 * @return bool True if requirements are met
47 */
48 function vigilante_check_requirements() {
49 $meets_requirements = true;
50
51 // Check PHP version
52 if ( version_compare( PHP_VERSION, VIGILANTE_MIN_PHP_VERSION, '<' ) ) {
53 $meets_requirements = false;
54 }
55
56 // Check WordPress version
57 global $wp_version;
58 if ( version_compare( $wp_version, VIGILANTE_MIN_WP_VERSION, '<' ) ) {
59 $meets_requirements = false;
60 }
61
62 if ( ! $meets_requirements ) {
63 add_action( 'admin_notices', 'vigilante_requirements_notice' );
64 }
65
66 return $meets_requirements;
67 }
68
69 /**
70 * Display requirements notice - called at admin_notices (after init)
71 */
72 function vigilante_requirements_notice() {
73 global $wp_version;
74 $errors = array();
75
76 if ( version_compare( PHP_VERSION, VIGILANTE_MIN_PHP_VERSION, '<' ) ) {
77 $errors[] = sprintf(
78 /* translators: 1: Current PHP version, 2: Required PHP version */
79 __( 'Vigilant requires PHP %2$s or higher. You are running PHP %1$s.', 'vigilante' ),
80 PHP_VERSION,
81 VIGILANTE_MIN_PHP_VERSION
82 );
83 }
84
85 if ( version_compare( $wp_version, VIGILANTE_MIN_WP_VERSION, '<' ) ) {
86 $errors[] = sprintf(
87 /* translators: 1: Current WordPress version, 2: Required WordPress version */
88 __( 'Vigilant requires WordPress %2$s or higher. You are running WordPress %1$s.', 'vigilante' ),
89 $wp_version,
90 VIGILANTE_MIN_WP_VERSION
91 );
92 }
93
94 foreach ( $errors as $error ) {
95 printf(
96 '<div class="notice notice-error"><p>%s</p></div>',
97 esc_html( $error )
98 );
99 }
100 }
101
102 /**
103 * Load plugin files
104 */
105 function vigilante_load_plugin() {
106 // Check requirements first
107 if ( ! vigilante_check_requirements() ) {
108 return;
109 }
110
111 // Load core classes (no translations used in these)
112 require_once VIGILANTE_INCLUDES_DIR . 'class-database.php';
113 require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php';
114 require_once VIGILANTE_INCLUDES_DIR . 'class-ip-utils.php';
115 require_once VIGILANTE_INCLUDES_DIR . 'class-backup-manager.php';
116 require_once VIGILANTE_INCLUDES_DIR . 'class-activator.php';
117 require_once VIGILANTE_INCLUDES_DIR . 'class-deactivator.php';
118
119 // Load security module files (just loading, not initializing)
120 require_once VIGILANTE_INCLUDES_DIR . 'class-firewall.php';
121 require_once VIGILANTE_INCLUDES_DIR . 'class-security-headers.php';
122 require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-protection.php';
123 require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-recovery.php';
124 require_once VIGILANTE_INCLUDES_DIR . 'class-wpconfig-security.php';
125 require_once VIGILANTE_INCLUDES_DIR . 'class-https-enforcer.php';
126 require_once VIGILANTE_INCLUDES_DIR . 'class-rest-api-security.php';
127 require_once VIGILANTE_INCLUDES_DIR . 'class-user-security.php';
128 require_once VIGILANTE_INCLUDES_DIR . 'class-login-security.php';
129 require_once VIGILANTE_INCLUDES_DIR . 'class-two-factor-email.php';
130 require_once VIGILANTE_INCLUDES_DIR . 'class-two-factor-totp.php';
131 require_once VIGILANTE_INCLUDES_DIR . 'class-email-template.php';
132 require_once VIGILANTE_INCLUDES_DIR . 'class-comment-security.php';
133 require_once VIGILANTE_INCLUDES_DIR . 'class-head-cleaner.php';
134 require_once VIGILANTE_INCLUDES_DIR . 'class-feed-manager.php';
135 require_once VIGILANTE_INCLUDES_DIR . 'class-activity-log.php';
136 require_once VIGILANTE_INCLUDES_DIR . 'class-audit-alerts.php';
137 require_once VIGILANTE_INCLUDES_DIR . 'class-file-integrity.php';
138 require_once VIGILANTE_INCLUDES_DIR . 'class-plugin-status.php';
139 require_once VIGILANTE_INCLUDES_DIR . 'class-under-attack.php';
140 require_once VIGILANTE_INCLUDES_DIR . 'class-database-backup.php';
141 require_once VIGILANTE_INCLUDES_DIR . 'class-database-prefix.php';
142 require_once VIGILANTE_INCLUDES_DIR . 'class-security-analyzer.php';
143
144 // Load admin classes
145 if ( is_admin() ) {
146 require_once VIGILANTE_ADMIN_DIR . 'class-admin-analyzer-ajax.php';
147 require_once VIGILANTE_ADMIN_DIR . 'class-admin-recovery-ajax.php';
148 require_once VIGILANTE_ADMIN_DIR . 'class-admin-audit-alerts-ajax.php';
149 require_once VIGILANTE_ADMIN_DIR . 'class-admin.php';
150 }
151
152 // Weekly Security Analyzer cron (registered even outside admin so it fires on cron hit).
153 add_action( 'vigilante_analyzer_weekly_scan', 'vigilante_run_analyzer_cron' );
154
155 // Daily plugin status check (closed-in-wp.org detection).
156 add_action( 'vigilante_plugin_status_check', 'vigilante_run_plugin_status_check' );
157
158 // Post-Under Attack scan (one-shot, scheduled by Vigilante_Under_Attack::deactivate).
159 add_action( 'vigilante_under_attack_post_scan', 'vigilante_run_post_under_attack_scan' );
160
161 // Initialize core components only - modules will be initialized at init
162 add_action( 'init', 'vigilante_init_plugin', 1 );
163 }
164
165 /**
166 * Initialize plugin at init hook (translations are ready)
167 */
168 function vigilante_init_plugin() {
169 Vigilante_Main::get_instance();
170 }
171
172 /**
173 * Main plugin class - Singleton pattern
174 */
175 final class Vigilante_Main {
176
177 /**
178 * Single instance of the class
179 *
180 * @var Vigilante_Main|null
181 */
182 private static $instance = null;
183
184 /**
185 * Settings instance
186 *
187 * @var Vigilante_Settings
188 */
189 public $settings;
190
191 /**
192 * Database instance
193 *
194 * @var Vigilante_Database
195 */
196 public $database;
197
198 /**
199 * Activity log instance
200 *
201 * @var Vigilante_Activity_Log
202 */
203 public $activity_log;
204
205 /**
206 * Get single instance of the class
207 *
208 * @return Vigilante_Main
209 */
210 public static function get_instance() {
211 if ( null === self::$instance ) {
212 self::$instance = new self();
213 }
214 return self::$instance;
215 }
216
217 /**
218 * Constructor - private to enforce singleton
219 */
220 private function __construct() {
221 $this->init_core();
222 $this->init_modules();
223 $this->init_hooks();
224 }
225
226 /**
227 * Prevent cloning
228 */
229 private function __clone() {}
230
231 /**
232 * Prevent unserializing
233 *
234 * @throws Exception Always throws exception.
235 */
236 public function __wakeup() {
237 throw new Exception( 'Cannot unserialize singleton' );
238 }
239
240 /**
241 * Initialize core components
242 */
243 private function init_core() {
244 $this->database = new Vigilante_Database();
245 $this->settings = new Vigilante_Settings();
246 $this->activity_log = new Vigilante_Activity_Log( $this->settings, $this->database );
247
248 // Auto-create/update tables when DB version is outdated (handles file-only updates)
249 if ( $this->database->needs_update() ) {
250 $this->database->create_tables();
251 }
252
253 // One-time cleanup: versions before 2.7.0 wrote config backups (including
254 // wp-config.php) as files under wp-content/vigilante-backups/. Those now
255 // live in the database, so remove anything left on disk.
256 if ( ! get_option( 'vigilante_legacy_backups_cleaned' ) ) {
257 Vigilante_Backup_Manager::cleanup_legacy_files();
258 update_option( 'vigilante_legacy_backups_cleaned', 1, false );
259 }
260
261 // One-time migration (2.9.0): add '.css' to File Integrity's excluded
262 // extensions on existing installs. Stylesheets are rewritten so often by
263 // themes and optimizer plugins that they were the main post-update false
264 // positive. New installs get it from the defaults; this brings existing
265 // sites in line without touching any other setting. Additive, idempotent.
266 if ( ! get_option( 'vigilante_css_exclusion_migrated' ) ) {
267 $fi = $this->settings->get_section( 'file_integrity' );
268 if ( is_array( $fi ) ) {
269 $ext = ( isset( $fi['excluded_extensions'] ) && is_array( $fi['excluded_extensions'] ) )
270 ? $fi['excluded_extensions']
271 : array();
272 if ( ! in_array( '.css', $ext, true ) ) {
273 $ext[] = '.css';
274 $fi['excluded_extensions'] = $ext;
275 $this->settings->update_section( 'file_integrity', $fi );
276 }
277 }
278 update_option( 'vigilante_css_exclusion_migrated', 1, false );
279 }
280
281 // One-time on upgrade to 2.9.0: drop any cached WordPress.org checksum
282 // manifests. The new comparison is array-aware and self-corrects a cached
283 // array-md5 value, but a manifest cached by an older version while wp.org
284 // was still propagating a new release could otherwise keep producing
285 // false "modified" results until it expires (up to 24h). Flushing on
286 // upgrade guarantees a clean slate on the very release that fixes them;
287 // the next scan refetches fresh manifests. One-time, bulk, no caching.
288 if ( ! get_option( 'vigilante_checksum_cache_flushed_290' ) ) {
289 global $wpdb;
290 $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-time 2.9.0 migration dropping stale checksum transients so the new comparison starts clean.
291 "DELETE FROM {$wpdb->options}
292 WHERE option_name LIKE '\\_transient\\_vigilante\\_plugin\\_checksums\\_%'
293 OR option_name LIKE '\\_transient\\_timeout\\_vigilante\\_plugin\\_checksums\\_%'
294 OR option_name LIKE '\\_transient\\_vigilante\\_theme\\_checksums\\_%'
295 OR option_name LIKE '\\_transient\\_timeout\\_vigilante\\_theme\\_checksums\\_%'
296 OR option_name LIKE '\\_transient\\_vigilante\\_core\\_checksums\\_%'
297 OR option_name LIKE '\\_transient\\_timeout\\_vigilante\\_core\\_checksums\\_%'"
298 );
299 update_option( 'vigilante_checksum_cache_flushed_290', 1, false );
300 }
301 }
302
303 /**
304 * Initialize security modules based on settings
305 */
306 private function init_modules() {
307 $options = $this->settings->get_all_options();
308
309 // Self-heal: a UI bug in earlier 2.4.x betas could leave a section's
310 // top-level 'enabled' flag set to false because the section forms do
311 // not render a checkbox for that field — saving any tab caused the
312 // generic save handler to treat the missing field as "unchecked" and
313 // store it as false. If the master module toggle on the Dashboard is
314 // on but the section flag is off, restore it here so the module's
315 // hooks can attach. Idempotent: noop on healthy installs.
316 $sections = array(
317 'firewall',
318 'security_headers',
319 'login_security',
320 'rest_api_security',
321 'user_security',
322 'wp_hardening',
323 'file_integrity',
324 'activity_log',
325 );
326 $heal_changed = false;
327 foreach ( $sections as $section_name ) {
328 if ( ! empty( $options['modules'][ $section_name ] )
329 && isset( $options[ $section_name ] )
330 && is_array( $options[ $section_name ] )
331 && array_key_exists( 'enabled', $options[ $section_name ] )
332 && empty( $options[ $section_name ]['enabled'] ) ) {
333 $options[ $section_name ]['enabled'] = true;
334 $heal_changed = true;
335 }
336 }
337 if ( $heal_changed ) {
338 update_option( Vigilante_Settings::OPTION_NAME, $options );
339 $this->settings->clear_cache();
340 $options = $this->settings->get_all_options();
341 }
342
343 // Firewall - runs early to block threats
344 if ( ! empty( $options['modules']['firewall'] ) ) {
345 new Vigilante_Firewall( $this->settings, $this->activity_log );
346 }
347
348 // Security Headers - rules are applied via .htaccess, no runtime hooks needed
349 // HTTPS Enforcer still needs runtime hooks
350 if ( ! empty( $options['modules']['security_headers'] ) ) {
351 new Vigilante_Https_Enforcer( $this->settings );
352 }
353
354 // REST API Security
355 if ( ! empty( $options['modules']['rest_api_security'] ) ) {
356 new Vigilante_Rest_Api_Security( $this->settings );
357 }
358
359 // User Security
360 if ( ! empty( $options['modules']['user_security'] ) ) {
361 new Vigilante_User_Security( $this->settings, $this->activity_log );
362 }
363
364 // Login Security
365 if ( ! empty( $options['modules']['login_security'] ) ) {
366 $login_security = new Vigilante_Login_Security( $this->settings, $this->database, $this->activity_log );
367
368 // Two-Factor Authentication (only if login security module is active)
369 new Vigilante_Two_Factor_Email( $this->settings, $this->database, $this->activity_log, $login_security );
370 new Vigilante_Two_Factor_TOTP( $this->settings, $this->database, $this->activity_log, $login_security );
371 }
372
373 // WordPress Hardening (includes comments, head cleaner, feeds)
374 if ( ! empty( $options['modules']['wp_hardening'] ) ) {
375 new Vigilante_Comment_Security( $this->settings );
376 new Vigilante_Head_Cleaner( $this->settings );
377 new Vigilante_Feed_Manager( $this->settings );
378 }
379
380 // File Integrity Scanner
381 if ( ! empty( $options['modules']['file_integrity'] ) ) {
382 new Vigilante_File_Integrity( $this->settings, $this->database, $this->activity_log );
383 new Vigilante_Plugin_Status( $this->settings, $this->activity_log );
384 }
385
386 // Activity Log is always initialized (core component)
387 // Logging is gated by the modules.activity_log toggle and per-type flags
388
389 // Audit Alerts engine - an alerting layer on top of Security Audit.
390 // Only instantiated when Security Audit is on, because it reacts to the
391 // events the activity log records (a passive subscriber, no per-module
392 // coupling). Both alert legs are opt-in, off by default.
393 if ( ! empty( $options['modules']['activity_log'] ) ) {
394 new Vigilante_Audit_Alerts( $this->settings, $this->activity_log );
395 }
396
397 // Under Attack mode - always loaded (independent of modules)
398 new Vigilante_Under_Attack( $this->settings, $this->activity_log );
399
400 // Admin interface
401 if ( is_admin() ) {
402 new Vigilante_Admin( $this->settings, $this->database, $this->activity_log );
403 }
404 }
405
406 /**
407 * Initialize WordPress hooks
408 */
409 private function init_hooks() {
410 // Plugin action links
411 add_filter( 'plugin_action_links_' . VIGILANTE_PLUGIN_BASENAME, array( $this, 'add_action_links' ) );
412
413 // Scheduled tasks
414 add_action( 'vigilante_daily_maintenance', array( $this, 'daily_maintenance' ) );
415 add_action( 'vigilante_hourly_checks', array( $this, 'hourly_checks' ) );
416
417 // AJAX handlers
418 add_action( 'wp_ajax_vigilante_dismiss_notice', array( $this, 'ajax_dismiss_notice' ) );
419
420 // Regenerate critical file baseline after Vigilante modifies wp-config.php or .htaccess
421 add_action( 'vigilante_critical_file_written', array( $this, 'on_critical_file_written' ) );
422
423 // Keep the server layer in step with the installed version.
424 add_action( 'init', array( $this, 'maybe_sync_server_files' ), 20 );
425 }
426
427 /**
428 * Rewrite the .htaccess block when the installed version has moved on
429 *
430 * Updating the plugin did not touch the file: the block was only rewritten
431 * on activation or when the Headers or Firewall tab was saved. So a fix
432 * that lives inside those rules never reached a site that merely updated,
433 * which is exactly what happened with the connect-src of 2.9.6: the browser
434 * kept receiving the old policy, and image uploads kept failing on
435 * WordPress 7.1 until someone pressed Save. This rewrites the block once
436 * per version, and picks up the rules that an activation from WP-CLI had to
437 * leave pending because it could not tell what server it was on.
438 *
439 * Only the content between the plugin markers is rewritten, the same part
440 * any save has always rewritten.
441 *
442 * @since 2.9.9
443 */
444 public function maybe_sync_server_files() {
445 $pending = (bool) get_option( 'vigilante_server_files_pending' );
446
447 if ( ! $pending && VIGILANTE_VERSION === get_option( 'vigilante_server_files_version' ) ) {
448 return;
449 }
450
451 // A failed write is not retried on every request.
452 if ( (int) get_option( 'vigilante_server_files_retry_after' ) > time() ) {
453 return;
454 }
455
456 /*
457 * A subsite has nothing to do here, ever: the file belongs to the main
458 * site. Marking it done keeps every request from re-checking.
459 *
460 * 2.10.0 asked the wrong question at this point and it cost the whole
461 * feature on networks. can_write_shared_files() ends in a capability
462 * check, and this runs on init for every request, so on a network the
463 * branch below was the one nearly every visitor took: it retired the job
464 * without having written a thing. The .htaccess was never refreshed after
465 * an update, and the one-shot snapshot behind it was consumed without
466 * being taken, so not even a network administrator visiting afterwards
467 * retried, because the version had already been marked. Reported by
468 * calzbert, who found it reading the code.
469 */
470 if ( ! Vigilante_Settings::owns_shared_files() ) {
471 $this->mark_server_files_synced();
472 return;
473 }
474
475 require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-manager.php';
476 $manager = Vigilante_Htaccess_Manager::get_instance();
477
478 if ( ! $manager->is_apache() ) {
479 // Still on the command line with nothing to learn from: stay pending.
480 if ( $manager->server_is_unknown() ) {
481 return;
482 }
483
484 // Not Apache: there is no block to keep in step.
485 $this->mark_server_files_synced();
486 return;
487 }
488
489 $options = get_option( Vigilante_Settings::OPTION_NAME, array() );
490 $headers = isset( $options['security_headers'] ) ? (array) $options['security_headers'] : array();
491 $failed = false;
492 $rewrote = false;
493
494 /*
495 * Last chance to keep what the file still says. The rewrites below are
496 * precisely what overwrites it, and on a site whose header settings the
497 * 2.9.8 migration reset, this file is the only remaining copy of what the
498 * owner had actually chosen. Captured here rather than inside the write
499 * path so it only ever happens on a version change: an ordinary save also
500 * leaves the file describing the previous values for an instant, and
501 * capturing there would spend the single slot on a difference the owner
502 * made deliberately.
503 */
504 $wrote_last = (string) get_option( 'vigilante_server_files_version' );
505
506 /*
507 * And only on the very first sync that arrives from a version older than
508 * this one. That is the whole window: the file still describes what the
509 * owner chose, and the rewrite below is what ends it. Gating on the
510 * version also keeps a future release, one that legitimately changes what
511 * the block contains, from reading its own improvement as damage and
512 * offering to undo it.
513 */
514 /*
515 * 2.10.1 and not 2.10.0, deliberately: it gives the networks a second
516 * chance. On a network 2.10.0 marked this done without writing anything,
517 * so the window closed with the snapshot untaken. But nothing was
518 * written, which means the .htaccess on those sites still describes the
519 * configuration its owner actually chose. Reopening the window one
520 * version wide is what lets them be recovered after all.
521 *
522 * Harmless where it already worked: a site that took a snapshot is
523 * skipped because one exists, and a site that found nothing to take has
524 * had its file rewritten to match its settings, so there is still no
525 * difference to find.
526 */
527 if ( '' === $wrote_last || version_compare( $wrote_last, '2.10.1', '<' ) ) {
528 require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-recovery.php';
529 Vigilante_Htaccess_Recovery::maybe_capture( $manager->get_content(), $this->settings );
530 }
531
532 $needs_protection_block = ! empty( $options['modules']['firewall'] )
533 || ! empty( $headers['hide_server_signature'] )
534 || ! empty( $headers['remove_fingerprinting_headers'] );
535
536 /*
537 * A 'locked' result is not a failure: another request is doing this very
538 * work right now. Returning without marking anything leaves the pending
539 * state alone, so whichever request wins finishes the job and this one
540 * stays out of the way. Treating it as a failure would arm the one hour
541 * backoff for something that is already being handled.
542 */
543 $locked = false;
544
545 if ( $needs_protection_block ) {
546 require_once VIGILANTE_INCLUDES_DIR . 'class-htaccess-protection.php';
547 $result = ( new Vigilante_Htaccess_Protection( $this->settings ) )->apply_rules( true );
548 $locked = $locked || ( is_wp_error( $result ) && 'locked' === $result->get_error_code() );
549 $failed = $failed || ( is_wp_error( $result ) && 'locked' !== $result->get_error_code() );
550 $rewrote = true;
551 }
552
553 if ( ! $locked && ! empty( $options['modules']['security_headers'] ) ) {
554 require_once VIGILANTE_INCLUDES_DIR . 'class-security-headers.php';
555 $result = ( new Vigilante_Security_Headers( $this->settings ) )->apply_rules( true );
556 $locked = $locked || ( is_wp_error( $result ) && 'locked' === $result->get_error_code() );
557 $failed = $failed || ( is_wp_error( $result ) && 'locked' !== $result->get_error_code() );
558 $rewrote = true;
559 }
560
561 if ( $locked ) {
562 return;
563 }
564
565 if ( $failed ) {
566 update_option( 'vigilante_server_files_retry_after', time() + HOUR_IN_SECONDS );
567
568 // A refusal to write the server rules is exactly the kind of thing
569 // that used to happen in silence, so it is recorded and retried in
570 // an hour instead of being forgotten.
571 if ( $this->activity_log ) {
572 $this->activity_log->log(
573 'system',
574 'server_rules_write_failed',
575 __( 'The .htaccess rules could not be rewritten after the update. Vigilant will try again in an hour; if the file is read only, fix its permissions or save the Firewall or Headers tab once.', 'vigilante' ),
576 array( 'version' => VIGILANTE_VERSION ),
577 'warning'
578 );
579 }
580
581 return;
582 }
583
584 $this->mark_server_files_synced();
585
586 if ( $rewrote && $this->activity_log ) {
587 $this->activity_log->log(
588 'system',
589 'server_rules_refreshed',
590 sprintf(
591 /* translators: %s: plugin version. */
592 __( 'The .htaccess rules were rewritten to match Vigilant %s.', 'vigilante' ),
593 VIGILANTE_VERSION
594 ),
595 array( 'version' => VIGILANTE_VERSION ),
596 'info'
597 );
598 }
599 }
600
601 /**
602 * Record that the server layer matches the installed version
603 *
604 * @since 2.9.9
605 */
606 private function mark_server_files_synced() {
607 update_option( 'vigilante_server_files_version', VIGILANTE_VERSION );
608 delete_option( 'vigilante_server_files_pending' );
609 delete_option( 'vigilante_server_files_retry_after' );
610 }
611
612 /**
613 * Update the critical file baseline after Vigilante writes to a monitored file
614 *
615 * @param string $filename File that was modified (e.g. 'wp-config.php').
616 */
617 public function on_critical_file_written( $filename ) {
618 if ( ! class_exists( 'Vigilante_File_Integrity' ) ) {
619 require_once VIGILANTE_INCLUDES_DIR . 'class-file-integrity.php';
620 }
621
622 $fi = new Vigilante_File_Integrity( $this->settings, $this->database, $this->activity_log );
623 $fi->update_critical_file_baseline( $filename );
624 }
625
626 /**
627 * Add plugin action links
628 *
629 * @param array $links Existing links.
630 * @return array Modified links.
631 */
632 public function add_action_links( $links ) {
633 $plugin_links = array(
634 '<a href="' . esc_url( admin_url( 'admin.php?page=vigilante' ) ) . '">' . esc_html__( 'Security Settings', 'vigilante' ) . '</a>',
635 );
636 return array_merge( $plugin_links, $links );
637 }
638
639 /**
640 * Daily maintenance tasks
641 */
642 public function daily_maintenance() {
643 // Clean old activity logs
644 $this->activity_log->cleanup_old_logs();
645
646 // Clean old login attempts
647 $this->database->cleanup_old_login_attempts();
648
649 // Clean expired 2FA codes and trusted devices
650 $this->database->cleanup_expired_2fa_codes();
651 $this->database->cleanup_expired_trusted_devices();
652
653 // Remove sensitive files (readme.html, license.txt, licencia.txt)
654 // WordPress core updates recreate these files, so we clean them daily
655 $advanced = $this->settings->get_section( 'advanced' );
656 if ( ! empty( $advanced['remove_readme'] ) ) {
657 $readme_path = ABSPATH . 'readme.html';
658 if ( file_exists( $readme_path ) ) {
659 wp_delete_file( $readme_path );
660 }
661 }
662 if ( ! empty( $advanced['remove_license'] ) ) {
663 $license_files = array( 'license.txt', 'licencia.txt' );
664 foreach ( $license_files as $license_file ) {
665 $license_path = ABSPATH . $license_file;
666 if ( file_exists( $license_path ) ) {
667 wp_delete_file( $license_path );
668 }
669 }
670 }
671
672 // Log maintenance
673 $this->activity_log->log( 'system', 'maintenance', __( 'Daily maintenance completed', 'vigilante' ) );
674 }
675
676 /**
677 * Hourly checks
678 */
679 public function hourly_checks() {
680 // File integrity scans are handled by the File_Integrity class own cron schedule
681 // based on the configured scan_frequency (daily/weekly).
682 }
683
684 /**
685 * AJAX handler for dismissing notices
686 */
687 public function ajax_dismiss_notice() {
688 check_ajax_referer( 'vigilante_dismiss_notice', 'nonce' );
689
690 if ( ! current_user_can( 'manage_options' ) ) {
691 wp_die( -1 );
692 }
693
694 $notice_id = isset( $_POST['notice_id'] ) ? sanitize_key( $_POST['notice_id'] ) : '';
695
696 if ( $notice_id ) {
697 $dismissed = get_option( 'vigilante_dismissed_notices', array() );
698 $dismissed[ $notice_id ] = time();
699 update_option( 'vigilante_dismissed_notices', $dismissed );
700 }
701
702 wp_send_json_success();
703 }
704 }
705
706 /**
707 * Cron handler for the weekly Security Analyzer scan.
708 *
709 * Resolves the shared Vigilante_Security_Analyzer (lazily; no cost when the
710 * cron is not firing) and lets it run the scan + regression email logic.
711 */
712 function vigilante_run_analyzer_cron() {
713 if ( ! class_exists( 'Vigilante_Security_Analyzer' ) ) {
714 require_once VIGILANTE_INCLUDES_DIR . 'class-security-analyzer.php';
715 }
716 if ( ! class_exists( 'Vigilante_Settings' ) ) {
717 require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php';
718 }
719
720 $settings = new Vigilante_Settings();
721 $activity_log = null;
722 if ( class_exists( 'Vigilante_Activity_Log' ) && class_exists( 'Vigilante_Database' ) ) {
723 $database = new Vigilante_Database();
724 $activity_log = new Vigilante_Activity_Log( $settings, $database );
725 }
726
727 $analyzer = new Vigilante_Security_Analyzer( $settings, $activity_log );
728 $analyzer->cron_weekly_scan();
729 }
730
731 /**
732 * Cron handler for the daily plugin status check.
733 *
734 * Resolves the shared Vigilante_Plugin_Status lazily so the daily cron has no
735 * cost while it is not firing.
736 */
737 function vigilante_run_plugin_status_check() {
738 if ( ! class_exists( 'Vigilante_Plugin_Status' ) ) {
739 require_once VIGILANTE_INCLUDES_DIR . 'class-plugin-status.php';
740 }
741 if ( ! class_exists( 'Vigilante_Settings' ) ) {
742 require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php';
743 }
744
745 $settings = new Vigilante_Settings();
746 $activity_log = null;
747 if ( class_exists( 'Vigilante_Activity_Log' ) && class_exists( 'Vigilante_Database' ) ) {
748 $database = new Vigilante_Database();
749 $activity_log = new Vigilante_Activity_Log( $settings, $database );
750 }
751
752 $checker = new Vigilante_Plugin_Status( $settings, $activity_log );
753 $checker->run_scheduled_check();
754 }
755
756 /**
757 * Run a Security Analyzer full scan after Under Attack mode deactivates.
758 *
759 * Scheduled one-shot from Vigilante_Under_Attack::deactivate() so the dashboard
760 * reflects the restored configuration with the slow HTTP/header probes the
761 * mode prevented from running safely while it was active.
762 */
763 function vigilante_run_post_under_attack_scan() {
764 if ( ! class_exists( 'Vigilante_Under_Attack' ) ) {
765 require_once VIGILANTE_INCLUDES_DIR . 'class-under-attack.php';
766 }
767 if ( ! class_exists( 'Vigilante_Settings' ) ) {
768 require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php';
769 }
770
771 $settings = new Vigilante_Settings();
772 $activity_log = null;
773 if ( class_exists( 'Vigilante_Activity_Log' ) && class_exists( 'Vigilante_Database' ) ) {
774 $database = new Vigilante_Database();
775 $activity_log = new Vigilante_Activity_Log( $settings, $database );
776 }
777
778 $under_attack = new Vigilante_Under_Attack( $settings, $activity_log );
779 $under_attack->run_analyzer_scan( 'all' );
780 }
781
782 /**
783 * Plugin activation hook
784 */
785 function vigilante_activate() {
786 require_once VIGILANTE_INCLUDES_DIR . 'class-database.php';
787 require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php';
788 require_once VIGILANTE_INCLUDES_DIR . 'class-backup-manager.php';
789 require_once VIGILANTE_INCLUDES_DIR . 'class-activator.php';
790
791 Vigilante_Activator::activate();
792 }
793 register_activation_hook( __FILE__, 'vigilante_activate' );
794
795 /**
796 * Plugin deactivation hook
797 */
798 function vigilante_deactivate() {
799 require_once VIGILANTE_INCLUDES_DIR . 'class-database.php';
800 require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php';
801 require_once VIGILANTE_INCLUDES_DIR . 'class-backup-manager.php';
802 require_once VIGILANTE_INCLUDES_DIR . 'class-deactivator.php';
803
804 Vigilante_Deactivator::deactivate();
805 }
806 register_deactivation_hook( __FILE__, 'vigilante_deactivate' );
807
808 /**
809 * Initialize plugin after WordPress loads
810 */
811 add_action( 'plugins_loaded', 'vigilante_load_plugin' );
812
813 /*
814 * The hidden wp-admin is answered as early as the request can be judged with
815 * certainty, before the theme and the other plugins load. The modules are built
816 * on init priority 1, so until 2.9.9 a request that was going to be refused had
817 * already paid for the whole boot.
818 */
819 add_action( 'plugins_loaded', 'vigilante_block_hidden_admin_early', 1 );
820
821 /**
822 * Cheap gate for the early hidden wp-admin rejection
823 *
824 * Everything that can be decided without loading a single plugin class is
825 * decided here, so the usual request pays nothing more than a couple of
826 * comparisons and one option read that WordPress has already cached.
827 *
828 * @since 2.9.9
829 */
830 function vigilante_block_hidden_admin_early() {
831 if ( ! is_admin() ) {
832 return;
833 }
834
835 $method = isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : 'GET';
836
837 // POST is how remote managers authenticate, and the later path lets it through too.
838 if ( 'GET' !== $method ) {
839 return;
840 }
841
842 $options = get_option( 'vigilante_options', array() );
843
844 if ( ! is_array( $options )
845 || empty( $options['modules']['login_security'] )
846 || empty( $options['login_security']['custom_login_url'] ) ) {
847 return;
848 }
849
850 require_once VIGILANTE_INCLUDES_DIR . 'class-ip-utils.php';
851 require_once VIGILANTE_INCLUDES_DIR . 'class-login-security.php';
852
853 Vigilante_Login_Security::maybe_block_hidden_admin_early( $options );
854 }
855
856 /**
857 * Helper function to get plugin instance
858 *
859 * @return Vigilante_Main
860 */
861 function vigilante() {
862 return Vigilante_Main::get_instance();
863 }