Coming from this thread here:
http://forums.modx.com/thread/80676/german-umlauts-and-blanks-in-image-names-and-paths#dis-post-444589
I was very happy to find all that different approaches to this problem. So thank you guys!
@manfred: no, I don't think that this fix has something to do with the issue discussed here, but correct me if I'm wrong!
I searched for the plugin mentioned earlier in this thread, but couldn't find it anymore under the link that susan posted and found it here again:
https://github.com/benjamin-vauchel/SanitizeFilename
I just copy & pasted the plugin itself to a modx installation of mine, but the transliteration did not work and I also like it more to replace spaces/unwanted chars with an underline and I also didn't need the added uniqeid, so I began to dig into the code a bit and came up with the following, rewritten plugin, which works quite well until now (not really tested intensively, but it shouldn't make problems...hopefully...if it does, please report it to me...)...ah, and I renamed it to SanitiX =))
<?php
/**
* MODx SanitiX sanitize filename plugin for the file manager upload process
*
* @author exside
* @credits:
* - Based on the code of the sanitizefilename plugin of Benjamin Vauchel https://github.com/benjamin-vauchel/SanitizeFilename
* - The Slug() phunction of AlixAxel https://github.com/alixaxel/phunction/blob/master/phunction/Text.php
* @version Version 1.1
* @modified 26.11.2012
*
* @description
* Remove unwanted characters from uploaded filenames and replace accented-characters with "normal" ones based on transliteration & language
* Example : a file with the following name "gäñz leidä file#nàmä mît jéglèçhem shì@t w0mä n!d sött.png"
* will be "gaenz_leidae_file_namae_mit_jeglechem_shi_t_w0mae_n_d_soett.png" (in a german environment with culturKey 'de')
*
* If a file already exists (with the sanitized filename), _duplicate is attached to the filename
*
* You can change the character that is used to replace spaces and special chars by specifing it in the $slug variable below, defaults to _
*
* Events: OnFileManagerUpload
*
* @package SanitiX
*/
// Cleaning function
if ( !function_exists('cleanFilename')) {
function cleanFilename($modx, $filename, $slug) {
// trim, lowercase, replace special chars, transliterate
if (function_exists('iconv')) {
// iconv somehow doesn't work without this and it doesn't sem to be set by modx
setlocale(LC_ALL, strtolower($modx->getOption('cultureKey')) . '_' . strtoupper($modx->getOption('cultureKey')));
$filename = strtolower(trim(preg_replace('~[^0-9a-z' . preg_quote(null, '~') . ']+~i', $slug, iconv('UTF-8', 'ASCII//TRANSLIT', $filename)), $slug));
//$modx->log(modX::LOG_LEVEL_ERROR, '[SanitiX] transliteraded filename: ' . $filename);
} else {
$modx->log(modX::LOG_LEVEL_ERROR, '[SanitiX] No iconv functions available, cannot transliterate the filename, that\'s why it looks ugly');
$filename = strtolower(trim(preg_replace('~[^0-9a-z' . preg_quote(null, '~') . ']+~i', $slug, $filename), $slug));
}
if ( empty($filename) ) {
return 'noname';
}
return $filename;
}
}
// We rename each of the uploaded files
foreach( $files as $file ) {
if ( $file['error'] == 0 ) {
$slug = '_';
$pathInfo = pathinfo($file['name']);
$basePath = $source->getBasePath();
$oldPath = $directory . $file['name'];
$newPath = cleanFilename($modx, $pathInfo['filename'], $slug) . '.' . $pathInfo['extension'];
if ( file_exists($basePath . $directory . $newPath) ) {
$newPath = cleanFilename($modx, $pathInfo['filename'], $slug) . '_duplicate.' . $pathInfo['extension'];
}
$source->renameObject($oldPath, $newPath);
} else {
$modx->log(modX::LOG_LEVEL_ERROR, '[SanitiX] There was an error during the upload process...');
}
}
hope it will be helpful to somebody! Improvements and suggestions are always welcome =)
[ed. note: exside last edited this post 11 years, 5 months ago.]