Passed
Pull Request — master (#4421)
by Owen
16:20 queued 05:51
created

ChainedBlockStream   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
wmc 26
eloc 58
c 0
b 0
f 0
dl 0
loc 169
ccs 39
cts 57
cp 0.6842
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A stream_tell() 0 3 1
C stream_open() 0 53 13
A stream_eof() 0 3 1
A stream_close() 0 4 1
A stream_stat() 0 4 1
B stream_seek() 0 13 7
A stream_read() 0 9 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared\OLE;
4
5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\Shared\OLE;
7
8
class ChainedBlockStream
9
{
10
    /** @var mixed */
11
    public $context;
12
13
    /**
14
     * The OLE container of the file that is being read.
15
     */
16
    public ?OLE $ole = null;
17
18
    /**
19
     * Parameters specified by fopen().
20
     */
21
    public array $params = [];
22
23
    /**
24
     * The binary data of the file.
25
     */
26
    public string $data;
27
28
    /**
29
     * The file pointer.
30
     *
31
     * @var int byte offset
32
     */
33
    public int $pos = 0;
34
35
    /**
36
     * Implements support for fopen().
37
     * For creating streams using this wrapper, use OLE_PPS_File::getStream().
38
     *
39
     * @param string $path resource name including scheme, e.g.
40
     *                                    ole-chainedblockstream://oleInstanceId=1
41
     * @param string $mode only "r" is supported
42
     * @param int $options mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
43
     * @param ?string $openedPath absolute path of the opened stream (out parameter)
44
     *
45
     * @return bool true on success
46
     */
47 5
    public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool // @codingStandardsIgnoreLine
48
    {
49 5
        if ($mode[0] !== 'r') {
50 2
            if ($options & STREAM_REPORT_ERRORS) {
51 1
                trigger_error('Only reading is supported', E_USER_WARNING);
52
            }
53
54 2
            return false;
55
        }
56
57
        // 25 is length of "ole-chainedblockstream://"
58 3
        parse_str(substr($path, 25), $this->params);
59 3
        if (!isset($this->params['oleInstanceId'], $this->params['blockId'], $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) { //* @phpstan-ignore-line
60 2
            if ($options & STREAM_REPORT_ERRORS) {
61 1
                trigger_error('OLE stream not found', E_USER_WARNING);
62
            }
63
64 2
            return false;
65
        }
66 1
        $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']]; //* @phpstan-ignore-line
67 1
        if (!($this->ole instanceof OLE)) {
68
            throw new Exception('class is not OLE');
69
        }
70
71 1
        $blockId = $this->params['blockId'];
72 1
        $this->data = '';
73 1
        if (isset($this->params['size']) && $this->params['size'] < $this->ole->bigBlockThreshold && $blockId != $this->ole->root->startBlock) {
74
            // Block id refers to small blocks
75
            $rootPos = $this->ole->getBlockOffset((int) $this->ole->root->startBlock);
76
            while ($blockId != -2) {
77
                $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
78
                $blockId = $this->ole->sbat[$blockId];
79
                fseek($this->ole->_file_handle, $pos);
80
                $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
81
            }
82
        } else {
83
            // Block id refers to big blocks
84 1
            while ($blockId != -2) {
85 1
                $pos = $this->ole->getBlockOffset($blockId);
86 1
                fseek($this->ole->_file_handle, $pos);
87 1
                $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
88 1
                $blockId = $this->ole->bbat[$blockId];
89
            }
90
        }
91 1
        if (isset($this->params['size'])) {
92
            $this->data = substr($this->data, 0, $this->params['size']); //* @phpstan-ignore-line
93
        }
94
95 1
        if ($options & STREAM_USE_PATH) {
96
            $openedPath = $path;
97
        }
98
99 1
        return true;
100
    }
101
102
    /**
103
     * Implements support for fclose().
104
     */
105 1
    public function stream_close(): void // @codingStandardsIgnoreLine
106
    {
107 1
        $this->ole = null;
108 1
        unset($GLOBALS['_OLE_INSTANCES']);
109
    }
110
111
    /**
112
     * Implements support for fread(), fgets() etc.
113
     *
114
     * @param int $count maximum number of bytes to read
115
     *
116
     * @return false|string
117
     */
118 1
    public function stream_read(int $count): bool|string // @codingStandardsIgnoreLine
119
    {
120 1
        if ($this->stream_eof()) {
121
            return false;
122
        }
123 1
        $s = substr($this->data, (int) $this->pos, $count);
124 1
        $this->pos += $count;
125
126 1
        return $s;
127
    }
128
129
    /**
130
     * Implements support for feof().
131
     *
132
     * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
133
     */
134 1
    public function stream_eof(): bool // @codingStandardsIgnoreLine
135
    {
136 1
        return $this->pos >= strlen($this->data);
137
    }
138
139
    /**
140
     * Returns the position of the file pointer, i.e. its offset into the file
141
     * stream. Implements support for ftell().
142
     */
143 1
    public function stream_tell(): int // @codingStandardsIgnoreLine
144
    {
145 1
        return $this->pos;
146
    }
147
148
    /**
149
     * Implements support for fseek().
150
     *
151
     * @param int $offset byte offset
152
     * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
153
     */
154 1
    public function stream_seek(int $offset, int $whence): bool // @codingStandardsIgnoreLine
155
    {
156 1
        if ($whence == SEEK_SET && $offset >= 0) {
157 1
            $this->pos = $offset;
158
        } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
159
            $this->pos += $offset;
160
        } elseif ($whence == SEEK_END && -$offset <= count($this->data)) { // @phpstan-ignore-line
161
            $this->pos = strlen($this->data) + $offset;
162
        } else {
163
            return false;
164
        }
165
166 1
        return true;
167
    }
168
169
    /**
170
     * Implements support for fstat(). Currently the only supported field is
171
     * "size".
172
     */
173
    public function stream_stat(): array // @codingStandardsIgnoreLine
174
    {
175
        return [
176
            'size' => strlen($this->data),
177
        ];
178
    }
179
180
    // Methods used by stream_wrapper_register() that are not implemented:
181
    // bool stream_flush ( void )
182
    // int stream_write ( string data )
183
    // bool rename ( string path_from, string path_to )
184
    // bool mkdir ( string path, int mode, int options )
185
    // bool rmdir ( string path, int options )
186
    // bool dir_opendir ( string path, int options )
187
    // array url_stat ( string path, int flags )
188
    // string dir_readdir ( void )
189
    // bool dir_rewinddir ( void )
190
    // bool dir_closedir ( void )
191
}
192