wpdatatables
/
lib
/
phpoffice
/
phpspreadsheet
/
src
/
PhpSpreadsheet
/
Writer
/
Xls
/
BIFFwriter.php
BIFFwriter.php in wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin 6.5.1.7, at lib/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xls/BIFFwriter.php
| 1 | <?php |
| 2 | |
| 3 | namespace PhpOffice\PhpSpreadsheet\Writer\Xls; |
| 4 | |
| 5 | use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException; |
| 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 | // */ |
| 40 | class BIFFwriter |
| 41 | { |
| 42 | /** |
| 43 | * The byte order of this architecture. 0 => little endian, 1 => big endian. |
| 44 | * |
| 45 | * @var ?int |
| 46 | */ |
| 47 | private static $byteOrder; |
| 48 | |
| 49 | /** |
| 50 | * The string containing the data of the BIFF stream. |
| 51 | * |
| 52 | * @var null|string |
| 53 | */ |
| 54 | public $_data; |
| 55 | |
| 56 | /** |
| 57 | * The size of the data in bytes. Should be the same as strlen($this->_data). |
| 58 | * |
| 59 | * @var int |
| 60 | */ |
| 61 | public $_datasize; |
| 62 | |
| 63 | /** |
| 64 | * The maximum length for a BIFF record (excluding record header and length field). See addContinue(). |
| 65 | * |
| 66 | * @var int |
| 67 | * |
| 68 | * @see addContinue() |
| 69 | */ |
| 70 | private $limit = 8224; |
| 71 | |
| 72 | /** |
| 73 | * Constructor. |
| 74 | */ |
| 75 | public function __construct() |
| 76 | { |
| 77 | $this->_data = ''; |
| 78 | $this->_datasize = 0; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Determine the byte order and store it as class data to avoid |
| 83 | * recalculating it for each call to new(). |
| 84 | * |
| 85 | * @return int |
| 86 | */ |
| 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 | |
| 107 | /** |
| 108 | * General storage function. |
| 109 | * |
| 110 | * @param string $data binary data to append |
| 111 | */ |
| 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 | |
| 121 | /** |
| 122 | * General storage function like append, but returns string instead of modifying $this->_data. |
| 123 | * |
| 124 | * @param string $data binary data to write |
| 125 | * |
| 126 | * @return string |
| 127 | */ |
| 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 | |
| 138 | /** |
| 139 | * Writes Excel BOF record to indicate the beginning of a stream or |
| 140 | * sub-stream in the BIFF file. |
| 141 | * |
| 142 | * @param int $type type of BIFF file to write: 0x0005 Workbook, |
| 143 | * 0x0010 Worksheet |
| 144 | */ |
| 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 | |
| 163 | /** |
| 164 | * Writes Excel EOF record to indicate the end of a BIFF stream. |
| 165 | */ |
| 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 | |
| 175 | /** |
| 176 | * Writes Excel EOF record to indicate the end of a BIFF stream. |
| 177 | */ |
| 178 | public function writeEof(): string |
| 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 | |
| 187 | /** |
| 188 | * Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In |
| 189 | * Excel 97 the limit is 8228 bytes. Records that are longer than these limits |
| 190 | * must be split up into CONTINUE blocks. |
| 191 | * |
| 192 | * This function takes a long BIFF record and inserts CONTINUE records as |
| 193 | * necessary. |
| 194 | * |
| 195 | * @param string $data The original binary data to be written |
| 196 | * |
| 197 | * @return string A very convenient string of continue blocks |
| 198 | */ |
| 199 | private function addContinue($data) |
| 200 | { |
| 201 | $limit = $this->limit; |
| 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 | } |
| 225 |