(string) $path, 'size' => 0, 'entries' => array(), ); } /** * Bytes an entry adds to the part (local header plus data plus central directory record). * * @param string $name Entry name. * @param int $bytes Data size. */ public static function cost( $name, $bytes ) { return 30 + 46 + 2 * strlen( $name ) + (int) $bytes; } /** * Adds bytes held in memory. * * @param array $state Writer state (updated). * @param string $name Entry name. * @param string $data Contents. * @param int $mtime Modification time. * @return array{crc: string, sha256: string, size: int} * @throws \RuntimeException On a write failure or a full part. */ public static function add_string( array &$state, $name, $data, $mtime ) { $data = (string) $data; $crc = hash( 'crc32b', $data ); $fh = self::append_handle( $state, $name, strlen( $data ) ); $ok = self::write_all( $fh, self::local_header( $name, $crc, strlen( $data ), $mtime ) ) && self::write_all( $fh, $data ); fclose( $fh ); if ( ! $ok ) { self::rollback( $state ); throw new \RuntimeException( 'Could not write to the archive part. Is the disk full?' ); } self::commit( $state, $name, $crc, strlen( $data ), $mtime ); return array( 'crc' => $crc, 'sha256' => hash( 'sha256', $data ), 'size' => strlen( $data ), ); } /** * Adds a file, or a range of it. * * Big files are read twice: once for the checksums (stored before the data) and once to copy. * If the file changes between the two reads, the entry is dropped and an exception says so. * * @param array $state Writer state (updated). * @param string $name Entry name. * @param string $src Source file. * @param int $offset Start of the range. * @param int|null $length Length of the range (null: to the end). * @param int|null $mtime Modification time (null: the file's). * @return array{crc: string, sha256: string, size: int} * @throws \RuntimeException When the file can't be read or changed while being added. */ public static function add_file( array &$state, $name, $src, $offset = 0, $length = null, $mtime = null ) { clearstatcache( true, $src ); $total = @filesize( $src ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( false === $total ) { throw new \RuntimeException( sprintf( 'Could not read %s.', $src ) ); } $offset = max( 0, (int) $offset ); $length = null === $length ? max( 0, $total - $offset ) : (int) $length; if ( $offset + $length > $total ) { throw new \RuntimeException( sprintf( '%s is shorter than expected (it changed while being backed up).', $src ) ); } $mtime = null === $mtime ? (int) @filemtime( $src ) : (int) $mtime; // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( $length <= self::IN_MEMORY ) { $data = 0 === $length ? '' : @file_get_contents( $src, false, null, $offset, $length ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( ! is_string( $data ) || strlen( $data ) !== $length ) { throw new \RuntimeException( sprintf( 'Could not read %s.', $src ) ); } return self::add_string( $state, $name, $data, $mtime ); } // Pass 1: checksums. $in = @fopen( $src, 'rb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( ! $in ) { throw new \RuntimeException( sprintf( 'Could not read %s.', $src ) ); } $crc_ctx = hash_init( 'crc32b' ); $sha_ctx = hash_init( 'sha256' ); fseek( $in, $offset ); $left = $length; while ( $left > 0 ) { $block = fread( $in, (int) min( self::BLOCK, $left ) ); if ( false === $block || '' === $block ) { fclose( $in ); throw new \RuntimeException( sprintf( '%s is shorter than expected (it changed while being backed up).', $src ) ); } hash_update( $crc_ctx, $block ); hash_update( $sha_ctx, $block ); $left -= strlen( $block ); } $crc = hash_final( $crc_ctx ); $sha = hash_final( $sha_ctx ); // Pass 2: copy, checking the data is still the same. $fh = self::append_handle( $state, $name, $length ); $ok = self::write_all( $fh, self::local_header( $name, $crc, $length, $mtime ) ); $check = hash_init( 'crc32b' ); fseek( $in, $offset ); $left = $length; while ( $ok && $left > 0 ) { $block = fread( $in, (int) min( self::BLOCK, $left ) ); if ( false === $block || '' === $block ) { $ok = false; break; } hash_update( $check, $block ); $ok = self::write_all( $fh, $block ); $left -= strlen( $block ); } fclose( $in ); fclose( $fh ); if ( ! $ok || hash_final( $check ) !== $crc ) { self::rollback( $state ); throw new \RuntimeException( sprintf( '%s changed while being backed up, or the disk is full. It will be tried again.', $src ) ); } self::commit( $state, $name, $crc, $length, $mtime ); return array( 'crc' => $crc, 'sha256' => $sha, 'size' => $length, ); } /** * Writes the central directory. The part is then a complete ZIP. * * @param array $state Writer state. * @return int Final size of the part. * @throws \RuntimeException On a write failure. */ public static function finish( array $state ) { $central = ''; foreach ( $state['entries'] as $e ) { list( $time, $date ) = self::dos_time( (int) $e['m'] ); $central .= pack( 'VvvvvvvVVVvvvvvVV', 0x02014b50, 20, // Made by (2.0). 20, // Version needed. self::flags( $e['n'] ), 0, // Stored. $time, $date, hexdec( $e['c'] ), $e['s'], $e['s'], strlen( $e['n'] ), 0, // Extra field length. 0, // Comment length. 0, // Disk number. 0, // Internal attributes. 0, // External attributes. $e['o'] ) . $e['n']; } $count = count( $state['entries'] ); $eocd = pack( 'VvvvvVVv', 0x06054b50, 0, 0, $count, $count, strlen( $central ), $state['size'], 0 ); $fh = self::handle_at( $state ); $ok = self::write_all( $fh, $central . $eocd ); fclose( $fh ); if ( ! $ok ) { throw new \RuntimeException( 'Could not write to the archive part. Is the disk full?' ); } clearstatcache( true, $state['path'] ); return (int) filesize( $state['path'] ); } /** * Opens the part for appending after cutting off anything uncommitted. * * @param array $state Writer state. * @param string $name Entry name (to check limits). * @param int $length Data size (to check limits). * @return resource * @throws \RuntimeException When the entry doesn't fit or the file can't be opened. */ private static function append_handle( array $state, $name, $length ) { if ( count( $state['entries'] ) >= self::MAX_ENTRIES || $state['size'] + self::cost( $name, $length ) + 22 > self::MAX_BYTES ) { throw new \RuntimeException( 'The archive part is full.' ); } if ( strlen( $name ) > 65535 || '' === $name ) { throw new \RuntimeException( 'The entry name is not valid.' ); } return self::handle_at( $state ); } /** * Handle positioned at the committed size (truncating anything after it). * * @param array $state Writer state. * @return resource * @throws \RuntimeException When the part can't be opened. */ private static function handle_at( array $state ) { $fh = @fopen( $state['path'], 'c+b' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( ! $fh ) { throw new \RuntimeException( 'Could not open the archive part.' ); } ftruncate( $fh, (int) $state['size'] ); fseek( $fh, (int) $state['size'] ); return $fh; } /** * Cuts the part back to its committed size. * * @param array $state Writer state. */ private static function rollback( array $state ) { $fh = @fopen( $state['path'], 'c+b' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( $fh ) { ftruncate( $fh, (int) $state['size'] ); fclose( $fh ); } } /** * Records a written entry. * * @param array $state Writer state (updated). * @param string $name Entry name. * @param string $crc CRC-32 (hex). * @param int $size Size. * @param int $mtime Modification time. */ private static function commit( array &$state, $name, $crc, $size, $mtime ) { $state['entries'][] = array( 'n' => $name, 'c' => $crc, 's' => $size, 'm' => $mtime, 'o' => $state['size'], ); $state['size'] += 30 + strlen( $name ) + $size; } /** * Local file header. * * @param string $name Entry name. * @param string $crc CRC-32 (hex). * @param int $size Size. * @param int $mtime Modification time. */ private static function local_header( $name, $crc, $size, $mtime ) { list( $time, $date ) = self::dos_time( $mtime ); return pack( 'VvvvvvVVVvv', 0x04034b50, 20, self::flags( $name ), 0, $time, $date, hexdec( $crc ), $size, $size, strlen( $name ), 0 ) . $name; } /** * General purpose flags: bit 11 marks UTF-8 names. * * @param string $name Entry name. */ private static function flags( $name ) { return preg_match( '//u', $name ) ? 0x0800 : 0; } /** * MS-DOS time and date (UTC; the exact time is also in the backup's index). * * @param int $mtime Unix time. * @return int[] {time, date} */ private static function dos_time( $mtime ) { $mtime = min( 4354819199, max( 315532800, (int) $mtime ) ); // DOS dates run from 1980 to 2107. list( $year, $mon, $day, $hour, $min, $sec ) = array_map( 'intval', explode( ' ', gmdate( 'Y n j G i s', $mtime ) ) ); return array( ( $hour << 11 ) | ( $min << 5 ) | ( $sec >> 1 ), ( ( $year - 1980 ) << 9 ) | ( $mon << 5 ) | $day, ); } /** * Writes all bytes. * * @param resource $fh Handle. * @param string $data Bytes. */ private static function write_all( $fh, $data ) { $len = strlen( $data ); $at = 0; while ( $at < $len ) { $n = fwrite( $fh, 0 === $at ? $data : substr( $data, $at ) ); if ( false === $n || 0 === $n ) { return false; } $at += $n; } return true; } }