= self::MAX_PARAMETER_COPIES ) { return false; } $json = wp_json_encode( $meta ); if ( false === $json ) { return false; } $fs = GorillaCache_Installer::filesystem(); $tmp = $file . '.' . bin2hex( random_bytes( 6 ) ) . '.tmp'; if ( ! $fs->put_contents( $tmp, self::MAGIC . "\n" . $json . "\n" . $body, 0644 ) ) { wp_delete_file( $tmp ); return false; } // PHP's rename() replaces the copy in one step, so a visitor never reads half a page; WordPress's move() // deletes the target first. It fails without a warning when a concurrent request replaced it first. if ( ! @rename( $tmp, $file ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.rename_rename -- presubmit-allow: check-10 (an atomic replace of a stored page; move() deletes the target first) wp_delete_file( $tmp ); return false; } if ( file_exists( $file . self::NOTE ) ) { wp_delete_file( $file . self::NOTE ); // Stored now: an older note of why it was not is out of date. } return true; } /** * Notes why a page was not stored, beside where its copy would be, for warming and wp gorilla-cache explain. * Written only for Gorilla Cache's own requests (they carry the site's secret token), so a visitor cannot make * it write anything, and at most once in ten minutes per address. * * @param string $file Absolute path the copy would have. * @param string $reason Reason from GorillaCache_Engine_Rules::storable(). * @param string $detail Detail from GorillaCache_Engine_Rules::refusal_detail(). * @param int $now Current time. * @return bool */ public static function write_note( $file, $reason, $detail, $now ) { if ( ! class_exists( 'GorillaCache_Installer' ) || ! function_exists( 'wp_mkdir_p' ) ) { return false; } $note = $file . self::NOTE; if ( file_exists( $note ) && filemtime( $note ) > (int) $now - 10 * 60 ) { return true; } $json = wp_json_encode( array( 'reason' => (string) $reason, 'detail' => (string) $detail, 'at' => (int) $now, ) ); if ( false === $json || ! wp_mkdir_p( dirname( $file ) ) ) { return false; } return (bool) GorillaCache_Installer::filesystem()->put_contents( $note, $json, 0644 ); } /** * Reads the note of why a page was not stored, when there is one younger than the lifespan. * * @param string $file Absolute path the copy would have. * @param int $lifespan Lifespan in seconds. * @param int $now Current time. * @return array{reason:string, detail:string, at:int}|null */ public static function read_note( $file, $lifespan, $now ) { $note = $file . self::NOTE; if ( ! is_readable( $note ) || filemtime( $note ) + (int) $lifespan <= (int) $now ) { return null; } $data = json_decode( (string) GorillaCache_Installer::filesystem()->get_contents( $note ), true ); if ( ! is_array( $data ) || empty( $data['reason'] ) ) { return null; } return array( 'reason' => (string) $data['reason'], 'detail' => isset( $data['detail'] ) ? (string) $data['detail'] : '', 'at' => isset( $data['at'] ) ? (int) $data['at'] : 0, ); } /** * Opens a cached response when it is fresh. * * @param string $file Absolute path of the cache file. * @param int $lifespan Lifespan in seconds. * @param int $now Current time. * @return array{meta:array, handle:resource}|null Null when missing, unreadable, corrupt or expired. */ public static function open( $file, $lifespan, $now ) { $handle = @fopen( $file, 'rb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- presubmit-allow: check-10 (serves a stored page before WordPress loads, so no WordPress function exists yet; the file may have been cleared a moment ago) if ( false === $handle ) { return null; } $magic = fgets( $handle ); $json = fgets( $handle ); $meta = is_string( $json ) ? json_decode( $json, true ) : null; if ( self::MAGIC . "\n" !== $magic || ! is_array( $meta ) || ! isset( $meta['created'], $meta['status'] ) || (int) $meta['created'] + (int) $lifespan <= (int) $now ) { fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- presubmit-allow: check-10 (serves a stored page before WordPress loads, so no WordPress function exists yet) return null; } return array( 'meta' => $meta, 'handle' => $handle, ); } /** * Sends the rest of an opened file (the body) and closes it. Falls back to a read loop where hosts * disable fpassthru(), as some do. * * @param resource $handle Handle positioned at the body. */ public static function send_body( $handle ) { $disabled = array_map( 'trim', explode( ',', (string) ini_get( 'disable_functions' ) ) ); if ( function_exists( 'fpassthru' ) && ! in_array( 'fpassthru', $disabled, true ) ) { fpassthru( $handle ); } else { // The stored page is the site's own HTML, sent unchanged: copied to the output stream, never echoed. $output = fopen( 'php://output', 'wb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- presubmit-allow: check-10 (serves a stored page before WordPress loads, so no WordPress function exists yet) if ( false !== $output ) { stream_copy_to_stream( $handle, $output ); fclose( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- presubmit-allow: check-10 (serves a stored page before WordPress loads, so no WordPress function exists yet) } } fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- presubmit-allow: check-10 (serves a stored page before WordPress loads, so no WordPress function exists yet) } }