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 */ 30class 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 $this->No = $No; 140 $this->Name = $name; 141 $this->Type = $type; 142 $this->PrevPps = $prev; 143 $this->NextPps = $next; 144 $this->DirPps = $dir; 145 $this->Time1st = $time_1st; 146 $this->Time2nd = $time_2nd; 147 $this->_data = $data; 148 $this->children = $children; 149 if ($data != '') { 150 $this->Size = strlen($data); 151 } else { 152 $this->Size = 0; 153 } 154 } 155 156 /** 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 if (!isset($this->_data)) { 164 return 0; 165 } 166 167 return strlen($this->_data); 168 } 169 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 $ret = str_pad($this->Name, 64, "\x00"); 178 179 $ret .= pack('v', strlen($this->Name) + 2) // 66 180 . pack('c', $this->Type) // 67 181 . pack('c', 0x00) //UK // 68 182 . pack('V', $this->PrevPps) //Prev // 72 183 . pack('V', $this->NextPps) //Next // 76 184 . pack('V', $this->DirPps) //Dir // 80 185 . "\x00\x09\x02\x00" // 84 186 . "\x00\x00\x00\x00" // 88 187 . "\xc0\x00\x00\x00" // 92 188 . "\x00\x00\x00\x46" // 96 // Seems to be ok only for Root 189 . "\x00\x00\x00\x00" // 100 190 . OLE::localDateToOLE($this->Time1st) // 108 191 . OLE::localDateToOLE($this->Time2nd) // 116 192 . pack('V', isset($this->startBlock) ? $this->startBlock : 0) // 120 193 . pack('V', $this->Size) // 124 194 . pack('V', 0); // 128 195 196 return $ret; 197 } 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 { 212 if (!is_array($to_save) || (empty($to_save))) { 213 return 0xFFFFFFFF; 214 } elseif (count($to_save) == 1) { 215 $cnt = count($raList); 216 // 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 $raList[$cnt]->No = $cnt; 219 $raList[$cnt]->PrevPps = 0xFFFFFFFF; 220 $raList[$cnt]->NextPps = 0xFFFFFFFF; 221 $raList[$cnt]->DirPps = self::savePpsSetPnt($raList, @$raList[$cnt]->children, $depth++); 222 } else { 223 $iPos = floor(count($to_save) / 2); 224 $aPrev = array_slice($to_save, 0, $iPos); 225 $aNext = array_slice($to_save, $iPos + 1); 226 $cnt = count($raList); 227 // 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 $raList[$cnt]->No = $cnt; 230 $raList[$cnt]->PrevPps = self::savePpsSetPnt($raList, $aPrev, $depth++); 231 $raList[$cnt]->NextPps = self::savePpsSetPnt($raList, $aNext, $depth++); 232 $raList[$cnt]->DirPps = self::savePpsSetPnt($raList, @$raList[$cnt]->children, $depth++); 233 } 234 235 return $cnt; 236 } 237} 238