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