0 ) { $hour = get_transient( 'svabg_alert_hour' ); $hour = is_array( $hour ) ? $hour : array( 'n' => 0, 'dropped' => 0 ); if ( $hour['n'] >= $cap ) { $hour['dropped']++; set_transient( 'svabg_alert_hour', $hour, HOUR_IN_SECONDS ); Softvolt_Ai_Bodyguard_Logger::log_throttled( 'alertcap', HOUR_IN_SECONDS, 'notice', 'alerts', sprintf( /* translators: %d: cap */ __( 'Alert limit of %d an hour reached; further alerts this hour are only logged.', 'ai-guardwall' ), $cap ) ); return false; } $hour['n']++; set_transient( 'svabg_alert_hour', $hour, HOUR_IN_SECONDS ); } $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); $subject = sprintf( '[AI GuardWall] %s – %s', $site, $subject ); $body = is_array( $lines ) ? implode( "\n", $lines ) : (string) $lines; $body .= "\n\n--\n" . sprintf( /* translators: 1: site url, 2: dashboard url */ __( "Site: %1\$s\nDashboard: %2\$s", 'ai-guardwall' ), home_url(), Softvolt_Ai_Bodyguard_Helpers::admin_url_page( 'softvoltai-ai-bodyguard' ) ); return wp_mail( $to, $subject, $body ); } public static function on_scan_finished( $scan_id, $stats ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_scan', true ) ) { return; } $counts = isset( $stats['severity'] ) ? $stats['severity'] : array(); $critical = (int) ( isset( $counts['critical'] ) ? $counts['critical'] : 0 ); $high = (int) ( isset( $counts['high'] ) ? $counts['high'] : 0 ); $total = (int) ( isset( $stats['findings'] ) ? $stats['findings'] : 0 ); if ( 0 === $total ) { if ( 'scheduled' === ( isset( $stats['type'] ) ? $stats['type'] : '' ) ) { return; // Quiet when a scheduled scan is clean. } } $lines = array(); $lines[] = sprintf( /* translators: %d: scan ID */ __( 'Scan #%d finished.', 'ai-guardwall' ), $scan_id ); $lines[] = sprintf( /* translators: %d: number */ __( 'Files scanned: %d', 'ai-guardwall' ), isset( $stats['files_scanned'] ) ? (int) $stats['files_scanned'] : 0 ); $lines[] = sprintf( /* translators: 1: total, 2: critical, 3: high */ __( 'Findings: %1$d (critical: %2$d, high: %3$d)', 'ai-guardwall' ), $total, $critical, $high ); $lines[] = ''; $top = Softvolt_Ai_Bodyguard_Findings::query( array( 'scan_id' => $scan_id, 'status' => 'open', 'per_page' => 15, 'orderby' => 'severity' ) ); foreach ( $top['rows'] as $row ) { $lines[] = sprintf( '[%s] %s — %s', strtoupper( $row['severity'] ), $row['location'], $row['title'] ); } $lines[] = ''; $lines[] = __( 'Open the Findings page to review, AI-analyse and clean them.', 'ai-guardwall' ); $lines[] = Softvolt_Ai_Bodyguard_Helpers::admin_url_page( 'softvoltai-ai-bodyguard-cleanup', array( 'tab' => 'findings' ) ); $subject = $critical > 0 ? sprintf( /* translators: %d: critical */ __( 'CRITICAL: %d critical findings', 'ai-guardwall' ), $critical ) : sprintf( /* translators: %d: total */ __( 'Scan finished: %d findings', 'ai-guardwall' ), $total ); self::send( $subject, $lines ); } public static function on_lockout( $ip, $type, $reason ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_lockout', false ) ) { return; } self::send( sprintf( /* translators: %s: IP address */ __( 'IP %s locked out', 'ai-guardwall' ), $ip ), array( sprintf( /* translators: %s: IP address */ __( 'IP: %s', 'ai-guardwall' ), $ip ), sprintf( /* translators: %s: type */ __( 'Type: %s', 'ai-guardwall' ), $type ), sprintf( /* translators: %s: reason */ __( 'Reason: %s', 'ai-guardwall' ), $reason ), sprintf( /* translators: %s: date */ __( 'Time: %s', 'ai-guardwall' ), wp_date( 'Y-m-d H:i:s' ) ), ) ); } public static function on_new_admin( $user_id, $context ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_new_admin', true ) ) { return; } $user = get_userdata( $user_id ); if ( ! $user ) { return; } self::send( __( 'New administrator account', 'ai-guardwall' ), array( __( 'An account with the Administrator role was created or promoted.', 'ai-guardwall' ), sprintf( /* translators: %s: user login */ __( 'Username: %s', 'ai-guardwall' ), $user->user_login ), sprintf( /* translators: %s: user email */ __( 'Email: %s', 'ai-guardwall' ), $user->user_email ), sprintf( /* translators: %s: context */ __( 'Context: %s', 'ai-guardwall' ), $context ), sprintf( /* translators: 1: user ID, 2: IP address */ __( 'By user ID: %1$d from IP %2$s', 'ai-guardwall' ), get_current_user_id(), Softvolt_Ai_Bodyguard_Helpers::client_ip() ), '', __( 'If you did not expect this, remove the account immediately and run a full scan.', 'ai-guardwall' ), ) ); } public static function on_file_changes( $scan_id, $changes ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_file_change', true ) || empty( $changes ) ) { return; } $lines = array( sprintf( /* translators: %d: scan ID */ __( 'File changes detected during scan #%d:', 'ai-guardwall' ), $scan_id ), '' ); $i = 0; foreach ( $changes as $change ) { $lines[] = sprintf( '%s: %s', $change['type'], $change['path'] ); if ( ++$i >= 60 ) { $lines[] = sprintf( /* translators: %d: number */ __( '…and %d more', 'ai-guardwall' ), count( $changes ) - 60 ); break; } } self::send( sprintf( /* translators: %d: number of changes */ __( '%d file changes detected', 'ai-guardwall' ), count( $changes ) ), $lines ); } public static function on_login( $user_login, $user ) { if ( ! $user instanceof WP_User ) { return; } $admin = user_can( $user, 'manage_options' ); $want = $admin ? ( Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_admin_login', false ) && Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'notify_admin_login' ) ) : (bool) Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_nonadmin_login', false ); // Remember the browsers and networks each user logs in from; with "first time only", known ones stay quiet. $device = substr( md5( Softvolt_Ai_Bodyguard_Helpers::user_agent() . '|' . preg_replace( '/[.:][^.:]*$/', '', Softvolt_Ai_Bodyguard_Helpers::client_ip() ) ), 0, 12 ); $known = get_user_meta( $user->ID, 'svabg_known_devices', true ); $known = is_array( $known ) ? $known : array(); $new = ! in_array( $device, $known, true ); if ( $new ) { $known[] = $device; update_user_meta( $user->ID, 'svabg_known_devices', array_slice( $known, -20 ) ); } if ( ! $want || ( ! $new && Softvolt_Ai_Bodyguard_Settings::get( 'alert_login_first_only', 1 ) ) ) { return; } self::send( sprintf( $admin ? /* translators: %s: value */ __( 'Administrator login: %s', 'ai-guardwall' ) : /* translators: %s: value */ __( 'Login: %s', 'ai-guardwall' ), $user_login ) . ( $new ? ' ' . __( '(new device)', 'ai-guardwall' ) : '' ), array( sprintf( /* translators: %s: user login */ __( 'User: %s', 'ai-guardwall' ), $user_login ), sprintf( /* translators: %s: IP address */ __( 'IP: %s', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::client_ip() ), sprintf( /* translators: %s: user agent */ __( 'Browser: %s', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::user_agent() ), sprintf( /* translators: %s: date */ __( 'Time: %s', 'ai-guardwall' ), wp_date( 'Y-m-d H:i:s' ) ), ) ); } public static function on_lost_password( $user_login ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_lost_password', false ) ) { return; } self::send( sprintf( /* translators: %s: user login */ __( 'Password reset requested for %s', 'ai-guardwall' ), $user_login ), array( sprintf( /* translators: %s: user login */ __( 'Someone asked for a password reset link for "%s".', 'ai-guardwall' ), $user_login ), sprintf( /* translators: %s: IP address */ __( 'IP: %s', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::client_ip() ), __( 'If nobody on your team did this, someone may be trying to take over the account. The link only goes to the account\'s own email address.', 'ai-guardwall' ), ) ); } public static function on_breached( $user ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_breached', 1 ) || ! $user instanceof WP_User ) { return; } self::send( sprintf( /* translators: %s: user login */ __( 'Login blocked: leaked password for %s', 'ai-guardwall' ), $user->user_login ), array( sprintf( /* translators: %s: user login */ __( 'The administrator "%s" tried to log in with a password that appears in known data breaches. The login was refused until the password is reset.', 'ai-guardwall' ), $user->user_login ), sprintf( /* translators: %s: IP address */ __( 'IP: %s', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::client_ip() ), ) ); } public static function on_vulnerable( $items ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_vulnerable', 1 ) || empty( $items ) ) { return; } $lines = array( __( 'The latest scan found software with publicly known security holes. Update or remove it:', 'ai-guardwall' ), '' ); foreach ( array_slice( $items, 0, 30 ) as $i ) { $lines[] = sprintf( '- %1$s %2$s: %3$s%4$s', $i['name'], $i['version'], $i['title'], $i['fixed_in'] ? ' ' . sprintf( /* translators: %s: fixed in */ __( '(fixed in %s)', 'ai-guardwall' ), $i['fixed_in'] ) : ' ' . __( '(no fix yet: consider removing it)', 'ai-guardwall' ) ); } $lines[] = ''; $lines[] = admin_url( 'update-core.php' ); self::send( sprintf( /* translators: %d: number of items */ _n( '%d vulnerable plugin, theme or core version', '%d vulnerable plugins, themes or core versions', count( $items ), 'ai-guardwall' ), count( $items ) ), $lines ); } /** * The firewall was switched off, or put in log-only mode. */ public static function on_settings_changed( $old, $new ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_firewall_off', 1 ) || ! is_array( $old ) || ! is_array( $new ) ) { return; } $was_on = ( ! isset( $old['hardening']['firewall'] ) || $old['hardening']['firewall'] ) && ( $old['firewall_mode'] ?? 'block' ) === 'block'; $now_on = ( ! isset( $new['hardening']['firewall'] ) || $new['hardening']['firewall'] ) && ( $new['firewall_mode'] ?? 'block' ) === 'block'; if ( $was_on && ! $now_on ) { $user = wp_get_current_user(); self::send( __( 'The firewall no longer blocks attacks', 'ai-guardwall' ), array( ( $new['firewall_mode'] ?? '' ) === 'log' ? __( 'The firewall was put in log-only (learning) mode.', 'ai-guardwall' ) : __( 'The firewall was switched off.', 'ai-guardwall' ), sprintf( /* translators: 1: value, 2: IP address */ __( 'By: %1$s from IP %2$s', 'ai-guardwall' ), $user && $user->exists() ? $user->user_login : __( 'unknown', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::client_ip() ), __( 'If this was not you, someone has administrator access: change every administrator password now.', 'ai-guardwall' ), ), true ); } } /** * AI GuardWall itself is being deactivated (sent before it stops). */ public static function on_self_deactivated() { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_firewall_off', 1 ) || ! function_exists( 'wp_mail' ) ) { return; } $user = wp_get_current_user(); self::send( __( 'AI GuardWall was deactivated', 'ai-guardwall' ), array( __( 'The security plugin was deactivated, so the firewall, login protection and scans have stopped.', 'ai-guardwall' ), sprintf( /* translators: 1: value, 2: IP address */ __( 'By: %1$s from IP %2$s', 'ai-guardwall' ), $user && $user->exists() ? $user->user_login : __( 'unknown', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::client_ip() ), __( 'If this was not you, someone has administrator access: reactivate the plugin, change every administrator password and run a scan.', 'ai-guardwall' ), ), true ); } public static function on_plugin_change( $plugin ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'alert_on_plugin_change', false ) ) { return; } $action = current_action(); self::send( sprintf( /* translators: 1: value, 2: plugin */ __( 'Plugin %1$s: %2$s', 'ai-guardwall' ), 'activated_plugin' === $action ? __( 'activated', 'ai-guardwall' ) : __( 'deactivated', 'ai-guardwall' ), $plugin ), array( sprintf( /* translators: %s: plugin */ __( 'Plugin: %s', 'ai-guardwall' ), $plugin ), sprintf( /* translators: 1: user ID, 2: IP address */ __( 'By user ID: %1$d from IP %2$s', 'ai-guardwall' ), get_current_user_id(), Softvolt_Ai_Bodyguard_Helpers::client_ip() ), ) ); } }