From 7b75c9e8b65066f52a05e79606d2dd22750196da Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Wed, 11 Oct 2023 18:21:15 -0400 Subject: [PATCH] allow email attachments with unicode --- CRM/Core/QuickForm/Action/Upload.php | 2 +- CRM/Utils/File.php | 9 ++++++++- CRM/Utils/Mail.php | 8 +++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CRM/Core/QuickForm/Action/Upload.php b/CRM/Core/QuickForm/Action/Upload.php index c86d0b2f16..9f7089893d 100644 --- a/CRM/Core/QuickForm/Action/Upload.php +++ b/CRM/Core/QuickForm/Action/Upload.php @@ -77,7 +77,7 @@ class CRM_Core_QuickForm_Action_Upload extends CRM_Core_QuickForm_Action { // rename the uploaded file with a unique number at the end $value = $element->getValue(); - $newName = CRM_Utils_File::makeFileName($value['name']); + $newName = CRM_Utils_File::makeFileName($value['name'], TRUE); $status = $element->moveUploadedFile($this->_uploadDir, $newName); if (!$status) { CRM_Core_Error::statusBounce(ts('We could not move the uploaded file %1 to the upload directory %2. Please verify that the \'Temporary Files\' setting points to a valid path which is writable by your web server.', [ diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index 833204adbf..e9d9750530 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -418,22 +418,29 @@ class CRM_Utils_File { * Make a valid file name. * * @param string $name + * @param bool $unicode * * @return string */ - public static function makeFileName($name) { + public static function makeFileName($name, bool $unicode = FALSE) { $uniqID = md5(uniqid(rand(), TRUE)); $info = pathinfo($name); $basename = substr($info['basename'], 0, -(strlen($info['extension'] ?? '') + (($info['extension'] ?? '') == '' ? 0 : 1)) ); if (!self::isExtensionSafe($info['extension'] ?? '')) { + if ($unicode) { + return self::makeFilenameWithUnicode("{$basename}_" . ($info['extension'] ?? '') . "_{$uniqID}", '_', 240) . ".unknown"; + } // munge extension so it cannot have an embbeded dot in it // The maximum length of a filename for most filesystems is 255 chars. // We'll truncate at 240 to give some room for the extension. return CRM_Utils_String::munge("{$basename}_" . ($info['extension'] ?? '') . "_{$uniqID}", '_', 240) . ".unknown"; } else { + if ($unicode) { + return self::makeFilenameWithUnicode("{$basename}_{$uniqID}", '_', 240) . "." . ($info['extension'] ?? ''); + } return CRM_Utils_String::munge("{$basename}_{$uniqID}", '_', 240) . "." . ($info['extension'] ?? ''); } } diff --git a/CRM/Utils/Mail.php b/CRM/Utils/Mail.php index 63e14b7000..3a21e9301f 100644 --- a/CRM/Utils/Mail.php +++ b/CRM/Utils/Mail.php @@ -276,7 +276,13 @@ class CRM_Utils_Mail { TRUE, 'base64', 'attachment', - (isset($attach['charset']) ? $attach['charset'] : '') + (isset($attach['charset']) ? $attach['charset'] : ''), + '', + '', + NULL, + NULL, + '', + 'utf-8' ); } } -- 2.25.1