. No logic, no function call and no bypass is * ever written to disk. * * Safety properties: * - It never touches a .maintenance file it did not write (a core update may own it). * Because the file has no room for a marker, ownership is a fingerprint of the exact * bytes this class last wrote, kept in a plain-text record inside the plugin's own * storage folder. A file whose bytes differ is someone else's. * - The timestamp is back-dated so WordPress itself ignores the file after LIFETIME * seconds, even if this process is killed before it can clean up. * - It is removed on normal completion, on an exception (callers use finally), and * from a shutdown function, which also runs after a fatal error or a timeout. * * While the file is fresh WordPress answers every request with the 503 page, this * plugin's own routes included: WordPress decides that before any plugin is loaded, so a * plugin has no hook to exempt itself. A restore interrupted inside the window therefore * resumes once the file has expired (at most LIFETIME seconds). */ final class Maintenance { /** How long a forgotten file keeps the site offline, in seconds (WordPress allows 600). */ public const LIFETIME = 300; /** @var string */ private $path; /** @var string Plain-text record: the SHA-256 of the bytes this class last wrote to $path. */ private $record; /** @var bool */ private $owned = false; /** @var bool */ private $hooked = false; public function __construct( ?string $path = null, ?string $record = null ) { $this->path = null !== $path ? $path : (string) apply_filters( 'migratex_maintenance_file', ABSPATH . '.maintenance' ); $this->record = null !== $record ? $record : Paths::root() . '/maintenance-' . substr( wp_hash( 'migratex-maintenance' ), 0, 32 ) . '.owner'; } public function path(): string { return $this->path; } public function isOwned(): bool { return $this->owned; } /** * Turns maintenance mode on. Safe to call repeatedly. * * @return bool True when this instance holds the file; false when it could not (read-only * folder) or must not (someone else's file is there). The caller carries on * either way: maintenance mode narrows the race, it is not a precondition. */ public function enable(): bool { if ( $this->owned ) { $this->refresh(); return true; } if ( file_exists( $this->path ) ) { // Ours from an attempt that died: take it over. Anyone else's: leave it alone. if ( ! $this->isOurs() ) { return false; } $this->owned = true; $this->hook(); $this->refresh(); return true; } // "x" fails if the file appears between the check above and now. $handle = @fopen( $this->path, 'x' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- a read-only folder is handled by the return value. if ( false === $handle ) { return false; } $contents = $this->contents(); fwrite( $handle, $contents ); fclose( $handle ); // Without the record the file could never be recognised as ours again: take it back down. if ( ! $this->remember( $contents ) ) { wp_delete_file( $this->path ); return false; } $this->owned = true; $this->hook(); return true; } /** * Restarts the clock. Called between placements so a slow copy cannot outlive the file. */ public function refresh(): void { if ( $this->owned && $this->isOurs() ) { $contents = $this->contents(); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents,WordPress.PHP.NoSilencedErrors.Discouraged if ( false !== @file_put_contents( $this->path, $contents ) ) { $this->remember( $contents ); } } } /** * Turns maintenance mode off. Idempotent, and a no-op for a file we do not own. */ public function disable(): void { if ( ! $this->owned ) { return; } $this->owned = false; if ( is_file( $this->path ) && $this->isOurs() ) { wp_delete_file( $this->path ); } if ( is_file( $this->record ) ) { wp_delete_file( $this->record ); } } /** * True when the file at the path is byte-for-byte what this class last wrote. */ public function isOurs(): bool { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents,WordPress.PHP.NoSilencedErrors.Discouraged $current = @file_get_contents( $this->path, false, null, 0, 512 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents,WordPress.PHP.NoSilencedErrors.Discouraged $known = is_file( $this->record ) ? @file_get_contents( $this->record, false, null, 0, 128 ) : false; return is_string( $current ) && is_string( $known ) && hash_equals( trim( $known ), hash( 'sha256', $current ) ); } private function remember( string $contents ): bool { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents,WordPress.PHP.NoSilencedErrors.Discouraged return false !== @file_put_contents( $this->record, hash( 'sha256', $contents ) ); } /** * Exactly the line WordPress core writes: ; ?> * * WordPress ends maintenance once time() - $upgrading >= 10 minutes. Back-dating the * integer timestamp by (10 minutes - LIFETIME) makes the file expire LIFETIME seconds from * now. The timestamp is the only variable part; nothing else is ever written. */ private function contents(): string { $upgrading = time() - ( 10 * MINUTE_IN_SECONDS - self::LIFETIME ); return '\n"; } private function hook(): void { if ( ! $this->hooked ) { $this->hooked = true; register_shutdown_function( array( $this, 'disable' ) ); } } }