Skip to content

Commit cb88c91

Browse files
committed
Improve HTML input to avoid unexpected end tag "p" exception in some cases
1 parent d61ef90 commit cb88c91

3 files changed

Lines changed: 63 additions & 31 deletions

File tree

src/Traits/Attachments.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
trait Attachments
1414
{
15-
/**
15+
/**
1616
* Generate attachment from any embbeded image within $html that exceeds any of the max dimensions (hardcoded 380 x 300 max)
1717
*
1818
* @Param $html string
@@ -21,19 +21,35 @@ trait Attachments
2121
*/
2222
protected function createAttachmentsFrom($html, $ticket, $comment = false, $count = 0)
2323
{
24-
25-
// Html field without embedded image <img> tags
24+
// Html field without embedded image <img> tags
2625
if (!preg_match('/src="data:image\/(png|jpeg|gif);base64,/', $html)){
2726
return [
2827
'html' => $html,
2928
'count' => 0
3029
];
3130
}
32-
33-
$dom = new \DomDocument();
34-
31+
32+
$dom = new \DomDocument();
33+
34+
if (@!$dom->loadHtml( mb_convert_encoding('<div>' . $html . '</div>', 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)){
35+
return [
36+
'html' => $html,
37+
'count' => 0
38+
];
39+
}
40+
41+
$container = $dom->getElementsByTagName('div')->item(0);
42+
$container = $container->parentNode->removeChild($container);
43+
while ($dom->firstChild) {
44+
$dom->removeChild($dom->firstChild);
45+
}
46+
47+
while ($container->firstChild ) {
48+
$dom->appendChild($container->firstChild);
49+
}
50+
3551
// Exit when loadHtml doesn't process correctly
36-
if (@!$dom->loadHtml( mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)){
52+
if (!$dom){
3753
\Log::info('ticket '.$ticket->id.' loadHtml error');
3854
return [
3955
'html' => $html,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<b>{{ trans('panichd::lang.description') }}</b>
22
<table border="0" cellpadding="10" cellspacing="0" style="border: 1px solid #ddd; border-radius: 5px;"><tr>
3-
<td>@include('panichd::emails.partial.html_field', ['html_field' => $ticket->html])</td>
3+
<td>@include('panichd::emails.partial.html_field', ['html_field' => $ticket->html, 'ticket' => $ticket, 'field' => 'description'])</td>
44
</tr></table><br /><br />
55
@if ($ticket->intervention_html)
66
<b>{{ trans('panichd::lang.intervention') }}</b>
77
<table border="0" cellpadding="10" cellspacing="0" style="border: 1px solid #ddd; border-radius: 5px;"><tr>
8-
<td>@include('panichd::emails.partial.html_field', ['html_field' => $ticket->intervention_html])</td>
8+
<td>@include('panichd::emails.partial.html_field', ['html_field' => $ticket->intervention_html, 'ticket' => $ticket, 'field' => 'intervention'])</td>
99
</tr></table><br /><br />
1010
@endif
Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
<?php
2-
$dom = new DomDocument();
3-
$dom->loadHtml( mb_convert_encoding($html_field, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
4-
5-
$images = $dom->getElementsByTagName('img');
6-
7-
// foreach <img> in the email html
8-
$i = 0;
9-
foreach($images as $img){
10-
$src = $img->getAttribute('src');
11-
12-
// if the img source is 'data-url'
13-
if(preg_match('/data:image\/png;base64,/', $src)){
14-
15-
$src = str_replace('data:image/png;base64,', '', $src);
16-
$img->removeAttribute('src');
17-
$img->setAttribute('src', $message->embedData(base64_decode($src), "embed".$i.".png"));
18-
19-
$i++;
20-
}
21-
}
22-
23-
echo $dom->saveHTML();
2+
$dom = new \DomDocument();
3+
4+
if ($dom->loadHtml( mb_convert_encoding('<div>' . $html_field . '</div>', 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)){
5+
6+
$container = $dom->getElementsByTagName('div')->item(0);
7+
$container = $container->parentNode->removeChild($container);
8+
while ($dom->firstChild) {
9+
$dom->removeChild($dom->firstChild);
10+
}
11+
12+
while ($container->firstChild ) {
13+
$dom->appendChild($container->firstChild);
14+
}
15+
16+
$images = $dom->getElementsByTagName('img');
17+
18+
// foreach <img> in the email html
19+
$i = 0;
20+
foreach($images as $img){
21+
$src = $img->getAttribute('src');
22+
23+
// if the img source is 'data-url'
24+
if(preg_match('/data:image\/png;base64,/', $src)){
25+
26+
$src = str_replace('data:image/png;base64,', '', $src);
27+
$img->removeAttribute('src');
28+
$img->setAttribute('src', $message->embedData(base64_decode($src), "embed".$i.".png"));
29+
30+
$i++;
31+
}
32+
}
33+
34+
echo $dom->saveHTML();
35+
36+
}else{
37+
\Log::warning('HMTL could not be processed for ' . $field . ' field in ticket #' . $ticket->id);
38+
echo $html_field;
39+
}
2440
?>

0 commit comments

Comments
 (0)