123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- <?php
- 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);
- $GLOBALS['_OLE_INSTANCES'] = array();
- class OLE extends PEAR
- {
-
- var $_file_handle;
-
- var $_list;
-
- var $root;
-
- var $bbat;
-
- var $sbat;
-
- var $bigBlockSize;
-
- var $smallBlockSize;
-
- function OLE()
- {
- $this->_list = array();
- }
-
- function _OLE()
- {
- fclose($this->_file_handle);
- }
-
- function read($file)
- {
- $fh = @fopen($file, "r");
- if (!$fh) {
- return $this->raiseError("Can't open file $file");
- }
- $this->_file_handle = $fh;
- $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, 28);
- if (fread($fh, 2) != "\xFE\xFF") {
-
- return $this->raiseError("Only Little-Endian encoding is supported.");
- }
-
- $this->bigBlockSize = pow(2, $this->_readInt2($fh));
- $this->smallBlockSize = pow(2, $this->_readInt2($fh));
-
- fseek($fh, 44);
-
- $bbatBlockCount = $this->_readInt4($fh);
-
- $directoryFirstBlockId = $this->_readInt4($fh);
-
- fseek($fh, 56);
-
- $this->bigBlockThreshold = $this->_readInt4($fh);
-
- $sbatFirstBlockId = $this->_readInt4($fh);
-
- $sbbatBlockCount = $this->_readInt4($fh);
-
- $mbatFirstBlockId = $this->_readInt4($fh);
-
- $mbbatBlockCount = $this->_readInt4($fh);
- $this->bbat = array();
-
-
- $mbatBlocks = array();
- for ($i = 0; $i < 109; $i++) {
- $mbatBlocks[] = $this->_readInt4($fh);
- }
-
- $pos = $this->_getBlockOffset($mbatFirstBlockId);
- for ($i = 0; $i < $mbbatBlockCount; $i++) {
- fseek($fh, $pos);
- for ($j = 0; $j < $this->bigBlockSize / 4 - 1; $j++) {
- $mbatBlocks[] = $this->_readInt4($fh);
- }
-
- $pos = $this->_getBlockOffset($this->_readInt4($fh));
- }
-
-
- for ($i = 0; $i < $bbatBlockCount; $i++) {
- $pos = $this->_getBlockOffset($mbatBlocks[$i]);
- fseek($fh, $pos);
- for ($j = 0 ; $j < $this->bigBlockSize / 4; $j++) {
- $this->bbat[] = $this->_readInt4($fh);
- }
- }
-
- $this->sbat = array();
- $shortBlockCount = $sbbatBlockCount * $this->bigBlockSize / 4;
- $sbatFh = $this->getStream($sbatFirstBlockId);
- for ($blockId = 0; $blockId < $shortBlockCount; $blockId++) {
- $this->sbat[$blockId] = $this->_readInt4($sbatFh);
- }
- fclose($sbatFh);
- $this->_readPpsWks($directoryFirstBlockId);
- return true;
- }
-
- function _getBlockOffset($blockId)
- {
- return 512 + $blockId * $this->bigBlockSize;
- }
-
- function getStream($blockIdOrPps)
- {
- include_once 'OLE/ChainedBlockStream.php';
- static $isRegistered = false;
- if (!$isRegistered) {
- stream_wrapper_register('ole-chainedblockstream',
- 'OLE_ChainedBlockStream');
- $isRegistered = true;
- }
-
-
-
- $GLOBALS['_OLE_INSTANCES'][] = $this;
- $instanceId = end(array_keys($GLOBALS['_OLE_INSTANCES']));
- $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId;
- if (is_a($blockIdOrPps, 'OLE_PPS')) {
- $path .= '&blockId=' . $blockIdOrPps->_StartBlock;
- $path .= '&size=' . $blockIdOrPps->Size;
- } else {
- $path .= '&blockId=' . $blockIdOrPps;
- }
- return fopen($path, 'r');
- }
-
- function _readInt1($fh)
- {
- list(, $tmp) = unpack("c", fread($fh, 1));
- return $tmp;
- }
-
- function _readInt2($fh)
- {
- list(, $tmp) = unpack("v", fread($fh, 2));
- return $tmp;
- }
-
- function _readInt4($fh)
- {
- list(, $tmp) = unpack("V", fread($fh, 4));
- return $tmp;
- }
-
- function _readPpsWks($blockId)
- {
- $fh = $this->getStream($blockId);
- for ($pos = 0; ; $pos += 128) {
- fseek($fh, $pos, SEEK_SET);
- $nameUtf16 = fread($fh, 64);
- $nameLength = $this->_readInt2($fh);
- $nameUtf16 = substr($nameUtf16, 0, $nameLength - 2);
-
- $name = str_replace("\x00", "", $nameUtf16);
- $type = $this->_readInt1($fh);
- switch ($type) {
- case OLE_PPS_TYPE_ROOT:
- require_once 'OLE/PPS/Root.php';
- $pps = new OLE_PPS_Root(null, null, array());
- $this->root = $pps;
- break;
- case OLE_PPS_TYPE_DIR:
- $pps = new OLE_PPS(null, null, null, null, null,
- null, null, null, null, array());
- break;
- case OLE_PPS_TYPE_FILE:
- require_once 'OLE/PPS/File.php';
- $pps = new OLE_PPS_File($name);
- break;
- default:
- continue;
- }
- fseek($fh, 1, SEEK_CUR);
- $pps->Type = $type;
- $pps->Name = $name;
- $pps->PrevPps = $this->_readInt4($fh);
- $pps->NextPps = $this->_readInt4($fh);
- $pps->DirPps = $this->_readInt4($fh);
- fseek($fh, 20, SEEK_CUR);
- $pps->Time1st = OLE::OLE2LocalDate(fread($fh, 8));
- $pps->Time2nd = OLE::OLE2LocalDate(fread($fh, 8));
- $pps->_StartBlock = $this->_readInt4($fh);
- $pps->Size = $this->_readInt4($fh);
- $pps->No = count($this->_list);
- $this->_list[] = $pps;
-
- if (isset($this->root) &&
- $this->_ppsTreeComplete($this->root->No)) {
- break;
- }
- }
- fclose($fh);
-
- foreach ($this->_list as $pps) {
- if ($pps->Type == OLE_PPS_TYPE_DIR || $pps->Type == OLE_PPS_TYPE_ROOT) {
- $nos = array($pps->DirPps);
- $pps->children = array();
- while ($nos) {
- $no = array_pop($nos);
- if ($no != -1) {
- $childPps = $this->_list[$no];
- $nos[] = $childPps->PrevPps;
- $nos[] = $childPps->NextPps;
- $pps->children[] = $childPps;
- }
- }
- }
- }
- return true;
- }
-
- function _ppsTreeComplete($index)
- {
- return isset($this->_list[$index]) &&
- ($pps = $this->_list[$index]) &&
- ($pps->PrevPps == -1 ||
- $this->_ppsTreeComplete($pps->PrevPps)) &&
- ($pps->NextPps == -1 ||
- $this->_ppsTreeComplete($pps->NextPps)) &&
- ($pps->DirPps == -1 ||
- $this->_ppsTreeComplete($pps->DirPps));
- }
-
- function isFile($index)
- {
- if (isset($this->_list[$index])) {
- return ($this->_list[$index]->Type == OLE_PPS_TYPE_FILE);
- }
- return false;
- }
-
- function isRoot($index)
- {
- if (isset($this->_list[$index])) {
- return ($this->_list[$index]->Type == OLE_PPS_TYPE_ROOT);
- }
- return false;
- }
-
- function ppsTotal()
- {
- return count($this->_list);
- }
-
- function getData($index, $position, $length)
- {
-
- if (!isset($this->_list[$index]) ||
- $position >= $this->_list[$index]->Size ||
- $position < 0) {
- return '';
- }
- $fh = $this->getStream($this->_list[$index]);
- $data = stream_get_contents($fh, $length, $position);
- fclose($fh);
- return $data;
- }
-
- function getDataLength($index)
- {
- if (isset($this->_list[$index])) {
- return $this->_list[$index]->Size;
- }
- return 0;
- }
-
- function Asc2Ucs($ascii)
- {
- $rawname = '';
- for ($i = 0; $i < strlen($ascii); $i++) {
- $rawname .= $ascii{$i} . "\x00";
- }
- return $rawname;
- }
-
- function LocalDate2OLE($date = null)
- {
- if (!isset($date)) {
- return "\x00\x00\x00\x00\x00\x00\x00\x00";
- }
-
- $factor = pow(2, 32);
-
- $days = 134774;
-
- $big_date = $days * 24 * 3600 +
- gmmktime(date("H",$date),date("i",$date),date("s",$date),
- date("m",$date),date("d",$date),date("Y",$date));
-
- $big_date *= 10000000;
- $high_part = floor($big_date / $factor);
-
- $low_part = floor((($big_date / $factor) - $high_part) * $factor);
-
- $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;
- }
-
- function OLE2LocalDate($string)
- {
- if (strlen($string) != 8) {
- return new PEAR_Error("Expecting 8 byte string");
- }
-
- $factor = pow(2,32);
- $high_part = 0;
- for ($i = 0; $i < 4; $i++) {
- list(, $high_part) = unpack('C', $string{(7 - $i)});
- if ($i < 3) {
- $high_part *= 0x100;
- }
- }
- $low_part = 0;
- for ($i = 4; $i < 8; $i++) {
- list(, $low_part) = unpack('C', $string{(7 - $i)});
- if ($i < 7) {
- $low_part *= 0x100;
- }
- }
- $big_date = ($high_part * $factor) + $low_part;
-
- $big_date /= 10000000;
-
- $days = 134774;
-
- $big_date -= $days * 24 * 3600;
- return floor($big_date);
- }
- }
- ?>
|