PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/include/php_writeexcel/class.writeexcel_biffwriter.inc.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 208 lines | 109 code | 37 blank | 62 comment | 15 complexity | ee43fb5afa6599b75760c9c0763d905b MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /*
  3. * Copyleft 2002 Johann Hanne
  4. *
  5. * This is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this software; if not, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /*
  21. * This is the Spreadsheet::WriteExcel Perl package ported to PHP
  22. * Spreadsheet::WriteExcel was written by John McNamara, jmcnamara@cpan.org
  23. */
  24. class writeexcel_biffwriter {
  25. var $byte_order;
  26. var $BIFF_version;
  27. var $_byte_order;
  28. var $_data;
  29. var $_datasize;
  30. var $_limit;
  31. /*
  32. * Constructor
  33. */
  34. function writeexcel_biffwriter() {
  35. $this->byte_order = '';
  36. $this->BIFF_version = 0x0500;
  37. $this->_byte_order = '';
  38. $this->_data = false;
  39. $this->_datasize = 0;
  40. $this->_limit = 2080;
  41. $this->_set_byte_order();
  42. }
  43. /*
  44. * Determine the byte order and store it as class data to avoid
  45. * recalculating it for each call to new().
  46. */
  47. function _set_byte_order() {
  48. $this->byteorder=0;
  49. // Check if "pack" gives the required IEEE 64bit float
  50. $teststr = pack("d", 1.2345);
  51. $number = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
  52. if ($number == $teststr) {
  53. $this->byte_order = 0; // Little Endian
  54. } elseif ($number == strrev($teststr)) {
  55. $this->byte_order = 1; // Big Endian
  56. } else {
  57. // Give up
  58. trigger_error("Required floating point format not supported ".
  59. "on this platform. See the portability section ".
  60. "of the documentation.", E_USER_ERROR);
  61. }
  62. $this->_byte_order = $this->byte_order;
  63. }
  64. /*
  65. * General storage function
  66. */
  67. function _prepend($data) {
  68. if (func_num_args()>1) {
  69. trigger_error("writeexcel_biffwriter::_prepend() ".
  70. "called with more than one argument", E_USER_ERROR);
  71. }
  72. if ($this->_debug) {
  73. print "*** writeexcel_biffwriter::_prepend() called:";
  74. for ($c=0;$c<strlen($data);$c++) {
  75. if ($c%16==0) {
  76. print "\n";
  77. }
  78. printf("%02X ", ord($data[$c]));
  79. }
  80. print "\n";
  81. }
  82. if (strlen($data) > $this->_limit) {
  83. $data = $this->_add_continue($data);
  84. }
  85. $this->_data = $data . $this->_data;
  86. $this->_datasize += strlen($data);
  87. }
  88. /*
  89. * General storage function
  90. */
  91. function _append($data) {
  92. if (func_num_args()>1) {
  93. trigger_error("writeexcel_biffwriter::_append() ".
  94. "called with more than one argument", E_USER_ERROR);
  95. }
  96. if ($this->_debug) {
  97. print "*** writeexcel_biffwriter::_append() called:";
  98. for ($c=0;$c<strlen($data);$c++) {
  99. if ($c%16==0) {
  100. print "\n";
  101. }
  102. printf("%02X ", ord($data[$c]));
  103. }
  104. print "\n";
  105. }
  106. if (strlen($data) > $this->_limit) {
  107. $data = $this->_add_continue($data);
  108. }
  109. $this->_data = $this->_data . $data;
  110. $this->_datasize += strlen($data);
  111. }
  112. /*
  113. * Writes Excel BOF record to indicate the beginning of a stream or
  114. * sub-stream in the BIFF file.
  115. *
  116. * $type = 0x0005, Workbook
  117. * $type = 0x0010, Worksheet
  118. */
  119. function _store_bof($type) {
  120. $record = 0x0809; // Record identifier
  121. $length = 0x0008; // Number of bytes to follow
  122. $version = $this->BIFF_version;
  123. // According to the SDK $build and $year should be set to zero.
  124. // However, this throws a warning in Excel 5. So, use these
  125. // magic numbers.
  126. $build = 0x096C;
  127. $year = 0x07C9;
  128. $header = pack("vv", $record, $length);
  129. $data = pack("vvvv", $version, $type, $build, $year);
  130. $this->_prepend($header . $data);
  131. }
  132. /*
  133. * Writes Excel EOF record to indicate the end of a BIFF stream.
  134. */
  135. function _store_eof() {
  136. $record = 0x000A; // Record identifier
  137. $length = 0x0000; // Number of bytes to follow
  138. $header = pack("vv", $record, $length);
  139. $this->_append($header);
  140. }
  141. /*
  142. * Excel limits the size of BIFF records. In Excel 5 the limit is 2084
  143. * bytes. In Excel 97 the limit is 8228 bytes. Records that are longer
  144. * than these limits must be split up into CONTINUE blocks.
  145. *
  146. * This function take a long BIFF record and inserts CONTINUE records as
  147. * necessary.
  148. */
  149. function _add_continue($data) {
  150. $limit = $this->_limit;
  151. $record = 0x003C; // Record identifier
  152. // The first 2080/8224 bytes remain intact. However, we have to change
  153. // the length field of the record.
  154. $tmp = substr($data, 0, $limit);
  155. $data = substr($data, $limit);
  156. $tmp = substr($tmp, 0, 2) . pack ("v", $limit-4) . substr($tmp, 4);
  157. // Strip out chunks of 2080/8224 bytes +4 for the header.
  158. while (strlen($data) > $limit) {
  159. $header = pack("vv", $record, $limit);
  160. $tmp .= $header;
  161. $tmp .= substr($data, 0, $limit);
  162. $data = substr($data, $limit);
  163. }
  164. // Mop up the last of the data
  165. $header = pack("vv", $record, strlen($data));
  166. $tmp .= $header;
  167. $tmp .= $data;
  168. return $tmp;
  169. }
  170. }
  171. ?>