PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/PHPExcel/Shared/OLERead.php

https://bitbucket.org/crowdguru/phpexcel
PHP | 317 lines | 167 code | 66 blank | 84 comment | 49 complexity | a06af90bff7e388a455aaee12a085bf6 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 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. define('IDENTIFIER_OLE', pack('CCCCCCCC', 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1));
  28. class PHPExcel_Shared_OLERead {
  29. private $data = '';
  30. // OLE identifier
  31. const IDENTIFIER_OLE = IDENTIFIER_OLE;
  32. // Size of a sector = 512 bytes
  33. const BIG_BLOCK_SIZE = 0x200;
  34. // Size of a short sector = 64 bytes
  35. const SMALL_BLOCK_SIZE = 0x40;
  36. // Size of a directory entry always = 128 bytes
  37. const PROPERTY_STORAGE_BLOCK_SIZE = 0x80;
  38. // Minimum size of a standard stream = 4096 bytes, streams smaller than this are stored as short streams
  39. const SMALL_BLOCK_THRESHOLD = 0x1000;
  40. // header offsets
  41. const NUM_BIG_BLOCK_DEPOT_BLOCKS_POS = 0x2c;
  42. const ROOT_START_BLOCK_POS = 0x30;
  43. const SMALL_BLOCK_DEPOT_BLOCK_POS = 0x3c;
  44. const EXTENSION_BLOCK_POS = 0x44;
  45. const NUM_EXTENSION_BLOCK_POS = 0x48;
  46. const BIG_BLOCK_DEPOT_BLOCKS_POS = 0x4c;
  47. // property storage offsets (directory offsets)
  48. const SIZE_OF_NAME_POS = 0x40;
  49. const TYPE_POS = 0x42;
  50. const START_BLOCK_POS = 0x74;
  51. const SIZE_POS = 0x78;
  52. public $wrkbook = null;
  53. public $summaryInformation = null;
  54. public $documentSummaryInformation = null;
  55. /**
  56. * Read the file
  57. *
  58. * @param $sFileName string Filename
  59. * @throws Exception
  60. */
  61. public function read($sFileName)
  62. {
  63. // Check if file exists and is readable
  64. if(!is_readable($sFileName)) {
  65. throw new Exception("Could not open " . $sFileName . " for reading! File does not exist, or it is not readable.");
  66. }
  67. // Get the file data
  68. $this->data = file_get_contents($sFileName);
  69. // Check OLE identifier
  70. if (substr($this->data, 0, 8) != self::IDENTIFIER_OLE) {
  71. throw new Exception('The filename ' . $sFileName . ' is not recognised as an OLE file');
  72. }
  73. // Total number of sectors used for the SAT
  74. $this->numBigBlockDepotBlocks = self::_GetInt4d($this->data, self::NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  75. // SecID of the first sector of the directory stream
  76. $this->rootStartBlock = self::_GetInt4d($this->data, self::ROOT_START_BLOCK_POS);
  77. // SecID of the first sector of the SSAT (or -2 if not extant)
  78. $this->sbdStartBlock = self::_GetInt4d($this->data, self::SMALL_BLOCK_DEPOT_BLOCK_POS);
  79. // SecID of the first sector of the MSAT (or -2 if no additional sectors are used)
  80. $this->extensionBlock = self::_GetInt4d($this->data, self::EXTENSION_BLOCK_POS);
  81. // Total number of sectors used by MSAT
  82. $this->numExtensionBlocks = self::_GetInt4d($this->data, self::NUM_EXTENSION_BLOCK_POS);
  83. $bigBlockDepotBlocks = array();
  84. $pos = self::BIG_BLOCK_DEPOT_BLOCKS_POS;
  85. $bbdBlocks = $this->numBigBlockDepotBlocks;
  86. if ($this->numExtensionBlocks != 0) {
  87. $bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
  88. }
  89. for ($i = 0; $i < $bbdBlocks; ++$i) {
  90. $bigBlockDepotBlocks[$i] = self::_GetInt4d($this->data, $pos);
  91. $pos += 4;
  92. }
  93. for ($j = 0; $j < $this->numExtensionBlocks; ++$j) {
  94. $pos = ($this->extensionBlock + 1) * self::BIG_BLOCK_SIZE;
  95. $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, self::BIG_BLOCK_SIZE / 4 - 1);
  96. for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; ++$i) {
  97. $bigBlockDepotBlocks[$i] = self::_GetInt4d($this->data, $pos);
  98. $pos += 4;
  99. }
  100. $bbdBlocks += $blocksToRead;
  101. if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
  102. $this->extensionBlock = self::_GetInt4d($this->data, $pos);
  103. }
  104. }
  105. $pos = $index = 0;
  106. $this->bigBlockChain = array();
  107. $bbs = self::BIG_BLOCK_SIZE / 4;
  108. for ($i = 0; $i < $this->numBigBlockDepotBlocks; ++$i) {
  109. $pos = ($bigBlockDepotBlocks[$i] + 1) * self::BIG_BLOCK_SIZE;
  110. for ($j = 0 ; $j < $bbs; ++$j) {
  111. $this->bigBlockChain[$index] = self::_GetInt4d($this->data, $pos);
  112. $pos += 4 ;
  113. ++$index;
  114. }
  115. }
  116. $pos = $index = 0;
  117. $sbdBlock = $this->sbdStartBlock;
  118. $this->smallBlockChain = array();
  119. while ($sbdBlock != -2) {
  120. $pos = ($sbdBlock + 1) * self::BIG_BLOCK_SIZE;
  121. for ($j = 0; $j < $bbs; ++$j) {
  122. $this->smallBlockChain[$index] = self::_GetInt4d($this->data, $pos);
  123. $pos += 4;
  124. ++$index;
  125. }
  126. $sbdBlock = $this->bigBlockChain[$sbdBlock];
  127. }
  128. // read the directory stream
  129. $block = $this->rootStartBlock;
  130. $this->entry = $this->_readData($block);
  131. $this->_readPropertySets();
  132. }
  133. /**
  134. * Extract binary stream data
  135. *
  136. * @return string
  137. */
  138. public function getStream($stream)
  139. {
  140. if ($stream === NULL) {
  141. return null;
  142. }
  143. $streamData = '';
  144. if ($this->props[$stream]['size'] < self::SMALL_BLOCK_THRESHOLD) {
  145. $rootdata = $this->_readData($this->props[$this->rootentry]['startBlock']);
  146. $block = $this->props[$stream]['startBlock'];
  147. while ($block != -2) {
  148. $pos = $block * self::SMALL_BLOCK_SIZE;
  149. $streamData .= substr($rootdata, $pos, self::SMALL_BLOCK_SIZE);
  150. $block = $this->smallBlockChain[$block];
  151. }
  152. return $streamData;
  153. } else {
  154. $numBlocks = $this->props[$stream]['size'] / self::BIG_BLOCK_SIZE;
  155. if ($this->props[$stream]['size'] % self::BIG_BLOCK_SIZE != 0) {
  156. ++$numBlocks;
  157. }
  158. if ($numBlocks == 0) return '';
  159. $block = $this->props[$stream]['startBlock'];
  160. while ($block != -2) {
  161. $pos = ($block + 1) * self::BIG_BLOCK_SIZE;
  162. $streamData .= substr($this->data, $pos, self::BIG_BLOCK_SIZE);
  163. $block = $this->bigBlockChain[$block];
  164. }
  165. return $streamData;
  166. }
  167. }
  168. /**
  169. * Read a standard stream (by joining sectors using information from SAT)
  170. *
  171. * @param int $bl Sector ID where the stream starts
  172. * @return string Data for standard stream
  173. */
  174. private function _readData($bl)
  175. {
  176. $block = $bl;
  177. $data = '';
  178. while ($block != -2) {
  179. $pos = ($block + 1) * self::BIG_BLOCK_SIZE;
  180. $data .= substr($this->data, $pos, self::BIG_BLOCK_SIZE);
  181. $block = $this->bigBlockChain[$block];
  182. }
  183. return $data;
  184. }
  185. /**
  186. * Read entries in the directory stream.
  187. */
  188. private function _readPropertySets() {
  189. $offset = 0;
  190. // loop through entires, each entry is 128 bytes
  191. $entryLen = strlen($this->entry);
  192. while ($offset < $entryLen) {
  193. // entry data (128 bytes)
  194. $d = substr($this->entry, $offset, self::PROPERTY_STORAGE_BLOCK_SIZE);
  195. // size in bytes of name
  196. $nameSize = ord($d[self::SIZE_OF_NAME_POS]) | (ord($d[self::SIZE_OF_NAME_POS+1]) << 8);
  197. // type of entry
  198. $type = ord($d[self::TYPE_POS]);
  199. // sectorID of first sector or short sector, if this entry refers to a stream (the case with workbook)
  200. // sectorID of first sector of the short-stream container stream, if this entry is root entry
  201. $startBlock = self::_GetInt4d($d, self::START_BLOCK_POS);
  202. $size = self::_GetInt4d($d, self::SIZE_POS);
  203. $name = str_replace("\x00", "", substr($d,0,$nameSize));
  204. $this->props[] = array (
  205. 'name' => $name,
  206. 'type' => $type,
  207. 'startBlock' => $startBlock,
  208. 'size' => $size);
  209. // Workbook directory entry (BIFF5 uses Book, BIFF8 uses Workbook)
  210. if (($name == 'Workbook') || ($name == 'Book') || ($name == 'WORKBOOK') || ($name == 'BOOK')) {
  211. $this->wrkbook = count($this->props) - 1;
  212. }
  213. // Root entry
  214. if ($name == 'Root Entry' || $name == 'ROOT ENTRY' || $name == 'R') {
  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. }