From 7aeea726e3074ff8976d2a3e0f1c33ba687b8ec0 Mon Sep 17 00:00:00 2001 From: Aderson Silva Date: Wed, 9 Jul 2025 13:26:21 -0300 Subject: [PATCH 1/4] fix: loader temp footer and header loader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a document started its header or footer numbering at a value greater than 1, it caused an error, and it also failed when the indices were non-sequential—for example: header2, header6 --- src/PhpWord/TemplateProcessor.php | 37 ++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 073393ffc4..7b2ca0713c 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -120,16 +120,9 @@ public function __construct($documentTemplate) // Temporary document content extraction $this->zipClass = new ZipArchive(); $this->zipClass->open($this->tempDocumentFilename); - $index = 1; - while (false !== $this->zipClass->locateName($this->getHeaderName($index))) { - $this->tempDocumentHeaders[$index] = $this->readPartWithRels($this->getHeaderName($index)); - ++$index; - } - $index = 1; - while (false !== $this->zipClass->locateName($this->getFooterName($index))) { - $this->tempDocumentFooters[$index] = $this->readPartWithRels($this->getFooterName($index)); - ++$index; - } + + $this->tempDocumentHeaders = $this->loadDocumentParts('word/header'); + $this->tempDocumentFooters = $this->loadDocumentParts('word/footer'); $this->tempDocumentMainPart = $this->readPartWithRels($this->getMainPartName()); $this->tempDocumentSettingsPart = $this->readPartWithRels($this->getSettingsPartName()); @@ -1504,4 +1497,28 @@ public function getTempDocumentFilename(): string { return $this->tempDocumentFilename; } + + /** + * Check existing files inside zip from prefix + * + * @param string $prefix + * @return array + */ + private function loadDocumentParts(string $prefix): array + { + $parts = []; + $total = $this->zipClass->numFiles; + + for ($i = 0; $i < $total; ++$i) { + $name = $this->zipClass->getNameIndex($i); + + if (preg_match('#^' . preg_quote($prefix, '#') . '(\d+)\.xml$#', $name, $m)) { + $idx = (int) $m[1]; + $parts[$idx] = $this->readPartWithRels($name); + } + } + + ksort($parts); + return $parts; + } } From 7f729c1756242e76725da96ceb123052d3a6ef8c Mon Sep 17 00:00:00 2001 From: Aderson Silva Date: Wed, 9 Jul 2025 13:32:27 -0300 Subject: [PATCH 2/4] fix: . to test --- src/PhpWord/TemplateProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 7b2ca0713c..c490d59520 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -1499,7 +1499,7 @@ public function getTempDocumentFilename(): string } /** - * Check existing files inside zip from prefix + * Check existing files inside zip from prefix. * * @param string $prefix * @return array From 0019500339714120d02b3e301b381037f83f4660 Mon Sep 17 00:00:00 2001 From: Aderson Silva Date: Wed, 9 Jul 2025 13:35:58 -0300 Subject: [PATCH 3/4] fix: php-cs-fixer --- src/PhpWord/TemplateProcessor.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index c490d59520..817d731b89 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -1500,9 +1500,6 @@ public function getTempDocumentFilename(): string /** * Check existing files inside zip from prefix. - * - * @param string $prefix - * @return array */ private function loadDocumentParts(string $prefix): array { From 46c786afec89d8055505c3c3dff0fd70baab254b Mon Sep 17 00:00:00 2001 From: Aderson Silva Date: Wed, 9 Jul 2025 13:38:25 -0300 Subject: [PATCH 4/4] fix: space in ksort --- src/PhpWord/TemplateProcessor.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 817d731b89..bb97329264 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -1516,6 +1516,7 @@ private function loadDocumentParts(string $prefix): array } ksort($parts); + return $parts; } }