count( $old_lines ), 'new_lines' => count( $new_lines ), 'added' => 0, 'removed' => 0, 'changed' => 0, 'hunks' => array(), ); // Simple line-by-line comparison $max_lines = max( count( $old_lines ), count( $new_lines ) ); $current_hunk = null; for ( $i = 0; $i < $max_lines; $i++ ) { $old_line = isset( $old_lines[ $i ] ) ? $old_lines[ $i ] : null; $new_line = isset( $new_lines[ $i ] ) ? $new_lines[ $i ] : null; $type = 'unchanged'; if ( $old_line === null && $new_line !== null ) { $type = 'added'; $diff['added']++; } elseif ( $old_line !== null && $new_line === null ) { $type = 'removed'; $diff['removed']++; } elseif ( $old_line !== $new_line ) { $type = 'changed'; $diff['changed']++; } if ( $type !== 'unchanged' ) { // Start new hunk or add to existing if ( $current_hunk === null ) { $current_hunk = array( 'start_line' => $i + 1, 'lines' => array(), ); } $current_hunk['lines'][] = array( 'line_num' => $i + 1, 'type' => $type, 'old' => $old_line, 'new' => $new_line, ); } else { // Close current hunk if exists if ( $current_hunk !== null ) { $diff['hunks'][] = $current_hunk; $current_hunk = null; } } } // Close final hunk if ( $current_hunk !== null ) { $diff['hunks'][] = $current_hunk; } return $diff; } /** * Render diff as HTML * * @param string $filepath Path to file * @param string|null $baseline_content Content from baseline (null if new file) * @param string|null $current_content Current content (null if deleted) * @return string HTML */ public static function render_diff_html( $filepath, $baseline_content, $current_content ) { $html = '
'; // Header $html .= '
'; $html .= '' . esc_html( basename( $filepath ) ) . ''; $html .= '' . esc_html( $filepath ) . ''; $html .= '
'; // Handle new/deleted files if ( $baseline_content === null ) { // New file $html .= '
'; $html .= '+ ' . esc_html__( 'New file added', 'ai-cyberguard' ) . ''; $html .= '
'; $html .= self::render_code_block( $current_content, 'added' ); } elseif ( $current_content === null ) { // Deleted file $html .= '
'; $html .= '- ' . esc_html__( 'File deleted', 'ai-cyberguard' ) . ''; $html .= '
'; $html .= self::render_code_block( $baseline_content, 'removed' ); } else { // Changed file - show diff $diff = self::generate_diff( $baseline_content, $current_content ); // Stats bar $html .= '
'; $html .= '+' . esc_html( $diff['added'] ) . ' ' . esc_html__( 'added', 'ai-cyberguard' ) . ''; $html .= '-' . esc_html( $diff['removed'] ) . ' ' . esc_html__( 'removed', 'ai-cyberguard' ) . ''; $html .= '~' . esc_html( $diff['changed'] ) . ' ' . esc_html__( 'changed', 'ai-cyberguard' ) . ''; $html .= '
'; // Render hunks if ( empty( $diff['hunks'] ) ) { $html .= '
'; $html .= esc_html__( 'No visible differences (possibly whitespace changes)', 'ai-cyberguard' ); $html .= '
'; } else { $html .= '
'; foreach ( $diff['hunks'] as $hunk ) { $html .= '
'; $html .= '
'; /* translators: 1: first line number of the diff hunk, 2: last line number of the diff hunk */ $html .= sprintf( esc_html__( 'Lines %1$d-%2$d', 'ai-cyberguard' ), $hunk['start_line'], $hunk['start_line'] + count( $hunk['lines'] ) - 1 ); $html .= '
'; foreach ( $hunk['lines'] as $line ) { $html .= self::render_diff_line( $line ); } $html .= '
'; } $html .= '
'; } } $html .= '
'; return $html; } /** * Render a single diff line */ private static function render_diff_line( $line ) { $html = ''; switch ( $line['type'] ) { case 'added': $html .= '
'; $html .= '' . esc_html( $line['line_num'] ) . ''; $html .= '+'; $html .= '' . esc_html( $line['new'] ?? '' ) . ''; $html .= '
'; break; case 'removed': $html .= '
'; $html .= '' . esc_html( $line['line_num'] ) . ''; $html .= '-'; $html .= '' . esc_html( $line['old'] ?? '' ) . ''; $html .= '
'; break; case 'changed': // Show old line $html .= '
'; $html .= '' . esc_html( $line['line_num'] ) . ''; $html .= '-'; $html .= '' . esc_html( $line['old'] ?? '' ) . ''; $html .= '
'; // Show new line $html .= '
'; $html .= '' . esc_html( $line['line_num'] ) . ''; $html .= '+'; $html .= '' . esc_html( $line['new'] ?? '' ) . ''; $html .= '
'; break; } return $html; } /** * Render code block (for new/deleted files) */ private static function render_code_block( $content, $type = 'normal' ) { $lines = explode( "\n", $content ); $max_lines = min( count( $lines ), 50 ); // Limit display $bg_color = $type === 'added' ? '#064e3b' : ( $type === 'removed' ? '#7f1d1d' : '#1e1e1e' ); $text_color = $type === 'added' ? '#34d399' : ( $type === 'removed' ? '#f87171' : '#e5e7eb' ); $line_bg = $type === 'added' ? '#065f46' : ( $type === 'removed' ? '#991b1b' : '#2d2d2d' ); $line_color = $type === 'added' ? '#6ee7b7' : ( $type === 'removed' ? '#fca5a5' : '#6b7280' ); $html = '
'; for ( $i = 0; $i < $max_lines; $i++ ) { $html .= '
'; $html .= '' . ( $i + 1 ) . ''; $html .= '' . esc_html( $lines[ $i ] ) . ''; $html .= '
'; } if ( count( $lines ) > $max_lines ) { $html .= '
'; /* translators: %d is the number of additional lines not shown */ $html .= sprintf( esc_html__( '... and %d more lines', 'ai-cyberguard' ), count( $lines ) - $max_lines ); $html .= '
'; } $html .= '
'; return $html; } /** * Get file content from baseline * * @param string $filepath Relative path * @return string|null Content or null if not found */ public static function get_baseline_content( $filepath ) { // FIX: Use Baseline::get() instead of direct DB read (avoids binlog bloat) $baseline = class_exists( 'GuardPress_Baseline' ) ? GuardPress_Baseline::get() : array(); if ( empty( $baseline ) ) { return null; } // Baseline stores hash/mtime/size but not file content. // Full content storage would need Time Machine (PRO feature). // This is a placeholder for now. return null; } /** * Get current file content */ public static function get_current_content( $filepath ) { $abspath = ABSPATH; $full_path = $abspath . ltrim( $filepath, '/' ); // ACG-082-H2 / ACG-084-H1 (audit 2026-06-27, MED): never return the content of a SYMLINK, a file behind a // symlinked PARENT, or a non-regular file — it would expose the bytes of an EXTERNAL file outside the install // in the admin visual-diff view. ACG-084-H1: adopt the SHARED host-aware no-follow contract when available // (tolerates a symlink AT a host-published trusted root, refuses one deeper); the v83 lstat + ABSPATH // containment below is the fallback. if ( class_exists( 'GuardPress_FS_Helpers' ) && method_exists( 'GuardPress_FS_Helpers', 'regular_file_in_abspath_no_symlink_components' ) ) { if ( ! GuardPress_FS_Helpers::regular_file_in_abspath_no_symlink_components( $full_path ) ) { return null; // symlink component / non-regular / outside install } } else { clearstatcache( false, $full_path ); if ( @is_link( $full_path ) || ( @file_exists( $full_path ) && ! @is_file( $full_path ) ) ) { return null; } $rp = @realpath( $full_path ); $ra = @realpath( $abspath ); if ( false === $rp || false === $ra || 0 !== strpos( str_replace( '\\', '/', $rp ), rtrim( str_replace( '\\', '/', $ra ), '/' ) . '/' ) ) { return null; // resolves outside the install } } if ( ! file_exists( $full_path ) || ! is_readable( $full_path ) ) { return null; } // Limit file size if ( filesize( $full_path ) > 512 * 1024 ) { return __( '(File too large to display - over 512KB)', 'ai-cyberguard' ); } return file_get_contents( $full_path ); } /** * Render inline diff (highlight changes within line) */ public static function highlight_inline_diff( $old, $new ) { // Simple word-level diff $old_words = preg_split( '/(\s+)/', $old, -1, PREG_SPLIT_DELIM_CAPTURE ); $new_words = preg_split( '/(\s+)/', $new, -1, PREG_SPLIT_DELIM_CAPTURE ); $old_html = ''; $new_html = ''; // Use simple LCS for inline diff // This is simplified - for production would use proper diff algorithm return array( 'old' => $old_html ?: esc_html( $old ), 'new' => $new_html ?: esc_html( $new ), ); } }