Completed
Push — master ( 33eefe...de01e5 )
by Mark
31s queued 27s
created

OLE::getStream()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0879

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 29
ccs 14
cts 17
cp 0.8235
rs 9.7
cc 4
nc 8
nop 1
crap 4.0879
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
// vim: set expandtab tabstop=4 shiftwidth=4:
6
// +----------------------------------------------------------------------+
7
// | PHP Version 4                                                        |
8
// +----------------------------------------------------------------------+
9
// | Copyright (c) 1997-2002 The PHP Group                                |
10
// +----------------------------------------------------------------------+
11
// | This source file is subject to version 2.02 of the PHP license,      |
12
// | that is bundled with this package in the file LICENSE, and is        |
13
// | available at through the world-wide-web at                           |
14
// | http://www.php.net/license/2_02.txt.                                 |
15
// | If you did not receive a copy of the PHP license and are unable to   |
16
// | obtain it through the world-wide-web, please send a note to          |
17
// | [email protected] so we can mail you a copy immediately.               |
18
// +----------------------------------------------------------------------+
19
// | Author: Xavier Noguer <[email protected]>                              |
20
// | Based on OLE::Storage_Lite by Kawai, Takanori                        |
21
// +----------------------------------------------------------------------+
22
//
23
24
use PhpOffice\PhpSpreadsheet\Exception;
25
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
26
use PhpOffice\PhpSpreadsheet\Shared\OLE\ChainedBlockStream;
27
use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\Root;
28
29
/*
30
 * Array for storing OLE instances that are accessed from
31
 * OLE_ChainedBlockStream::stream_open().
32
 *
33
 * @var array
34
 */
35 69
$GLOBALS['_OLE_INSTANCES'] = [];
36
37
/**
38
 * OLE package base class.
39
 *
40
 * @author   Xavier Noguer <[email protected]>
41
 * @author   Christian Schmidt <[email protected]>
42
 */
43
class OLE
44
{
45
    const OLE_PPS_TYPE_ROOT = 5;
46
    const OLE_PPS_TYPE_DIR = 1;
47
    const OLE_PPS_TYPE_FILE = 2;
48
    const OLE_DATA_SIZE_SMALL = 0x1000;
49
    const OLE_LONG_INT_SIZE = 4;
50
    const OLE_PPS_SIZE = 0x80;
51
52
    /**
53
     * The file handle for reading an OLE container.
54
     *
55
     * @var resource
56
     */
57
    public $_file_handle;
58
59
    /**
60
     * Array of PPS's found on the OLE container.
61
     *
62
     * @var array
63
     */
64
    public $_list = [];
65
66
    /**
67
     * Root directory of OLE container.
68
     *
69
     * @var Root
70
     */
71
    public $root;
72
73
    /**
74
     * Big Block Allocation Table.
75
     *
76
     * @var array (blockId => nextBlockId)
77
     */
78
    public $bbat;
79
80
    /**
81
     * Short Block Allocation Table.
82
     *
83
     * @var array (blockId => nextBlockId)
84
     */
85
    public $sbat;
86
87
    /**
88
     * Size of big blocks. This is usually 512.
89
     *
90
     * @var int number of octets per block
91
     */
92
    public $bigBlockSize;
93
94
    /**
95
     * Size of small blocks. This is usually 64.
96
     *
97
     * @var int number of octets per block
98
     */
99
    public $smallBlockSize;
100
101
    /**
102
     * Threshold for big blocks.
103
     *
104
     * @var int
105
     */
106
    public $bigBlockThreshold;
107
108
    /**
109
     * Reads an OLE container from the contents of the file given.
110
     *
111
     * @acces public
112
     *
113
     * @param string $filename
114
     *
115
     * @return bool true on success, PEAR_Error on failure
116
     */
117 3
    public function read($filename)
118
    {
119 3
        $fh = @fopen($filename, 'rb');
120 3
        if ($fh === false) {
121 1
            throw new ReaderException("Can't open file $filename");
122
        }
123 2
        $this->_file_handle = $fh;
124
125 2
        $signature = fread($fh, 8);
126 2
        if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) {
127 1
            throw new ReaderException("File doesn't seem to be an OLE container.");
128
        }
129 1
        fseek($fh, 28);
130 1
        if (fread($fh, 2) != "\xFE\xFF") {
131
            // This shouldn't be a problem in practice
132
            throw new ReaderException('Only Little-Endian encoding is supported.');
133
        }
134
        // Size of blocks and short blocks in bytes
135 1
        $this->bigBlockSize = 2 ** self::readInt2($fh);
136 1
        $this->smallBlockSize = 2 ** self::readInt2($fh);
137
138
        // Skip UID, revision number and version number
139 1
        fseek($fh, 44);
140
        // Number of blocks in Big Block Allocation Table
141 1
        $bbatBlockCount = self::readInt4($fh);
142
143
        // Root chain 1st block
144 1
        $directoryFirstBlockId = self::readInt4($fh);
145
146
        // Skip unused bytes
147 1
        fseek($fh, 56);
148
        // Streams shorter than this are stored using small blocks
149 1
        $this->bigBlockThreshold = self::readInt4($fh);
150
        // Block id of first sector in Short Block Allocation Table
151 1
        $sbatFirstBlockId = self::readInt4($fh);
152
        // Number of blocks in Short Block Allocation Table
153 1
        $sbbatBlockCount = self::readInt4($fh);
154
        // Block id of first sector in Master Block Allocation Table
155 1
        $mbatFirstBlockId = self::readInt4($fh);
156
        // Number of blocks in Master Block Allocation Table
157 1
        $mbbatBlockCount = self::readInt4($fh);
158 1
        $this->bbat = [];
159
160
        // Remaining 4 * 109 bytes of current block is beginning of Master
161
        // Block Allocation Table
162 1
        $mbatBlocks = [];
163 1
        for ($i = 0; $i < 109; ++$i) {
164 1
            $mbatBlocks[] = self::readInt4($fh);
165
        }
166
167
        // Read rest of Master Block Allocation Table (if any is left)
168 1
        $pos = $this->getBlockOffset($mbatFirstBlockId);
169 1
        for ($i = 0; $i < $mbbatBlockCount; ++$i) {
170
            fseek($fh, $pos);
171
            for ($j = 0; $j < $this->bigBlockSize / 4 - 1; ++$j) {
172
                $mbatBlocks[] = self::readInt4($fh);
173
            }
174
            // Last block id in each block points to next block
175
            $pos = $this->getBlockOffset(self::readInt4($fh));
176
        }
177
178
        // Read Big Block Allocation Table according to chain specified by $mbatBlocks
179 1
        for ($i = 0; $i < $bbatBlockCount; ++$i) {
180 1
            $pos = $this->getBlockOffset($mbatBlocks[$i]);
181 1
            fseek($fh, $pos);
182 1
            for ($j = 0; $j < $this->bigBlockSize / 4; ++$j) {
183 1
                $this->bbat[] = self::readInt4($fh);
184
            }
185
        }
186
187
        // Read short block allocation table (SBAT)
188 1
        $this->sbat = [];
189 1
        $shortBlockCount = $sbbatBlockCount * $this->bigBlockSize / 4;
190 1
        $sbatFh = $this->getStream($sbatFirstBlockId);
191 1
        for ($blockId = 0; $blockId < $shortBlockCount; ++$blockId) {
192 1
            $this->sbat[$blockId] = self::readInt4($sbatFh);
193
        }
194 1
        fclose($sbatFh);
195
196 1
        $this->readPpsWks($directoryFirstBlockId);
197
198 1
        return true;
199
    }
200
201
    /**
202
     * @param int $blockId byte offset from beginning of file
203
     *
204
     * @return int
205
     */
206 1
    public function getBlockOffset($blockId)
207
    {
208 1
        return 512 + $blockId * $this->bigBlockSize;
209
    }
210
211
    /**
212
     * Returns a stream for use with fread() etc. External callers should
213
     * use \PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\File::getStream().
214
     *
215
     * @param int|OLE\PPS $blockIdOrPps block id or PPS
216
     *
217
     * @return resource read-only stream
218
     */
219 1
    public function getStream($blockIdOrPps)
220
    {
221 1
        static $isRegistered = false;
222 1
        if (!$isRegistered) {
223 1
            stream_wrapper_register('ole-chainedblockstream', ChainedBlockStream::class);
224 1
            $isRegistered = true;
225
        }
226
227
        // Store current instance in global array, so that it can be accessed
228
        // in OLE_ChainedBlockStream::stream_open().
229
        // Object is removed from self::$instances in OLE_Stream::close().
230 1
        $GLOBALS['_OLE_INSTANCES'][] = $this;
231 1
        $keys = array_keys($GLOBALS['_OLE_INSTANCES']);
232 1
        $instanceId = end($keys);
233
234 1
        $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId;
235 1
        if ($blockIdOrPps instanceof OLE\PPS) {
236
            $path .= '&blockId=' . $blockIdOrPps->startBlock;
237
            $path .= '&size=' . $blockIdOrPps->Size;
238
        } else {
239 1
            $path .= '&blockId=' . $blockIdOrPps;
240
        }
241
242 1
        $resource = fopen($path, 'rb');
243 1
        if ($resource === false) {
244
            throw new Exception("Unable to open stream $path");
245
        }
246
247 1
        return $resource;
248
    }
249
250
    /**
251
     * Reads a signed char.
252
     *
253
     * @param resource $fileHandle file handle
254
     *
255
     * @return int
256
     */
257 1
    private static function readInt1($fileHandle)
258
    {
259 1
        [, $tmp] = unpack('c', fread($fileHandle, 1) ?: '') ?: [0, 0];
260
261 1
        return $tmp;
262
    }
263
264
    /**
265
     * Reads an unsigned short (2 octets).
266
     *
267
     * @param resource $fileHandle file handle
268
     *
269
     * @return int
270
     */
271 1
    private static function readInt2($fileHandle)
272
    {
273 1
        [, $tmp] = unpack('v', fread($fileHandle, 2) ?: '') ?: [0, 0];
274
275 1
        return $tmp;
276
    }
277
278
    private const SIGNED_4OCTET_LIMIT = 2147483648;
279
280
    private const SIGNED_4OCTET_SUBTRACT = 2 * self::SIGNED_4OCTET_LIMIT;
281
282
    /**
283
     * Reads long (4 octets), interpreted as if signed on 32-bit system.
284
     *
285
     * @param resource $fileHandle file handle
286
     *
287
     * @return int
288
     */
289 1
    private static function readInt4($fileHandle)
290
    {
291 1
        [, $tmp] = unpack('V', fread($fileHandle, 4) ?: '') ?: [0, 0];
292 1
        if ($tmp >= self::SIGNED_4OCTET_LIMIT) {
293 1
            $tmp -= self::SIGNED_4OCTET_SUBTRACT;
294
        }
295
296 1
        return $tmp;
297
    }
298
299
    /**
300
     * Gets information about all PPS's on the OLE container from the PPS WK's
301
     * creates an OLE_PPS object for each one.
302
     *
303
     * @param int $blockId the block id of the first block
304
     *
305
     * @return bool true on success, PEAR_Error on failure
306
     */
307 1
    public function readPpsWks($blockId)
308
    {
309 1
        $fh = $this->getStream($blockId);
310 1
        for ($pos = 0; true; $pos += 128) {
311 1
            fseek($fh, $pos, SEEK_SET);
312 1
            $nameUtf16 = (string) fread($fh, 64);
313 1
            $nameLength = self::readInt2($fh);
314 1
            $nameUtf16 = substr($nameUtf16, 0, $nameLength - 2);
315
            // Simple conversion from UTF-16LE to ISO-8859-1
316 1
            $name = str_replace("\x00", '', $nameUtf16);
317 1
            $type = self::readInt1($fh);
318
            switch ($type) {
319 1
                case self::OLE_PPS_TYPE_ROOT:
320 1
                    $pps = new OLE\PPS\Root(null, null, []);
321 1
                    $this->root = $pps;
322
323 1
                    break;
324 1
                case self::OLE_PPS_TYPE_DIR:
325
                    $pps = new OLE\PPS(null, null, null, null, null, null, null, null, null, []);
326
327
                    break;
328 1
                case self::OLE_PPS_TYPE_FILE:
329 1
                    $pps = new OLE\PPS\File($name);
330
331 1
                    break;
332
                default:
333
                    throw new Exception('Unsupported PPS type');
334
            }
335 1
            fseek($fh, 1, SEEK_CUR);
336 1
            $pps->Type = $type;
337 1
            $pps->Name = $name;
338 1
            $pps->PrevPps = self::readInt4($fh);
339 1
            $pps->NextPps = self::readInt4($fh);
340 1
            $pps->DirPps = self::readInt4($fh);
341 1
            fseek($fh, 20, SEEK_CUR);
342 1
            $pps->Time1st = self::OLE2LocalDate((string) fread($fh, 8));
343 1
            $pps->Time2nd = self::OLE2LocalDate((string) fread($fh, 8));
344 1
            $pps->startBlock = self::readInt4($fh);
345 1
            $pps->Size = self::readInt4($fh);
346 1
            $pps->No = count($this->_list);
347 1
            $this->_list[] = $pps;
348
349
            // check if the PPS tree (starting from root) is complete
350 1
            if (isset($this->root) && $this->ppsTreeComplete($this->root->No)) { //* @phpstan-ignore-line
351 1
                break;
352
            }
353
        }
354 1
        fclose($fh);
355
356
        // Initialize $pps->children on directories
357 1
        foreach ($this->_list as $pps) {
358 1
            if ($pps->Type == self::OLE_PPS_TYPE_DIR || $pps->Type == self::OLE_PPS_TYPE_ROOT) {
359 1
                $nos = [$pps->DirPps];
360 1
                $pps->children = [];
361 1
                while (!empty($nos)) {
362 1
                    $no = array_pop($nos);
363 1
                    if ($no != -1) {
364 1
                        $childPps = $this->_list[$no];
365 1
                        $nos[] = $childPps->PrevPps;
366 1
                        $nos[] = $childPps->NextPps;
367 1
                        $pps->children[] = $childPps;
368
                    }
369
                }
370
            }
371
        }
372
373 1
        return true;
374
    }
375
376
    /**
377
     * It checks whether the PPS tree is complete (all PPS's read)
378
     * starting with the given PPS (not necessarily root).
379
     *
380
     * @param int $index The index of the PPS from which we are checking
381
     *
382
     * @return bool Whether the PPS tree for the given PPS is complete
383
     */
384 1
    private function ppsTreeComplete($index)
385
    {
386 1
        return isset($this->_list[$index]) &&
387 1
            ($pps = $this->_list[$index]) &&
388 1
            ($pps->PrevPps == -1 ||
389 1
                $this->ppsTreeComplete($pps->PrevPps)) &&
390 1
            ($pps->NextPps == -1 ||
391 1
                $this->ppsTreeComplete($pps->NextPps)) &&
392 1
            ($pps->DirPps == -1 ||
393 1
                $this->ppsTreeComplete($pps->DirPps));
394
    }
395
396
    /**
397
     * Checks whether a PPS is a File PPS or not.
398
     * If there is no PPS for the index given, it will return false.
399
     *
400
     * @param int $index The index for the PPS
401
     *
402
     * @return bool true if it's a File PPS, false otherwise
403
     */
404
    public function isFile($index)
405
    {
406
        if (isset($this->_list[$index])) {
407
            return $this->_list[$index]->Type == self::OLE_PPS_TYPE_FILE;
408
        }
409
410
        return false;
411
    }
412
413
    /**
414
     * Checks whether a PPS is a Root PPS or not.
415
     * If there is no PPS for the index given, it will return false.
416
     *
417
     * @param int $index the index for the PPS
418
     *
419
     * @return bool true if it's a Root PPS, false otherwise
420
     */
421
    public function isRoot($index)
422
    {
423
        if (isset($this->_list[$index])) {
424
            return $this->_list[$index]->Type == self::OLE_PPS_TYPE_ROOT;
425
        }
426
427
        return false;
428
    }
429
430
    /**
431
     * Gives the total number of PPS's found in the OLE container.
432
     *
433
     * @return int The total number of PPS's found in the OLE container
434
     */
435
    public function ppsTotal()
436
    {
437
        return count($this->_list);
438
    }
439
440
    /**
441
     * Gets data from a PPS
442
     * If there is no PPS for the index given, it will return an empty string.
443
     *
444
     * @param int $index The index for the PPS
445
     * @param int $position The position from which to start reading
446
     *                          (relative to the PPS)
447
     * @param int $length The amount of bytes to read (at most)
448
     *
449
     * @return string The binary string containing the data requested
450
     *
451
     * @see OLE_PPS_File::getStream()
452
     */
453
    public function getData($index, $position, $length)
454
    {
455
        // if position is not valid return empty string
456
        if (!isset($this->_list[$index]) || ($position >= $this->_list[$index]->Size) || ($position < 0)) {
457
            return '';
458
        }
459
        $fh = $this->getStream($this->_list[$index]);
460
        $data = (string) stream_get_contents($fh, $length, $position);
461
        fclose($fh);
462
463
        return $data;
464
    }
465
466
    /**
467
     * Gets the data length from a PPS
468
     * If there is no PPS for the index given, it will return 0.
469
     *
470
     * @param int $index The index for the PPS
471
     *
472
     * @return int The amount of bytes in data the PPS has
473
     */
474
    public function getDataLength($index)
475
    {
476
        if (isset($this->_list[$index])) {
477
            return $this->_list[$index]->Size;
478
        }
479
480
        return 0;
481
    }
482
483
    /**
484
     * Utility function to transform ASCII text to Unicode.
485
     *
486
     * @param string $ascii The ASCII string to transform
487
     *
488
     * @return string The string in Unicode
489
     */
490 90
    public static function ascToUcs($ascii)
491
    {
492 90
        $rawname = '';
493 90
        $iMax = strlen($ascii);
494 90
        for ($i = 0; $i < $iMax; ++$i) {
495 90
            $rawname .= $ascii[$i]
496 90
                . "\x00";
497
        }
498
499 90
        return $rawname;
500
    }
501
502
    /**
503
     * Utility function
504
     * Returns a string for the OLE container with the date given.
505
     *
506
     * @param float|int $date A timestamp
507
     *
508
     * @return string The string for the OLE container
509
     */
510 89
    public static function localDateToOLE($date)
511
    {
512 89
        if (!$date) {
513 89
            return "\x00\x00\x00\x00\x00\x00\x00\x00";
514
        }
515 89
        $dateTime = Date::dateTimeFromTimestamp("$date");
516
517
        // days from 1-1-1601 until the beggining of UNIX era
518 89
        $days = 134774;
519
        // calculate seconds
520 89
        $big_date = $days * 24 * 3600 + (float) $dateTime->format('U');
521
        // multiply just to make MS happy
522 89
        $big_date *= 10000000;
523
524
        // Make HEX string
525 89
        $res = '';
526
527 89
        $factor = 2 ** 56;
528 89
        while ($factor >= 1) {
529 89
            $hex = (int) floor($big_date / $factor);
530 89
            $res = pack('c', $hex) . $res;
531 89
            $big_date = fmod($big_date, $factor);
532 89
            $factor /= 256;
533
        }
534
535 89
        return $res;
536
    }
537
538
    /**
539
     * Returns a timestamp from an OLE container's date.
540
     *
541
     * @param string $oleTimestamp A binary string with the encoded date
542
     *
543
     * @return float|int The Unix timestamp corresponding to the string
544
     */
545 82
    public static function OLE2LocalDate($oleTimestamp)
546
    {
547 82
        if (strlen($oleTimestamp) != 8) {
548 1
            throw new ReaderException('Expecting 8 byte string');
549
        }
550
551
        // convert to units of 100 ns since 1601:
552 81
        $unpackedTimestamp = unpack('v4', $oleTimestamp) ?: [];
553 81
        $timestampHigh = (float) $unpackedTimestamp[4] * 65536 + (float) $unpackedTimestamp[3];
554 81
        $timestampLow = (float) $unpackedTimestamp[2] * 65536 + (float) $unpackedTimestamp[1];
555
556
        // translate to seconds since 1601:
557 81
        $timestampHigh /= 10000000;
558 81
        $timestampLow /= 10000000;
559
560
        // days from 1601 to 1970:
561 81
        $days = 134774;
562
563
        // translate to seconds since 1970:
564 81
        $unixTimestamp = floor(65536.0 * 65536.0 * $timestampHigh + $timestampLow - $days * 24 * 3600 + 0.5);
565
566 81
        return IntOrFloat::evaluate($unixTimestamp);
567
    }
568
}
569