.upload/ (Rollbakr_Upload::begin(), append()). * They are the downloads as ROLLBAKR made them: one plain .tar holding the archives (or several * for a big site), or the separate archives of an older download. When they are all there, a * job of type 'upload' unpacks the .tar (if any), checks the archives (hashes against the * manifest, or a manifest made from the dump's header and the archives' contents when none * came) and renames the folder to /. * * @package ROLLBAKR */ defined( 'ABSPATH' ) || exit; /** * Receiving and checking uploaded backups. */ class Rollbakr_Upload { const TYPE = 'upload'; /** Bytes a single piece may carry (the screen sends 4 MB; a little slack). */ const MAX_PIECE = 6 * MB_IN_BYTES; /** Free space that must be left over after the files. */ const SPARE = 100 * MB_IN_BYTES; /** Bytes hashed or scanned at a time. */ const CHUNK = 8388608; /** * Register the job type. * * @return void */ public static function register() { Rollbakr_Job::register( self::TYPE, array( __CLASS__, 'run' ), array( __CLASS__, 'cleanup' ), array( // Closures: the types are registered before 'init', when translations must not be loaded yet. 'label' => static fn() => __( 'Upload', 'rollbakr' ), 'kind' => 'other', 'steps' => array( 'unpack' => static fn() => __( 'Opening the file you sent…', 'rollbakr' ), 'verify' => static fn() => __( 'Checking the files you sent…', 'rollbakr' ), 'scan' => static fn() => __( 'Reading what is in the files…', 'rollbakr' ), 'finish' => static fn() => __( 'Putting the backup in the list…', 'rollbakr' ), ), ) ); } /** * Folder the pieces of a backup are collected in, with a trailing slash. * * @param string $id Backup id. * @return string */ public static function dir( $id ) { return Rollbakr_Storage::dir() . $id . '.upload/'; } /** * Whether a name is one of the files a backup is made of, or a .tar download that holds them * (bundle.tar, or bundle-of.tar for one of several). * * @param string $name File name without the "--" part. * @return bool */ public static function is_valid_name( $name ) { return 'database.sql.gz' === $name || 'manifest.json' === $name || (bool) preg_match( '/^files-[1-9]\d{0,3}\.tar\.gz$/', (string) $name ) || self::is_bundle_name( $name ); } /** * Whether a name is a .tar download (see Rollbakr_Download::bundle_filename()), as parse_name() names it. * * @param string $name File name. * @return bool */ public static function is_bundle_name( $name ) { return (bool) preg_match( '/^bundle(-[1-9]\d{0,2}of[1-9]\d{0,2})?\.tar$/', (string) $name ); } /** * Split a downloaded file's name into the backup id and the file name, or null. * Downloads are named "-.tar", "--of.tar" (Rollbakr_Download::bundle_filename()) * or "--" (Rollbakr_Download::filename()). A .tar is named 'bundle.tar' or * 'bundle-of.tar' here. * * @param string $filename Name as downloaded. * @return array{id:string,name:string}|null */ public static function parse_name( $filename ) { if ( preg_match( '/(\d{4}-\d{2}-\d{2}-\d{6}-[a-z0-9]{6})(?:-(database\.sql\.gz|files-\d+\.tar\.gz|manifest\.json)|(-[1-9]\d{0,2}of[1-9]\d{0,2})?\.tar)$/', (string) $filename, $m ) ) { $name = isset( $m[2] ) && '' !== $m[2] ? $m[2] : 'bundle' . ( $m[3] ?? '' ) . '.tar'; if ( self::is_valid_name( $name ) ) { return array( 'id' => $m[1], 'name' => $name, ); } } return null; } /** * Start receiving a backup: make its folder (empty) and note what is expected. * * @param string $id Backup id (from the file names). * @param array $files Files that will be sent. * @return true|WP_Error */ public static function begin( $id, array $files ) { if ( ! Rollbakr_Storage::is_valid_id( $id ) ) { return new WP_Error( 'rollbakr_upload_id', __( 'The file names do not carry a backup id.', 'rollbakr' ) ); } if ( Rollbakr_Backups::get( $id ) ) { return new WP_Error( 'rollbakr_upload_exists', __( 'This backup is already on the server.', 'rollbakr' ), array( 'id' => $id ) ); } if ( ! Rollbakr_Storage::is_safe() ) { return new WP_Error( 'rollbakr_storage_unsafe', __( 'The backup folder cannot be used, so nothing is received.', 'rollbakr' ) ); } // Sending gigabytes only to be told at the end that a job is in the way would be a waste. $other = Rollbakr_Job::current(); if ( $other && 'running' === $other->status ) { return new WP_Error( 'rollbakr_job_exists', __( 'A job is already waiting to be continued or cancelled.', 'rollbakr' ) ); } $ready = Rollbakr_Storage::protect(); if ( is_wp_error( $ready ) ) { return $ready; } $expected = array(); $total = 0; foreach ( $files as $f ) { $name = (string) ( $f['name'] ?? '' ); $size = (int) ( $f['size'] ?? -1 ); if ( ! self::is_valid_name( $name ) || $size < 0 || isset( $expected[ $name ] ) ) { return new WP_Error( 'rollbakr_upload_name', sprintf( /* translators: %s: file name */ __( '%s is not a file of a ROLLBAKR backup.', 'rollbakr' ), $name ) ); } $expected[ $name ] = $size; $total += $size; } $bundles = array_values( array_filter( array_keys( $expected ), array( __CLASS__, 'is_bundle_name' ) ) ); if ( $bundles && count( $bundles ) !== count( $expected ) ) { return new WP_Error( 'rollbakr_upload_name', __( 'Send either the .tar file(s) of the backup, or its separate files, not both.', 'rollbakr' ) ); } if ( $bundles ) { // Of a split download, every part: bundle-1of3.tar … bundle-3of3.tar. $parts = array(); foreach ( $bundles as $b ) { if ( preg_match( '/^bundle-(\d+)of(\d+)\.tar$/', $b, $pm ) ) { $parts[ (int) $pm[1] ] = (int) $pm[2]; } } $count = $parts ? max( $parts ) : 0; if ( $parts && ( count( $parts ) !== $count || min( $parts ) !== $count || count( $bundles ) !== $count ) ) { return new WP_Error( 'rollbakr_upload_nothing', sprintf( /* translators: %1$d: how many parts (used four times) */ __( 'This backup was downloaded as %1$d files: send every one of them (1of%1$d … %1$dof%1$d).', 'rollbakr' ), $count ) ); } } elseif ( ! isset( $expected['database.sql.gz'] ) && ! isset( $expected['files-1.tar.gz'] ) ) { return new WP_Error( 'rollbakr_upload_nothing', __( 'Choose at least the database (database.sql.gz) or the files (files-1.tar.gz).', 'rollbakr' ) ); } $free = Rollbakr_Storage::free_space(); // A .tar is unpacked next to itself before it is removed, so it needs the room twice. if ( null !== $free && $free < ( $bundles ? 2 * $total : $total ) + self::SPARE ) { return new WP_Error( 'rollbakr_upload_space', sprintf( /* translators: 1: needed, 2: free */ __( 'Not enough free space: about %1$s is needed and %2$s is free.', 'rollbakr' ), size_format( $total + self::SPARE ), size_format( $free ) ) ); } $dir = self::dir( $id ); if ( is_dir( $dir ) ) { Rollbakr_Storage::remove_tree( $dir ); } if ( ! wp_mkdir_p( $dir ) ) { return new WP_Error( 'rollbakr_upload_dir', __( 'The upload folder could not be created.', 'rollbakr' ) ); } file_put_contents( $dir . 'index.php', " $id, 'files' => $expected, 'created' => time(), 'user' => get_current_user_id(), ) ) ); return true; } /** * What begin() noted, or null. * * @param string $id Backup id. * @return array|null */ public static function expected( $id ) { if ( ! Rollbakr_Storage::is_valid_id( $id ) || ! is_file( self::dir( $id ) . 'upload.json' ) ) { return null; } $u = json_decode( (string) file_get_contents( self::dir( $id ) . 'upload.json' ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions return is_array( $u ) && ( $u['id'] ?? '' ) === $id ? $u : null; } /** * Append a piece of a file. The piece must start exactly where the file ends so far. * * @param string $id Backup id. * @param string $name File name. * @param int $offset Where the piece starts. * @param string $data The piece. * @return array{size:int}|WP_Error The file's size afterwards. */ public static function append( $id, $name, $offset, $data ) { $u = self::expected( $id ); if ( ! $u || ! isset( $u['files'][ $name ] ) ) { return new WP_Error( 'rollbakr_upload_missing', __( 'This upload was not started, or the file is not one of it.', 'rollbakr' ) ); } $path = self::dir( $id ) . $name; $size = is_file( $path ) ? (int) filesize( $path ) : 0; if ( (int) $offset !== $size ) { return new WP_Error( 'rollbakr_upload_offset', __( 'The pieces arrived out of order. Start again.', 'rollbakr' ), array( 'size' => $size ) ); } if ( strlen( $data ) > self::MAX_PIECE || $size + strlen( $data ) > (int) $u['files'][ $name ] ) { return new WP_Error( 'rollbakr_upload_size', __( 'The file is larger than announced.', 'rollbakr' ) ); } if ( '' !== $data && false === file_put_contents( $path, $data, FILE_APPEND | LOCK_EX ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions return new WP_Error( 'rollbakr_upload_write', __( 'The piece could not be written (disk full?).', 'rollbakr' ) ); } clearstatcache( true, $path ); return array( 'size' => is_file( $path ) ? (int) filesize( $path ) : 0 ); } /** * All files have arrived: start the job that checks them and puts the backup in the list. * * @param string $id Backup id. * @return Rollbakr_Job|WP_Error */ public static function finish( $id ) { $u = self::expected( $id ); if ( ! $u ) { return new WP_Error( 'rollbakr_upload_missing', __( 'This upload was not started.', 'rollbakr' ) ); } foreach ( (array) $u['files'] as $name => $size ) { $path = self::dir( $id ) . $name; if ( ! is_file( $path ) || (int) filesize( $path ) !== (int) $size ) { return new WP_Error( 'rollbakr_upload_incomplete', sprintf( /* translators: %s: file name */ __( '%s has not arrived completely.', 'rollbakr' ), $name ) ); } } return Rollbakr_Job::start( self::TYPE, array( 'backup' => $id, 'files' => array_keys( (array) $u['files'] ), 'kind' => 'uploaded', ) ); } /** * Throw the pieces away (the administrator gave up, or started over). * * @param string $id Backup id. * @return bool */ public static function discard( $id ) { if ( ! Rollbakr_Storage::is_valid_id( $id ) || ! is_dir( self::dir( $id ) ) ) { return false; } return Rollbakr_Storage::remove_tree( self::dir( $id ) ); } /** * One step of the job. * * @param Rollbakr_Job $job Job. * @param float $deadline Deadline. * @return bool */ public static function run( Rollbakr_Job $job, $deadline ) { switch ( $job->phase ) { case '': self::prepare( $job ); return false; case 'unpack': self::unpack( $job, $deadline ); return false; case 'verify': self::verify( $job, $deadline ); return false; case 'scan': self::scan( $job, $deadline ); return false; case 'finish': return self::finish_job( $job ); default: throw new RuntimeException( esc_html( 'Unknown phase ' . $job->phase ) ); } } /** * Cleanup on failure or cancel: the pieces go, the log stays in .failed or .cancelled. * * @param Rollbakr_Job $job Job. * @return void */ public static function cleanup( Rollbakr_Job $job ) { $id = (string) ( $job->args['backup'] ?? '' ); if ( Rollbakr_Storage::is_valid_id( $id ) && is_dir( self::dir( $id ) ) ) { Rollbakr_Storage::remove_tree( self::dir( $id ) ); } $dir = $job->dir(); $suffix = 'failed' === $job->status ? '.failed' : '.cancelled'; $final = Rollbakr_Storage::dir() . $job->id . $suffix; if ( ! file_exists( $final ) && rename( rtrim( $dir, '/' ), $final ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions $job->state['dir'] = $job->id . $suffix; } } /** * Phase 1: lay out the state. A .tar download is opened first (phase 'unpack'); separate files * go straight to the checks. * * @param Rollbakr_Job $job Job. * @return void */ private static function prepare( Rollbakr_Job $job ) { $id = (string) $job->args['backup']; $files = array_values( array_filter( (array) $job->args['files'], static fn( $n ) => 'manifest.json' !== $n ) ); $bundles = array_values( array_filter( $files, array( __CLASS__, 'is_bundle_name' ) ) ); sort( $bundles, SORT_NATURAL ); $job->log( 'upload ' . $id . ': ' . implode( ', ', (array) $job->args['files'] ) ); $job->state = array( 'backup' => $id, 'files' => array(), 'manifest' => null, 'started' => time(), 'times' => array(), 'summary' => array(), 'unpack' => array( 'todo' => $bundles, 'name' => '', 'offset' => 0, 'entry' => '', 'entry_size' => 0, 'entry_done' => 0, 'out' => array(), ), 'verify' => array(), 'scan' => array(), ); if ( $bundles ) { $job->state['times']['unpack'] = time(); $job->phase = 'unpack'; $job->set_progress( 0, 0, $bundles[0], array( 'unit' => 'bytes' ) ); return; } self::lay_out( $job, $files ); } /** * The archives are in the upload folder (sent one by one, or unpacked from a .tar): read the * manifest if one came, and lay out the checks. * * @param Rollbakr_Job $job Job. * @param string[] $files Archive names (without manifest.json). * @return void */ private static function lay_out( Rollbakr_Job $job, array $files ) { $id = (string) $job->state['backup']; $dir = self::dir( $id ); sort( $files, SORT_NATURAL ); $manifest = null; if ( is_file( $dir . 'manifest.json' ) ) { $manifest = json_decode( (string) file_get_contents( $dir . 'manifest.json' ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( ! is_array( $manifest ) || ( $manifest['id'] ?? '' ) !== $id || ! is_array( $manifest['archives'] ?? null ) ) { throw new RuntimeException( esc_html( __( 'manifest.json is not the manifest of this backup.', 'rollbakr' ) ) ); } foreach ( $files as $name ) { if ( ! isset( $manifest['archives'][ $name ] ) ) { throw new RuntimeException( esc_html( sprintf( /* translators: %s: file name */ __( '%s is not listed in the manifest.', 'rollbakr' ), $name ) ) ); } } foreach ( array_keys( (array) $manifest['archives'] ) as $name ) { if ( ! in_array( $name, $files, true ) ) { throw new RuntimeException( esc_html( sprintf( /* translators: %s: file name */ __( 'The manifest lists %s, which was not sent. Send every file of the backup.', 'rollbakr' ), $name ) ) ); } } $job->log( 'manifest.json present' ); } else { $job->log( 'no manifest.json: it will be made from the files' ); } $job->state['files'] = $files; $job->state['manifest'] = $manifest; $job->state['times']['verify'] = time(); $job->state['verify'] = array( 'todo' => $files, 'name' => '', 'ctx' => '', 'offset' => 0, 'hashes' => array(), ); $job->state['scan'] = array( 'todo' => $files, 'name' => '', 'offset' => 0, 'members' => array(), 'header' => array(), 'tables' => 0, 'rows' => 0, 'roots' => array(), 'count' => 0, 'bytes' => 0, ); $job->phase = 'verify'; $job->set_progress( 0, count( $files ), '', array( 'unit' => 'archives' ) ); } /** * Phase 'unpack': take the archives out of the .tar download(s) into the upload folder, entry by * entry, a piece at a time; then throw the .tar away. Continues from where a step stopped (the * position in the .tar and in the entry). Only entries named like this backup's downloads are * accepted; the name decides where the data goes, so nothing can be written anywhere else. * * @param Rollbakr_Job $job Job. * @param float $deadline Deadline. * @return void */ private static function unpack( Rollbakr_Job $job, $deadline ) { $u = &$job->state['unpack']; $id = (string) $job->state['backup']; $dir = self::dir( $id ); // At least one piece per step, however short the step: a budget that is already spent must // not leave the job where it was. $first = true; while ( ( $u['todo'] || '' !== $u['name'] ) && ( $first || microtime( true ) < $deadline ) ) { if ( '' === $u['name'] ) { $u['name'] = array_shift( $u['todo'] ); $u['offset'] = 0; $u['entry'] = ''; } $path = $dir . $u['name']; $total = (int) filesize( $path ); $fh = fopen( $path, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( ! $fh ) { throw new RuntimeException( esc_html( sprintf( 'Cannot read %s', $u['name'] ) ) ); } fseek( $fh, (int) $u['offset'] ); $done = false; try { while ( $first || microtime( true ) < $deadline ) { $first = false; if ( '' === $u['entry'] ) { $h = (string) fread( $fh, Rollbakr_Untar::BLOCK ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( '' === $h || "\0" === $h[0] ) { $done = true; // The end-of-archive blocks, or the end of the file. break; } $e = self::tar_header( $h ); if ( 'L' === $e['type'] ) { // GNU long name: the data holds the name of the entry that follows. A name has a limit // (and cannot be longer than what is left of the file): a header that says otherwise is // not from ROLLBAKR, and is not read into memory. if ( $e['size'] > 4096 || $e['size'] > $total - (int) ftell( $fh ) ) { throw new RuntimeException( esc_html( sprintf( /* translators: %s: file name */ __( '%s is not a .tar made by ROLLBAKR (a header does not add up).', 'rollbakr' ), $u['name'] ) ) ); } $long = rtrim( (string) fread( $fh, $e['size'] + Rollbakr_Untar::pad( $e['size'] ) ), "\0" ); // phpcs:ignore WordPress.WP.AlternativeFunctions $h = (string) fread( $fh, Rollbakr_Untar::BLOCK ); // phpcs:ignore WordPress.WP.AlternativeFunctions $e = self::tar_header( $h ); $e['name'] = '' !== $long ? $long : $e['name']; } if ( '0' !== $e['type'] ) { throw new RuntimeException( esc_html( sprintf( /* translators: %s: file name */ __( '%s is not a .tar made by ROLLBAKR (it holds something other than the backup\'s files).', 'rollbakr' ), $u['name'] ) ) ); } $p = self::parse_name( basename( str_replace( '\\', '/', $e['name'] ) ) ); if ( ! $p || $p['id'] !== $id || self::is_bundle_name( $p['name'] ) ) { throw new RuntimeException( esc_html( sprintf( /* translators: 1: entry name, 2: .tar name */ __( '%1$s in %2$s is not a file of this backup.', 'rollbakr' ), $e['name'], $u['name'] ) ) ); } if ( isset( $u['out'][ $p['name'] ] ) ) { throw new RuntimeException( esc_html( sprintf( /* translators: %s: file name */ __( '%s appears twice in the .tar file(s).', 'rollbakr' ), $p['name'] ) ) ); } $u['entry'] = $p['name']; $u['entry_size'] = (int) $e['size']; $u['entry_done'] = 0; $u['offset'] = (int) ftell( $fh ); file_put_contents( $dir . $u['entry'], '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions } $out = $dir . $u['entry']; clearstatcache( true, $out ); $have = is_file( $out ) ? (int) filesize( $out ) : 0; if ( $have > (int) $u['entry_done'] ) { // A step that died after writing but before saving its state: cut back to what was noted. Rollbakr_Tar::truncate( $out, (int) $u['entry_done'] ); } elseif ( $have < (int) $u['entry_done'] ) { throw new RuntimeException( esc_html( sprintf( 'Lost part of %s', $u['entry'] ) ) ); } $left = (int) $u['entry_size'] - (int) $u['entry_done']; if ( $left > 0 ) { $data = (string) fread( $fh, min( self::CHUNK, $left ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( '' === $data ) { throw new RuntimeException( esc_html( sprintf( /* translators: %s: file name */ __( '%s ends in the middle of a file. Download it again and send it again.', 'rollbakr' ), $u['name'] ) ) ); } if ( false === file_put_contents( $out, $data, FILE_APPEND | LOCK_EX ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions throw new RuntimeException( esc_html( __( 'The file could not be written (disk full?).', 'rollbakr' ) ) ); } $u['entry_done'] += strlen( $data ); $u['offset'] = (int) ftell( $fh ); } if ( (int) $u['entry_done'] >= (int) $u['entry_size'] ) { fseek( $fh, Rollbakr_Untar::pad( (int) $u['entry_size'] ), SEEK_CUR ); $u['out'][ $u['entry'] ] = (int) $u['entry_size']; $u['offset'] = (int) ftell( $fh ); $u['entry'] = ''; } $job->set_progress( (int) $u['offset'], $total, $u['name'], array( 'unit' => 'bytes' ) ); } } finally { fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions } if ( $done ) { $job->log( sprintf( 'unpacked %s: %s', $u['name'], implode( ', ', array_keys( $u['out'] ) ) ?: '(nothing)' ) ); wp_delete_file( $path ); $u['name'] = ''; } } if ( ! $u['todo'] && '' === $u['name'] ) { $files = array_values( array_filter( array_keys( $u['out'] ), static fn( $n ) => 'manifest.json' !== $n ) ); if ( ! in_array( 'database.sql.gz', $files, true ) && ! in_array( 'files-1.tar.gz', $files, true ) ) { throw new RuntimeException( esc_html( __( 'The .tar file holds neither the database (database.sql.gz) nor the files (files-1.tar.gz).', 'rollbakr' ) ) ); } $job->state['summary']['unpack'] = __( 'The .tar is opened', 'rollbakr' ); self::lay_out( $job, $files ); } } /** * Read a ustar header block (as Rollbakr_Download writes it). * * @param string $h 512 bytes. * @return array{name:string,size:int,type:string} * @throws RuntimeException When it is not a tar header. */ private static function tar_header( $h ) { if ( Rollbakr_Untar::BLOCK !== strlen( $h ) ) { throw new RuntimeException( esc_html( __( 'The .tar file ends in the middle of a header. Download it again and send it again.', 'rollbakr' ) ) ); } $sum = (int) octdec( trim( substr( $h, 148, 8 ) ) ); $calc = 0; for ( $i = 0; $i < Rollbakr_Untar::BLOCK; $i++ ) { $calc += ( $i >= 148 && $i < 156 ) ? 32 : ord( $h[ $i ] ); } if ( $calc !== $sum ) { throw new RuntimeException( esc_html( __( 'This is not a .tar file made by ROLLBAKR (a header does not add up).', 'rollbakr' ) ) ); } $name = rtrim( substr( $h, 0, 100 ), "\0" ); $prefix = rtrim( substr( $h, 345, 155 ), "\0" ); $type = $h[156]; return array( 'name' => '' !== $prefix ? $prefix . '/' . $name : $name, 'size' => (int) octdec( trim( substr( $h, 124, 12 ) ) ), 'type' => "\0" === $type ? '0' : $type, ); } /** * Phase 2: SHA-256 of every archive, against the manifest when there is one. * * @param Rollbakr_Job $job Job. * @param float $deadline Deadline. * @return void */ private static function verify( Rollbakr_Job $job, $deadline ) { $v = &$job->state['verify']; $dir = self::dir( (string) $job->state['backup'] ); $all = count( $job->state['files'] ); while ( ( $v['todo'] || '' !== $v['name'] ) && microtime( true ) < $deadline ) { if ( '' === $v['name'] ) { $v['name'] = array_shift( $v['todo'] ); $v['ctx'] = Rollbakr_Job::save_hash( hash_init( 'sha256' ) ); $v['offset'] = 0; } $job->set_progress( count( $v['hashes'] ), $all, $v['name'], array( 'unit' => 'archives' ) ); $ctx = Rollbakr_Job::load_hash( $v['ctx'] ); $fh = fopen( $dir . $v['name'], 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( ! $fh ) { throw new RuntimeException( esc_html( sprintf( 'Cannot read %s', $v['name'] ) ) ); } fseek( $fh, $v['offset'] ); while ( ! feof( $fh ) && microtime( true ) < $deadline ) { $data = fread( $fh, self::CHUNK ); // phpcs:ignore WordPress.WP.AlternativeFunctions hash_update( $ctx, $data ); $v['offset'] += strlen( $data ); } $done = feof( $fh ); fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( $done ) { $hash = hash_final( $ctx ); $expected = $job->state['manifest']['archives'][ $v['name'] ]['sha256'] ?? null; if ( null !== $expected && ! hash_equals( (string) $expected, $hash ) ) { throw new RuntimeException( esc_html( sprintf( /* translators: %s: file name */ __( '%s does not match the hash in the manifest. Download it again and send it again.', 'rollbakr' ), $v['name'] ) ) ); } $v['hashes'][ $v['name'] ] = array( 'size' => (int) filesize( $dir . $v['name'] ), 'sha256' => $hash, ); $v['name'] = ''; $v['ctx'] = ''; } else { $v['ctx'] = Rollbakr_Job::save_hash( $ctx ); } } if ( ! $v['todo'] && '' === $v['name'] ) { $job->log( 'verified: ' . implode( ', ', array_keys( $v['hashes'] ) ) ); $job->state['summary']['verify'] = __( 'The files match the manifest', 'rollbakr' ); if ( $job->state['manifest'] ) { $job->phase = 'finish'; $job->set_progress( 0, 0, __( 'Finishing', 'rollbakr' ) ); } else { $job->state['summary']['verify'] = __( 'The hashes are computed', 'rollbakr' ); $job->state['times']['scan'] = time(); $job->phase = 'scan'; $job->set_progress( 0, $all, '', array( 'unit' => 'archives' ) ); } } } /** * Phase 3 (no manifest): read the dump's header and count its tables, and the archives' entries, * to make a manifest. * * @param Rollbakr_Job $job Job. * @param float $deadline Deadline. * @return void */ private static function scan( Rollbakr_Job $job, $deadline ) { $sc = &$job->state['scan']; $dir = self::dir( (string) $job->state['backup'] ); $all = count( $job->state['files'] ); while ( ( $sc['todo'] || '' !== $sc['name'] ) && microtime( true ) < $deadline ) { if ( '' === $sc['name'] ) { $sc['name'] = array_shift( $sc['todo'] ); $sc['offset'] = 0; $sc['members'] = array(); } $job->set_progress( $all - count( $sc['todo'] ) - 1, $all, $sc['name'], array( 'unit' => 'archives' ) ); $gz = new Rollbakr_Gz_Reader( $dir . $sc['name'], (int) $sc['offset'], (array) $sc['members'] ); try { if ( 'database.sql.gz' === $sc['name'] ) { $done = self::scan_dump( $gz, $sc, $deadline ); } else { $done = self::scan_tar( $gz, $sc, $deadline ); } // The scanners leave 'offset' at the line or entry to continue from. $sc['members'] = $gz->members; } finally { $gz->close(); } if ( $done ) { $sc['name'] = ''; } } if ( ! $sc['todo'] && '' === $sc['name'] ) { $job->log( sprintf( 'scanned: %d tables, %d rows, %d files, %s, roots %s', $sc['tables'], $sc['rows'], $sc['count'], size_format( $sc['bytes'] ), implode( ',', array_keys( $sc['roots'] ) ) ) ); $job->phase = 'finish'; $job->set_progress( 0, 0, __( 'Finishing', 'rollbakr' ) ); } } /** * Read the dump: the header lines at the start, then count the tables and rows. * * @param Rollbakr_Gz_Reader $gz Dump. * @param array $sc Scan state (by reference). * @param float $deadline Deadline. * @return bool True at the end of the dump. */ private static function scan_dump( Rollbakr_Gz_Reader $gz, array &$sc, $deadline ) { $buf = ''; while ( true ) { $nl = strpos( $buf, "\n" ); while ( false === $nl ) { $d = $gz->read( 1048576 ); if ( '' === $d ) { break; } $buf .= $d; $nl = strpos( $buf, "\n" ); } if ( false === $nl ) { $sc['offset'] = $gz->logical; return true; } $line = substr( $buf, 0, $nl ); $buf = (string) substr( $buf, $nl + 1 ); if ( str_starts_with( $line, '-- ' ) ) { if ( preg_match( '/^-- (Site|Prefix|Site id|ROLLBAKR version|Date): (.*)$/', $line, $m ) ) { $sc['header'][ $m[1] ] = trim( $m[2] ); } elseif ( preg_match( '/^-- WordPress (\S+) \/ PHP (\S+) \/ MySQL (\S+)/', $line, $m ) ) { $sc['header']['versions'] = array( $m[1], $m[2], $m[3] ); } elseif ( str_starts_with( $line, '-- table ' ) ) { ++$sc['tables']; } } elseif ( str_starts_with( $line, '(' ) ) { ++$sc['rows']; } if ( microtime( true ) >= $deadline ) { // Continue next step from the start of the line not yet handled. $sc['offset'] = $gz->logical - strlen( $buf ); return false; } } } /** * Read an archive's headers only: which top folders it holds, how many files, how many bytes. * * @param Rollbakr_Gz_Reader $gz Archive. * @param array $sc Scan state (by reference). * @param float $deadline Deadline. * @return bool True at the end of the archive. */ private static function scan_tar( Rollbakr_Gz_Reader $gz, array &$sc, $deadline ) { $tar = new Rollbakr_Untar( $gz ); while ( microtime( true ) < $deadline ) { $e = $tar->next(); if ( null === $e ) { return true; } $top = explode( '/', trim( str_replace( '\\', '/', $e['name'] ), '/' ) )[0]; if ( '' !== $top ) { $sc['roots'][ $top ] = true; } if ( '0' === $e['type'] ) { ++$sc['count']; $sc['bytes'] += (int) $e['size']; } $tar->skip( $e['size'] ); $sc['offset'] = $gz->logical; // An entry boundary: where the next step continues. } return false; } /** * Phase 4: write (or copy) the manifest, move the folder into the list, write the log. * * @param Rollbakr_Job $job Job. * @return bool */ private static function finish_job( Rollbakr_Job $job ) { $id = (string) $job->state['backup']; $dir = self::dir( $id ); if ( Rollbakr_Backups::get( $id ) ) { throw new RuntimeException( esc_html( __( 'This backup is already on the server.', 'rollbakr' ) ) ); } $m = $job->state['manifest'] ?: self::make_manifest( $job ); $m['kind'] = 'uploaded'; $m['status'] = 'complete'; $m['uploaded'] = time(); $m['uploader'] = (int) $job->user; if ( '' === (string) ( $m['user_login'] ?? '' ) ) { // A made manifest does not know who took the backup: the one who sent it is the best name. $who = get_userdata( (int) $job->user ); $m['user'] = (int) $job->user; $m['user_login'] = $who ? $who->user_login : ''; } file_put_contents( $dir . 'manifest.json', wp_json_encode( $m, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions wp_delete_file( $dir . 'upload.json' ); $final = Rollbakr_Storage::dir() . $id; if ( ! rename( rtrim( $dir, '/' ), $final ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions throw new RuntimeException( 'Cannot rename the upload folder' ); } $job->log( sprintf( 'in the list as %s (%s)', $id, $job->state['manifest'] ? 'manifest sent' : 'manifest made' ) ); // This job's log becomes the backup's log, and its record goes there too. $own = $job->dir(); if ( is_file( $own . 'log.txt' ) ) { file_put_contents( $final . '/log.txt', (string) file_get_contents( $own . 'log.txt' ), FILE_APPEND ); // phpcs:ignore WordPress.WP.AlternativeFunctions } Rollbakr_Storage::remove_tree( $own ); $job->state['dir'] = $id; $job->state['record_as'] = $id; // The record (written when this step ends) names the parts like a backup's does. $job->args['parts'] = (array) ( $m['parts'] ?? array() ); $job->state['summary']['finish'] = __( 'In the list', 'rollbakr' ); $job->set_progress( 1, 1, __( 'Done', 'rollbakr' ) ); return true; } /** * A manifest from what the scan found (no manifest.json was sent). * * @param Rollbakr_Job $job Job. * @return array */ private static function make_manifest( Rollbakr_Job $job ) { global $wpdb; $id = (string) $job->state['backup']; $sc = $job->state['scan']; $h = (array) $sc['header']; $v = (array) ( $h['versions'] ?? array() ); $dbs = in_array( 'database.sql.gz', $job->state['files'], true ); // The parts, from the top folders the archives hold. $main = array(); $content = Rollbakr_Files::content_dir(); foreach ( array( 'themes' => Rollbakr_Storage::normalize( get_theme_root() ), 'plugins' => Rollbakr_Storage::normalize( WP_PLUGIN_DIR ), 'uploads' => Rollbakr_Storage::normalize( wp_get_upload_dir()['basedir'] ), ) as $part => $abs ) { $main[ str_starts_with( $abs, $content . '/' ) ? substr( $abs, strlen( $content ) + 1 ) : $part ] = $part; } $parts = $dbs ? array( 'database' ) : array(); foreach ( array_keys( (array) $sc['roots'] ) as $top ) { $parts[] = $main[ $top ] ?? 'other'; } $parts = array_values( array_intersect( Rollbakr_Files::PARTS, array_unique( $parts ) ) ); // When the backup was made, from the id (the site's clock at the time). $created = time(); try { $dt = DateTime::createFromFormat( 'Y-m-d-His', substr( $id, 0, 17 ), wp_timezone() ); if ( $dt ) { $created = $dt->getTimestamp(); } } catch ( Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement // Keep now. } // An older dump has no site id; a dump of this site's URL and prefix is taken as this site's. $site_id = (string) ( $h['Site id'] ?? '' ); $home = (string) ( $h['Site'] ?? '' ); $inferred = false; if ( '' === $site_id && '' !== $home && Rollbakr_Restore::same_address( home_url(), $home ) && ( $h['Prefix'] ?? '' ) === $wpdb->prefix ) { $site_id = Rollbakr_Storage::site_id(); $inferred = true; } $job->log( 'manifest made from the dump header and the archives' . ( '' === $site_id ? ' (no site id)' : '' ) ); return array( 'format' => 1, 'id' => $id, 'kind' => 'uploaded', 'created' => $created, 'finished' => $created, 'site_id' => $site_id, 'home' => $home, 'siteurl' => $home, 'prefix' => (string) ( $h['Prefix'] ?? '' ), 'content_dir' => basename( Rollbakr_Files::content_dir() ), 'versions' => array( 'wordpress' => (string) ( $v[0] ?? '' ), 'php' => (string) ( $v[1] ?? '' ), 'mysql' => (string) ( $v[2] ?? '' ), // From 1.0.1 the dump says which ROLLBAKR made it; empty for an older one. 'rollbakr' => (string) ( $h['ROLLBAKR version'] ?? '' ), ), 'parts' => $parts, 'user' => 0, 'user_login' => '', 'status' => 'complete', 'database' => array( 'tables' => (int) $sc['tables'], 'rows' => (int) $sc['rows'], 'utc' => true, ), 'files' => array( 'count' => (int) $sc['count'], 'bytes' => (int) $sc['bytes'], 'folders' => 0, 'skipped' => 0, ), 'archives' => $job->state['verify']['hashes'], 'excluded' => array(), 'reconstructed' => true, // The site id was not in the dump (an older backup): taken to be this site's by URL and prefix. 'site_id_inferred' => $inferred, ); } }