\n"; public static string $reason = "not-evaluated"; public static function path(string $uri): string { if ( $uri === "" || strlen($uri) > 2048 || $uri[0] !== "/" || str_starts_with($uri, "//") || strpbrk($uri, "?#\\\r\n\0") !== false ) { return ""; } $decoded = rawurldecode($uri); if (preg_match('~[\x00-\x20\\\\?#%]|(?:^|/)\.\.?(/|$)|//~', $decoded)) { return ""; } return $uri; } /** WordPress has not added magic quotes at drop-in time. Reject ambiguous paths instead of normalizing them into a valid cache key. */ public static function request_path(array $server): string { return isset($server["REQUEST_URI"]) && is_string($server["REQUEST_URI"]) ? self::path($server["REQUEST_URI"]) : ""; } public static function reason(array $s, array $c): string { if (empty($c["enabled"])) { return "disabled"; } if (!empty($c["conflict"])) { return "cache-provider-conflict"; } if (($s["REQUEST_METHOD"] ?? "") !== "GET") { return "method-not-get"; } if (!empty($s["HTTP_COOKIE"])) { return "cookie-session"; } if ( !empty($s["HTTP_AUTHORIZATION"]) || !empty($s["REDIRECT_HTTP_AUTHORIZATION"]) || !empty($s["PHP_AUTH_USER"]) ) { return "authorization"; } if ( !empty($s["HTTP_X_WP_NONCE"]) || !empty($s["REMOTE_USER"]) || !empty($s["REDIRECT_REMOTE_USER"]) || !empty($s["HTTP_X_REQUESTED_WITH"]) || !empty($s["HTTP_X_HTTP_METHOD_OVERRIDE"]) ) { return "private-request"; } if (!empty($s["HTTP_RANGE"])) { return "range-request"; } if ( !empty($s["HTTP_X_FORWARDED_HOST"]) || !empty($s["HTTP_X_ORIGINAL_URL"]) || !empty($s["HTTP_X_REWRITE_URL"]) ) { return "ambiguous-forwarding"; } $scheme = !empty($s["HTTPS"]) && strtolower((string) $s["HTTPS"]) !== "off" ? "https" : "http"; if ( strtolower((string) ($s["HTTP_HOST"] ?? "")) !== ($c["host"] ?? "") || $scheme !== ($c["scheme"] ?? "") ) { return "origin-mismatch"; } $uri = (string) ($s["REQUEST_URI"] ?? ""); if (str_contains($uri, "?")) { return "query-string"; } $path = self::path($uri); if ($path === "") { return "invalid-path"; } if ( preg_match( '~(?:^|/)(?:wp-admin|wp-json|wp-login\.php|wp-cron\.php|xmlrpc\.php|wp-content|wp-includes|feed|comments|search|cart|checkout|my-account)(?:/|$)~i', rawurldecode($path), ) ) { return "protected-path"; } foreach ($c["exclude"] ?? [] as $prefix) { if ( $path === $prefix || str_starts_with($path, rtrim($prefix, "/") . "/") ) { return "excluded-path"; } } if (!in_array($path, $c["paths"] ?? [], true)) { return "not-approved-public-page"; } return "eligible"; } public static function atomic(string $file, string $data, bool $create_only = false): bool { $tmp = $file . "." . bin2hex(random_bytes(6)) . ".tmp.php"; if (file_put_contents($tmp, $data, LOCK_EX) !== strlen($data)) { if (is_file($tmp)) {wp_delete_file($tmp);} return false; } @chmod($tmp, 0600); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod -- Private local cache file; no remote filesystem or credentials in the early-cache engine. // Atomic same-directory replacement preserves a complete old or new config; WP_Filesystem::move may delete the destination first. if ($create_only) { // Publish a complete file only if no other cache provider has taken this location. $published = function_exists('link') && @link($tmp, $file); wp_delete_file($tmp); return $published; } if (!@rename($tmp, $file)) { // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- Same-directory atomic replacement; a copy/delete can expose partial configuration. wp_delete_file($tmp); return false; } if (function_exists("opcache_invalidate")) { @opcache_invalidate($file, true); } return true; } public static function version(string $dir, string $path): string { $file = $dir . "/v-" . hash("sha256", $path) . ".php"; if (!is_file($file)) { return "0"; } return substr( (string) file_get_contents($file), strlen(self::GUARD), 64, ); } public static function key(array $c, string $path): string { return hash( "sha256", ($c["generation"] ?? "") . "|" . $c["scheme"] . "|" . $c["host"] . "|" . $path . "|" . self::version($c["dir"], $path), ); } public static function read(array $c, string $path): ?array { $f = $c["dir"] . "/p-" . self::key($c, $path) . ".php"; if (!is_file($f) || filesize($f) > 3100000) { return null; } $raw = (string) file_get_contents($f); if (!str_starts_with($raw, self::GUARD)) { return null; } $entry = json_decode(substr($raw, strlen(self::GUARD)), true); if ( !is_array($entry) || !is_int($entry["expires"] ?? null) || !is_int($entry["created"] ?? null) || $entry["expires"] <= time() || !is_string($entry["html"] ?? null) || !is_array($entry["headers"] ?? []) ) { return null; } return $entry; } public static function safe_response( string $html, array $headers, int $status, ): string { if ($status !== 200) { return "status-not-200"; } if ( strlen($html) < 80 || strlen($html) > 2000000 || !preg_match("~