Failed Conditions
Pull Request — master (#3876)
by Abdul Malik
22:45 queued 13:31
created

OLE   F

Complexity

Total Complexity 64

Size/Duplication

Total Lines 508
Duplicated Lines 0 %

Test Coverage

Coverage 82.51%

Importance

Changes 0
Metric Value
wmc 64
eloc 190
dl 0
loc 508
ccs 151
cts 183
cp 0.8251
rs 3.28
c 0
b 0
f 0

How to fix   Complexity   

Complex Class

Complex classes like OLE often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OLE, and based on these observations, apply Extract Interface, too.

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