path = $path; $this->cd_path = $cd_path; } /** * Open both files and rewind them to a saved cursor. * * @param array $cursor Keys: offset, cd, count. * @return true|\WP_Error */ public function open( array $cursor ) { $this->offset = isset( $cursor['offset'] ) ? (int) $cursor['offset'] : 0; $this->cd_len = isset( $cursor['cd'] ) ? (int) $cursor['cd'] : 0; $this->count = isset( $cursor['count'] ) ? (int) $cursor['count'] : 0; // A resumed archive that has vanished cannot be rebuilt from a cursor. if ( $this->offset > 0 && ( ! file_exists( $this->path ) || ! file_exists( $this->cd_path ) ) ) { return new \WP_Error( 'sitezip_zip_missing', __( 'The partially written backup file disappeared.', 'sitezip' ) ); } $this->fh = @fopen( $this->path, 'c+b' ); $this->cd = @fopen( $this->cd_path, 'c+b' ); if ( ! $this->fh || ! $this->cd ) { $this->close(); return new \WP_Error( 'sitezip_zip_open', __( 'Could not open the backup file for writing. Check that wp-content is writable.', 'sitezip' ) ); } clearstatcache( true, $this->path ); clearstatcache( true, $this->cd_path ); if ( filesize( $this->path ) < $this->offset || filesize( $this->cd_path ) < $this->cd_len ) { $this->close(); return new \WP_Error( 'sitezip_zip_short', __( 'The partially written backup file is shorter than expected.', 'sitezip' ) ); } ftruncate( $this->fh, $this->offset ); ftruncate( $this->cd, $this->cd_len ); fseek( $this->fh, $this->offset ); fseek( $this->cd, $this->cd_len ); return true; } /** * Position to persist so the next request can resume. * * @return array */ public function cursor() { return array( 'offset' => $this->offset, 'cd' => $this->cd_len, 'count' => $this->count, ); } /** * Release file handles. * * @return void */ public function close() { if ( $this->fh ) { fclose( $this->fh ); } if ( $this->cd ) { fclose( $this->cd ); } $this->fh = null; $this->cd = null; } /** * Add a file from disk. * * @param string $abs_path Source file. * @param string $entry Entry name inside the archive. * @return bool|\WP_Error True when added, false when the source could not be read (skipped), WP_Error when the archive could not be written. */ public function add_file( $abs_path, $entry ) { $in = @fopen( $abs_path, 'rb' ); if ( ! $in ) { return false; } $size = (int) @filesize( $abs_path ); $mtime = (int) @filemtime( $abs_path ); $ext = strtolower( pathinfo( $entry, PATHINFO_EXTENSION ) ); $store = $size > self::DEFLATE_MAX_BYTES || in_array( $ext, self::STORE_EXTENSIONS, true ); $result = $this->write_entry( $entry, $mtime, $size, $store, $in ); fclose( $in ); return $result; } /** * Add an entry from a string. * * @param string $entry Entry name. * @param string $contents Contents. * @return bool|\WP_Error */ public function add_string( $entry, $contents ) { $in = fopen( 'php://memory', 'w+b' ); fwrite( $in, $contents ); rewind( $in ); $result = $this->write_entry( $entry, time(), strlen( $contents ), false, $in ); fclose( $in ); return $result; } /** * Write the central directory and end records, then close. * * @return true|\WP_Error */ public function finish() { $cd_offset = $this->offset; fseek( $this->cd, 0 ); $copied = stream_copy_to_stream( $this->cd, $this->fh ); if ( false === $copied || (int) $copied !== $this->cd_len ) { return $this->write_error(); } $this->offset += $this->cd_len; $needs_zip64 = $this->count >= self::U16 || $cd_offset >= self::U32 || $this->cd_len >= self::U32; $tail = ''; if ( $needs_zip64 ) { $eocd64_offset = $this->offset; $tail .= pack( 'V', 0x06064b50 ) . self::pack64( 44 ) . pack( 'vvVV', 45, 45, 0, 0 ) . self::pack64( $this->count ) . self::pack64( $this->count ) . self::pack64( $this->cd_len ) . self::pack64( $cd_offset ); $tail .= pack( 'VV', 0x07064b50, 0 ) . self::pack64( $eocd64_offset ) . pack( 'V', 1 ); } $tail .= pack( 'VvvvvVVv', 0x06054b50, 0, 0, min( $this->count, self::U16 ), min( $this->count, self::U16 ), min( $this->cd_len, self::U32 ), min( $cd_offset, self::U32 ), 0 ); if ( ! $this->write( $tail ) ) { return $this->write_error(); } fflush( $this->fh ); $this->close(); return true; } /** * Stream one entry into the archive. * * The local header is written with placeholder CRC and sizes, the data * is streamed after it, and the header is patched in place afterwards, * so no entry is ever held in memory. * * @param string $entry Entry name. * @param int $mtime Modification time. * @param int $size Expected uncompressed size. * @param bool $store Whether to skip compression. * @param resource $in Source stream. * @return bool|\WP_Error */ private function write_entry( $entry, $mtime, $size, $store, $in ) { $deflate = null; if ( ! $store && function_exists( 'deflate_init' ) ) { $deflate = deflate_init( ZLIB_ENCODING_RAW, array( 'level' => 1 ) ); } $method = $deflate ? 8 : 0; $header_offset = $this->offset; $local_zip64 = $size >= self::U32; list( $dos_time, $dos_date ) = self::dos_time( $mtime ); // Bit 11: entry names are UTF-8. $flags = 0x0800; $version = $local_zip64 ? 45 : 20; // Exact UTC modification time (Info-ZIP "UT" field). DOS times are // local and 2-second, so restore uses this to spot unchanged files. $stamp = $mtime > 0 ? pack( 'vvCV', 0x5455, 5, 1, $mtime ) : ''; $extra = ( $local_zip64 ? pack( 'vv', 0x0001, 16 ) . self::pack64( 0 ) . self::pack64( 0 ) : '' ) . $stamp; $name_len = strlen( $entry ); $size_mark = $local_zip64 ? self::U32 : 0; $header = pack( 'VvvvvvVVVvv', 0x04034b50, $version, $flags, $method, $dos_time, $dos_date, 0, $size_mark, $size_mark, $name_len, strlen( $extra ) ) . $entry . $extra; if ( ! $this->write( $header ) ) { return $this->write_error(); } $crc = hash_init( 'crc32b' ); $usize = 0; $csize = 0; while ( ! feof( $in ) ) { $chunk = fread( $in, self::READ_BYTES ); if ( false === $chunk ) { break; } if ( '' === $chunk ) { continue; } hash_update( $crc, $chunk ); $usize += strlen( $chunk ); $out = $deflate ? deflate_add( $deflate, $chunk, ZLIB_NO_FLUSH ) : $chunk; if ( '' !== $out ) { if ( ! $this->write( $out ) ) { return $this->write_error(); } $csize += strlen( $out ); } } if ( $deflate ) { $out = deflate_add( $deflate, '', ZLIB_FINISH ); if ( ! $this->write( $out ) ) { return $this->write_error(); } $csize += strlen( $out ); } // The file grew past 4 GB while being read: without ZIP64 fields in // the local header this entry cannot be described, so drop it. if ( ! $local_zip64 && ( $usize >= self::U32 || $csize >= self::U32 ) ) { $this->rewind_to( $header_offset ); return false; } $crc_value = unpack( 'N', hash_final( $crc, true ) ); $crc_value = $crc_value[1]; // Patch CRC and sizes into the local header. fseek( $this->fh, $header_offset + 14 ); if ( $local_zip64 ) { fwrite( $this->fh, pack( 'V', $crc_value ) ); fseek( $this->fh, $header_offset + 30 + $name_len + 4 ); fwrite( $this->fh, self::pack64( $usize ) . self::pack64( $csize ) ); } else { fwrite( $this->fh, pack( 'VVV', $crc_value, $csize, $usize ) ); } fseek( $this->fh, $this->offset ); // Central directory record, with ZIP64 fields only where needed. $cd_extra = ''; if ( $usize >= self::U32 ) { $cd_extra .= self::pack64( $usize ); } if ( $csize >= self::U32 ) { $cd_extra .= self::pack64( $csize ); } if ( $header_offset >= self::U32 ) { $cd_extra .= self::pack64( $header_offset ); } if ( '' !== $cd_extra ) { $cd_extra = pack( 'vv', 0x0001, strlen( $cd_extra ) ) . $cd_extra; } $cd_version = ( '' !== $cd_extra || $local_zip64 ) ? 45 : 20; $cd_extra .= $stamp; $record = pack( 'VvvvvvvVVVvvvvvVV', 0x02014b50, 0x0300 | $cd_version, // Made by: Unix. $cd_version, $flags, $method, $dos_time, $dos_date, $crc_value, min( $csize, self::U32 ), min( $usize, self::U32 ), $name_len, strlen( $cd_extra ), 0, 0, 0, ( 0100644 << 16 ), // Regular file, rw-r--r--. min( $header_offset, self::U32 ) ) . $entry . $cd_extra; if ( false === fwrite( $this->cd, $record ) ) { return $this->write_error(); } $this->cd_len += strlen( $record ); $this->count++; fflush( $this->fh ); fflush( $this->cd ); return true; } /** * Write bytes to the archive and advance the offset. * * @param string $bytes Bytes. * @return bool */ private function write( $bytes ) { $length = strlen( $bytes ); if ( 0 === $length ) { return true; } $written = fwrite( $this->fh, $bytes ); if ( $written !== $length ) { return false; } $this->offset += $length; return true; } /** * Discard a half-written entry. * * @param int $offset Offset to rewind to. * @return void */ private function rewind_to( $offset ) { ftruncate( $this->fh, $offset ); fseek( $this->fh, $offset ); $this->offset = $offset; } /** * Error for a failed archive write, usually a full disk. * * @return \WP_Error */ private function write_error() { return new \WP_Error( 'sitezip_zip_write', __( 'Could not write to the backup file. The disk may be full.', 'sitezip' ) ); } /** * Little-endian unsigned 64-bit integer. * * @param int $value Value. * @return string */ private static function pack64( $value ) { return pack( 'P', (int) $value ); } /** * MS-DOS time and date fields. * * @param int $timestamp Unix timestamp. * @return int[] Time, date. */ private static function dos_time( $timestamp ) { $parts = getdate( max( 315532800, (int) $timestamp ) ); return array( ( $parts['hours'] << 11 ) | ( $parts['minutes'] << 5 ) | ( $parts['seconds'] >> 1 ), ( ( $parts['year'] - 1980 ) << 9 ) | ( $parts['mon'] << 5 ) | $parts['mday'], ); } }