37 private $mimeFromClsid;
40 private $valid =
false;
42 private $sectorLength;
46 private const TYPE_UNALLOCATED = 0;
47 private const TYPE_STORAGE = 1;
48 private const TYPE_STREAM = 2;
49 private const TYPE_ROOT = 5;
58 private static $mimesByClsid = [
60 '00020810-0000-0000-C000-000000000046' =>
'application/vnd.ms-excel',
61 '00020820-0000-0000-C000-000000000046' =>
'application/vnd.ms-excel',
62 '00020906-0000-0000-C000-000000000046' =>
'application/msword',
63 '64818D10-4F9B-11CF-86EA-00AA00B929E8' =>
'application/vnd.ms-powerpoint',
79 $handle = fopen( $fileName,
'r' );
80 if ( $handle ===
false ) {
83 'error' =>
'file does not exist',
103 $reader =
new self( $fileHandle );
105 'valid' => $reader->valid,
106 'mime' => $reader->mime,
107 'mimeFromClsid' => $reader->mimeFromClsid
109 if ( $reader->error ) {
110 $info[
'error'] = $reader->error;
111 $info[
'errorCode'] = $reader->errorCode;
116 private function __construct( $fileHandle ) {
117 $this->file = $fileHandle;
120 }
catch ( RuntimeException $e ) {
121 $this->valid =
false;
122 $this->error = $e->getMessage();
123 $this->errorCode = $e->getCode();
127 private function init() {
128 $this->header = $this->unpackOffset( 0, [
129 'header_signature' => 8,
130 'header_clsid' => 16,
131 'minor_version' => 2,
132 'major_version' => 2,
135 'mini_sector_shift' => 2,
137 'num_dir_sectors' => 4,
138 'num_fat_sectors' => 4,
139 'first_dir_sector' => 4,
140 'transaction_signature_number' => 4,
141 'mini_stream_cutoff_size' => 4,
142 'first_mini_fat_sector' => 4,
143 'num_mini_fat_sectors' => 4,
144 'first_difat_sector' => 4,
145 'num_difat_sectors' => 4,
148 if ( $this->header[
'header_signature'] !==
"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" ) {
149 $this->error(
'invalid signature: ' . bin2hex( $this->header[
'header_signature'] ),
150 self::ERROR_INVALID_SIGNATURE );
152 $this->sectorLength = 1 << $this->header[
'sector_shift'];
154 $this->readDirectory();
159 private function sectorOffset( $sectorId ) {
160 return $this->sectorLength * ( $sectorId + 1 );
163 private function decodeClsid( $binaryClsid ) {
164 $parts = unpack(
'Va/vb/vc/C8d', $binaryClsid );
165 return sprintf(
"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
185 private function unpackOffset( $offset, $struct ) {
186 $block = $this->readOffset( $offset, array_sum( $struct ) );
187 return $this->unpack( $block, 0, $struct );
196 private function unpack( $block, $offset, $struct ) {
198 foreach ( $struct as $key => $length ) {
200 $data[$key] = substr( $block, $offset, $length );
202 $data[$key] = $this->bin2dec( $block, $offset, $length );
209 private function bin2dec( $str, $offset, $length ) {
211 for ( $i = $length - 1; $i >= 0; $i-- ) {
213 $value += ord( $str[$offset + $i] );
218 private function readOffset( $offset, $length ) {
219 $this->fseek( $offset );
221 $block = @fread( $this->file, $length );
222 if ( $block ===
false ) {
223 $this->error(
'error reading from file', self::ERROR_READ );
225 if ( strlen( $block ) !== $length ) {
226 $this->error(
'unable to read the required number of bytes from the file',
227 self::ERROR_READ_PAST_END );
232 private function readSector( $sectorId ) {
233 return $this->readOffset( $this->sectorOffset( $sectorId ), 1 << $this->header[
'sector_shift'] );
241 private function error( $message, $code ) {
242 throw new RuntimeException( $message, $code );
245 private function fseek( $offset ) {
247 $result = @fseek( $this->file, $offset );
248 if ( $result !== 0 ) {
249 $this->error(
"unable to seek to offset $offset", self::ERROR_SEEK );
253 private function readDifat() {
254 $binaryDifat = $this->header[
'difat'];
255 $nextDifatSector = $this->header[
'first_difat_sector'];
256 for ( $i = 0; $i < $this->header[
'num_difat_sectors']; $i++ ) {
257 $block = $this->readSector( $nextDifatSector );
258 $binaryDifat .= substr( $block, 0, $this->sectorLength - 4 );
259 $nextDifatSector = $this->bin2dec( $block, $this->sectorLength - 4, 4 );
260 if ( $nextDifatSector == 0xFFFFFFFE ) {
266 for ( $pos = 0; $pos < strlen( $binaryDifat ); $pos += 4 ) {
267 $fatSector = $this->bin2dec( $binaryDifat, $pos, 4 );
268 if ( $fatSector < 0xFFFFFFFC ) {
269 $this->difat[] = $fatSector;
276 private function getNextSectorIdFromFat( $sectorId ) {
277 $entriesPerSector = intdiv( $this->sectorLength, 4 );
278 $fatSectorId = intdiv( $sectorId, $entriesPerSector );
279 $fatSectorArray = $this->getFatSector( $fatSectorId );
280 return $fatSectorArray[$sectorId % $entriesPerSector];
283 private function getFatSector( $fatSectorId ) {
284 if ( !isset( $this->fat[$fatSectorId] ) ) {
286 if ( !isset( $this->difat[$fatSectorId] ) ) {
287 $this->error(
'FAT sector requested beyond the end of the DIFAT', self::ERROR_INVALID_FORMAT );
289 $absoluteSectorId = $this->difat[$fatSectorId];
290 $block = $this->readSector( $absoluteSectorId );
291 for ( $pos = 0; $pos < strlen( $block ); $pos += 4 ) {
292 $fat[] = $this->bin2dec( $block, $pos, 4 );
294 $this->fat[$fatSectorId] = $fat;
296 return $this->fat[$fatSectorId];
299 private function readDirectory() {
300 $dirSectorId = $this->header[
'first_dir_sector'];
303 while ( $dirSectorId !== 0xFFFFFFFE ) {
304 if ( isset( $seenSectorIds[$dirSectorId] ) ) {
305 $this->error(
'FAT loop detected', self::ERROR_INVALID_FORMAT );
307 $seenSectorIds[$dirSectorId] =
true;
309 $binaryDir .= $this->readSector( $dirSectorId );
310 $dirSectorId = $this->getNextSectorIdFromFat( $dirSectorId );
323 'create_time_low' => 4,
324 'create_time_high' => 4,
325 'modify_time_low' => 4,
326 'modify_time_high' => 4,
331 $entryLength = array_sum( $struct );
333 for ( $pos = 0; $pos < strlen( $binaryDir ); $pos += $entryLength ) {
334 $entry = $this->unpack( $binaryDir, $pos, $struct );
338 $entry[
'size_high'] = 0;
340 $type = $entry[
'object_type'];
341 if ( $type == self::TYPE_UNALLOCATED ) {
345 $name = iconv(
'UTF-16LE',
'UTF-8', substr( $entry[
'name_raw'], 0, $entry[
'name_length'] - 2 ) );
347 $clsid = $this->decodeClsid( $entry[
'clsid'] );
348 if ( $type == self::TYPE_ROOT && isset( self::$mimesByClsid[$clsid] ) ) {
349 $this->mimeFromClsid = self::$mimesByClsid[$clsid];
352 if ( $name ===
'Workbook' ) {
353 $this->mime =
'application/vnd.ms-excel';
354 } elseif ( $name ===
'WordDocument' ) {
355 $this->mime =
'application/msword';
356 } elseif ( $name ===
'PowerPoint Document' ) {
357 $this->mime =
'application/vnd.ms-powerpoint';