*/ private $visibility_source; private WordPressPathProvider $paths; /** * @param callable():array|null $visibility_source Testable coarse server observer. */ public function __construct( FindingFactory $findings, ?callable $visibility_source = null, ?WordPressPathProvider $paths = null ) { $this->findings = $findings; $this->visibility_source = $visibility_source ?? array( $this, 'php_visibility' ); $this->paths = $paths ?? new WordPressPathProvider(); } public function id(): string { return 'environment.server_visibility'; } /** * @return array */ public function execute( string $timestamp ): array { try { $visibility = call_user_func( $this->visibility_source ); } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } if ( ! is_array( $visibility ) ) { return $this->error_finding( $timestamp ); } $observations = array(); $this->append_enum( $observations, $visibility, 'php_sapi', self::SAPI_VALUES ); $this->append_enum( $observations, $visibility, 'os_family', self::OS_VALUES ); $this->append_enum( $observations, $visibility, 'web_server_family', self::WEB_VALUES ); $this->append_enum( $observations, $visibility, 'document_root_state', self::ROOT_VALUES ); $this->append_boolean( $observations, $visibility, 'php_ini_loaded' ); $this->append_boolean( $observations, $visibility, 'process_user_api_available' ); $observed = count( $observations ); $total = 6; if ( isset( $visibility['php_sapi'] ) && 'unavailable' === $visibility['php_sapi'] ) { $status = 'fail'; $coverage = $observed === $total ? 'complete' : 'partial'; } elseif ( 0 === $observed ) { $status = 'not_tested'; $coverage = 'none'; } elseif ( $observed < $total ) { $status = 'warning'; $coverage = 'partial'; } else { $status = 'pass'; $coverage = 'complete'; } return $this->findings->build( $this->id(), $status, array( 'observations' => $observations, 'method' => 'coarse_php_visible_server_metadata', 'limitations' => array( 'No hostname, IP, server banner/version, username, document-root path, PHP ini path or hosting-type guess was stored. CLI/server coverage is separate.' ), ), $observed, $total, $coverage, $timestamp ); } /** * @param list> $observations Validated observations. * @param array $source Source values. * @param list $allowed Allowed coarse values. */ private function append_enum( array &$observations, array $source, string $field, array $allowed ): void { if ( isset( $source[ $field ] ) && is_string( $source[ $field ] ) && in_array( $source[ $field ], $allowed, true ) ) { $observations[] = array( 'field' => $field, 'value' => $source[ $field ], ); } } /** * @param list> $observations Validated observations. * @param array $source Source values. */ private function append_boolean( array &$observations, array $source, string $field ): void { if ( isset( $source[ $field ] ) && is_bool( $source[ $field ] ) ) { $observations[] = array( 'field' => $field, 'value' => $source[ $field ], ); } } /** * @return array{php_sapi:string,os_family:string,web_server_family:string,document_root_state:string,php_ini_loaded:bool,process_user_api_available:bool} */ private function php_visibility(): array { $document_root_value = isset( $_SERVER['DOCUMENT_ROOT'] ) && is_string( $_SERVER['DOCUMENT_ROOT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ) : ''; $server_software = isset( $_SERVER['SERVER_SOFTWARE'] ) && is_string( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : ''; $document_root = '' !== $document_root_value ? realpath( $document_root_value ) : false; $wordpress_root = realpath( $this->paths->wordpress_root() ); $root_state = 'document_root_unavailable'; if ( false !== $document_root && false !== $wordpress_root ) { $document_root = rtrim( wp_normalize_path( $document_root ), '/' ) . '/'; $wordpress_root = rtrim( wp_normalize_path( $wordpress_root ), '/' ) . '/'; $root_state = 0 === strpos( $wordpress_root, $document_root ) ? 'wordpress_inside_document_root' : 'wordpress_outside_document_root'; } return array( 'php_sapi' => $this->sapi_family( PHP_SAPI ), 'os_family' => $this->os_family( PHP_OS_FAMILY ), 'web_server_family' => $this->web_server_family( $server_software ), 'document_root_state' => $root_state, 'php_ini_loaded' => false !== php_ini_loaded_file(), 'process_user_api_available' => function_exists( 'get_current_user' ) || function_exists( 'posix_geteuid' ), ); } private function sapi_family( string $sapi ): string { $sapi = strtolower( $sapi ); if ( false !== strpos( $sapi, 'fpm' ) ) { return 'fpm'; } if ( false !== strpos( $sapi, 'cgi' ) ) { return 'cgi'; } if ( false !== strpos( $sapi, 'apache' ) ) { return 'apache'; } if ( false !== strpos( $sapi, 'lite' ) ) { return 'litespeed'; } if ( 'cli-server' === $sapi ) { return 'cli_server'; } return in_array( $sapi, array( 'cli', 'embed' ), true ) ? $sapi : 'other'; } private function os_family( string $family ): string { $family = strtolower( $family ); return in_array( $family, self::OS_VALUES, true ) ? $family : 'unknown'; } private function web_server_family( string $banner ): string { if ( '' === $banner ) { return 'unavailable'; } $banner = strtolower( $banner ); foreach ( array( 'openresty', 'nginx', 'apache', 'litespeed', 'caddy' ) as $family ) { if ( false !== strpos( $banner, $family ) ) { return $family; } } return false !== strpos( $banner, 'microsoft-iis' ) ? 'iis' : 'other'; } /** * @return array */ private function error_finding( string $timestamp ): array { return $this->findings->build( $this->id(), 'error', array( 'observations' => array(), 'method' => 'coarse_php_visible_server_metadata', 'limitations' => array( 'The local server visibility adapter failed; no exception, banner, user or path was stored.' ), ), 0, 6, 'error', $timestamp ); } }