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: PPS.php,v 1.7 2007/02/13 21:00:42 schmidt Exp $
21
22
23require_once 'PEAR.php';
24require_once 'OLE.php';
25
26/**
27* Class for creating PPS's for OLE containers
28*
29* @author   Xavier Noguer <xnoguer@php.net>
30* @category Structures
31* @package  OLE
32*/
33class OLE_PPS extends PEAR
34{
35    /**
36    * The PPS index
37    * @var integer
38    */
39    var $No;
40
41    /**
42    * The PPS name (in Unicode)
43    * @var string
44    */
45    var $Name;
46
47    /**
48    * The PPS type. Dir, Root or File
49    * @var integer
50    */
51    var $Type;
52
53    /**
54    * The index of the previous PPS
55    * @var integer
56    */
57    var $PrevPps;
58
59    /**
60    * The index of the next PPS
61    * @var integer
62    */
63    var $NextPps;
64
65    /**
66    * The index of it's first child if this is a Dir or Root PPS
67    * @var integer
68    */
69    var $DirPps;
70
71    /**
72    * A timestamp
73    * @var integer
74    */
75    var $Time1st;
76
77    /**
78    * A timestamp
79    * @var integer
80    */
81    var $Time2nd;
82
83    /**
84    * Starting block (small or big) for this PPS's data  inside the container
85    * @var integer
86    */
87    var $_StartBlock;
88
89    /**
90    * The size of the PPS's data (in bytes)
91    * @var integer
92    */
93    var $Size;
94
95    /**
96    * The PPS's data (only used if it's not using a temporary file)
97    * @var string
98    */
99    var $_data;
100
101    /**
102    * Array of child PPS's (only used by Root and Dir PPS's)
103    * @var array
104    */
105    var $children = array();
106
107    /**
108    * Pointer to OLE container
109    * @var OLE
110    */
111    var $ole;
112
113    /**
114    * The constructor
115    *
116    * @access public
117    * @param integer $No   The PPS index
118    * @param string  $name The PPS name
119    * @param integer $type The PPS type. Dir, Root or File
120    * @param integer $prev The index of the previous PPS
121    * @param integer $next The index of the next PPS
122    * @param integer $dir  The index of it's first child if this is a Dir or Root PPS
123    * @param integer $time_1st A timestamp
124    * @param integer $time_2nd A timestamp
125    * @param string  $data  The (usually binary) source data of the PPS
126    * @param array   $children Array containing children PPS for this PPS
127    */
128    function __construct ($No, $name, $type, $prev, $next, $dir, $time_1st, $time_2nd, $data, $children)
129    {
130        $this->OLE_PPS ($No, $name, $type, $prev, $next, $dir, $time_1st, $time_2nd, $data, $children);
131    }
132
133    /**
134    * OLE_PPS
135    *
136    * @access public
137    * @param integer $No   The PPS index
138    * @param string  $name The PPS name
139    * @param integer $type The PPS type. Dir, Root or File
140    * @param integer $prev The index of the previous PPS
141    * @param integer $next The index of the next PPS
142    * @param integer $dir  The index of it's first child if this is a Dir or Root PPS
143    * @param integer $time_1st A timestamp
144    * @param integer $time_2nd A timestamp
145    * @param string  $data  The (usually binary) source data of the PPS
146    * @param array   $children Array containing children PPS for this PPS
147    */
148    function OLE_PPS($No, $name, $type, $prev, $next, $dir, $time_1st, $time_2nd, $data, $children)
149    {
150        $this->No      = $No;
151        $this->Name    = $name;
152        $this->Type    = $type;
153        $this->PrevPps = $prev;
154        $this->NextPps = $next;
155        $this->DirPps  = $dir;
156        $this->Time1st = $time_1st;
157        $this->Time2nd = $time_2nd;
158        $this->_data      = $data;
159        $this->children   = $children;
160        if ($data != '') {
161            $this->Size = strlen($data);
162        } else {
163            $this->Size = 0;
164        }
165    }
166
167    /**
168    * Returns the amount of data saved for this PPS
169    *
170    * @access private
171    * @return integer The amount of data (in bytes)
172    */
173    function _DataLen()
174    {
175        if (!isset($this->_data)) {
176            return 0;
177        }
178        if (isset($this->_PPS_FILE)) {
179            fseek($this->_PPS_FILE, 0);
180            $stats = fstat($this->_PPS_FILE);
181            return $stats[7];
182        } else {
183            return strlen($this->_data);
184        }
185    }
186
187    /**
188    * Returns a string with the PPS's WK (What is a WK?)
189    *
190    * @access private
191    * @return string The binary string
192    */
193    function _getPpsWk()
194    {
195        $ret = $this->Name;
196        for ($i = 0; $i < (64 - strlen($this->Name)); $i++) {
197            $ret .= "\x00";
198        }
199        $ret .= pack("v", strlen($this->Name) + 2)  // 66
200              . pack("c", $this->Type)              // 67
201              . pack("c", 0x00) //UK                // 68
202              . pack("V", $this->PrevPps) //Prev    // 72
203              . pack("V", $this->NextPps) //Next    // 76
204              . pack("V", $this->DirPps)  //Dir     // 80
205              . "\x00\x09\x02\x00"                  // 84
206              . "\x00\x00\x00\x00"                  // 88
207              . "\xc0\x00\x00\x00"                  // 92
208              . "\x00\x00\x00\x46"                  // 96 // Seems to be ok only for Root
209              . "\x00\x00\x00\x00"                  // 100
210              . OLE::LocalDate2OLE($this->Time1st)       // 108
211              . OLE::LocalDate2OLE($this->Time2nd)       // 116
212              . pack("V", isset($this->_StartBlock)?
213                        $this->_StartBlock:0)        // 120
214              . pack("V", $this->Size)               // 124
215              . pack("V", 0);                        // 128
216        return $ret;
217    }
218
219    /**
220    * Updates index and pointers to previous, next and children PPS's for this
221    * PPS. I don't think it'll work with Dir PPS's.
222    *
223    * @access private
224    * @param array &$pps_array Reference to the array of PPS's for the whole OLE
225    *                          container
226    * @return integer          The index for this PPS
227    */
228    function _savePpsSetPnt(&$pps_array)
229    {
230        $pps_array[count($pps_array)] = &$this;
231        $this->No = count($pps_array) - 1;
232        $this->PrevPps = 0xFFFFFFFF;
233        $this->NextPps = 0xFFFFFFFF;
234        if (count($this->children) > 0) {
235            $this->DirPps = $this->children[0]->_savePpsSetPnt($pps_array);
236        } else {
237            $this->DirPps = 0xFFFFFFFF;
238        }
239        return $this->No;
240    }
241}
242?>
243