-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Display the images in notification emails correctly
- Loading branch information
1 parent
0ebb324
commit 1bcf9c9
Showing
4 changed files
with
170 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
// This file is part of Bileto. | ||
// Copyright 2022-2024 Probesys | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
namespace App\Utils; | ||
|
||
class DomHelper | ||
{ | ||
/** | ||
* Replace the images URLs in a DOM string. | ||
* | ||
* The content string must be a valid DOM element. | ||
* | ||
* The mapping variable contains a list of URLs to replace, where the keys | ||
* are the URLs in the DOM and the values are the new URLs. | ||
* | ||
* @param array<string, string> $mapping | ||
*/ | ||
public static function replaceImagesUrls(string $content, array $mapping): string | ||
{ | ||
if (!$content) { | ||
return ''; | ||
} | ||
|
||
$contentDom = new \DOMDocument(); | ||
|
||
// DOMDocument::loadHTML considers the source string to be encoded in | ||
// ISO-8859-1 by default. In order to not ending with weird characters, | ||
// we encode the non-ASCII chars (i.e. all chars above >0x80) to HTML | ||
// entities. | ||
$content = mb_encode_numericentity( | ||
$content, | ||
[0x80, 0x10FFFF, 0, -1], | ||
'UTF-8' | ||
); | ||
|
||
$libxmlOptions = \LIBXML_NOERROR | \LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD; | ||
$contentDom->loadHTML($content, $libxmlOptions); | ||
$contentDomXPath = new \DomXPath($contentDom); | ||
|
||
foreach ($mapping as $initialUrl => $newUrl) { | ||
$imageNodes = $contentDomXPath->query("//img[@src='{$initialUrl}']"); | ||
|
||
if ($imageNodes === false || $imageNodes->length === 0) { | ||
// no corresponding node, the URL doesn't appear in the content | ||
continue; | ||
} | ||
|
||
foreach ($imageNodes as $imageNode) { | ||
if ($imageNode instanceof \DOMElement) { | ||
$imageNode->setAttribute('src', $newUrl); | ||
} | ||
} | ||
} | ||
|
||
$result = $contentDom->saveHTML(); | ||
|
||
if ($result === false) { | ||
return $content; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
// This file is part of Bileto. | ||
// Copyright 2022-2024 Probesys | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
namespace App\Tests\Utils; | ||
|
||
use App\Utils\DomHelper; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DomHelperTest extends TestCase | ||
{ | ||
public function testReplaceImagesUrls(): void | ||
{ | ||
$content = '<img alt="" src="https://example.coop/image.jpg">'; | ||
$mapping = [ | ||
'https://example.coop/image.jpg' => 'cid:image.jpg', | ||
]; | ||
|
||
$newContent = DomHelper::replaceImagesUrls($content, $mapping); | ||
|
||
$newContent = trim($newContent); | ||
$this->assertSame('<img alt="" src="cid:image.jpg">', $newContent); | ||
} | ||
|
||
public function testReplaceImagesUrlsWithUnmatchingUrl(): void | ||
{ | ||
$content = '<img alt="" src="/image.jpg">'; | ||
$mapping = [ | ||
'https://example.coop/image.jpg' => 'cid:image.jpg', | ||
]; | ||
|
||
$newContent = DomHelper::replaceImagesUrls($content, $mapping); | ||
|
||
$newContent = trim($newContent); | ||
$this->assertSame($content, $newContent); | ||
} | ||
|
||
public function testReplaceImagesUrlsWithNoImage(): void | ||
{ | ||
$content = '<p>My content</p>'; | ||
$mapping = [ | ||
'https://example.coop/image.jpg' => 'cid:image.jpg', | ||
]; | ||
|
||
$newContent = DomHelper::replaceImagesUrls($content, $mapping); | ||
|
||
$newContent = trim($newContent); | ||
$this->assertSame($content, $newContent); | ||
} | ||
|
||
public function testReplaceImagesUrlsWithEmptyContent(): void | ||
{ | ||
$content = ''; | ||
$mapping = [ | ||
'https://example.coop/image.jpg' => 'cid:image.jpg', | ||
]; | ||
|
||
$newContent = DomHelper::replaceImagesUrls($content, $mapping); | ||
|
||
$newContent = trim($newContent); | ||
$this->assertSame('', $newContent); | ||
} | ||
} |