PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Shared/OLERead.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 340 lines | 193 code | 63 blank | 84 comment | 47 complexity | 1475d8d05a94b1910838691b90cea981 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. define('IDENTIFIER_OLE', pack('CCCCCCCC', 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1));
  28. class PHPExcel_Shared_OLERead
  29. {
  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 Exception
  61. */
  62. public function read($sFileName)
  63. {
  64. // Check if file exists and is readable
  65. if (! is_readable($sFileName))
  66. {
  67. throw new Exception("Could not open " . $sFileName . " for reading! File does not exist, or it is not readable.");
  68. }
  69. // Get the file data
  70. $this->data = file_get_contents($sFileName);
  71. // Check OLE identifier
  72. if (substr($this->data, 0, 8) != self :: IDENTIFIER_OLE)
  73. {
  74. throw new Exception('The filename ' . $sFileName . ' is not recognised as an OLE file');
  75. }
  76. // Total number of sectors used for the SAT
  77. $this->numBigBlockDepotBlocks = self :: _GetInt4d($this->data, self :: NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  78. // SecID of the first sector of the directory stream
  79. $this->rootStartBlock = self :: _GetInt4d($this->data, self :: ROOT_START_BLOCK_POS);
  80. // SecID of the first sector of the SSAT (or -2 if not extant)
  81. $this->sbdStartBlock = self :: _GetInt4d($this->data, self :: SMALL_BLOCK_DEPOT_BLOCK_POS);
  82. // SecID of the first sector of the MSAT (or -2 if no additional sectors are used)
  83. $this->extensionBlock = self :: _GetInt4d($this->data, self :: EXTENSION_BLOCK_POS);
  84. // Total number of sectors used by MSAT
  85. $this->numExtensionBlocks = self :: _GetInt4d($this->data, self :: NUM_EXTENSION_BLOCK_POS);
  86. $bigBlockDepotBlocks = array();
  87. $pos = self :: BIG_BLOCK_DEPOT_BLOCKS_POS;
  88. $bbdBlocks = $this->numBigBlockDepotBlocks;
  89. if ($this->numExtensionBlocks != 0)
  90. {
  91. $bbdBlocks = (self :: BIG_BLOCK_SIZE - self :: BIG_BLOCK_DEPOT_BLOCKS_POS) / 4;
  92. }
  93. for($i = 0; $i < $bbdBlocks; ++ $i)
  94. {
  95. $bigBlockDepotBlocks[$i] = self :: _GetInt4d($this->data, $pos);
  96. $pos += 4;
  97. }
  98. for($j = 0; $j < $this->numExtensionBlocks; ++ $j)
  99. {
  100. $pos = ($this->extensionBlock + 1) * self :: BIG_BLOCK_SIZE;
  101. $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, self :: BIG_BLOCK_SIZE / 4 - 1);
  102. for($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; ++ $i)
  103. {
  104. $bigBlockDepotBlocks[$i] = self :: _GetInt4d($this->data, $pos);
  105. $pos += 4;
  106. }
  107. $bbdBlocks += $blocksToRead;
  108. if ($bbdBlocks < $this->numBigBlockDepotBlocks)
  109. {
  110. $this->extensionBlock = self :: _GetInt4d($this->data, $pos);
  111. }
  112. }
  113. $pos = $index = 0;
  114. $this->bigBlockChain = array();
  115. $bbs = self :: BIG_BLOCK_SIZE / 4;
  116. for($i = 0; $i < $this->numBigBlockDepotBlocks; ++ $i)
  117. {
  118. $pos = ($bigBlockDepotBlocks[$i] + 1) * self :: BIG_BLOCK_SIZE;
  119. for($j = 0; $j < $bbs; ++ $j)
  120. {
  121. $this->bigBlockChain[$index] = self :: _GetInt4d($this->data, $pos);
  122. $pos += 4;
  123. ++ $index;
  124. }
  125. }
  126. $pos = $index = 0;
  127. $sbdBlock = $this->sbdStartBlock;
  128. $this->smallBlockChain = array();
  129. while ($sbdBlock != - 2)
  130. {
  131. $pos = ($sbdBlock + 1) * self :: BIG_BLOCK_SIZE;
  132. for($j = 0; $j < $bbs; ++ $j)
  133. {
  134. $this->smallBlockChain[$index] = self :: _GetInt4d($this->data, $pos);
  135. $pos += 4;
  136. ++ $index;
  137. }
  138. $sbdBlock = $this->bigBlockChain[$sbdBlock];
  139. }
  140. // read the directory stream
  141. $block = $this->rootStartBlock;
  142. $this->entry = $this->_readData($block);
  143. $this->_readPropertySets();
  144. }
  145. /**
  146. * Extract binary stream data
  147. *
  148. * @return string
  149. */
  150. public function getStream($stream)
  151. {
  152. if (is_null($stream))
  153. {
  154. return null;
  155. }
  156. $streamData = '';
  157. if ($this->props[$stream]['size'] < self :: SMALL_BLOCK_THRESHOLD)
  158. {
  159. $rootdata = $this->_readData($this->props[$this->rootentry]['startBlock']);
  160. $block = $this->props[$stream]['startBlock'];
  161. while ($block != - 2)
  162. {
  163. $pos = $block * self :: SMALL_BLOCK_SIZE;
  164. $streamData .= substr($rootdata, $pos, self :: SMALL_BLOCK_SIZE);
  165. $block = $this->smallBlockChain[$block];
  166. }
  167. return $streamData;
  168. }
  169. else
  170. {
  171. $numBlocks = $this->props[$stream]['size'] / self :: BIG_BLOCK_SIZE;
  172. if ($this->props[$stream]['size'] % self :: BIG_BLOCK_SIZE != 0)
  173. {
  174. ++ $numBlocks;
  175. }
  176. if ($numBlocks == 0)
  177. return '';
  178. $block = $this->props[$stream]['startBlock'];
  179. while ($block != - 2)
  180. {
  181. $pos = ($block + 1) * self :: BIG_BLOCK_SIZE;
  182. $streamData .= substr($this->data, $pos, self :: BIG_BLOCK_SIZE);
  183. $block = $this->bigBlockChain[$block];
  184. }
  185. return $streamData;
  186. }
  187. }
  188. /**
  189. * Read a standard stream (by joining sectors using information from SAT)
  190. *
  191. * @param int $bl Sector ID where the stream starts
  192. * @return string Data for standard stream
  193. */
  194. private function _readData($bl)
  195. {
  196. $block = $bl;
  197. $data = '';
  198. while ($block != - 2)
  199. {
  200. $pos = ($block + 1) * self :: BIG_BLOCK_SIZE;
  201. $data .= substr($this->data, $pos, self :: BIG_BLOCK_SIZE);
  202. $block = $this->bigBlockChain[$block];
  203. }
  204. return $data;
  205. }
  206. /**
  207. * Read entries in the directory stream.
  208. */
  209. private function _readPropertySets()
  210. {
  211. $offset = 0;
  212. // loop through entires, each entry is 128 bytes
  213. $entryLen = strlen($this->entry);
  214. while ($offset < $entryLen)
  215. {
  216. // entry data (128 bytes)
  217. $d = substr($this->entry, $offset, self :: PROPERTY_STORAGE_BLOCK_SIZE);
  218. // size in bytes of name
  219. $nameSize = ord($d[self :: SIZE_OF_NAME_POS]) | (ord($d[self :: SIZE_OF_NAME_POS + 1]) << 8);
  220. // type of entry
  221. $type = ord($d[self :: TYPE_POS]);
  222. // sectorID of first sector or short sector, if this entry refers to a stream (the case with workbook)
  223. // sectorID of first sector of the short-stream container stream, if this entry is root entry
  224. $startBlock = self :: _GetInt4d($d, self :: START_BLOCK_POS);
  225. $size = self :: _GetInt4d($d, self :: SIZE_POS);
  226. $name = str_replace("\x00", "", substr($d, 0, $nameSize));
  227. $this->props[] = array('name' => $name, 'type' => $type, 'startBlock' => $startBlock, 'size' => $size);
  228. // Workbook directory entry (BIFF5 uses Book, BIFF8 uses Workbook)
  229. if (($name == 'Workbook') || ($name == 'Book') || ($name == 'WORKBOOK') || ($name == 'BOOK'))
  230. {
  231. $this->wrkbook = count($this->props) - 1;
  232. }
  233. // Root entry
  234. if ($name == 'Root Entry' || $name == 'ROOT ENTRY' || $name == 'R')
  235. {
  236. $this->rootentry = count($this->props) - 1;
  237. }
  238. // Summary information
  239. if ($name == chr(5) . 'SummaryInformation')
  240. {
  241. // echo 'Summary Information<br />';
  242. $this->summaryInformation = count($this->props) - 1;
  243. }
  244. // Additional Document Summary information
  245. if ($name == chr(5) . 'DocumentSummaryInformation')
  246. {
  247. // echo 'Document Summary Information<br />';
  248. $this->documentSummaryInformation = count($this->props) - 1;
  249. }
  250. $offset += self :: PROPERTY_STORAGE_BLOCK_SIZE;
  251. }
  252. }
  253. /**
  254. * Read 4 bytes of data at specified position
  255. *
  256. * @param string $data
  257. * @param int $pos
  258. * @return int
  259. */
  260. private static function _GetInt4d($data, $pos)
  261. {
  262. // FIX: represent numbers correctly on 64-bit system
  263. // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  264. // Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
  265. $_or_24 = ord($data[$pos + 3]);
  266. if ($_or_24 >= 128)
  267. {
  268. // negative number
  269. $_ord_24 = - abs((256 - $_or_24) << 24);
  270. }
  271. else
  272. {
  273. $_ord_24 = ($_or_24 & 127) << 24;
  274. }
  275. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $_ord_24;
  276. }
  277. }