0, 'size' => $size, 'chain' => '', ); } if ( $size !== (int) $sum['size'] ) { return new \WP_Error( 'sitezip_sum_changed', __( 'The backup file changed while it was being checked.', 'sitezip' ) ); } $fh = @fopen( $path, 'rb' ); if ( ! $fh ) { return new \WP_Error( 'sitezip_sum_open', __( 'Could not read the backup file to check it.', 'sitezip' ) ); } fseek( $fh, (int) $sum['pos'] ); // At least one block per call, so a late start still makes progress. $first = true; while ( $sum['pos'] < $size && ( $first || microtime( true ) < $deadline ) ) { $first = false; $ctx = hash_init( 'sha256' ); $left = min( self::SUM_BLOCK, $size - $sum['pos'] ); $read = $left; while ( $left > 0 ) { $chunk = fread( $fh, min( 1048576, $left ) ); if ( false === $chunk || '' === $chunk ) { fclose( $fh ); return new \WP_Error( 'sitezip_sum_read', __( 'Could not read the backup file to check it.', 'sitezip' ) ); } hash_update( $ctx, $chunk ); $left -= strlen( $chunk ); } $sum['chain'] = hash( 'sha256', $sum['chain'] . hash_final( $ctx ) ); $sum['pos'] += $read; } fclose( $fh ); return $sum['pos'] >= $size; } /** * The fingerprint saved for an archive, if any. * * @param string $path Archive path. * @return string|null 64 hex characters. */ public static function saved_fingerprint( $path ) { $file = self::sum_path( $path ); if ( ! is_file( $file ) || is_link( $file ) ) { return null; } $sum = trim( (string) @file_get_contents( $file, false, null, 0, 128 ) ); return preg_match( '/^[a-f0-9]{64}$/', $sum ) ? $sum : null; } /** * Default backup name: the site address plus the current date and time, * e.g. "example-com-2026-09-16-143012". * * @return string */ public static function default_name() { $parts = wp_parse_url( home_url() ); $site = ( isset( $parts['host'] ) ? $parts['host'] : '' ) . ( isset( $parts['path'] ) ? $parts['path'] : '' ); $site = self::sanitize_name( $site ); return ( '' !== $site ? $site : 'site' ) . '-' . wp_date( 'Y-m-d-His' ); } /** * Reduce a user-supplied name to a safe file name stem. * * @param string $name Raw name, with or without ".zip". * @return string Possibly empty. */ public static function sanitize_name( $name ) { $name = preg_replace( '/\.zip$/i', '', trim( (string) $name ) ); $name = remove_accents( $name ); $name = preg_replace( '/[^A-Za-z0-9._-]+/', '-', $name ); $name = preg_replace( array( '/\.{2,}/', '/-{2,}/' ), array( '.', '-' ), $name ); $name = substr( $name, 0, 120 ); return trim( $name, '.-_' ); } /** * A file name in the backups folder that is not already taken. * * @param string $name Requested name; empty means the default. * @return string|\WP_Error File name including ".zip". */ public static function unique_filename( $name ) { $dir = PathGuard::backups_dir(); if ( is_wp_error( $dir ) ) { return $dir; } $stem = self::sanitize_name( $name ); if ( '' === $stem ) { $stem = self::default_name(); } $file = $stem . '.zip'; for ( $i = 2; file_exists( $dir . $file ) || file_exists( $dir . $file . '.part' ); $i++ ) { $file = $stem . '-' . $i . '.zip'; } return $file; } /** * Whether a string is a plain archive file name with no path in it. * * Deliberately permissive about characters, since archives copied in by * hand may contain spaces or brackets; containment is what matters. * * @param string $file Candidate. * @return bool */ public static function is_valid_filename( $file ) { return is_string( $file ) && '' !== $file && strlen( $file ) <= 255 && '.' !== $file[0] && false === strpbrk( $file, "/\\\0" ) && basename( $file ) === $file && (bool) preg_match( '/\.zip$/i', $file ); } /** * Absolute path of an existing archive in the backups folder. * * @param string $file File name. * @return string|\WP_Error */ public static function path( $file ) { $dir = PathGuard::backups_dir(); if ( is_wp_error( $dir ) ) { return $dir; } $not_found = new \WP_Error( 'sitezip_not_found', __( 'That backup file does not exist.', 'sitezip' ), array( 'status' => 404 ) ); if ( ! self::is_valid_filename( $file ) ) { return $not_found; } $path = $dir . $file; if ( ! is_file( $path ) || is_link( $path ) || ! PathGuard::is_inside( $path, $dir ) ) { return $not_found; } return $path; } /** * Every archive in the backups folder, newest first. * * @return array[] */ public static function all() { $dir = PathGuard::backups_dir(); if ( is_wp_error( $dir ) ) { return array(); } self::clean_stale_uploads( $dir ); $out = array(); foreach ( (array) @scandir( $dir ) as $file ) { if ( ! self::is_valid_filename( $file ) ) { continue; } $path = $dir . $file; if ( ! is_file( $path ) || is_link( $path ) ) { continue; } $size = (int) filesize( $path ); $mtime = (int) filemtime( $path ); $manifest = self::manifest( $path ); $out[] = array( 'file' => $file, 'size' => $size, 'modified' => gmdate( 'c', $mtime ), 'created' => ( $manifest && ! empty( $manifest['created'] ) ) ? $manifest['created'] : gmdate( 'c', $mtime ), 'site_url' => ( $manifest && ! empty( $manifest['home_url'] ) ) ? $manifest['home_url'] : '', 'valid' => (bool) $manifest, 'options' => ( $manifest && ! empty( $manifest['options'] ) && is_array( $manifest['options'] ) ) ? $manifest['options'] : null, 'files' => ( $manifest && isset( $manifest['file_count'] ) ) ? (int) $manifest['file_count'] : null, 'mtime' => $mtime, ); } usort( $out, static function ( $a, $b ) { return $b['mtime'] - $a['mtime']; } ); foreach ( $out as &$backup ) { unset( $backup['mtime'] ); } return $out; } /** * Read the manifest from an archive. * * Parsed manifests are cached by path, size and mtime, because opening * a very large archive means reading its whole central directory. * * @param string $path Archive path. * @return array|null Null when the file is not a SiteZip archive. */ public static function manifest( $path ) { if ( ! class_exists( '\ZipArchive' ) ) { return null; } clearstatcache( true, $path ); $key = 'sitezip_m_' . md5( $path . '|' . filesize( $path ) . '|' . filemtime( $path ) ); $cached = get_transient( $key ); if ( is_array( $cached ) ) { return empty( $cached['format'] ) ? null : $cached; } $manifest = null; $zip = new \ZipArchive(); if ( true === $zip->open( $path ) ) { $raw = $zip->getFromName( self::MANIFEST, 1048576 ); foreach ( self::LEGACY_MANIFESTS as $legacy ) { if ( false !== $raw ) { break; } $raw = $zip->getFromName( $legacy, 1048576 ); } $zip->close(); $decoded = is_string( $raw ) ? json_decode( $raw, true ) : null; if ( is_array( $decoded ) && isset( $decoded['format'] ) && self::FORMAT === (int) $decoded['format'] ) { $manifest = $decoded; } } // Cache negative results too, so a foreign zip is not reopened on every page load. set_transient( $key, $manifest ? $manifest : array( 'format' => 0 ), WEEK_IN_SECONDS ); return $manifest; } /** * Delete an archive. * * @param string $file File name. * @return true|\WP_Error */ public static function delete( $file ) { $path = self::path( $file ); if ( is_wp_error( $path ) ) { return $path; } if ( ! @unlink( $path ) ) { return new \WP_Error( 'sitezip_delete', __( 'Could not delete that backup file.', 'sitezip' ), array( 'status' => 500 ) ); } @unlink( self::sum_path( $path ) ); return true; } /** * Path of an in-progress browser upload. * * @param string $upload_id 32 hex characters. * @return string|\WP_Error */ public static function upload_path( $upload_id ) { if ( ! is_string( $upload_id ) || ! preg_match( '/^[a-f0-9]{32}$/', $upload_id ) ) { return new \WP_Error( 'sitezip_upload_id', __( 'Invalid upload.', 'sitezip' ), array( 'status' => 400 ) ); } $dir = PathGuard::backups_dir(); if ( is_wp_error( $dir ) ) { return $dir; } return $dir . 'upload-' . $upload_id . '.part'; } /** * Remove abandoned uploads and partial backups older than a day. * * @param string $dir Backups folder. * @return void */ private static function clean_stale_uploads( $dir ) { if ( ( new JobStore() )->get_running() ) { return; } foreach ( (array) glob( $dir . '*.part' ) as $part ) { if ( is_file( $part ) && ! is_link( $part ) && filemtime( $part ) < time() - DAY_IN_SECONDS ) { @unlink( $part ); } } } }