pepebe
7/11/2015 - 12:05 PM

SanitiX sanitize filename plugin for the file manager upload process - Source: http://forums.modx.com/thread/73940/revolution-removing-unwan

<?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...');
    }
}