Нема описа

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