Skip to content

Commit 94b74c2

Browse files
committed
when adding image to relationship list check that the generated relationship id is actually unique
1 parent ab7d822 commit 94b74c2

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file is part of PHPWord - A pure PHP library for reading and writing
45
* word processing documents.
@@ -17,14 +18,14 @@
1718

1819
namespace PhpOffice\PhpWord;
1920

20-
use PhpOffice\PhpWord\Escaper\RegExp;
21-
use PhpOffice\PhpWord\Escaper\Xml;
22-
use PhpOffice\PhpWord\Exception\CopyFileException;
23-
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
24-
use PhpOffice\PhpWord\Exception\Exception;
25-
use PhpOffice\PhpWord\Shared\Text;
26-
use PhpOffice\PhpWord\Shared\XMLWriter;
2721
use PhpOffice\PhpWord\Shared\ZipArchive;
22+
use PhpOffice\PhpWord\Shared\XMLWriter;
23+
use PhpOffice\PhpWord\Shared\Text;
24+
use PhpOffice\PhpWord\Exception\Exception;
25+
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
26+
use PhpOffice\PhpWord\Exception\CopyFileException;
27+
use PhpOffice\PhpWord\Escaper\Xml;
28+
use PhpOffice\PhpWord\Escaper\RegExp;
2829

2930
class TemplateProcessor
3031
{
@@ -620,8 +621,8 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
620621

621622
// collect document parts
622623
$searchParts = array(
623-
$this->getMainPartName() => &$this->tempDocumentMainPart,
624-
);
624+
$this->getMainPartName() => &$this->tempDocumentMainPart,
625+
);
625626
foreach (array_keys($this->tempDocumentHeaders) as $headerIndex) {
626627
$searchParts[$this->getHeaderName($headerIndex)] = &$this->tempDocumentHeaders[$headerIndex];
627628
}
@@ -661,7 +662,7 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
661662
if (preg_match('/(<[^<]+>)([^<]*)(' . preg_quote($varNameWithArgsFixed) . ')([^>]*)(<[^>]+>)/Uu', $partContent, $matches)) {
662663
$wholeTag = $matches[0];
663664
array_shift($matches);
664-
list($openTag, $prefix, , $postfix, $closeTag) = $matches;
665+
list($openTag, $prefix,, $postfix, $closeTag) = $matches;
665666
$replaceXml = $openTag . $prefix . $closeTag . $xmlImage . $openTag . $postfix . $closeTag;
666667
// replace on each iteration, because in one tag we can have 2+ inline variables => before proceed next variable we need to change $partContent
667668
$partContent = $this->setValueForPart($wholeTag, $replaceXml, $partContent, $limit);
@@ -747,8 +748,10 @@ public function cloneRow($search, $numberOfClones)
747748

748749
// If tmpXmlRow doesn't contain continue, this row is no longer part of the spanned row.
749750
$tmpXmlRow = $this->getSlice($extraRowStart, $extraRowEnd);
750-
if (!preg_match('#<w:vMerge/>#', $tmpXmlRow) &&
751-
!preg_match('#<w:vMerge w:val="continue"\s*/>#', $tmpXmlRow)) {
751+
if (
752+
!preg_match('#<w:vMerge/>#', $tmpXmlRow) &&
753+
!preg_match('#<w:vMerge w:val="continue"\s*/>#', $tmpXmlRow)
754+
) {
752755
break;
753756
}
754757
// This row was a spanned row, update $rowEnd and search for the next row.
@@ -1067,7 +1070,11 @@ protected function getRelationsName($documentPartName)
10671070
protected function getNextRelationsIndex($documentPartName)
10681071
{
10691072
if (isset($this->tempDocumentRelations[$documentPartName])) {
1070-
return substr_count($this->tempDocumentRelations[$documentPartName], '<Relationship');
1073+
$candidate = substr_count($this->tempDocumentRelations[$documentPartName], '<Relationship');
1074+
while (strpos($this->tempDocumentRelations[$documentPartName], 'Id="rId' . $candidate . '"') !== false) {
1075+
$candidate++;
1076+
}
1077+
return $candidate;
10711078
}
10721079

10731080
return 1;

0 commit comments

Comments
 (0)