gmdate( 'Y-m-d H:i:s' ), 'level' => substr( (string) $level, 0, 12 ), 'event' => substr( (string) $event, 0, 48 ), 'ip' => substr( (string) $ip, 0, 45 ), 'user_id' => (int) $user_id, 'message' => Softvolt_Ai_Bodyguard_Helpers::trim_text( wp_strip_all_tags( (string) $message ), 2000 ), 'data' => empty( $data ) ? null : Softvolt_Ai_Bodyguard_Helpers::esc_json( $data ), ); $ok = $wpdb->insert( $table, $row, array( '%s', '%s', '%s', '%s', '%d', '%s', '%s' ) ); return $ok ? (int) $wpdb->insert_id : 0; } /** * Logs at most once per ($key, $seconds) to avoid flooding (used by the firewall). */ public static function log_throttled( $key, $seconds, $level, $event, $message, $data = array() ) { $t_key = 'svabg_lt_' . md5( $key ); if ( get_transient( $t_key ) ) { return 0; } set_transient( $t_key, 1, $seconds ); return self::log( $level, $event, $message, $data ); } public static function query( $args = array() ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'log' ); $defaults = array( 'per_page' => 50, 'page' => 1, 'event' => '', 'level' => '', 'ip' => '', 'search' => '', 'since' => '', ); $args = wp_parse_args( $args, $defaults ); $where = array( '1=1' ); $vals = array(); if ( '' !== $args['event'] ) { $where[] = 'event = %s'; $vals[] = $args['event']; } if ( '' !== $args['level'] ) { $where[] = 'level = %s'; $vals[] = $args['level']; } if ( '' !== $args['ip'] ) { $where[] = 'ip = %s'; $vals[] = $args['ip']; } if ( '' !== $args['search'] ) { $where[] = 'message LIKE %s'; $vals[] = '%' . $wpdb->esc_like( $args['search'] ) . '%'; } if ( '' !== $args['since'] ) { $where[] = 'created_at >= %s'; $vals[] = $args['since']; } $where_sql = implode( ' AND ', $where ); $offset = max( 0, ( (int) $args['page'] - 1 ) * (int) $args['per_page'] ); $limit = max( 1, min( 500, (int) $args['per_page'] ) ); $count_sql = $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE {$where_sql}", array_merge( array( $table ), $vals ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The interpolated part holds fixed column names and placeholders only; every value goes through prepare(). $rows_sql = $wpdb->prepare( "SELECT * FROM %i WHERE {$where_sql} ORDER BY id DESC LIMIT %d, %d", array_merge( array( $table ), $vals, array( $offset, $limit ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The interpolated part holds fixed column names and placeholders only; every value goes through prepare(). return array( 'total' => (int) $wpdb->get_var( $count_sql ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared 'rows' => $wpdb->get_results( $rows_sql, ARRAY_A ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared ); } public static function count_since( $event, $seconds ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'log' ); $since = gmdate( 'Y-m-d H:i:s', time() - (int) $seconds ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE event = %s AND created_at >= %s", $table, $event, $since ) ); } /** * How often an event was logged on each of the last $days days (UTC), oldest first. * * @since 1.5.0 * @param string $event Event name. * @param int $days Days, today included. * @return array 'Y-m-d' => count. */ public static function daily( $event, $days ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'log' ); $days = max( 1, (int) $days ); $out = array(); for ( $i = $days - 1; $i >= 0; $i-- ) { $out[ gmdate( 'Y-m-d', time() - $i * DAY_IN_SECONDS ) ] = 0; } $since = array_key_first( $out ) . ' 00:00:00'; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT DATE(created_at) AS d, COUNT(*) AS c FROM %i WHERE event = %s AND created_at >= %s GROUP BY DATE(created_at)", $table, $event, $since ), ARRAY_A ); foreach ( (array) $rows as $row ) { if ( isset( $out[ $row['d'] ] ) ) { $out[ $row['d'] ] = (int) $row['c']; } } return $out; } public static function purge( $days ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'log' ); $cutoff = gmdate( 'Y-m-d H:i:s', time() - max( 1, (int) $days ) * DAY_IN_SECONDS ); return $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE created_at < %s", $table, $cutoff ) ); } public static function clear() { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'log' ); return $wpdb->query( $wpdb->prepare( "TRUNCATE TABLE %i", $table ) ); } public static function events() { return array( 'login_success' => __( 'Login', 'ai-guardwall' ), 'login_failed' => __( 'Failed login', 'ai-guardwall' ), 'login_lockout' => __( 'Login lockout', 'ai-guardwall' ), 'login_2fa' => __( 'Two-factor', 'ai-guardwall' ), 'firewall_block' => __( 'Firewall block', 'ai-guardwall' ), 'firewall_log' => __( 'Firewall (log only)', 'ai-guardwall' ), 'rate_limit' => __( 'Rate limit', 'ai-guardwall' ), 'ip_blocked' => __( 'IP blocked', 'ai-guardwall' ), 'user_created' => __( 'User created', 'ai-guardwall' ), 'user_deleted' => __( 'User deleted', 'ai-guardwall' ), 'user_role_changed' => __( 'Role changed', 'ai-guardwall' ), 'user_updated' => __( 'User updated', 'ai-guardwall' ), 'new_admin' => __( 'New administrator', 'ai-guardwall' ), 'plugin_change' => __( 'Plugin change', 'ai-guardwall' ), 'theme_change' => __( 'Theme change', 'ai-guardwall' ), 'option_change' => __( 'Critical option change', 'ai-guardwall' ), 'file_edit' => __( 'File editor used', 'ai-guardwall' ), 'scan_started' => __( 'Scan started', 'ai-guardwall' ), 'scan_finished' => __( 'Scan finished', 'ai-guardwall' ), 'file_change' => __( 'File change detected', 'ai-guardwall' ), 'remediation' => __( 'Remediation', 'ai-guardwall' ), 'ai_analysis' => __( 'AI analysis', 'ai-guardwall' ), 'hardening' => __( 'Hardening change', 'ai-guardwall' ), 'settings' => __( 'Settings change', 'ai-guardwall' ), 'tool' => __( 'Tool used', 'ai-guardwall' ), 'plugin_activated' => __( 'Plugin activated', 'ai-guardwall' ), 'plugin_deactivated' => __( 'Plugin deactivated', 'ai-guardwall' ), 'upload_blocked' => __( 'Upload blocked', 'ai-guardwall' ), 'error' => __( 'Error', 'ai-guardwall' ), ); } }