ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
BIFFwriter.php
Go to the documentation of this file.
1 <?php
28 // Original file header of PEAR::Spreadsheet_Excel_Writer_BIFFwriter (used as the base for this class):
29 // -----------------------------------------------------------------------------------------
30 // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
31 // *
32 // * The majority of this is _NOT_ my code. I simply ported it from the
33 // * PERL Spreadsheet::WriteExcel module.
34 // *
35 // * The author of the Spreadsheet::WriteExcel module is John McNamara
36 // * <jmcnamara@cpan.org>
37 // *
38 // * I _DO_ maintain this code, and John McNamara has nothing to do with the
39 // * porting of this code to PHP. Any questions directly related to this
40 // * class library should be directed to me.
41 // *
42 // * License Information:
43 // *
44 // * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
45 // * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
46 // *
47 // * This library is free software; you can redistribute it and/or
48 // * modify it under the terms of the GNU Lesser General Public
49 // * License as published by the Free Software Foundation; either
50 // * version 2.1 of the License, or (at your option) any later version.
51 // *
52 // * This library is distributed in the hope that it will be useful,
53 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55 // * Lesser General Public License for more details.
56 // *
57 // * You should have received a copy of the GNU Lesser General Public
58 // * License along with this library; if not, write to the Free Software
59 // * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
60 // */
61 
62 
71 {
76  private static $_byte_order;
77 
82  public $_data;
83 
88  public $_datasize;
89 
95  public $_limit = 8224;
96 
100  public function __construct()
101  {
102  $this->_data = '';
103  $this->_datasize = 0;
104 // $this->_limit = 8224;
105  }
106 
113  public static function getByteOrder()
114  {
115  if (!isset(self::$_byte_order)) {
116  // Check if "pack" gives the required IEEE 64bit float
117  $teststr = pack("d", 1.2345);
118  $number = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
119  if ($number == $teststr) {
120  $byte_order = 0; // Little Endian
121  } elseif ($number == strrev($teststr)){
122  $byte_order = 1; // Big Endian
123  } else {
124  // Give up. I'll fix this in a later version.
125  throw new PHPExcel_Writer_Exception("Required floating point format not supported on this platform.");
126  }
127  self::$_byte_order = $byte_order;
128  }
129 
130  return self::$_byte_order;
131  }
132 
139  function _append($data)
140  {
141  if (strlen($data) - 4 > $this->_limit) {
142  $data = $this->_addContinue($data);
143  }
144  $this->_data .= $data;
145  $this->_datasize += strlen($data);
146  }
147 
154  public function writeData($data)
155  {
156  if (strlen($data) - 4 > $this->_limit) {
157  $data = $this->_addContinue($data);
158  }
159  $this->_datasize += strlen($data);
160 
161  return $data;
162  }
163 
172  function _storeBof($type)
173  {
174  $record = 0x0809; // Record identifier (BIFF5-BIFF8)
175  $length = 0x0010;
176 
177  // by inspection of real files, MS Office Excel 2007 writes the following
178  $unknown = pack("VV", 0x000100D1, 0x00000406);
179 
180  $build = 0x0DBB; // Excel 97
181  $year = 0x07CC; // Excel 97
182 
183  $version = 0x0600; // BIFF8
184 
185  $header = pack("vv", $record, $length);
186  $data = pack("vvvv", $version, $type, $build, $year);
187  $this->_append($header . $data . $unknown);
188  }
189 
195  function _storeEof()
196  {
197  $record = 0x000A; // Record identifier
198  $length = 0x0000; // Number of bytes to follow
199 
200  $header = pack("vv", $record, $length);
201  $this->_append($header);
202  }
203 
209  public function writeEof()
210  {
211  $record = 0x000A; // Record identifier
212  $length = 0x0000; // Number of bytes to follow
213  $header = pack("vv", $record, $length);
214  return $this->writeData($header);
215  }
216 
229  function _addContinue($data)
230  {
231  $limit = $this->_limit;
232  $record = 0x003C; // Record identifier
233 
234  // The first 2080/8224 bytes remain intact. However, we have to change
235  // the length field of the record.
236  $tmp = substr($data, 0, 2) . pack("v", $limit) . substr($data, 4, $limit);
237 
238  $header = pack("vv", $record, $limit); // Headers for continue records
239 
240  // Retrieve chunks of 2080/8224 bytes +4 for the header.
241  $data_length = strlen($data);
242  for ($i = $limit + 4; $i < ($data_length - $limit); $i += $limit) {
243  $tmp .= $header;
244  $tmp .= substr($data, $i, $limit);
245  }
246 
247  // Retrieve the last chunk of data
248  $header = pack("vv", $record, strlen($data) - $i);
249  $tmp .= $header;
250  $tmp .= substr($data, $i, strlen($data) - $i);
251 
252  return $tmp;
253  }
254 
255 }
_append($data)
General storage function.
Definition: BIFFwriter.php:139
_addContinue($data)
Excel limits the size of BIFF records.
Definition: BIFFwriter.php:229
_storeBof($type)
Writes Excel BOF record to indicate the beginning of a stream or sub-stream in the BIFF file...
Definition: BIFFwriter.php:172
writeData($data)
General storage function like _append, but returns string instead of modifying $this->_data.
Definition: BIFFwriter.php:154
writeEof()
Writes Excel EOF record to indicate the end of a BIFF stream.
Definition: BIFFwriter.php:209
static getByteOrder()
Determine the byte order and store it as class data to avoid recalculating it for each call to new()...
Definition: BIFFwriter.php:113
$header
_storeEof()
Writes Excel EOF record to indicate the end of a BIFF stream.
Definition: BIFFwriter.php:195