'text/html; charset=utf-8', 'htm' => 'text/html; charset=utf-8', 'webmanifest' => 'application/manifest+json', 'wasm' => 'application/wasm', 'css' => 'text/css; charset=utf-8', 'js' => 'application/javascript; charset=utf-8', 'mjs' => 'application/javascript; charset=utf-8', 'json' => 'application/json; charset=utf-8', 'map' => 'application/json; charset=utf-8', 'xml' => 'application/xml; charset=utf-8', 'xsl' => 'text/xsl; charset=utf-8', 'txt' => 'text/plain; charset=utf-8', 'svg' => 'image/svg+xml', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'webp' => 'image/webp', 'avif' => 'image/avif', 'ico' => 'image/x-icon', 'bmp' => 'image/bmp', 'woff' => 'font/woff', 'woff2' => 'font/woff2', 'ttf' => 'font/ttf', 'otf' => 'font/otf', 'eot' => 'application/vnd.ms-fontobject', 'mp4' => 'video/mp4', 'webm' => 'video/webm', 'ogv' => 'video/ogg', 'ogg' => 'audio/ogg', 'mp3' => 'audio/mpeg', 'wav' => 'audio/wav', 'm4a' => 'audio/mp4', 'mov' => 'video/quicktime', 'pdf' => 'application/pdf', 'zip' => 'application/zip', 'csv' => 'text/csv; charset=utf-8', ); /** @var array|null Cached replacement map. */ private static $replacements = null; /* ------------------------------------------------------------------ * * URL helpers * ------------------------------------------------------------------ */ /** * Resolve $url against $base and return an absolute URL on the WordPress * origin (fragment removed), or null if it points elsewhere. */ public static function normalize( $url, $base = '' ) { $url = html_entity_decode( trim( (string) $url ), ENT_QUOTES | ENT_HTML5 ); $url = str_replace( '\\/', '/', $url ); if ( '' === $url || preg_match( '#^(data|mailto|tel|javascript|about|blob):#i', $url ) || '#' === $url[0] ) { return null; } $url = preg_replace( '/#.*$/', '', $url ); $source = wp_parse_url( CFSP_Settings::source_url() ); $host = strtolower( $source['host'] ); $port = isset( $source['port'] ) ? ':' . $source['port'] : ''; if ( 0 === strpos( $url, '//' ) ) { $url = $source['scheme'] . ':' . $url; } elseif ( ! preg_match( '#^[a-z][a-z0-9+.-]*://#i', $url ) ) { // Relative URL. $base_parts = wp_parse_url( $base ? $base : CFSP_Settings::source_url() . '/' ); if ( '/' === $url[0] ) { $path = $url; } else { $base_path = isset( $base_parts['path'] ) ? $base_parts['path'] : '/'; $dir = substr( $base_path, 0, strrpos( $base_path, '/' ) + 1 ); $path = $dir . $url; } $url = $source['scheme'] . '://' . $host . $port . self::remove_dot_segments( $path ); } $parts = wp_parse_url( $url ); // A link written with the static site's address (https://example.com/wp-content/uploads/x.png) // is the same file or page in WordPress: read it as the WordPress address. if ( ! empty( $parts['host'] ) && strtolower( $parts['host'] ) !== $host ) { $live = self::live_to_source( $parts ); if ( $live ) { $parts = $live; } } if ( empty( $parts['host'] ) || strtolower( $parts['host'] ) !== $host ) { return null; } if ( ( isset( $parts['port'] ) ? ':' . $parts['port'] : '' ) !== $port ) { return null; } $path = isset( $parts['path'] ) ? self::remove_dot_segments( $parts['path'] ) : '/'; $home = isset( $source['path'] ) ? rtrim( $source['path'], '/' ) : ''; if ( $home && 0 !== strpos( $path, $home ) ) { return null; } $normalized = $source['scheme'] . '://' . $host . $port . $path; if ( isset( $parts['query'] ) && '' !== $parts['query'] ) { $normalized .= '?' . $parts['query']; } return $normalized; } /** * URL parts on the static site's host (Destination URL, with or without "www."), moved to the * WordPress host and path. Null for any other host, or when no Destination URL is known. */ private static function live_to_source( array $parts ) { static $live = null; if ( null === $live ) { $live = array(); $dest = wp_parse_url( CFSP_Settings::destination_url() ); $source = wp_parse_url( CFSP_Settings::source_url() ); if ( ! empty( $dest['host'] ) && ! empty( $source['host'] ) ) { $dhost = strtolower( $dest['host'] ); $alt = 0 === strpos( $dhost, 'www.' ) ? substr( $dhost, 4 ) : 'www.' . $dhost; foreach ( array( $dhost, $alt ) as $h ) { if ( strtolower( $source['host'] ) !== $h ) { $live[ $h ] = true; } } $live = array( 'hosts' => $live, 'dpath' => isset( $dest['path'] ) ? rtrim( $dest['path'], '/' ) : '', 'spath' => isset( $source['path'] ) ? rtrim( $source['path'], '/' ) : '', 'host' => strtolower( $source['host'] ), 'port' => isset( $source['port'] ) ? $source['port'] : null, ); } } if ( empty( $live['hosts'] ) || isset( $parts['port'] ) || ! isset( $live['hosts'][ strtolower( $parts['host'] ) ] ) ) { return null; } $path = isset( $parts['path'] ) && '' !== $parts['path'] ? $parts['path'] : '/'; if ( '' !== $live['dpath'] ) { if ( $path !== $live['dpath'] && 0 !== strpos( $path, $live['dpath'] . '/' ) ) { return null; } $path = substr( $path, strlen( $live['dpath'] ) ); $path = '' === $path ? '/' : $path; } $parts['path'] = $live['spath'] . $path; $parts['host'] = $live['host']; unset( $parts['port'] ); if ( null !== $live['port'] ) { $parts['port'] = $live['port']; } return $parts; } private static function remove_dot_segments( $path ) { $out = array(); foreach ( explode( '/', $path ) as $seg ) { if ( '..' === $seg ) { array_pop( $out ); } elseif ( '.' !== $seg ) { $out[] = $seg; } } $result = implode( '/', $out ); return '' === $result || '/' !== $result[0] ? '/' . $result : $result; } /** Path of the URL relative to the WordPress home, always starting with "/". */ /** Is this redirect target a login wall in front of WordPress, e.g. Cloudflare Access? */ public static function is_login_wall( $location ) { return (bool) preg_match( '#\.cloudflareaccess\.com/|/cdn-cgi/access/(?:login|authorized)#i', (string) $location ); } public static function relative_path( $url ) { $path = (string) wp_parse_url( $url, PHP_URL_PATH ); $home = rtrim( (string) wp_parse_url( CFSP_Settings::source_url(), PHP_URL_PATH ), '/' ); if ( $home && 0 === strpos( $path, $home ) ) { $path = substr( $path, strlen( $home ) ); } return '/' . ltrim( rawurldecode( $path ), '/' ); } public static function extension( $url ) { $path = (string) wp_parse_url( $url, PHP_URL_PATH ); $base = basename( $path ); $dot = strrpos( $base, '.' ); return false === $dot ? '' : strtolower( substr( $base, $dot + 1 ) ); } public static function is_asset( $url ) { return in_array( self::extension( $url ), self::ASSET_EXT, true ); } public static function is_excluded( $url ) { $rel = self::relative_path( $url ); $q = wp_parse_url( $url, PHP_URL_QUERY ); $cmp = $rel . ( $q ? '?' . $q : '' ); if ( false !== strpos( $cmp, '/uploads/cfsp-' ) ) { return true; } // Media is served from R2, so it isn't part of the static site. if ( CFSP_Media::exclude_uploads() && 0 === strpos( $rel, CFSP_Media::uploads_rel_path() ) ) { return true; } if ( 'php' === self::extension( $url ) ) { return true; } // Pages with query strings (?replytocom=, ?share=, ...) cannot be represented as static files. if ( $q && ! self::is_asset( $url ) ) { return true; } foreach ( CFSP_Settings::lines( 'exclude_patterns' ) as $pattern ) { if ( false !== strpos( $cmp, $pattern ) ) { return true; } } // Crawler switches that can be enforced by URL shape. if ( ! CFSP_Settings::get( 'enhanced_crawl' ) ) { return (bool) apply_filters( 'cfsp_exclude_url', false, $url ); } if ( preg_match( '#/feed/?$#', $rel ) ) { if ( ! CFSP_Settings::crawler( 'feeds' ) || ( ! CFSP_Settings::get( 'comment_feeds' ) && self::is_comment_feed( $url ) ) ) { return true; } } if ( ! CFSP_Settings::crawler( 'pagination' ) && preg_match( '#/page/\d+/?$#', $rel ) ) { return true; } if ( ! CFSP_Settings::crawler( 'authors' ) ) { global $wp_rewrite; $base = $wp_rewrite && $wp_rewrite->author_base ? $wp_rewrite->author_base : 'author'; if ( 0 === strpos( $rel, '/' . $base . '/' ) ) { return true; } } return (bool) apply_filters( 'cfsp_exclude_url', false, $url ); } /** Per-post comment feeds (/my-post/feed/): usually unwanted, and they double the file count. */ private static function is_comment_feed( $url ) { static $cache = array(); if ( isset( $cache[ $url ] ) ) { return $cache[ $url ]; } $rel = self::relative_path( $url ); if ( 0 === strpos( $rel, '/comments/feed' ) ) { return $cache[ $url ] = true; } $parent = preg_replace( '#feed/?$#', '', $url ); if ( '/' === self::relative_path( $parent ) ) { return $cache[ $url ] = false; // Main feed. } return $cache[ $url ] = url_to_postid( $parent ) > 0; } /** Canonical key used to track a URL in queues (assets drop their ?ver= query). */ public static function key( $url ) { if ( self::is_asset( $url ) ) { $url = preg_replace( '/\?.*$/', '', $url ); } return $url; } /** * Map a URL to its file path inside the export directory (relative, no leading slash). * Returns null for paths that are unsafe. */ public static function url_to_file( $url, $is_html = null ) { $rel = self::relative_path( $url ); if ( false !== strpos( $rel, '..' ) || false !== strpos( $rel, "\0" ) ) { return null; } $ext = self::extension( $url ); if ( null === $is_html ) { $is_html = ! in_array( $ext, self::ASSET_EXT, true ) && 'html' !== $ext && 'htm' !== $ext; } $feed = CFSP_Feeds::feed_file( $rel ); if ( null !== $feed ) { $rel = $feed; } elseif ( '/' === substr( $rel, -1 ) ) { $rel .= 'index.html'; } elseif ( $is_html && '' === $ext ) { $rel .= '/index.html'; } return ltrim( $rel, '/' ); } /* ------------------------------------------------------------------ * * Export * ------------------------------------------------------------------ */ /** * Export one URL. * * @return array { ok: bool, removed: bool, file: string|null, links: string[], error: string|null } */ /** Export one URL. */ public static function export( $url ) { $results = self::export_many( array( $url ) ); return $results[0]; } /** * Export several URLs. Files on disk are copied; the rest are fetched in parallel * (Advanced > "Pages exported at the same time"). * * @return array[] One result per URL, in the same order. */ public static function export_many( array $urls ) { $urls = array_values( $urls ); $results = array(); $fetch = array(); $start = microtime( true ); foreach ( $urls as $i => $url ) { $local = self::export_local( $url ); if ( null !== $local ) { $results[ $i ] = $local; } else { $fetch[ $i ] = $url; } } if ( $fetch ) { $asked = microtime( true ); $responses = self::fetch_many( $fetch ); self::$timing['fetch'] += microtime( true ) - $asked; foreach ( $fetch as $i => $url ) { $results[ $i ] = self::handle_response( $url, $responses[ $i ] ?? new WP_Error( 'cfsp_http', 'No response' ) ); } } self::$timing['total'] += microtime( true ) - $start; ksort( $results ); return $results; } /** * Seconds spent in export_many() during this request: 'fetch' waiting for WordPress to answer, * 'total' including copying, processing and writing. Read and reset by the job runner. * * @var array */ public static $timing = array( 'fetch' => 0.0, 'total' => 0.0, ); private static function new_result() { return array( 'ok' => false, 'removed' => false, 'file' => null, 'links' => array(), 'error' => null, ); } /** * Fast path: real files on disk (assets, additional files) are copied instead of fetched over HTTP. * Anything else goes through HTTP so the web server's access rules still apply. * * @return array|null Result, or null when the URL has to be fetched. */ private static function export_local( $url ) { $ext = self::extension( $url ); if ( ( self::is_asset( $url ) || isset( self::MIME[ $ext ] ) ) && ! wp_parse_url( $url, PHP_URL_QUERY ) ) { $local = self::url_to_local_file( $url ); if ( $local ) { return self::store( $url, file_get_contents( $local ), self::MIME[ $ext ] ?? 'application/octet-stream', self::new_result() ); } } return null; } /** Turn a fetched page (or a fetch error) into an export result. */ /** * Addresses typed into Push > Specific pages. If WordPress has nothing there (404, redirect, * soft 404), that's a typo or a static file name, not a removed page: never delete for them. * * @var array */ public static $protect = array(); private static function missing_but_protected( $url, $result ) { $result['error'] = 'WordPress has no page at this address. Nothing was removed.'; return $result; } private static function handle_response( $url, $response ) { $result = self::new_result(); $ext = self::extension( $url ); if ( is_wp_error( $response ) ) { $result['error'] = $response->get_error_message(); return $result; } $code = (int) wp_remote_retrieve_response_code( $response ); // A redirect means nothing lives at this address (e.g. a plugin sending missing pages to the // home page). Don't save the redirect target under this name; export the target instead. if ( $code >= 300 && $code < 400 ) { $location = wp_remote_retrieve_header( $response, 'location' ); $location = is_array( $location ) ? end( $location ) : (string) $location; // A login wall in front of WordPress (Cloudflare Access) is not "this page moved": saving or // pruning anything now would wipe the static site. Stop instead. if ( self::is_login_wall( $location ) ) { $result['error'] = self::LOGIN_WALL_ERROR; $result['blocked'] = true; return $result; } if ( isset( self::$protect[ $url ] ) ) { return self::missing_but_protected( $url, $result ); } $location = remove_query_arg( array( self::NOCACHE_PARAM, 'cfsp_404' ), $location ); $target = '' !== $location ? self::normalize( $location, $url ) : null; $file = self::url_to_file( $url ); if ( $file && ( ! $target || self::url_to_file( $target ) !== $file ) ) { self::delete_file( $file ); } if ( $target && ! self::is_excluded( $target ) ) { $result['links'][] = self::key( $target ); } if ( CFSP_Settings::get( 'debug_log' ) ) { CFSP_Logger::log( sprintf( 'Skipped %s (redirects to %s)', $url, $location ) ); } $result['ok'] = true; $result['removed'] = true; return $result; } if ( ( 404 === $code || 410 === $code ) && isset( self::$protect[ $url ] ) ) { return self::missing_but_protected( $url, $result ); } if ( 404 === $code || 410 === $code ) { // Page no longer exists: make sure it disappears from the static site too. $file = self::url_to_file( $url ); if ( $file ) { self::delete_file( $file ); } $result['ok'] = true; $result['removed'] = true; return $result; } if ( $code < 200 || $code >= 300 ) { $result['error'] = 'HTTP ' . $code; return $result; } $type = strtolower( (string) wp_remote_retrieve_header( $response, 'content-type' ) ); // "Soft 404": a text/data file address (robots.txt, humans.txt, ads.txt, *.xml, *.json) // answered with an HTML page. The file doesn't really exist. if ( self::is_asset( $url ) && ! in_array( $ext, array( 'html', 'htm' ), true ) && false !== strpos( $type, 'text/html' ) ) { if ( isset( self::$protect[ $url ] ) ) { return self::missing_but_protected( $url, $result ); } $file = self::url_to_file( $url, false ); if ( $file ) { self::delete_file( $file ); } $result['ok'] = true; $result['removed'] = true; return $result; } return self::store( $url, wp_remote_retrieve_body( $response ), $type, $result ); } /** * Export the theme's real 404 page as /404.html (Cloudflare Pages serves it for missing URLs). * WordPress renders the 404 template directly for this request (CFSP_Feeds::render_404), so a * plugin that redirects missing pages to the home page can't turn it into a copy of the home page. */ public static function export_404() { $response = self::fetch( add_query_arg( 'cfsp_404', '1', CFSP_Settings::source_url() . '/' ) ); $body = ''; if ( ! is_wp_error( $response ) && in_array( (int) wp_remote_retrieve_response_code( $response ), array( 200, 404 ), true ) ) { $body = (string) wp_remote_retrieve_body( $response ); } $html = ''; if ( $body && false !== stripos( $body, ' (int) CFSP_Settings::get( 'http_timeout' ), 'redirection' => 0, // Redirects are handled in export(), never saved as content. // The direct route never leaves this machine, and the origin certificate (e.g. a Cloudflare // Origin CA one) is only trusted by the proxy, so it is not verified there. 'sslverify' => $direct ? false : (bool) CFSP_Settings::get( 'sslverify' ), 'user-agent' => 'Static-Push/' . CFSP_VERSION . '; ' . home_url(), 'cfsp_direct' => $direct, 'headers' => array_merge( array( 'X-CFSP-Export' => '1', 'Cache-Control' => 'no-cache', ), CFSP_Private::export_headers() // Lets the exporter through Private WordPress mode. ), ); // Optional HTTP basic auth for staging sites: define( 'CFSP_BASIC_AUTH', 'user:pass' ); if ( defined( 'CFSP_BASIC_AUTH' ) && CFSP_BASIC_AUTH ) { $args['headers']['Authorization'] = 'Basic ' . base64_encode( CFSP_BASIC_AUTH ); } return array( $fetch_url, apply_filters( 'cfsp_fetch_args', $args, $url ) ); } public static function fetch( $url ) { $direct = self::direct_enabled(); list( $fetch_url, $args ) = self::fetch_args( $url, $direct ); $response = wp_remote_get( $fetch_url, $args ); if ( $direct && is_wp_error( $response ) && self::is_connect_error( $response->get_error_message() ) ) { self::direct_failed( $response->get_error_message() ); list( $fetch_url, $args ) = self::fetch_args( $url, false ); $response = wp_remote_get( $fetch_url, $args ); } return $response; } /** * Fetch several pages at once (Requests::request_multiple, the library bundled with WordPress). * * @param string[] $urls Keyed by any id. * @return array Same keys; wp_remote_get()-style arrays or WP_Error. */ public static function fetch_many( array $urls ) { $class = class_exists( '\WpOrg\Requests\Requests' ) ? '\WpOrg\Requests\Requests' : ( class_exists( 'Requests' ) ? 'Requests' : null ); if ( count( $urls ) < 2 || ! $class || ! function_exists( 'curl_multi_init' ) ) { return array_map( array( __CLASS__, 'fetch' ), $urls ); } $direct = self::direct_enabled(); $requests = array(); foreach ( $urls as $id => $url ) { list( $fetch_url, $args ) = self::fetch_args( $url, $direct ); $requests[ $id ] = array( 'url' => $fetch_url, 'headers' => $args['headers'], 'type' => 'GET', 'options' => array( 'timeout' => $args['timeout'], 'connect_timeout' => 10, 'follow_redirects' => false, 'useragent' => $args['user-agent'], 'verify' => (bool) $args['sslverify'], 'verifyname' => (bool) $args['sslverify'], ), ); } $options = array(); if ( $direct ) { $hooks_class = class_exists( '\WpOrg\Requests\Hooks' ) ? '\WpOrg\Requests\Hooks' : 'Requests_Hooks'; $hooks = new $hooks_class(); $pin = function ( $handle ) { self::pin_handle( $handle ); }; $hooks->register( 'curl.before_multi_add', $pin ); $hooks->register( 'curl.before_send', $pin ); $options['hooks'] = $hooks; } try { $responses = call_user_func( array( $class, 'request_multiple' ), $requests, $options ); } catch ( \Exception $e ) { return array_map( array( __CLASS__, 'fetch' ), $urls ); } $out = array(); $failed = array(); foreach ( $urls as $id => $url ) { $r = $responses[ $id ] ?? null; if ( is_object( $r ) && ! ( $r instanceof \Exception ) && isset( $r->status_code ) ) { $headers = array(); foreach ( $r->headers as $name => $value ) { $headers[ strtolower( $name ) ] = $value; } $out[ $id ] = array( 'headers' => $headers, 'body' => (string) $r->body, 'response' => array( 'code' => (int) $r->status_code, 'message' => '', ), 'cookies' => array(), 'filename' => null, ); } else { $message = $r instanceof \Exception ? $r->getMessage() : 'No response'; $out[ $id ] = new WP_Error( 'cfsp_http', $message ); if ( self::is_connect_error( $message ) ) { $failed[ $id ] = $message; } } } // The direct route doesn't work here: switch to the normal one and retry those pages. if ( $direct && $failed ) { self::direct_failed( reset( $failed ) ); foreach ( array_keys( $failed ) as $id ) { $out[ $id ] = self::fetch( $urls[ $id ] ); } } return $out; } /* ------------------------------------------------------------------ * * Direct fetch: connect to this server instead of the public DNS answer * (e.g. Cloudflare), so exports don't make a round trip through the proxy. * ------------------------------------------------------------------ */ public static function direct_enabled() { return CFSP_Settings::get( 'direct_fetch' ) && defined( 'CURLOPT_RESOLVE' ) && ! get_transient( 'cfsp_direct_off' ); } /** * Arguments for a background-worker call to this site's own admin-ajax.php. With direct fetch on, * the call stays on this server, so a proxy that blocks the server's own IP (a Cloudflare WAF or * bot rule answering 403) can't stop background jobs. */ public static function loopback_args( array $args ) { if ( self::direct_enabled() ) { $args['cfsp_direct'] = true; } return $args; } /** The address this server answers on for its own hostname: the setting, or 127.0.0.1. */ public static function direct_ip() { $ip = trim( (string) CFSP_Settings::get( 'direct_ip' ) ); return filter_var( $ip, FILTER_VALIDATE_IP ) ? $ip : '127.0.0.1'; } private static function resolve_entries() { $host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); $ip = self::direct_ip(); $ip = false !== strpos( $ip, ':' ) ? '[' . $ip . ']' : $ip; return $host ? array( $host . ':443:' . $ip, $host . ':80:' . $ip ) : array(); } /** * Point a cURL handle's requests for this site's hostname at this server. The certificate is not * verified: the connection never leaves the machine, and origin certificates (e.g. Cloudflare Origin * CA) are only trusted by the proxy. Set here as well, because Requests' parallel mode doesn't apply * its "verify" option to each connection. */ public static function pin_handle( $handle ) { $entries = self::resolve_entries(); if ( $entries && defined( 'CURLOPT_RESOLVE' ) ) { // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_setopt -- Only inside WordPress's own HTTP request (http_api_curl / Requests hooks). curl_setopt( $handle, CURLOPT_RESOLVE, $entries ); curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, 0 ); // phpcs:enable } } /** http_api_curl: pin single exporter requests (wp_remote_get with 'cfsp_direct'). */ public static function pin_curl( $handle, $args, $url ) { if ( ! empty( $args['cfsp_direct'] ) && strtolower( (string) wp_parse_url( $url, PHP_URL_HOST ) ) === strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) ) { self::pin_handle( $handle ); } } /** A failure to reach the server at all (as opposed to a slow or failing page). */ private static function is_connect_error( $message ) { return (bool) preg_match( '/cURL error (6|7|35|51|52|56|60)\b|could not resolve|failed to connect|connection refused|ssl/i', (string) $message ); } private static function direct_failed( $message ) { set_transient( 'cfsp_direct_off', 1, HOUR_IN_SECONDS ); CFSP_Logger::error( sprintf( 'Fetching pages directly from this server (%s) failed: %s. Using the normal route for the next hour.', self::direct_ip(), $message ) ); } /** Map a same-origin asset URL to a readable file under the WordPress install. */ private static function url_to_local_file( $url ) { $path = rawurldecode( (string) wp_parse_url( $url, PHP_URL_PATH ) ); // Never read dotfiles/dot-folders (.env, .git, .htaccess …) straight from disk. if ( preg_match( '#(^|/)\.#', $path ) ) { return null; } // Code and config files are never read from disk (they go through HTTP like any page). if ( preg_match( '#\.(php\d?|phtml|phar|inc|ini|sql|log|env)$#i', $path ) ) { return null; } // Uploads, themes, plugins, then the site folder (covers wp-includes and anything else public). foreach ( CFSP_Paths::roots() as $root ) { list( $root_url, $dir ) = $root; $url_prefix = rtrim( (string) wp_parse_url( $root_url, PHP_URL_PATH ), '/' ) . '/'; if ( 0 === strpos( $path, $url_prefix ) ) { $real = realpath( $dir . '/' . substr( $path, strlen( $url_prefix ) ) ); if ( $real && is_file( $real ) && CFSP_Paths::is_allowed( $real ) ) { return $real; } return null; } } return null; } /* ------------------------------------------------------------------ * * Rewriting * ------------------------------------------------------------------ */ /** * Rewrite WordPress URLs for the static site. * * @param string $content * @param string $type html|css|xml|text|robots */ public static function rewrite( $content, $type = 'html' ) { // Media offloaded to R2: park uploads URLs so the rewrites below leave them alone. // Not in robots.txt, where "Allow: /wp-content/uploads/" must stay a path (no full URLs allowed). if ( 'robots' !== $type ) { $content = CFSP_Media::protect( $content ); } if ( 'html' !== $type || CFSP_Settings::get( 'force_replace' ) ) { // Every occurrence, including inline CSS/JS and JSON. $content = self::replace_origin( $content ); } else { // Only URL-bearing attributes and CSS url(). $content = CFSP_Cleanup::replace_callback( '#(\s(?:href|src|srcset|data-src|data-srcset|data-lazy-src|data-bg|poster|action|content|imagesrcset)\s*=\s*)(["\'])(.*?)\2#is', function ( $m ) { return $m[1] . $m[2] . self::replace_origin( $m[3] ) . $m[2]; }, $content ); $content = CFSP_Cleanup::replace_callback( '#url\(\s*(["\']?)([^"\')]+)\1\s*\)#i', function ( $m ) { return 'url(' . $m[1] . self::replace_origin( $m[2] ) . $m[1] . ')'; }, $content ); } // Remove the cache-busting parameter if a theme echoed the current URL. $content = CFSP_Cleanup::replace( '/(\?|&|&|%3F|%26)' . self::NOCACHE_PARAM . '(=|%3D)\d+/', '', $content ); // Hide WP: strip ?ver= from asset URLs. if ( CFSP_Settings::get( 'hide_ver' ) && in_array( $type, array( 'html', 'css' ), true ) ) { $content = CFSP_Cleanup::replace( '#\?ver=[\w.\-]+(?:&|&\#038;|&)#', '?', $content ); $content = CFSP_Cleanup::replace( '#(?:\?|&|&\#038;|&)ver=[\w.\-]+#', '', $content ); } $content = CFSP_Media::restore( $content ); if ( in_array( $type, array( 'html', 'xml' ), true ) ) { $content = CFSP_Feeds::convert_links( $content ); } return apply_filters( 'cfsp_rewrite_content', $content, $type ); } /** Replace every form of the WordPress origin (http, https, protocol-relative, JSON-escaped, URL-encoded). */ public static function replace_origin( $content ) { if ( null === self::$replacements ) { self::$replacements = self::build_replacements(); } foreach ( self::$replacements as $rule ) { $content = CFSP_Cleanup::replace( $rule[0], $rule[1], $content ); } return $content; } private static function build_replacements() { $source = wp_parse_url( CFSP_Settings::source_url() ); $hostpath = $source['host'] . ( isset( $source['port'] ) ? ':' . $source['port'] : '' ) . ( isset( $source['path'] ) ? rtrim( $source['path'], '/' ) : '' ); $base = CFSP_Settings::url_base(); $relative = 'relative' === CFSP_Settings::get( 'url_mode' ) || '' === $base; $pairs = array( 'https://' . $hostpath => $base, 'http://' . $hostpath => $base, '//' . $hostpath => $relative ? $base : preg_replace( '#^https?:#', '', $base ), ); // Don't match a longer host such as example.com.au when replacing example.com. $boundary = '(?![a-zA-Z0-9\-_]|\.[a-zA-Z0-9])'; $rules = array(); foreach ( $pairs as $from => $to ) { $variants = array( array( $from, $to, '/' ), array( str_replace( '/', '\\/', $from ), str_replace( '/', '\\/', $to ), '\\/' ), // JSON escaped. array( rawurlencode( $from ), rawurlencode( $to ), '%2F' ), // URL encoded. ); foreach ( $variants as $v ) { list( $f, $t, $slash ) = $v; $quoted = preg_quote( $f, '#' ); if ( $relative ) { // "https://site/page" -> "/page", bare "https://site" -> "/". $rules[] = array( '#' . $quoted . '(?=' . preg_quote( $slash, '#' ) . ')#i', addcslashes( $t, '\\$' ) ); $rules[] = array( '#' . $quoted . $boundary . '#i', addcslashes( $t . $slash, '\\$' ) ); } else { $rules[] = array( '#' . $quoted . $boundary . '#i', addcslashes( $t, '\\$' ) ); } } } return $rules; } /** Remove WordPress-only markup and apply the Optimize / Hide WP / Forms / Search options. */ public static function clean_html( $html ) { $html = CFSP_Cleanup::apply( $html ); return apply_filters( 'cfsp_clean_html', $html ); } /** * True when processing lost the page: a full HTML document that comes out without , or * real content that comes out with no text at all. */ public static function broken_html( $before, $after ) { if ( ! is_string( $after ) ) { return true; } if ( false !== stripos( $before, '' ) ) { return false === stripos( $after, '' ); } return '' !== trim( wp_strip_all_tags( $before ) ) && '' === trim( wp_strip_all_tags( $after ) ); } /* ------------------------------------------------------------------ * * Link extraction * ------------------------------------------------------------------ */ public static function extract_html_links( $html, $base ) { $raw = array(); if ( preg_match_all( '#\s(?:href|src|data-src|data-lazy-src|data-bg|data-background|poster)\s*=\s*(["\'])(.*?)\1#is', $html, $m ) ) { $raw = array_merge( $raw, $m[2] ); } // Absolute URLs in (og:image, twitter:image, msapplication-TileImage, ...). if ( preg_match_all( '#]*content\s*=\s*(["\'])((?:https?:)?//.*?)\1#is', $html, $m ) ) { $raw = array_merge( $raw, $m[2] ); } if ( preg_match_all( '#\s(?:srcset|data-srcset|imagesrcset)\s*=\s*(["\'])(.*?)\1#is', $html, $m ) ) { foreach ( $m[2] as $srcset ) { foreach ( explode( ',', $srcset ) as $candidate ) { $parts = preg_split( '/\s+/', trim( $candidate ) ); if ( ! empty( $parts[0] ) ) { $raw[] = $parts[0]; } } } } // Inline