OLERead.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. defined('IDENTIFIER_OLE') ||
  8. define('IDENTIFIER_OLE', pack('CCCCCCCC', 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1));
  9. class PHPExcel_Shared_OLERead
  10. {
  11. private $data = '';
  12. // OLE identifier
  13. const IDENTIFIER_OLE = IDENTIFIER_OLE;
  14. // Size of a sector = 512 bytes
  15. const BIG_BLOCK_SIZE = 0x200;
  16. // Size of a short sector = 64 bytes
  17. const SMALL_BLOCK_SIZE = 0x40;
  18. // Size of a directory entry always = 128 bytes
  19. const PROPERTY_STORAGE_BLOCK_SIZE = 0x80;
  20. // Minimum size of a standard stream = 4096 bytes, streams smaller than this are stored as short streams
  21. const SMALL_BLOCK_THRESHOLD = 0x1000;
  22. // header offsets
  23. const NUM_BIG_BLOCK_DEPOT_BLOCKS_POS = 0x2c;
  24. const ROOT_START_BLOCK_POS = 0x30;
  25. const SMALL_BLOCK_DEPOT_BLOCK_POS = 0x3c;
  26. const EXTENSION_BLOCK_POS = 0x44;
  27. const NUM_EXTENSION_BLOCK_POS = 0x48;
  28. const BIG_BLOCK_DEPOT_BLOCKS_POS = 0x4c;
  29. // property storage offsets (directory offsets)
  30. const SIZE_OF_NAME_POS = 0x40;
  31. const TYPE_POS = 0x42;
  32. const START_BLOCK_POS = 0x74;
  33. const SIZE_POS = 0x78;
  34. public $wrkbook = null;
  35. public $summaryInformation = null;
  36. public $documentSummaryInformation = null;
  37. /**
  38. * Read the file
  39. *
  40. * @param $sFileName string Filename
  41. * @throws PHPExcel_Reader_Exception
  42. */
  43. public function read($sFileName)
  44. {
  45. // Check if file exists and is readable
  46. if (!is_readable($sFileName)) {
  47. throw new PHPExcel_Reader_Exception("Could not open " . $sFileName . " for reading! File does not exist, or it is not readable.");
  48. }
  49. // Get the file identifier
  50. // Don't bother reading the whole file until we know it's a valid OLE file
  51. $this->data = file_get_contents($sFileName, false, null, 0, 8);
  52. // Check OLE identifier
  53. if ($this->data != self::IDENTIFIER_OLE) {
  54. throw new PHPExcel_Reader_Exception('The filename ' . $sFileName . ' is not recognised as an OLE file');
  55. }
  56. // Get the file data
  57. $this->data = file_get_contents($sFileName);
  58. // Total number of sectors used for the SAT
  59. $this->numBigBlockDepotBlocks = self::getInt4d($this->data, self::NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  60. // SecID of the first sector of the directory stream
  61. $this->rootStartBlock = self::getInt4d($this->data, self::ROOT_START_BLOCK_POS);
  62. // SecID of the first sector of the SSAT (or -2 if not extant)
  63. $this->sbdStartBlock = self::getInt4d($this->data, self::SMALL_BLOCK_DEPOT_BLOCK_POS);
  64. // SecID of the first sector of the MSAT (or -2 if no additional sectors are used)
  65. $this->extensionBlock = self::getInt4d($this->data, self::EXTENSION_BLOCK_POS);
  66. // Total number of sectors used by MSAT
  67. $this->numExtensionBlocks = self::getInt4d($this->data, self::NUM_EXTENSION_BLOCK_POS);
  68. $bigBlockDepotBlocks = array();
  69. $pos = self::BIG_BLOCK_DEPOT_BLOCKS_POS;
  70. $bbdBlocks = $this->numBigBlockDepotBlocks;
  71. if ($this->numExtensionBlocks != 0) {
  72. $bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
  73. }
  74. for ($i = 0; $i < $bbdBlocks; ++$i) {
  75. $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos);
  76. $pos += 4;
  77. }
  78. for ($j = 0; $j < $this->numExtensionBlocks; ++$j) {
  79. $pos = ($this->extensionBlock + 1) * self::BIG_BLOCK_SIZE;
  80. $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, self::BIG_BLOCK_SIZE / 4 - 1);
  81. for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; ++$i) {
  82. $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos);
  83. $pos += 4;
  84. }
  85. $bbdBlocks += $blocksToRead;
  86. if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
  87. $this->extensionBlock = self::getInt4d($this->data, $pos);
  88. }
  89. }
  90. $pos = 0;
  91. $this->bigBlockChain = '';
  92. $bbs = self::BIG_BLOCK_SIZE / 4;
  93. for ($i = 0; $i < $this->numBigBlockDepotBlocks; ++$i) {
  94. $pos = ($bigBlockDepotBlocks[$i] + 1) * self::BIG_BLOCK_SIZE;
  95. $this->bigBlockChain .= substr($this->data, $pos, 4*$bbs);
  96. $pos += 4*$bbs;
  97. }
  98. $pos = 0;
  99. $sbdBlock = $this->sbdStartBlock;
  100. $this->smallBlockChain = '';
  101. while ($sbdBlock != -2) {
  102. $pos = ($sbdBlock + 1) * self::BIG_BLOCK_SIZE;
  103. $this->smallBlockChain .= substr($this->data, $pos, 4*$bbs);
  104. $pos += 4*$bbs;
  105. $sbdBlock = self::getInt4d($this->bigBlockChain, $sbdBlock*4);
  106. }
  107. // read the directory stream
  108. $block = $this->rootStartBlock;
  109. $this->entry = $this->_readData($block);
  110. $this->readPropertySets();
  111. }
  112. /**
  113. * Extract binary stream data
  114. *
  115. * @return string
  116. */
  117. public function getStream($stream)
  118. {
  119. if ($stream === null) {
  120. return null;
  121. }
  122. $streamData = '';
  123. if ($this->props[$stream]['size'] < self::SMALL_BLOCK_THRESHOLD) {
  124. $rootdata = $this->_readData($this->props[$this->rootentry]['startBlock']);
  125. $block = $this->props[$stream]['startBlock'];
  126. while ($block != -2) {
  127. $pos = $block * self::SMALL_BLOCK_SIZE;
  128. $streamData .= substr($rootdata, $pos, self::SMALL_BLOCK_SIZE);
  129. $block = self::getInt4d($this->smallBlockChain, $block*4);
  130. }
  131. return $streamData;
  132. } else {
  133. $numBlocks = $this->props[$stream]['size'] / self::BIG_BLOCK_SIZE;
  134. if ($this->props[$stream]['size'] % self::BIG_BLOCK_SIZE != 0) {
  135. ++$numBlocks;
  136. }
  137. if ($numBlocks == 0) {
  138. return '';
  139. }
  140. $block = $this->props[$stream]['startBlock'];
  141. while ($block != -2) {
  142. $pos = ($block + 1) * self::BIG_BLOCK_SIZE;
  143. $streamData .= substr($this->data, $pos, self::BIG_BLOCK_SIZE);
  144. $block = self::getInt4d($this->bigBlockChain, $block*4);
  145. }
  146. return $streamData;
  147. }
  148. }
  149. /**
  150. * Read a standard stream (by joining sectors using information from SAT)
  151. *
  152. * @param int $bl Sector ID where the stream starts
  153. * @return string Data for standard stream
  154. */
  155. private function _readData($bl)
  156. {
  157. $block = $bl;
  158. $data = '';
  159. while ($block != -2) {
  160. $pos = ($block + 1) * self::BIG_BLOCK_SIZE;
  161. $data .= substr($this->data, $pos, self::BIG_BLOCK_SIZE);
  162. $block = self::getInt4d($this->bigBlockChain, $block*4);
  163. }
  164. return $data;
  165. }
  166. /**
  167. * Read entries in the directory stream.
  168. */
  169. private function readPropertySets()
  170. {
  171. $offset = 0;
  172. // loop through entires, each entry is 128 bytes
  173. $entryLen = strlen($this->entry);
  174. while ($offset < $entryLen) {
  175. // entry data (128 bytes)
  176. $d = substr($this->entry, $offset, self::PROPERTY_STORAGE_BLOCK_SIZE);
  177. // size in bytes of name
  178. $nameSize = ord($d[self::SIZE_OF_NAME_POS]) | (ord($d[self::SIZE_OF_NAME_POS+1]) << 8);
  179. // type of entry
  180. $type = ord($d[self::TYPE_POS]);
  181. // sectorID of first sector or short sector, if this entry refers to a stream (the case with workbook)
  182. // sectorID of first sector of the short-stream container stream, if this entry is root entry
  183. $startBlock = self::getInt4d($d, self::START_BLOCK_POS);
  184. $size = self::getInt4d($d, self::SIZE_POS);
  185. $name = str_replace("\x00", "", substr($d, 0, $nameSize));
  186. $this->props[] = array(
  187. 'name' => $name,
  188. 'type' => $type,
  189. 'startBlock' => $startBlock,
  190. 'size' => $size
  191. );
  192. // tmp helper to simplify checks
  193. $upName = strtoupper($name);
  194. // Workbook directory entry (BIFF5 uses Book, BIFF8 uses Workbook)
  195. if (($upName === 'WORKBOOK') || ($upName === 'BOOK')) {
  196. $this->wrkbook = count($this->props) - 1;
  197. } elseif ($upName === 'ROOT ENTRY' || $upName === 'R') {
  198. // Root entry
  199. $this->rootentry = count($this->props) - 1;
  200. }
  201. // Summary information
  202. if ($name == chr(5) . 'SummaryInformation') {
  203. // echo 'Summary Information<br />';
  204. $this->summaryInformation = count($this->props) - 1;
  205. }
  206. // Additional Document Summary information
  207. if ($name == chr(5) . 'DocumentSummaryInformation') {
  208. // echo 'Document Summary Information<br />';
  209. $this->documentSummaryInformation = count($this->props) - 1;
  210. }
  211. $offset += self::PROPERTY_STORAGE_BLOCK_SIZE;
  212. }
  213. }
  214. /**
  215. * Read 4 bytes of data at specified position
  216. *
  217. * @param string $data
  218. * @param int $pos
  219. * @return int
  220. */
  221. private static function getInt4d($data, $pos)
  222. {
  223. // FIX: represent numbers correctly on 64-bit system
  224. // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  225. // Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
  226. $_or_24 = ord($data[$pos + 3]);
  227. if ($_or_24 >= 128) {
  228. // negative number
  229. $_ord_24 = -abs((256 - $_or_24) << 24);
  230. } else {
  231. $_ord_24 = ($_or_24 & 127) << 24;
  232. }
  233. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $_ord_24;
  234. }
  235. }