0 ) { $mtime = @filemtime( $file ); if ( $mtime && ( time() - $mtime ) > $lifespan ) { return; // Expired – let WordPress regenerate it. } } // Correct keys for intermediate caches (proxy/CDN): with a separate mobile cache the // User-Agent is part of the key. Because of Cache-Control: no-cache the browser // always revalidates (fast 304), so after a content update no stale HTML // is stuck in the browser's heuristic cache. $vary = ! empty( $config['cache_mobile_separate'] ) ? 'Accept-Encoding, User-Agent' : 'Accept-Encoding'; header( 'Vary: ' . $vary ); header( 'Cache-Control: no-cache' ); // 304 Not Modified support. $mtime = @filemtime( $file ); if ( $mtime ) { $last_modified = gmdate( 'D, d M Y H:i:s', $mtime ) . ' GMT'; header( 'Last-Modified: ' . $last_modified ); $ims = isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? strtotime( (string) filter_var( $_SERVER['HTTP_IF_MODIFIED_SINCE'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH ) ) : false; if ( $ims && $ims >= $mtime ) { header( 'X-Koci-Cache: HIT' ); header( 'HTTP/1.1 304 Not Modified' ); exit; } } // phpcs:enable // Static gzip HTML is not served. In some hosting/proxy/PHP zlib // environments it causes double compression or broken Content-Encoding handling, // which the browser shows as raw, unreadable characters. // The plain HTML is returned; the web server/PHP can take care of compression. // Serves the plain HTML. header( 'Content-Type: text/html; charset=UTF-8' ); header( 'X-Koci-Cache: HIT' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- The complete HTML page that WordPress generated earlier; no WordPress functions exist yet. echo @file_get_contents( $file ); exit; } /** * Reads the config written by the plugin (null when there is none). * * @return array|null */ function koci_cache_config() { $config_file = koci_cache_config_file(); if ( ! @is_readable( $config_file ) ) { return null; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Runs before WordPress loads; a local file. $config = json_decode( (string) @file_get_contents( $config_file ), true ); return is_array( $config ) ? $config : null; } /** * Path of the rules file in the uploads folder, filled in by the plugin when it installs this drop-in. * * @return string */ function koci_cache_config_file() { return '__KOCI_CACHE_CONFIG_FILE__'; } /** * Cleans a request URI: drops every character that is not valid in a URL (the same set esc_url_raw() keeps). * * @param string $uri Request URI. * @return string */ function koci_cache_clean_uri( $uri ) { $uri = str_replace( ' ', '%20', ltrim( (string) $uri ) ); $uri = (string) preg_replace( '|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\[\]\x80-\xff]|i', '', $uri ); return '' !== $uri ? $uri : '/'; } /** * Cleans a host name (copy of Koci_Cache_Filesystem::clean_host()). * * @param string $host Host. * @return string */ function koci_cache_clean_host( $host ) { return strtolower( (string) preg_replace( '/[^A-Za-z0-9.\-:\[\]]/', '', (string) $host ) ); } /** * The user agent of the request, without control characters. * * @return string */ function koci_cache_user_agent() { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Runs before WordPress adds slashes. return isset( $_SERVER['HTTP_USER_AGENT'] ) ? (string) filter_var( $_SERVER['HTTP_USER_AGENT'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH ) : ''; } /** * The query key of the request (copy of Koci_Cache_Filesystem::query_key()): '' without a query string, * '-q' for allowed keys with simple values, false when the request must not be cached. * * @param array $allowed Allowed keys. * @return string|false */ function koci_cache_request_query_key( $allowed ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Read-only: only builds a cache file name; runs before WordPress adds slashes. if ( empty( $_GET ) ) { return ''; } $pairs = array(); foreach ( array_keys( $_GET ) as $key ) { if ( ! in_array( $key, $allowed, true ) || ! isset( $_GET[ $key ] ) ) { return false; } $value = filter_var( $_GET[ $key ], FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR ); if ( ! is_string( $value ) || ! preg_match( '/^[A-Za-z0-9_.~,+\-]{0,200}$/', $value ) ) { return false; } $pairs[ $key ] = $value; } // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash ksort( $pairs ); return '-q' . substr( md5( http_build_query( $pairs ) ), 0, 12 ); } /** * Whether the request is https (same logic as the generator). * * @return bool */ function koci_cache_is_https() { // phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Runs before WordPress adds slashes. if ( isset( $_SERVER['HTTPS'] ) && in_array( strtolower( (string) filter_var( $_SERVER['HTTPS'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW ) ), array( 'on', '1' ), true ) ) { return true; } if ( isset( $_SERVER['SERVER_PORT'] ) && 443 === (int) $_SERVER['SERVER_PORT'] ) { return true; } if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === strtolower( (string) filter_var( $_SERVER['HTTP_X_FORWARDED_PROTO'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW ) ) ) { return true; } // phpcs:enable WordPress.Security.ValidatedSanitizedInput.MissingUnslash return false; } /** * Mobile detection (same logic as the generator). * * @return bool */ function koci_cache_is_mobile() { return (bool) preg_match( '/(android|iphone|ipod|blackberry|iemobile|opera mini|mobile)/i', koci_cache_user_agent() ); } /** * Cache file name (copy of Koci_Cache_Filesystem::filename). * * @param bool $is_https Https. * @param bool $is_mobile Mobile. * @param bool $gzip Gzip. * @param string $query_key Query key ('' = none). * @return string */ function koci_cache_filename( $is_https, $is_mobile, $gzip, $query_key = '' ) { $name = 'index'; if ( $is_https ) { $name .= '-https'; } if ( $is_mobile ) { $name .= '-mobile'; } if ( '' !== $query_key ) { $name .= $query_key; } $name .= '.html'; if ( $gzip ) { $name .= '_gzip'; } return $name; } /** * Host + URI -> cache folder (copy of Koci_Cache_Filesystem::path_for). * * @param string $cache_dir Cache root (with a trailing slash). * @param string $uri Request URI. * @return string */ function koci_cache_path_for( $cache_dir, $uri ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Runs before WordPress adds slashes. $host = isset( $_SERVER['HTTP_HOST'] ) ? koci_cache_clean_host( filter_var( $_SERVER['HTTP_HOST'], FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW ) ) : ''; $host = koci_cache_sanitize_segment( '' !== $host ? $host : 'localhost', true ); $path = $uri; $pos = strpos( $path, '?' ); if ( false !== $pos ) { $path = substr( $path, 0, $pos ); } $path = rawurldecode( $path ); $path = trim( $path, '/' ); $segments = array(); if ( '' !== $path ) { foreach ( explode( '/', $path ) as $seg ) { if ( '' === $seg || '.' === $seg || '..' === $seg ) { continue; } $segments[] = koci_cache_sanitize_segment( $seg, false ); } } $dir = rtrim( $cache_dir, '/' ) . '/' . $host . '/'; if ( $segments ) { $dir .= implode( '/', $segments ) . '/'; } return $dir; } /** * Segment cleaning (copy of Koci_Cache_Filesystem::sanitize_segment) – injective, rawurlencode. * * @param string $segment Segment. * @param bool $is_host Whether it is the host. * @return string */ function koci_cache_sanitize_segment( $segment, $is_host = false ) { $segment = str_replace( "\0", '', $segment ); if ( $is_host ) { $segment = strtolower( $segment ); } $enc = rawurlencode( $segment ); if ( strlen( $enc ) > 180 ) { $enc = substr( $enc, 0, 150 ) . '-' . substr( md5( $segment ), 0, 16 ); } return $enc; } // Start. koci_cache_serve(); }