Subversion Repositories eFlore/Applications.cel

Rev

Rev 970 | Rev 1605 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
418 aurelien 1
<?php
2
/*
1604 raphael 3
*  Module written/ported by Xavier Noguer <xnoguer@php.net>
418 aurelien 4
*
5
*  The majority of this is _NOT_ my code.  I simply ported it from the
6
*  PERL Spreadsheet::WriteExcel module.
7
*
1604 raphael 8
*  The author of the Spreadsheet::WriteExcel module is John McNamara
418 aurelien 9
*  <jmcnamara@cpan.org>
10
*
11
*  I _DO_ maintain this code, and John McNamara has nothing to do with the
12
*  porting of this code to PHP.  Any questions directly related to this
13
*  class library should be directed to me.
14
*
15
*  License Information:
16
*
17
*    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
1604 raphael 18
*    Copyright (c) 2002-2003 Xavier Noguer xnoguer@php.net
418 aurelien 19
*
20
*    This library is free software; you can redistribute it and/or
21
*    modify it under the terms of the GNU Lesser General Public
22
*    License as published by the Free Software Foundation; either
23
*    version 2.1 of the License, or (at your option) any later version.
24
*
25
*    This library is distributed in the hope that it will be useful,
26
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
27
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28
*    Lesser General Public License for more details.
29
*
30
*    You should have received a copy of the GNU Lesser General Public
31
*    License along with this library; if not, write to the Free Software
32
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33
*/
34
 
1604 raphael 35
require_once 'PEAR.php';
418 aurelien 36
 
37
/**
38
* Class for writing Excel BIFF records.
1604 raphael 39
*
418 aurelien 40
* From "MICROSOFT EXCEL BINARY FILE FORMAT" by Mark O'Brien (Microsoft Corporation):
41
*
1604 raphael 42
* BIFF (BInary File Format) is the file format in which Excel documents are
418 aurelien 43
* saved on disk.  A BIFF file is a complete description of an Excel document.
1604 raphael 44
* BIFF files consist of sequences of variable-length records. There are many
45
* different types of BIFF records.  For example, one record type describes a
46
* formula entered into a cell; one describes the size and location of a
418 aurelien 47
* window into a document; another describes a picture format.
48
*
1604 raphael 49
* @author   Xavier Noguer <xnoguer@php.net>
418 aurelien 50
* @category FileFormats
51
* @package  Spreadsheet_Excel_Writer
52
*/
53
 
54
class Spreadsheet_Excel_Writer_BIFFwriter extends PEAR
55
{
56
    /**
1604 raphael 57
    * The BIFF/Excel version (5).
418 aurelien 58
    * @var integer
59
    */
60
    var $_BIFF_version = 0x0500;
61
 
62
    /**
1604 raphael 63
    * The byte order of this architecture. 0 => little endian, 1 => big endian
418 aurelien 64
    * @var integer
65
    */
66
    var $_byte_order;
67
 
68
    /**
69
    * The string containing the data of the BIFF stream
70
    * @var string
71
    */
72
    var $_data;
73
 
74
    /**
75
    * The size of the data in bytes. Should be the same as strlen($this->_data)
76
    * @var integer
77
    */
78
    var $_datasize;
79
 
80
    /**
81
    * The maximun length for a BIFF record. See _addContinue()
82
    * @var integer
83
    * @see _addContinue()
84
    */
85
    var $_limit;
1604 raphael 86
 
418 aurelien 87
    /**
1604 raphael 88
    * The temporary dir for storing the OLE file
89
    * @var string
90
    */
91
    var $_tmp_dir;
92
 
93
    /**
94
    * The temporary file for storing the OLE file
95
    * @var string
96
    */
97
    var $_tmp_file;
98
 
99
    /**
418 aurelien 100
    * Constructor
101
    *
102
    * @access public
103
    */
104
    function Spreadsheet_Excel_Writer_BIFFwriter()
105
    {
106
        $this->_byte_order = '';
107
        $this->_data       = '';
108
        $this->_datasize   = 0;
1604 raphael 109
        $this->_limit      = 2080;
110
        $this->_tmp_dir    = '';
418 aurelien 111
        // Set the byte order
112
        $this->_setByteOrder();
113
    }
114
 
1604 raphael 115
    /**
116
    * Determine the byte order and store it as class data to avoid
117
    * recalculating it for each call to new().
118
    *
119
    * @access private
120
    */
418 aurelien 121
    function _setByteOrder()
122
    {
1604 raphael 123
        // Check if "pack" gives the required IEEE 64bit float
124
        $teststr = pack("d", 1.2345);
125
        $number  = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
126
        if ($number == $teststr) {
127
            $byte_order = 0;    // Little Endian
128
        } elseif ($number == strrev($teststr)){
129
            $byte_order = 1;    // Big Endian
130
        } else {
131
            // Give up. I'll fix this in a later version.
132
            return $this->raiseError("Required floating point format ".
133
                                     "not supported on this platform.");
418 aurelien 134
        }
135
        $this->_byte_order = $byte_order;
136
    }
137
 
1604 raphael 138
    /**
139
    * General storage function
140
    *
141
    * @param string $data binary data to prepend
142
    * @access private
143
    */
418 aurelien 144
    function _prepend($data)
145
    {
146
        if (strlen($data) > $this->_limit) {
147
            $data = $this->_addContinue($data);
148
        }
149
        $this->_data      = $data.$this->_data;
150
        $this->_datasize += strlen($data);
151
    }
152
 
1604 raphael 153
    /**
154
    * General storage function
155
    *
156
    * @param string $data binary data to append
157
    * @access private
158
    */
418 aurelien 159
    function _append($data)
160
    {
161
        if (strlen($data) > $this->_limit) {
162
            $data = $this->_addContinue($data);
163
        }
164
        $this->_data      = $this->_data.$data;
165
        $this->_datasize += strlen($data);
166
    }
167
 
1604 raphael 168
    /**
169
    * Writes Excel BOF record to indicate the beginning of a stream or
170
    * sub-stream in the BIFF file.
171
    *
172
    * @param  integer $type Type of BIFF file to write: 0x0005 Workbook,
173
    *                       0x0010 Worksheet.
174
    * @access private
175
    */
418 aurelien 176
    function _storeBof($type)
177
    {
178
        $record  = 0x0809;        // Record identifier
1604 raphael 179
 
180
        // According to the SDK $build and $year should be set to zero.
181
        // However, this throws a warning in Excel 5. So, use magic numbers.
182
        if ($this->_BIFF_version == 0x0500) {
183
            $length  = 0x0008;
184
            $unknown = '';
185
            $build   = 0x096C;
186
            $year    = 0x07C9;
187
        } elseif ($this->_BIFF_version == 0x0600) {
188
            $length  = 0x0010;
189
            $unknown = pack("VV", 0x00000041, 0x00000006); //unknown last 8 bytes for BIFF8
190
            $build   = 0x0DBB;
191
            $year    = 0x07CC;
192
        }
418 aurelien 193
        $version = $this->_BIFF_version;
1604 raphael 194
 
418 aurelien 195
        $header  = pack("vv",   $record, $length);
196
        $data    = pack("vvvv", $version, $type, $build, $year);
1604 raphael 197
        $this->_prepend($header . $data . $unknown);
418 aurelien 198
    }
199
 
1604 raphael 200
    /**
201
    * Writes Excel EOF record to indicate the end of a BIFF stream.
202
    *
203
    * @access private
204
    */
205
    function _storeEof()
418 aurelien 206
    {
207
        $record    = 0x000A;   // Record identifier
208
        $length    = 0x0000;   // Number of bytes to follow
209
        $header    = pack("vv", $record, $length);
210
        $this->_append($header);
211
    }
212
 
1604 raphael 213
    /**
214
    * Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
215
    * Excel 97 the limit is 8228 bytes. Records that are longer than these limits
216
    * must be split up into CONTINUE blocks.
217
    *
218
    * This function takes a long BIFF record and inserts CONTINUE records as
219
    * necessary.
220
    *
221
    * @param  string  $data The original binary data to be written
222
    * @return string        A very convenient string of continue blocks
223
    * @access private
224
    */
418 aurelien 225
    function _addContinue($data)
226
    {
1604 raphael 227
        $limit  = $this->_limit;
228
        $record = 0x003C;         // Record identifier
229
 
418 aurelien 230
        // The first 2080/8224 bytes remain intact. However, we have to change
231
        // the length field of the record.
232
        $tmp = substr($data, 0, 2).pack("v", $limit-4).substr($data, 4, $limit - 4);
1604 raphael 233
 
418 aurelien 234
        $header = pack("vv", $record, $limit);  // Headers for continue records
1604 raphael 235
 
418 aurelien 236
        // Retrieve chunks of 2080/8224 bytes +4 for the header.
1604 raphael 237
        $data_length = strlen($data);
238
        for ($i = $limit; $i <  ($data_length - $limit); $i += $limit) {
418 aurelien 239
            $tmp .= $header;
240
            $tmp .= substr($data, $i, $limit);
241
        }
1604 raphael 242
 
418 aurelien 243
        // Retrieve the last chunk of data
244
        $header  = pack("vv", $record, strlen($data) - $i);
245
        $tmp    .= $header;
1604 raphael 246
        $tmp    .= substr($data, $i, strlen($data) - $i);
247
 
248
        return $tmp;
418 aurelien 249
    }
1604 raphael 250
 
251
    /**
252
    * Sets the temp dir used for storing the OLE file
253
    *
254
    * @access public
255
    * @param string $dir The dir to be used as temp dir
256
    * @return true if given dir is valid, false otherwise
257
    */
258
    function setTempDir($dir)
259
    {
260
        if (is_dir($dir)) {
261
            $this->_tmp_dir = $dir;
262
            return true;
263
        }
264
        return false;
265
    }
418 aurelien 266
}
267
?>