= 6). * @return string 'aisv' | 'gzip' | 'zip' | 'unknown'. */ public static function head_type( $bytes ) { $bytes = (string) $bytes; if ( 0 === strncmp( $bytes, self::AISV_MAGIC, 6 ) ) { return 'aisv'; } if ( 0 === strncmp( $bytes, "\x1f\x8b", 2 ) ) { return 'gzip'; } if ( 0 === strncmp( $bytes, "PK\x03\x04", 4 ) || 0 === strncmp( $bytes, "PK\x05\x06", 4 ) || // Empty archive. 0 === strncmp( $bytes, "PK\x07\x08", 4 ) // Spanned archive marker. ) { return 'zip'; } return 'unknown'; } /** * True if these leading bytes are an accepted upload container (raw .aisv, or a * .gz / .zip we know how to unwrap). * * @param string $bytes First bytes. * @return bool */ public static function is_accepted_head( $bytes ) { return 'unknown' !== self::head_type( $bytes ); } /** * Normalise $src into a raw .aisv (plus any .aisv.N volumes) at $dest. * * On success $dest is a valid .aisv and $src has been consumed (renamed or deleted). * On failure nothing usable is left behind. * * @param string $src Source path (raw .aisv, or a .gz/.zip wrapper). * @param string $dest Destination .aisv path (volume 1). * @return true|\WP_Error */ public static function to_aisv( $src, $dest ) { $type = self::sniff( $src ); if ( 'aisv' === $type ) { if ( ! @rename( $src, $dest ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename return new \WP_Error( 'aisiteark_finalize', __( 'Could not finalize the uploaded backup.', 'ai-siteark' ) ); } } elseif ( 'gzip' === $type ) { $res = self::gunzip( $src, $dest ); if ( is_wp_error( $res ) ) { return $res; } @unlink( $src ); } elseif ( 'zip' === $type ) { $res = self::unzip( $src, $dest ); if ( is_wp_error( $res ) ) { return $res; } @unlink( $src ); } else { return new \WP_Error( 'aisiteark_not_container', __( 'That file is not an AI-SiteArk (.aisv) backup, nor a .zip or .gz containing one.', 'ai-siteark' ) ); } // Final safety gate — let a security plugin / host AV veto the backup. $scan = self::scan( $dest ); if ( is_wp_error( $scan ) ) { self::cleanup_volumes( $dest ); return $scan; } return true; } /** * Decompress a gzip stream to $dest, verifying it is a .aisv. * * @param string $src Gzip file. * @param string $dest Destination .aisv. * @return true|\WP_Error */ private static function gunzip( $src, $dest ) { if ( ! function_exists( 'gzopen' ) ) { return new \WP_Error( 'aisiteark_no_zlib', __( 'This server cannot decompress .gz files (the zlib PHP extension is missing). Upload the .aisv file directly.', 'ai-siteark' ) ); } $in = @gzopen( $src, 'rb' ); $out = @fopen( $dest, 'wb' ); if ( ! $in || ! $out ) { if ( $in ) { gzclose( $in ); } if ( $out ) { fclose( $out ); } @unlink( $dest ); return new \WP_Error( 'aisiteark_gz_open', __( 'Could not open the .gz archive.', 'ai-siteark' ) ); } $first = (string) gzread( $in, 6 ); if ( self::AISV_MAGIC !== $first ) { gzclose( $in ); fclose( $out ); @unlink( $dest ); return new \WP_Error( 'aisiteark_gz_notaisv', __( 'The .gz file does not contain an AI-SiteArk (.aisv) backup.', 'ai-siteark' ) ); } fwrite( $out, $first ); while ( ! gzeof( $in ) ) { $buf = gzread( $in, self::CHUNK ); if ( false === $buf || '' === $buf ) { break; } if ( false === fwrite( $out, $buf ) ) { gzclose( $in ); fclose( $out ); @unlink( $dest ); return new \WP_Error( 'aisiteark_gz_write', __( 'Could not write the decompressed backup to disk (check free space and folder permissions).', 'ai-siteark' ) ); } } gzclose( $in ); fclose( $out ); return true; } /** * Extract the .aisv backup (and any .aisv.N volumes) out of a .zip, ignoring every * other entry. Each volume is written to a name WE derive from $dest, never from the * entry's own name — so a malicious entry name cannot escape the backups dir. * * @param string $src Zip file. * @param string $dest Destination .aisv (volume 1). * @return true|\WP_Error */ private static function unzip( $src, $dest ) { if ( ! class_exists( '\ZipArchive' ) ) { return new \WP_Error( 'aisiteark_no_zip', __( 'This server cannot open .zip files (the zip PHP extension is missing). Upload the .aisv, or a .gz, instead.', 'ai-siteark' ) ); } $zip = new \ZipArchive(); if ( true !== $zip->open( $src ) ) { return new \WP_Error( 'aisiteark_zip_open', __( 'Could not open the .zip archive — it may be corrupt.', 'ai-siteark' ) ); } // Map each .aisv[.N] entry to its volume index (1 = the base .aisv). $vols = array(); for ( $i = 0; $i < $zip->numFiles; $i++ ) { $name = (string) $zip->getNameIndex( $i ); if ( '' === $name || '/' === substr( $name, -1 ) ) { continue; // Directory entry. } $base = strtolower( basename( str_replace( '\\', '/', $name ) ) ); if ( preg_match( '/\.aisv$/', $base ) ) { $vols[1] = $i; } elseif ( preg_match( '/\.aisv\.(\d+)$/', $base, $m ) ) { $vols[ (int) $m[1] ] = $i; } // Everything else is deliberately ignored — never extracted. } if ( empty( $vols[1] ) && 0 === count( $vols ) ) { $zip->close(); return new \WP_Error( 'aisiteark_zip_notaisv', __( 'The .zip does not contain an AI-SiteArk (.aisv) backup.', 'ai-siteark' ) ); } if ( empty( $vols[1] ) ) { $zip->close(); return new \WP_Error( 'aisiteark_zip_novol1', __( 'The .zip is missing the first backup volume (the .aisv file).', 'ai-siteark' ) ); } ksort( $vols ); foreach ( $vols as $vol => $idx ) { $target = ( 1 === $vol ) ? $dest : $dest . '.' . $vol; $res = self::stream_zip_entry( $zip, $idx, $target, ( 1 === $vol ) ); if ( is_wp_error( $res ) ) { $zip->close(); self::cleanup_volumes( $dest ); return $res; } } $zip->close(); return true; } /** * Stream one zip entry to $target. If $verify_magic, the entry must begin with the * .aisv magic (guards the primary volume). * * @param \ZipArchive $zip Open archive. * @param int $idx Entry index. * @param string $target Destination path. * @param bool $verify_magic Require the .aisv magic at the head. * @return true|\WP_Error */ private static function stream_zip_entry( $zip, $idx, $target, $verify_magic ) { $name = (string) $zip->getNameIndex( $idx ); $in = $zip->getStream( $name ); if ( ! $in ) { return new \WP_Error( 'aisiteark_zip_read', __( 'Could not read the backup inside the .zip.', 'ai-siteark' ) ); } $out = @fopen( $target, 'wb' ); if ( ! $out ) { fclose( $in ); return new \WP_Error( 'aisiteark_zip_write', __( 'Could not write the extracted backup to disk (check free space and folder permissions).', 'ai-siteark' ) ); } if ( $verify_magic ) { $first = (string) fread( $in, 6 ); if ( self::AISV_MAGIC !== $first ) { fclose( $in ); fclose( $out ); @unlink( $target ); return new \WP_Error( 'aisiteark_zip_notaisv', __( 'The file inside the .zip is not an AI-SiteArk (.aisv) backup.', 'ai-siteark' ) ); } fwrite( $out, $first ); } while ( ! feof( $in ) ) { $buf = fread( $in, self::CHUNK ); if ( false === $buf || '' === $buf ) { break; } if ( false === fwrite( $out, $buf ) ) { fclose( $in ); fclose( $out ); @unlink( $target ); return new \WP_Error( 'aisiteark_zip_write', __( 'Could not write the extracted backup to disk (check free space and folder permissions).', 'ai-siteark' ) ); } } fclose( $in ); fclose( $out ); return true; } /** * Delete $dest and every .aisv.N sibling volume (cleanup after a failed unwrap). * * @param string $dest Volume-1 path. * @return void */ private static function cleanup_volumes( $dest ) { if ( is_file( $dest ) ) { @unlink( $dest ); } $i = 2; while ( is_file( $dest . '.' . $i ) ) { @unlink( $dest . '.' . $i ); ++$i; if ( $i > 4096 ) { break; // Sanity stop. } } } /** * Optional security scan of an uploaded backup, immediately before it becomes * restorable. Extensible so a host AV / security plugin (or Pro's ClamAV bridge) * can plug in without this class shipping an engine of its own. * * NOTE: a full-site backup legitimately contains the site's own PHP (WordPress core, * every plugin and theme), so a generic AV pass is of limited value and prone to * false positives — the real protection is that this class only ever extracts a * single, magic-verified .aisv and never writes an attacker-named file. The hook is * here for sites that want a scanner in the loop anyway. * * @param string $path The finalized .aisv path. * @return true|\WP_Error true to allow; WP_Error to block the restore. */ public static function scan( $path ) { /** * Filter: scan an uploaded backup before it can be restored. * * Return a WP_Error (or boolean false) to REJECT the file; return true (default) * to allow it. Handlers receive the absolute path to the finalized .aisv. * * @param bool|\WP_Error $verdict Allow (true) by default. * @param string $path Absolute path to the .aisv. */ $verdict = apply_filters( 'aisiteark_scan_upload', true, $path ); if ( is_wp_error( $verdict ) ) { return $verdict; } if ( false === $verdict ) { return new \WP_Error( 'aisiteark_scan_reject', __( 'A security scan rejected this backup, so the restore was blocked.', 'ai-siteark' ) ); } return true; } }