= self::MAX_ENTRIES ) { break; } $entry = self::path_entry( $row, $entries, $runtime_reserved ); if ( $entry ) { $entries[] = $entry; } } return self::sort_longest_first( $entries ); } /** * The blog section, from the top-level policy fields, with exactly the * checks the single-prefix proxy applied: enabled, not held back, an https * origin, and a prefix that is not the site root. * * @param array $policy Decoded policy. * @return array|null */ public static function blog_entry( $policy ) { if ( ! is_array( $policy ) || empty( $policy['enabled'] ) ) { return null; } if ( array_key_exists( 'agentSurfaceReady', $policy ) && ! $policy['agentSurfaceReady'] ) { return null; } $origin_base = isset( $policy['originBase'] ) ? (string) $policy['originBase'] : ''; if ( '' === $origin_base || 0 !== strpos( $origin_base, 'https://' ) ) { return null; } $prefix = isset( $policy['prefix'] ) ? '/' . trim( (string) $policy['prefix'], '/' ) : self::DEFAULT_PREFIX; if ( '/' === $prefix ) { return null; } $excludes = ( isset( $policy['excludePaths'] ) && is_array( $policy['excludePaths'] ) ) ? $policy['excludePaths'] : array(); return array( 'id' => self::BLOG_ID, 'is_blog' => true, 'prefix' => $prefix, // as configured (cache key input) 'lprefix' => strtolower( $prefix ), // for matching 'origin_base' => $origin_base, // raw (cache key input) 'sitemap_url' => rtrim( $origin_base, '/' ) . $prefix . '/sitemap.xml', 'custom_sitemap' => false, 'cache_seconds' => isset( $policy['cacheSeconds'] ) ? max( 0, (int) $policy['cacheSeconds'] ) : self::DEFAULT_CACHE, 'exclude_paths' => $excludes, ); } /** * One additional section from the `paths` list, or null when it is invalid. * * @param mixed $row Raw entry. * @param array $existing Entries accepted so far. * @param array $runtime_reserved Store paths that may not be claimed. * @return array|null */ public static function path_entry( $row, $existing = array(), $runtime_reserved = array() ) { if ( ! is_array( $row ) || ! isset( $row['enabled'] ) || true !== $row['enabled'] ) { return null; } $prefix = self::normalize_prefix( isset( $row['prefix'] ) ? $row['prefix'] : '' ); if ( null === $prefix || self::is_reserved_prefix( $prefix ) ) { return null; } foreach ( (array) $runtime_reserved as $taken ) { if ( self::overlaps( $prefix, strtolower( rtrim( (string) $taken, '/' ) ) ) ) { return null; } } foreach ( $existing as $e ) { if ( self::overlaps( $prefix, $e['lprefix'] ) ) { return null; // never share or nest inside another section (incl. the blog) } } $origin_base = isset( $row['originBase'] ) && is_string( $row['originBase'] ) ? $row['originBase'] : ''; if ( ! self::is_valid_origin( $origin_base ) ) { return null; } $id = isset( $row['id'] ) && is_string( $row['id'] ) ? strtolower( $row['id'] ) : ''; if ( '' === $id ) { $id = str_replace( '/', '-', ltrim( $prefix, '/' ) ); } if ( ! preg_match( self::ID_RE, $id ) || self::BLOG_ID === $id ) { return null; } foreach ( $existing as $e ) { if ( $e['id'] === $id ) { return null; } } $default_sitemap = rtrim( $origin_base, '/' ) . $prefix . '/sitemap.xml'; $sitemap_url = $default_sitemap; if ( isset( $row['sitemapUrl'] ) && null !== $row['sitemapUrl'] && '' !== $row['sitemapUrl'] ) { if ( ! is_string( $row['sitemapUrl'] ) || ! self::is_valid_origin( $row['sitemapUrl'] ) ) { return null; // a sitemap we cannot trust means no allow-list at all } $sitemap_url = $row['sitemapUrl']; } $cache = self::DEFAULT_CACHE; if ( isset( $row['cacheSeconds'] ) && is_numeric( $row['cacheSeconds'] ) ) { $cache = min( self::MAX_CACHE, max( 0, (int) $row['cacheSeconds'] ) ); } $excludes = array(); if ( isset( $row['excludePaths'] ) && is_array( $row['excludePaths'] ) ) { foreach ( array_slice( $row['excludePaths'], 0, self::MAX_EXCLUDES ) as $ex ) { if ( is_string( $ex ) && '' !== $ex && '/' === $ex[0] ) { $excludes[] = strtolower( $ex ); } } } return array( 'id' => $id, 'is_blog' => false, 'prefix' => $prefix, 'lprefix' => $prefix, 'origin_base' => $origin_base, 'sitemap_url' => $sitemap_url, 'custom_sitemap' => ( $sitemap_url !== $default_sitemap ), 'cache_seconds' => $cache, 'exclude_paths' => $excludes, ); } /** * Lower-case, single leading slash, no trailing slash; null unless it * matches PREFIX_RE. * * @param mixed $raw Raw prefix. * @return string|null */ public static function normalize_prefix( $raw ) { if ( ! is_string( $raw ) ) { return null; } $p = '/' . trim( strtolower( trim( $raw ) ), '/' ); return preg_match( self::PREFIX_RE, $p ) ? $p : null; } /** * True when the prefix's first segment is a WordPress/WooCommerce route. * * @param string $prefix Normalised prefix. * @return bool */ public static function is_reserved_prefix( $prefix ) { $parts = explode( '/', ltrim( (string) $prefix, '/' ) ); $first = strtolower( $parts[0] ); if ( '' === $first || 0 === strpos( $first, 'wp-' ) ) { return true; } return in_array( $first, self::RESERVED, true ); } /** * Segment-aware overlap: equal, or one nested under the other. * * @param string $a Lower-case path, no trailing slash. * @param string $b Lower-case path, no trailing slash. * @return bool */ public static function overlaps( $a, $b ) { if ( '' === $a || '' === $b ) { return false; // the site root is never a section, and never blocks one } return $a === $b || 0 === strpos( $a, $b . '/' ) || 0 === strpos( $b, $a . '/' ); } /** * HTTPS only, a real host name, no query string, fragment or credentials. * * @param mixed $url Candidate origin or sitemap URL. * @return bool */ public static function is_valid_origin( $url ) { if ( ! is_string( $url ) || strlen( $url ) > 300 || 1 !== preg_match( self::ORIGIN_RE, $url, $m ) ) { return false; } if ( isset( $m[1] ) && '' !== $m[1] ) { $port = (int) $m[1]; if ( $port < 1 || $port > 65535 ) { return false; } } return true; } /** * Sort longest prefix first (stable for equal lengths). * * @param array $entries Entries. * @return array */ public static function sort_longest_first( $entries ) { $indexed = array(); foreach ( array_values( $entries ) as $i => $e ) { $indexed[] = array( $i, $e ); } usort( $indexed, static function ( $x, $y ) { $d = strlen( $y[1]['lprefix'] ) - strlen( $x[1]['lprefix'] ); return 0 !== $d ? $d : $x[0] - $y[0]; } ); $out = array(); foreach ( $indexed as $pair ) { $out[] = $pair[1]; } return $out; } /** * The section a request path belongs to, or null. Entries must already be * sorted longest first (build_entries does this). * * @param array $entries Entries. * @param string $path Request path (any case, trailing slash allowed). * @return array|null */ public static function match( $entries, $path ) { $lpath = self::normalize_path( $path ); foreach ( (array) $entries as $e ) { if ( $lpath === $e['lprefix'] || 0 === strpos( $lpath, $e['lprefix'] . '/' ) ) { return $e; } } return null; } /** * Lower-case, no trailing slash (the form sitemap paths are stored in). * * @param string $path Path. * @return string */ public static function normalize_path( $path ) { return strtolower( rtrim( (string) $path, '/' ) ); } /** * True when the path is excluded by the hard list or the entry's own list. * Plain prefix comparison, as the single-prefix proxy always did. * * @param string $lpath Normalised request path. * @param array $entry Entry. * @param array $always_exclude Hard-coded exclusions. * @return bool */ public static function is_excluded( $lpath, $entry, $always_exclude ) { $extra = isset( $entry['exclude_paths'] ) && is_array( $entry['exclude_paths'] ) ? $entry['exclude_paths'] : array(); foreach ( array_merge( (array) $always_exclude, $extra ) as $ex ) { $ex = strtolower( (string) $ex ); if ( '' !== $ex && 0 === strpos( $lpath, $ex ) ) { return true; } } return false; } /** * True for `{prefix}/sitemap.xml`, which the origin serves but does not list * in itself, so it bypasses the allow-list. * * @param string $lpath Normalised request path. * @param array $entry Entry. * @return bool */ public static function is_section_sitemap( $lpath, $entry ) { return $lpath === $entry['lprefix'] . '/sitemap.xml'; } /** * Cache key for a section's sitemap allow-list. The blog keeps the exact key * it has always used, so an update never discards a warm cache. * * @param array $entry Entry. * @return string */ public static function paths_cache_key( $entry ) { if ( ! empty( $entry['is_blog'] ) ) { return self::PATHS_PREFIX . md5( $entry['origin_base'] . '|' . $entry['prefix'] ); } return self::PATHS_PREFIX . md5( 'entry|' . $entry['id'] . '|' . $entry['origin_base'] . '|' . $entry['prefix'] . '|' . $entry['sitemap_url'] ); } /** * Cache key marking a recent sitemap fetch failure for a section. * * @param array $entry Entry. * @return string */ public static function paths_fail_key( $entry ) { return self::PATHS_FAIL_PREFIX . md5( $entry['sitemap_url'] ); } /** * Body cache key for a request: the path plus the query string with * tracking parameters removed. A request without tracking parameters gets * exactly the key it always had. * * @param string $path Request path, as received. * @param string $query Raw query string (no leading `?`). * @return string */ public static function body_cache_key( $path, $query ) { return self::BODY_PREFIX . md5( $path . '?' . self::cache_query( $query ) ); } /** * Remove utm_* and click-id parameters from a raw query string, keeping the * remaining parameters byte-for-byte and in order. * * @param string $query Raw query string. * @return string */ public static function cache_query( $query ) { $query = (string) $query; if ( '' === $query ) { return ''; } $kept = array(); foreach ( explode( '&', $query ) as $pair ) { if ( '' === $pair ) { continue; } $eq = strpos( $pair, '=' ); $name = strtolower( rawurldecode( false === $eq ? $pair : substr( $pair, 0, $eq ) ) ); if ( 0 === strpos( $name, 'utm_' ) || in_array( $name, self::TRACKING_PARAMS, true ) ) { continue; } $kept[] = $pair; } if ( count( $kept ) === count( array_filter( explode( '&', $query ), 'strlen' ) ) ) { return $query; // nothing removed: return it untouched } return implode( '&', $kept ); } }