From 81f694b8f291c8ede0bfa5a3afa44dfdd438e5fc Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 23 Feb 2023 18:29:58 +0100 Subject: [PATCH] FIx duplicate image file from src data --- htdocs/core/class/CMailFile.class.php | 30 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php index 9e8593e6d1b..92c657f257f 100644 --- a/htdocs/core/class/CMailFile.class.php +++ b/htdocs/core/class/CMailFile.class.php @@ -273,10 +273,16 @@ class CMailFile if ($this->html_images[$i]) { $this->atleastoneimage = 1; if ($this->html_images[$i]['type'] == 'cidfromdata') { - $posindice = count($filename_list); - $filename_list[$posindice] = $this->html_images[$i]['fullpath']; - $mimetype_list[$posindice] = $this->html_images[$i]['content_type']; - $mimefilename_list[$posindice] = $this->html_images[$i]['name']; + if (!in_array($this->html_images[$i]['fullpath'], $filename_list)) { + // If this file path is not already into the $filename_list, we add it. + $posindice = count($filename_list); + $filename_list[$posindice] = $this->html_images[$i]['fullpath']; + $mimetype_list[$posindice] = $this->html_images[$i]['content_type']; + $mimefilename_list[$posindice] = $this->html_images[$i]['name']; + } else { + $posindice = array_search($this->html_images[$i]['fullpath'], $filename_list); + } + // We complete the array of cid_list $cid_list[$posindice] = $this->html_images[$i]['cid']; } dol_syslog("CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i]['name'], LOG_DEBUG); @@ -284,6 +290,8 @@ class CMailFile } } } + //var_dump($filename_list); + //var_dump($cid_list);exit; // Set atleastoneimage if there is at least one file (into $filename_list array) if (is_array($filename_list)) { @@ -1130,6 +1138,13 @@ class CMailFile return 'Bad value for sendmode'; } + // Now we delete image files that were created dynamically to manage data inline files + foreach ($this->html_images as $val) { + if (!empty($val['type']) && $val['type'] == 'cidfromdata') { + //dol_delete($val['fullpath']); + } + } + $parameters = array('sent' => $res); $action = ''; $reshook = $hookmanager->executeHooks('sendMailAfter', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks @@ -1659,7 +1674,7 @@ class CMailFile } /** - * Seearch images into html message and init array this->images_encoded if found + * Search images into html message and init array this->images_encoded if found * * @param string $images_dir Location of physical images files. For example $dolibarr_main_data_root.'/medias' * @return int >0 if OK, <0 if KO @@ -1746,7 +1761,8 @@ class CMailFile } /** - * Seearch images with data:image format into html message + * Seearch images with data:image format into html message. + * If we find some, we create it on disk. * * @param string $images_dir Location of where to store physicaly images files. For example $dolibarr_main_data_root.'/medias' * @return int >0 if OK, <0 if KO @@ -1788,7 +1804,7 @@ class CMailFile foreach ($matches[1] as $key => $ext) { // We save the image to send in disk $filecontent = $matches[2][$key]; - $cid = 'cid000'.dol_hash($this->html, 'md5'); + $cid = 'cid000'.dol_hash($this->html, 'md5'); // The id must not change if image is same $destfiletmp = $images_dir.'/'.$cid.'.'.$ext; $fhandle = @fopen($destfiletmp, 'w');