PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/Classes/PHPExcel/Writer/Excel5.php

https://github.com/iGroup/PHPExcel
PHP | 936 lines | 538 code | 138 blank | 260 comment | 50 complexity | 583507b7f8ac725427b0c447c0077c3b MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_Excel5
  23. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel5
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel5
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel5 extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  35. {
  36. /**
  37. * PHPExcel object
  38. *
  39. * @var PHPExcel
  40. */
  41. private $_phpExcel;
  42. /**
  43. * Total number of shared strings in workbook
  44. *
  45. * @var int
  46. */
  47. private $_str_total = 0;
  48. /**
  49. * Number of unique shared strings in workbook
  50. *
  51. * @var int
  52. */
  53. private $_str_unique = 0;
  54. /**
  55. * Array of unique shared strings in workbook
  56. *
  57. * @var array
  58. */
  59. private $_str_table = array();
  60. /**
  61. * Color cache. Mapping between RGB value and color index.
  62. *
  63. * @var array
  64. */
  65. private $_colors;
  66. /**
  67. * Formula parser
  68. *
  69. * @var PHPExcel_Writer_Excel5_Parser
  70. */
  71. private $_parser;
  72. /**
  73. * Identifier clusters for drawings. Used in MSODRAWINGGROUP record.
  74. *
  75. * @var array
  76. */
  77. private $_IDCLs;
  78. /**
  79. * Basic OLE object summary information
  80. *
  81. * @var array
  82. */
  83. private $_summaryInformation;
  84. /**
  85. * Extended OLE object document summary information
  86. *
  87. * @var array
  88. */
  89. private $_documentSummaryInformation;
  90. /**
  91. * Create a new PHPExcel_Writer_Excel5
  92. *
  93. * @param PHPExcel $phpExcel PHPExcel object
  94. */
  95. public function __construct(PHPExcel $phpExcel) {
  96. $this->_phpExcel = $phpExcel;
  97. $this->_parser = new PHPExcel_Writer_Excel5_Parser();
  98. }
  99. /**
  100. * Save PHPExcel to file
  101. *
  102. * @param string $pFilename
  103. * @throws PHPExcel_Writer_Exception
  104. */
  105. public function save($pFilename = null) {
  106. // garbage collect
  107. $this->_phpExcel->garbageCollect();
  108. $saveDebugLog = PHPExcel_Calculation::getInstance()->writeDebugLog;
  109. PHPExcel_Calculation::getInstance()->writeDebugLog = false;
  110. $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
  111. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  112. // initialize colors array
  113. $this->_colors = array();
  114. // Initialise workbook writer
  115. $this->_writerWorkbook = new PHPExcel_Writer_Excel5_Workbook($this->_phpExcel,
  116. $this->_str_total, $this->_str_unique, $this->_str_table,
  117. $this->_colors, $this->_parser);
  118. // Initialise worksheet writers
  119. $countSheets = $this->_phpExcel->getSheetCount();
  120. for ($i = 0; $i < $countSheets; ++$i) {
  121. $this->_writerWorksheets[$i] = new PHPExcel_Writer_Excel5_Worksheet($this->_str_total, $this->_str_unique,
  122. $this->_str_table, $this->_colors,
  123. $this->_parser,
  124. $this->_preCalculateFormulas,
  125. $this->_phpExcel->getSheet($i));
  126. }
  127. // build Escher objects. Escher objects for workbooks needs to be build before Escher object for workbook.
  128. $this->_buildWorksheetEschers();
  129. $this->_buildWorkbookEscher();
  130. // add 15 identical cell style Xfs
  131. // for now, we use the first cellXf instead of cellStyleXf
  132. $cellXfCollection = $this->_phpExcel->getCellXfCollection();
  133. for ($i = 0; $i < 15; ++$i) {
  134. $this->_writerWorkbook->addXfWriter($cellXfCollection[0], true);
  135. }
  136. // add all the cell Xfs
  137. foreach ($this->_phpExcel->getCellXfCollection() as $style) {
  138. $this->_writerWorkbook->addXfWriter($style, false);
  139. }
  140. // add fonts from rich text eleemnts
  141. for ($i = 0; $i < $countSheets; ++$i) {
  142. foreach ($this->_writerWorksheets[$i]->_phpSheet->getCellCollection() as $cellID) {
  143. $cell = $this->_writerWorksheets[$i]->_phpSheet->getCell($cellID);
  144. $cVal = $cell->getValue();
  145. if ($cVal instanceof PHPExcel_RichText) {
  146. $elements = $cVal->getRichTextElements();
  147. foreach ($elements as $element) {
  148. if ($element instanceof PHPExcel_RichText_Run) {
  149. $font = $element->getFont();
  150. $this->_writerWorksheets[$i]->_fntHashIndex[$font->getHashCode()] = $this->_writerWorkbook->_addFont($font);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. // initialize OLE file
  157. $workbookStreamName = 'Workbook';
  158. $OLE = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs($workbookStreamName));
  159. // Write the worksheet streams before the global workbook stream,
  160. // because the byte sizes of these are needed in the global workbook stream
  161. $worksheetSizes = array();
  162. for ($i = 0; $i < $countSheets; ++$i) {
  163. $this->_writerWorksheets[$i]->close();
  164. $worksheetSizes[] = $this->_writerWorksheets[$i]->_datasize;
  165. }
  166. // add binary data for global workbook stream
  167. $OLE->append( $this->_writerWorkbook->writeWorkbook($worksheetSizes) );
  168. // add binary data for sheet streams
  169. for ($i = 0; $i < $countSheets; ++$i) {
  170. $OLE->append($this->_writerWorksheets[$i]->getData());
  171. }
  172. $this->_documentSummaryInformation = $this->_writeDocumentSummaryInformation();
  173. // initialize OLE Document Summary Information
  174. if(isset($this->_documentSummaryInformation) && !empty($this->_documentSummaryInformation)){
  175. $OLE_DocumentSummaryInformation = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs(chr(5) . 'DocumentSummaryInformation'));
  176. $OLE_DocumentSummaryInformation->append($this->_documentSummaryInformation);
  177. }
  178. $this->_summaryInformation = $this->_writeSummaryInformation();
  179. // initialize OLE Summary Information
  180. if(isset($this->_summaryInformation) && !empty($this->_summaryInformation)){
  181. $OLE_SummaryInformation = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs(chr(5) . 'SummaryInformation'));
  182. $OLE_SummaryInformation->append($this->_summaryInformation);
  183. }
  184. // define OLE Parts
  185. $arrRootData = array($OLE);
  186. // initialize OLE Properties file
  187. if(isset($OLE_SummaryInformation)){
  188. $arrRootData[] = $OLE_SummaryInformation;
  189. }
  190. // initialize OLE Extended Properties file
  191. if(isset($OLE_DocumentSummaryInformation)){
  192. $arrRootData[] = $OLE_DocumentSummaryInformation;
  193. }
  194. $root = new PHPExcel_Shared_OLE_PPS_Root(time(), time(), $arrRootData);
  195. // save the OLE file
  196. $res = $root->save($pFilename);
  197. PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  198. PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog;
  199. }
  200. /**
  201. * Set temporary storage directory
  202. *
  203. * @deprecated
  204. * @param string $pValue Temporary storage directory
  205. * @throws PHPExcel_Writer_Exception Exception when directory does not exist
  206. * @return PHPExcel_Writer_Excel5
  207. */
  208. public function setTempDir($pValue = '') {
  209. return $this;
  210. }
  211. /**
  212. * Build the Worksheet Escher objects
  213. *
  214. */
  215. private function _buildWorksheetEschers()
  216. {
  217. // 1-based index to BstoreContainer
  218. $blipIndex = 0;
  219. $lastReducedSpId = 0;
  220. $lastSpId = 0;
  221. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
  222. // sheet index
  223. $sheetIndex = $sheet->getParent()->getIndex($sheet);
  224. $escher = null;
  225. // check if there are any shapes for this sheet
  226. $filterRange = $sheet->getAutoFilter()->getRange();
  227. if (count($sheet->getDrawingCollection()) == 0 && empty($filterRange)) {
  228. continue;
  229. }
  230. // create intermediate Escher object
  231. $escher = new PHPExcel_Shared_Escher();
  232. // dgContainer
  233. $dgContainer = new PHPExcel_Shared_Escher_DgContainer();
  234. // set the drawing index (we use sheet index + 1)
  235. $dgId = $sheet->getParent()->getIndex($sheet) + 1;
  236. $dgContainer->setDgId($dgId);
  237. $escher->setDgContainer($dgContainer);
  238. // spgrContainer
  239. $spgrContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer();
  240. $dgContainer->setSpgrContainer($spgrContainer);
  241. // add one shape which is the group shape
  242. $spContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
  243. $spContainer->setSpgr(true);
  244. $spContainer->setSpType(0);
  245. $spContainer->setSpId(($sheet->getParent()->getIndex($sheet) + 1) << 10);
  246. $spgrContainer->addChild($spContainer);
  247. // add the shapes
  248. $countShapes[$sheetIndex] = 0; // count number of shapes (minus group shape), in sheet
  249. foreach ($sheet->getDrawingCollection() as $drawing) {
  250. ++$blipIndex;
  251. ++$countShapes[$sheetIndex];
  252. // add the shape
  253. $spContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
  254. // set the shape type
  255. $spContainer->setSpType(0x004B);
  256. // set the shape flag
  257. $spContainer->setSpFlag(0x02);
  258. // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index)
  259. $reducedSpId = $countShapes[$sheetIndex];
  260. $spId = $reducedSpId
  261. | ($sheet->getParent()->getIndex($sheet) + 1) << 10;
  262. $spContainer->setSpId($spId);
  263. // keep track of last reducedSpId
  264. $lastReducedSpId = $reducedSpId;
  265. // keep track of last spId
  266. $lastSpId = $spId;
  267. // set the BLIP index
  268. $spContainer->setOPT(0x4104, $blipIndex);
  269. // set coordinates and offsets, client anchor
  270. $coordinates = $drawing->getCoordinates();
  271. $offsetX = $drawing->getOffsetX();
  272. $offsetY = $drawing->getOffsetY();
  273. $width = $drawing->getWidth();
  274. $height = $drawing->getHeight();
  275. $twoAnchor = PHPExcel_Shared_Excel5::oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height);
  276. $spContainer->setStartCoordinates($twoAnchor['startCoordinates']);
  277. $spContainer->setStartOffsetX($twoAnchor['startOffsetX']);
  278. $spContainer->setStartOffsetY($twoAnchor['startOffsetY']);
  279. $spContainer->setEndCoordinates($twoAnchor['endCoordinates']);
  280. $spContainer->setEndOffsetX($twoAnchor['endOffsetX']);
  281. $spContainer->setEndOffsetY($twoAnchor['endOffsetY']);
  282. $spgrContainer->addChild($spContainer);
  283. }
  284. // AutoFilters
  285. if(!empty($filterRange)){
  286. $rangeBounds = PHPExcel_Cell::rangeBoundaries($filterRange);
  287. $iNumColStart = $rangeBounds[0][0];
  288. $iNumColEnd = $rangeBounds[1][0];
  289. $iInc = $iNumColStart;
  290. while($iInc <= $iNumColEnd){
  291. ++$countShapes[$sheetIndex];
  292. // create an Drawing Object for the dropdown
  293. $oDrawing = new PHPExcel_Worksheet_BaseDrawing();
  294. // get the coordinates of drawing
  295. $cDrawing = PHPExcel_Cell::stringFromColumnIndex($iInc - 1) . $rangeBounds[0][1];
  296. $oDrawing->setCoordinates($cDrawing);
  297. $oDrawing->setWorksheet($sheet);
  298. // add the shape
  299. $spContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
  300. // set the shape type
  301. $spContainer->setSpType(0x00C9);
  302. // set the shape flag
  303. $spContainer->setSpFlag(0x01);
  304. // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index)
  305. $reducedSpId = $countShapes[$sheetIndex];
  306. $spId = $reducedSpId
  307. | ($sheet->getParent()->getIndex($sheet) + 1) << 10;
  308. $spContainer->setSpId($spId);
  309. // keep track of last reducedSpId
  310. $lastReducedSpId = $reducedSpId;
  311. // keep track of last spId
  312. $lastSpId = $spId;
  313. $spContainer->setOPT(0x007F, 0x01040104); // Protection -> fLockAgainstGrouping
  314. $spContainer->setOPT(0x00BF, 0x00080008); // Text -> fFitTextToShape
  315. $spContainer->setOPT(0x01BF, 0x00010000); // Fill Style -> fNoFillHitTest
  316. $spContainer->setOPT(0x01FF, 0x00080000); // Line Style -> fNoLineDrawDash
  317. $spContainer->setOPT(0x03BF, 0x000A0000); // Group Shape -> fPrint
  318. // set coordinates and offsets, client anchor
  319. $endCoordinates = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::stringFromColumnIndex($iInc - 1));
  320. $endCoordinates .= $rangeBounds[0][1] + 1;
  321. $spContainer->setStartCoordinates($cDrawing);
  322. $spContainer->setStartOffsetX(0);
  323. $spContainer->setStartOffsetY(0);
  324. $spContainer->setEndCoordinates($endCoordinates);
  325. $spContainer->setEndOffsetX(0);
  326. $spContainer->setEndOffsetY(0);
  327. $spgrContainer->addChild($spContainer);
  328. $iInc++;
  329. }
  330. }
  331. // identifier clusters, used for workbook Escher object
  332. $this->_IDCLs[$dgId] = $lastReducedSpId;
  333. // set last shape index
  334. $dgContainer->setLastSpId($lastSpId);
  335. // set the Escher object
  336. $this->_writerWorksheets[$sheetIndex]->setEscher($escher);
  337. }
  338. }
  339. /**
  340. * Build the Escher object corresponding to the MSODRAWINGGROUP record
  341. */
  342. private function _buildWorkbookEscher()
  343. {
  344. $escher = null;
  345. // any drawings in this workbook?
  346. $found = false;
  347. foreach ($this->_phpExcel->getAllSheets() as $sheet) {
  348. if (count($sheet->getDrawingCollection()) > 0) {
  349. $found = true;
  350. break;
  351. }
  352. }
  353. // nothing to do if there are no drawings
  354. if (!$found) {
  355. return;
  356. }
  357. // if we reach here, then there are drawings in the workbook
  358. $escher = new PHPExcel_Shared_Escher();
  359. // dggContainer
  360. $dggContainer = new PHPExcel_Shared_Escher_DggContainer();
  361. $escher->setDggContainer($dggContainer);
  362. // set IDCLs (identifier clusters)
  363. $dggContainer->setIDCLs($this->_IDCLs);
  364. // this loop is for determining maximum shape identifier of all drawing
  365. $spIdMax = 0;
  366. $totalCountShapes = 0;
  367. $countDrawings = 0;
  368. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
  369. $sheetCountShapes = 0; // count number of shapes (minus group shape), in sheet
  370. if (count($sheet->getDrawingCollection()) > 0) {
  371. ++$countDrawings;
  372. foreach ($sheet->getDrawingCollection() as $drawing) {
  373. ++$sheetCountShapes;
  374. ++$totalCountShapes;
  375. $spId = $sheetCountShapes
  376. | ($this->_phpExcel->getIndex($sheet) + 1) << 10;
  377. $spIdMax = max($spId, $spIdMax);
  378. }
  379. }
  380. }
  381. $dggContainer->setSpIdMax($spIdMax + 1);
  382. $dggContainer->setCDgSaved($countDrawings);
  383. $dggContainer->setCSpSaved($totalCountShapes + $countDrawings); // total number of shapes incl. one group shapes per drawing
  384. // bstoreContainer
  385. $bstoreContainer = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer();
  386. $dggContainer->setBstoreContainer($bstoreContainer);
  387. // the BSE's (all the images)
  388. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
  389. foreach ($sheet->getDrawingCollection() as $drawing) {
  390. if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
  391. $filename = $drawing->getPath();
  392. list($imagesx, $imagesy, $imageFormat) = getimagesize($filename);
  393. switch ($imageFormat) {
  394. case 1: // GIF, not supported by BIFF8, we convert to PNG
  395. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  396. ob_start();
  397. imagepng(imagecreatefromgif($filename));
  398. $blipData = ob_get_contents();
  399. ob_end_clean();
  400. break;
  401. case 2: // JPEG
  402. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
  403. $blipData = file_get_contents($filename);
  404. break;
  405. case 3: // PNG
  406. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  407. $blipData = file_get_contents($filename);
  408. break;
  409. case 6: // Windows DIB (BMP), we convert to PNG
  410. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  411. ob_start();
  412. imagepng(PHPExcel_Shared_Drawing::imagecreatefrombmp($filename));
  413. $blipData = ob_get_contents();
  414. ob_end_clean();
  415. break;
  416. default: continue 2;
  417. }
  418. $blip = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
  419. $blip->setData($blipData);
  420. $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
  421. $BSE->setBlipType($blipType);
  422. $BSE->setBlip($blip);
  423. $bstoreContainer->addBSE($BSE);
  424. } else if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
  425. switch ($drawing->getRenderingFunction()) {
  426. case PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG:
  427. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
  428. $renderingFunction = 'imagejpeg';
  429. break;
  430. case PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF:
  431. case PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG:
  432. case PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT:
  433. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  434. $renderingFunction = 'imagepng';
  435. break;
  436. }
  437. ob_start();
  438. call_user_func($renderingFunction, $drawing->getImageResource());
  439. $blipData = ob_get_contents();
  440. ob_end_clean();
  441. $blip = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
  442. $blip->setData($blipData);
  443. $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
  444. $BSE->setBlipType($blipType);
  445. $BSE->setBlip($blip);
  446. $bstoreContainer->addBSE($BSE);
  447. }
  448. }
  449. }
  450. // Set the Escher object
  451. $this->_writerWorkbook->setEscher($escher);
  452. }
  453. /**
  454. * Build the OLE Part for DocumentSummary Information
  455. * @return string
  456. */
  457. private function _writeDocumentSummaryInformation(){
  458. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  459. $data = pack('v', 0xFFFE);
  460. // offset: 2; size: 2;
  461. $data .= pack('v', 0x0000);
  462. // offset: 4; size: 2; OS version
  463. $data .= pack('v', 0x0106);
  464. // offset: 6; size: 2; OS indicator
  465. $data .= pack('v', 0x0002);
  466. // offset: 8; size: 16
  467. $data .= pack('VVVV', 0x00, 0x00, 0x00, 0x00);
  468. // offset: 24; size: 4; section count
  469. $data .= pack('V', 0x0001);
  470. // offset: 28; size: 16; first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
  471. $data .= pack('vvvvvvvv', 0xD502, 0xD5CD, 0x2E9C, 0x101B, 0x9793, 0x0008, 0x2C2B, 0xAEF9);
  472. // offset: 44; size: 4; offset of the start
  473. $data .= pack('V', 0x30);
  474. // SECTION
  475. $dataSection = array();
  476. $dataSection_NumProps = 0;
  477. $dataSection_Summary = '';
  478. $dataSection_Content = '';
  479. // GKPIDDSI_CODEPAGE: CodePage
  480. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x01),
  481. 'offset' => array('pack' => 'V'),
  482. 'type' => array('pack' => 'V', 'data' => 0x02), // 2 byte signed integer
  483. 'data' => array('data' => 1252));
  484. $dataSection_NumProps++;
  485. // GKPIDDSI_CATEGORY : Category
  486. if($this->_phpExcel->getProperties()->getCategory()){
  487. $dataProp = $this->_phpExcel->getProperties()->getCategory();
  488. $dataProp = 'Test result file';
  489. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x02),
  490. 'offset' => array('pack' => 'V'),
  491. 'type' => array('pack' => 'V', 'data' => 0x1E),
  492. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  493. $dataSection_NumProps++;
  494. }
  495. // GKPIDDSI_VERSION :Version of the application that wrote the property storage
  496. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x17),
  497. 'offset' => array('pack' => 'V'),
  498. 'type' => array('pack' => 'V', 'data' => 0x03),
  499. 'data' => array('pack' => 'V', 'data' => 0x000C0000));
  500. $dataSection_NumProps++;
  501. // GKPIDDSI_SCALE : FALSE
  502. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0B),
  503. 'offset' => array('pack' => 'V'),
  504. 'type' => array('pack' => 'V', 'data' => 0x0B),
  505. 'data' => array('data' => false));
  506. $dataSection_NumProps++;
  507. // GKPIDDSI_LINKSDIRTY : True if any of the values for the linked properties have changed outside of the application
  508. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x10),
  509. 'offset' => array('pack' => 'V'),
  510. 'type' => array('pack' => 'V', 'data' => 0x0B),
  511. 'data' => array('data' => false));
  512. $dataSection_NumProps++;
  513. // GKPIDDSI_SHAREDOC : FALSE
  514. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x13),
  515. 'offset' => array('pack' => 'V'),
  516. 'type' => array('pack' => 'V', 'data' => 0x0B),
  517. 'data' => array('data' => false));
  518. $dataSection_NumProps++;
  519. // GKPIDDSI_HYPERLINKSCHANGED : True if any of the values for the _PID_LINKS (hyperlink text) have changed outside of the application
  520. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x16),
  521. 'offset' => array('pack' => 'V'),
  522. 'type' => array('pack' => 'V', 'data' => 0x0B),
  523. 'data' => array('data' => false));
  524. $dataSection_NumProps++;
  525. // GKPIDDSI_DOCSPARTS
  526. // MS-OSHARED p75 (2.3.3.2.2.1)
  527. // Structure is VtVecUnalignedLpstrValue (2.3.3.1.9)
  528. // cElements
  529. $dataProp = pack('v', 0x0001);
  530. $dataProp .= pack('v', 0x0000);
  531. // array of UnalignedLpstr
  532. // cch
  533. $dataProp .= pack('v', 0x000A);
  534. $dataProp .= pack('v', 0x0000);
  535. // value
  536. $dataProp .= 'Worksheet'.chr(0);
  537. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0D),
  538. 'offset' => array('pack' => 'V'),
  539. 'type' => array('pack' => 'V', 'data' => 0x101E),
  540. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  541. $dataSection_NumProps++;
  542. // GKPIDDSI_HEADINGPAIR
  543. // VtVecHeadingPairValue
  544. // cElements
  545. $dataProp = pack('v', 0x0002);
  546. $dataProp .= pack('v', 0x0000);
  547. // Array of vtHeadingPair
  548. // vtUnalignedString - headingString
  549. // stringType
  550. $dataProp .= pack('v', 0x001E);
  551. // padding
  552. $dataProp .= pack('v', 0x0000);
  553. // UnalignedLpstr
  554. // cch
  555. $dataProp .= pack('v', 0x0013);
  556. $dataProp .= pack('v', 0x0000);
  557. // value
  558. $dataProp .= 'Feuilles de calcul';
  559. // vtUnalignedString - headingParts
  560. // wType : 0x0003 = 32 bit signed integer
  561. $dataProp .= pack('v', 0x0300);
  562. // padding
  563. $dataProp .= pack('v', 0x0000);
  564. // value
  565. $dataProp .= pack('v', 0x0100);
  566. $dataProp .= pack('v', 0x0000);
  567. $dataProp .= pack('v', 0x0000);
  568. $dataProp .= pack('v', 0x0000);
  569. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0C),
  570. 'offset' => array('pack' => 'V'),
  571. 'type' => array('pack' => 'V', 'data' => 0x100C),
  572. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  573. $dataSection_NumProps++;
  574. // 4 Section Length
  575. // 4 Property count
  576. // 8 * $dataSection_NumProps (8 = ID (4) + OffSet(4))
  577. $dataSection_Content_Offset = 8 + $dataSection_NumProps * 8;
  578. foreach ($dataSection as $dataProp){
  579. // Summary
  580. $dataSection_Summary .= pack($dataProp['summary']['pack'], $dataProp['summary']['data']);
  581. // Offset
  582. $dataSection_Summary .= pack($dataProp['offset']['pack'], $dataSection_Content_Offset);
  583. // DataType
  584. $dataSection_Content .= pack($dataProp['type']['pack'], $dataProp['type']['data']);
  585. // Data
  586. if($dataProp['type']['data'] == 0x02){ // 2 byte signed integer
  587. $dataSection_Content .= pack('V', $dataProp['data']['data']);
  588. $dataSection_Content_Offset += 4 + 4;
  589. }
  590. elseif($dataProp['type']['data'] == 0x03){ // 4 byte signed integer
  591. $dataSection_Content .= pack('V', $dataProp['data']['data']);
  592. $dataSection_Content_Offset += 4 + 4;
  593. }
  594. elseif($dataProp['type']['data'] == 0x0B){ // Boolean
  595. if($dataProp['data']['data'] == false){
  596. $dataSection_Content .= pack('V', 0x0000);
  597. } else {
  598. $dataSection_Content .= pack('V', 0x0001);
  599. }
  600. $dataSection_Content_Offset += 4 + 4;
  601. }
  602. elseif($dataProp['type']['data'] == 0x1E){ // null-terminated string prepended by dword string length
  603. // Null-terminated string
  604. $dataProp['data']['data'] .= chr(0);
  605. $dataProp['data']['length'] += 1;
  606. // Complete the string with null string for being a %4
  607. $dataProp['data']['length'] = $dataProp['data']['length'] + ((4 - $dataProp['data']['length'] % 4)==4 ? 0 : (4 - $dataProp['data']['length'] % 4));
  608. $dataProp['data']['data'] = str_pad($dataProp['data']['data'], $dataProp['data']['length'], chr(0), STR_PAD_RIGHT);
  609. $dataSection_Content .= pack('V', $dataProp['data']['length']);
  610. $dataSection_Content .= $dataProp['data']['data'];
  611. $dataSection_Content_Offset += 4 + 4 + strlen($dataProp['data']['data']);
  612. }
  613. elseif($dataProp['type']['data'] == 0x40){ // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  614. $dataSection_Content .= $dataProp['data']['data'];
  615. $dataSection_Content_Offset += 4 + 8;
  616. }
  617. else {
  618. // Data Type Not Used at the moment
  619. $dataSection_Content .= $dataProp['data']['data'];
  620. $dataSection_Content_Offset += 4 + $dataProp['data']['length'];
  621. }
  622. }
  623. // Now $dataSection_Content_Offset contains the size of the content
  624. // section header
  625. // offset: $secOffset; size: 4; section length
  626. // + x Size of the content (summary + content)
  627. $data .= pack('V', $dataSection_Content_Offset);
  628. // offset: $secOffset+4; size: 4; property count
  629. $data .= pack('V', $dataSection_NumProps);
  630. // Section Summary
  631. $data .= $dataSection_Summary;
  632. // Section Content
  633. $data .= $dataSection_Content;
  634. return $data;
  635. }
  636. /**
  637. * Build the OLE Part for Summary Information
  638. * @return string
  639. */
  640. private function _writeSummaryInformation(){
  641. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  642. $data = pack('v', 0xFFFE);
  643. // offset: 2; size: 2;
  644. $data .= pack('v', 0x0000);
  645. // offset: 4; size: 2; OS version
  646. $data .= pack('v', 0x0106);
  647. // offset: 6; size: 2; OS indicator
  648. $data .= pack('v', 0x0002);
  649. // offset: 8; size: 16
  650. $data .= pack('VVVV', 0x00, 0x00, 0x00, 0x00);
  651. // offset: 24; size: 4; section count
  652. $data .= pack('V', 0x0001);
  653. // offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
  654. $data .= pack('vvvvvvvv', 0x85E0, 0xF29F, 0x4FF9, 0x1068, 0x91AB, 0x0008, 0x272B, 0xD9B3);
  655. // offset: 44; size: 4; offset of the start
  656. $data .= pack('V', 0x30);
  657. // SECTION
  658. $dataSection = array();
  659. $dataSection_NumProps = 0;
  660. $dataSection_Summary = '';
  661. $dataSection_Content = '';
  662. // CodePage : CP-1252
  663. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x01),
  664. 'offset' => array('pack' => 'V'),
  665. 'type' => array('pack' => 'V', 'data' => 0x02), // 2 byte signed integer
  666. 'data' => array('data' => 1252));
  667. $dataSection_NumProps++;
  668. // Title
  669. if($this->_phpExcel->getProperties()->getTitle()){
  670. $dataProp = $this->_phpExcel->getProperties()->getTitle();
  671. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x02),
  672. 'offset' => array('pack' => 'V'),
  673. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  674. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  675. $dataSection_NumProps++;
  676. }
  677. // Subject
  678. if($this->_phpExcel->getProperties()->getSubject()){
  679. $dataProp = $this->_phpExcel->getProperties()->getSubject();
  680. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x03),
  681. 'offset' => array('pack' => 'V'),
  682. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  683. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  684. $dataSection_NumProps++;
  685. }
  686. // Author (Creator)
  687. if($this->_phpExcel->getProperties()->getCreator()){
  688. $dataProp = $this->_phpExcel->getProperties()->getCreator();
  689. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x04),
  690. 'offset' => array('pack' => 'V'),
  691. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  692. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  693. $dataSection_NumProps++;
  694. }
  695. // Keywords
  696. if($this->_phpExcel->getProperties()->getKeywords()){
  697. $dataProp = $this->_phpExcel->getProperties()->getKeywords();
  698. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x05),
  699. 'offset' => array('pack' => 'V'),
  700. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  701. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  702. $dataSection_NumProps++;
  703. }
  704. // Comments (Description)
  705. if($this->_phpExcel->getProperties()->getDescription()){
  706. $dataProp = $this->_phpExcel->getProperties()->getDescription();
  707. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x06),
  708. 'offset' => array('pack' => 'V'),
  709. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  710. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  711. $dataSection_NumProps++;
  712. }
  713. // Last Saved By (LastModifiedBy)
  714. if($this->_phpExcel->getProperties()->getLastModifiedBy()){
  715. $dataProp = $this->_phpExcel->getProperties()->getLastModifiedBy();
  716. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x08),
  717. 'offset' => array('pack' => 'V'),
  718. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  719. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  720. $dataSection_NumProps++;
  721. }
  722. // Created Date/Time
  723. if($this->_phpExcel->getProperties()->getCreated()){
  724. $dataProp = $this->_phpExcel->getProperties()->getCreated();
  725. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0C),
  726. 'offset' => array('pack' => 'V'),
  727. 'type' => array('pack' => 'V', 'data' => 0x40), // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  728. 'data' => array('data' => PHPExcel_Shared_OLE::LocalDate2OLE($dataProp)));
  729. $dataSection_NumProps++;
  730. }
  731. // Modified Date/Time
  732. if($this->_phpExcel->getProperties()->getModified()){
  733. $dataProp = $this->_phpExcel->getProperties()->getModified();
  734. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0D),
  735. 'offset' => array('pack' => 'V'),
  736. 'type' => array('pack' => 'V', 'data' => 0x40), // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  737. 'data' => array('data' => PHPExcel_Shared_OLE::LocalDate2OLE($dataProp)));
  738. $dataSection_NumProps++;
  739. }
  740. // Security
  741. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x13),
  742. 'offset' => array('pack' => 'V'),
  743. 'type' => array('pack' => 'V', 'data' => 0x03), // 4 byte signed integer
  744. 'data' => array('data' => 0x00));
  745. $dataSection_NumProps++;
  746. // 4 Section Length
  747. // 4 Property count
  748. // 8 * $dataSection_NumProps (8 = ID (4) + OffSet(4))
  749. $dataSection_Content_Offset = 8 + $dataSection_NumProps * 8;
  750. foreach ($dataSection as $dataProp){
  751. // Summary
  752. $dataSection_Summary .= pack($dataProp['summary']['pack'], $dataProp['summary']['data']);
  753. // Offset
  754. $dataSection_Summary .= pack($dataProp['offset']['pack'], $dataSection_Content_Offset);
  755. // DataType
  756. $dataSection_Content .= pack($dataProp['type']['pack'], $dataProp['type']['data']);
  757. // Data
  758. if($dataProp['type']['data'] == 0x02){ // 2 byte signed integer
  759. $dataSection_Content .= pack('V', $dataProp['data']['data']);
  760. $dataSection_Content_Offset += 4 + 4;
  761. }
  762. elseif($dataProp['type']['data'] == 0x03){ // 4 byte signed integer
  763. $dataSection_Content .= pack('V', $dataProp['data']['data']);
  764. $dataSection_Content_Offset += 4 + 4;
  765. }
  766. elseif($dataProp['type']['data'] == 0x1E){ // null-terminated string prepended by dword string length
  767. // Null-terminated string
  768. $dataProp['data']['data'] .= chr(0);
  769. $dataProp['data']['length'] += 1;
  770. // Complete the string with null string for being a %4
  771. $dataProp['data']['length'] = $dataProp['data']['length'] + ((4 - $dataProp['data']['length'] % 4)==4 ? 0 : (4 - $dataProp['data']['length'] % 4));
  772. $dataProp['data']['data'] = str_pad($dataProp['data']['data'], $dataProp['data']['length'], chr(0), STR_PAD_RIGHT);
  773. $dataSection_Content .= pack('V', $dataProp['data']['length']);
  774. $dataSection_Content .= $dataProp['data']['data'];
  775. $dataSection_Content_Offset += 4 + 4 + strlen($dataProp['data']['data']);
  776. }
  777. elseif($dataProp['type']['data'] == 0x40){ // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  778. $dataSection_Content .= $dataProp['data']['data'];
  779. $dataSection_Content_Offset += 4 + 8;
  780. }
  781. else {
  782. // Data Type Not Used at the moment
  783. }
  784. }
  785. // Now $dataSection_Content_Offset contains the size of the content
  786. // section header
  787. // offset: $secOffset; size: 4; section length
  788. // + x Size of the content (summary + content)
  789. $data .= pack('V', $dataSection_Content_Offset);
  790. // offset: $secOffset+4; size: 4; property count
  791. $data .= pack('V', $dataSection_NumProps);
  792. // Section Summary
  793. $data .= $dataSection_Summary;
  794. // Section Content
  795. $data .= $dataSection_Content;
  796. return $data;
  797. }
  798. }