nfTlWNl rn Ѻq$³*4A8t+SjįQ&VY{ V~PQ?ltjdLa ͐p'G*!gؑ8@B_#1Iķխ}`[W">/m6@/yzk Ѻq$³*n[^O"_L0\vJrr/M&oAY\QN#]>H7Kh>/. vu˼ƒPQ?ltjd劏^0 <]W*d iԗK ቓPQ?ltjd6O<> \]֔|)Y?"#XbkOydmaL_v xV8U&wBʿu6M@T}8ɽdD]f'މb1ʖy"W-ZB!jrݑ 0#rҧnBK|%\`Lٔ*ܝIc X0q۶٘aTS#m$ٛ>qvUfXTFr?bL_v xV8U&wBʿu6HTmOpo5kh\/S,_JRnPQ?ltjdˣƨ _ &!XQ~qdDފ? \ìPW #GrJsf]ZV hgrP>ٴK=3ͮe?&O HRC%`u}m`xy23yX)_#1Iķխ}`%1C*JBqߓ Rdku S%VX7*s!*XfcfܚaMqRIӵOtⓙ}]w&]TDS׎#WU x$`ʲ#| CӝINgꑥPMӜ(wľ7|6j +=}gn&aIYuwp0N5CyQc!9\`Lٔ*ܝIc.3E`tD]}Na<ᰉmy>B;=(y?5lDuJ>״ggn&aIYu-3UNՔuJ>״ggn&aIYuKY4D2ϹE+[07væGrJsf]ZV hX+E}4=H}Q -j847 GrJsf]ZV hX+E}4=H}{bG7׮֔|քQ LR0:zSѴ#> UХm{c.5 P -$ݭjJ(mYvn4zŇ/gָRneNjl.>+y#Wn*P:w90;xI˫Ί1*4KQբ+a\!ULUdDG^5]/f&n-QG+Ålj9T'%; wOXEIj4K8M+N4f3b(۲#q`d|ԧ@>0;(YGm$}\ҎXVCʜTIjӊRT  4*PwXq3r$cl&N c7riat(۲#q`d|ԧ"H$] r(YQFW^X[GI"_[23 [*[&p=gG ,r?UL5oʖ9 к{AG@˾;=/u_u4 (ynn9Z=XpG[. ģǙH3!8r8yo:'5pn_ Eƨ_EMd"6[`~FXco(Z]\j)OkLds]Mq [zsȫJƃA_WN(۲#q`d|ԧ0{Ų3чoZ6ޑ/]oxQڈW7E+3j%+HFdGTSM6d >0ιKwy MM-Q_[۝*^7//8¬U_C?8X9Udx)^y mmף"S.# bSOB{ PITBVo$%Gcv/whBe7x0x@jWn P}>*^HNlK };D7|rOֳݛtx^p 'CZ!x駬)^f&gNVWD%(NVS̨{'OQDZu*Ne6lUr)j.܋J}UT$s:T}߂IdvdG!jCym^W1)s[pFŇ4_zE]d;߾Ÿ iFA2ev@MKx~s $HѦ֧f:ǹ#eD2עe1Md|>L ^b@߂DUAuU=TI ښI]}-ݹUr.!zױyG}1N<5)LϝJQHr_A– `BGQCSM-̎<_lOJ3҃"?Bmt0((֑l_2X8opܩmh nZX_ؗi磼s\6qJN1&x&!vNh=\a5 'g,Y%/S-iq  .A!KZ=3;`RT,($xDF??2 hh58eQvg{dqg!BզL©P+Tꩻp=P[)[cZd4ζ<kƜ9Ѿ|2֕1Nap`$&lͣtnLj>7DFH 8.5ẚJ31Gރbdr?gHĚQ9,_P0i-2 'D?pcG$ T݅gq\޶"j$h滊K?[z&M@`B̞Χ#yg0U%= Gzy7:%PDڞaD:6Չ|F $9NݭnVcu\80k9t Kī%`0KAsaC .1?I;2YY8w׶1utP!I"Gm3pb52K #_ ;؟P^R7p]O!C'j-{ZBNN/P(ꮷjEP~'3TN(_A/ݜ(A)ޭk#`5*~IBglr6m-ejԸۡhRM>L80k9t K%HC p)); } // build Escher objects. Escher objects for workbooks needs to be build before Escher object for workbook. $this->buildWorksheetEschers(); $this->buildWorkbookEscher(); // add 15 identical cell style Xfs // for now, we use the first cellXf instead of cellStyleXf $cellXfCollection = $this->spreadsheet->getCellXfCollection(); for ($i = 0; $i < 15; ++$i) { $this->writerWorkbook->addXfWriter($cellXfCollection[0], true); } // add all the cell Xfs foreach ($this->spreadsheet->getCellXfCollection() as $style) { $this->writerWorkbook->addXfWriter($style, false); } // add fonts from rich text eleemnts for ($i = 0; $i < $countSheets; ++$i) { foreach ($this->writerWorksheets[$i]->phpSheet->getCellCollection()->getCoordinates() as $coordinate) { /** @var Cell $cell */ $cell = $this->writerWorksheets[$i]->phpSheet->getCellCollection()->get($coordinate); $cVal = $cell->getValue(); if ($cVal instanceof RichText) { $elements = $cVal->getRichTextElements(); foreach ($elements as $element) { if ($element instanceof Run) { $font = $element->getFont(); if ($font !== null) { $this->writerWorksheets[$i]->fontHashIndex[$font->getHashCode()] = $this->writerWorkbook->addFont($font); } } } } } } // initialize OLE file $workbookStreamName = 'Workbook'; $OLE = new File(OLE::ascToUcs($workbookStreamName)); // Write the worksheet streams before the global workbook stream, // because the byte sizes of these are needed in the global workbook stream $worksheetSizes = []; for ($i = 0; $i < $countSheets; ++$i) { $this->writerWorksheets[$i]->close(); $worksheetSizes[] = $this->writerWorksheets[$i]->_datasize; } // add binary data for global workbook stream $OLE->append($this->writerWorkbook->writeWorkbook($worksheetSizes)); // add binary data for sheet streams for ($i = 0; $i < $countSheets; ++$i) { $OLE->append($this->writerWorksheets[$i]->getData()); } $this->documentSummaryInformation = $this->writeDocumentSummaryInformation(); // initialize OLE Document Summary Information if (!empty($this->documentSummaryInformation)) { $OLE_DocumentSummaryInformation = new File(OLE::ascToUcs(chr(5) . 'DocumentSummaryInformation')); $OLE_DocumentSummaryInformation->append($this->documentSummaryInformation); } $this->summaryInformation = $this->writeSummaryInformation(); // initialize OLE Summary Information if (!empty($this->summaryInformation)) { $OLE_SummaryInformation = new File(OLE::ascToUcs(chr(5) . 'SummaryInformation')); $OLE_SummaryInformation->append($this->summaryInformation); } // define OLE Parts $arrRootData = [$OLE]; // initialize OLE Properties file if (isset($OLE_SummaryInformation)) { $arrRootData[] = $OLE_SummaryInformation; } // initialize OLE Extended Properties file if (isset($OLE_DocumentSummaryInformation)) { $arrRootData[] = $OLE_DocumentSummaryInformation; } $time = $this->spreadsheet->getProperties()->getModified(); $root = new Root($time, $time, $arrRootData); // save the OLE file $this->openFileHandle($filename); $root->save($this->fileHandle); $this->maybeCloseFileHandle(); Functions::setReturnDateType($saveDateReturnType); Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); } /** * Build the Worksheet Escher objects. */ private function buildWorksheetEschers(): void { // 1-based index to BstoreContainer $blipIndex = 0; $lastReducedSpId = 0; $lastSpId = 0; foreach ($this->spreadsheet->getAllsheets() as $sheet) { // sheet index $sheetIndex = $sheet->getParentOrThrow()->getIndex($sheet); // check if there are any shapes for this sheet $filterRange = $sheet->getAutoFilter()->getRange(); if (count($sheet->getDrawingCollection()) == 0 && empty($filterRange)) { continue; } // create intermediate Escher object $escher = new Escher(); // dgContainer $dgContainer = new DgContainer(); // set the drawing index (we use sheet index + 1) $dgId = $sheet->getParentOrThrow()->getIndex($sheet) + 1; $dgContainer->setDgId($dgId); $escher->setDgContainer($dgContainer); // spgrContainer $spgrContainer = new SpgrContainer(); $dgContainer->setSpgrContainer($spgrContainer); // add one shape which is the group shape $spContainer = new SpContainer(); $spContainer->setSpgr(true); $spContainer->setSpType(0); $spContainer->setSpId(($sheet->getParentOrThrow()->getIndex($sheet) + 1) << 10); $spgrContainer->addChild($spContainer); // add the shapes $countShapes[$sheetIndex] = 0; // count number of shapes (minus group shape), in sheet foreach ($sheet->getDrawingCollection() as $drawing) { ++$blipIndex; ++$countShapes[$sheetIndex]; // add the shape $spContainer = new SpContainer(); // set the shape type $spContainer->setSpType(0x004B); // set the shape flag $spContainer->setSpFlag(0x02); // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index) $reducedSpId = $countShapes[$sheetIndex]; $spId = $reducedSpId | ($sheet->getParentOrThrow()->getIndex($sheet) + 1) << 10; $spContainer->setSpId($spId); // keep track of last reducedSpId $lastReducedSpId = $reducedSpId; // keep track of last spId $lastSpId = $spId; // set the BLIP index $spContainer->setOPT(0x4104, $blipIndex); // set coordinates and offsets, client anchor $coordinates = $drawing->getCoordinates(); $offsetX = $drawing->getOffsetX(); $offsetY = $drawing->getOffsetY(); $width = $drawing->getWidth(); $height = $drawing->getHeight(); $twoAnchor = \PhpOffice\PhpSpreadsheet\Shared\Xls::oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height); if (is_array($twoAnchor)) { $spContainer->setStartCoordinates($twoAnchor['startCoordinates']); $spContainer->setStartOffsetX($twoAnchor['startOffsetX']); $spContainer->setStartOffsetY($twoAnchor['startOffsetY']); $spContainer->setEndCoordinates($twoAnchor['endCoordinates']); $spContainer->setEndOffsetX($twoAnchor['endOffsetX']); $spContainer->setEndOffsetY($twoAnchor['endOffsetY']); $spgrContainer->addChild($spContainer); } } // AutoFilters if (!empty($filterRange)) { $rangeBounds = Coordinate::rangeBoundaries($filterRange); $iNumColStart = $rangeBounds[0][0]; $iNumColEnd = $rangeBounds[1][0]; $iInc = $iNumColStart; while ($iInc <= $iNumColEnd) { ++$countShapes[$sheetIndex]; // create an Drawing Object for the dropdown $oDrawing = new BaseDrawing(); // get the coordinates of drawing $cDrawing = Coordinate::stringFromColumnIndex($iInc) . $rangeBounds[0][1]; $oDrawing->setCoordinates($cDrawing); $oDrawing->setWorksheet($sheet); // add the shape $spContainer = new SpContainer(); // set the shape type $spContainer->setSpType(0x00C9); // set the shape flag $spContainer->setSpFlag(0x01); // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index) $reducedSpId = $countShapes[$sheetIndex]; $spId = $reducedSpId | ($sheet->getParentOrThrow()->getIndex($sheet) + 1) << 10; $spContainer->setSpId($spId); // keep track of last reducedSpId $lastReducedSpId = $reducedSpId; // keep track of last spId $lastSpId = $spId; $spContainer->setOPT(0x007F, 0x01040104); // Protection -> fLockAgainstGrouping $spContainer->setOPT(0x00BF, 0x00080008); // Text -> fFitTextToShape $spContainer->setOPT(0x01BF, 0x00010000); // Fill Style -> fNoFillHitTest $spContainer->setOPT(0x01FF, 0x00080000); // Line Style -> fNoLineDrawDash $spContainer->setOPT(0x03BF, 0x000A0000); // Group Shape -> fPrint // set coordinates and offsets, client anchor $endCoordinates = Coordinate::stringFromColumnIndex($iInc); $endCoordinates .= $rangeBounds[0][1] + 1; $spContainer->setStartCoordinates($cDrawing); $spContainer->setStartOffsetX(0); $spContainer->setStartOffsetY(0); $spContainer->setEndCoordinates($endCoordinates); $spContainer->setEndOffsetX(0); $spContainer->setEndOffsetY(0); $spgrContainer->addChild($spContainer); ++$iInc; } } // identifier clusters, used for workbook Escher object $this->IDCLs[$dgId] = $lastReducedSpId; // set last shape index $dgContainer->setLastSpId($lastSpId); // set the Escher object $this->writerWorksheets[$sheetIndex]->setEscher($escher); } } private function processMemoryDrawing(BstoreContainer &$bstoreContainer, MemoryDrawing $drawing, string $renderingFunctionx): void { switch ($renderingFunctionx) { case MemoryDrawing::RENDERING_JPEG: $blipType = BSE::BLIPTYPE_JPEG; $renderingFunction = 'imagejpeg'; break; default: $blipType = BSE::BLIPTYPE_PNG; $renderingFunction = 'imagepng'; break; } ob_start(); call_user_func($renderingFunction, $drawing->getImageResource()); $blipData = ob_get_contents(); ob_end_clean(); $blip = new Blip(); $blip->setData("$blipData"); $BSE = new BSE(); $BSE->setBlipType($blipType); $BSE->setBlip($blip); $bstoreContainer->addBSE($BSE); } private function processDrawing(BstoreContainer &$bstoreContainer, Drawing $drawing): void { $blipType = 0; $blipData = ''; $filename = $drawing->getPath(); $imageSize = getimagesize($filename); $imageFormat = empty($imageSize) ? 0 : ($imageSize[2] ?? 0); switch ($imageFormat) { case 1: // GIF, not supported by BIFF8, we convert to PNG $blipType = BSE::BLIPTYPE_PNG; $newImage = @imagecreatefromgif($filename); if ($newImage === false) { throw new Exception("Unable to create image from $filename"); } ob_start(); imagepng($newImage); $blipData = ob_get_contents(); ob_end_clean(); break; case 2: // JPEG $blipType = BSE::BLIPTYPE_JPEG; $blipData = file_get_contents($filename); break; case 3: // PNG $blipType = BSE::BLIPTYPE_PNG; $blipData = file_get_contents($filename); break; case 6: // Windows DIB (BMP), we convert to PNG $blipType = BSE::BLIPTYPE_PNG; $newImage = @imagecreatefrombmp($filename); if ($newImage === false) { throw new Exception("Unable to create image from $filename"); } ob_start(); imagepng($newImage); $blipData = ob_get_contents(); ob_end_clean(); break; } if ($blipData) { $blip = new Blip(); $blip->setData($blipData); $BSE = new BSE(); $BSE->setBlipType($blipType); $BSE->setBlip($blip); $bstoreContainer->addBSE($BSE); } } private function processBaseDrawing(BstoreContainer &$bstoreContainer, BaseDrawing $drawing): void { if ($drawing instanceof Drawing) { $this->processDrawing($bstoreContainer, $drawing); } elseif ($drawing instanceof MemoryDrawing) { $this->processMemoryDrawing($bstoreContainer, $drawing, $drawing->getRenderingFunction()); } } private function checkForDrawings(): bool { // any drawings in this workbook? $found = false; foreach ($this->spreadsheet->getAllSheets() as $sheet) { if (count($sheet->getDrawingCollection()) > 0) { $found = true; break; } } return $found; } /** * Build the Escher object corresponding to the MSODRAWINGGROUP record. */ private function buildWorkbookEscher(): void { // nothing to do if there are no drawings if (!$this->checkForDrawings()) { return; } // if we reach here, then there are drawings in the workbook $escher = new Escher(); // dggContainer $dggContainer = new DggContainer(); $escher->setDggContainer($dggContainer); // set IDCLs (identifier clusters) $dggContainer->setIDCLs($this->IDCLs); // this loop is for determining maximum shape identifier of all drawing $spIdMax = 0; $totalCountShapes = 0; $countDrawings = 0; foreach ($this->spreadsheet->getAllsheets() as $sheet) { $sheetCountShapes = 0; // count number of shapes (minus group shape), in sheet $addCount = 0; foreach ($sheet->getDrawingCollection() as $drawing) { $addCount = 1; ++$sheetCountShapes; ++$totalCountShapes; $spId = $sheetCountShapes | ($this->spreadsheet->getIndex($sheet) + 1) << 10; $spIdMax = max($spId, $spIdMax); } $countDrawings += $addCount; } $dggContainer->setSpIdMax($spIdMax + 1); $dggContainer->setCDgSaved($countDrawings); $dggContainer->setCSpSaved($totalCountShapes + $countDrawings); // total number of shapes incl. one group shapes per drawing // bstoreContainer $bstoreContainer = new BstoreContainer(); $dggContainer->setBstoreContainer($bstoreContainer); // the BSE's (all the images) foreach ($this->spreadsheet->getAllsheets() as $sheet) { foreach ($sheet->getDrawingCollection() as $drawing) { $this->processBaseDrawing($bstoreContainer, $drawing); } } // Set the Escher object $this->writerWorkbook->setEscher($escher); } /** * Build the OLE Part for DocumentSummary Information. * * @return string */ private function writeDocumentSummaryInformation() { // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark) $data = pack('v', 0xFFFE); // offset: 2; size: 2; $data .= pack('v', 0x0000); // offset: 4; size: 2; OS version $data .= pack('v', 0x0106); // offset: 6; size: 2; OS indicator $data .= pack('v', 0x0002); // offset: 8; size: 16 $data .= pack('VVVV', 0x00, 0x00, 0x00, 0x00); // offset: 24; size: 4; section count $data .= pack('V', 0x0001); // 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 $data .= pack('vvvvvvvv', 0xD502, 0xD5CD, 0x2E9C, 0x101B, 0x9793, 0x0008, 0x2C2B, 0xAEF9); // offset: 44; size: 4; offset of the start $data .= pack('V', 0x30); // SECTION $dataSection = []; $dataSection_NumProps = 0; $dataSection_Summary = ''; $dataSection_Content = ''; // GKPIDDSI_CODEPAGE: CodePage $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x01], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x02], // 2 byte signed integer 'data' => ['data' => 1252], ]; ++$dataSection_NumProps; // GKPIDDSI_CATEGORY : Category $dataProp = $this->spreadsheet->getProperties()->getCategory(); if ($dataProp) { $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x02], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x1E], 'data' => ['data' => $dataProp, 'length' => strlen($dataProp)], ]; ++$dataSection_NumProps; } // GKPIDDSI_VERSION :Version of the application that wrote the property storage $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x17], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x03], 'data' => ['pack' => 'V', 'data' => 0x000C0000], ]; ++$dataSection_NumProps; // GKPIDDSI_SCALE : FALSE $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x0B], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x0B], 'data' => ['data' => false], ]; ++$dataSection_NumProps; // GKPIDDSI_LINKSDIRTY : True if any of the values for the linked properties have changed outside of the application $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x10], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x0B], 'data' => ['data' => false], ]; ++$dataSection_NumProps; // GKPIDDSI_SHAREDOC : FALSE $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x13], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x0B], 'data' => ['data' => false], ]; ++$dataSection_NumProps; // GKPIDDSI_HYPERLINKSCHANGED : True if any of the values for the _PID_LINKS (hyperlink text) have changed outside of the application $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x16], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x0B], 'data' => ['data' => false], ]; ++$dataSection_NumProps; // GKPIDDSI_DOCSPARTS // MS-OSHARED p75 (2.3.3.2.2.1) // Structure is VtVecUnalignedLpstrValue (2.3.3.1.9) // cElements $dataProp = pack('v', 0x0001); $dataProp .= pack('v', 0x0000); // array of UnalignedLpstr // cch $dataProp .= pack('v', 0x000A); $dataProp .= pack('v', 0x0000); // value $dataProp .= 'Worksheet' . chr(0); $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x0D], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x101E], 'data' => ['data' => $dataProp, 'length' => strlen($dataProp)], ]; ++$dataSection_NumProps; // GKPIDDSI_HEADINGPAIR // VtVecHeadingPairValue // cElements $dataProp = pack('v', 0x0002); $dataProp .= pack('v', 0x0000); // Array of vtHeadingPair // vtUnalignedString - headingString // stringType $dataProp .= pack('v', 0x001E); // padding $dataProp .= pack('v', 0x0000); // UnalignedLpstr // cch $dataProp .= pack('v', 0x0013); $dataProp .= pack('v', 0x0000); // value $dataProp .= 'Feuilles de calcul'; // vtUnalignedString - headingParts // wType : 0x0003 = 32 bit signed integer $dataProp .= pack('v', 0x0300); // padding $dataProp .= pack('v', 0x0000); // value $dataProp .= pack('v', 0x0100); $dataProp .= pack('v', 0x0000); $dataProp .= pack('v', 0x0000); $dataProp .= pack('v', 0x0000); $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x0C], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x100C], 'data' => ['data' => $dataProp, 'length' => strlen($dataProp)], ]; ++$dataSection_NumProps; // 4 Section Length // 4 Property count // 8 * $dataSection_NumProps (8 = ID (4) + OffSet(4)) $dataSection_Content_Offset = 8 + $dataSection_NumProps * 8; foreach ($dataSection as $dataProp) { // Summary $dataSection_Summary .= pack($dataProp['summary']['pack'], $dataProp['summary']['data']); // Offset $dataSection_Summary .= pack($dataProp['offset']['pack'], $dataSection_Content_Offset); // DataType $dataSection_Content .= pack($dataProp['type']['pack'], $dataProp['type']['data']); // Data if ($dataProp['type']['data'] == 0x02) { // 2 byte signed integer $dataSection_Content .= pack('V', $dataProp['data']['data']); $dataSection_Content_Offset += 4 + 4; } elseif ($dataProp['type']['data'] == 0x03) { // 4 byte signed integer $dataSection_Content .= pack('V', $dataProp['data']['data']); $dataSection_Content_Offset += 4 + 4; } elseif ($dataProp['type']['data'] == 0x0B) { // Boolean $dataSection_Content .= pack('V', (int) $dataProp['data']['data']); $dataSection_Content_Offset += 4 + 4; } elseif ($dataProp['type']['data'] == 0x1E) { // null-terminated string prepended by dword string length // Null-terminated string $dataProp['data']['data'] .= chr(0); ++$dataProp['data']['length']; // Complete the string with null string for being a %4 $dataProp['data']['length'] = $dataProp['data']['length'] + ((4 - $dataProp['data']['length'] % 4) == 4 ? 0 : (4 - $dataProp['data']['length'] % 4)); $dataProp['data']['data'] = str_pad($dataProp['data']['data'], $dataProp['data']['length'], chr(0), STR_PAD_RIGHT); $dataSection_Content .= pack('V', $dataProp['data']['length']); $dataSection_Content .= $dataProp['data']['data']; $dataSection_Content_Offset += 4 + 4 + strlen($dataProp['data']['data']); /* Condition below can never be true } elseif ($dataProp['type']['data'] == 0x40) { // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601) $dataSection_Content .= $dataProp['data']['data']; $dataSection_Content_Offset += 4 + 8; */ } else { $dataSection_Content .= $dataProp['data']['data']; $dataSection_Content_Offset += 4 + $dataProp['data']['length']; } } // Now $dataSection_Content_Offset contains the size of the content // section header // offset: $secOffset; size: 4; section length // + x Size of the content (summary + content) $data .= pack('V', $dataSection_Content_Offset); // offset: $secOffset+4; size: 4; property count $data .= pack('V', $dataSection_NumProps); // Section Summary $data .= $dataSection_Summary; // Section Content $data .= $dataSection_Content; return $data; } /** * @param float|int $dataProp */ private function writeSummaryPropOle($dataProp, int &$dataSection_NumProps, array &$dataSection, int $sumdata, int $typdata): void { if ($dataProp) { $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => $sumdata], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => $typdata], // null-terminated string prepended by dword string length 'data' => ['data' => OLE::localDateToOLE($dataProp)], ]; ++$dataSection_NumProps; } } private function writeSummaryProp(string $dataProp, int &$dataSection_NumProps, array &$dataSection, int $sumdata, int $typdata): void { if ($dataProp) { $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => $sumdata], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => $typdata], // null-terminated string prepended by dword string length 'data' => ['data' => $dataProp, 'length' => strlen($dataProp)], ]; ++$dataSection_NumProps; } } /** * Build the OLE Part for Summary Information. * * @return string */ private function writeSummaryInformation() { // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark) $data = pack('v', 0xFFFE); // offset: 2; size: 2; $data .= pack('v', 0x0000); // offset: 4; size: 2; OS version $data .= pack('v', 0x0106); // offset: 6; size: 2; OS indicator $data .= pack('v', 0x0002); // offset: 8; size: 16 $data .= pack('VVVV', 0x00, 0x00, 0x00, 0x00); // offset: 24; size: 4; section count $data .= pack('V', 0x0001); // 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 $data .= pack('vvvvvvvv', 0x85E0, 0xF29F, 0x4FF9, 0x1068, 0x91AB, 0x0008, 0x272B, 0xD9B3); // offset: 44; size: 4; offset of the start $data .= pack('V', 0x30); // SECTION $dataSection = []; $dataSection_NumProps = 0; $dataSection_Summary = ''; $dataSection_Content = ''; // CodePage : CP-1252 $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x01], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x02], // 2 byte signed integer 'data' => ['data' => 1252], ]; ++$dataSection_NumProps; $props = $this->spreadsheet->getProperties(); $this->writeSummaryProp($props->getTitle(), $dataSection_NumProps, $dataSection, 0x02, 0x1e); $this->writeSummaryProp($props->getSubject(), $dataSection_NumProps, $dataSection, 0x03, 0x1e); $this->writeSummaryProp($props->getCreator(), $dataSection_NumProps, $dataSection, 0x04, 0x1e); $this->writeSummaryProp($props->getKeywords(), $dataSection_NumProps, $dataSection, 0x05, 0x1e); $this->writeSummaryProp($props->getDescription(), $dataSection_NumProps, $dataSection, 0x06, 0x1e); $this->writeSummaryProp($props->getLastModifiedBy(), $dataSection_NumProps, $dataSection, 0x08, 0x1e); $this->writeSummaryPropOle($props->getCreated(), $dataSection_NumProps, $dataSection, 0x0c, 0x40); $this->writeSummaryPropOle($props->getModified(), $dataSection_NumProps, $dataSection, 0x0d, 0x40); // Security $dataSection[] = [ 'summary' => ['pack' => 'V', 'data' => 0x13], 'offset' => ['pack' => 'V'], 'type' => ['pack' => 'V', 'data' => 0x03], // 4 byte signed integer 'data' => ['data' => 0x00], ]; ++$dataSection_NumProps; // 4 Section Length // 4 Property count // 8 * $dataSection_NumProps (8 = ID (4) + OffSet(4)) $dataSection_Content_Offset = 8 + $dataSection_NumProps * 8; foreach ($dataSection as $dataProp) { // Summary $dataSection_Summary .= pack($dataProp['summary']['pack'], $dataProp['summary']['data']); // Offset $dataSection_Summary .= pack($dataProp['offset']['pack'], $dataSection_Content_Offset); // DataType $dataSection_Content .= pack($dataProp['type']['pack'], $dataProp['type']['data']); // Data if ($dataProp['type']['data'] == 0x02) { // 2 byte signed integer $dataSection_Content .= pack('V', $dataProp['data']['data']); $dataSection_Content_Offset += 4 + 4; } elseif ($dataProp['type']['data'] == 0x03) { // 4 byte signed integer $dataSection_Content .= pack('V', $dataProp['data']['data']); $dataSection_Content_Offset += 4 + 4; } elseif ($dataProp['type']['data'] == 0x1E) { // null-terminated string prepended by dword string length // Null-terminated string $dataProp['data']['data'] .= chr(0); ++$dataProp['data']['length']; // Complete the string with null string for being a %4 $dataProp['data']['length'] = $dataProp['data']['length'] + ((4 - $dataProp['data']['length'] % 4) == 4 ? 0 : (4 - $dataProp['data']['length'] % 4)); $dataProp['data']['data'] = str_pad($dataProp['data']['data'], $dataProp['data']['length'], chr(0), STR_PAD_RIGHT); $dataSection_Content .= pack('V', $dataProp['data']['length']); $dataSection_Content .= $dataProp['data']['data']; $dataSection_Content_Offset += 4 + 4 + strlen($dataProp['data']['data']); } elseif ($dataProp['type']['data'] == 0x40) { // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601) $dataSection_Content .= $dataProp['data']['data']; $dataSection_Content_Offset += 4 + 8; } // Data Type Not Used at the moment } // Now $dataSection_Content_Offset contains the size of the content // section header // offset: $secOffset; size: 4; section length // + x Size of the content (summary + content) $data .= pack('V', $dataSection_Content_Offset); // offset: $secOffset+4; size: 4; property count $data .= pack('V', $dataSection_NumProps); // Section Summary $data .= $dataSection_Summary; // Section Content $data .= $dataSection_Content; return $data; } }