PMCORE-487

This commit is contained in:
Julio Cesar Laura Avendaño
2020-04-08 20:14:09 +00:00
parent ce22498d01
commit 30d199060c
2 changed files with 37 additions and 15 deletions

View File

@@ -14,9 +14,21 @@ class FixContentDispositionFilenameTest extends TestCase
{ {
//The file name send to the function //The file name send to the function
$fileName = "text\"text ?text/text"; $fileName = "text\"text ?text/text";
//Calling the fixContentDispositionFilename() function //Calling the fixContentDispositionFilename() function
$res = fixContentDispositionFilename($fileName); $res = fixContentDispositionFilename($fileName);
//Assert the special characters where replaced with the correct values //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);
} }
} }

View File

@@ -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 $fileName
* @param string $replacement * @param string $replacement
* *
* @return string * @return string
* *
* @see cases_Step.php * @see workflow/engine/methods/cases/cases_ShowOutputDocument.php
* @see \ProcessMaker\BusinessModel\Cases\OutputDocument::addCasesOutputDocument() *
* @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 = '_') function fixContentDispositionFilename($fileName, $replacement = '_')
{ {
//(double quote) has to be removed // The reserved characters vary depending on the S.O., but this list covers the more important
//(question mark) has to be replaced by underscore due to the issue in google chrome $invalidCharacters = [
//(forward slash) has to replaced by underscore "<", //(less than)
//(backslash) has to replaced by underscore ">", //(greater than)
$default = [ ":", //(colon)
'/[\"]/' => '', "\"", //(double quote)
'/[\?]/' => $replacement, "/", //(forward slash)
'/[\\|\/]/' => $replacement, "\\", //(backslash)
'/\\\\/' => $replacement "|", //(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;
} }
/** /**