Failed Conditions
Push — master ( 735103...6a4138 )
by Adrien
12:48
created

PPS::getPpsWk()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 18
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 22
ccs 18
cts 18
cp 1
crap 2
rs 9.6666
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared\OLE;
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
use PhpOffice\PhpSpreadsheet\Shared\OLE;
24
25
/**
26
 * Class for creating PPS's for OLE containers.
27
 *
28
 * @author   Xavier Noguer <[email protected]>
29
 */
30
class PPS
31
{
32
    /**
33
     * The PPS index.
34
     *
35
     * @var int
36
     */
37
    public $No;
38
39
    /**
40
     * The PPS name (in Unicode).
41
     *
42
     * @var string
43
     */
44
    public $Name;
45
46
    /**
47
     * The PPS type. Dir, Root or File.
48
     *
49
     * @var int
50
     */
51
    public $Type;
52
53
    /**
54
     * The index of the previous PPS.
55
     *
56
     * @var int
57
     */
58
    public $PrevPps;
59
60
    /**
61
     * The index of the next PPS.
62
     *
63
     * @var int
64
     */
65
    public $NextPps;
66
67
    /**
68
     * The index of it's first child if this is a Dir or Root PPS.
69
     *
70
     * @var int
71
     */
72
    public $DirPps;
73
74
    /**
75
     * A timestamp.
76
     *
77
     * @var int
78
     */
79
    public $Time1st;
80
81
    /**
82
     * A timestamp.
83
     *
84
     * @var int
85
     */
86
    public $Time2nd;
87
88
    /**
89
     * Starting block (small or big) for this PPS's data  inside the container.
90
     *
91
     * @var int
92
     */
93
    public $startBlock;
94
95
    /**
96
     * The size of the PPS's data (in bytes).
97
     *
98
     * @var int
99
     */
100
    public $Size;
101
102
    /**
103
     * The PPS's data (only used if it's not using a temporary file).
104
     *
105
     * @var string
106
     */
107
    public $_data;
108
109
    /**
110
     * Array of child PPS's (only used by Root and Dir PPS's).
111
     *
112
     * @var array
113
     */
114
    public $children = [];
115
116
    /**
117
     * Pointer to OLE container.
118
     *
119
     * @var OLE
120
     */
121
    public $ole;
122
123
    /**
124
     * The constructor.
125
     *
126
     * @param int $No The PPS index
127
     * @param string $name The PPS name
128
     * @param int $type The PPS type. Dir, Root or File
129
     * @param int $prev The index of the previous PPS
130
     * @param int $next The index of the next PPS
131
     * @param int $dir The index of it's first child if this is a Dir or Root PPS
132
     * @param int $time_1st A timestamp
133
     * @param int $time_2nd A timestamp
134
     * @param string $data The (usually binary) source data of the PPS
135
     * @param array $children Array containing children PPS for this PPS
136
     */
137
    public function __construct($No, $name, $type, $prev, $next, $dir, $time_1st, $time_2nd, $data, $children)
138
    {
139 51
        $this->No = $No;
140
        $this->Name = $name;
141 51
        $this->Type = $type;
142 51
        $this->PrevPps = $prev;
143 51
        $this->NextPps = $next;
144 51
        $this->DirPps = $dir;
145 51
        $this->Time1st = $time_1st;
146 51
        $this->Time2nd = $time_2nd;
147 51
        $this->_data = $data;
148 51
        $this->children = $children;
149 51
        if ($data != '') {
150 51
            $this->Size = strlen($data);
151 51
        } else {
152
            $this->Size = 0;
153
        }
154 51
    }
155
156 51
    /**
157
     * Returns the amount of data saved for this PPS.
158
     *
159
     * @return int The amount of data (in bytes)
160
     */
161
    public function getDataLen()
162
    {
163 51
        if (!isset($this->_data)) {
164
            return 0;
165 51
        }
166
167
        return strlen($this->_data);
168
    }
169 51
170
    /**
171
     * Returns a string with the PPS's WK (What is a WK?).
172
     *
173
     * @return string The binary string
174
     */
175
    public function getPpsWk()
176
    {
177 51
        $ret = str_pad($this->Name, 64, "\x00");
178
179 51
        $ret .= pack('v', strlen($this->Name) + 2)  // 66
180
            . pack('c', $this->Type)              // 67
181 51
            . pack('c', 0x00) //UK                // 68
182 51
            . pack('V', $this->PrevPps) //Prev    // 72
183 51
            . pack('V', $this->NextPps) //Next    // 76
184 51
            . pack('V', $this->DirPps)  //Dir     // 80
185 51
            . "\x00\x09\x02\x00"                  // 84
186 51
            . "\x00\x00\x00\x00"                  // 88
187 51
            . "\xc0\x00\x00\x00"                  // 92
188 51
            . "\x00\x00\x00\x46"                  // 96 // Seems to be ok only for Root
189 51
            . "\x00\x00\x00\x00"                  // 100
190 51
            . OLE::localDateToOLE($this->Time1st)          // 108
191 51
            . OLE::localDateToOLE($this->Time2nd)          // 116
192 51
            . pack('V', isset($this->startBlock) ? $this->startBlock : 0)  // 120
193 51
            . pack('V', $this->Size)               // 124
194 51
            . pack('V', 0); // 128
195 51
196 51
        return $ret;
197 51
    }
198
199
    /**
200
     * Updates index and pointers to previous, next and children PPS's for this
201
     * PPS. I don't think it'll work with Dir PPS's.
202
     *
203
     * @param array &$raList Reference to the array of PPS's for the whole OLE
204
     *                          container
205
     * @param mixed $to_save
206
     * @param mixed $depth
207
     *
208
     * @return int The index for this PPS
209
     */
210
    public static function savePpsSetPnt(&$raList, $to_save, $depth = 0)
211 51
    {
212
        if (!is_array($to_save) || (empty($to_save))) {
213 51
            return 0xFFFFFFFF;
214 51
        } elseif (count($to_save) == 1) {
215 51
            $cnt = count($raList);
216 51
            // If the first entry, it's the root... Don't clone it!
217
            $raList[$cnt] = ($depth == 0) ? $to_save[0] : clone $to_save[0];
218 51
            $raList[$cnt]->No = $cnt;
219 51
            $raList[$cnt]->PrevPps = 0xFFFFFFFF;
220 51
            $raList[$cnt]->NextPps = 0xFFFFFFFF;
221 51
            $raList[$cnt]->DirPps = self::savePpsSetPnt($raList, @$raList[$cnt]->children, $depth++);
222 51
        } else {
223
            $iPos = floor(count($to_save) / 2);
224 51
            $aPrev = array_slice($to_save, 0, $iPos);
0 ignored issues
show
Bug introduced by
$iPos of type double is incompatible with the type integer expected by parameter $length of array_slice(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

224
            $aPrev = array_slice($to_save, 0, /** @scrutinizer ignore-type */ $iPos);
Loading history...
225 51
            $aNext = array_slice($to_save, $iPos + 1);
0 ignored issues
show
Bug introduced by
$iPos + 1 of type double is incompatible with the type integer expected by parameter $offset of array_slice(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

225
            $aNext = array_slice($to_save, /** @scrutinizer ignore-type */ $iPos + 1);
Loading history...
226 51
            $cnt = count($raList);
227 51
            // If the first entry, it's the root... Don't clone it!
228
            $raList[$cnt] = ($depth == 0) ? $to_save[$iPos] : clone $to_save[$iPos];
229 51
            $raList[$cnt]->No = $cnt;
230 51
            $raList[$cnt]->PrevPps = self::savePpsSetPnt($raList, $aPrev, $depth++);
231 51
            $raList[$cnt]->NextPps = self::savePpsSetPnt($raList, $aNext, $depth++);
232 51
            $raList[$cnt]->DirPps = self::savePpsSetPnt($raList, @$raList[$cnt]->children, $depth++);
233 51
        }
234
235
        return $cnt;
236 51
    }
237
}
238