From 954863ef16b97c661b653dbfab4b8f9b2980e93e Mon Sep 17 00:00:00 2001 From: "michal.schroeder" Date: Fri, 30 May 2025 13:17:20 +0200 Subject: [PATCH 1/3] Enhance error handling in encoding functions and update TOC test with static HTML content --- src/PhpWord/Shared/Microsoft/PasswordEncoder.php | 7 ++++++- src/PhpWord/Shared/Text.php | 5 +++++ tests/bootstrap.php | 4 +++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Shared/Microsoft/PasswordEncoder.php b/src/PhpWord/Shared/Microsoft/PasswordEncoder.php index 45f9a53632..4762cc7104 100644 --- a/src/PhpWord/Shared/Microsoft/PasswordEncoder.php +++ b/src/PhpWord/Shared/Microsoft/PasswordEncoder.php @@ -18,6 +18,8 @@ namespace PhpOffice\PhpWord\Shared\Microsoft; +use PhpOffice\PhpWord\Exception\Exception; + /** * Password encoder for microsoft office applications. */ @@ -119,8 +121,11 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_ // Get the single-byte values by iterating through the Unicode characters of the truncated password. // For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte. $passUtf8 = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8'); - $byteChars = []; + if (!is_string($passUtf8)) { + throw new Exception('Failed to convert password to UCS-2LE'); + } + $byteChars = []; for ($i = 0; $i < mb_strlen($password); ++$i) { $byteChars[$i] = ord(substr($passUtf8, $i * 2, 1)); diff --git a/src/PhpWord/Shared/Text.php b/src/PhpWord/Shared/Text.php index 90550c0650..251764b3dd 100644 --- a/src/PhpWord/Shared/Text.php +++ b/src/PhpWord/Shared/Text.php @@ -18,6 +18,8 @@ namespace PhpOffice\PhpWord\Shared; +use PhpOffice\PhpWord\Exception\Exception; + /** * Text. */ @@ -148,6 +150,9 @@ public static function toUTF8($value = '') if (null !== $value && !self::isUTF8($value)) { // PHP8.2 : utf8_encode is deprecated, but mb_convert_encoding always usable $value = (function_exists('mb_convert_encoding')) ? mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1') : utf8_encode($value); + if ($value === false) { + throw new Exception('Unable to convert text to UTF-8'); + } } return $value; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7c4e0a3e1b..f9e0ca2388 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -54,7 +54,9 @@ function phpunit10ErrorHandler(int $errno, string $errstr, string $filename, int function utf8decode(string $value, string $toEncoding = 'ISO-8859-1'): string { - return function_exists('mb_convert_encoding') ? mb_convert_encoding($value, $toEncoding, 'UTF-8') : utf8_decode($value); + $result = function_exists('mb_convert_encoding') ? mb_convert_encoding($value, $toEncoding, 'UTF-8') : utf8_decode($value); + + return $result === false ? '' : $result; } if (!method_exists(PHPUnit\Framework\TestCase::class, 'setOutputCallback')) { From 4dfe9007061f19ba2129383b632fbdc09f03adb3 Mon Sep 17 00:00:00 2001 From: "michal.schroeder" Date: Fri, 30 May 2025 13:17:24 +0200 Subject: [PATCH 2/3] Update TOC test to use static HTML content instead of external API --- tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php index 95e79114aa..66778a41ec 100644 --- a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php @@ -63,11 +63,16 @@ public function testWriteTitleWithoutpageNumber(): void $section = $phpWord->addSection(); $section->addTOC(); + $staticHtml = '

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. + Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. + Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi.

+

Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. + Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim.

'; + //more than one title and random text for create more than one page for ($i = 1; $i <= 10; ++$i) { $section->addTitle('Title ' . $i, 1); - $content = file_get_contents('https://loripsum.net/api/10/long'); - \PhpOffice\PhpWord\Shared\Html::addHtml($section, $content ? $content : '', false, false); + \PhpOffice\PhpWord\Shared\Html::addHtml($section, $staticHtml, false, false); $section->addPageBreak(); } From f949f02c9f2ae7331bdcc24471e0d8cff8773794 Mon Sep 17 00:00:00 2001 From: "michal.schroeder" Date: Fri, 30 May 2025 15:25:55 +0200 Subject: [PATCH 3/3] update changelog --- docs/changes/1.x/1.4.0.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index ed095035e3..9eb6d42d71 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -31,6 +31,7 @@ - Writer HTML: Fixed null string for Text Elements by [@armagedon007](https://github.com/armagedon007) and [@Progi1984](https://github.com/Progi1984) in [#2738](https://github.com/PHPOffice/PHPWord/pull/2738) - Template Processor: Fix 0 considered as empty string by [@cavasinf](https://github.com/cavasinf), [@SnipsMine](https://github.com/SnipsMine) and [@Progi1984](https://github.com/Progi1984) fixing [#2572](https://github.com/PHPOffice/PHPWord/issues/2572), [#2703](https://github.com/PHPOffice/PHPWord/issues/2703) in [#2748](https://github.com/PHPOffice/PHPWord/pull/2748) - Word2007 Writer : Corrected generating TOC to fix page number missing issues [@jgiacomello](https://github.com/jgiacomello) in [#2556](https://github.com/PHPOffice/PHPWord/pull/2556) +- Enhanced error handling in encoding functions [@michalschroeder](https://github.com/michalschroeder) in [#2784](https://github.com/PHPOffice/PHPWord/pull/2784) ### Miscellaneous @@ -38,6 +39,7 @@ - Add test case to make sure vMerge defaults to 'continue' by [@SpraxDev](https://github.com/SpraxDev) in [#2677](https://github.com/PHPOffice/PHPWord/pull/2677) - Adding the possibility to use iterate search and replace with setValues by [@moghwan](https://github.com/moghwan) in [#2632](https://github.com/PHPOffice/PHPWord/pull/2640) - Add test cases that test the ODTText and Word2007 reader using the corresponding writer, increasing test coverage by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2745](https://github.com/PHPOffice/PHPWord/pull/2745) +- Updated TOCTest to use static HTML content instead of external resource [@michalschroeder](https://github.com/michalschroeder) in [#2784](https://github.com/PHPOffice/PHPWord/pull/2784) ### Deprecations - Deprecate `PhpOffice\PhpWord\Style\Paragraph::getIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::getIndentLeft()`