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