From a9941473eb3eedbb065d4fcabe12a848abf65235 Mon Sep 17 00:00:00 2001
From: "kaushik.p" <kaushik.p@vtigersolutions.com>
Date: Sat, 20 Jan 2024 21:31:10 +0530
Subject: [PATCH] #Fixes::158183087::kaushik::email attachments 500 error fixed

---
 modules/Emails/views/MassSaveAjax.php | 29 ++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/modules/Emails/views/MassSaveAjax.php b/modules/Emails/views/MassSaveAjax.php
index 69c5a485b..f4759a073 100644
--- a/modules/Emails/views/MassSaveAjax.php
+++ b/modules/Emails/views/MassSaveAjax.php
@@ -196,11 +196,13 @@ class Emails_MassSaveAjax_View extends Vtiger_Footer_View {
 					 * Ex: for PDF: if filename - abc_md5(abc).pdf then raw filename - abc.pdf
 					 * For Normal documents: rawFileName is not exist in the attachments info. So it fallback to normal filename
 					 */
-					$rawFileName = $existingAttachInfo['storedname'];
+					$rawFileName = $existingAttachInfo['attachment'];
+
 					if (!$rawFileName) {
 						$rawFileName = $existingAttachInfo['attachment'];
 					}
-					$file_name = $existingAttachInfo['attachment'];
+					$file_name = $existingAttachInfo['storedname'];
+
 					$path = $existingAttachInfo['path'];
 					$fileId = $existingAttachInfo['fileid'];
 
@@ -211,7 +213,11 @@ class Emails_MassSaveAjax_View extends Vtiger_Footer_View {
 					}
 					$oldFilePath = $path.'/'.$oldFileName;
 
-					$binFile = sanitizeUploadFileName($rawFileName, $upload_badext);
+
+					$binFile = sanitizeUploadFileName($rawFileName, $upload_badext); // 
+
+
+
 
 					$current_id = $adb->getUniqueID("vtiger_crmentity");
 
@@ -221,7 +227,10 @@ class Emails_MassSaveAjax_View extends Vtiger_Footer_View {
 
 					//get the file path inwhich folder we want to upload the file
 					$upload_file_path = decideFilePath();
-					$newFilePath = $upload_file_path . $current_id . "_" . $binFile;
+					$encryptFileName = Vtiger_Util_Helper::getEncryptedFileName($binFile);
+					$newFilePath = $upload_file_path . $current_id . "_" . $encryptFileName;
+
+
 
 					copy($oldFilePath, $newFilePath);
 
@@ -229,9 +238,15 @@ class Emails_MassSaveAjax_View extends Vtiger_Footer_View {
 					$params1 = array($current_id, $current_user->getId(), $ownerId, $moduleName . " Attachment", $recordModel->get('description'), $adb->formatDate($date_var, true), $adb->formatDate($date_var, true));
 					$adb->pquery($sql1, $params1);
 
-					$sql2 = "insert into vtiger_attachments(attachmentsid, name, description, type, path) values(?, ?, ?, ?, ?)";
-					$params2 = array($current_id, $filename, $recordModel->get('description'), $filetype, $upload_file_path);
-					$result = $adb->pquery($sql2, $params2);
+					$sql2 = "INSERT INTO vtiger_attachments(attachmentsid, name, description, type, path, storedname) values(?, ?, ?, ?, ?, ?)";
+					$params2 = array($current_id, $filename, $this->column_fields['description'], $filetype, $upload_file_path, $encryptFileName);
+					$adb->pquery($sql2, $params2);
+					
+					// NOTE: Missing storedname columns in below code
+					// $sql2 = "insert into vtiger_attachments(attachmentsid, name, description, type, path) values(?, ?, ?, ?, ?)";
+					// $params2 = array($current_id, $filename, $recordModel->get('description'), $filetype, $upload_file_path);
+					// $result = $adb->pquery($sql2, $params2);
+
 
 					$sql3 = 'insert into vtiger_seattachmentsrel values(?,?)';
 					$adb->pquery($sql3, array($recordModel->getId(), $current_id));
-- 
GitLab