diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ce722c4..ce553f05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ v0.16.0 (xx xxx 2018) - Fix regex in `cloneBlock` function @nicoder #1269 - HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436 - RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493 +- Fix parsing of Office 365 documents @Timanx #1485 v0.15.0 (14 Jul 2018) ---------------------- diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 7a5eaf55..95468878 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -503,11 +503,19 @@ class TemplateProcessor } /** + * Usually, the name of main part document will be 'document.xml'. However, some .docx files (possibly those from Office 365, experienced also on documents from Word Online created from blank templates) have file 'document22.xml' in their zip archive instead of 'document.xml'. This method searches content types file to correctly determine the file name. + * * @return string */ protected function getMainPartName() { - return 'word/document.xml'; + $contentTypes = $this->zipClass->getFromName('[Content_Types].xml'); + + $pattern = '~PartName="\/(word\/document.*?\.xml)" ContentType="application\/vnd\.openxmlformats-officedocument\.wordprocessingml\.document\.main\+xml"~'; + + preg_match($pattern, $contentTypes, $matches); + + return array_key_exists(1, $matches) ? $matches[1] : 'word/document.xml'; } /** diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index ea739561..1513486e 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -276,4 +276,13 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase ); } } + + public function testMainPartNameDetection() + { + $templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx'); + + $variables = array('test'); + + $this->assertEquals($variables, $templateProcessor->getVariables()); + } } diff --git a/tests/PhpWord/_files/templates/document22-xml.docx b/tests/PhpWord/_files/templates/document22-xml.docx new file mode 100644 index 00000000..206d80f4 Binary files /dev/null and b/tests/PhpWord/_files/templates/document22-xml.docx differ