diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Util/Helpers/FixContentDispositionFilenameTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Util/Helpers/FixContentDispositionFilenameTest.php index fd11c54fe..085b9b392 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Util/Helpers/FixContentDispositionFilenameTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Util/Helpers/FixContentDispositionFilenameTest.php @@ -14,9 +14,21 @@ class FixContentDispositionFilenameTest extends TestCase { //The file name send to the function $fileName = "text\"text ?text/text"; + //Calling the fixContentDispositionFilename() function $res = fixContentDispositionFilename($fileName); + //Assert the special characters where replaced with the correct values - $this->assertEquals('texttext _text_text', $res); + $this->assertEquals('text_text%20_text_text', $res); + + // Initialize the variables for the test related to PMCORE-487 + $fileName = "12/2-20@test,TEST#123$56%100^500&Version*Test(URL)+File-Files. Test Output\SmartProcess"; + $expected = "12_2-20%40test%2CTEST%23123%2456%25100%5E500%26Version_Test%28URL%29%2BFile-Files.%20Test%20Output_SmartProcess"; + + // Calling the fixContentDispositionFilename() function + $newFileName = fixContentDispositionFilename($fileName); + + // Assert the values + $this->assertEquals($expected, $newFileName); } -} \ No newline at end of file +} diff --git a/workflow/engine/src/ProcessMaker/Util/helpers.php b/workflow/engine/src/ProcessMaker/Util/helpers.php index a87832f54..cf4fff1b3 100644 --- a/workflow/engine/src/ProcessMaker/Util/helpers.php +++ b/workflow/engine/src/ProcessMaker/Util/helpers.php @@ -475,30 +475,40 @@ function changeAbbreviationOfDirectives($size) } /** - * Encoding header filename used in Content-Disposition + * Remove reserved characters for file names, this value will be used in the headers for stream the file * * @param string $fileName * @param string $replacement * * @return string * - * @see cases_Step.php - * @see \ProcessMaker\BusinessModel\Cases\OutputDocument::addCasesOutputDocument() + * @see workflow/engine/methods/cases/cases_ShowOutputDocument.php + * + * @link https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#file-and-directory-names + * @link https://en.wikipedia.org/wiki/Filename#Comparison_of_filename_limitations */ function fixContentDispositionFilename($fileName, $replacement = '_') { - //(double quote) has to be removed - //(question mark) has to be replaced by underscore due to the issue in google chrome - //(forward slash) has to replaced by underscore - //(backslash) has to replaced by underscore - $default = [ - '/[\"]/' => '', - '/[\?]/' => $replacement, - '/[\\|\/]/' => $replacement, - '/\\\\/' => $replacement + // The reserved characters vary depending on the S.O., but this list covers the more important + $invalidCharacters = [ + "<", //(less than) + ">", //(greater than) + ":", //(colon) + "\"", //(double quote) + "/", //(forward slash) + "\\", //(backslash) + "|", //(vertical bar or pipe) + "?", //(question mark) + "*", //(asterisk) ]; - return preg_replace(array_keys($default), array_values($default), $fileName); + // Replace the reserved characters + $fileName = str_replace($invalidCharacters, $replacement, $fileName);; + + // We need to encode the string in order to preserve some characters like "%" + $fileName = rawurlencode($fileName); + + return $fileName; } /**