91 $file = fopen( $fileName,
'r' );
93 return $zdr->execute();
109 return $zdr->execute();
133 private const ZIP64_EXTRA_HEADER = 0x0001;
136 private const SEGSIZE = 16384;
139 private const GENERAL_UTF8 = 11;
142 private const GENERAL_CD_ENCRYPTED = 13;
153 if ( isset( $options[
'zip64'] ) ) {
154 $this->zip64 = $options[
'zip64'];
163 private function execute() {
165 if ( !$this->file ) {
166 return Status::newFatal(
'zip-file-open-error' );
169 $status = Status::newGood();
171 $this->readEndOfCentralDirectoryRecord();
172 if ( $this->zip64 ) {
173 [ $offset, $size ] = $this->findZip64CentralDirectory();
174 $this->readCentralDirectory( $offset, $size );
176 if ( $this->eocdr[
'CD size'] == 0xffffffff
177 || $this->eocdr[
'CD offset'] == 0xffffffff
178 || $this->eocdr[
'CD entries total'] == 0xffff
180 $this->error(
'zip-unsupported',
'Central directory header indicates ZIP64, ' .
181 'but we are in legacy mode. Rejecting this upload is necessary to avoid ' .
182 'opening vulnerabilities on clients using OpenJDK 7 or later.' );
185 [ $offset, $size ] = $this->findOldCentralDirectory();
186 $this->readCentralDirectory( $offset, $size );
192 fclose( $this->file );
204 private function error( $code, $debugMessage ) {
205 wfDebug( __CLASS__ .
": Fatal error: $debugMessage" );
214 private function readEndOfCentralDirectoryRecord() {
218 'CD start disk' => 2,
219 'CD entries this disk' => 2,
220 'CD entries total' => 2,
223 'file comment length' => 2,
225 $structSize = $this->getStructSize( $info );
226 $startPos = $this->getFileLength() - 65536 - $structSize;
227 if ( $startPos < 0 ) {
231 if ( $this->getFileLength() === 0 ) {
232 $this->error(
'zip-wrong-format',
"The file is empty." );
235 $block = $this->getBlock( $startPos );
236 $sigPos = strrpos( $block,
"PK\x05\x06" );
237 if ( $sigPos ===
false ) {
238 $this->error(
'zip-wrong-format',
239 "zip file lacks EOCDR signature. It probably isn't a zip file." );
242 $this->eocdr = $this->unpack( substr( $block, $sigPos ), $info );
243 $this->eocdr[
'EOCDR size'] = $structSize + $this->eocdr[
'file comment length'];
245 if ( $structSize + $this->eocdr[
'file comment length'] != strlen( $block ) - $sigPos ) {
247 $this->error(
'zip-wrong-format',
'there is a ZIP signature but it is not at ' .
248 'the end of the file. It could be an OLE file with a ZIP file embedded.' );
250 if ( $this->eocdr[
'disk'] !== 0
251 || $this->eocdr[
'CD start disk'] !== 0
253 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR)' );
255 $this->eocdr += $this->unpack(
257 [
'file comment' => [
'string', $this->eocdr[
'file comment length'] ] ],
258 $sigPos + $structSize );
259 $this->eocdr[
'position'] = $startPos + $sigPos;
266 private function readZip64EndOfCentralDirectoryLocator() {
268 'signature' => [
'string', 4 ],
269 'eocdr64 start disk' => 4,
270 'eocdr64 offset' => 8,
271 'number of disks' => 4,
273 $structSize = $this->getStructSize( $info );
275 $start = $this->getFileLength() - $this->eocdr[
'EOCDR size'] - $structSize;
276 $block = $this->getBlock( $start, $structSize );
277 $this->eocdr64Locator =
$data = $this->unpack( $block, $info );
279 if (
$data[
'signature'] !==
"PK\x06\x07" ) {
283 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory locator' );
291 private function readZip64EndOfCentralDirectoryRecord() {
292 if ( $this->eocdr64Locator[
'eocdr64 start disk'] != 0
293 || $this->eocdr64Locator[
'number of disks'] != 0
295 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64 locator)' );
299 'signature' => [
'string', 4 ],
301 'version made by' => 2,
302 'version needed' => 2,
304 'CD start disk' => 4,
305 'CD entries this disk' => 8,
306 'CD entries total' => 8,
310 $structSize = $this->getStructSize( $info );
311 $block = $this->getBlock( $this->eocdr64Locator[
'eocdr64 offset'], $structSize );
312 $this->eocdr64 =
$data = $this->unpack( $block, $info );
313 if (
$data[
'signature'] !==
"PK\x06\x06" ) {
314 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory record' );
316 if (
$data[
'disk'] !== 0
317 ||
$data[
'CD start disk'] !== 0
319 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64)' );
329 private function findOldCentralDirectory() {
330 $size = $this->eocdr[
'CD size'];
331 $offset = $this->eocdr[
'CD offset'];
332 $endPos = $this->eocdr[
'position'];
336 if ( $offset + $size != $endPos ) {
337 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
338 'of central directory record' );
341 return [ $offset, $size ];
350 private function findZip64CentralDirectory() {
354 $size = $this->eocdr[
'CD size'];
355 $offset = $this->eocdr[
'CD offset'];
356 $numEntries = $this->eocdr[
'CD entries total'];
357 $endPos = $this->eocdr[
'position'];
358 if ( $size == 0xffffffff
359 || $offset == 0xffffffff
360 || $numEntries == 0xffff
362 $this->readZip64EndOfCentralDirectoryLocator();
364 if ( isset( $this->eocdr64Locator[
'eocdr64 offset'] ) ) {
365 $this->readZip64EndOfCentralDirectoryRecord();
366 if ( isset( $this->eocdr64[
'CD offset'] ) ) {
367 $size = $this->eocdr64[
'CD size'];
368 $offset = $this->eocdr64[
'CD offset'];
369 $endPos = $this->eocdr64Locator[
'eocdr64 offset'];
375 if ( $offset + $size != $endPos ) {
376 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
377 'of central directory record' );
380 return [ $offset, $size ];
388 private function readCentralDirectory( $offset, $size ) {
389 $block = $this->getBlock( $offset, $size );
392 'signature' => [
'string', 4 ],
393 'version made by' => 2,
394 'version needed' => 2,
396 'compression method' => 2,
400 'compressed size' => 4,
401 'uncompressed size' => 4,
403 'extra field length' => 2,
404 'comment length' => 2,
405 'disk number start' => 2,
406 'internal attrs' => 2,
407 'external attrs' => 4,
408 'local header offset' => 4,
410 $fixedSize = $this->getStructSize( $fixedInfo );
413 while ( $pos < $size ) {
414 $data = $this->unpack( $block, $fixedInfo, $pos );
417 if (
$data[
'signature'] !==
"PK\x01\x02" ) {
418 $this->error(
'zip-bad',
'Invalid signature found in directory entry' );
422 'name' => [
'string',
$data[
'name length'] ],
423 'extra field' => [
'string',
$data[
'extra field length'] ],
424 'comment' => [
'string',
$data[
'comment length'] ],
426 $data += $this->unpack( $block, $variableInfo, $pos );
427 $pos += $this->getStructSize( $variableInfo );
429 if ( $this->zip64 && (
430 $data[
'compressed size'] == 0xffffffff
431 ||
$data[
'uncompressed size'] == 0xffffffff
432 ||
$data[
'local header offset'] == 0xffffffff )
434 $zip64Data = $this->unpackZip64Extra(
$data[
'extra field'] );
440 if ( $this->testBit(
$data[
'general bits'], self::GENERAL_CD_ENCRYPTED ) ) {
441 $this->error(
'zip-unsupported',
'central directory encryption is not supported' );
447 $time =
$data[
'mod time'];
448 $date =
$data[
'mod date'];
450 $year = 1980 + ( $date >> 9 );
451 $month = ( $date >> 5 ) & 15;
453 $hour = ( $time >> 11 ) & 31;
454 $minute = ( $time >> 5 ) & 63;
455 $second = ( $time & 31 ) * 2;
456 $timestamp = sprintf(
"%04d%02d%02d%02d%02d%02d",
457 $year, $month, $day, $hour, $minute, $second );
460 if ( $this->testBit(
$data[
'general bits'], self::GENERAL_UTF8 ) ) {
461 $name =
$data[
'name'];
463 $name = iconv(
'CP437',
'UTF-8',
$data[
'name'] );
469 'mtime' => $timestamp,
470 'size' =>
$data[
'uncompressed size'],
472 call_user_func( $this->callback, $userData );
481 private function unpackZip64Extra( $extraField ) {
486 $extraHeaderSize = $this->getStructSize( $extraHeaderInfo );
489 'uncompressed size' => 8,
490 'compressed size' => 8,
491 'local header offset' => 8,
492 'disk number start' => 4,
496 while ( $extraPos < strlen( $extraField ) ) {
497 $extra = $this->unpack( $extraField, $extraHeaderInfo, $extraPos );
498 $extraPos += $extraHeaderSize;
499 $extra += $this->unpack( $extraField,
500 [
'data' => [
'string', $extra[
'size'] ] ],
502 $extraPos += $extra[
'size'];
504 if ( $extra[
'id'] == self::ZIP64_EXTRA_HEADER ) {
505 return $this->unpack( $extra[
'data'], $zip64ExtraInfo );
516 private function getFileLength() {
517 if ( $this->fileLength ===
null ) {
518 $stat = fstat( $this->file );
519 $this->fileLength = $stat[
'size'];
535 private function getBlock( $start, $length =
null ) {
538 $this->error(
'zip-bad',
"getBlock() requested position $start, " .
539 "file length is $fileLength" );
541 if ( $length ===
null ) {
544 $end = $start + $length;
546 $this->error(
'zip-bad',
"getBlock() requested end position $end, " .
547 "file length is $fileLength" );
549 $startSeg = (int)floor( $start / self::SEGSIZE );
550 $endSeg = (int)ceil( $end / self::SEGSIZE );
553 for ( $segIndex = $startSeg; $segIndex <= $endSeg; $segIndex++ ) {
554 $block .= $this->getSegment( $segIndex );
557 $block = substr( $block,
558 $start - $startSeg * self::SEGSIZE,
561 if ( strlen( $block ) < $length ) {
562 $this->error(
'zip-bad',
'getBlock() returned an unexpectedly small amount of data' );
581 private function getSegment( $segIndex ) {
582 if ( !isset( $this->buffer[$segIndex] ) ) {
583 $bytePos = $segIndex * self::SEGSIZE;
584 if ( $bytePos >= $this->getFileLength() ) {
585 $this->buffer[$segIndex] =
'';
589 if ( fseek( $this->file, $bytePos ) ) {
590 $this->error(
'zip-bad',
"seek to $bytePos failed" );
592 $seg = fread( $this->file, self::SEGSIZE );
593 if ( $seg ===
false ) {
594 $this->error(
'zip-bad',
"read from $bytePos failed" );
596 $this->buffer[$segIndex] = $seg;
599 return $this->buffer[$segIndex];
607 private function getStructSize( $struct ) {
609 foreach ( $struct as $type ) {
610 if ( is_array( $type ) ) {
611 [ , $fieldSize ] = $type;
641 private function unpack( $string, $struct, $offset = 0 ) {
642 $size = $this->getStructSize( $struct );
643 if ( $offset + $size > strlen( $string ) ) {
644 $this->error(
'zip-bad',
'unpack() would run past the end of the supplied string' );
649 foreach ( $struct as $key => $type ) {
650 if ( is_array( $type ) ) {
651 [ $typeName, $fieldSize ] = $type;
652 switch ( $typeName ) {
654 $data[$key] = substr( $string, $pos, $fieldSize );
658 throw new UnexpectedValueException( __METHOD__ .
": invalid type \"$typeName\"" );
662 $length = intval( $type );
667 for ( $i = $length - 1; $i >= 0; $i-- ) {
669 $value += ord( $string[$pos + $i] );
673 if ( $value > 2 ** 52 ) {
674 $this->error(
'zip-unsupported',
'number too large to be stored in a double. ' .
675 'This could happen if we tried to unpack a 64-bit structure ' .
676 'at an invalid location.' );
678 $data[$key] = $value;
694 private function testBit( $value, $bitIndex ) {
695 return (
bool)( ( $value >> $bitIndex ) & 1 );