PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pear/OLE.php

https://bitbucket.org/blackriver/openx
PHP | 410 lines | 220 code | 31 blank | 159 comment | 35 complexity | ebb50817d31ec2948d7d56bf07eaaa9c MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Xavier Noguer <xnoguer@php.net> |
  17. // | Based on OLE::Storage_Lite by Kawai, Takanori |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: OLE.php 6 2006-12-15 17:27:27Z $
  21. /**
  22. * Constants for OLE package
  23. */
  24. define('OLE_PPS_TYPE_ROOT', 5);
  25. define('OLE_PPS_TYPE_DIR', 1);
  26. define('OLE_PPS_TYPE_FILE', 2);
  27. define('OLE_DATA_SIZE_SMALL', 0x1000);
  28. define('OLE_LONG_INT_SIZE', 4);
  29. define('OLE_PPS_SIZE', 0x80);
  30. require_once('PEAR.php');
  31. require_once 'OLE/PPS.php';
  32. /**
  33. * OLE package base class.
  34. *
  35. * @author Xavier Noguer <xnoguer@php.net>
  36. * @category Structures
  37. * @package OLE
  38. */
  39. class OLE extends PEAR
  40. {
  41. /**
  42. * The file handle for reading an OLE container
  43. * @var resource
  44. */
  45. var $_file_handle;
  46. /**
  47. * Array of PPS's found on the OLE container
  48. * @var array
  49. */
  50. var $_list;
  51. /**
  52. * Creates a new OLE object
  53. * Remember to use ampersand when creating an OLE object ($my_ole =& new OLE();)
  54. * @access public
  55. */
  56. function OLE()
  57. {
  58. $this->_list = array();
  59. }
  60. /**
  61. * Reads an OLE container from the contents of the file given.
  62. *
  63. * @acces public
  64. * @param string $file
  65. * @return mixed true on success, PEAR_Error on failure
  66. */
  67. function read($file)
  68. {
  69. /* consider storing offsets as constants */
  70. $big_block_size_offset = 30;
  71. $iBdbCnt_offset = 44;
  72. $bd_start_offset = 68;
  73. $fh = @fopen($file, "r");
  74. if ($fh == false) {
  75. return $this->raiseError("Can't open file $file");
  76. }
  77. $this->_file_handle = $fh;
  78. /* begin reading OLE attributes */
  79. fseek($fh, 0);
  80. $signature = fread($fh, 8);
  81. if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) {
  82. return $this->raiseError("File doesn't seem to be an OLE container.");
  83. }
  84. fseek($fh, $big_block_size_offset);
  85. $packed_array = unpack("v", fread($fh, 2));
  86. $big_block_size = pow(2, $packed_array['']);
  87. $packed_array = unpack("v", fread($fh, 2));
  88. $small_block_size = pow(2, $packed_array['']);
  89. $i1stBdL = ($big_block_size - 0x4C) / OLE_LONG_INT_SIZE;
  90. fseek($fh, $iBdbCnt_offset);
  91. $packed_array = unpack("V", fread($fh, 4));
  92. $iBdbCnt = $packed_array[''];
  93. $packed_array = unpack("V", fread($fh, 4));
  94. $pps_wk_start = $packed_array[''];
  95. fseek($fh, $bd_start_offset);
  96. $packed_array = unpack("V", fread($fh, 4));
  97. $bd_start = $packed_array[''];
  98. $packed_array = unpack("V", fread($fh, 4));
  99. $bd_count = $packed_array[''];
  100. $packed_array = unpack("V", fread($fh, 4));
  101. $iAll = $packed_array['']; // this may be wrong
  102. /* create OLE_PPS objects from */
  103. $ret = $this->_readPpsWks($pps_wk_start, $big_block_size);
  104. if (PEAR::isError($ret)) {
  105. return $ret;
  106. }
  107. return true;
  108. }
  109. /**
  110. * Destructor (using PEAR)
  111. * Just closes the file handle on the OLE file.
  112. *
  113. * @access private
  114. */
  115. function _OLE()
  116. {
  117. fclose($this->_file_handle);
  118. }
  119. /**
  120. * Gets information about all PPS's on the OLE container from the PPS WK's
  121. * creates an OLE_PPS object for each one.
  122. *
  123. * @access private
  124. * @param integer $pps_wk_start Position inside the OLE file where PPS WK's start
  125. * @param integer $big_block_size Size of big blobks in the OLE file
  126. * @return mixed true on success, PEAR_Error on failure
  127. */
  128. function _readPpsWks($pps_wk_start, $big_block_size)
  129. {
  130. $pointer = ($pps_wk_start + 1) * $big_block_size;
  131. while (1)
  132. {
  133. fseek($this->_file_handle, $pointer);
  134. $pps_wk = fread($this->_file_handle, OLE_PPS_SIZE);
  135. if (strlen($pps_wk) != OLE_PPS_SIZE) {
  136. break; // Excel likes to add a trailing byte sometimes
  137. //return $this->raiseError("PPS at $pointer seems too short: ".strlen($pps_wk));
  138. }
  139. $name_length = unpack("c", substr($pps_wk, 64, 2)); // FIXME (2 bytes??)
  140. $name_length = $name_length[''] - 2;
  141. $name = substr($pps_wk, 0, $name_length);
  142. $type = unpack("c", substr($pps_wk, 66, 1));
  143. if (($type[''] != OLE_PPS_TYPE_ROOT) and
  144. ($type[''] != OLE_PPS_TYPE_DIR) and
  145. ($type[''] != OLE_PPS_TYPE_FILE))
  146. {
  147. return $this->raiseError("PPS at $pointer has unknown type: {$type['']}");
  148. }
  149. $prev = unpack("V", substr($pps_wk, 68, 4));
  150. $next = unpack("V", substr($pps_wk, 72, 4));
  151. $dir = unpack("V", substr($pps_wk, 76, 4));
  152. // there is no magic number, it can take different values.
  153. //$magic = unpack("V", strrev(substr($pps_wk, 92, 4)));
  154. $time_1st = substr($pps_wk, 100, 8);
  155. $time_2nd = substr($pps_wk, 108, 8);
  156. $start_block = unpack("V", substr($pps_wk, 116, 4));
  157. $size = unpack("V", substr($pps_wk, 120, 4));
  158. // _data member will point to position in file!!
  159. // OLE_PPS object is created with an empty children array!!
  160. $this->_list[] = new OLE_PPS(null, '', $type[''], $prev[''], $next[''],
  161. $dir[''], OLE::OLE2LocalDate($time_1st),
  162. OLE::OLE2LocalDate($time_2nd),
  163. ($start_block[''] + 1) * $big_block_size, array());
  164. // give it a size
  165. $this->_list[count($this->_list) - 1]->Size = $size[''];
  166. // check if the PPS tree (starting from root) is complete
  167. if ($this->_ppsTreeComplete(0)) {
  168. break;
  169. }
  170. $pointer += OLE_PPS_SIZE;
  171. }
  172. }
  173. /**
  174. * It checks whether the PPS tree is complete (all PPS's read)
  175. * starting with the given PPS (not necessarily root)
  176. *
  177. * @access private
  178. * @param integer $index The index of the PPS from which we are checking
  179. * @return boolean Whether the PPS tree for the given PPS is complete
  180. */
  181. function _ppsTreeComplete($index)
  182. {
  183. if ($this->_list[$index]->NextPps != -1) {
  184. if (!isset($this->_list[$this->_list[$index]->NextPps])) {
  185. return false;
  186. }
  187. else {
  188. return $this->_ppsTreeComplete($this->_list[$index]->NextPps);
  189. }
  190. }
  191. if ($this->_list[$index]->DirPps != -1) {
  192. if (!isset($this->_list[$this->_list[$index]->DirPps])) {
  193. return false;
  194. }
  195. else {
  196. return $this->_ppsTreeComplete($this->_list[$index]->DirPps);
  197. }
  198. }
  199. return true;
  200. }
  201. /**
  202. * Checks whether a PPS is a File PPS or not.
  203. * If there is no PPS for the index given, it will return false.
  204. *
  205. * @access public
  206. * @param integer $index The index for the PPS
  207. * @return bool true if it's a File PPS, false otherwise
  208. */
  209. function isFile($index)
  210. {
  211. if (isset($this->_list[$index])) {
  212. return ($this->_list[$index]->Type == OLE_PPS_TYPE_FILE);
  213. }
  214. return false;
  215. }
  216. /**
  217. * Checks whether a PPS is a Root PPS or not.
  218. * If there is no PPS for the index given, it will return false.
  219. *
  220. * @access public
  221. * @param integer $index The index for the PPS.
  222. * @return bool true if it's a Root PPS, false otherwise
  223. */
  224. function isRoot($index)
  225. {
  226. if (isset($this->_list[$index])) {
  227. return ($this->_list[$index]->Type == OLE_PPS_TYPE_ROOT);
  228. }
  229. return false;
  230. }
  231. /**
  232. * Gives the total number of PPS's found in the OLE container.
  233. *
  234. * @access public
  235. * @return integer The total number of PPS's found in the OLE container
  236. */
  237. function ppsTotal()
  238. {
  239. return count($this->_list);
  240. }
  241. /**
  242. * Gets data from a PPS
  243. * If there is no PPS for the index given, it will return an empty string.
  244. *
  245. * @access public
  246. * @param integer $index The index for the PPS
  247. * @param integer $position The position from which to start reading
  248. * (relative to the PPS)
  249. * @param integer $length The amount of bytes to read (at most)
  250. * @return string The binary string containing the data requested
  251. */
  252. function getData($index, $position, $length)
  253. {
  254. // if position is not valid return empty string
  255. if (!isset($this->_list[$index]) or ($position >= $this->_list[$index]->Size) or ($position < 0)) {
  256. return '';
  257. }
  258. // Beware!!! _data member is actually a position
  259. fseek($this->_file_handle, $this->_list[$index]->_data + $position);
  260. return fread($this->_file_handle, $length);
  261. }
  262. /**
  263. * Gets the data length from a PPS
  264. * If there is no PPS for the index given, it will return 0.
  265. *
  266. * @access public
  267. * @param integer $index The index for the PPS
  268. * @return integer The amount of bytes in data the PPS has
  269. */
  270. function getDataLength($index)
  271. {
  272. if (isset($this->_list[$index])) {
  273. return $this->_list[$index]->Size;
  274. }
  275. return 0;
  276. }
  277. /**
  278. * Utility function to transform ASCII text to Unicode
  279. *
  280. * @access public
  281. * @static
  282. * @param string $ascii The ASCII string to transform
  283. * @return string The string in Unicode
  284. */
  285. function Asc2Ucs($ascii)
  286. {
  287. $rawname = '';
  288. for ($i = 0; $i < strlen($ascii); $i++) {
  289. $rawname .= $ascii{$i}."\x00";
  290. }
  291. return $rawname;
  292. }
  293. /**
  294. * Utility function
  295. * Returns a string for the OLE container with the date given
  296. *
  297. * @access public
  298. * @static
  299. * @param integer $date A timestamp
  300. * @return string The string for the OLE container
  301. */
  302. function LocalDate2OLE($date = null)
  303. {
  304. if (!isset($date)) {
  305. return "\x00\x00\x00\x00\x00\x00\x00\x00";
  306. }
  307. // factor used for separating numbers into 4 bytes parts
  308. $factor = pow(2,32);
  309. // days from 1-1-1601 until the beggining of UNIX era
  310. $days = 134774;
  311. // calculate seconds
  312. $big_date = $days*24*3600 + gmmktime(date("H",$date),date("i",$date),date("s",$date),
  313. date("m",$date),date("d",$date),date("Y",$date));
  314. // multiply just to make MS happy
  315. $big_date *= 10000000;
  316. $high_part = floor($big_date/$factor);
  317. // lower 4 bytes
  318. $low_part = floor((($big_date/$factor) - $high_part)*$factor);
  319. // Make HEX string
  320. $res = '';
  321. for ($i=0; $i<4; $i++)
  322. {
  323. $hex = $low_part % 0x100;
  324. $res .= pack('c', $hex);
  325. $low_part /= 0x100;
  326. }
  327. for ($i=0; $i<4; $i++)
  328. {
  329. $hex = $high_part % 0x100;
  330. $res .= pack('c', $hex);
  331. $high_part /= 0x100;
  332. }
  333. return $res;
  334. }
  335. /**
  336. * Returns a timestamp from an OLE container's date
  337. *
  338. * @access public
  339. * @static
  340. * @param integer $string A binary string with the encoded date
  341. * @return string The timestamp corresponding to the string
  342. */
  343. function OLE2LocalDate($string)
  344. {
  345. if (strlen($string) != 8) {
  346. return new PEAR_Error("Expecting 8 byte string");
  347. }
  348. // factor used for separating numbers into 4 bytes parts
  349. $factor = pow(2,32);
  350. $high_part = 0;
  351. for ($i=0; $i<4; $i++)
  352. {
  353. $al = unpack('C', $string{(7 - $i)});
  354. $high_part += $al[''];
  355. if ($i < 3) {
  356. $high_part *= 0x100;
  357. }
  358. }
  359. $low_part = 0;
  360. for ($i=4; $i<8; $i++)
  361. {
  362. $al = unpack('C', $string{(7 - $i)});
  363. $low_part += $al[''];
  364. if ($i < 7) {
  365. $low_part *= 0x100;
  366. }
  367. }
  368. $big_date = ($high_part*$factor) + $low_part;
  369. // translate to seconds
  370. $big_date /= 10000000;
  371. // days from 1-1-1601 until the beggining of UNIX era
  372. $days = 134774;
  373. // translate to seconds from beggining of UNIX era
  374. $big_date -= $days*24*3600;
  375. return floor($big_date);
  376. }
  377. }
  378. ?>