OLE.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  8. // +----------------------------------------------------------------------+
  9. // | PHP Version 4 |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 1997-2002 The PHP Group |
  12. // +----------------------------------------------------------------------+
  13. // | This source file is subject to version 2.02 of the PHP license, |
  14. // | that is bundled with this package in the file LICENSE, and is |
  15. // | available at through the world-wide-web at |
  16. // | http://www.php.net/license/2_02.txt. |
  17. // | If you did not receive a copy of the PHP license and are unable to |
  18. // | obtain it through the world-wide-web, please send a note to |
  19. // | license@php.net so we can mail you a copy immediately. |
  20. // +----------------------------------------------------------------------+
  21. // | Author: Xavier Noguer <xnoguer@php.net> |
  22. // | Based on OLE::Storage_Lite by Kawai, Takanori |
  23. // +----------------------------------------------------------------------+
  24. //
  25. // $Id: OLE.php,v 1.13 2007/03/07 14:38:25 schmidt Exp $
  26. /**
  27. * Array for storing OLE instances that are accessed from
  28. * OLE_ChainedBlockStream::stream_open().
  29. * @var array
  30. */
  31. $GLOBALS['_OLE_INSTANCES'] = array();
  32. /**
  33. * OLE package base class.
  34. *
  35. * @author Xavier Noguer <xnoguer@php.net>
  36. * @author Christian Schmidt <schmidt@php.net>
  37. * @category PHPExcel
  38. * @package PHPExcel_Shared_OLE
  39. */
  40. class PHPExcel_Shared_OLE
  41. {
  42. const OLE_PPS_TYPE_ROOT = 5;
  43. const OLE_PPS_TYPE_DIR = 1;
  44. const OLE_PPS_TYPE_FILE = 2;
  45. const OLE_DATA_SIZE_SMALL = 0x1000;
  46. const OLE_LONG_INT_SIZE = 4;
  47. const OLE_PPS_SIZE = 0x80;
  48. /**
  49. * The file handle for reading an OLE container
  50. * @var resource
  51. */
  52. public $_file_handle;
  53. /**
  54. * Array of PPS's found on the OLE container
  55. * @var array
  56. */
  57. public $_list = array();
  58. /**
  59. * Root directory of OLE container
  60. * @var OLE_PPS_Root
  61. */
  62. public $root;
  63. /**
  64. * Big Block Allocation Table
  65. * @var array (blockId => nextBlockId)
  66. */
  67. public $bbat;
  68. /**
  69. * Short Block Allocation Table
  70. * @var array (blockId => nextBlockId)
  71. */
  72. public $sbat;
  73. /**
  74. * Size of big blocks. This is usually 512.
  75. * @var int number of octets per block.
  76. */
  77. public $bigBlockSize;
  78. /**
  79. * Size of small blocks. This is usually 64.
  80. * @var int number of octets per block
  81. */
  82. public $smallBlockSize;
  83. /**
  84. * Reads an OLE container from the contents of the file given.
  85. *
  86. * @acces public
  87. * @param string $file
  88. * @return mixed true on success, PEAR_Error on failure
  89. */
  90. public function read($file)
  91. {
  92. $fh = fopen($file, "r");
  93. if (!$fh) {
  94. throw new PHPExcel_Reader_Exception("Can't open file $file");
  95. }
  96. $this->_file_handle = $fh;
  97. $signature = fread($fh, 8);
  98. if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) {
  99. throw new PHPExcel_Reader_Exception("File doesn't seem to be an OLE container.");
  100. }
  101. fseek($fh, 28);
  102. if (fread($fh, 2) != "\xFE\xFF") {
  103. // This shouldn't be a problem in practice
  104. throw new PHPExcel_Reader_Exception("Only Little-Endian encoding is supported.");
  105. }
  106. // Size of blocks and short blocks in bytes
  107. $this->bigBlockSize = pow(2, self::_readInt2($fh));
  108. $this->smallBlockSize = pow(2, self::_readInt2($fh));
  109. // Skip UID, revision number and version number
  110. fseek($fh, 44);
  111. // Number of blocks in Big Block Allocation Table
  112. $bbatBlockCount = self::_readInt4($fh);
  113. // Root chain 1st block
  114. $directoryFirstBlockId = self::_readInt4($fh);
  115. // Skip unused bytes
  116. fseek($fh, 56);
  117. // Streams shorter than this are stored using small blocks
  118. $this->bigBlockThreshold = self::_readInt4($fh);
  119. // Block id of first sector in Short Block Allocation Table
  120. $sbatFirstBlockId = self::_readInt4($fh);
  121. // Number of blocks in Short Block Allocation Table
  122. $sbbatBlockCount = self::_readInt4($fh);
  123. // Block id of first sector in Master Block Allocation Table
  124. $mbatFirstBlockId = self::_readInt4($fh);
  125. // Number of blocks in Master Block Allocation Table
  126. $mbbatBlockCount = self::_readInt4($fh);
  127. $this->bbat = array();
  128. // Remaining 4 * 109 bytes of current block is beginning of Master
  129. // Block Allocation Table
  130. $mbatBlocks = array();
  131. for ($i = 0; $i < 109; ++$i) {
  132. $mbatBlocks[] = self::_readInt4($fh);
  133. }
  134. // Read rest of Master Block Allocation Table (if any is left)
  135. $pos = $this->_getBlockOffset($mbatFirstBlockId);
  136. for ($i = 0; $i < $mbbatBlockCount; ++$i) {
  137. fseek($fh, $pos);
  138. for ($j = 0; $j < $this->bigBlockSize / 4 - 1; ++$j) {
  139. $mbatBlocks[] = self::_readInt4($fh);
  140. }
  141. // Last block id in each block points to next block
  142. $pos = $this->_getBlockOffset(self::_readInt4($fh));
  143. }
  144. // Read Big Block Allocation Table according to chain specified by
  145. // $mbatBlocks
  146. for ($i = 0; $i < $bbatBlockCount; ++$i) {
  147. $pos = $this->_getBlockOffset($mbatBlocks[$i]);
  148. fseek($fh, $pos);
  149. for ($j = 0; $j < $this->bigBlockSize / 4; ++$j) {
  150. $this->bbat[] = self::_readInt4($fh);
  151. }
  152. }
  153. // Read short block allocation table (SBAT)
  154. $this->sbat = array();
  155. $shortBlockCount = $sbbatBlockCount * $this->bigBlockSize / 4;
  156. $sbatFh = $this->getStream($sbatFirstBlockId);
  157. for ($blockId = 0; $blockId < $shortBlockCount; ++$blockId) {
  158. $this->sbat[$blockId] = self::_readInt4($sbatFh);
  159. }
  160. fclose($sbatFh);
  161. $this->_readPpsWks($directoryFirstBlockId);
  162. return true;
  163. }
  164. /**
  165. * @param int block id
  166. * @param int byte offset from beginning of file
  167. * @access public
  168. */
  169. public function _getBlockOffset($blockId)
  170. {
  171. return 512 + $blockId * $this->bigBlockSize;
  172. }
  173. /**
  174. * Returns a stream for use with fread() etc. External callers should
  175. * use PHPExcel_Shared_OLE_PPS_File::getStream().
  176. * @param int|PPS block id or PPS
  177. * @return resource read-only stream
  178. */
  179. public function getStream($blockIdOrPps)
  180. {
  181. static $isRegistered = false;
  182. if (!$isRegistered) {
  183. stream_wrapper_register('ole-chainedblockstream', 'PHPExcel_Shared_OLE_ChainedBlockStream');
  184. $isRegistered = true;
  185. }
  186. // Store current instance in global array, so that it can be accessed
  187. // in OLE_ChainedBlockStream::stream_open().
  188. // Object is removed from self::$instances in OLE_Stream::close().
  189. $GLOBALS['_OLE_INSTANCES'][] = $this;
  190. $instanceId = end(array_keys($GLOBALS['_OLE_INSTANCES']));
  191. $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId;
  192. if ($blockIdOrPps instanceof PHPExcel_Shared_OLE_PPS) {
  193. $path .= '&blockId=' . $blockIdOrPps->_StartBlock;
  194. $path .= '&size=' . $blockIdOrPps->Size;
  195. } else {
  196. $path .= '&blockId=' . $blockIdOrPps;
  197. }
  198. return fopen($path, 'r');
  199. }
  200. /**
  201. * Reads a signed char.
  202. * @param resource file handle
  203. * @return int
  204. * @access public
  205. */
  206. private static function _readInt1($fh)
  207. {
  208. list(, $tmp) = unpack("c", fread($fh, 1));
  209. return $tmp;
  210. }
  211. /**
  212. * Reads an unsigned short (2 octets).
  213. * @param resource file handle
  214. * @return int
  215. * @access public
  216. */
  217. private static function _readInt2($fh)
  218. {
  219. list(, $tmp) = unpack("v", fread($fh, 2));
  220. return $tmp;
  221. }
  222. /**
  223. * Reads an unsigned long (4 octets).
  224. * @param resource file handle
  225. * @return int
  226. * @access public
  227. */
  228. private static function _readInt4($fh)
  229. {
  230. list(, $tmp) = unpack("V", fread($fh, 4));
  231. return $tmp;
  232. }
  233. /**
  234. * Gets information about all PPS's on the OLE container from the PPS WK's
  235. * creates an OLE_PPS object for each one.
  236. *
  237. * @access public
  238. * @param integer the block id of the first block
  239. * @return mixed true on success, PEAR_Error on failure
  240. */
  241. public function _readPpsWks($blockId)
  242. {
  243. $fh = $this->getStream($blockId);
  244. for ($pos = 0;; $pos += 128) {
  245. fseek($fh, $pos, SEEK_SET);
  246. $nameUtf16 = fread($fh, 64);
  247. $nameLength = self::_readInt2($fh);
  248. $nameUtf16 = substr($nameUtf16, 0, $nameLength - 2);
  249. // Simple conversion from UTF-16LE to ISO-8859-1
  250. $name = str_replace("\x00", "", $nameUtf16);
  251. $type = self::_readInt1($fh);
  252. switch ($type) {
  253. case self::OLE_PPS_TYPE_ROOT:
  254. $pps = new PHPExcel_Shared_OLE_PPS_Root(null, null, array());
  255. $this->root = $pps;
  256. break;
  257. case self::OLE_PPS_TYPE_DIR:
  258. $pps = new PHPExcel_Shared_OLE_PPS(null, null, null, null, null, null, null, null, null, array());
  259. break;
  260. case self::OLE_PPS_TYPE_FILE:
  261. $pps = new PHPExcel_Shared_OLE_PPS_File($name);
  262. break;
  263. default:
  264. continue;
  265. }
  266. fseek($fh, 1, SEEK_CUR);
  267. $pps->Type = $type;
  268. $pps->Name = $name;
  269. $pps->PrevPps = self::_readInt4($fh);
  270. $pps->NextPps = self::_readInt4($fh);
  271. $pps->DirPps = self::_readInt4($fh);
  272. fseek($fh, 20, SEEK_CUR);
  273. $pps->Time1st = self::OLE2LocalDate(fread($fh, 8));
  274. $pps->Time2nd = self::OLE2LocalDate(fread($fh, 8));
  275. $pps->_StartBlock = self::_readInt4($fh);
  276. $pps->Size = self::_readInt4($fh);
  277. $pps->No = count($this->_list);
  278. $this->_list[] = $pps;
  279. // check if the PPS tree (starting from root) is complete
  280. if (isset($this->root) && $this->_ppsTreeComplete($this->root->No)) {
  281. break;
  282. }
  283. }
  284. fclose($fh);
  285. // Initialize $pps->children on directories
  286. foreach ($this->_list as $pps) {
  287. if ($pps->Type == self::OLE_PPS_TYPE_DIR || $pps->Type == self::OLE_PPS_TYPE_ROOT) {
  288. $nos = array($pps->DirPps);
  289. $pps->children = array();
  290. while ($nos) {
  291. $no = array_pop($nos);
  292. if ($no != -1) {
  293. $childPps = $this->_list[$no];
  294. $nos[] = $childPps->PrevPps;
  295. $nos[] = $childPps->NextPps;
  296. $pps->children[] = $childPps;
  297. }
  298. }
  299. }
  300. }
  301. return true;
  302. }
  303. /**
  304. * It checks whether the PPS tree is complete (all PPS's read)
  305. * starting with the given PPS (not necessarily root)
  306. *
  307. * @access public
  308. * @param integer $index The index of the PPS from which we are checking
  309. * @return boolean Whether the PPS tree for the given PPS is complete
  310. */
  311. public function _ppsTreeComplete($index)
  312. {
  313. return isset($this->_list[$index]) &&
  314. ($pps = $this->_list[$index]) &&
  315. ($pps->PrevPps == -1 ||
  316. $this->_ppsTreeComplete($pps->PrevPps)) &&
  317. ($pps->NextPps == -1 ||
  318. $this->_ppsTreeComplete($pps->NextPps)) &&
  319. ($pps->DirPps == -1 ||
  320. $this->_ppsTreeComplete($pps->DirPps));
  321. }
  322. /**
  323. * Checks whether a PPS is a File PPS or not.
  324. * If there is no PPS for the index given, it will return false.
  325. *
  326. * @access public
  327. * @param integer $index The index for the PPS
  328. * @return bool true if it's a File PPS, false otherwise
  329. */
  330. public function isFile($index)
  331. {
  332. if (isset($this->_list[$index])) {
  333. return ($this->_list[$index]->Type == self::OLE_PPS_TYPE_FILE);
  334. }
  335. return false;
  336. }
  337. /**
  338. * Checks whether a PPS is a Root PPS or not.
  339. * If there is no PPS for the index given, it will return false.
  340. *
  341. * @access public
  342. * @param integer $index The index for the PPS.
  343. * @return bool true if it's a Root PPS, false otherwise
  344. */
  345. public function isRoot($index)
  346. {
  347. if (isset($this->_list[$index])) {
  348. return ($this->_list[$index]->Type == self::OLE_PPS_TYPE_ROOT);
  349. }
  350. return false;
  351. }
  352. /**
  353. * Gives the total number of PPS's found in the OLE container.
  354. *
  355. * @access public
  356. * @return integer The total number of PPS's found in the OLE container
  357. */
  358. public function ppsTotal()
  359. {
  360. return count($this->_list);
  361. }
  362. /**
  363. * Gets data from a PPS
  364. * If there is no PPS for the index given, it will return an empty string.
  365. *
  366. * @access public
  367. * @param integer $index The index for the PPS
  368. * @param integer $position The position from which to start reading
  369. * (relative to the PPS)
  370. * @param integer $length The amount of bytes to read (at most)
  371. * @return string The binary string containing the data requested
  372. * @see OLE_PPS_File::getStream()
  373. */
  374. public function getData($index, $position, $length)
  375. {
  376. // if position is not valid return empty string
  377. if (!isset($this->_list[$index]) || ($position >= $this->_list[$index]->Size) || ($position < 0)) {
  378. return '';
  379. }
  380. $fh = $this->getStream($this->_list[$index]);
  381. $data = stream_get_contents($fh, $length, $position);
  382. fclose($fh);
  383. return $data;
  384. }
  385. /**
  386. * Gets the data length from a PPS
  387. * If there is no PPS for the index given, it will return 0.
  388. *
  389. * @access public
  390. * @param integer $index The index for the PPS
  391. * @return integer The amount of bytes in data the PPS has
  392. */
  393. public function getDataLength($index)
  394. {
  395. if (isset($this->_list[$index])) {
  396. return $this->_list[$index]->Size;
  397. }
  398. return 0;
  399. }
  400. /**
  401. * Utility function to transform ASCII text to Unicode
  402. *
  403. * @access public
  404. * @static
  405. * @param string $ascii The ASCII string to transform
  406. * @return string The string in Unicode
  407. */
  408. public static function Asc2Ucs($ascii)
  409. {
  410. $rawname = '';
  411. for ($i = 0; $i < strlen($ascii); ++$i) {
  412. $rawname .= $ascii{$i} . "\x00";
  413. }
  414. return $rawname;
  415. }
  416. /**
  417. * Utility function
  418. * Returns a string for the OLE container with the date given
  419. *
  420. * @access public
  421. * @static
  422. * @param integer $date A timestamp
  423. * @return string The string for the OLE container
  424. */
  425. public static function LocalDate2OLE($date = null)
  426. {
  427. if (!isset($date)) {
  428. return "\x00\x00\x00\x00\x00\x00\x00\x00";
  429. }
  430. // factor used for separating numbers into 4 bytes parts
  431. $factor = pow(2, 32);
  432. // days from 1-1-1601 until the beggining of UNIX era
  433. $days = 134774;
  434. // calculate seconds
  435. $big_date = $days*24*3600 + gmmktime(date("H", $date), date("i", $date), date("s", $date), date("m", $date), date("d", $date), date("Y", $date));
  436. // multiply just to make MS happy
  437. $big_date *= 10000000;
  438. $high_part = floor($big_date / $factor);
  439. // lower 4 bytes
  440. $low_part = floor((($big_date / $factor) - $high_part) * $factor);
  441. // Make HEX string
  442. $res = '';
  443. for ($i = 0; $i < 4; ++$i) {
  444. $hex = $low_part % 0x100;
  445. $res .= pack('c', $hex);
  446. $low_part /= 0x100;
  447. }
  448. for ($i = 0; $i < 4; ++$i) {
  449. $hex = $high_part % 0x100;
  450. $res .= pack('c', $hex);
  451. $high_part /= 0x100;
  452. }
  453. return $res;
  454. }
  455. /**
  456. * Returns a timestamp from an OLE container's date
  457. *
  458. * @access public
  459. * @static
  460. * @param integer $string A binary string with the encoded date
  461. * @return string The timestamp corresponding to the string
  462. */
  463. public static function OLE2LocalDate($string)
  464. {
  465. if (strlen($string) != 8) {
  466. return new PEAR_Error("Expecting 8 byte string");
  467. }
  468. // factor used for separating numbers into 4 bytes parts
  469. $factor = pow(2, 32);
  470. list(, $high_part) = unpack('V', substr($string, 4, 4));
  471. list(, $low_part) = unpack('V', substr($string, 0, 4));
  472. $big_date = ($high_part * $factor) + $low_part;
  473. // translate to seconds
  474. $big_date /= 10000000;
  475. // days from 1-1-1601 until the beggining of UNIX era
  476. $days = 134774;
  477. // translate to seconds from beggining of UNIX era
  478. $big_date -= $days * 24 * 3600;
  479. return floor($big_date);
  480. }
  481. }