Character replacement for attachments
Hi guys,
I just received a support ticket from another osTicket user with a related but not the same problem.
This problem is with the change of the file names from the Portuguese character set to the English counter-part. What happens is the file name is truncated, most likely the Portuguese characters are replaced with an underscore.
The function that replaces the letters like "ç" with an underscore is located (on the default 1.6ST distribution) in /include/class.format.php on line 34. The standard PHP function is called preg_replace, documentation can be found (here).
To correct this issue to replace for example a "ç" with a "c" find the following code in include/class.format.php:
$search = array('/ß/','/ä/','/Ä/','/ö/','/Ö/','/ü/','/Ü/','([^)');
$replace = array('ss','ae','Ae','oe','Oe','ue','Ue','_');
return preg_replace($search,$replace,$filename);
and replace it with
$search = array('/Á/', '/Â/', '/Ã/', '/À/', '/Ç/', '/É/', '/Ê/', '/Í/', '/Ó/', '/Ô/', '/Õ/', '/Ú/', '/á/', '/â/', '/ã/', '/à/', '/ç/', '/é/', '/ê/', '/í/', '/ó/', '/ô/', '/õ/', '/ú/','/ /','([^)');
$replace = array('A', 'A', 'A', 'A', 'C', 'E', 'E', 'I', 'O', 'O', 'O', 'U', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'i', 'o', 'o', 'o', 'u','_','');
return preg_replace($search,$replace,$filename);
What does this do?
The $search variable defines the characters that requires replacing, you will notice the ([^) at the end, this tells the preg_replace function to replace anything that is not a standard English character or number.
The $replace variable defines what the replacement character is.
preg_replace will basically count what item in the $search array that it has found and replace it with the equivalent in the $replace array, so for the example of "Ç", it is the 5th item int he $search array so it will replace it with the 5th item in the $replace array being "C".
Note: you will see that each character is entered twice, once in upper case and once in lower case. This is because preg_replace is strict on character casing, this is because some applications using preg_replace may change the character case as part of the application, an example would be if you have a user defined input, some users may enter something like "HeLlO wOrLd", with the right string preg_replace can change this to "Hello World", "hello world", "HELLO WORLD" or any even "H***o W***d".
You can make the $search and replace arrays as long as you need to, so if you really want to you can include all non-English characters as shown (in this post on PHP.net). Keep in mind that with preg_replace being a regular expression, in the $search array you need the following format
'/ITEM/',
a search string with the '/ or the /' will return an error and the function will fail. So if you're going to copy and paste the example, make sure for the $search array you format the expression correctly before trying it.
Cheers,
Ryan - Ahgora Sistemas
www.ahgora.com.br(www.ahgora.com.br)