] * : Create a private JSON file outside the WordPress root. Existing files are never overwritten. * * @param list $args Positional arguments; none are accepted. * @param array $assoc_args Named arguments. */ public function preflight( array $args, array $assoc_args ): void { if ( array() !== $args || array() !== array_diff( array_keys( $assoc_args ), array( 'output' ) ) ) { \WP_CLI::error( __( 'The preflight command accepts only an optional output path.', '320px-site-audit' ) ); } $repository = null; $preflight = null; try { global $wpdb; $repository = new ImportChallengeRepository( $wpdb ); $preflight = $repository->issue( get_current_user_id() ); $json = wp_json_encode( $preflight, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); if ( false === $json ) { throw new \RuntimeException( 'server_preflight_encode_failed' ); } if ( isset( $assoc_args['output'] ) ) { ( new PrivateJsonWriter() )->write( (string) $assoc_args['output'], $json . "\n" ); \WP_CLI::success( __( 'The one-time private preflight file was created. It expires in 15 minutes.', '320px-site-audit' ) ); return; } \WP_CLI::line( $json ); } catch ( Throwable $exception ) { if ( $repository instanceof ImportChallengeRepository && is_array( $preflight ) ) { try { $repository->revoke( $preflight['site_ref'], $preflight['challenge'] ); } catch ( Throwable $cleanup_exception ) { // The command remains closed and generic if local cleanup itself fails. unset( $cleanup_exception ); } } \WP_CLI::error( __( 'The one-time server import preflight could not be created.', '320px-site-audit' ) ); } } /** * Run bounded local server diagnostics and save the result in this site. * * ## OPTIONS * * [--profile=] * : auto, shared, vps, or container. Auto never guesses shared versus VPS. * * [--language=] * : en or ru. Defaults to the current site language. * * [--format=] * : summary or json. * * [--output=] * : Create a private JSON file outside the WordPress root. Existing files are never overwritten. * * @subcommand server-scan * * @param list $args Positional arguments; none are accepted. * @param array $assoc_args Named arguments. */ public function server_scan( array $args, array $assoc_args ): void { if ( array() !== $args ) { \WP_CLI::error( __( 'This command does not accept positional arguments.', '320px-site-audit' ) ); } $allowed = array( 'profile', 'language', 'format', 'output' ); if ( array() !== array_diff( array_keys( $assoc_args ), $allowed ) ) { \WP_CLI::error( __( 'An unknown server-scan option was provided.', '320px-site-audit' ) ); } $profile = isset( $assoc_args['profile'] ) ? (string) $assoc_args['profile'] : 'auto'; $language = isset( $assoc_args['language'] ) ? (string) $assoc_args['language'] : $this->site_language(); $format = isset( $assoc_args['format'] ) ? (string) $assoc_args['format'] : 'summary'; if ( ! in_array( $profile, array( 'auto', 'shared', 'vps', 'container' ), true ) || ! in_array( $language, array( 'en', 'ru' ), true ) || ! in_array( $format, array( 'summary', 'json' ), true ) ) { \WP_CLI::error( __( 'The profile, language, or format value is invalid.', '320px-site-audit' ) ); } if ( isset( $assoc_args['output'] ) && 'json' !== $format ) { \WP_CLI::error( __( 'The output option requires --format=json.', '320px-site-audit' ) ); } try { global $wpdb; $service = new ServerAuditService( $wpdb, new AuditRegistry( plugin_dir_path( PX320_SITE_AUDIT_FILE ) . 'registry/audit-registry.v1.json' ) ); $result = $service->run( $profile, $language, get_current_user_id() ); $payload = array( 'schema_version' => '1.0.0', 'kind' => 'server_run_export', 'plugin_version' => PX320_SITE_AUDIT_VERSION, 'run_uuid' => $result['run_uuid'], 'profile' => $result['profile'], 'status' => $result['status'], 'findings' => $result['findings'], ); $json = wp_json_encode( $payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); if ( false === $json ) { throw new \RuntimeException( 'server_export_encode_failed' ); } if ( isset( $assoc_args['output'] ) ) { ( new PrivateJsonWriter() )->write( (string) $assoc_args['output'], $json . "\n" ); \WP_CLI::success( __( 'The private server report was created. Review it before sharing.', '320px-site-audit' ) ); return; } if ( 'json' === $format ) { \WP_CLI::line( $json ); return; } \WP_CLI::success( sprintf( /* translators: 1: run UUID, 2: observed hosting profile. */ __( 'Server scan completed locally. Run: %1$s. Profile: %2$s. No report data was transmitted.', '320px-site-audit' ), $result['run_uuid'], $result['profile'] ) ); } catch ( Throwable $exception ) { \WP_CLI::error( __( 'The server scan could not be completed. No external request was made.', '320px-site-audit' ) ); } } /** * Import one validated standalone server artifact into this local site. * * ## OPTIONS * * * : Absolute path to a private artifact outside the WordPress root. * * [--language=] * : en or ru. Defaults to the current site language. * * @param list $args Exact artifact path. * @param array $assoc_args Named arguments. */ public function import( array $args, array $assoc_args ): void { if ( 1 !== count( $args ) || array() !== array_diff( array_keys( $assoc_args ), array( 'language' ) ) ) { \WP_CLI::error( __( 'Provide exactly one absolute artifact path and an optional language.', '320px-site-audit' ) ); } $language = isset( $assoc_args['language'] ) ? (string) $assoc_args['language'] : $this->site_language(); if ( ! in_array( $language, array( 'en', 'ru' ), true ) ) { \WP_CLI::error( __( 'The report language must be en or ru.', '320px-site-audit' ) ); } try { global $wpdb; $artifact = ( new StandaloneServerArtifact() )->read_artifact( (string) $args[0] ); ( new ImportChallengeRepository( $wpdb ) )->consume( $artifact['site_ref'], $artifact['challenge'], $artifact['created_at'] ); $service = new ServerAuditService( $wpdb, new AuditRegistry( plugin_dir_path( PX320_SITE_AUDIT_FILE ) . 'registry/audit-registry.v1.json' ) ); $result = $service->run_observations( $artifact['observations'], $language, get_current_user_id() ); \WP_CLI::success( sprintf( /* translators: %s: run UUID. */ __( 'The standalone observations were imported once into local run %s. No report data was transmitted.', '320px-site-audit' ), $result['run_uuid'] ) ); } catch ( Throwable $exception ) { \WP_CLI::error( __( 'The standalone artifact was rejected or could not be imported. No external request was made.', '320px-site-audit' ) ); } } /** * Match the complete Wordfence scanner feed to local components without * transmitting the component inventory. * * The command requires an administrator selected through WP-CLI's global * `--user` option. Put the API key in `PX320_WORDFENCE_TOKEN`; never pass it * on the command line. Without `--confirm-network`, this command only prints * the disclosure and makes zero requests. * * ## OPTIONS * * [--language=] * : en or ru. Defaults to the current site language. * * [--confirm-network] * : Confirm one request to the fixed Wordfence v3 scanner-feed endpoint. * * [--format=] * : summary or json. * * @param list $args Positional arguments; none are accepted. * @param array $assoc_args Named arguments. */ public function vulnerabilities( array $args, array $assoc_args ): void { if ( array() !== $args || array() !== array_diff( array_keys( $assoc_args ), array( 'language', 'confirm-network', 'format' ) ) ) { \WP_CLI::error( __( 'The vulnerability command accepts only language, format, and the network confirmation flag.', '320px-site-audit' ) ); } $language = isset( $assoc_args['language'] ) ? (string) $assoc_args['language'] : $this->site_language(); $format = isset( $assoc_args['format'] ) ? (string) $assoc_args['format'] : 'summary'; if ( ! in_array( $language, array( 'en', 'ru' ), true ) || ! in_array( $format, array( 'summary', 'json' ), true ) ) { \WP_CLI::error( __( 'The report language or output format is invalid.', '320px-site-audit' ) ); } if ( ! current_user_can( 'manage_options' ) || get_current_user_id() < 1 ) { \WP_CLI::error( __( 'Select an administrator with the global --user option before an external check.', '320px-site-audit' ) ); } \WP_CLI::line( __( 'Recipient: www.wordfence.com. Purpose: download the complete v3 scanner feed and match it locally.', '320px-site-audit' ) ); \WP_CLI::line( __( 'Sent: network address and transport metadata, the 320px Site Audit user agent, and your Wordfence API credential. Not sent: site URL, plugin/theme inventory, versions, report, or personal data.', '320px-site-audit' ) ); \WP_CLI::line( __( 'The API key is read once from PX320_WORDFENCE_TOKEN and is not stored. Wordfence terms and privacy policy apply.', '320px-site-audit' ) ); if ( ! isset( $assoc_args['confirm-network'] ) ) { \WP_CLI::success( __( 'Preview only: zero requests were made. Add --confirm-network after reviewing this disclosure.', '320px-site-audit' ) ); return; } $api_key = getenv( 'PX320_WORDFENCE_TOKEN' ); if ( ! is_string( $api_key ) || '' === $api_key ) { \WP_CLI::error( __( 'PX320_WORDFENCE_TOKEN is missing. The plugin never stores this credential.', '320px-site-audit' ) ); } try { global $wpdb; $registry = new AuditRegistry( plugin_dir_path( PX320_SITE_AUDIT_FILE ) . 'registry/audit-registry.v1.json' ); $builder = new ExternalFindingBuilder( new FindingFactory( $registry, new FindingContract() ) ); $client = new WordfenceFeedClient( PX320_SITE_AUDIT_VERSION ); $service = new ExternalAuditService( new RunRepository( $wpdb, new RunStateMachine() ), new FindingRepository( $wpdb, new FindingContract() ), new ExternalOperationRepository( $wpdb, ( new LocalHashKey() )->derive() ), $registry, $builder, new TransactionManager( $wpdb ) ); $result = $service->execute( 'external.vulnerability_intelligence', $language, get_current_user_id(), ExternalController::DISCLOSURE_VERSION, array( 'request_transport_metadata', 'authorization_bearer_credential' ), 'wordfence_scanner_feed_v3', function ( string $timestamp ) use ( $api_key, $client, $builder ): array { $path = $client->download( $api_key ); try { $match = ( new WordfenceVulnerabilityMatcher() )->match_file( $path, ( new VulnerabilityInventory() )->snapshot() ); return $builder->vulnerabilities( $match, $timestamp ); } finally { $client->delete( $path ); } } ); unset( $api_key ); if ( 'json' === $format ) { $json = wp_json_encode( $result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); if ( false === $json ) { throw new \RuntimeException( 'vulnerability_result_encode_failed' ); } \WP_CLI::line( $json ); return; } if ( 'error' === $result['finding']['status'] ) { \WP_CLI::line( sprintf( /* translators: %s: local run UUID. */ __( 'The external request or feed adapter failed safely. An error finding was saved as run %s; no remote error body, site inventory, report, or API key was stored.', '320px-site-audit' ), $result['run_uuid'] ) ); return; } \WP_CLI::success( sprintf( /* translators: %s: local run UUID. */ __( 'The feed was matched locally and saved as run %s. No site inventory or report was transmitted.', '320px-site-audit' ), $result['run_uuid'] ) ); } catch ( Throwable $exception ) { unset( $api_key ); \WP_CLI::error( __( 'The vulnerability operation could not be completed safely. No remote error body or API key was stored.', '320px-site-audit' ) ); } } private function site_language(): string { $locale = function_exists( 'get_locale' ) ? get_locale() : 'en_US'; return 0 === strpos( strtolower( $locale ), 'ru' ) ? 'ru' : 'en'; } }