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
// +----------------------------------------------------------------------+
20
// $Id: OLE.php,v 1.7 2003/08/21 15:15:40 xnoguer Exp $
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
if(!class_exists('OLE_PPS')){
34
require_once 'OLE/PPS.php';
36
if(!class_exists('OLE')){
40
* OLE package base class.
42
* @author Xavier Noguer <xnoguer@php.net>
43
* @category Structures
46
class OLE extends PEAR
49
* The file handle for reading an OLE container
55
* Array of PPS's found on the OLE container
61
* Creates a new OLE object
62
* Remember to use ampersand when creating an OLE object ($my_ole =& new OLE();)
67
$this->_list = array();
71
* Reads an OLE container from the contents of the file given.
75
* @return mixed true on success, PEAR_Error on failure
79
/* consider storing offsets as constants */
80
$big_block_size_offset = 30;
82
$bd_start_offset = 68;
84
$fh = @fopen($file, "r");
86
return $this->raiseError("Can't open file $file");
88
$this->_file_handle = $fh;
90
/* begin reading OLE attributes */
92
$signature = fread($fh, 8);
93
if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) {
94
return $this->raiseError("File doesn't seem to be an OLE container.");
96
fseek($fh, $big_block_size_offset);
97
$packed_array = unpack("v", fread($fh, 2));
98
$big_block_size = pow(2, $packed_array['']);
100
$packed_array = unpack("v", fread($fh, 2));
101
$small_block_size = pow(2, $packed_array['']);
102
$i1stBdL = ($big_block_size - 0x4C) / OLE_LONG_INT_SIZE;
104
fseek($fh, $iBdbCnt_offset);
105
$packed_array = unpack("V", fread($fh, 4));
106
$iBdbCnt = $packed_array[''];
108
$packed_array = unpack("V", fread($fh, 4));
109
$pps_wk_start = $packed_array[''];
111
fseek($fh, $bd_start_offset);
112
$packed_array = unpack("V", fread($fh, 4));
113
$bd_start = $packed_array[''];
114
$packed_array = unpack("V", fread($fh, 4));
115
$bd_count = $packed_array[''];
116
$packed_array = unpack("V", fread($fh, 4));
117
$iAll = $packed_array['']; // this may be wrong
118
/* create OLE_PPS objects from */
119
$ret = $this->_readPpsWks($pps_wk_start, $big_block_size);
120
if (PEAR::isError($ret)) {
127
* Destructor (using PEAR)
128
* Just closes the file handle on the OLE file.
134
fclose($this->_file_handle);
138
* Gets information about all PPS's on the OLE container from the PPS WK's
139
* creates an OLE_PPS object for each one.
142
* @param integer $pps_wk_start Position inside the OLE file where PPS WK's start
143
* @param integer $big_block_size Size of big blobks in the OLE file
144
* @return mixed true on success, PEAR_Error on failure
146
function _readPpsWks($pps_wk_start, $big_block_size)
148
$pointer = ($pps_wk_start + 1) * $big_block_size;
151
fseek($this->_file_handle, $pointer);
152
$pps_wk = fread($this->_file_handle, OLE_PPS_SIZE);
153
if (strlen($pps_wk) != OLE_PPS_SIZE) {
154
break; // Excel likes to add a trailing byte sometimes
155
//return $this->raiseError("PPS at $pointer seems too short: ".strlen($pps_wk));
157
$name_length = unpack("c", substr($pps_wk, 64, 2)); // FIXME (2 bytes??)
158
$name_length = $name_length[''] - 2;
159
$name = substr($pps_wk, 0, $name_length);
160
$type = unpack("c", substr($pps_wk, 66, 1));
161
if (($type[''] != OLE_PPS_TYPE_ROOT) and
162
($type[''] != OLE_PPS_TYPE_DIR) and
163
($type[''] != OLE_PPS_TYPE_FILE))
165
return $this->raiseError("PPS at $pointer has unknown type: {$type['']}");
167
$prev = unpack("V", substr($pps_wk, 68, 4));
168
$next = unpack("V", substr($pps_wk, 72, 4));
169
$dir = unpack("V", substr($pps_wk, 76, 4));
170
// there is no magic number, it can take different values.
171
//$magic = unpack("V", strrev(substr($pps_wk, 92, 4)));
172
$time_1st = substr($pps_wk, 100, 8);
173
$time_2nd = substr($pps_wk, 108, 8);
174
$start_block = unpack("V", substr($pps_wk, 116, 4));
175
$size = unpack("V", substr($pps_wk, 120, 4));
176
// _data member will point to position in file!!
177
// OLE_PPS object is created with an empty children array!!
178
$this->_list[] = new OLE_PPS(null, '', $type[''], $prev[''], $next[''],
179
$dir[''], OLE::OLE2LocalDate($time_1st),
180
OLE::OLE2LocalDate($time_2nd),
181
($start_block[''] + 1) * $big_block_size, array());
183
$this->_list[count($this->_list) - 1]->Size = $size[''];
184
// check if the PPS tree (starting from root) is complete
185
if ($this->_ppsTreeComplete(0)) {
188
$pointer += OLE_PPS_SIZE;
193
* It checks whether the PPS tree is complete (all PPS's read)
194
* starting with the given PPS (not necessarily root)
197
* @param integer $index The index of the PPS from which we are checking
198
* @return boolean Whether the PPS tree for the given PPS is complete
200
function _ppsTreeComplete($index)
202
if ($this->_list[$index]->NextPps != -1) {
203
if (!isset($this->_list[$this->_list[$index]->NextPps])) {
207
return $this->_ppsTreeComplete($this->_list[$index]->NextPps);
210
if ($this->_list[$index]->DirPps != -1) {
211
if (!isset($this->_list[$this->_list[$index]->DirPps])) {
215
return $this->_ppsTreeComplete($this->_list[$index]->DirPps);
222
* Checks whether a PPS is a File PPS or not.
223
* If there is no PPS for the index given, it will return false.
226
* @param integer $index The index for the PPS
227
* @return bool true if it's a File PPS, false otherwise
229
function isFile($index)
231
if (isset($this->_list[$index])) {
232
return ($this->_list[$index]->Type == OLE_PPS_TYPE_FILE);
238
* Checks whether a PPS is a Root PPS or not.
239
* If there is no PPS for the index given, it will return false.
242
* @param integer $index The index for the PPS.
243
* @return bool true if it's a Root PPS, false otherwise
245
function isRoot($index)
247
if (isset($this->_list[$index])) {
248
return ($this->_list[$index]->Type == OLE_PPS_TYPE_ROOT);
254
* Gives the total number of PPS's found in the OLE container.
257
* @return integer The total number of PPS's found in the OLE container
261
return count($this->_list);
265
* Gets data from a PPS
266
* If there is no PPS for the index given, it will return an empty string.
269
* @param integer $index The index for the PPS
270
* @param integer $position The position from which to start reading
271
* (relative to the PPS)
272
* @param integer $length The amount of bytes to read (at most)
273
* @return string The binary string containing the data requested
275
function getData($index, $position, $length)
277
// if position is not valid return empty string
278
if (!isset($this->_list[$index]) or ($position >= $this->_list[$index]->Size) or ($position < 0)) {
281
// Beware!!! _data member is actually a position
282
fseek($this->_file_handle, $this->_list[$index]->_data + $position);
283
return fread($this->_file_handle, $length);
287
* Gets the data length from a PPS
288
* If there is no PPS for the index given, it will return 0.
291
* @param integer $index The index for the PPS
292
* @return integer The amount of bytes in data the PPS has
294
function getDataLength($index)
296
if (isset($this->_list[$index])) {
297
return $this->_list[$index]->Size;
303
* Utility function to transform ASCII text to Unicode
307
* @param string $ascii The ASCII string to transform
308
* @return string The string in Unicode
310
function Asc2Ucs($ascii)
313
for ($i = 0; $i < strlen($ascii); $i++) {
314
$rawname .= $ascii{$i}."\x00";
321
* Returns a string for the OLE container with the date given
325
* @param integer $date A timestamp
326
* @return string The string for the OLE container
328
function LocalDate2OLE($date = null)
331
return "\x00\x00\x00\x00\x00\x00\x00\x00";
334
// factor used for separating numbers into 4 bytes parts
337
// days from 1-1-1601 until the beggining of UNIX era
340
$big_date = $days*24*3600 + gmmktime(date("H",$date),date("i",$date),date("s",$date),
341
date("m",$date),date("d",$date),date("Y",$date));
342
// multiply just to make MS happy
343
$big_date *= 10000000;
345
$high_part = floor($big_date/$factor);
347
$low_part = floor((($big_date/$factor) - $high_part)*$factor);
352
for ($i=0; $i<4; $i++)
354
$hex = $low_part % 0x100;
355
$res .= pack('c', $hex);
358
for ($i=0; $i<4; $i++)
360
$hex = $high_part % 0x100;
361
$res .= pack('c', $hex);
368
* Returns a timestamp from an OLE container's date
372
* @param integer $string A binary string with the encoded date
373
* @return string The timestamp corresponding to the string
375
function OLE2LocalDate($string)
377
if (strlen($string) != 8) {
378
return new PEAR_Error("Expecting 8 byte string");
381
// factor used for separating numbers into 4 bytes parts
384
for ($i=0; $i<4; $i++)
386
$al = unpack('C', $string{(7 - $i)});
387
$high_part += $al[''];
393
for ($i=4; $i<8; $i++)
395
$al = unpack('C', $string{(7 - $i)});
396
$low_part += $al[''];
401
$big_date = ($high_part*$factor) + $low_part;
402
// translate to seconds
403
$big_date /= 10000000;
405
// days from 1-1-1601 until the beggining of UNIX era
408
// translate to seconds from beggining of UNIX era
409
$big_date -= $days*24*3600;
410
return floor($big_date);