Newest first. */ public static function all(): array { $items = array(); $dir = Paths::archives(); if ( ! is_dir( $dir ) ) { return $items; } foreach ( scandir( $dir ) as $entry ) { $path = self::path( $entry ); if ( null === $path ) { continue; } $size = (int) filesize( $path ); $items[] = array( 'file' => $entry, 'size' => $size, 'size_human' => Format::bytes( (int) $size ), 'created' => (int) filemtime( $path ), ); } usort( $items, static function ( $a, $b ) { return $b['created'] <=> $a['created']; } ); return $items; } /** * @return string|null Absolute path of an existing archive, or null if the name is not valid. */ public static function path( string $name ): ?string { if ( 1 !== preg_match( '/^[A-Za-z0-9._-]{1,200}\.' . preg_quote( MIGRATEX_ARCHIVE_EXT, '/' ) . '$/', $name ) || false !== strpos( $name, '..' ) ) { return null; } $path = Paths::archives() . '/' . $name; return ( is_file( $path ) && ! is_link( $path ) && Paths::contains( Paths::archives(), $path ) ) ? $path : null; } /** * Some hashing of an archive's SHA-256, within $seconds; call again until it says "ready". See Checksum. * * @return array{status:string,file:string,size:int,mtime:int,done:int,percent:float,sha256?:string,computed_at?:int}|null Null if the name is not a backup. */ public static function checksumStep( string $name, float $seconds ): ?array { $path = self::path( $name ); return null === $path ? null : array( 'file' => $name ) + Checksum::step( $path, $seconds ); } /** * The SHA-256 of an archive, all of it, for a caller that can wait (the other way to ask is checksumStep()). * * @return array{sha256:string,size:int}|null Null if the name is not a backup or it cannot be read. */ public static function checksum( string $name ): ?array { do { $step = self::checksumStep( $name, 600.0 ); if ( null === $step || 'error' === $step['status'] ) { return null; } } while ( 'ready' !== $step['status'] ); return array( 'sha256' => (string) $step['sha256'], 'size' => $step['size'], ); } public static function delete( string $name ): bool { $path = self::path( $name ); if ( null !== $path ) { // The checksum record, a hash half done, and the earlier checksum file go with the backup. foreach ( array( Checksum::metaPath( $path ), Checksum::partPath( $path ), $path . '.sha256' ) as $companion ) { if ( is_file( $companion ) ) { @unlink( $companion ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink,WordPress.PHP.NoSilencedErrors.Discouraged } } } return null !== $path && @unlink( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink,WordPress.PHP.NoSilencedErrors.Discouraged } }