MediaWiki
REL1_19
|
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. | |
execute () | |
Read the directory according to settings in $this. | |
findOldCentralDirectory () | |
Find the location of the central directory, as would be seen by a non-ZIP64 reader. | |
findZip64CentralDirectory () | |
Find the location of the central directory, as would be seen by a ZIP64-compliant reader. | |
getBlock ($start, $length=null) | |
Get the file contents from a given offset. | |
getFileLength () | |
Get the length of the file. | |
getSegment ($segIndex) | |
Get a section of the file starting at position $segIndex * self::SEGSIZE, of length self::SEGSIZE. | |
getStructSize ($struct) | |
Get the size of a structure in bytes. | |
hexDump ($s) | |
Debugging helper function which dumps a string in hexdump -C format. | |
readCentralDirectory ($offset, $size) | |
Read the central directory at the given location. | |
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. | |
readZip64EndOfCentralDirectoryLocator () | |
Read the header called the "ZIP64 end of central directory locator". | |
readZip64EndOfCentralDirectoryRecord () | |
Read the header called the "ZIP64 end of central directory record". | |
testBit ($value, $bitIndex) | |
Returns a bit from a given position in an integer value, converted to boolean. | |
unpack ($string, $struct, $offset=0) | |
Unpack a binary structure. | |
unpackZip64Extra ($extraField) | |
Interpret ZIP64 "extra field" data and return an associative array. | |
Static Public Member Functions | |
static | read ($fileName, $callback, $options=array()) |
Read a ZIP file and call a function for each file discovered in it. | |
Public Attributes | |
$buffer | |
A segmented cache of the file contents. | |
$callback | |
The file data callback. | |
$data | |
$eocdr | |
Stored headers. | |
$eocdr64 | |
$eocdr64Locator | |
$file | |
The opened file resource. | |
$fileLength | |
The cached length of the file, or null if it has not been loaded yet. | |
$fileName | |
The file name. | |
$zip64 = false | |
The ZIP64 mode. | |
const | GENERAL_CD_ENCRYPTED = 13 |
The index of the "general field" bit for central directory encryption. | |
const | GENERAL_UTF8 = 11 |
The index of the "general field" bit for UTF-8 file names. | |
const | SEGSIZE = 16384 |
The segment size for the file contents cache. | |
const | ZIP64_EXTRA_HEADER = 0x0001 |
The "extra field" ID for ZIP64 central directory entries. | |
Protected Member Functions | |
__construct ($fileName, $callback, $options) | |
Private constructor. |
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 11 of file ZipDirectoryReader.php.
ZipDirectoryReader::__construct | ( | $ | fileName, |
$ | callback, | ||
$ | options | ||
) | [protected] |
Private constructor.
Definition at line 112 of file ZipDirectoryReader.php.
ZipDirectoryReader::error | ( | $ | code, |
$ | debugMessage | ||
) |
Throw an error, and log a debug message.
Definition at line 163 of file ZipDirectoryReader.php.
References wfDebug().
Referenced by execute(), findOldCentralDirectory(), findZip64CentralDirectory(), getBlock(), getSegment(), readCentralDirectory(), readEndOfCentralDirectoryRecord(), readZip64EndOfCentralDirectoryLocator(), readZip64EndOfCentralDirectoryRecord(), and unpack().
Read the directory according to settings in $this.
Definition at line 126 of file ZipDirectoryReader.php.
References $size, error(), findOldCentralDirectory(), findZip64CentralDirectory(), ZipDirectoryReaderError\getErrorCode(), Status\newFatal(), Status\newGood(), readCentralDirectory(), and readEndOfCentralDirectoryRecord().
Find the location of the central directory, as would be seen by a non-ZIP64 reader.
Definition at line 282 of file ZipDirectoryReader.php.
References $size, and error().
Referenced by execute().
Find the location of the central directory, as would be seen by a ZIP64-compliant reader.
Definition at line 302 of file ZipDirectoryReader.php.
References $size, error(), readZip64EndOfCentralDirectoryLocator(), and readZip64EndOfCentralDirectoryRecord().
Referenced by execute().
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.
$start | The byte offset of the start of the block. |
$length | The number of bytes to return. If omitted, the remainder of the file will be returned. |
Definition at line 482 of file ZipDirectoryReader.php.
References $fileLength, error(), getFileLength(), and getSegment().
Referenced by readCentralDirectory(), readEndOfCentralDirectoryRecord(), readZip64EndOfCentralDirectoryLocator(), and readZip64EndOfCentralDirectoryRecord().
Get the length of the file.
Definition at line 464 of file ZipDirectoryReader.php.
Referenced by getBlock(), getSegment(), readEndOfCentralDirectoryRecord(), and readZip64EndOfCentralDirectoryLocator().
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 satsify 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.
Definition at line 524 of file ZipDirectoryReader.php.
References error(), getFileLength(), and SEGSIZE.
Referenced by getBlock().
ZipDirectoryReader::getStructSize | ( | $ | struct | ) |
Get the size of a structure in bytes.
See unpack() for the format of $struct.
Definition at line 546 of file ZipDirectoryReader.php.
References $size.
Referenced by readCentralDirectory(), readEndOfCentralDirectoryRecord(), readZip64EndOfCentralDirectoryLocator(), readZip64EndOfCentralDirectoryRecord(), unpack(), and unpackZip64Extra().
ZipDirectoryReader::hexDump | ( | $ | s | ) |
Debugging helper function which dumps a string in hexdump -C format.
Definition at line 640 of file ZipDirectoryReader.php.
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 behaviour.
$fileName | string The archive file name |
$callback | Array The callback function. It will be called for each file with a single associative array each time, with members: |
$options | Array An associative array of read options, with the option name in the key. This may currently contain: |
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.
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 69 of file ZipDirectoryReader.php.
References $callback, $fileName, and $options.
Referenced by ZipDirectoryReaderTest\readZipAssertError(), ZipDirectoryReaderTest\readZipAssertSuccess(), and UploadBase\verifyPartialFile().
ZipDirectoryReader::readCentralDirectory | ( | $ | offset, |
$ | size | ||
) |
Read the central directory at the given location.
Definition at line 337 of file ZipDirectoryReader.php.
References $data, $size, error(), getBlock(), getStructSize(), testBit(), unpack(), and unpackZip64Extra().
Referenced by execute().
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 173 of file ZipDirectoryReader.php.
References error(), getBlock(), getFileLength(), getStructSize(), and unpack().
Referenced by execute().
Read the header called the "ZIP64 end of central directory locator".
An error will be raised if it does not exist.
Definition at line 219 of file ZipDirectoryReader.php.
References $data, error(), getBlock(), getFileLength(), getStructSize(), and unpack().
Referenced by findZip64CentralDirectory().
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 244 of file ZipDirectoryReader.php.
References $data, error(), getBlock(), getStructSize(), and unpack().
Referenced by findZip64CentralDirectory().
ZipDirectoryReader::testBit | ( | $ | value, |
$ | bitIndex | ||
) |
Returns a bit from a given position in an integer value, converted to boolean.
$value | integer |
$bitIndex | The index of the bit, where 0 is the LSB. |
Definition at line 633 of file ZipDirectoryReader.php.
Referenced by readCentralDirectory().
ZipDirectoryReader::unpack | ( | $ | string, |
$ | struct, | ||
$ | offset = 0 |
||
) |
Unpack a binary structure.
This is like the built-in unpack() function except nicer.
$string | The binary data input |
$struct | An 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:
|
$offset | The offset into the string at which to start unpacking. |
Definition at line 580 of file ZipDirectoryReader.php.
References $data, $size, error(), and getStructSize().
Referenced by readCentralDirectory(), readEndOfCentralDirectoryRecord(), readZip64EndOfCentralDirectoryLocator(), readZip64EndOfCentralDirectoryRecord(), and unpackZip64Extra().
ZipDirectoryReader::unpackZip64Extra | ( | $ | extraField | ) |
Interpret ZIP64 "extra field" data and return an associative array.
Definition at line 430 of file ZipDirectoryReader.php.
References getStructSize(), and unpack().
Referenced by readCentralDirectory().
ZipDirectoryReader::$buffer |
A segmented cache of the file contents.
Definition at line 84 of file ZipDirectoryReader.php.
ZipDirectoryReader::$callback |
The file data callback.
Definition at line 87 of file ZipDirectoryReader.php.
Referenced by __construct(), and read().
ZipDirectoryReader::$data |
Definition at line 95 of file ZipDirectoryReader.php.
Referenced by readCentralDirectory(), readZip64EndOfCentralDirectoryLocator(), readZip64EndOfCentralDirectoryRecord(), and unpack().
ZipDirectoryReader::$eocdr |
Stored headers.
Definition at line 93 of file ZipDirectoryReader.php.
ZipDirectoryReader::$eocdr64 |
Definition at line 93 of file ZipDirectoryReader.php.
ZipDirectoryReader::$eocdr64Locator |
Definition at line 93 of file ZipDirectoryReader.php.
ZipDirectoryReader::$file |
The opened file resource.
Definition at line 78 of file ZipDirectoryReader.php.
ZipDirectoryReader::$fileLength |
The cached length of the file, or null if it has not been loaded yet.
Definition at line 81 of file ZipDirectoryReader.php.
Referenced by getBlock().
ZipDirectoryReader::$fileName |
The file name.
Definition at line 75 of file ZipDirectoryReader.php.
Referenced by __construct(), and read().
ZipDirectoryReader::$zip64 = false |
The ZIP64 mode.
Definition at line 90 of file ZipDirectoryReader.php.
const ZipDirectoryReader::GENERAL_CD_ENCRYPTED = 13 |
The index of the "general field" bit for central directory encryption.
Definition at line 107 of file ZipDirectoryReader.php.
const ZipDirectoryReader::GENERAL_UTF8 = 11 |
The index of the "general field" bit for UTF-8 file names.
Definition at line 104 of file ZipDirectoryReader.php.
const ZipDirectoryReader::SEGSIZE = 16384 |
The segment size for the file contents cache.
Definition at line 101 of file ZipDirectoryReader.php.
Referenced by getSegment().
const ZipDirectoryReader::ZIP64_EXTRA_HEADER = 0x0001 |
The "extra field" ID for ZIP64 central directory entries.
Definition at line 98 of file ZipDirectoryReader.php.