1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
<?php
/*
* Copyleft 2002 Johann Hanne
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA
*/
/*
* This is the Spreadsheet::WriteExcel Perl package ported to PHP
* Spreadsheet::WriteExcel was written by John McNamara, jmcnamara@cpan.org
*/
class writeexcel_olewriter {
var $_OLEfilename;
var $_OLEtmpfilename; /* ABR */
var $_filehandle;
var $_fileclosed;
var $_internal_fh;
var $_biff_only;
var $_size_allowed;
var $_biffsize;
var $_booksize;
var $_big_blocks;
var $_list_blocks;
var $_root_start;
var $_block_count;
/*
* Constructor
*/
function __construct($filename) {
$this->_OLEfilename = $filename;
$this->_filehandle = false;
$this->_fileclosed = 0;
$this->_internal_fh = 0;
$this->_biff_only = 0;
$this->_size_allowed = 0;
$this->_biffsize = 0;
$this->_booksize = 0;
$this->_big_blocks = 0;
$this->_list_blocks = 0;
$this->_root_start = 0;
$this->_block_count = 4;
$this->_initialize();
}
/*
* Check for a valid filename and store the filehandle.
*/
function _initialize() {
$OLEfile = $this->_OLEfilename;
/* Check for a filename. Workbook.pm will catch this first. */
if ($OLEfile == '') {
trigger_error("Filename required", E_USER_ERROR);
}
/*
* If the filename is a resource it is assumed that it is a valid
* filehandle, if not we create a filehandle.
*/
if (is_resource($OLEfile)) {
$fh = $OLEfile;
} else {
// Create a new file, open for writing
$fh = fopen($OLEfile, "wb");
// The workbook class also checks this but something may have
// happened since then.
if (!$fh) {
trigger_error("Cannot open $OLEfile! It may be in use or ".
"protected.", E_USER_ERROR);
}
$this->_internal_fh = 1;
}
// Store filehandle
$this->_filehandle = $fh;
}
/*
* Set the size of the data to be written to the OLE stream
*
* $big_blocks = (109 depot block x (128 -1 marker word)
* - (1 x end words)) = 13842
* $maxsize = $big_blocks * 512 bytes = 7087104
*/
function set_size($size) {
$maxsize = 7087104;
if ($size > $maxsize) {
trigger_error("Maximum file size, $maxsize, exceeded. To create ".
"files bigger than this limit please use the ".
"workbookbig class.", E_USER_ERROR);
return ($this->_size_allowed = 0);
}
$this->_biffsize = $size;
// Set the min file size to 4k to avoid having to use small blocks
if ($size > 4096) {
$this->_booksize = $size;
} else {
$this->_booksize = 4096;
}
return ($this->_size_allowed = 1);
}
/*
* Calculate various sizes needed for the OLE stream
*/
function _calculate_sizes() {
$datasize = $this->_booksize;
if ($datasize % 512 == 0) {
$this->_big_blocks = $datasize/512;
} else {
$this->_big_blocks = floor($datasize/512)+1;
}
// There are 127 list blocks and 1 marker blocks for each big block
// depot + 1 end of chain block
$this->_list_blocks = floor(($this->_big_blocks)/127)+1;
$this->_root_start = $this->_big_blocks;
//print $this->_biffsize. "\n";
//print $this->_big_blocks. "\n";
//print $this->_list_blocks. "\n";
}
/*
* Write root entry, big block list and close the filehandle.
* This method must be called so that the file contents are
* actually written.
*/
function close() {
if (!$this->_size_allowed) {
return;
}
if (!$this->_biff_only) {
$this->_write_padding();
$this->_write_property_storage();
$this->_write_big_block_depot();
}
// Close the filehandle if it was created internally.
if ($this->_internal_fh) {
fclose($this->_filehandle);
}
/* ABR */
if ($this->_OLEtmpfilename != '') {
$fh = fopen($this->_OLEtmpfilename, "rb");
if ($fh == false) {
trigger_error("Cannot read temporary file!", E_USER_ERROR);
}
fpassthru($fh);
fclose($fh);
unlink($this->_OLEtmpfilename);
};
$this->_fileclosed = 1;
}
/*
* Write BIFF data to OLE file.
*/
function write($data) {
fputs($this->_filehandle, $data);
}
/*
* Write OLE header block.
*/
function write_header() {
if ($this->_biff_only) {
return;
}
$this->_calculate_sizes();
$root_start = $this->_root_start;
$num_lists = $this->_list_blocks;
$id = pack("C8", 0xD0, 0xCF, 0x11, 0xE0,
0xA1, 0xB1, 0x1A, 0xE1);
$unknown1 = pack("VVVV", 0x00, 0x00, 0x00, 0x00);
$unknown2 = pack("vv", 0x3E, 0x03);
$unknown3 = pack("v", -2);
$unknown4 = pack("v", 0x09);
$unknown5 = pack("VVV", 0x06, 0x00, 0x00);
$num_bbd_blocks = pack("V", $num_lists);
$root_startblock = pack("V", $root_start);
$unknown6 = pack("VV", 0x00, 0x1000);
$sbd_startblock = pack("V", -2);
$unknown7 = pack("VVV", 0x00, -2 ,0x00);
$unused = pack("V", -1);
fputs($this->_filehandle, $id);
fputs($this->_filehandle, $unknown1);
fputs($this->_filehandle, $unknown2);
fputs($this->_filehandle, $unknown3);
fputs($this->_filehandle, $unknown4);
fputs($this->_filehandle, $unknown5);
fputs($this->_filehandle, $num_bbd_blocks);
fputs($this->_filehandle, $root_startblock);
fputs($this->_filehandle, $unknown6);
fputs($this->_filehandle, $sbd_startblock);
fputs($this->_filehandle, $unknown7);
for ($c=1;$c<=$num_lists;$c++) {
$root_start++;
fputs($this->_filehandle, pack("V", $root_start));
}
for ($c=$num_lists;$c<=108;$c++) {
fputs($this->_filehandle, $unused);
}
}
/*
* Write big block depot.
*/
function _write_big_block_depot() {
$num_blocks = $this->_big_blocks;
$num_lists = $this->_list_blocks;
$total_blocks = $num_lists * 128;
$used_blocks = $num_blocks + $num_lists + 2;
$marker = pack("V", -3);
$end_of_chain = pack("V", -2);
$unused = pack("V", -1);
for ($i=1;$i<=($num_blocks-1);$i++) {
fputs($this->_filehandle, pack("V", $i));
}
fputs($this->_filehandle, $end_of_chain);
fputs($this->_filehandle, $end_of_chain);
for ($c=1;$c<=$num_lists;$c++) {
fputs($this->_filehandle, $marker);
}
for ($c=$used_blocks;$c<=$total_blocks;$c++) {
fputs($this->_filehandle, $unused);
}
}
/*
* Write property storage. TODO: add summary sheets
*/
function _write_property_storage() {
$rootsize = -2;
$booksize = $this->_booksize;
// name type dir start size
$this->_write_pps('Root Entry', 0x05, 1, -2, 0x00);
$this->_write_pps('Book', 0x02, -1, 0x00, $booksize);
$this->_write_pps('', 0x00, -1, 0x00, 0x0000);
$this->_write_pps('', 0x00, -1, 0x00, 0x0000);
}
/*
* Write property sheet in property storage
*/
function _write_pps($name, $type, $dir, $start, $size) {
$names = array();
$length = 0;
if ($name != '') {
$name = $name . "\0";
// Simulate a Unicode string
$chars=preg_split("''", $name, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) {
array_push($names, ord($char));
}
$length = strlen($name) * 2;
}
$rawname = call_user_func_array('pack', array_merge(array("v*"), $names));
$zero = pack("C", 0);
$pps_sizeofname = pack("v", $length); //0x40
$pps_type = pack("v", $type); //0x42
$pps_prev = pack("V", -1); //0x44
$pps_next = pack("V", -1); //0x48
$pps_dir = pack("V", $dir); //0x4c
$unknown1 = pack("V", 0);
$pps_ts1s = pack("V", 0); //0x64
$pps_ts1d = pack("V", 0); //0x68
$pps_ts2s = pack("V", 0); //0x6c
$pps_ts2d = pack("V", 0); //0x70
$pps_sb = pack("V", $start); //0x74
$pps_size = pack("V", $size); //0x78
fputs($this->_filehandle, $rawname);
fputs($this->_filehandle, str_repeat($zero, (64-$length)));
fputs($this->_filehandle, $pps_sizeofname);
fputs($this->_filehandle, $pps_type);
fputs($this->_filehandle, $pps_prev);
fputs($this->_filehandle, $pps_next);
fputs($this->_filehandle, $pps_dir);
fputs($this->_filehandle, str_repeat($unknown1, 5));
fputs($this->_filehandle, $pps_ts1s);
fputs($this->_filehandle, $pps_ts1d);
fputs($this->_filehandle, $pps_ts2d);
fputs($this->_filehandle, $pps_ts2d);
fputs($this->_filehandle, $pps_sb);
fputs($this->_filehandle, $pps_size);
fputs($this->_filehandle, $unknown1);
}
/*
* Pad the end of the file
*/
function _write_padding() {
$biffsize = $this->_biffsize;
if ($biffsize < 4096) {
$min_size = 4096;
} else {
$min_size = 512;
}
if ($biffsize % $min_size != 0) {
$padding = $min_size - ($biffsize % $min_size);
fputs($this->_filehandle, str_repeat("\0", $padding));
}
}
}
?>
|