From 1d73598d67f4cdf2a14d6fbbd86a0f12d9992b13 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:10:37 -0700 Subject: [PATCH 1/3] Restore 2 Disabled Tests For some https requests for file_get_contents, "context" needs to be added to function call. It is not clear why this has changed recently. --- src/PhpSpreadsheet/Reader/Xlsx.php | 4 ++-- src/PhpSpreadsheet/Worksheet/Drawing.php | 7 ++++++- .../Reader/Xlsx/URLImageTest.php | 18 +++++------------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 116af7ec87..53cc1b53da 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -1451,7 +1451,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet ); if (isset($images[$linkImageKey])) { $url = str_replace('xl/drawings/', '', $images[$linkImageKey]); - $objDrawing->setPath($url); + $objDrawing->setPath($url, false); } if ($objDrawing->getPath() === '') { continue; @@ -1544,7 +1544,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet ); if (isset($images[$linkImageKey])) { $url = str_replace('xl/drawings/', '', $images[$linkImageKey]); - $objDrawing->setPath($url); + $objDrawing->setPath($url, false); } if ($objDrawing->getPath() === '') { continue; diff --git a/src/PhpSpreadsheet/Worksheet/Drawing.php b/src/PhpSpreadsheet/Worksheet/Drawing.php index 71a808815a..f1db719738 100644 --- a/src/PhpSpreadsheet/Worksheet/Drawing.php +++ b/src/PhpSpreadsheet/Worksheet/Drawing.php @@ -109,7 +109,12 @@ public function setPath(string $path, bool $verifyFile = true, ?ZipArchive $zip } // Implicit that it is a URL, rather store info than running check above on value in other places. $this->isUrl = true; - $imageContents = @file_get_contents($path); + $ctx = null; + // https://github.com/php/php-src/issues/16023 + if (str_starts_with($path, 'https:')) { + $ctx = stream_context_create(['ssl' => ['crypto_method'=>STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT]]); + } + $imageContents = @file_get_contents($path, false, $ctx); if ($imageContents !== false) { $filePath = tempnam(sys_get_temp_dir(), 'Drawing'); if ($filePath) { diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php index 4f7fa1e583..7757aa09a3 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php @@ -12,8 +12,7 @@ class URLImageTest extends TestCase { - // https://github.com/readthedocs/readthedocs.org/issues/11615 - public function xtestURLImageSource(): void + public function testURLImageSource(): void { if (getenv('SKIP_URL_IMAGE_TEST') === '1') { self::markTestSkipped('Skipped due to setting of environment variable'); @@ -31,20 +30,13 @@ public function xtestURLImageSource(): void // Check if the source is a URL or a file path self::assertTrue($drawing->getIsURL()); self::assertSame('https://phpspreadsheet.readthedocs.io/en/latest/topics/images/01-03-filter-icon-1.png', $drawing->getPath()); - $imageContents = file_get_contents($drawing->getPath()); - self::assertNotFalse($imageContents); - $filePath = tempnam(sys_get_temp_dir(), 'Drawing'); - self::assertNotFalse($filePath); - self::assertNotFalse(file_put_contents($filePath, $imageContents)); - $mimeType = mime_content_type($filePath); - unlink($filePath); - self::assertNotFalse($mimeType); - $extension = File::mime2ext($mimeType); - self::assertSame('png', $extension); + self::assertSame(IMAGETYPE_PNG, $drawing->getType()); + self::assertSame(84, $drawing->getWidth()); + self::assertSame(44, $drawing->getHeight()); } } - public function xtestURLImageSourceNotFound(): void + public function testURLImageSourceNotFound(): void { if (getenv('SKIP_URL_IMAGE_TEST') === '1') { self::markTestSkipped('Skipped due to setting of environment variable'); From f7fcb4bb1a7ae748acd782f934afa5fca3321d7e Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:20:43 -0700 Subject: [PATCH 2/3] Correct Spacing --- src/PhpSpreadsheet/Worksheet/Drawing.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Worksheet/Drawing.php b/src/PhpSpreadsheet/Worksheet/Drawing.php index f1db719738..bee049100d 100644 --- a/src/PhpSpreadsheet/Worksheet/Drawing.php +++ b/src/PhpSpreadsheet/Worksheet/Drawing.php @@ -112,7 +112,7 @@ public function setPath(string $path, bool $verifyFile = true, ?ZipArchive $zip $ctx = null; // https://github.com/php/php-src/issues/16023 if (str_starts_with($path, 'https:')) { - $ctx = stream_context_create(['ssl' => ['crypto_method'=>STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT]]); + $ctx = stream_context_create(['ssl' => ['crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT]]); } $imageContents = @file_get_contents($path, false, $ctx); if ($imageContents !== false) { From 512c21e808b83da44b8af0ec53987f0c2357d89a Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:27:33 -0700 Subject: [PATCH 3/3] Add Disconnects --- tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php index 7757aa09a3..e715621d39 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php @@ -34,6 +34,7 @@ public function testURLImageSource(): void self::assertSame(84, $drawing->getWidth()); self::assertSame(44, $drawing->getHeight()); } + $spreadsheet->disconnectWorksheets(); } public function testURLImageSourceNotFound(): void @@ -48,6 +49,7 @@ public function testURLImageSourceNotFound(): void $worksheet = $spreadsheet->getActiveSheet(); $collection = $worksheet->getDrawingCollection(); self::assertCount(0, $collection); + $spreadsheet->disconnectWorksheets(); } public function testURLImageSourceBadProtocol(): void