No Description

OLERead.php 9.4KB

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