ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
BIFFwriter.php
Go to the documentation of this file.
1 <?php
2 /*
3 * Module written/ported by Xavier Noguer <xnoguer@php.net>
4 *
5 * The majority of this is _NOT_ my code. I simply ported it from the
6 * PERL Spreadsheet::WriteExcel module.
7 *
8 * The author of the Spreadsheet::WriteExcel module is John McNamara
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
18 * Copyright (c) 2002-2003 Xavier Noguer xnoguer@php.net
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 
35 require_once('PEAR.php');
36 
55 {
60  var $_BIFF_version = 0x0500;
61 
67 
72  var $_data;
73 
79 
85  var $_limit;
86 
93  {
94  $this->_byte_order = '';
95  $this->_data = '';
96  $this->_datasize = 0;
97  $this->_limit = 2080;
98  // Set the byte order
99  $this->_setByteOrder();
100  }
101 
108  function _setByteOrder()
109  {
110  // Check if "pack" gives the required IEEE 64bit float
111  $teststr = pack("d", 1.2345);
112  $number = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
113  if ($number == $teststr) {
114  $byte_order = 0; // Little Endian
115  }
116  elseif ($number == strrev($teststr)){
117  $byte_order = 1; // Big Endian
118  }
119  else {
120  // Give up. I'll fix this in a later version.
121  return $this->raiseError("Required floating point format ".
122  "not supported on this platform.");
123  }
124  $this->_byte_order = $byte_order;
125  }
126 
133  function _prepend($data)
134  {
135  if (strlen($data) > $this->_limit) {
136  $data = $this->_addContinue($data);
137  }
138  $this->_data = $data.$this->_data;
139  $this->_datasize += strlen($data);
140  }
141 
148  function _append($data)
149  {
150  if (strlen($data) > $this->_limit) {
151  $data = $this->_addContinue($data);
152  }
153  $this->_data = $this->_data.$data;
154  $this->_datasize += strlen($data);
155  }
156 
165  function _storeBof($type)
166  {
167  $record = 0x0809; // Record identifier
168 
169  // According to the SDK $build and $year should be set to zero.
170  // However, this throws a warning in Excel 5. So, use magic numbers.
171  if ($this->_BIFF_version == 0x0500) {
172  $length = 0x0008;
173  $unknown = '';
174  $build = 0x096C;
175  $year = 0x07C9;
176  }
177  elseif ($this->_BIFF_version == 0x0600) {
178  $length = 0x0010;
179  $unknown = pack("VV", 0x00000041, 0x00000006); //unknown last 8 bytes for BIFF8
180  $build = 0x0DBB;
181  $year = 0x07CC;
182  }
183  $version = $this->_BIFF_version;
184 
185  $header = pack("vv", $record, $length);
186  $data = pack("vvvv", $version, $type, $build, $year);
187  $this->_prepend($header.$data.$unknown);
188  }
189 
195  function _storeEof()
196  {
197  $record = 0x000A; // Record identifier
198  $length = 0x0000; // Number of bytes to follow
199  $header = pack("vv", $record, $length);
200  $this->_append($header);
201  }
202 
215  function _addContinue($data)
216  {
217  $limit = $this->_limit;
218  $record = 0x003C; // Record identifier
219 
220  // The first 2080/8224 bytes remain intact. However, we have to change
221  // the length field of the record.
222  $tmp = substr($data, 0, 2).pack("v", $limit-4).substr($data, 4, $limit - 4);
223 
224  $header = pack("vv", $record, $limit); // Headers for continue records
225 
226  // Retrieve chunks of 2080/8224 bytes +4 for the header.
227  for($i = $limit; $i < strlen($data) - $limit; $i += $limit)
228  {
229  $tmp .= $header;
230  $tmp .= substr($data, $i, $limit);
231  }
232 
233  // Retrieve the last chunk of data
234  $header = pack("vv", $record, strlen($data) - $i);
235  $tmp .= $header;
236  $tmp .= substr($data,$i,strlen($data) - $i);
237 
238  return $tmp;
239  }
240 }
241 ?>