ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
OLE_Root.php
Go to the documentation of this file.
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Xavier Noguer <xnoguer@php.net> |
17 // | Based on OLE::Storage_Lite by Kawai, Takanori |
18 // +----------------------------------------------------------------------+
19 //
20 // $Id: OLE_Root.php 21503 2009-09-07 07:33:14Z hschottm $
21 
22 
24 if (!defined('PHPEXCEL_ROOT')) {
28  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
29 }
30 
31 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/OLE/OLE_PPS.php';
32 
41  {
46  public $_tmp_dir;
47 
52  public function __construct($time_1st, $time_2nd, $raChild)
53  {
54  $this->_tmp_dir = '';
56  null,
57  PHPExcel_Shared_OLE::Asc2Ucs('Root Entry'),
59  null,
60  null,
61  null,
62  $time_1st,
63  $time_2nd,
64  null,
65  $raChild);
66  }
67 
75  public function setTempDir($dir)
76  {
77  if (is_dir($dir)) {
78  $this->_tmp_dir = $dir;
79  return true;
80  }
81  return false;
82  }
83 
93  public function save($filename)
94  {
95  // Initial Setting for saving
96  $this->_BIG_BLOCK_SIZE = pow(2,
97  ((isset($this->_BIG_BLOCK_SIZE))? $this->_adjust2($this->_BIG_BLOCK_SIZE) : 9));
98  $this->_SMALL_BLOCK_SIZE= pow(2,
99  ((isset($this->_SMALL_BLOCK_SIZE))? $this->_adjust2($this->_SMALL_BLOCK_SIZE): 6));
100 
101  // Open temp file if we are sending output to stdout
102  if ($filename == '-' || $filename == '') {
103  $this->_tmp_filename = tempnam($this->_tmp_dir, "OLE_PPS_Root");
104  $this->_FILEH_ = fopen($this->_tmp_filename,"w+b");
105  if ($this->_FILEH_ == false) {
106  throw new Exception("Can't create temporary file.");
107  }
108  } else {
109  $this->_FILEH_ = fopen($filename, "wb");
110  if ($this->_FILEH_ == false) {
111  throw new Exception("Can't open $filename. It may be in use or protected.");
112  }
113  }
114  // Make an array of PPS's (for Save)
115  $aList = array();
116  $this->_savePpsSetPnt($aList);
117  // calculate values for header
118  list($iSBDcnt, $iBBcnt, $iPPScnt) = $this->_calcSize($aList); //, $rhInfo);
119  // Save Header
120  $this->_saveHeader($iSBDcnt, $iBBcnt, $iPPScnt);
121 
122  // Make Small Data string (write SBD)
123  $this->_data = $this->_makeSmallData($aList);
124 
125  // Write BB
126  $this->_saveBigData($iSBDcnt, $aList);
127  // Write PPS
128  $this->_savePps($aList);
129  // Write Big Block Depot and BDList and Adding Header informations
130  $this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt);
131  // Close File, send it to stdout if necessary
132  if (($filename == '-') || ($filename == '')) {
133  fseek($this->_FILEH_, 0);
134  fpassthru($this->_FILEH_);
135  fclose($this->_FILEH_);
136  // Delete the temporary file.
137  unlink($this->_tmp_filename);
138  } else {
139  fclose($this->_FILEH_);
140  }
141 
142  return true;
143  }
144 
152  public function _calcSize(&$raList)
153  {
154  // Calculate Basic Setting
155  list($iSBDcnt, $iBBcnt, $iPPScnt) = array(0,0,0);
156  $iSmallLen = 0;
157  $iSBcnt = 0;
158  for ($i = 0; $i < count($raList); ++$i) {
159  if ($raList[$i]->Type == PHPExcel_Shared_OLE::OLE_PPS_TYPE_FILE) {
160  $raList[$i]->Size = $raList[$i]->_DataLen();
161  if ($raList[$i]->Size < PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) {
162  $iSBcnt += floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
163  + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0);
164  } else {
165  $iBBcnt += (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) +
166  (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0));
167  }
168  }
169  }
170  $iSmallLen = $iSBcnt * $this->_SMALL_BLOCK_SIZE;
171  $iSlCnt = floor($this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE);
172  $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt)? 1:0);
173  $iBBcnt += (floor($iSmallLen / $this->_BIG_BLOCK_SIZE) +
174  (( $iSmallLen % $this->_BIG_BLOCK_SIZE)? 1: 0));
175  $iCnt = count($raList);
176  $iBdCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_PPS_SIZE;
177  $iPPScnt = (floor($iCnt/$iBdCnt) + (($iCnt % $iBdCnt)? 1: 0));
178 
179  return array($iSBDcnt, $iBBcnt, $iPPScnt);
180  }
181 
190  public function _adjust2($i2)
191  {
192  $iWk = log($i2)/log(2);
193  return ($iWk > floor($iWk))? floor($iWk)+1:$iWk;
194  }
195 
204  public function _saveHeader($iSBDcnt, $iBBcnt, $iPPScnt)
205  {
206  $FILE = $this->_FILEH_;
207 
208  // Calculate Basic Setting
209  $iBlCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE;
210  $i1stBdL = ($this->_BIG_BLOCK_SIZE - 0x4C) / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE;
211 
212  $iBdExL = 0;
213  $iAll = $iBBcnt + $iPPScnt + $iSBDcnt;
214  $iAllW = $iAll;
215  $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt)? 1: 0);
216  $iBdCnt = floor(($iAll + $iBdCntW) / $iBlCnt) + ((($iAllW+$iBdCntW) % $iBlCnt)? 1: 0);
217 
218  // Calculate BD count
219  if ($iBdCnt > $i1stBdL) {
220  while (1) {
221  ++$iBdExL;
222  ++$iAllW;
223  $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt)? 1: 0);
224  $iBdCnt = floor(($iAllW + $iBdCntW) / $iBlCnt) + ((($iAllW+$iBdCntW) % $iBlCnt)? 1: 0);
225  if ($iBdCnt <= ($iBdExL*$iBlCnt+ $i1stBdL)) {
226  break;
227  }
228  }
229  }
230 
231  // Save Header
232  fwrite($FILE,
233  "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
234  . "\x00\x00\x00\x00"
235  . "\x00\x00\x00\x00"
236  . "\x00\x00\x00\x00"
237  . "\x00\x00\x00\x00"
238  . pack("v", 0x3b)
239  . pack("v", 0x03)
240  . pack("v", -2)
241  . pack("v", 9)
242  . pack("v", 6)
243  . pack("v", 0)
244  . "\x00\x00\x00\x00"
245  . "\x00\x00\x00\x00"
246  . pack("V", $iBdCnt)
247  . pack("V", $iBBcnt+$iSBDcnt) //ROOT START
248  . pack("V", 0)
249  . pack("V", 0x1000)
250  . pack("V", $iSBDcnt ? 0 : -2) //Small Block Depot
251  . pack("V", 1)
252  );
253  // Extra BDList Start, Count
254  if ($iBdCnt < $i1stBdL) {
255  fwrite($FILE,
256  pack("V", -2). // Extra BDList Start
257  pack("V", 0) // Extra BDList Count
258  );
259  } else {
260  fwrite($FILE, pack("V", $iAll+$iBdCnt) . pack("V", $iBdExL));
261  }
262 
263  // BDList
264  for ($i = 0; $i < $i1stBdL && $i < $iBdCnt; ++$i) {
265  fwrite($FILE, pack("V", $iAll+$i));
266  }
267  if ($i < $i1stBdL) {
268  for ($j = 0; $j < ($i1stBdL-$i); ++$j) {
269  fwrite($FILE, (pack("V", -1)));
270  }
271  }
272  }
273 
281  public function _saveBigData($iStBlk, &$raList)
282  {
283  $FILE = $this->_FILEH_;
284 
285  // cycle through PPS's
286  for ($i = 0; $i < count($raList); ++$i) {
287  if ($raList[$i]->Type != PHPExcel_Shared_OLE::OLE_PPS_TYPE_DIR) {
288  $raList[$i]->Size = $raList[$i]->_DataLen();
289  if (($raList[$i]->Size >= PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) ||
290  (($raList[$i]->Type == PHPExcel_Shared_OLE::OLE_PPS_TYPE_ROOT) && isset($raList[$i]->_data)))
291  {
292  // Write Data
293  if (isset($raList[$i]->_PPS_FILE)) {
294  $iLen = 0;
295  fseek($raList[$i]->_PPS_FILE, 0); // To The Top
296  while($sBuff = fread($raList[$i]->_PPS_FILE, 4096)) {
297  $iLen += strlen($sBuff);
298  fwrite($FILE, $sBuff);
299  }
300  } else {
301  fwrite($FILE, $raList[$i]->_data);
302  }
303 
304  if ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE) {
305  for ($j = 0; $j < ($this->_BIG_BLOCK_SIZE - ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)); ++$j) {
306  fwrite($FILE, "\x00");
307  }
308  }
309  // Set For PPS
310  $raList[$i]->_StartBlock = $iStBlk;
311  $iStBlk +=
312  (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) +
313  (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0));
314  }
315  // Close file for each PPS, and unlink it
316  if (isset($raList[$i]->_PPS_FILE)) {
317  fclose($raList[$i]->_PPS_FILE);
318  $raList[$i]->_PPS_FILE = null;
319  unlink($raList[$i]->_tmp_filename);
320  }
321  }
322  }
323  }
324 
331  public function _makeSmallData(&$raList)
332  {
333  $sRes = '';
334  $FILE = $this->_FILEH_;
335  $iSmBlk = 0;
336 
337  for ($i = 0; $i < count($raList); ++$i) {
338  // Make SBD, small data string
339  if ($raList[$i]->Type == PHPExcel_Shared_OLE::OLE_PPS_TYPE_FILE) {
340  if ($raList[$i]->Size <= 0) {
341  continue;
342  }
343  if ($raList[$i]->Size < PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) {
344  $iSmbCnt = floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
345  + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0);
346  // Add to SBD
347  for ($j = 0; $j < ($iSmbCnt-1); ++$j) {
348  fwrite($FILE, pack("V", $j+$iSmBlk+1));
349  }
350  fwrite($FILE, pack("V", -2));
351 
352  // Add to Data String(this will be written for RootEntry)
353  if ($raList[$i]->_PPS_FILE) {
354  fseek($raList[$i]->_PPS_FILE, 0); // To The Top
355  while ($sBuff = fread($raList[$i]->_PPS_FILE, 4096)) {
356  $sRes .= $sBuff;
357  }
358  } else {
359  $sRes .= $raList[$i]->_data;
360  }
361  if ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) {
362  for ($j = 0; $j < ($this->_SMALL_BLOCK_SIZE - ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)); ++$j) {
363  $sRes .= "\x00";
364  }
365  }
366  // Set for PPS
367  $raList[$i]->_StartBlock = $iSmBlk;
368  $iSmBlk += $iSmbCnt;
369  }
370  }
371  }
372  $iSbCnt = floor($this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE);
373  if ($iSmBlk % $iSbCnt) {
374  for ($i = 0; $i < ($iSbCnt - ($iSmBlk % $iSbCnt)); ++$i) {
375  fwrite($FILE, pack("V", -1));
376  }
377  }
378  return $sRes;
379  }
380 
387  public function _savePps(&$raList)
388  {
389  // Save each PPS WK
390  for ($i = 0; $i < count($raList); ++$i) {
391  fwrite($this->_FILEH_, $raList[$i]->_getPpsWk());
392  }
393  // Adjust for Block
394  $iCnt = count($raList);
395  $iBCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_PPS_SIZE;
396  if ($iCnt % $iBCnt) {
397  for ($i = 0; $i < (($iBCnt - ($iCnt % $iBCnt)) * PHPExcel_Shared_OLE::OLE_PPS_SIZE); ++$i) {
398  fwrite($this->_FILEH_, "\x00");
399  }
400  }
401  }
402 
411  public function _saveBbd($iSbdSize, $iBsize, $iPpsCnt)
412  {
413  $FILE = $this->_FILEH_;
414  // Calculate Basic Setting
415  $iBbCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE;
416  $i1stBdL = ($this->_BIG_BLOCK_SIZE - 0x4C) / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE;
417 
418  $iBdExL = 0;
419  $iAll = $iBsize + $iPpsCnt + $iSbdSize;
420  $iAllW = $iAll;
421  $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt)? 1: 0);
422  $iBdCnt = floor(($iAll + $iBdCntW) / $iBbCnt) + ((($iAllW+$iBdCntW) % $iBbCnt)? 1: 0);
423  // Calculate BD count
424  if ($iBdCnt >$i1stBdL) {
425  while (1) {
426  ++$iBdExL;
427  ++$iAllW;
428  $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt)? 1: 0);
429  $iBdCnt = floor(($iAllW + $iBdCntW) / $iBbCnt) + ((($iAllW+$iBdCntW) % $iBbCnt)? 1: 0);
430  if ($iBdCnt <= ($iBdExL*$iBbCnt+ $i1stBdL)) {
431  break;
432  }
433  }
434  }
435 
436  // Making BD
437  // Set for SBD
438  if ($iSbdSize > 0) {
439  for ($i = 0; $i < ($iSbdSize - 1); ++$i) {
440  fwrite($FILE, pack("V", $i+1));
441  }
442  fwrite($FILE, pack("V", -2));
443  }
444  // Set for B
445  for ($i = 0; $i < ($iBsize - 1); ++$i) {
446  fwrite($FILE, pack("V", $i+$iSbdSize+1));
447  }
448  fwrite($FILE, pack("V", -2));
449 
450  // Set for PPS
451  for ($i = 0; $i < ($iPpsCnt - 1); ++$i) {
452  fwrite($FILE, pack("V", $i+$iSbdSize+$iBsize+1));
453  }
454  fwrite($FILE, pack("V", -2));
455  // Set for BBD itself ( 0xFFFFFFFD : BBD)
456  for ($i = 0; $i < $iBdCnt; ++$i) {
457  fwrite($FILE, pack("V", 0xFFFFFFFD));
458  }
459  // Set for ExtraBDList
460  for ($i = 0; $i < $iBdExL; ++$i) {
461  fwrite($FILE, pack("V", 0xFFFFFFFC));
462  }
463  // Adjust for Block
464  if (($iAllW + $iBdCnt) % $iBbCnt) {
465  for ($i = 0; $i < ($iBbCnt - (($iAllW + $iBdCnt) % $iBbCnt)); ++$i) {
466  fwrite($FILE, pack("V", -1));
467  }
468  }
469  // Extra BDList
470  if ($iBdCnt > $i1stBdL) {
471  $iN=0;
472  $iNb=0;
473  for ($i = $i1stBdL;$i < $iBdCnt; $i++, ++$iN) {
474  if ($iN >= ($iBbCnt - 1)) {
475  $iN = 0;
476  ++$iNb;
477  fwrite($FILE, pack("V", $iAll+$iBdCnt+$iNb));
478  }
479  fwrite($FILE, pack("V", $iBsize+$iSbdSize+$iPpsCnt+$i));
480  }
481  if (($iBdCnt-$i1stBdL) % ($iBbCnt-1)) {
482  for ($i = 0; $i < (($iBbCnt - 1) - (($iBdCnt - $i1stBdL) % ($iBbCnt - 1))); ++$i) {
483  fwrite($FILE, pack("V", -1));
484  }
485  }
486  fwrite($FILE, pack("V", -2));
487  }
488  }
489  }