3 namespace PhpOffice\PhpSpreadsheet\Shared\OLE\PPS
;
5 // vim: set expandtab tabstop=4 shiftwidth=4:
6 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
23 use PhpOffice\PhpSpreadsheet\Shared\OLE
;
24 use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS
;
25 use PhpOffice\PhpSpreadsheet\Writer\Exception
as WriterException
;
28 * Class for creating Root PPS's for OLE containers.
30 * @author Xavier Noguer <xnoguer@php.net>
32 * @category PhpSpreadsheet
34 class Root
extends PPS
37 * Directory for temporary files.
41 protected $tempDirectory;
51 private $tempFilename;
56 private $smallBlockSize;
61 private $bigBlockSize;
64 * @param int $time_1st A timestamp
65 * @param int $time_2nd A timestamp
66 * @param File[] $raChild
68 public function __construct($time_1st, $time_2nd, $raChild)
70 $this->tempDirectory
= \PhpOffice\PhpSpreadsheet\Shared\File
::sysGetTempDir();
72 parent
::__construct(null, OLE
::ascToUcs('Root Entry'), OLE
::OLE_PPS_TYPE_ROOT
, null, null, null, $time_1st, $time_2nd, null, $raChild);
76 * Method for saving the whole OLE container (including files).
77 * In fact, if called with an empty argument (or '-'), it saves to a
78 * temporary file and then outputs it's contents to stdout.
79 * If a resource pointer to a stream created by fopen() is passed
80 * it will be used, but you have to close such stream by yourself.
82 * @param resource|string $filename the name of the file or stream where to save the OLE container
84 * @throws WriterException
86 * @return bool true on success
88 public function save($filename)
90 // Initial Setting for saving
91 $this->bigBlockSize
= pow(
93 (isset($this->bigBlockSize
)) ? self
::adjust2($this->bigBlockSize
) : 9
95 $this->smallBlockSize
= pow(
97 (isset($this->smallBlockSize
)) ? self
::adjust2($this->smallBlockSize
) : 6
100 if (is_resource($filename)) {
101 $this->fileHandle
= $filename;
102 } elseif ($filename == '-' ||
$filename == '') {
103 if ($this->tempDirectory
=== null) {
104 $this->tempDirectory
= \PhpOffice\PhpSpreadsheet\Shared\File
::sysGetTempDir();
106 $this->tempFilename
= tempnam($this->tempDirectory
, 'OLE_PPS_Root');
107 $this->fileHandle
= fopen($this->tempFilename
, 'w+b');
108 if ($this->fileHandle
== false) {
109 throw new WriterException("Can't create temporary file.");
112 $this->fileHandle
= fopen($filename, 'wb');
114 if ($this->fileHandle
== false) {
115 throw new WriterException("Can't open $filename. It may be in use or protected.");
117 // Make an array of PPS's (for Save)
119 PPS
::_savePpsSetPnt($aList, [$this]);
120 // calculate values for header
121 list($iSBDcnt, $iBBcnt, $iPPScnt) = $this->_calcSize($aList); //, $rhInfo);
123 $this->_saveHeader($iSBDcnt, $iBBcnt, $iPPScnt);
125 // Make Small Data string (write SBD)
126 $this->_data
= $this->_makeSmallData($aList);
129 $this->_saveBigData($iSBDcnt, $aList);
131 $this->_savePps($aList);
132 // Write Big Block Depot and BDList and Adding Header informations
133 $this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt);
135 if (!is_resource($filename)) {
136 fclose($this->fileHandle
);
143 * Calculate some numbers.
145 * @param array $raList Reference to an array of PPS's
147 * @return float[] The array of numbers
149 public function _calcSize(&$raList)
151 // Calculate Basic Setting
152 list($iSBDcnt, $iBBcnt, $iPPScnt) = [0, 0, 0];
155 $iCount = count($raList);
156 for ($i = 0; $i < $iCount; ++
$i) {
157 if ($raList[$i]->Type
== OLE
::OLE_PPS_TYPE_FILE
) {
158 $raList[$i]->Size
= $raList[$i]->getDataLen();
159 if ($raList[$i]->Size
< OLE
::OLE_DATA_SIZE_SMALL
) {
160 $iSBcnt +
= floor($raList[$i]->Size
/ $this->smallBlockSize
)
161 +
(($raList[$i]->Size %
$this->smallBlockSize
) ?
1 : 0);
163 $iBBcnt +
= (floor($raList[$i]->Size
/ $this->bigBlockSize
) +
164 (($raList[$i]->Size %
$this->bigBlockSize
) ?
1 : 0));
168 $iSmallLen = $iSBcnt * $this->smallBlockSize
;
169 $iSlCnt = floor($this->bigBlockSize
/ OLE
::OLE_LONG_INT_SIZE
);
170 $iSBDcnt = floor($iSBcnt / $iSlCnt) +
(($iSBcnt %
$iSlCnt) ?
1 : 0);
171 $iBBcnt +
= (floor($iSmallLen / $this->bigBlockSize
) +
172 (($iSmallLen %
$this->bigBlockSize
) ?
1 : 0));
173 $iCnt = count($raList);
174 $iBdCnt = $this->bigBlockSize
/ OLE
::OLE_PPS_SIZE
;
175 $iPPScnt = (floor($iCnt / $iBdCnt) +
(($iCnt %
$iBdCnt) ?
1 : 0));
177 return [$iSBDcnt, $iBBcnt, $iPPScnt];
181 * Helper function for caculating a magic value for block sizes.
183 * @param int $i2 The argument
189 private static function adjust2($i2)
191 $iWk = log($i2) / log(2);
193 return ($iWk > floor($iWk)) ?
floor($iWk) +
1 : $iWk;
199 * @param int $iSBDcnt
201 * @param int $iPPScnt
203 public function _saveHeader($iSBDcnt, $iBBcnt, $iPPScnt)
205 $FILE = $this->fileHandle
;
207 // Calculate Basic Setting
208 $iBlCnt = $this->bigBlockSize
/ OLE
::OLE_LONG_INT_SIZE
;
209 $i1stBdL = ($this->bigBlockSize
- 0x4C) / OLE
::OLE_LONG_INT_SIZE
;
212 $iAll = $iBBcnt +
$iPPScnt +
$iSBDcnt;
214 $iBdCntW = floor($iAllW / $iBlCnt) +
(($iAllW %
$iBlCnt) ?
1 : 0);
215 $iBdCnt = floor(($iAll +
$iBdCntW) / $iBlCnt) +
((($iAllW +
$iBdCntW) %
$iBlCnt) ?
1 : 0);
217 // Calculate BD count
218 if ($iBdCnt > $i1stBdL) {
222 $iBdCntW = floor($iAllW / $iBlCnt) +
(($iAllW %
$iBlCnt) ?
1 : 0);
223 $iBdCnt = floor(($iAllW +
$iBdCntW) / $iBlCnt) +
((($iAllW +
$iBdCntW) %
$iBlCnt) ?
1 : 0);
224 if ($iBdCnt <= ($iBdExL * $iBlCnt +
$i1stBdL)) {
233 "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
247 . pack('V', $iBBcnt +
$iSBDcnt) //ROOT START
250 . pack('V', $iSBDcnt ?
0 : -2) //Small Block Depot
251 . pack('V', $iSBDcnt)
253 // Extra BDList Start, Count
254 if ($iBdCnt < $i1stBdL) {
257 pack('V', -2) // Extra BDList Start
258 . pack('V', 0)// Extra BDList Count
261 fwrite($FILE, pack('V', $iAll +
$iBdCnt) . pack('V', $iBdExL));
265 for ($i = 0; $i < $i1stBdL && $i < $iBdCnt; ++
$i) {
266 fwrite($FILE, pack('V', $iAll +
$i));
270 for ($j = 0; $j < $jB; ++
$j) {
271 fwrite($FILE, (pack('V', -1)));
277 * Saving big data (PPS's with data bigger than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL).
280 * @param array &$raList Reference to array of PPS's
282 public function _saveBigData($iStBlk, &$raList)
284 $FILE = $this->fileHandle
;
286 // cycle through PPS's
287 $iCount = count($raList);
288 for ($i = 0; $i < $iCount; ++
$i) {
289 if ($raList[$i]->Type
!= OLE
::OLE_PPS_TYPE_DIR
) {
290 $raList[$i]->Size
= $raList[$i]->getDataLen();
291 if (($raList[$i]->Size
>= OLE
::OLE_DATA_SIZE_SMALL
) ||
(($raList[$i]->Type
== OLE
::OLE_PPS_TYPE_ROOT
) && isset($raList[$i]->_data
))) {
292 fwrite($FILE, $raList[$i]->_data
);
294 if ($raList[$i]->Size %
$this->bigBlockSize
) {
295 fwrite($FILE, str_repeat("\x00", $this->bigBlockSize
- ($raList[$i]->Size %
$this->bigBlockSize
)));
298 $raList[$i]->startBlock
= $iStBlk;
300 (floor($raList[$i]->Size
/ $this->bigBlockSize
) +
301 (($raList[$i]->Size %
$this->bigBlockSize
) ?
1 : 0));
308 * get small data (PPS's with data smaller than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL).
310 * @param array &$raList Reference to array of PPS's
314 public function _makeSmallData(&$raList)
317 $FILE = $this->fileHandle
;
320 $iCount = count($raList);
321 for ($i = 0; $i < $iCount; ++
$i) {
322 // Make SBD, small data string
323 if ($raList[$i]->Type
== OLE
::OLE_PPS_TYPE_FILE
) {
324 if ($raList[$i]->Size
<= 0) {
327 if ($raList[$i]->Size
< OLE
::OLE_DATA_SIZE_SMALL
) {
328 $iSmbCnt = floor($raList[$i]->Size
/ $this->smallBlockSize
)
329 +
(($raList[$i]->Size %
$this->smallBlockSize
) ?
1 : 0);
332 for ($j = 0; $j < $jB; ++
$j) {
333 fwrite($FILE, pack('V', $j +
$iSmBlk +
1));
335 fwrite($FILE, pack('V', -2));
337 // Add to Data String(this will be written for RootEntry)
338 $sRes .= $raList[$i]->_data
;
339 if ($raList[$i]->Size %
$this->smallBlockSize
) {
340 $sRes .= str_repeat("\x00", $this->smallBlockSize
- ($raList[$i]->Size %
$this->smallBlockSize
));
343 $raList[$i]->startBlock
= $iSmBlk;
348 $iSbCnt = floor($this->bigBlockSize
/ OLE
::OLE_LONG_INT_SIZE
);
349 if ($iSmBlk %
$iSbCnt) {
350 $iB = $iSbCnt - ($iSmBlk %
$iSbCnt);
351 for ($i = 0; $i < $iB; ++
$i) {
352 fwrite($FILE, pack('V', -1));
360 * Saves all the PPS's WKs.
362 * @param array $raList Reference to an array with all PPS's
364 public function _savePps(&$raList)
367 $iC = count($raList);
368 for ($i = 0; $i < $iC; ++
$i) {
369 fwrite($this->fileHandle
, $raList[$i]->_getPpsWk());
372 $iCnt = count($raList);
373 $iBCnt = $this->bigBlockSize
/ OLE
::OLE_PPS_SIZE
;
374 if ($iCnt %
$iBCnt) {
375 fwrite($this->fileHandle
, str_repeat("\x00", ($iBCnt - ($iCnt %
$iBCnt)) * OLE
::OLE_PPS_SIZE
));
380 * Saving Big Block Depot.
382 * @param int $iSbdSize
384 * @param int $iPpsCnt
386 public function _saveBbd($iSbdSize, $iBsize, $iPpsCnt)
388 $FILE = $this->fileHandle
;
389 // Calculate Basic Setting
390 $iBbCnt = $this->bigBlockSize
/ OLE
::OLE_LONG_INT_SIZE
;
391 $i1stBdL = ($this->bigBlockSize
- 0x4C) / OLE
::OLE_LONG_INT_SIZE
;
394 $iAll = $iBsize +
$iPpsCnt +
$iSbdSize;
396 $iBdCntW = floor($iAllW / $iBbCnt) +
(($iAllW %
$iBbCnt) ?
1 : 0);
397 $iBdCnt = floor(($iAll +
$iBdCntW) / $iBbCnt) +
((($iAllW +
$iBdCntW) %
$iBbCnt) ?
1 : 0);
398 // Calculate BD count
399 if ($iBdCnt > $i1stBdL) {
403 $iBdCntW = floor($iAllW / $iBbCnt) +
(($iAllW %
$iBbCnt) ?
1 : 0);
404 $iBdCnt = floor(($iAllW +
$iBdCntW) / $iBbCnt) +
((($iAllW +
$iBdCntW) %
$iBbCnt) ?
1 : 0);
405 if ($iBdCnt <= ($iBdExL * $iBbCnt +
$i1stBdL)) {
414 for ($i = 0; $i < ($iSbdSize - 1); ++
$i) {
415 fwrite($FILE, pack('V', $i +
1));
417 fwrite($FILE, pack('V', -2));
420 for ($i = 0; $i < ($iBsize - 1); ++
$i) {
421 fwrite($FILE, pack('V', $i +
$iSbdSize +
1));
423 fwrite($FILE, pack('V', -2));
426 for ($i = 0; $i < ($iPpsCnt - 1); ++
$i) {
427 fwrite($FILE, pack('V', $i +
$iSbdSize +
$iBsize +
1));
429 fwrite($FILE, pack('V', -2));
430 // Set for BBD itself ( 0xFFFFFFFD : BBD)
431 for ($i = 0; $i < $iBdCnt; ++
$i) {
432 fwrite($FILE, pack('V', 0xFFFFFFFD));
434 // Set for ExtraBDList
435 for ($i = 0; $i < $iBdExL; ++
$i) {
436 fwrite($FILE, pack('V', 0xFFFFFFFC));
439 if (($iAllW +
$iBdCnt) %
$iBbCnt) {
440 $iBlock = ($iBbCnt - (($iAllW +
$iBdCnt) %
$iBbCnt));
441 for ($i = 0; $i < $iBlock; ++
$i) {
442 fwrite($FILE, pack('V', -1));
446 if ($iBdCnt > $i1stBdL) {
449 for ($i = $i1stBdL; $i < $iBdCnt; $i++
, ++
$iN) {
450 if ($iN >= ($iBbCnt - 1)) {
453 fwrite($FILE, pack('V', $iAll +
$iBdCnt +
$iNb));
455 fwrite($FILE, pack('V', $iBsize +
$iSbdSize +
$iPpsCnt +
$i));
457 if (($iBdCnt - $i1stBdL) %
($iBbCnt - 1)) {
458 $iB = ($iBbCnt - 1) - (($iBdCnt - $i1stBdL) %
($iBbCnt - 1));
459 for ($i = 0; $i < $iB; ++
$i) {
460 fwrite($FILE, pack('V', -1));
463 fwrite($FILE, pack('V', -2));