120 ) { return false; } $segs[] = $seg; } if ( count( $segs ) > 24 ) { return false; } return RAVENCACHE_CACHE_DIR . '/pages/' . $host . ( $segs ? '/' . implode( '/', $segs ) : '' ) . '/'; } function ravencache_is_https() { if ( ravencache_server( 'HTTPS' ) && 'off' !== strtolower( ravencache_server( 'HTTPS' ) ) ) { return true; } if ( isset( $_SERVER['SERVER_PORT'] ) && 443 === (int) $_SERVER['SERVER_PORT'] ) { return true; } if ( 'https' === strtolower( ravencache_server( 'HTTP_X_FORWARDED_PROTO' ) ) ) { return true; } if ( false !== strpos( ravencache_server( 'HTTP_CF_VISITOR' ), 'https' ) ) { return true; } return false; } /** * Analiza bieżącego żądania. * * @return array ok(bool), reason(string), dir, variant */ function ravencache_analyze( $cfg ) { $r = array( 'ok' => false, 'reason' => '', 'dir' => '', 'variant' => '' ); if ( empty( $cfg['enabled'] ) ) { $r['reason'] = 'disabled'; return $r; } if ( 'cli' === PHP_SAPI || 'phpdbg' === PHP_SAPI ) { $r['reason'] = 'cli'; return $r; } $method = ravencache_server( 'REQUEST_METHOD' ); if ( '' === $method ) { $method = 'GET'; } if ( 'GET' !== $method && 'HEAD' !== $method ) { $r['reason'] = 'method'; return $r; } if ( ! empty( $_SERVER['PHP_AUTH_USER'] ) ) { $r['reason'] = 'http-auth'; return $r; } $uri = ravencache_server( 'REQUEST_URI' ); if ( '' === $uri ) { $r['reason'] = 'no-uri'; return $r; } $host = strtolower( ravencache_server( 'HTTP_HOST' ) ); if ( empty( $cfg['hosts'] ) || ! in_array( $host, $cfg['hosts'], true ) ) { $r['reason'] = 'host'; return $r; } $qpos = strpos( $uri, '?' ); $path = false === $qpos ? $uri : substr( $uri, 0, $qpos ); $query = false === $qpos ? '' : substr( $uri, $qpos + 1 ); if ( '' === $path || '/' !== $path[0] ) { $r['reason'] = 'bad-path'; return $r; } if ( preg_match( '#/(wp-admin|wp-json|wp-login\.php|wp-cron\.php|xmlrpc\.php|wp-comments-post\.php|wp-includes|wp-content)(/|$)#', $path ) || preg_match( '#(^|/)(feed|robots\.txt|favicon\.ico)(/|$)|\.(xml|txt|json|php|xsl|css|js)$#i', $path ) ) { $r['reason'] = 'builtin-exclude'; return $r; } $qhash = ''; if ( '' !== $query ) { $ignored = isset( $cfg['ignore_params'] ) ? $cfg['ignore_params'] : array(); $allowed = isset( $cfg['allow_params'] ) ? $cfg['allow_params'] : array(); $keep = array(); foreach ( explode( '&', $query ) as $pair ) { if ( '' === $pair ) { continue; } $eq = strpos( $pair, '=' ); $name = urldecode( false === $eq ? $pair : substr( $pair, 0, $eq ) ); if ( ravencache_param_in( $name, $ignored ) ) { continue; } if ( ravencache_param_in( $name, $allowed ) ) { $keep[] = $pair; continue; } $r['reason'] = 'query-string'; return $r; } if ( $keep ) { sort( $keep ); $qhash = substr( md5( implode( '&', $keep ) ), 0, 12 ); } } if ( ! empty( $cfg['exclude_urls'] ) && ravencache_match_path( $path, $cfg['exclude_urls'] ) ) { $r['reason'] = 'excluded-url'; return $r; } $prefixes = array( 'wordpress_logged_in_', 'wp-postpass_', 'comment_author_', 'woocommerce_items_in_cart', 'woocommerce_cart_hash', 'wp_woocommerce_session_', 'edd_wp_session', 'speedcache_bypass' ); if ( ! empty( $cfg['exclude_cookies'] ) ) { $prefixes = array_merge( $prefixes, $cfg['exclude_cookies'] ); } foreach ( array_keys( $_COOKIE ) as $cookie ) { foreach ( $prefixes as $prefix ) { if ( '' !== $prefix && 0 === strpos( $cookie, $prefix ) ) { $r['reason'] = 'cookie'; return $r; } } } $ua = ravencache_server( 'HTTP_USER_AGENT' ); if ( ! empty( $cfg['exclude_agents'] ) ) { foreach ( $cfg['exclude_agents'] as $agent ) { if ( '' !== $agent && false !== stripos( $ua, $agent ) ) { $r['reason'] = 'user-agent'; return $r; } } } $mobile = ! empty( $cfg['mobile'] ) && preg_match( '/Mobile|Android|iP(hone|od)|Opera Mini|IEMobile|BlackBerry/i', $ua ); $dir = ravencache_dir( $host, $path ); if ( ! $dir ) { $r['reason'] = 'bad-dir'; return $r; } $r['ok'] = true; $r['dir'] = $dir; $r['variant'] = ( ravencache_is_https() ? '.https' : '.http' ) . ( $mobile ? '.m' : '' ) . ( '/' === substr( $path, -1 ) ? '' : '.ns' ) . ( '' !== $qhash ? '.q' . $qhash : '' ); return $r; } /** Serwowanie strony z cache (wywoływane przez drop-in). */ function ravencache_serve() { $cfg = ravencache_config(); if ( empty( $cfg['enabled'] ) ) { return; } $r = ravencache_analyze( $cfg ); if ( ! $r['ok'] ) { return; } $base = $r['dir'] . '__sc' . $r['variant'] . '.html'; $ae = ravencache_server( 'HTTP_ACCEPT_ENCODING' ); $gz = ! empty( $cfg['gzip'] ) && false !== stripos( $ae, 'gzip' ) && is_file( $base . '.gz' ); $file = $gz ? $base . '.gz' : $base; $mtime = @filemtime( $file ); if ( ! $mtime ) { return; } $ttl = isset( $cfg['ttl'] ) ? (int) $cfg['ttl'] : 86400; if ( $ttl > 0 && ( $mtime + $ttl ) < time() ) { return; } $size = (int) @filesize( $file ); if ( $size < 1 ) { return; } $etag = '"' . dechex( $mtime ) . '-' . dechex( $size ) . ( $gz ? '-gz' : '' ) . '"'; $not_modified = false; if ( '' !== ravencache_server( 'HTTP_IF_NONE_MATCH' ) ) { $not_modified = false !== strpos( ravencache_server( 'HTTP_IF_NONE_MATCH' ), $etag ); } elseif ( '' !== ravencache_server( 'HTTP_IF_MODIFIED_SINCE' ) ) { $since = strtotime( ravencache_server( 'HTTP_IF_MODIFIED_SINCE' ) ); $not_modified = $since && $since >= $mtime; } $cc = ! empty( $cfg['cache_control'] ) ? $cfg['cache_control'] : 'max-age=0, must-revalidate'; header( 'X-RavenCache: HIT' ); header( 'Cache-Control: ' . $cc ); header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $mtime ) . ' GMT' ); header( 'ETag: ' . $etag ); header( 'Vary: Accept-Encoding' . ( ! empty( $cfg['mobile'] ) ? ', User-Agent' : '' ) ); if ( $not_modified ) { if ( function_exists( 'http_response_code' ) ) { http_response_code( 304 ); } exit; } header( 'Content-Type: text/html; charset=' . ( ! empty( $cfg['charset'] ) ? $cfg['charset'] : 'UTF-8' ) ); if ( $gz ) { header( 'Content-Encoding: gzip' ); } header( 'Content-Length: ' . $size ); if ( 'HEAD' === ravencache_server( 'REQUEST_METHOD' ) ) { exit; } // Streams a cached page from the plugin's own cache directory. This drop-in runs before WordPress and // WP_Filesystem exist, and readfile() avoids loading large pages into memory. readfile( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile exit; } } if ( ! defined( 'RAVENCACHE_LIB_ONLY' ) ) { ravencache_serve(); }