2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
24 * Constants for OLE package
26 define('OLE_PPS_TYPE_ROOT', 5);
27 define('OLE_PPS_TYPE_DIR', 1);
28 define('OLE_PPS_TYPE_FILE', 2);
29 define('OLE_DATA_SIZE_SMALL', 0x1000);
30 define('OLE_LONG_INT_SIZE', 4);
31 define('OLE_PPS_SIZE', 0x80);
33 require_once('PEAR.php');
34 require_once 'OLE/PPS.php';
37 * OLE package base class.
39 * @author Xavier Noguer <xnoguer@php.net>
40 * @category Structures
43 class OLE
extends PEAR
46 * The file handle for reading an OLE container
52 * Array of PPS's found on the OLE container
58 * Creates a new OLE object
59 * Remember to use ampersand when creating an OLE object ($my_ole =& new OLE();)
64 $this->_list
= array();
68 * Reads an OLE container from the contents of the file given.
72 * @return mixed true on success, PEAR_Error on failure
76 /* consider storing offsets as constants */
77 $big_block_size_offset = 30;
79 $bd_start_offset = 68;
81 $fh = @fopen
($file, "r");
83 return $this->raiseError("Can't open file $file");
85 $this->_file_handle
= $fh;
87 /* begin reading OLE attributes */
89 $signature = fread($fh, 8);
90 if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) {
91 return $this->raiseError("File doesn't seem to be an OLE container.");
93 fseek($fh, $big_block_size_offset);
94 $packed_array = unpack("v", fread($fh, 2));
95 $big_block_size = pow(2, $packed_array['']);
97 $packed_array = unpack("v", fread($fh, 2));
98 $small_block_size = pow(2, $packed_array['']);
99 $i1stBdL = ($big_block_size - 0x4C) / OLE_LONG_INT_SIZE
;
101 fseek($fh, $iBdbCnt_offset);
102 $packed_array = unpack("V", fread($fh, 4));
103 $iBdbCnt = $packed_array[''];
105 $packed_array = unpack("V", fread($fh, 4));
106 $pps_wk_start = $packed_array[''];
108 fseek($fh, $bd_start_offset);
109 $packed_array = unpack("V", fread($fh, 4));
110 $bd_start = $packed_array[''];
111 $packed_array = unpack("V", fread($fh, 4));
112 $bd_count = $packed_array[''];
113 $packed_array = unpack("V", fread($fh, 4));
114 $iAll = $packed_array['']; // this may be wrong
115 /* create OLE_PPS objects from */
116 $ret = $this->_readPpsWks($pps_wk_start, $big_block_size);
117 if (PEAR
::isError($ret)) {
124 * Destructor (using PEAR)
125 * Just closes the file handle on the OLE file.
131 fclose($this->_file_handle
);
135 * Gets information about all PPS's on the OLE container from the PPS WK's
136 * creates an OLE_PPS object for each one.
139 * @param integer $pps_wk_start Position inside the OLE file where PPS WK's start
140 * @param integer $big_block_size Size of big blobks in the OLE file
141 * @return mixed true on success, PEAR_Error on failure
143 function _readPpsWks($pps_wk_start, $big_block_size)
145 $pointer = ($pps_wk_start +
1) * $big_block_size;
148 fseek($this->_file_handle
, $pointer);
149 $pps_wk = fread($this->_file_handle
, OLE_PPS_SIZE
);
150 if (strlen($pps_wk) != OLE_PPS_SIZE
) {
151 break; // Excel likes to add a trailing byte sometimes
152 //return $this->raiseError("PPS at $pointer seems too short: ".strlen($pps_wk));
154 $name_length = unpack("c", substr($pps_wk, 64, 2)); // FIXME (2 bytes??)
155 $name_length = $name_length[''] - 2;
156 $name = substr($pps_wk, 0, $name_length);
157 $type = unpack("c", substr($pps_wk, 66, 1));
158 if (($type[''] != OLE_PPS_TYPE_ROOT
) and
159 ($type[''] != OLE_PPS_TYPE_DIR
) and
160 ($type[''] != OLE_PPS_TYPE_FILE
))
162 return $this->raiseError("PPS at $pointer has unknown type: {$type['']}");
164 $prev = unpack("V", substr($pps_wk, 68, 4));
165 $next = unpack("V", substr($pps_wk, 72, 4));
166 $dir = unpack("V", substr($pps_wk, 76, 4));
167 // there is no magic number, it can take different values.
168 //$magic = unpack("V", strrev(substr($pps_wk, 92, 4)));
169 $time_1st = substr($pps_wk, 100, 8);
170 $time_2nd = substr($pps_wk, 108, 8);
171 $start_block = unpack("V", substr($pps_wk, 116, 4));
172 $size = unpack("V", substr($pps_wk, 120, 4));
173 // _data member will point to position in file!!
174 // OLE_PPS object is created with an empty children array!!
175 $this->_list
[] = new OLE_PPS(null, '', $type[''], $prev[''], $next[''],
176 $dir[''], OLE
::OLE2LocalDate($time_1st),
177 OLE
::OLE2LocalDate($time_2nd),
178 ($start_block[''] +
1) * $big_block_size, array());
180 $this->_list
[count($this->_list
) - 1]->Size
= $size[''];
181 // check if the PPS tree (starting from root) is complete
182 if ($this->_ppsTreeComplete(0)) {
185 $pointer +
= OLE_PPS_SIZE
;
190 * It checks whether the PPS tree is complete (all PPS's read)
191 * starting with the given PPS (not necessarily root)
194 * @param integer $index The index of the PPS from which we are checking
195 * @return boolean Whether the PPS tree for the given PPS is complete
197 function _ppsTreeComplete($index)
199 if ($this->_list
[$index]->NextPps
!= -1) {
200 if (!isset($this->_list
[$this->_list
[$index]->NextPps
])) {
204 return $this->_ppsTreeComplete($this->_list
[$index]->NextPps
);
207 if ($this->_list
[$index]->DirPps
!= -1) {
208 if (!isset($this->_list
[$this->_list
[$index]->DirPps
])) {
212 return $this->_ppsTreeComplete($this->_list
[$index]->DirPps
);
219 * Checks whether a PPS is a File PPS or not.
220 * If there is no PPS for the index given, it will return false.
223 * @param integer $index The index for the PPS
224 * @return bool true if it's a File PPS, false otherwise
226 function isFile($index)
228 if (isset($this->_list
[$index])) {
229 return ($this->_list
[$index]->Type
== OLE_PPS_TYPE_FILE
);
235 * Checks whether a PPS is a Root PPS or not.
236 * If there is no PPS for the index given, it will return false.
239 * @param integer $index The index for the PPS.
240 * @return bool true if it's a Root PPS, false otherwise
242 function isRoot($index)
244 if (isset($this->_list
[$index])) {
245 return ($this->_list
[$index]->Type
== OLE_PPS_TYPE_ROOT
);
251 * Gives the total number of PPS's found in the OLE container.
254 * @return integer The total number of PPS's found in the OLE container
258 return count($this->_list
);
262 * Gets data from a PPS
263 * If there is no PPS for the index given, it will return an empty string.
266 * @param integer $index The index for the PPS
267 * @param integer $position The position from which to start reading
268 * (relative to the PPS)
269 * @param integer $length The amount of bytes to read (at most)
270 * @return string The binary string containing the data requested
272 function getData($index, $position, $length)
274 // if position is not valid return empty string
275 if (!isset($this->_list
[$index]) or ($position >= $this->_list
[$index]->Size
) or ($position < 0)) {
278 // Beware!!! _data member is actually a position
279 fseek($this->_file_handle
, $this->_list
[$index]->_data +
$position);
280 return fread($this->_file_handle
, $length);
284 * Gets the data length from a PPS
285 * If there is no PPS for the index given, it will return 0.
288 * @param integer $index The index for the PPS
289 * @return integer The amount of bytes in data the PPS has
291 function getDataLength($index)
293 if (isset($this->_list
[$index])) {
294 return $this->_list
[$index]->Size
;
300 * Utility function to transform ASCII text to Unicode
304 * @param string $ascii The ASCII string to transform
305 * @return string The string in Unicode
307 function Asc2Ucs($ascii)
310 for ($i = 0; $i < strlen($ascii); $i++
) {
311 $rawname .= $ascii{$i}."\x00";
318 * Returns a string for the OLE container with the date given
322 * @param integer $date A timestamp
323 * @return string The string for the OLE container
325 function LocalDate2OLE($date = null)
328 return "\x00\x00\x00\x00\x00\x00\x00\x00";
331 // factor used for separating numbers into 4 bytes parts
334 // days from 1-1-1601 until the beggining of UNIX era
337 $big_date = $days*24*3600 +
gmmktime(date("H",$date),date("i",$date),date("s",$date),
338 date("m",$date),date("d",$date),date("Y",$date));
339 // multiply just to make MS happy
340 $big_date *= 10000000;
342 $high_part = floor($big_date/$factor);
344 $low_part = floor((($big_date/$factor) - $high_part)*$factor);
349 for ($i=0; $i<4; $i++
)
351 $hex = $low_part %
0x100;
352 $res .= pack('c', $hex);
355 for ($i=0; $i<4; $i++
)
357 $hex = $high_part %
0x100;
358 $res .= pack('c', $hex);
365 * Returns a timestamp from an OLE container's date
369 * @param integer $string A binary string with the encoded date
370 * @return string The timestamp corresponding to the string
372 function OLE2LocalDate($string)
374 if (strlen($string) != 8) {
375 return new PEAR_Error("Expecting 8 byte string");
378 // factor used for separating numbers into 4 bytes parts
381 for ($i=0; $i<4; $i++
)
383 $al = unpack('C', $string{(7 - $i)});
384 $high_part +
= $al[''];
390 for ($i=4; $i<8; $i++
)
392 $al = unpack('C', $string{(7 - $i)});
393 $low_part +
= $al[''];
398 $big_date = ($high_part*$factor) +
$low_part;
399 // translate to seconds
400 $big_date /= 10000000;
402 // days from 1-1-1601 until the beggining of UNIX era
405 // translate to seconds from beggining of UNIX era
406 $big_date -= $days*24*3600;
407 return floor($big_date);