*/ private array $entries = []; /** * The entry being streamed, if any. * * @var array{name: string, method: int, offset: int, size: int, compressed: int, hash: \HashContext, deflate: \DeflateContext|null, temp: resource|null}|null */ private ?array $open = null; /** * Whether deflate (zlib) is used. * * @var bool */ private bool $deflate; /** * MS-DOS time of all entries. * * @var int */ private int $dos_time; /** * MS-DOS date of all entries. * * @var int */ private int $dos_date; /** * Constructor. * * @param resource $out Writable stream (php://output, php://temp, ...). * @param bool $compress Deflate when zlib is available; false always stores. * @param int|null $timestamp Modification time of the entries; default now. */ public function __construct( $out, bool $compress = true, ?int $timestamp = null ) { $this->out = $out; $this->deflate = $compress && function_exists( 'deflate_init' ); $t = getdate( $timestamp ?? time() ); $this->dos_time = ( $t['hours'] << 11 ) | ( $t['minutes'] << 5 ) | intdiv( $t['seconds'], 2 ); $this->dos_date = ( max( 0, $t['year'] - 1980 ) << 9 ) | ( $t['mon'] << 5 ) | $t['mday']; } /** * Add a small entry whose content is already in memory. * * @param string $name Path inside the archive (ASCII). * @param string $data Content. */ public function add( string $name, string $data ): void { $this->refuse_while_open(); $method = self::STORE; $body = $data; if ( $this->deflate ) { $method = self::DEFLATE; $body = self::deflate_whole( $data ); } $entry = [ 'name' => $name, 'flags' => 0, 'method' => $method, 'crc' => crc32( $data ), 'compressed' => strlen( $body ), 'size' => strlen( $data ), 'offset' => $this->offset, ]; $this->emit( $this->local_header( $entry ) ); $this->emit( $body ); $this->entries[] = $entry; } /** * Start a streamed entry. Feed it with write(), finish it with end(). * * @param string $name Path inside the archive (ASCII). * @throws \RuntimeException When zlib cannot start a deflate stream. */ public function begin( string $name ): void { $this->refuse_while_open(); $deflate = null; $temp = null; if ( $this->deflate ) { $deflate = deflate_init( ZLIB_ENCODING_RAW, [ 'level' => 6 ] ); if ( false === $deflate ) { throw new \RuntimeException( 'Could not start a deflate stream.' ); } } else { $temp = fopen( 'php://temp/maxmemory:2097152', 'w+b' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( false === $temp ) { throw new \RuntimeException( 'Could not open a temporary stream.' ); } } $this->open = [ 'name' => $name, 'method' => $deflate ? self::DEFLATE : self::STORE, 'offset' => $this->offset, 'size' => 0, 'compressed' => 0, 'hash' => hash_init( 'crc32b' ), 'deflate' => $deflate, 'temp' => $temp, ]; if ( $deflate ) { // Sizes are unknown yet: zeros here, the real values follow the data. $this->emit( $this->local_header( [ 'name' => $name, 'flags' => self::FLAG_DESCRIPTOR, 'method' => self::DEFLATE, 'crc' => 0, 'compressed' => 0, 'size' => 0, ] ) ); } } /** * Append to the streamed entry. * * @param string $data Uncompressed bytes. * @throws \LogicException When no entry is open. */ public function write( string $data ): void { if ( null === $this->open ) { throw new \LogicException( 'No ZIP entry is open.' ); } if ( '' === $data ) { return; } hash_update( $this->open['hash'], $data ); $this->open['size'] += strlen( $data ); if ( $this->open['deflate'] ) { $this->open['compressed'] += $this->emit( (string) deflate_add( $this->open['deflate'], $data, ZLIB_NO_FLUSH ) ); } elseif ( $this->open['temp'] ) { fwrite( $this->open['temp'], $data ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite $this->open['compressed'] += strlen( $data ); } } /** * Push what zlib holds so far to the destination, so a long download keeps * moving. */ public function flush(): void { if ( null !== $this->open && $this->open['deflate'] ) { $this->open['compressed'] += $this->emit( (string) deflate_add( $this->open['deflate'], '', ZLIB_SYNC_FLUSH ) ); } fflush( $this->out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fflush } /** * Finish the streamed entry. * * @throws \LogicException When no entry is open. */ public function end(): void { if ( null === $this->open ) { throw new \LogicException( 'No ZIP entry is open.' ); } $open = $this->open; if ( $open['deflate'] ) { $open['compressed'] += $this->emit( (string) deflate_add( $open['deflate'], '', ZLIB_FINISH ) ); } $entry = [ 'name' => $open['name'], 'flags' => $open['deflate'] ? self::FLAG_DESCRIPTOR : 0, 'method' => $open['method'], 'crc' => (int) hexdec( hash_final( $open['hash'] ) ), 'compressed' => $open['compressed'], 'size' => $open['size'], 'offset' => $open['offset'], ]; $this->open = null; $this->guard( $entry['size'] ); if ( $open['deflate'] ) { $this->emit( pack( 'VVVV', 0x08074b50, $entry['crc'], $entry['compressed'], $entry['size'] ) ); } elseif ( $open['temp'] ) { $this->emit( $this->local_header( $entry ) ); rewind( $open['temp'] ); $copied = stream_copy_to_stream( $open['temp'], $this->out ); fclose( $open['temp'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose $this->offset += (int) $copied; } $this->entries[] = $entry; } /** * Write the central directory. The archive is complete after this. */ public function finish(): void { if ( null !== $this->open ) { $this->end(); } $start = $this->offset; foreach ( $this->entries as $e ) { $this->emit( pack( 'VvvvvvvVVVvvvvvVV', 0x02014b50, 20, 20, $e['flags'], $e['method'], $this->dos_time, $this->dos_date, $e['crc'], $e['compressed'], $e['size'], strlen( $e['name'] ), 0, 0, 0, 0, 0, $e['offset'] ) . $e['name'] ); } $count = count( $this->entries ); $this->guard( $this->offset ); $this->emit( pack( 'VvvvvVVv', 0x06054b50, 0, 0, $count, $count, $this->offset - $start, $start, 0 ) ); fflush( $this->out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fflush } /** * Local file header. * * @param array{name: string, flags: int, method: int, crc: int, compressed: int, size: int} $e Entry. */ private function local_header( array $e ): string { return pack( 'VvvvvvVVVvv', 0x04034b50, 20, $e['flags'], $e['method'], $this->dos_time, $this->dos_date, $e['crc'], $e['compressed'], $e['size'], strlen( $e['name'] ), 0 ) . $e['name']; } /** * Raw deflate of a whole string. * * @param string $data Content. * @throws \RuntimeException When zlib fails. */ private static function deflate_whole( string $data ): string { $context = deflate_init( ZLIB_ENCODING_RAW, [ 'level' => 6 ] ); $out = false === $context ? false : deflate_add( $context, $data, ZLIB_FINISH ); if ( false === $out ) { throw new \RuntimeException( 'Could not deflate a ZIP entry.' ); } return $out; } /** * Write to the destination and keep the offset. * * @param string $bytes Bytes. * @return int Bytes written. */ private function emit( string $bytes ): int { if ( '' === $bytes ) { return 0; } fwrite( $this->out, $bytes ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite $this->offset += strlen( $bytes ); return strlen( $bytes ); } /** * Entries are written one after the other. * * @throws \LogicException While a streamed entry is open. */ private function refuse_while_open(): void { if ( null !== $this->open ) { throw new \LogicException( 'Finish the streamed ZIP entry first.' ); } } /** * Refuse sizes that need ZIP64. * * @param int $bytes Size or offset. * @throws \OverflowException Above 4 GB. */ private function guard( int $bytes ): void { if ( $bytes > self::MAX ) { throw new \OverflowException( 'The export is larger than 4 GB.' ); } } }