ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
BIFFwriter.php
Go to the documentation of this file.
1<?php
2
4
6
7// Original file header of PEAR::Spreadsheet_Excel_Writer_BIFFwriter (used as the base for this class):
8// -----------------------------------------------------------------------------------------
9// * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
10// *
11// * The majority of this is _NOT_ my code. I simply ported it from the
12// * PERL Spreadsheet::WriteExcel module.
13// *
14// * The author of the Spreadsheet::WriteExcel module is John McNamara
15// * <jmcnamara@cpan.org>
16// *
17// * I _DO_ maintain this code, and John McNamara has nothing to do with the
18// * porting of this code to PHP. Any questions directly related to this
19// * class library should be directed to me.
20// *
21// * License Information:
22// *
23// * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
24// * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
25// *
26// * This library is free software; you can redistribute it and/or
27// * modify it under the terms of the GNU Lesser General Public
28// * License as published by the Free Software Foundation; either
29// * version 2.1 of the License, or (at your option) any later version.
30// *
31// * This library is distributed in the hope that it will be useful,
32// * but WITHOUT ANY WARRANTY; without even the implied warranty of
33// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34// * Lesser General Public License for more details.
35// *
36// * You should have received a copy of the GNU Lesser General Public
37// * License along with this library; if not, write to the Free Software
38// * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39// */
41{
47 private static $byteOrder;
48
54 public $_data;
55
61 public $_datasize;
62
70 private $limit = 8224;
71
75 public function __construct()
76 {
77 $this->_data = '';
78 $this->_datasize = 0;
79 }
80
87 public static function getByteOrder()
88 {
89 if (!isset(self::$byteOrder)) {
90 // Check if "pack" gives the required IEEE 64bit float
91 $teststr = pack('d', 1.2345);
92 $number = pack('C8', 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
93 if ($number == $teststr) {
94 $byte_order = 0; // Little Endian
95 } elseif ($number == strrev($teststr)) {
96 $byte_order = 1; // Big Endian
97 } else {
98 // Give up. I'll fix this in a later version.
99 throw new WriterException('Required floating point format not supported on this platform.');
100 }
101 self::$byteOrder = $byte_order;
102 }
103
104 return self::$byteOrder;
105 }
106
112 protected function append($data): void
113 {
114 if (strlen($data) - 4 > $this->limit) {
115 $data = $this->addContinue($data);
116 }
117 $this->_data .= $data;
118 $this->_datasize += strlen($data);
119 }
120
128 public function writeData($data)
129 {
130 if (strlen($data) - 4 > $this->limit) {
131 $data = $this->addContinue($data);
132 }
133 $this->_datasize += strlen($data);
134
135 return $data;
136 }
137
145 protected function storeBof($type): void
146 {
147 $record = 0x0809; // Record identifier (BIFF5-BIFF8)
148 $length = 0x0010;
149
150 // by inspection of real files, MS Office Excel 2007 writes the following
151 $unknown = pack('VV', 0x000100D1, 0x00000406);
152
153 $build = 0x0DBB; // Excel 97
154 $year = 0x07CC; // Excel 97
155
156 $version = 0x0600; // BIFF8
157
158 $header = pack('vv', $record, $length);
159 $data = pack('vvvv', $version, $type, $build, $year);
160 $this->append($header . $data . $unknown);
161 }
162
166 protected function storeEof(): void
167 {
168 $record = 0x000A; // Record identifier
169 $length = 0x0000; // Number of bytes to follow
170
171 $header = pack('vv', $record, $length);
172 $this->append($header);
173 }
174
178 public function writeEof()
179 {
180 $record = 0x000A; // Record identifier
181 $length = 0x0000; // Number of bytes to follow
182 $header = pack('vv', $record, $length);
183
184 return $this->writeData($header);
185 }
186
199 private function addContinue($data)
200 {
202 $record = 0x003C; // Record identifier
203
204 // The first 2080/8224 bytes remain intact. However, we have to change
205 // the length field of the record.
206 $tmp = substr($data, 0, 2) . pack('v', $limit) . substr($data, 4, $limit);
207
208 $header = pack('vv', $record, $limit); // Headers for continue records
209
210 // Retrieve chunks of 2080/8224 bytes +4 for the header.
211 $data_length = strlen($data);
212 for ($i = $limit + 4; $i < ($data_length - $limit); $i += $limit) {
213 $tmp .= $header;
214 $tmp .= substr($data, $i, $limit);
215 }
216
217 // Retrieve the last chunk of data
218 $header = pack('vv', $record, strlen($data) - $i);
219 $tmp .= $header;
220 $tmp .= substr($data, $i);
221
222 return $tmp;
223 }
224}
$version
Definition: build.php:27
An exception for terminatinating execution or to throw for unit testing.
storeBof($type)
Writes Excel BOF record to indicate the beginning of a stream or sub-stream in the BIFF file.
Definition: BIFFwriter.php:145
writeEof()
Writes Excel EOF record to indicate the end of a BIFF stream.
Definition: BIFFwriter.php:178
static getByteOrder()
Determine the byte order and store it as class data to avoid recalculating it for each call to new().
Definition: BIFFwriter.php:87
addContinue($data)
Excel limits the size of BIFF records.
Definition: BIFFwriter.php:199
append($data)
General storage function.
Definition: BIFFwriter.php:112
writeData($data)
General storage function like append, but returns string instead of modifying $this->_data.
Definition: BIFFwriter.php:128
storeEof()
Writes Excel EOF record to indicate the end of a BIFF stream.
Definition: BIFFwriter.php:166
$i
Definition: disco.tpl.php:19
$type
$data
Definition: bench.php:6