|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BmltEnabled\Mayo\Rest\Helpers; |
| 4 | + |
| 5 | +if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | + |
| 7 | +/** |
| 8 | + * Validates that a BMLT root server URL is well-formed and reachable. |
| 9 | + * |
| 10 | + * Performs cheap checks first (format, scheme) before an outbound handshake |
| 11 | + * against the BMLT semantic endpoint to confirm the URL actually points at a |
| 12 | + * BMLT root server. |
| 13 | + */ |
| 14 | +class RootServerValidator { |
| 15 | + |
| 16 | + /** |
| 17 | + * Timeout, in seconds, for the outbound reachability handshake. |
| 18 | + * |
| 19 | + * Kept shorter than the read-path lookup in ServiceBodyLookup (15s) so a |
| 20 | + * slow host can't hang a settings save for long. |
| 21 | + */ |
| 22 | + const TIMEOUT = 10; |
| 23 | + |
| 24 | + /** |
| 25 | + * Validate and normalize a BMLT root server URL. |
| 26 | + * |
| 27 | + * @param string $url The candidate root server URL. |
| 28 | + * @return string|\WP_Error Normalized URL (no trailing slash) on success, |
| 29 | + * or a WP_Error describing why validation failed. |
| 30 | + */ |
| 31 | + public static function validate( $url ) { |
| 32 | + $url = is_string( $url ) ? trim( $url ) : ''; |
| 33 | + |
| 34 | + if ( $url === '' ) { |
| 35 | + return new \WP_Error( |
| 36 | + 'invalid_root_server_url', |
| 37 | + __( 'Please enter a BMLT root server URL.', 'mayo-events-manager' ), |
| 38 | + [ 'status' => 400 ] |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // 1. Format — must be a valid absolute URL. |
| 43 | + $normalized = untrailingslashit( esc_url_raw( $url ) ); |
| 44 | + |
| 45 | + if ( empty( $normalized ) || ! wp_http_validate_url( $normalized ) ) { |
| 46 | + return new \WP_Error( |
| 47 | + 'invalid_root_server_url', |
| 48 | + sprintf( |
| 49 | + /* translators: %s: the URL the user entered. */ |
| 50 | + __( '"%s" is not a valid URL.', 'mayo-events-manager' ), |
| 51 | + $url |
| 52 | + ), |
| 53 | + [ 'status' => 400 ] |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + // 2. Scheme — require https. |
| 58 | + $scheme = strtolower( (string) wp_parse_url( $normalized, PHP_URL_SCHEME ) ); |
| 59 | + if ( $scheme !== 'https' ) { |
| 60 | + return new \WP_Error( |
| 61 | + 'invalid_root_server_url', |
| 62 | + sprintf( |
| 63 | + /* translators: %s: the URL the user entered. */ |
| 64 | + __( 'The BMLT root server URL must start with "https://" (got "%s").', 'mayo-events-manager' ), |
| 65 | + $url |
| 66 | + ), |
| 67 | + [ 'status' => 400 ] |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + // 3. Reachability — handshake against the BMLT semantic endpoint. |
| 72 | + $response = wp_remote_get( |
| 73 | + $normalized . '/client_interface/json/?switcher=GetServerInfo', |
| 74 | + [ |
| 75 | + 'timeout' => self::TIMEOUT, |
| 76 | + 'sslverify' => true, |
| 77 | + ] |
| 78 | + ); |
| 79 | + |
| 80 | + if ( is_wp_error( $response ) ) { |
| 81 | + return new \WP_Error( |
| 82 | + 'root_server_unreachable', |
| 83 | + sprintf( |
| 84 | + /* translators: %s: the BMLT root server URL. */ |
| 85 | + __( 'Could not reach a BMLT root server at %s', 'mayo-events-manager' ), |
| 86 | + $normalized |
| 87 | + ), |
| 88 | + [ 'status' => 400 ] |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + $code = (int) wp_remote_retrieve_response_code( $response ); |
| 93 | + if ( $code !== 200 ) { |
| 94 | + return new \WP_Error( |
| 95 | + 'root_server_unreachable', |
| 96 | + sprintf( |
| 97 | + /* translators: 1: the BMLT root server URL, 2: the HTTP status code returned. */ |
| 98 | + __( 'Could not reach a BMLT root server at %1$s (HTTP %2$d).', 'mayo-events-manager' ), |
| 99 | + $normalized, |
| 100 | + $code |
| 101 | + ), |
| 102 | + [ 'status' => 400 ] |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 107 | + if ( ! self::looks_like_server_info( $data ) ) { |
| 108 | + return new \WP_Error( |
| 109 | + 'not_a_bmlt_root_server', |
| 110 | + sprintf( |
| 111 | + /* translators: %s: the BMLT root server URL. */ |
| 112 | + __( 'The URL %s did not respond like a BMLT root server.', 'mayo-events-manager' ), |
| 113 | + $normalized |
| 114 | + ), |
| 115 | + [ 'status' => 400 ] |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + return $normalized; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Determine whether a decoded GetServerInfo response looks like a BMLT |
| 124 | + * root server. GetServerInfo returns a single-element list of objects |
| 125 | + * carrying a `version` field, e.g. [ { "version": "3.0.0", ... } ]. |
| 126 | + * |
| 127 | + * @param mixed $data Decoded JSON response body. |
| 128 | + * @return bool |
| 129 | + */ |
| 130 | + private static function looks_like_server_info( $data ) { |
| 131 | + if ( ! is_array( $data ) ) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + if ( isset( $data['version'] ) ) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + return isset( $data[0] ) && is_array( $data[0] ) && isset( $data[0]['version'] ); |
| 140 | + } |
| 141 | +} |
0 commit comments