current_time( 'mysql' ), 'total' => 0, 'unchanged' => 0, 'changed' => 0, 'new' => 0, 'deleted' => 0, 'details' => array(), 'no_baseline' => false, 'skipped' => 0, // RC-118-05: files discovered but NOT verified (unreadable / hash failed) — a coverage gap that must mark a SCHEDULED scan incomplete, exactly like the batch scanner. // ACG-149-R36-C (pre-release audit 2026-07-27): how many findings this run had to DROP // because 'details' hit MAX_DETAILS. See add_detail(). Always present so a consumer can // read it without isset() and can never mistake "absent" for "none". 'details_dropped' => 0, ); $current_keys = array(); // normalized keys seen now $is_full = GuardPress_FS_Helpers::is_full_scan(); foreach ( $paths as $p ) { if ( is_file( $p ) ) { $files = array( new SplFileInfo( $p ) ); } elseif ( is_dir( $p ) ) { // FIX (audit 2026-06-10): CATCH_GET_CHILD so an unreadable subdir // is skipped rather than throwing and aborting the whole scan. // ACG-031-005 ADD-A (audit 2026-06-20): SELF_FIRST (not LEAVES_ONLY) // so directory entries are yielded and an unreadable/symlinked subdir // — whose children CATCH_GET_CHILD silently swallows — is COUNTED as a // coverage gap instead of vanishing (the cron path otherwise always // read green over a permission-locked subtree). $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $p, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD ); } else { $files = array(); } foreach ( $files as $file ) { // display path + normalized key if ( is_object( $file ) && method_exists( $file, 'isFile' ) ) { // ACG-031-005 ADD-A: a symlinked/unreadable directory means its // whole subtree was not walked — count it, then skip. if ( $file->isDir() ) { $dp = $file->getPathname(); // ACG-082-H3 (audit 2026-06-27, MED): an EXCLUDED/ignored directory is out of scope — do not // count a symlinked/unreadable EXCLUDED dir as a coverage gap (false "incomplete"). Apply the // scope filter BEFORE the symlink/unreadable gap accounting. if ( GuardPress_FS_Helpers::should_exclude_path( $dp ) || GuardPress_FS_Helpers::is_ignored( $dp ) ) { continue; } if ( $file->isLink() || ! @is_readable( $dp ) ) { $report['skipped']++; } continue; } if ( ! $file->isFile() ) continue; $fp = $file->getPathname(); } else { $fp = (string) $file; } // ACG-081-H2 (audit 2026-06-27, MED): apply path exclusions/ignores BEFORE the symlink coverage-gap // logic. A symlink inside an internal-artifact path (guardpress-data) or a user-excluded directory is // NOT a coverage gap — it is out of scope — so counting it as skipped produced a FALSE incomplete. if ( GuardPress_FS_Helpers::should_exclude_path( $fp ) ) continue; if ( GuardPress_FS_Helpers::is_ignored( $fp ) ) continue; // ACG-080-004 / ACG-084-002 (audit 2026-06-27, HIGH): an IN-SCOPE symlinked FILE — or a file reachable // only THROUGH a symlinked PARENT directory swapped in after discovery, or a FIFO/special file — is a // COVERAGE GAP, not a real file to verify. is_link($fp) tested only the basename, so a symlinked // ANCESTOR let realpath()/filesize()/hash_file() follow the link, hash the EXTERNAL target and record a // scan key OUTSIDE the install (and a FIFO would HANG hash_file). The shared no-symlink-component + // regular-file contract rejects all of these BEFORE any key/total/hash. Count it skipped (incomplete/amber). if ( ! self::is_safe_regular_file( $fp ) ) { $report['skipped']++; continue; } // RC-118-05: an UNREADABLE file is a coverage gap (discovered but not checked), // not a silent skip. Count it so a scheduled scan is marked incomplete (amber), // matching the manual batch scanner — otherwise the cron path always reads green. if ( ! @is_readable( $fp ) ) { $report['skipped']++; continue; } $real = function_exists('realpath') ? realpath($fp) : $fp; $key = GuardPress_FS_Helpers::normalize_path( $real ? $real : $fp ); $current_keys[ $key ] = true; $report['total']++; // FIX (audit 2026-06-10): cap size before hashing (see MAX_HASH_BYTES). // ACG-137-B5: the SCHEDULED path had the identical defect as the manual one — a baselined // file padded past the cap was never compared (no 'changed') and never missed (current_keys // was already set, so no 'deleted'), i.e. it silently left integrity monitoring. Same model // as class-batch-scanner.php: the cap decides whether we HASH, not whether the file matters; // a baseline entry WITHOUT the 'oversize' marker was provably <= cap when indexed, so finding // it over the cap now is a proven change. Kept byte-for-byte consistent with the manual path // so the two can never disagree about the same file again (see class-cron.php). $fsize = @filesize( $fp ); $in_baseline = isset( $baseline[ $key ] ); $was_oversize = $in_baseline && ! empty( $baseline[ $key ]['oversize'] ); if ( false !== $fsize && $fsize > self::MAX_HASH_BYTES ) { $report['skipped_large'] = ( isset( $report['skipped_large'] ) ? $report['skipped_large'] : 0 ) + 1; if ( $in_baseline && ! $was_oversize ) { $report['changed']++; $gp_nb = wp_normalize_path( $fp ); // ACG-137-B5 (self-review round 2): MAX_DETAILS guard. Every other details write in // both scanners is bounded; this one was not — and it is on the CRON path, which is // exactly the binlog bloat MAX_DETAILS exists to prevent. My comment above claimed // this branch was "byte-for-byte consistent with the manual path"; it was the one // place they differed. Caught reviewing my own diff. // ACG-149-R36-C: the bound now goes through add_detail(), which COUNTS what it drops. self::add_detail( $report, $gp_nb, array( 'status' => 'changed', 'note' => __( 'This file grew past the 50 MB integrity limit, so its contents are no longer checked. A code file that suddenly becomes this large is worth a look.', 'ai-cyberguard' ), 'baseline_hash' => isset( $baseline[ $key ]['hash'] ) ? $baseline[ $key ]['hash'] : '', 'current_hash' => '', 'mtime' => ( $gp_mtb = @filemtime( $fp ) ) ? (int) $gp_mtb : 0, ) ); } continue; } $hash = hash_file( 'sha256', $fp ); // RC-118-05: a file that vanished/became unreadable between discovery and // hashing (hash_file returns false) is a coverage gap, not "unchanged" — // count it as skipped and move on rather than comparing a false hash. if ( false === $hash ) { $report['skipped']++; continue; } // Normalize path for consistent storage (Windows compatibility) $normalized_fp = wp_normalize_path( $fp ); if ( $in_baseline ) { if ( $was_oversize ) { // ACG-137-B5: was over the cap at baseline time, now under it — a proven change, and // explicitly NOT 'new' (it existed all along; it just was not indexed). Stated // explicitly rather than relying on the '' !== $hash comparison below happening to // land on 'changed' with an empty baseline_hash and a misleading generic note. $report['changed']++; self::add_detail( $report, $normalized_fp, array( 'status' => 'changed', 'note' => __( 'This file used to be too large to check and is now small enough — so it has changed. Its contents are being checked again from now on.', 'ai-cyberguard' ), 'baseline_hash' => '', 'current_hash' => $hash, 'mtime' => ( $gp_mts = @filemtime( $fp ) ) ? (int) $gp_mts : 0, ) ); } elseif ( $baseline[ $key ]['hash'] === $hash ) { $report['unchanged']++; } else { $mtime = @filemtime( $fp ); $report['changed']++; // FIX: Cap details to prevent large DB writes (binlog bloat from CRON) self::add_detail( $report, $normalized_fp, array( 'status' => 'changed', 'note' => self::status_hint( $fp, 'changed' ), 'baseline_hash' => $baseline[ $key ]['hash'], 'current_hash' => $hash, 'mtime' => $mtime ? (int)$mtime : 0, ) ); } } elseif ( ! $has_baseline ) { // No baseline at all — "not in baseline" is true for every file. // Count it but emit NO new-file finding; flag the report so the UI // prompts the user to create a baseline. $report['unchanged']++; $report['no_baseline'] = true; } else { $report['new']++; self::add_detail( $report, $normalized_fp, array( 'status' => 'new', 'note' => self::status_hint( $fp, 'new' ), ) ); } } } // Deleted = in baseline but not in current scan, but: // * In Regular mode: DO NOT count files that current mode would exclude anyway. foreach ( $baseline as $bkey => $bdata ) { $display = isset( $bdata['path'] ) ? $bdata['path'] : $bkey; // Respect global exclusions and mode-specific exclusions // (should_exclude_path already handles Regular vs Full mode internally) if ( GuardPress_FS_Helpers::should_exclude_path( $display ) ) { continue; } if ( ! isset( $current_keys[ $bkey ] ) ) { $report['deleted']++; self::add_detail( $report, wp_normalize_path( $display ), array( 'status' => 'deleted' ) ); } } update_option( self::OPTION_LAST_REPORT, $report, false ); return $report; } /** * ACG-149-R36-C (pre-release audit 2026-07-27) — A DROPPED FINDING MUST STILL BE COUNTED. * * MAX_DETAILS caps 'details' at 2500 rows to keep a cron write out of binlog-bloat territory. * That cap is right; being SILENT about it was not. Five sites each did * `if ( count( $report['details'] ) < self::MAX_DETAILS )` and simply skipped the row, while * the counter beside it ($report['changed'] / ['new'] / ['deleted']) still incremented. On a * site with 2,701 new files the report therefore read `new = 2701, details = 2500`, and: * * - the email listed the first 2500 and mentioned nothing about the other 201; * - Cron::malware_paths_from_report() reads 'details', so those 201 files were never * malware-scanned either — a backdoor among them was invisible in BOTH engines; * - no counter anywhere in the report named the gap, so nothing downstream could report it. * * This is the round-32 lesson (R31-002) in a place we had not looked: a bound the user cannot * see is indistinguishable from coverage. One primitive now owns the decision so the five sites * cannot drift apart, and every drop lands in 'details_dropped', which the notifier prints as a * cause of incomplete coverage. * * @param array $report Report being built (by reference). * @param string $key Normalized path key. * @param array $row The finding row. */ private static function add_detail( &$report, $key, $row ) { if ( ! isset( $report['details'] ) || ! is_array( $report['details'] ) ) { $report['details'] = array(); } if ( count( $report['details'] ) < self::MAX_DETAILS ) { $report['details'][ $key ] = $row; return; } $report['details_dropped'] = ( isset( $report['details_dropped'] ) ? (int) $report['details_dropped'] : 0 ) + 1; } private static function status_hint( $path, $status ){ $l = strtolower( str_replace('\\','/',$path) ); if ( strpos($l,'/wp-content/plugins/')!==false || strpos($l,'/wp-content/themes/')!==false || strpos($l,'/wp-includes/')!==false ) { return 'Likely a legitimate WordPress/plugin/theme update'; } return ''; } public static function get_last_report(){ return get_option(self::OPTION_LAST_REPORT, array()); } }