a11538e4e3452b4947db503359d986a189efc3d5.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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: Root.php,v 1.9 2005/04/23 21:53:49 dufuz Exp $
  21. /**
  22. * Class for creating Root PPS's for OLE containers
  23. *
  24. * @author Xavier Noguer <xnoguer@php.net>
  25. * @category PHPExcel
  26. * @package PHPExcel_Shared_OLE
  27. */
  28. class PHPExcel_Shared_OLE_PPS_Root extends PHPExcel_Shared_OLE_PPS
  29. {
  30. /**
  31. * Directory for temporary files
  32. * @var string
  33. */
  34. protected $_tmp_dir = NULL;
  35. /**
  36. * @param integer $time_1st A timestamp
  37. * @param integer $time_2nd A timestamp
  38. */
  39. public function __construct($time_1st, $time_2nd, $raChild)
  40. {
  41. $this->_tempDir = PHPExcel_Shared_File::sys_get_temp_dir();
  42. parent::__construct(
  43. null,
  44. PHPExcel_Shared_OLE::Asc2Ucs('Root Entry'),
  45. PHPExcel_Shared_OLE::OLE_PPS_TYPE_ROOT,
  46. null,
  47. null,
  48. null,
  49. $time_1st,
  50. $time_2nd,
  51. null,
  52. $raChild);
  53. }
  54. /**
  55. * Method for saving the whole OLE container (including files).
  56. * In fact, if called with an empty argument (or '-'), it saves to a
  57. * temporary file and then outputs it's contents to stdout.
  58. * If a resource pointer to a stream created by fopen() is passed
  59. * it will be used, but you have to close such stream by yourself.
  60. *
  61. * @param string|resource $filename The name of the file or stream where to save the OLE container.
  62. * @access public
  63. * @return mixed true on success
  64. */
  65. public function save($filename)
  66. {
  67. // Initial Setting for saving
  68. $this->_BIG_BLOCK_SIZE = pow(2,
  69. ((isset($this->_BIG_BLOCK_SIZE))? self::_adjust2($this->_BIG_BLOCK_SIZE) : 9));
  70. $this->_SMALL_BLOCK_SIZE= pow(2,
  71. ((isset($this->_SMALL_BLOCK_SIZE))? self::_adjust2($this->_SMALL_BLOCK_SIZE): 6));
  72. if (is_resource($filename)) {
  73. $this->_FILEH_ = $filename;
  74. } else if ($filename == '-' || $filename == '') {
  75. if ($this->_tmp_dir === NULL)
  76. $this->_tmp_dir = PHPExcel_Shared_File::sys_get_temp_dir();
  77. $this->_tmp_filename = tempnam($this->_tmp_dir, "OLE_PPS_Root");
  78. $this->_FILEH_ = fopen($this->_tmp_filename,"w+b");
  79. if ($this->_FILEH_ == false) {
  80. throw new PHPExcel_Writer_Exception("Can't create temporary file.");
  81. }
  82. } else {
  83. $this->_FILEH_ = fopen($filename, "wb");
  84. }
  85. if ($this->_FILEH_ == false) {
  86. throw new PHPExcel_Writer_Exception("Can't open $filename. It may be in use or protected.");
  87. }
  88. // Make an array of PPS's (for Save)
  89. $aList = array();
  90. PHPExcel_Shared_OLE_PPS::_savePpsSetPnt($aList, array($this));
  91. // calculate values for header
  92. list($iSBDcnt, $iBBcnt, $iPPScnt) = $this->_calcSize($aList); //, $rhInfo);
  93. // Save Header
  94. $this->_saveHeader($iSBDcnt, $iBBcnt, $iPPScnt);
  95. // Make Small Data string (write SBD)
  96. $this->_data = $this->_makeSmallData($aList);
  97. // Write BB
  98. $this->_saveBigData($iSBDcnt, $aList);
  99. // Write PPS
  100. $this->_savePps($aList);
  101. // Write Big Block Depot and BDList and Adding Header informations
  102. $this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt);
  103. if (!is_resource($filename)) {
  104. fclose($this->_FILEH_);
  105. }
  106. return true;
  107. }
  108. /**
  109. * Calculate some numbers
  110. *
  111. * @access public
  112. * @param array $raList Reference to an array of PPS's
  113. * @return array The array of numbers
  114. */
  115. public function _calcSize(&$raList)
  116. {
  117. // Calculate Basic Setting
  118. list($iSBDcnt, $iBBcnt, $iPPScnt) = array(0,0,0);
  119. $iSmallLen = 0;
  120. $iSBcnt = 0;
  121. $iCount = count($raList);
  122. for ($i = 0; $i < $iCount; ++$i) {
  123. if ($raList[$i]->Type == PHPExcel_Shared_OLE::OLE_PPS_TYPE_FILE) {
  124. $raList[$i]->Size = $raList[$i]->_DataLen();
  125. if ($raList[$i]->Size < PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) {
  126. $iSBcnt += floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
  127. + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0);
  128. } else {
  129. $iBBcnt += (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) +
  130. (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0));
  131. }
  132. }
  133. }
  134. $iSmallLen = $iSBcnt * $this->_SMALL_BLOCK_SIZE;
  135. $iSlCnt = floor($this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE);
  136. $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt)? 1:0);
  137. $iBBcnt += (floor($iSmallLen / $this->_BIG_BLOCK_SIZE) +
  138. (( $iSmallLen % $this->_BIG_BLOCK_SIZE)? 1: 0));
  139. $iCnt = count($raList);
  140. $iBdCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_PPS_SIZE;
  141. $iPPScnt = (floor($iCnt/$iBdCnt) + (($iCnt % $iBdCnt)? 1: 0));
  142. return array($iSBDcnt, $iBBcnt, $iPPScnt);
  143. }
  144. /**
  145. * Helper function for caculating a magic value for block sizes
  146. *
  147. * @access public
  148. * @param integer $i2 The argument
  149. * @see save()
  150. * @return integer
  151. */
  152. private static function _adjust2($i2)
  153. {
  154. $iWk = log($i2)/log(2);
  155. return ($iWk > floor($iWk))? floor($iWk)+1:$iWk;
  156. }
  157. /**
  158. * Save OLE header
  159. *
  160. * @access public
  161. * @param integer $iSBDcnt
  162. * @param integer $iBBcnt
  163. * @param integer $iPPScnt
  164. */
  165. public function _saveHeader($iSBDcnt, $iBBcnt, $iPPScnt)
  166. {
  167. $FILE = $this->_FILEH_;
  168. // Calculate Basic Setting
  169. $iBlCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE;
  170. $i1stBdL = ($this->_BIG_BLOCK_SIZE - 0x4C) / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE;
  171. $iBdExL = 0;
  172. $iAll = $iBBcnt + $iPPScnt + $iSBDcnt;
  173. $iAllW = $iAll;
  174. $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt)? 1: 0);
  175. $iBdCnt = floor(($iAll + $iBdCntW) / $iBlCnt) + ((($iAllW+$iBdCntW) % $iBlCnt)? 1: 0);
  176. // Calculate BD count
  177. if ($iBdCnt > $i1stBdL) {
  178. while (1) {
  179. ++$iBdExL;
  180. ++$iAllW;
  181. $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt)? 1: 0);
  182. $iBdCnt = floor(($iAllW + $iBdCntW) / $iBlCnt) + ((($iAllW+$iBdCntW) % $iBlCnt)? 1: 0);
  183. if ($iBdCnt <= ($iBdExL*$iBlCnt+ $i1stBdL)) {
  184. break;
  185. }
  186. }
  187. }
  188. // Save Header
  189. fwrite($FILE,
  190. "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
  191. . "\x00\x00\x00\x00"
  192. . "\x00\x00\x00\x00"
  193. . "\x00\x00\x00\x00"
  194. . "\x00\x00\x00\x00"
  195. . pack("v", 0x3b)
  196. . pack("v", 0x03)
  197. . pack("v", -2)
  198. . pack("v", 9)
  199. . pack("v", 6)
  200. . pack("v", 0)
  201. . "\x00\x00\x00\x00"
  202. . "\x00\x00\x00\x00"
  203. . pack("V", $iBdCnt)
  204. . pack("V", $iBBcnt+$iSBDcnt) //ROOT START
  205. . pack("V", 0)
  206. . pack("V", 0x1000)
  207. . pack("V", $iSBDcnt ? 0 : -2) //Small Block Depot
  208. . pack("V", $iSBDcnt)
  209. );
  210. // Extra BDList Start, Count
  211. if ($iBdCnt < $i1stBdL) {
  212. fwrite($FILE,
  213. pack("V", -2) // Extra BDList Start
  214. . pack("V", 0) // Extra BDList Count
  215. );
  216. } else {
  217. fwrite($FILE, pack("V", $iAll+$iBdCnt) . pack("V", $iBdExL));
  218. }
  219. // BDList
  220. for ($i = 0; $i < $i1stBdL && $i < $iBdCnt; ++$i) {
  221. fwrite($FILE, pack("V", $iAll+$i));
  222. }
  223. if ($i < $i1stBdL) {
  224. $jB = $i1stBdL - $i;
  225. for ($j = 0; $j < $jB; ++$j) {
  226. fwrite($FILE, (pack("V", -1)));
  227. }
  228. }
  229. }
  230. /**
  231. * Saving big data (PPS's with data bigger than PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL)
  232. *
  233. * @access public
  234. * @param integer $iStBlk
  235. * @param array &$raList Reference to array of PPS's
  236. */
  237. public function _saveBigData($iStBlk, &$raList)
  238. {
  239. $FILE = $this->_FILEH_;
  240. // cycle through PPS's
  241. $iCount = count($raList);
  242. for ($i = 0; $i < $iCount; ++$i) {
  243. if ($raList[$i]->Type != PHPExcel_Shared_OLE::OLE_PPS_TYPE_DIR) {
  244. $raList[$i]->Size = $raList[$i]->_DataLen();
  245. if (($raList[$i]->Size >= PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) ||
  246. (($raList[$i]->Type == PHPExcel_Shared_OLE::OLE_PPS_TYPE_ROOT) && isset($raList[$i]->_data)))
  247. {
  248. // Write Data
  249. //if (isset($raList[$i]->_PPS_FILE)) {
  250. // $iLen = 0;
  251. // fseek($raList[$i]->_PPS_FILE, 0); // To The Top
  252. // while($sBuff = fread($raList[$i]->_PPS_FILE, 4096)) {
  253. // $iLen += strlen($sBuff);
  254. // fwrite($FILE, $sBuff);
  255. // }
  256. //} else {
  257. fwrite($FILE, $raList[$i]->_data);
  258. //}
  259. if ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE) {
  260. fwrite($FILE, str_repeat("\x00", $this->_BIG_BLOCK_SIZE - ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)));
  261. }
  262. // Set For PPS
  263. $raList[$i]->_StartBlock = $iStBlk;
  264. $iStBlk +=
  265. (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) +
  266. (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0));
  267. }
  268. // Close file for each PPS, and unlink it
  269. //if (isset($raList[$i]->_PPS_FILE)) {
  270. // fclose($raList[$i]->_PPS_FILE);
  271. // $raList[$i]->_PPS_FILE = null;
  272. // unlink($raList[$i]->_tmp_filename);
  273. //}
  274. }
  275. }
  276. }
  277. /**
  278. * get small data (PPS's with data smaller than PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL)
  279. *
  280. * @access public
  281. * @param array &$raList Reference to array of PPS's
  282. */
  283. public function _makeSmallData(&$raList)
  284. {
  285. $sRes = '';
  286. $FILE = $this->_FILEH_;
  287. $iSmBlk = 0;
  288. $iCount = count($raList);
  289. for ($i = 0; $i < $iCount; ++$i) {
  290. // Make SBD, small data string
  291. if ($raList[$i]->Type == PHPExcel_Shared_OLE::OLE_PPS_TYPE_FILE) {
  292. if ($raList[$i]->Size <= 0) {
  293. continue;
  294. }
  295. if ($raList[$i]->Size < PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) {
  296. $iSmbCnt = floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
  297. + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0);
  298. // Add to SBD
  299. $jB = $iSmbCnt - 1;
  300. for ($j = 0; $j < $jB; ++$j) {
  301. fwrite($FILE, pack("V", $j+$iSmBlk+1));
  302. }
  303. fwrite($FILE, pack("V", -2));
  304. //// Add to Data String(this will be written for RootEntry)
  305. //if ($raList[$i]->_PPS_FILE) {
  306. // fseek($raList[$i]->_PPS_FILE, 0); // To The Top
  307. // while ($sBuff = fread($raList[$i]->_PPS_FILE, 4096)) {
  308. // $sRes .= $sBuff;
  309. // }
  310. //} else {
  311. $sRes .= $raList[$i]->_data;
  312. //}
  313. if ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) {
  314. $sRes .= str_repeat("\x00",$this->_SMALL_BLOCK_SIZE - ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE));
  315. }
  316. // Set for PPS
  317. $raList[$i]->_StartBlock = $iSmBlk;
  318. $iSmBlk += $iSmbCnt;
  319. }
  320. }
  321. }
  322. $iSbCnt = floor($this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE);
  323. if ($iSmBlk % $iSbCnt) {
  324. $iB = $iSbCnt - ($iSmBlk % $iSbCnt);
  325. for ($i = 0; $i < $iB; ++$i) {
  326. fwrite($FILE, pack("V", -1));
  327. }
  328. }
  329. return $sRes;
  330. }
  331. /**
  332. * Saves all the PPS's WKs
  333. *
  334. * @access public
  335. * @param array $raList Reference to an array with all PPS's
  336. */
  337. public function _savePps(&$raList)
  338. {
  339. // Save each PPS WK
  340. $iC = count($raList);
  341. for ($i = 0; $i < $iC; ++$i) {
  342. fwrite($this->_FILEH_, $raList[$i]->_getPpsWk());
  343. }
  344. // Adjust for Block
  345. $iCnt = count($raList);
  346. $iBCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_PPS_SIZE;
  347. if ($iCnt % $iBCnt) {
  348. fwrite($this->_FILEH_, str_repeat("\x00",($iBCnt - ($iCnt % $iBCnt)) * PHPExcel_Shared_OLE::OLE_PPS_SIZE));
  349. }
  350. }
  351. /**
  352. * Saving Big Block Depot
  353. *
  354. * @access public
  355. * @param integer $iSbdSize
  356. * @param integer $iBsize
  357. * @param integer $iPpsCnt
  358. */
  359. public function _saveBbd($iSbdSize, $iBsize, $iPpsCnt)
  360. {
  361. $FILE = $this->_FILEH_;
  362. // Calculate Basic Setting
  363. $iBbCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE;
  364. $i1stBdL = ($this->_BIG_BLOCK_SIZE - 0x4C) / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE;
  365. $iBdExL = 0;
  366. $iAll = $iBsize + $iPpsCnt + $iSbdSize;
  367. $iAllW = $iAll;
  368. $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt)? 1: 0);
  369. $iBdCnt = floor(($iAll + $iBdCntW) / $iBbCnt) + ((($iAllW+$iBdCntW) % $iBbCnt)? 1: 0);
  370. // Calculate BD count
  371. if ($iBdCnt >$i1stBdL) {
  372. while (1) {
  373. ++$iBdExL;
  374. ++$iAllW;
  375. $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt)? 1: 0);
  376. $iBdCnt = floor(($iAllW + $iBdCntW) / $iBbCnt) + ((($iAllW+$iBdCntW) % $iBbCnt)? 1: 0);
  377. if ($iBdCnt <= ($iBdExL*$iBbCnt+ $i1stBdL)) {
  378. break;
  379. }
  380. }
  381. }
  382. // Making BD
  383. // Set for SBD
  384. if ($iSbdSize > 0) {
  385. for ($i = 0; $i < ($iSbdSize - 1); ++$i) {
  386. fwrite($FILE, pack("V", $i+1));
  387. }
  388. fwrite($FILE, pack("V", -2));
  389. }
  390. // Set for B
  391. for ($i = 0; $i < ($iBsize - 1); ++$i) {
  392. fwrite($FILE, pack("V", $i+$iSbdSize+1));
  393. }
  394. fwrite($FILE, pack("V", -2));
  395. // Set for PPS
  396. for ($i = 0; $i < ($iPpsCnt - 1); ++$i) {
  397. fwrite($FILE, pack("V", $i+$iSbdSize+$iBsize+1));
  398. }
  399. fwrite($FILE, pack("V", -2));
  400. // Set for BBD itself ( 0xFFFFFFFD : BBD)
  401. for ($i = 0; $i < $iBdCnt; ++$i) {
  402. fwrite($FILE, pack("V", 0xFFFFFFFD));
  403. }
  404. // Set for ExtraBDList
  405. for ($i = 0; $i < $iBdExL; ++$i) {
  406. fwrite($FILE, pack("V", 0xFFFFFFFC));
  407. }
  408. // Adjust for Block
  409. if (($iAllW + $iBdCnt) % $iBbCnt) {
  410. $iBlock = ($iBbCnt - (($iAllW + $iBdCnt) % $iBbCnt));
  411. for ($i = 0; $i < $iBlock; ++$i) {
  412. fwrite($FILE, pack("V", -1));
  413. }
  414. }
  415. // Extra BDList
  416. if ($iBdCnt > $i1stBdL) {
  417. $iN=0;
  418. $iNb=0;
  419. for ($i = $i1stBdL;$i < $iBdCnt; $i++, ++$iN) {
  420. if ($iN >= ($iBbCnt - 1)) {
  421. $iN = 0;
  422. ++$iNb;
  423. fwrite($FILE, pack("V", $iAll+$iBdCnt+$iNb));
  424. }
  425. fwrite($FILE, pack("V", $iBsize+$iSbdSize+$iPpsCnt+$i));
  426. }
  427. if (($iBdCnt-$i1stBdL) % ($iBbCnt-1)) {
  428. $iB = ($iBbCnt - 1) - (($iBdCnt - $i1stBdL) % ($iBbCnt - 1));
  429. for ($i = 0; $i < $iB; ++$i) {
  430. fwrite($FILE, pack("V", -1));
  431. }
  432. }
  433. fwrite($FILE, pack("V", -2));
  434. }
  435. }
  436. }