if ( ! $ok ) { @unlink( $tmp ); } return $ok; } /** A generation marker is read from disk, not from a per-request Redis/option copy. */ function pj_home_disk_generation() { if ( ! pj_home_disk_ready() ) { return ''; } $path = pj_home_disk_dir() . '/revision'; $gen = @file_get_contents( $path ); if ( is_string( $gen ) && preg_match( '/^[a-f0-9]{32}$/D', $gen ) ) { return $gen; } pj_home_disk_invalidate(); $gen = @file_get_contents( $path ); return is_string( $gen ) && preg_match( '/^[a-f0-9]{32}$/D', $gen ) ? $gen : ''; } function pj_home_disk_invalidate() { if ( ! pj_home_disk_ready() ) { return; } $path = pj_home_disk_dir() . '/revision'; if ( ! pj_home_disk_write( $path, str_replace( '-', '', wp_generate_uuid4() ) ) ) { // Fail closed if the generation cannot be advanced. Never remove unrelated files. @unlink( $path ); foreach ( glob( pj_home_disk_dir() . '/home-*.php' ) ?: array() as $file ) { @unlink( $file ); } } } /** The only allowed cookie is Polylang's language preference, matching the URL. */ function pj_home_disk_language() { $lang = function_exists( 'pll_current_language' ) ? pll_current_language( 'slug' ) : ''; return in_array( $lang, array( 'en', 'fa', 'ps' ), true ) ? $lang : ''; } function pj_home_disk_request_cookies_safe() { $raw = (string) ( $_SERVER['HTTP_COOKIE'] ?? '' ); if ( '' === $raw && empty( $_COOKIE ) ) { return true; } $lang = pj_home_disk_language(); // Require an exact raw header and parsed value; reject duplicate, unknown or malformed cookies. return '' !== $lang && 1 === count( $_COOKIE ) && isset( $_COOKIE['pll_language'] ) && $lang === $_COOKIE['pll_language'] && 1 === preg_match( '/^\s*pll_language=' . $lang . '\s*$/D', $raw ); } /** Expose only a fixed diagnostic code, never cookie values or account data. */ function pj_home_disk_bypass( $reason ) { if ( ! headers_sent() ) { header( 'X-Pajhwok-Home-Cache-Reason: ' . $reason ); } return false; } /** Only canonical, anonymous GET requests can share HTML. */ function pj_home_disk_eligible() { if ( ! pj_home_request() ) { return false; } if ( ! headers_sent() ) { header( 'X-Pajhwok-Home-Cache-Policy: pll-language-v1' ); } if ( ! pj_home_disk_enabled() ) { return pj_home_disk_bypass( 'disabled' ); } if ( is_preview() || is_user_logged_in() || is_customize_preview() || ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) || ! is_ssl() || 'GET' !== ( $_SERVER['REQUEST_METHOD'] ?? '' ) || ! empty( $_SERVER['QUERY_STRING'] ) || ! empty( $_GET ) || ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) || ! empty( $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ) || ! empty( $_SERVER['PHP_AUTH_USER'] ) || ! empty( $_SERVER['HTTP_RANGE'] ) ) { return pj_home_disk_bypass( 'private-or-nonstandard-request' ); } if ( ! pj_home_disk_request_cookies_safe() ) { return pj_home_disk_bypass( 'request-cookie' ); } $url = wp_parse_url( pj_home_url() ); $host = strtolower( (string) ( $_SERVER['HTTP_HOST'] ?? '' ) ); $expected = strtolower( (string) ( $url['host'] ?? '' ) . ( isset( $url['port'] ) ? ':' . $url['port'] : '' ) ); if ( ! $expected || $host !== $expected || ( $_SERVER['REQUEST_URI'] ?? '' ) !== ( $url['path'] ?? '/' ) ) { return pj_home_disk_bypass( 'noncanonical-url' ); } return pj_home_disk_headers_safe( true ); } /** Only the matching Polylang Set-Cookie may coexist with shared public HTML. * Its header is generated per request by WordPress; it is never saved or replayed. */ function pj_home_disk_headers_safe( $before = false ) { $lang = pj_home_disk_language(); $language_cookie_seen = false; foreach ( headers_list() as $h ) { if ( 0 === stripos( $h, 'Set-Cookie:' ) ) { if ( $language_cookie_seen || '' === $lang || ! preg_match( '/^(?i:Set-Cookie):\s*pll_language=' . $lang . '(?:;|$)/D', $h ) ) { return pj_home_disk_bypass( 'response-cookie' ); } $language_cookie_seen = true; continue; } if ( preg_match( '/^(?:Location|Content-Disposition|WWW-Authenticate):/i', $h ) || ( 0 === stripos( $h, 'Content-Security-Policy:' ) && preg_match( '/nonce-/i', $h ) ) ) { return pj_home_disk_bypass( 'private-response-header' ); } if ( $before && 0 === stripos( $h, 'Cache-Control:' ) && preg_match( '/private|no-store|no-cache/i', $h ) ) { return pj_home_disk_bypass( 'origin-cache-control' ); } if ( 0 === stripos( $h, 'Vary:' ) ) { foreach ( explode( ',', substr( $h, 5 ) ) as $v ) { if ( ! in_array( strtolower( trim( $v ) ), array( 'accept-encoding', 'cookie' ), true ) ) { return pj_home_disk_bypass( 'unsupported-vary' ); } } } } return true; } /** Capture the eligibility before our own mandatory no-store headers are sent. */ add_action( 'template_redirect', static function () { $GLOBALS['pj_home_disk_candidate'] = pj_home_disk_eligible(); }, 98 ); /** Keep only current HTML, versioned by generation, exact language and device class. */ function pj_home_disk_context() { if ( empty( $GLOBALS['pj_home_disk_candidate'] ) ) { return null; } if ( ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) || is_user_logged_in() || ! pj_home_disk_request_cookies_safe() ) { pj_home_disk_bypass( 'private-context' ); return null; } if ( ! pj_home_disk_headers_safe() ) { return null; } $gen = pj_home_disk_generation(); $keep = min( 60, max( 0, (int) pj_opt( 'site', 'speed', 'server_keep' ) ) ); if ( ! $gen || ! $keep ) { pj_home_disk_bypass( 'storage-or-ttl' ); return null; } $lang = pj_live_lang(); $device = function_exists( 'pj_push_device' ) ? pj_push_device( (string) ( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ) : ( wp_is_mobile() ? 'mobile' : 'desktop' ); // Do not let arbitrarily many client-supplied keys create arbitrary files. $key = substr( hash( 'sha256', PJ_VER . '|pll-language-v1|' . pj_home_url() . '|' . $lang . '|' . $device . '|' . ( wp_is_mobile() ? 'mobile' : 'desktop' ) ), 0, 24 ); return array( 'path' => pj_home_disk_dir() . '/home-' . $key . '.php', 'gen' => $gen, 'ttl' => $keep, 'started' => time() ); } function pj_home_disk_get( $ctx ) { if ( ! $ctx || ! is_readable( $ctx['path'] ) || is_link( $ctx['path'] ) ) { return null; } $raw = @file_get_contents( $ctx['path'], false, null, 0, 12 * 1024 * 1024 ); $prefix = "