Skip to content

Commit d15d172

Browse files
acheypemrflos
authored andcommitted
fix(TextareaField): sanitize base64 images works now with different types of img tags
1 parent 4763fd5 commit d15d172

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

tools/bazar/fields/TextareaField.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,7 @@ private function sanitizeAttach(string $text, array $entry): string
266266
private function sanitizeBase64Img(string $text, array $entry): string
267267
{
268268
$wiki = $this->getWiki();
269-
$regExpSearch = '(<img\s*'; // image
270-
$regExpSearch .= 'style="[^"]*"\s*)'; // with style
271-
$imageExtensions = '(gif|jpeg|png|jpg|svg|webp)';
272-
$imageContent = '([^"]*)';
273-
$regExpSearch .= "src=\"data:image\/$imageExtensions;base64,$imageContent\"\\s*"; // src base 64
274-
$regExpSearch .= '[^>]*((?<=data-filename=")[^"]*)(?=")'; // containing eventually a filename
275-
$regExpSearch .= '[^>]*>'; // end of img tag
269+
$regExpSearch = '(<img(?>\s*style="[^"]*")?\s*)src="data:image\/(gif|jpeg|png|jpg|svg|webp);base64,([^"]*)"\s*[^>]*(?>(?<=data-filename=")[^"]*")?[^>]*>';
276270
if (preg_match_all("/$regExpSearch/", $text, $matches)) {
277271
if (!class_exists('attach')) {
278272
include('tools/attach/libs/attach.lib.php');
@@ -285,7 +279,7 @@ private function sanitizeBase64Img(string $text, array $entry): string
285279
$imageContent = base64_decode($matches[3][$index]);
286280
$fileName = $matches[4][$index];
287281
if (empty(trim($fileName))) {
288-
$fileName = bin2hex(random_bytes(10)).$imageType;
282+
$fileName = bin2hex(random_bytes(10)) . '.' . $imageType;
289283
}
290284
if (preg_match('/^(.*)(\.[A-Za-z0-9]+)$/m', $fileName, $matchesForFile)) {
291285
$fileNameWithoutExtension = $matchesForFile[1];
@@ -294,7 +288,7 @@ private function sanitizeBase64Img(string $text, array $entry): string
294288
} else {
295289
$fileName = $this->sanitizeFileName($fileName);
296290
}
297-
291+
298292
$attach = new \Attach($wiki);
299293
$attach->file = $fileName;
300294

@@ -312,10 +306,10 @@ private function sanitizeBase64Img(string $text, array $entry): string
312306
if (!empty($newFilePath)) {
313307
// save file
314308
file_put_contents($newFilePath, $imageContent);
315-
309+
316310
$newText = $matches[1][$index];
317311
$newText .= "src=\"$newFilePath\">";
318-
312+
319313
$text = str_replace($textToReplace, $newText, $text);
320314
}
321315
unset($attach);

0 commit comments

Comments
 (0)