Library::all() ) ); } /** * The SHA-256 of one backup as it is stored on the server, for comparing with a downloaded copy. * * Does a few seconds of hashing per request and says how far it has got, so a multi-gigabyte backup is * done over several short requests (the page asks again until "ready"); a hash worked out before is * returned at once, as long as the file has not changed. Administrators only. */ public function checksum( Request $request ): Response { if ( ! current_user_can( 'manage_options' ) ) { throw new HttpException( 'om_forbidden', esc_html__( 'You do not have permission to do that.', 'migratex' ), 403 ); } $file = $request->str( 'file' ); if ( null === Library::path( $file ) ) { throw new HttpException( 'om_backup_not_found', esc_html__( 'That backup no longer exists.', 'migratex' ), 404 ); } // A slice of the host's time, never more than a few seconds, so the request is well inside any limit. $step = Library::checksumStep( $file, min( 8.0, Environment::tickSeconds() ) ); if ( null === $step || 'error' === $step['status'] ) { throw new HttpException( 'om_backup_checksum', esc_html__( 'The checksum could not be calculated. Check that the file is readable.', 'migratex' ), 500 ); } $data = array( 'status' => $step['status'], 'file' => $file, 'algo' => 'SHA-256', 'size' => $step['size'], 'size_human' => Format::bytes( $step['size'] ), 'created' => wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $step['mtime'] ), 'percent' => $step['percent'], 'done' => $step['done'], ); if ( 'ready' === $step['status'] ) { $data['sha256'] = $step['sha256']; $data['computed'] = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) ( $step['computed_at'] ?? time() ) ); } return Response::data( $data ); } public function delete( Request $request ): Response { $file = $request->str( 'file' ); if ( null === Library::path( $file ) ) { throw new HttpException( 'om_backup_not_found', esc_html__( 'That backup no longer exists.', 'migratex' ), 404 ); } $path = (string) Library::path( $file ); foreach ( $this->store->ids() as $id ) { $job = $this->store->load( $id ); if ( null !== $job && ! $job->isTerminal() && $job->get( 'archive_path' ) === $path ) { throw new HttpException( 'om_backup_in_use', esc_html__( 'This backup is being restored right now and cannot be deleted.', 'migratex' ), 409 ); } } if ( ! Library::delete( $file ) ) { throw new HttpException( 'om_backup_delete', esc_html__( 'The backup could not be deleted. Check file permissions.', 'migratex' ), 500 ); } return Response::data( array( 'backups' => Library::all() ) ); } }