| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Vigilant |
| 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.9.5 |
| 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.0 |
| 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.9.5' ); |
| 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-wpconfig-security.php'; |
| 124 |
require_once VIGILANTE_INCLUDES_DIR . 'class-https-enforcer.php'; |
| 125 |
require_once VIGILANTE_INCLUDES_DIR . 'class-rest-api-security.php'; |
| 126 |
require_once VIGILANTE_INCLUDES_DIR . 'class-user-security.php'; |
| 127 |
require_once VIGILANTE_INCLUDES_DIR . 'class-login-security.php'; |
| 128 |
require_once VIGILANTE_INCLUDES_DIR . 'class-two-factor-email.php'; |
| 129 |
require_once VIGILANTE_INCLUDES_DIR . 'class-two-factor-totp.php'; |
| 130 |
require_once VIGILANTE_INCLUDES_DIR . 'class-email-template.php'; |
| 131 |
require_once VIGILANTE_INCLUDES_DIR . 'class-comment-security.php'; |
| 132 |
require_once VIGILANTE_INCLUDES_DIR . 'class-head-cleaner.php'; |
| 133 |
require_once VIGILANTE_INCLUDES_DIR . 'class-feed-manager.php'; |
| 134 |
require_once VIGILANTE_INCLUDES_DIR . 'class-activity-log.php'; |
| 135 |
require_once VIGILANTE_INCLUDES_DIR . 'class-audit-alerts.php'; |
| 136 |
require_once VIGILANTE_INCLUDES_DIR . 'class-file-integrity.php'; |
| 137 |
require_once VIGILANTE_INCLUDES_DIR . 'class-plugin-status.php'; |
| 138 |
require_once VIGILANTE_INCLUDES_DIR . 'class-under-attack.php'; |
| 139 |
require_once VIGILANTE_INCLUDES_DIR . 'class-database-backup.php'; |
| 140 |
require_once VIGILANTE_INCLUDES_DIR . 'class-database-prefix.php'; |
| 141 |
require_once VIGILANTE_INCLUDES_DIR . 'class-security-analyzer.php'; |
| 142 |
|
| 143 |
// Load admin classes |
| 144 |
if ( is_admin() ) { |
| 145 |
require_once VIGILANTE_ADMIN_DIR . 'class-admin-analyzer-ajax.php'; |
| 146 |
require_once VIGILANTE_ADMIN_DIR . 'class-admin-audit-alerts-ajax.php'; |
| 147 |
require_once VIGILANTE_ADMIN_DIR . 'class-admin.php'; |
| 148 |
} |
| 149 |
|
| 150 |
// Weekly Security Analyzer cron (registered even outside admin so it fires on cron hit). |
| 151 |
add_action( 'vigilante_analyzer_weekly_scan', 'vigilante_run_analyzer_cron' ); |
| 152 |
|
| 153 |
// Daily plugin status check (closed-in-wp.org detection). |
| 154 |
add_action( 'vigilante_plugin_status_check', 'vigilante_run_plugin_status_check' ); |
| 155 |
|
| 156 |
// Post-Under Attack scan (one-shot, scheduled by Vigilante_Under_Attack::deactivate). |
| 157 |
add_action( 'vigilante_under_attack_post_scan', 'vigilante_run_post_under_attack_scan' ); |
| 158 |
|
| 159 |
// Initialize core components only - modules will be initialized at init |
| 160 |
add_action( 'init', 'vigilante_init_plugin', 1 ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Initialize plugin at init hook (translations are ready) |
| 165 |
*/ |
| 166 |
function vigilante_init_plugin() { |
| 167 |
Vigilante_Main::get_instance(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Main plugin class - Singleton pattern |
| 172 |
*/ |
| 173 |
final class Vigilante_Main { |
| 174 |
|
| 175 |
/** |
| 176 |
* Single instance of the class |
| 177 |
* |
| 178 |
* @var Vigilante_Main|null |
| 179 |
*/ |
| 180 |
private static $instance = null; |
| 181 |
|
| 182 |
/** |
| 183 |
* Settings instance |
| 184 |
* |
| 185 |
* @var Vigilante_Settings |
| 186 |
*/ |
| 187 |
public $settings; |
| 188 |
|
| 189 |
/** |
| 190 |
* Database instance |
| 191 |
* |
| 192 |
* @var Vigilante_Database |
| 193 |
*/ |
| 194 |
public $database; |
| 195 |
|
| 196 |
/** |
| 197 |
* Activity log instance |
| 198 |
* |
| 199 |
* @var Vigilante_Activity_Log |
| 200 |
*/ |
| 201 |
public $activity_log; |
| 202 |
|
| 203 |
/** |
| 204 |
* Get single instance of the class |
| 205 |
* |
| 206 |
* @return Vigilante_Main |
| 207 |
*/ |
| 208 |
public static function get_instance() { |
| 209 |
if ( null === self::$instance ) { |
| 210 |
self::$instance = new self(); |
| 211 |
} |
| 212 |
return self::$instance; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Constructor - private to enforce singleton |
| 217 |
*/ |
| 218 |
private function __construct() { |
| 219 |
$this->init_core(); |
| 220 |
$this->init_modules(); |
| 221 |
$this->init_hooks(); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Prevent cloning |
| 226 |
*/ |
| 227 |
private function __clone() {} |
| 228 |
|
| 229 |
/** |
| 230 |
* Prevent unserializing |
| 231 |
* |
| 232 |
* @throws Exception Always throws exception. |
| 233 |
*/ |
| 234 |
public function __wakeup() { |
| 235 |
throw new Exception( 'Cannot unserialize singleton' ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Initialize core components |
| 240 |
*/ |
| 241 |
private function init_core() { |
| 242 |
$this->database = new Vigilante_Database(); |
| 243 |
$this->settings = new Vigilante_Settings(); |
| 244 |
$this->activity_log = new Vigilante_Activity_Log( $this->settings, $this->database ); |
| 245 |
|
| 246 |
// Auto-create/update tables when DB version is outdated (handles file-only updates) |
| 247 |
if ( $this->database->needs_update() ) { |
| 248 |
$this->database->create_tables(); |
| 249 |
} |
| 250 |
|
| 251 |
// One-time cleanup: versions before 2.7.0 wrote config backups (including |
| 252 |
// wp-config.php) as files under wp-content/vigilante-backups/. Those now |
| 253 |
// live in the database, so remove anything left on disk. |
| 254 |
if ( ! get_option( 'vigilante_legacy_backups_cleaned' ) ) { |
| 255 |
Vigilante_Backup_Manager::cleanup_legacy_files(); |
| 256 |
update_option( 'vigilante_legacy_backups_cleaned', 1, false ); |
| 257 |
} |
| 258 |
|
| 259 |
// One-time migration (2.9.0): add '.css' to File Integrity's excluded |
| 260 |
// extensions on existing installs. Stylesheets are rewritten so often by |
| 261 |
// themes and optimizer plugins that they were the main post-update false |
| 262 |
// positive. New installs get it from the defaults; this brings existing |
| 263 |
// sites in line without touching any other setting. Additive, idempotent. |
| 264 |
if ( ! get_option( 'vigilante_css_exclusion_migrated' ) ) { |
| 265 |
$fi = $this->settings->get_section( 'file_integrity' ); |
| 266 |
if ( is_array( $fi ) ) { |
| 267 |
$ext = ( isset( $fi['excluded_extensions'] ) && is_array( $fi['excluded_extensions'] ) ) |
| 268 |
? $fi['excluded_extensions'] |
| 269 |
: array(); |
| 270 |
if ( ! in_array( '.css', $ext, true ) ) { |
| 271 |
$ext[] = '.css'; |
| 272 |
$fi['excluded_extensions'] = $ext; |
| 273 |
$this->settings->update_section( 'file_integrity', $fi ); |
| 274 |
} |
| 275 |
} |
| 276 |
update_option( 'vigilante_css_exclusion_migrated', 1, false ); |
| 277 |
} |
| 278 |
|
| 279 |
// One-time on upgrade to 2.9.0: drop any cached WordPress.org checksum |
| 280 |
// manifests. The new comparison is array-aware and self-corrects a cached |
| 281 |
// array-md5 value, but a manifest cached by an older version while wp.org |
| 282 |
// was still propagating a new release could otherwise keep producing |
| 283 |
// false "modified" results until it expires (up to 24h). Flushing on |
| 284 |
// upgrade guarantees a clean slate on the very release that fixes them; |
| 285 |
// the next scan refetches fresh manifests. One-time, bulk, no caching. |
| 286 |
if ( ! get_option( 'vigilante_checksum_cache_flushed_290' ) ) { |
| 287 |
global $wpdb; |
| 288 |
$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. |
| 289 |
"DELETE FROM {$wpdb->options} |
| 290 |
WHERE option_name LIKE '\\_transient\\_vigilante\\_plugin\\_checksums\\_%' |
| 291 |
OR option_name LIKE '\\_transient\\_timeout\\_vigilante\\_plugin\\_checksums\\_%' |
| 292 |
OR option_name LIKE '\\_transient\\_vigilante\\_theme\\_checksums\\_%' |
| 293 |
OR option_name LIKE '\\_transient\\_timeout\\_vigilante\\_theme\\_checksums\\_%' |
| 294 |
OR option_name LIKE '\\_transient\\_vigilante\\_core\\_checksums\\_%' |
| 295 |
OR option_name LIKE '\\_transient\\_timeout\\_vigilante\\_core\\_checksums\\_%'" |
| 296 |
); |
| 297 |
update_option( 'vigilante_checksum_cache_flushed_290', 1, false ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Initialize security modules based on settings |
| 303 |
*/ |
| 304 |
private function init_modules() { |
| 305 |
$options = $this->settings->get_all_options(); |
| 306 |
|
| 307 |
// Self-heal: a UI bug in earlier 2.4.x betas could leave a section's |
| 308 |
// top-level 'enabled' flag set to false because the section forms do |
| 309 |
// not render a checkbox for that field — saving any tab caused the |
| 310 |
// generic save handler to treat the missing field as "unchecked" and |
| 311 |
// store it as false. If the master module toggle on the Dashboard is |
| 312 |
// on but the section flag is off, restore it here so the module's |
| 313 |
// hooks can attach. Idempotent: noop on healthy installs. |
| 314 |
$sections = array( |
| 315 |
'firewall', |
| 316 |
'security_headers', |
| 317 |
'login_security', |
| 318 |
'rest_api_security', |
| 319 |
'user_security', |
| 320 |
'wp_hardening', |
| 321 |
'file_integrity', |
| 322 |
'activity_log', |
| 323 |
); |
| 324 |
$heal_changed = false; |
| 325 |
foreach ( $sections as $section_name ) { |
| 326 |
if ( ! empty( $options['modules'][ $section_name ] ) |
| 327 |
&& isset( $options[ $section_name ] ) |
| 328 |
&& is_array( $options[ $section_name ] ) |
| 329 |
&& array_key_exists( 'enabled', $options[ $section_name ] ) |
| 330 |
&& empty( $options[ $section_name ]['enabled'] ) ) { |
| 331 |
$options[ $section_name ]['enabled'] = true; |
| 332 |
$heal_changed = true; |
| 333 |
} |
| 334 |
} |
| 335 |
if ( $heal_changed ) { |
| 336 |
update_option( Vigilante_Settings::OPTION_NAME, $options ); |
| 337 |
$this->settings->clear_cache(); |
| 338 |
$options = $this->settings->get_all_options(); |
| 339 |
} |
| 340 |
|
| 341 |
// Firewall - runs early to block threats |
| 342 |
if ( ! empty( $options['modules']['firewall'] ) ) { |
| 343 |
new Vigilante_Firewall( $this->settings, $this->activity_log ); |
| 344 |
} |
| 345 |
|
| 346 |
// Security Headers - rules are applied via .htaccess, no runtime hooks needed |
| 347 |
// HTTPS Enforcer still needs runtime hooks |
| 348 |
if ( ! empty( $options['modules']['security_headers'] ) ) { |
| 349 |
new Vigilante_Https_Enforcer( $this->settings ); |
| 350 |
} |
| 351 |
|
| 352 |
// REST API Security |
| 353 |
if ( ! empty( $options['modules']['rest_api_security'] ) ) { |
| 354 |
new Vigilante_Rest_Api_Security( $this->settings ); |
| 355 |
} |
| 356 |
|
| 357 |
// User Security |
| 358 |
if ( ! empty( $options['modules']['user_security'] ) ) { |
| 359 |
new Vigilante_User_Security( $this->settings, $this->activity_log ); |
| 360 |
} |
| 361 |
|
| 362 |
// Login Security |
| 363 |
if ( ! empty( $options['modules']['login_security'] ) ) { |
| 364 |
$login_security = new Vigilante_Login_Security( $this->settings, $this->database, $this->activity_log ); |
| 365 |
|
| 366 |
// Two-Factor Authentication (only if login security module is active) |
| 367 |
new Vigilante_Two_Factor_Email( $this->settings, $this->database, $this->activity_log, $login_security ); |
| 368 |
new Vigilante_Two_Factor_TOTP( $this->settings, $this->database, $this->activity_log, $login_security ); |
| 369 |
} |
| 370 |
|
| 371 |
// WordPress Hardening (includes comments, head cleaner, feeds) |
| 372 |
if ( ! empty( $options['modules']['wp_hardening'] ) ) { |
| 373 |
new Vigilante_Comment_Security( $this->settings ); |
| 374 |
new Vigilante_Head_Cleaner( $this->settings ); |
| 375 |
new Vigilante_Feed_Manager( $this->settings ); |
| 376 |
} |
| 377 |
|
| 378 |
// File Integrity Scanner |
| 379 |
if ( ! empty( $options['modules']['file_integrity'] ) ) { |
| 380 |
new Vigilante_File_Integrity( $this->settings, $this->database, $this->activity_log ); |
| 381 |
new Vigilante_Plugin_Status( $this->settings, $this->activity_log ); |
| 382 |
} |
| 383 |
|
| 384 |
// Activity Log is always initialized (core component) |
| 385 |
// Logging is gated by the modules.activity_log toggle and per-type flags |
| 386 |
|
| 387 |
// Audit Alerts engine - an alerting layer on top of Security Audit. |
| 388 |
// Only instantiated when Security Audit is on, because it reacts to the |
| 389 |
// events the activity log records (a passive subscriber, no per-module |
| 390 |
// coupling). Both alert legs are opt-in, off by default. |
| 391 |
if ( ! empty( $options['modules']['activity_log'] ) ) { |
| 392 |
new Vigilante_Audit_Alerts( $this->settings, $this->activity_log ); |
| 393 |
} |
| 394 |
|
| 395 |
// Under Attack mode - always loaded (independent of modules) |
| 396 |
new Vigilante_Under_Attack( $this->settings, $this->activity_log ); |
| 397 |
|
| 398 |
// Admin interface |
| 399 |
if ( is_admin() ) { |
| 400 |
new Vigilante_Admin( $this->settings, $this->database, $this->activity_log ); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Initialize WordPress hooks |
| 406 |
*/ |
| 407 |
private function init_hooks() { |
| 408 |
// Plugin action links |
| 409 |
add_filter( 'plugin_action_links_' . VIGILANTE_PLUGIN_BASENAME, array( $this, 'add_action_links' ) ); |
| 410 |
|
| 411 |
// Scheduled tasks |
| 412 |
add_action( 'vigilante_daily_maintenance', array( $this, 'daily_maintenance' ) ); |
| 413 |
add_action( 'vigilante_hourly_checks', array( $this, 'hourly_checks' ) ); |
| 414 |
|
| 415 |
// AJAX handlers |
| 416 |
add_action( 'wp_ajax_vigilante_dismiss_notice', array( $this, 'ajax_dismiss_notice' ) ); |
| 417 |
|
| 418 |
// Regenerate critical file baseline after Vigilante modifies wp-config.php or .htaccess |
| 419 |
add_action( 'vigilante_critical_file_written', array( $this, 'on_critical_file_written' ) ); |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Update the critical file baseline after Vigilante writes to a monitored file |
| 424 |
* |
| 425 |
* @param string $filename File that was modified (e.g. 'wp-config.php'). |
| 426 |
*/ |
| 427 |
public function on_critical_file_written( $filename ) { |
| 428 |
if ( ! class_exists( 'Vigilante_File_Integrity' ) ) { |
| 429 |
require_once VIGILANTE_INCLUDES_DIR . 'class-file-integrity.php'; |
| 430 |
} |
| 431 |
|
| 432 |
$fi = new Vigilante_File_Integrity( $this->settings, $this->database, $this->activity_log ); |
| 433 |
$fi->update_critical_file_baseline( $filename ); |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Add plugin action links |
| 438 |
* |
| 439 |
* @param array $links Existing links. |
| 440 |
* @return array Modified links. |
| 441 |
*/ |
| 442 |
public function add_action_links( $links ) { |
| 443 |
$plugin_links = array( |
| 444 |
'<a href="' . esc_url( admin_url( 'admin.php?page=vigilante' ) ) . '">' . esc_html__( 'Security Settings', 'vigilante' ) . '</a>', |
| 445 |
); |
| 446 |
return array_merge( $plugin_links, $links ); |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Daily maintenance tasks |
| 451 |
*/ |
| 452 |
public function daily_maintenance() { |
| 453 |
// Clean old activity logs |
| 454 |
$this->activity_log->cleanup_old_logs(); |
| 455 |
|
| 456 |
// Clean old login attempts |
| 457 |
$this->database->cleanup_old_login_attempts(); |
| 458 |
|
| 459 |
// Clean expired 2FA codes and trusted devices |
| 460 |
$this->database->cleanup_expired_2fa_codes(); |
| 461 |
$this->database->cleanup_expired_trusted_devices(); |
| 462 |
|
| 463 |
// Remove sensitive files (readme.html, license.txt, licencia.txt) |
| 464 |
// WordPress core updates recreate these files, so we clean them daily |
| 465 |
$advanced = $this->settings->get_section( 'advanced' ); |
| 466 |
if ( ! empty( $advanced['remove_readme'] ) ) { |
| 467 |
$readme_path = ABSPATH . 'readme.html'; |
| 468 |
if ( file_exists( $readme_path ) ) { |
| 469 |
wp_delete_file( $readme_path ); |
| 470 |
} |
| 471 |
} |
| 472 |
if ( ! empty( $advanced['remove_license'] ) ) { |
| 473 |
$license_files = array( 'license.txt', 'licencia.txt' ); |
| 474 |
foreach ( $license_files as $license_file ) { |
| 475 |
$license_path = ABSPATH . $license_file; |
| 476 |
if ( file_exists( $license_path ) ) { |
| 477 |
wp_delete_file( $license_path ); |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
// Log maintenance |
| 483 |
$this->activity_log->log( 'system', 'maintenance', __( 'Daily maintenance completed', 'vigilante' ) ); |
| 484 |
} |
| 485 |
|
| 486 |
/** |
| 487 |
* Hourly checks |
| 488 |
*/ |
| 489 |
public function hourly_checks() { |
| 490 |
// File integrity scans are handled by the File_Integrity class own cron schedule |
| 491 |
// based on the configured scan_frequency (daily/weekly). |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* AJAX handler for dismissing notices |
| 496 |
*/ |
| 497 |
public function ajax_dismiss_notice() { |
| 498 |
check_ajax_referer( 'vigilante_dismiss_notice', 'nonce' ); |
| 499 |
|
| 500 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 501 |
wp_die( -1 ); |
| 502 |
} |
| 503 |
|
| 504 |
$notice_id = isset( $_POST['notice_id'] ) ? sanitize_key( $_POST['notice_id'] ) : ''; |
| 505 |
|
| 506 |
if ( $notice_id ) { |
| 507 |
$dismissed = get_option( 'vigilante_dismissed_notices', array() ); |
| 508 |
$dismissed[ $notice_id ] = time(); |
| 509 |
update_option( 'vigilante_dismissed_notices', $dismissed ); |
| 510 |
} |
| 511 |
|
| 512 |
wp_send_json_success(); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
* Cron handler for the weekly Security Analyzer scan. |
| 518 |
* |
| 519 |
* Resolves the shared Vigilante_Security_Analyzer (lazily; no cost when the |
| 520 |
* cron is not firing) and lets it run the scan + regression email logic. |
| 521 |
*/ |
| 522 |
function vigilante_run_analyzer_cron() { |
| 523 |
if ( ! class_exists( 'Vigilante_Security_Analyzer' ) ) { |
| 524 |
require_once VIGILANTE_INCLUDES_DIR . 'class-security-analyzer.php'; |
| 525 |
} |
| 526 |
if ( ! class_exists( 'Vigilante_Settings' ) ) { |
| 527 |
require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php'; |
| 528 |
} |
| 529 |
|
| 530 |
$settings = new Vigilante_Settings(); |
| 531 |
$activity_log = null; |
| 532 |
if ( class_exists( 'Vigilante_Activity_Log' ) && class_exists( 'Vigilante_Database' ) ) { |
| 533 |
$database = new Vigilante_Database(); |
| 534 |
$activity_log = new Vigilante_Activity_Log( $settings, $database ); |
| 535 |
} |
| 536 |
|
| 537 |
$analyzer = new Vigilante_Security_Analyzer( $settings, $activity_log ); |
| 538 |
$analyzer->cron_weekly_scan(); |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* Cron handler for the daily plugin status check. |
| 543 |
* |
| 544 |
* Resolves the shared Vigilante_Plugin_Status lazily so the daily cron has no |
| 545 |
* cost while it is not firing. |
| 546 |
*/ |
| 547 |
function vigilante_run_plugin_status_check() { |
| 548 |
if ( ! class_exists( 'Vigilante_Plugin_Status' ) ) { |
| 549 |
require_once VIGILANTE_INCLUDES_DIR . 'class-plugin-status.php'; |
| 550 |
} |
| 551 |
if ( ! class_exists( 'Vigilante_Settings' ) ) { |
| 552 |
require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php'; |
| 553 |
} |
| 554 |
|
| 555 |
$settings = new Vigilante_Settings(); |
| 556 |
$activity_log = null; |
| 557 |
if ( class_exists( 'Vigilante_Activity_Log' ) && class_exists( 'Vigilante_Database' ) ) { |
| 558 |
$database = new Vigilante_Database(); |
| 559 |
$activity_log = new Vigilante_Activity_Log( $settings, $database ); |
| 560 |
} |
| 561 |
|
| 562 |
$checker = new Vigilante_Plugin_Status( $settings, $activity_log ); |
| 563 |
$checker->run_scheduled_check(); |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Run a Security Analyzer full scan after Under Attack mode deactivates. |
| 568 |
* |
| 569 |
* Scheduled one-shot from Vigilante_Under_Attack::deactivate() so the dashboard |
| 570 |
* reflects the restored configuration with the slow HTTP/header probes the |
| 571 |
* mode prevented from running safely while it was active. |
| 572 |
*/ |
| 573 |
function vigilante_run_post_under_attack_scan() { |
| 574 |
if ( ! class_exists( 'Vigilante_Under_Attack' ) ) { |
| 575 |
require_once VIGILANTE_INCLUDES_DIR . 'class-under-attack.php'; |
| 576 |
} |
| 577 |
if ( ! class_exists( 'Vigilante_Settings' ) ) { |
| 578 |
require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php'; |
| 579 |
} |
| 580 |
|
| 581 |
$settings = new Vigilante_Settings(); |
| 582 |
$activity_log = null; |
| 583 |
if ( class_exists( 'Vigilante_Activity_Log' ) && class_exists( 'Vigilante_Database' ) ) { |
| 584 |
$database = new Vigilante_Database(); |
| 585 |
$activity_log = new Vigilante_Activity_Log( $settings, $database ); |
| 586 |
} |
| 587 |
|
| 588 |
$under_attack = new Vigilante_Under_Attack( $settings, $activity_log ); |
| 589 |
$under_attack->run_analyzer_scan( 'all' ); |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Plugin activation hook |
| 594 |
*/ |
| 595 |
function vigilante_activate() { |
| 596 |
require_once VIGILANTE_INCLUDES_DIR . 'class-database.php'; |
| 597 |
require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php'; |
| 598 |
require_once VIGILANTE_INCLUDES_DIR . 'class-backup-manager.php'; |
| 599 |
require_once VIGILANTE_INCLUDES_DIR . 'class-activator.php'; |
| 600 |
|
| 601 |
Vigilante_Activator::activate(); |
| 602 |
} |
| 603 |
register_activation_hook( __FILE__, 'vigilante_activate' ); |
| 604 |
|
| 605 |
/** |
| 606 |
* Plugin deactivation hook |
| 607 |
*/ |
| 608 |
function vigilante_deactivate() { |
| 609 |
require_once VIGILANTE_INCLUDES_DIR . 'class-database.php'; |
| 610 |
require_once VIGILANTE_INCLUDES_DIR . 'class-settings.php'; |
| 611 |
require_once VIGILANTE_INCLUDES_DIR . 'class-backup-manager.php'; |
| 612 |
require_once VIGILANTE_INCLUDES_DIR . 'class-deactivator.php'; |
| 613 |
|
| 614 |
Vigilante_Deactivator::deactivate(); |
| 615 |
} |
| 616 |
register_deactivation_hook( __FILE__, 'vigilante_deactivate' ); |
| 617 |
|
| 618 |
/** |
| 619 |
* Initialize plugin after WordPress loads |
| 620 |
*/ |
| 621 |
add_action( 'plugins_loaded', 'vigilante_load_plugin' ); |
| 622 |
|
| 623 |
/** |
| 624 |
* Helper function to get plugin instance |
| 625 |
* |
| 626 |
* @return Vigilante_Main |
| 627 |
*/ |
| 628 |
function vigilante() { |
| 629 |
return Vigilante_Main::get_instance(); |
| 630 |
} |