MediaWiki  1.23.2
ZipDirectoryReader Class Reference

A class for reading ZIP file directories, for the purposes of upload verification. More...

Public Member Functions

 error ( $code, $debugMessage)
 Throw an error, and log a debug message. More...
 
 execute ()
 Read the directory according to settings in $this. More...
 
 findOldCentralDirectory ()
 Find the location of the central directory, as would be seen by a non-ZIP64 reader. More...
 
 findZip64CentralDirectory ()
 Find the location of the central directory, as would be seen by a ZIP64-compliant reader. More...
 
 getBlock ( $start, $length=null)
 Get the file contents from a given offset. More...
 
 getFileLength ()
 Get the length of the file. More...
 
 getSegment ( $segIndex)
 Get a section of the file starting at position $segIndex * self::SEGSIZE, of length self::SEGSIZE. More...
 
 getStructSize ( $struct)
 Get the size of a structure in bytes. More...
 
 hexDump ( $s)
 Debugging helper function which dumps a string in hexdump -C format. More...
 
 readCentralDirectory ( $offset, $size)
 Read the central directory at the given location. More...
 
 readEndOfCentralDirectoryRecord ()
 Read the header which is at the end of the central directory, unimaginatively called the "end of central directory record" by the ZIP spec. More...
 
 readZip64EndOfCentralDirectoryLocator ()
 Read the header called the "ZIP64 end of central directory locator". More...
 
 readZip64EndOfCentralDirectoryRecord ()
 Read the header called the "ZIP64 end of central directory record". More...
 
 testBit ( $value, $bitIndex)
 Returns a bit from a given position in an integer value, converted to boolean. More...
 
 unpack ( $string, $struct, $offset=0)
 Unpack a binary structure. More...
 
 unpackZip64Extra ( $extraField)
 Interpret ZIP64 "extra field" data and return an associative array. More...
 

Static Public Member Functions

static read ( $fileName, $callback, $options=array())
 Read a ZIP file and call a function for each file discovered in it. More...
 

Public Attributes

 $eocdr64
 
 $eocdr64Locator
 
const GENERAL_CD_ENCRYPTED = 13
 The index of the "general field" bit for central directory encryption. More...
 
const GENERAL_UTF8 = 11
 The index of the "general field" bit for UTF-8 file names. More...
 
const SEGSIZE = 16384
 The segment size for the file contents cache. More...
 
const ZIP64_EXTRA_HEADER = 0x0001
 The "extra field" ID for ZIP64 central directory entries. More...
 

Protected Member Functions

 __construct ( $fileName, $callback, $options)
 Private constructor. More...
 

Protected Attributes

 $buffer
 A segmented cache of the file contents. More...
 
 $callback
 The file data callback. More...
 
 $data
 
 $eocdr
 Stored headers. More...
 
 $file
 The opened file resource. More...
 
 $fileLength
 The cached length of the file, or null if it has not been loaded yet. More...
 
 $fileName
 The file name. More...
 
 $zip64 = false
 The ZIP64 mode. More...
 

Detailed Description

A class for reading ZIP file directories, for the purposes of upload verification.

Only a functional interface is provided: ZipFileReader::read(). No access is given to object instances.

Definition at line 31 of file ZipDirectoryReader.php.

Constructor & Destructor Documentation

◆ __construct()

ZipDirectoryReader::__construct (   $fileName,
  $callback,
  $options 
)
protected

Private constructor.

Definition at line 133 of file ZipDirectoryReader.php.

References $callback, $fileName, and $options.

Member Function Documentation

◆ error()

ZipDirectoryReader::error (   $code,
  $debugMessage 
)

◆ execute()

ZipDirectoryReader::execute ( )

◆ findOldCentralDirectory()

ZipDirectoryReader::findOldCentralDirectory ( )

Find the location of the central directory, as would be seen by a non-ZIP64 reader.

Returns
List containing offset, size and end position.

Definition at line 304 of file ZipDirectoryReader.php.

References $size, array(), and error().

Referenced by execute().

◆ findZip64CentralDirectory()

ZipDirectoryReader::findZip64CentralDirectory ( )

Find the location of the central directory, as would be seen by a ZIP64-compliant reader.

Returns
array List containing offset, size and end position.

Definition at line 325 of file ZipDirectoryReader.php.

References $size, array(), error(), readZip64EndOfCentralDirectoryLocator(), and readZip64EndOfCentralDirectoryRecord().

Referenced by execute().

◆ getBlock()

ZipDirectoryReader::getBlock (   $start,
  $length = null 
)

Get the file contents from a given offset.

If there are not enough bytes in the file to satisfy the request, an exception will be thrown.

Parameters
int$startThe byte offset of the start of the block.
int$lengthThe number of bytes to return. If omitted, the remainder of the file will be returned.
Returns
string

Definition at line 508 of file ZipDirectoryReader.php.

References $fileLength, error(), getFileLength(), and getSegment().

Referenced by readCentralDirectory(), readEndOfCentralDirectoryRecord(), readZip64EndOfCentralDirectoryLocator(), and readZip64EndOfCentralDirectoryRecord().

◆ getFileLength()

ZipDirectoryReader::getFileLength ( )

Get the length of the file.

Definition at line 489 of file ZipDirectoryReader.php.

References $fileLength, and file.

Referenced by getBlock(), getSegment(), readEndOfCentralDirectoryRecord(), and readZip64EndOfCentralDirectoryLocator().

◆ getSegment()

ZipDirectoryReader::getSegment (   $segIndex)

Get a section of the file starting at position $segIndex * self::SEGSIZE, of length self::SEGSIZE.

The result is cached. This is a helper function for getBlock().

If there are not enough bytes in the file to satisfy the request, the return value will be truncated. If a request is made for a segment beyond the end of the file, an empty string will be returned.

Returns
string

Definition at line 551 of file ZipDirectoryReader.php.

References error(), file, getFileLength(), and SEGSIZE.

Referenced by getBlock().

◆ getStructSize()

ZipDirectoryReader::getStructSize (   $struct)

Get the size of a structure in bytes.

See unpack() for the format of $struct.

Returns
int

Definition at line 576 of file ZipDirectoryReader.php.

References $size, $type, as, and list.

Referenced by readCentralDirectory(), readEndOfCentralDirectoryRecord(), readZip64EndOfCentralDirectoryLocator(), readZip64EndOfCentralDirectoryRecord(), unpack(), and unpackZip64Extra().

◆ hexDump()

ZipDirectoryReader::hexDump (   $s)

Debugging helper function which dumps a string in hexdump -C format.

Definition at line 672 of file ZipDirectoryReader.php.

References $n, and $s.

◆ read()

static ZipDirectoryReader::read (   $fileName,
  $callback,
  $options = array() 
)
static

Read a ZIP file and call a function for each file discovered in it.

Because this class is aimed at verification, an error is raised on suspicious or ambiguous input, instead of emulating some standard behavior.

Parameters
string$fileNameThe archive file name
array$callbackThe callback function. It will be called for each file with a single associative array each time, with members:
  • name: The file name. Directories conventionally have a trailing slash.
  • mtime: The file modification time, in MediaWiki 14-char format
  • size: The uncompressed file size
Parameters
array$optionsAn associative array of read options, with the option name in the key. This may currently contain:
  • zip64: If this is set to true, then we will emulate a library with ZIP64 support, like OpenJDK 7. If it is set to false, then we will emulate a library with no knowledge of ZIP64.

    NOTE: The ZIP64 code is untested and probably doesn't work. It turned out to be easier to just reject ZIP64 archive uploads, since they are likely to be very rare. Confirming safety of a ZIP64 file is fairly complex. What do you do with a file that is ambiguous and broken when read with a non-ZIP64 reader, but valid when read with a ZIP64 reader? This situation is normal for a valid ZIP64 file, and working out what non-ZIP64 readers will make of such a file is not trivial.

Returns
Status object. The following fatal errors are defined:
 - zip-file-open-error: The file could not be opened.

 - zip-wrong-format: The file does not appear to be a ZIP file.

 - zip-bad: There was something wrong or ambiguous about the file
   data.

 - zip-unsupported: The ZIP file uses features which
   ZipDirectoryReader does not support.

The default messages for those fatal errors are written in a way that makes sense for upload verification.

If a fatal error is returned, more information about the error will be available in the debug log.

Note that the callback function may be called any number of times before a fatal error is returned. If this occurs, the data sent to the callback function should be discarded.

Definition at line 89 of file ZipDirectoryReader.php.

References $callback, $fileName, and $options.

Referenced by ZipDirectoryReaderTest\readZipAssertError(), ZipDirectoryReaderTest\readZipAssertSuccess(), and UploadBase\verifyPartialFile().

◆ readCentralDirectory()

ZipDirectoryReader::readCentralDirectory (   $offset,
  $size 
)

Read the central directory at the given location.

Definition at line 361 of file ZipDirectoryReader.php.

References $data, $name, $size, $time, $timestamp, array(), error(), getBlock(), getStructSize(), testBit(), unpack(), and unpackZip64Extra().

Referenced by execute().

◆ readEndOfCentralDirectoryRecord()

ZipDirectoryReader::readEndOfCentralDirectoryRecord ( )

Read the header which is at the end of the central directory, unimaginatively called the "end of central directory record" by the ZIP spec.

Definition at line 195 of file ZipDirectoryReader.php.

References array(), error(), getBlock(), getFileLength(), getStructSize(), and unpack().

Referenced by execute().

◆ readZip64EndOfCentralDirectoryLocator()

ZipDirectoryReader::readZip64EndOfCentralDirectoryLocator ( )

Read the header called the "ZIP64 end of central directory locator".

An error will be raised if it does not exist.

Definition at line 241 of file ZipDirectoryReader.php.

References $data, array(), error(), getBlock(), getFileLength(), getStructSize(), and unpack().

Referenced by findZip64CentralDirectory().

◆ readZip64EndOfCentralDirectoryRecord()

ZipDirectoryReader::readZip64EndOfCentralDirectoryRecord ( )

Read the header called the "ZIP64 end of central directory record".

It may replace the regular "end of central directory record" in ZIP64 files.

Definition at line 266 of file ZipDirectoryReader.php.

References $data, array(), error(), getBlock(), getStructSize(), and unpack().

Referenced by findZip64CentralDirectory().

◆ testBit()

ZipDirectoryReader::testBit (   $value,
  $bitIndex 
)

Returns a bit from a given position in an integer value, converted to boolean.

Parameters
$valueinteger
int$bitIndexThe index of the bit, where 0 is the LSB.
Returns
bool

Definition at line 665 of file ZipDirectoryReader.php.

References $value.

Referenced by readCentralDirectory().

◆ unpack()

ZipDirectoryReader::unpack (   $string,
  $struct,
  $offset = 0 
)

Unpack a binary structure.

This is like the built-in unpack() function except nicer.

Parameters
string$stringThe binary data input
array$structAn associative array giving structure members and their types. In the key is the field name. The value may be either an integer, in which case the field is a little-endian unsigned integer encoded in the given number of bytes, or an array, in which case the first element of the array is the type name, and the subsequent elements are type-dependent parameters. Only one such type is defined:
  • "string": The second array element gives the length of string. Not null terminated.
int$offsetThe offset into the string at which to start unpacking.
Exceptions
MWException
Returns
array Unpacked associative array. Note that large integers in the input may be represented as floating point numbers in the return value, so the use of weak comparison is advised.

Definition at line 612 of file ZipDirectoryReader.php.

References $data, $size, $type, $value, array(), as, error(), getStructSize(), and list.

Referenced by readCentralDirectory(), readEndOfCentralDirectoryRecord(), readZip64EndOfCentralDirectoryLocator(), readZip64EndOfCentralDirectoryRecord(), and unpackZip64Extra().

◆ unpackZip64Extra()

ZipDirectoryReader::unpackZip64Extra (   $extraField)

Interpret ZIP64 "extra field" data and return an associative array.

Returns
array|bool

Definition at line 455 of file ZipDirectoryReader.php.

References array(), getStructSize(), and unpack().

Referenced by readCentralDirectory().

Member Data Documentation

◆ $buffer

ZipDirectoryReader::$buffer
protected

A segmented cache of the file contents.

Definition at line 105 of file ZipDirectoryReader.php.

◆ $callback

ZipDirectoryReader::$callback
protected

The file data callback.

Definition at line 108 of file ZipDirectoryReader.php.

Referenced by __construct(), and read().

◆ $data

ZipDirectoryReader::$data
protected

◆ $eocdr

ZipDirectoryReader::$eocdr
protected

Stored headers.

Definition at line 114 of file ZipDirectoryReader.php.

◆ $eocdr64

ZipDirectoryReader::$eocdr64

Definition at line 114 of file ZipDirectoryReader.php.

◆ $eocdr64Locator

ZipDirectoryReader::$eocdr64Locator

Definition at line 114 of file ZipDirectoryReader.php.

◆ $file

ZipDirectoryReader::$file
protected

The opened file resource.

Definition at line 99 of file ZipDirectoryReader.php.

◆ $fileLength

ZipDirectoryReader::$fileLength
protected

The cached length of the file, or null if it has not been loaded yet.

Definition at line 102 of file ZipDirectoryReader.php.

Referenced by getBlock(), and getFileLength().

◆ $fileName

ZipDirectoryReader::$fileName
protected

The file name.

Definition at line 96 of file ZipDirectoryReader.php.

Referenced by __construct(), and read().

◆ $zip64

ZipDirectoryReader::$zip64 = false
protected

The ZIP64 mode.

Definition at line 111 of file ZipDirectoryReader.php.

◆ GENERAL_CD_ENCRYPTED

const ZipDirectoryReader::GENERAL_CD_ENCRYPTED = 13

The index of the "general field" bit for central directory encryption.

Definition at line 128 of file ZipDirectoryReader.php.

◆ GENERAL_UTF8

const ZipDirectoryReader::GENERAL_UTF8 = 11

The index of the "general field" bit for UTF-8 file names.

Definition at line 125 of file ZipDirectoryReader.php.

◆ SEGSIZE

const ZipDirectoryReader::SEGSIZE = 16384

The segment size for the file contents cache.

Definition at line 122 of file ZipDirectoryReader.php.

Referenced by getSegment().

◆ ZIP64_EXTRA_HEADER

const ZipDirectoryReader::ZIP64_EXTRA_HEADER = 0x0001

The "extra field" ID for ZIP64 central directory entries.

Definition at line 119 of file ZipDirectoryReader.php.


The documentation for this class was generated from the following file: