Root.php 17 KB

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