<![CDATA[ Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb - MODX Community Forums]]> https://forums.modx.com/thread/?thread=73940 <![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-451735 sottwell Jan 23, 2013, 09:31 AM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-451735 <![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-451727
So, if the original filename doesn't need to be renamed, you will always satisfy the condition:

 if ( file_exists($basePath . $newPath) ) { ...


it would be best to store file in a temporary folder or to execute the plugin before uploading the file.

Otherwise the only solution is to use unique_id() as the original plugin of Hansek does...

]]>
tillilab Jan 23, 2013, 08:54 AM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-451727
<![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-451604 exside Jan 22, 2013, 02:27 PM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-451604 <![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-451570
the only problem is that the filename is always changed with "duplicate", even if it doesn't exist...

May be it depends on the media source?

]]>
tillilab Jan 22, 2013, 11:25 AM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-451570
<![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-444623 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 =)]]>
exside Nov 26, 2012, 10:17 AM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-444623
<![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-426658 2.2.3 ??

MODX Revolution 2.2.3-pl (June 13, 2012)
====================================
- [#8051] Added extra sanitization for modFileHandler.sanitizePath
]]>
manfred62 Jun 14, 2012, 10:08 AM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-426658
<![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-415265 I'm testing it,
actually my customer is scared by the unique_id() suffix,
but I think there is no other way to avoid overwriting files... smiley

]]>
tillilab Mar 08, 2012, 11:03 AM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-415265
<![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-415087 Hansek Mar 07, 2012, 03:51 AM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb?page=2#dis-post-415087 <![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb#dis-post-414856
With the plugin suggested by Hansek the solution could be upgrading-safe?

Or may be a plugin to be launched as soon as file is uploaded?
]]>
tillilab Mar 05, 2012, 02:30 PM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb#dis-post-414856
<![CDATA[Re: Revolution: Removing unwanted forreign characters on file upload that breaks stuff like phpthumb]]> https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb#dis-post-412431 BTW, a few sites we work with which we haven't upgrade to 2.2, was able to make same adjustment in the following file.

/core/mode/modx/processor/browser/file/upload.php]]>
orbitalalliance Feb 17, 2012, 10:27 AM https://forums.modx.com/thread/73940/revolution-removing-unwanted-forreign-characters-on-file-upload-that-breaks-stuff-like-phpthumb#dis-post-412431