*/ private $path_source; /** @var callable(string,string):bool */ private $path_signal; /** @var callable(string):mixed */ private $normalize_path; private string $hash_key; private WordPressPathProvider $paths; /** * @param callable():array|null $path_source Testable path allowlist. * @param callable(string,string):bool|null $path_signal Testable filesystem metadata observer. * @param callable(string):mixed|null $normalize_path Testable path normalizer. */ public function __construct( FindingFactory $findings, ?callable $path_source = null, ?callable $path_signal = null, ?callable $normalize_path = null, ?string $hash_key = null, ?WordPressPathProvider $paths = null ) { $this->findings = $findings; $this->path_source = $path_source ?? array( $this, 'wordpress_paths' ); $this->path_signal = $path_signal ?? static function ( string $signal, string $path ): bool { switch ( $signal ) { case 'exists': return file_exists( $path ); case 'readable': return is_readable( $path ); case 'writable': // Direct metadata inspection is deliberately read-only; WP_Filesystem may request credentials. // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable return is_writable( $path ); default: return false; } }; $this->normalize_path = $normalize_path ?? static function ( string $path ): string { return wp_normalize_path( $path ); }; $this->hash_key = $hash_key ?? ( new LocalHashKey() )->derive(); $this->paths = $paths ?? new WordPressPathProvider(); } public function id(): string { return 'environment.storage_paths'; } /** * @return array */ public function execute( string $timestamp ): array { try { $paths = call_user_func( $this->path_source ); } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } if ( ! is_array( $paths ) ) { return $this->error_finding( $timestamp ); } $path_hashes = array(); $masked_paths = array(); $signals = array(); $failures = 0; $warnings = 0; try { foreach ( $paths as $entry ) { if ( ! is_array( $entry ) || ! isset( $entry['type'], $entry['path'], $entry['required'], $entry['writable_expected'] ) || ! is_string( $entry['type'] ) || ! is_string( $entry['path'] ) || '' === $entry['path'] ) { continue; } $normalized = call_user_func( $this->normalize_path, $entry['path'] ); if ( ! is_string( $normalized ) || '' === $normalized ) { continue; } $exists = (bool) call_user_func( $this->path_signal, 'exists', $entry['path'] ); $readable = (bool) call_user_func( $this->path_signal, 'readable', $entry['path'] ); $writable = (bool) call_user_func( $this->path_signal, 'writable', $entry['path'] ); $path_hashes[] = array( 'type' => $entry['type'], 'hash' => hash_hmac( 'sha256', $normalized, $this->hash_key ), ); $masked_paths[] = array( 'type' => $entry['type'], 'label' => '{' . str_replace( '_', '-', $entry['type'] ) . '}', ); $signals[] = array( 'type' => $entry['type'], 'exists' => $exists, 'readable' => $readable, 'writable' => $writable, 'required' => (bool) $entry['required'], 'writable_expected' => (bool) $entry['writable_expected'], ); if ( (bool) $entry['required'] && ( ! $exists || ! $readable ) ) { ++$failures; } elseif ( (bool) $entry['writable_expected'] && ( ! $exists || ! $writable ) ) { ++$warnings; } } } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } $observed = count( $signals ); $total = count( $paths ); if ( 0 === $observed ) { $status = 'not_tested'; $coverage = 'none'; } elseif ( $failures > 0 ) { $status = 'fail'; $coverage = $observed === $total ? 'complete' : 'partial'; } elseif ( $warnings > 0 ) { $status = 'warning'; $coverage = $observed === $total ? 'complete' : 'partial'; } elseif ( $observed < $total ) { $status = 'not_tested'; $coverage = 'partial'; } else { $status = 'pass'; $coverage = 'complete'; } return $this->findings->build( $this->id(), $status, array( 'path_hashes' => $path_hashes, 'masked_paths' => $masked_paths, 'signal_types' => $signals, 'observed_count' => $observed, 'truncated' => false, 'limitations' => array( 'Only allowlisted directory metadata was observed. Raw paths, directory contents and file contents were not stored.' ), ), $observed, $total, $coverage, $timestamp ); } /** * @return array */ private function wordpress_paths(): array { $paths = array( array( 'type' => 'wordpress_root', 'path' => $this->paths->wordpress_root(), 'required' => true, 'writable_expected' => false, ), array( 'type' => 'content_root', 'path' => $this->paths->content_root(), 'required' => true, 'writable_expected' => false, ), array( 'type' => 'plugins_root', 'path' => $this->paths->plugins_root(), 'required' => true, 'writable_expected' => false, ), array( 'type' => 'themes_root', 'path' => $this->paths->theme_roots()[0] ?? '', 'required' => true, 'writable_expected' => false, ), ); $uploads_root = $this->paths->uploads_root(); if ( '' !== $uploads_root ) { $paths[] = array( 'type' => 'uploads_root', 'path' => $uploads_root, 'required' => false, 'writable_expected' => true, ); } return $paths; } /** * @return array */ private function error_finding( string $timestamp ): array { return $this->findings->build( $this->id(), 'error', array( 'path_hashes' => array(), 'masked_paths' => array(), 'signal_types' => array(), 'observed_count' => 0, 'truncated' => false, 'limitations' => array( 'The allowlisted path observer failed; no path or exception content was stored.' ), ), 0, 0, 'error', $timestamp ); } }