91 $file = fopen( $fileName,
'r' );
93 return $zdr->execute();
109 return $zdr->execute();
135 private const ZIP64_EXTRA_HEADER = 0x0001;
138 private const SEGSIZE = 16384;
141 private const GENERAL_UTF8 = 11;
144 private const GENERAL_CD_ENCRYPTED = 13;
155 if ( isset( $options[
'zip64'] ) ) {
156 $this->zip64 = $options[
'zip64'];
165 private function execute() {
167 if ( !$this->file ) {
168 return Status::newFatal(
'zip-file-open-error' );
171 $status = Status::newGood();
173 $this->readEndOfCentralDirectoryRecord();
174 if ( $this->zip64 ) {
175 [ $offset, $size ] = $this->findZip64CentralDirectory();
176 $this->readCentralDirectory( $offset, $size );
178 if ( $this->eocdr[
'CD size'] == 0xffffffff
179 || $this->eocdr[
'CD offset'] == 0xffffffff
180 || $this->eocdr[
'CD entries total'] == 0xffff
182 $this->error(
'zip-unsupported',
'Central directory header indicates ZIP64, ' .
183 'but we are in legacy mode. Rejecting this upload is necessary to avoid ' .
184 'opening vulnerabilities on clients using OpenJDK 7 or later.' );
187 [ $offset, $size ] = $this->findOldCentralDirectory();
188 $this->readCentralDirectory( $offset, $size );
194 fclose( $this->file );
206 private function error( $code, $debugMessage ) {
207 wfDebug( __CLASS__ .
": Fatal error: $debugMessage" );
216 private function readEndOfCentralDirectoryRecord() {
220 'CD start disk' => 2,
221 'CD entries this disk' => 2,
222 'CD entries total' => 2,
225 'file comment length' => 2,
227 $structSize = $this->getStructSize( $info );
228 $startPos = $this->getFileLength() - 65536 - $structSize;
229 if ( $startPos < 0 ) {
233 if ( $this->getFileLength() === 0 ) {
234 $this->error(
'zip-wrong-format',
"The file is empty." );
237 $block = $this->getBlock( $startPos );
238 $sigPos = strrpos( $block,
"PK\x05\x06" );
239 if ( $sigPos ===
false ) {
240 $this->error(
'zip-wrong-format',
241 "zip file lacks EOCDR signature. It probably isn't a zip file." );
244 $this->eocdr = $this->unpack( substr( $block, $sigPos ), $info );
245 $this->eocdr[
'EOCDR size'] = $structSize + $this->eocdr[
'file comment length'];
247 if ( $structSize + $this->eocdr[
'file comment length'] != strlen( $block ) - $sigPos ) {
249 $this->error(
'zip-wrong-format',
'there is a ZIP signature but it is not at ' .
250 'the end of the file. It could be an OLE file with a ZIP file embedded.' );
252 if ( $this->eocdr[
'disk'] !== 0
253 || $this->eocdr[
'CD start disk'] !== 0
255 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR)' );
257 $this->eocdr += $this->unpack(
259 [
'file comment' => [
'string', $this->eocdr[
'file comment length'] ] ],
260 $sigPos + $structSize );
261 $this->eocdr[
'position'] = $startPos + $sigPos;
268 private function readZip64EndOfCentralDirectoryLocator() {
270 'signature' => [
'string', 4 ],
271 'eocdr64 start disk' => 4,
272 'eocdr64 offset' => 8,
273 'number of disks' => 4,
275 $structSize = $this->getStructSize( $info );
277 $start = $this->getFileLength() - $this->eocdr[
'EOCDR size'] - $structSize;
278 $block = $this->getBlock( $start, $structSize );
279 $this->eocdr64Locator =
$data = $this->unpack( $block, $info );
281 if (
$data[
'signature'] !==
"PK\x06\x07" ) {
285 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory locator' );
293 private function readZip64EndOfCentralDirectoryRecord() {
294 if ( $this->eocdr64Locator[
'eocdr64 start disk'] != 0
295 || $this->eocdr64Locator[
'number of disks'] != 0
297 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64 locator)' );
301 'signature' => [
'string', 4 ],
303 'version made by' => 2,
304 'version needed' => 2,
306 'CD start disk' => 4,
307 'CD entries this disk' => 8,
308 'CD entries total' => 8,
312 $structSize = $this->getStructSize( $info );
313 $block = $this->getBlock( $this->eocdr64Locator[
'eocdr64 offset'], $structSize );
314 $this->eocdr64 =
$data = $this->unpack( $block, $info );
315 if (
$data[
'signature'] !==
"PK\x06\x06" ) {
316 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory record' );
318 if (
$data[
'disk'] !== 0
319 ||
$data[
'CD start disk'] !== 0
321 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64)' );
331 private function findOldCentralDirectory() {
332 $size = $this->eocdr[
'CD size'];
333 $offset = $this->eocdr[
'CD offset'];
334 $endPos = $this->eocdr[
'position'];
338 if ( $offset + $size != $endPos ) {
339 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
340 'of central directory record' );
343 return [ $offset, $size ];
352 private function findZip64CentralDirectory() {
356 $size = $this->eocdr[
'CD size'];
357 $offset = $this->eocdr[
'CD offset'];
358 $numEntries = $this->eocdr[
'CD entries total'];
359 $endPos = $this->eocdr[
'position'];
360 if ( $size == 0xffffffff
361 || $offset == 0xffffffff
362 || $numEntries == 0xffff
364 $this->readZip64EndOfCentralDirectoryLocator();
366 if ( isset( $this->eocdr64Locator[
'eocdr64 offset'] ) ) {
367 $this->readZip64EndOfCentralDirectoryRecord();
368 if ( isset( $this->eocdr64[
'CD offset'] ) ) {
369 $size = $this->eocdr64[
'CD size'];
370 $offset = $this->eocdr64[
'CD offset'];
371 $endPos = $this->eocdr64Locator[
'eocdr64 offset'];
377 if ( $offset + $size != $endPos ) {
378 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
379 'of central directory record' );
382 return [ $offset, $size ];
390 private function readCentralDirectory( $offset, $size ) {
391 $block = $this->getBlock( $offset, $size );
394 'signature' => [
'string', 4 ],
395 'version made by' => 2,
396 'version needed' => 2,
398 'compression method' => 2,
402 'compressed size' => 4,
403 'uncompressed size' => 4,
405 'extra field length' => 2,
406 'comment length' => 2,
407 'disk number start' => 2,
408 'internal attrs' => 2,
409 'external attrs' => 4,
410 'local header offset' => 4,
412 $fixedSize = $this->getStructSize( $fixedInfo );
415 while ( $pos < $size ) {
416 $data = $this->unpack( $block, $fixedInfo, $pos );
419 if (
$data[
'signature'] !==
"PK\x01\x02" ) {
420 $this->error(
'zip-bad',
'Invalid signature found in directory entry' );
424 'name' => [
'string',
$data[
'name length'] ],
425 'extra field' => [
'string',
$data[
'extra field length'] ],
426 'comment' => [
'string',
$data[
'comment length'] ],
428 $data += $this->unpack( $block, $variableInfo, $pos );
429 $pos += $this->getStructSize( $variableInfo );
431 if ( $this->zip64 && (
432 $data[
'compressed size'] == 0xffffffff
433 ||
$data[
'uncompressed size'] == 0xffffffff
434 ||
$data[
'local header offset'] == 0xffffffff )
436 $zip64Data = $this->unpackZip64Extra(
$data[
'extra field'] );
442 if ( $this->testBit(
$data[
'general bits'], self::GENERAL_CD_ENCRYPTED ) ) {
443 $this->error(
'zip-unsupported',
'central directory encryption is not supported' );
449 $time =
$data[
'mod time'];
450 $date =
$data[
'mod date'];
452 $year = 1980 + ( $date >> 9 );
453 $month = ( $date >> 5 ) & 15;
455 $hour = ( $time >> 11 ) & 31;
456 $minute = ( $time >> 5 ) & 63;
457 $second = ( $time & 31 ) * 2;
458 $timestamp = sprintf(
"%04d%02d%02d%02d%02d%02d",
459 $year, $month, $day, $hour, $minute, $second );
462 if ( $this->testBit(
$data[
'general bits'], self::GENERAL_UTF8 ) ) {
463 $name =
$data[
'name'];
465 $name = iconv(
'CP437',
'UTF-8',
$data[
'name'] );
471 'mtime' => $timestamp,
472 'size' =>
$data[
'uncompressed size'],
474 call_user_func( $this->callback, $userData );
483 private function unpackZip64Extra( $extraField ) {
488 $extraHeaderSize = $this->getStructSize( $extraHeaderInfo );
491 'uncompressed size' => 8,
492 'compressed size' => 8,
493 'local header offset' => 8,
494 'disk number start' => 4,
498 while ( $extraPos < strlen( $extraField ) ) {
499 $extra = $this->unpack( $extraField, $extraHeaderInfo, $extraPos );
500 $extraPos += $extraHeaderSize;
501 $extra += $this->unpack( $extraField,
502 [
'data' => [
'string', $extra[
'size'] ] ],
504 $extraPos += $extra[
'size'];
506 if ( $extra[
'id'] == self::ZIP64_EXTRA_HEADER ) {
507 return $this->unpack( $extra[
'data'], $zip64ExtraInfo );
518 private function getFileLength() {
519 if ( $this->fileLength ===
null ) {
520 $stat = fstat( $this->file );
521 $this->fileLength = $stat[
'size'];
537 private function getBlock( $start, $length =
null ) {
540 $this->error(
'zip-bad',
"getBlock() requested position $start, " .
541 "file length is $fileLength" );
543 if ( $length ===
null ) {
546 $end = $start + $length;
548 $this->error(
'zip-bad',
"getBlock() requested end position $end, " .
549 "file length is $fileLength" );
551 $startSeg = (int)floor( $start / self::SEGSIZE );
552 $endSeg = (int)ceil( $end / self::SEGSIZE );
555 for ( $segIndex = $startSeg; $segIndex <= $endSeg; $segIndex++ ) {
556 $block .= $this->getSegment( $segIndex );
559 $block = substr( $block,
560 $start - $startSeg * self::SEGSIZE,
563 if ( strlen( $block ) < $length ) {
564 $this->error(
'zip-bad',
'getBlock() returned an unexpectedly small amount of data' );
583 private function getSegment( $segIndex ) {
584 if ( !isset( $this->buffer[$segIndex] ) ) {
585 $bytePos = $segIndex * self::SEGSIZE;
586 if ( $bytePos >= $this->getFileLength() ) {
587 $this->buffer[$segIndex] =
'';
591 if ( fseek( $this->file, $bytePos ) ) {
592 $this->error(
'zip-bad',
"seek to $bytePos failed" );
594 $seg = fread( $this->file, self::SEGSIZE );
595 if ( $seg ===
false ) {
596 $this->error(
'zip-bad',
"read from $bytePos failed" );
598 $this->buffer[$segIndex] = $seg;
601 return $this->buffer[$segIndex];
609 private function getStructSize( $struct ) {
611 foreach ( $struct as $type ) {
612 if ( is_array( $type ) ) {
613 [ , $fieldSize ] = $type;
643 private function unpack( $string, $struct, $offset = 0 ) {
644 $size = $this->getStructSize( $struct );
645 if ( $offset + $size > strlen( $string ) ) {
646 $this->error(
'zip-bad',
'unpack() would run past the end of the supplied string' );
651 foreach ( $struct as $key => $type ) {
652 if ( is_array( $type ) ) {
653 [ $typeName, $fieldSize ] = $type;
654 switch ( $typeName ) {
656 $data[$key] = substr( $string, $pos, $fieldSize );
660 throw new UnexpectedValueException( __METHOD__ .
": invalid type \"$typeName\"" );
664 $length = intval( $type );
669 for ( $i = $length - 1; $i >= 0; $i-- ) {
671 $value += ord( $string[$pos + $i] );
675 if ( $value > 2 ** 52 ) {
676 $this->error(
'zip-unsupported',
'number too large to be stored in a double. ' .
677 'This could happen if we tried to unpack a 64-bit structure ' .
678 'at an invalid location.' );
680 $data[$key] = $value;
696 private function testBit( $value, $bitIndex ) {
697 return (
bool)( ( $value >> $bitIndex ) & 1 );