45 private $mimeFromClsid;
51 private $valid =
false;
54 private $sectorLength;
60 private const TYPE_UNALLOCATED = 0;
61 private const TYPE_STORAGE = 1;
62 private const TYPE_STREAM = 2;
63 private const TYPE_ROOT = 5;
72 private const MIMES_BY_CLSID = [
74 '00020810-0000-0000-C000-000000000046' =>
'application/vnd.ms-excel',
75 '00020820-0000-0000-C000-000000000046' =>
'application/vnd.ms-excel',
76 '00020906-0000-0000-C000-000000000046' =>
'application/msword',
77 '64818D10-4F9B-11CF-86EA-00AA00B929E8' =>
'application/vnd.ms-powerpoint',
93 $handle = fopen( $fileName,
'r' );
94 if ( $handle ===
false ) {
97 'error' =>
'file does not exist',
117 $reader =
new self( $fileHandle );
119 'valid' => $reader->valid,
120 'mime' => $reader->mime,
121 'mimeFromClsid' => $reader->mimeFromClsid
123 if ( $reader->error ) {
124 $info[
'error'] = $reader->error;
125 $info[
'errorCode'] = $reader->errorCode;
130 private function __construct( $fileHandle ) {
131 $this->file = $fileHandle;
134 }
catch ( RuntimeException $e ) {
135 $this->valid =
false;
136 $this->error = $e->getMessage();
137 $this->errorCode = $e->getCode();
141 private function init() {
142 $this->header = $this->unpackOffset( 0, [
143 'header_signature' => 8,
144 'header_clsid' => 16,
145 'minor_version' => 2,
146 'major_version' => 2,
149 'mini_sector_shift' => 2,
151 'num_dir_sectors' => 4,
152 'num_fat_sectors' => 4,
153 'first_dir_sector' => 4,
154 'transaction_signature_number' => 4,
155 'mini_stream_cutoff_size' => 4,
156 'first_mini_fat_sector' => 4,
157 'num_mini_fat_sectors' => 4,
158 'first_difat_sector' => 4,
159 'num_difat_sectors' => 4,
162 if ( $this->header[
'header_signature'] !==
"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" ) {
163 $this->error(
'invalid signature: ' . bin2hex( $this->header[
'header_signature'] ),
164 self::ERROR_INVALID_SIGNATURE );
166 $this->sectorLength = 1 << $this->header[
'sector_shift'];
168 $this->readDirectory();
173 private function sectorOffset( $sectorId ) {
174 return $this->sectorLength * ( $sectorId + 1 );
177 private function decodeClsid( $binaryClsid ) {
178 $parts = unpack(
'Va/vb/vc/C8d', $binaryClsid );
179 return sprintf(
"%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
199 private function unpackOffset( $offset, $struct ) {
200 $block = $this->readOffset( $offset, array_sum( $struct ) );
201 return $this->unpack( $block, 0, $struct );
210 private function unpack( $block, $offset, $struct ) {
212 foreach ( $struct as $key => $length ) {
214 $data[$key] = substr( $block, $offset, $length );
216 $data[$key] = $this->bin2dec( $block, $offset, $length );
223 private function bin2dec( $str, $offset, $length ) {
225 for ( $i = $length - 1; $i >= 0; $i-- ) {
227 $value += ord( $str[$offset + $i] );
232 private function readOffset( $offset, $length ) {
233 $this->fseek( $offset );
235 $block = @fread( $this->file, $length );
236 if ( $block ===
false ) {
237 $this->error(
'error reading from file', self::ERROR_READ );
239 if ( strlen( $block ) !== $length ) {
240 $this->error(
'unable to read the required number of bytes from the file',
241 self::ERROR_READ_PAST_END );
246 private function readSector( $sectorId ) {
247 return $this->readOffset( $this->sectorOffset( $sectorId ), 1 << $this->header[
'sector_shift'] );
255 private function error( $message, $code ) {
256 throw new RuntimeException( $message, $code );
259 private function fseek( $offset ) {
261 $result = @fseek( $this->file, $offset );
262 if ( $result !== 0 ) {
263 $this->error(
"unable to seek to offset $offset", self::ERROR_SEEK );
267 private function readDifat() {
268 $binaryDifat = $this->header[
'difat'];
269 $nextDifatSector = $this->header[
'first_difat_sector'];
270 for ( $i = 0; $i < $this->header[
'num_difat_sectors']; $i++ ) {
271 $block = $this->readSector( $nextDifatSector );
272 $binaryDifat .= substr( $block, 0, $this->sectorLength - 4 );
273 $nextDifatSector = $this->bin2dec( $block, $this->sectorLength - 4, 4 );
274 if ( $nextDifatSector == 0xFFFFFFFE ) {
280 for ( $pos = 0; $pos < strlen( $binaryDifat ); $pos += 4 ) {
281 $fatSector = $this->bin2dec( $binaryDifat, $pos, 4 );
282 if ( $fatSector < 0xFFFFFFFC ) {
283 $this->difat[] = $fatSector;
290 private function getNextSectorIdFromFat( $sectorId ) {
291 $entriesPerSector = intdiv( $this->sectorLength, 4 );
292 $fatSectorId = intdiv( $sectorId, $entriesPerSector );
293 $fatSectorArray = $this->getFatSector( $fatSectorId );
294 return $fatSectorArray[$sectorId % $entriesPerSector];
297 private function getFatSector( $fatSectorId ) {
298 if ( !isset( $this->fat[$fatSectorId] ) ) {
300 if ( !isset( $this->difat[$fatSectorId] ) ) {
301 $this->error(
'FAT sector requested beyond the end of the DIFAT', self::ERROR_INVALID_FORMAT );
303 $absoluteSectorId = $this->difat[$fatSectorId];
304 $block = $this->readSector( $absoluteSectorId );
305 for ( $pos = 0; $pos < strlen( $block ); $pos += 4 ) {
306 $fat[] = $this->bin2dec( $block, $pos, 4 );
308 $this->fat[$fatSectorId] = $fat;
310 return $this->fat[$fatSectorId];
313 private function readDirectory() {
314 $dirSectorId = $this->header[
'first_dir_sector'];
317 while ( $dirSectorId !== 0xFFFFFFFE ) {
318 if ( isset( $seenSectorIds[$dirSectorId] ) ) {
319 $this->error(
'FAT loop detected', self::ERROR_INVALID_FORMAT );
321 $seenSectorIds[$dirSectorId] =
true;
323 $binaryDir .= $this->readSector( $dirSectorId );
324 $dirSectorId = $this->getNextSectorIdFromFat( $dirSectorId );
337 'create_time_low' => 4,
338 'create_time_high' => 4,
339 'modify_time_low' => 4,
340 'modify_time_high' => 4,
345 $entryLength = array_sum( $struct );
347 for ( $pos = 0; $pos < strlen( $binaryDir ); $pos += $entryLength ) {
348 $entry = $this->unpack( $binaryDir, $pos, $struct );
352 $entry[
'size_high'] = 0;
354 $type = $entry[
'object_type'];
355 if ( $type == self::TYPE_UNALLOCATED ) {
359 $name = iconv(
'UTF-16LE',
'UTF-8', substr( $entry[
'name_raw'], 0, $entry[
'name_length'] - 2 ) );
361 $clsid = $this->decodeClsid( $entry[
'clsid'] );
362 if ( $type == self::TYPE_ROOT && isset( self::MIMES_BY_CLSID[$clsid] ) ) {
363 $this->mimeFromClsid = self::MIMES_BY_CLSID[$clsid];
366 if ( $name ===
'Workbook' ) {
367 $this->mime =
'application/vnd.ms-excel';
368 } elseif ( $name ===
'WordDocument' ) {
369 $this->mime =
'application/msword';
370 } elseif ( $name ===
'PowerPoint Document' ) {
371 $this->mime =
'application/vnd.ms-powerpoint';