Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SignalSlot Fiddle
<?php
namespace STUBR\Template\Resource;
/**
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
class ResourceStorageExtended extends \TYPO3\CMS\Core\Resource\ResourceStorage {
/**
* Returns a string where any character not matching [.a-zA-Z0-9_-] is
* substituted by '_'
* Trailing dots are removed
*
* Previously in \TYPO3\CMS\Core\Utility\File\BasicFileUtility::cleanFileName()
*
* @param string $fileName Input string, typically the body of a fileName
* @param string $charset Charset of the a fileName (defaults to current charset; depending on context)
* @return string Output string with any characters not matching [.a-zA-Z0-9_-] is substituted by '_' and trailing dots removed
* @throws Exception\InvalidFileNameException
*/
public function sanitizeFileName($fileName, $charset = '')
{
// Handle UTF-8 characters
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
// Allow ".", "-", 0-9, a-z, A-Z and everything beyond U+C0 (latin capital letter a with grave)
$cleanFileName = preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . ']/u', '_', trim($fileName));
} else {
// Define character set
if (!$charset) {
if (TYPO3_MODE === 'FE') {
$charset = $GLOBALS['TSFE']->renderCharset;
} else {
// default for Backend
$charset = 'utf-8';
}
}
// If a charset was found, convert fileName
if ($charset) {
$fileName = $this->getCharsetConversion()->specCharsToASCII($charset, $fileName);
}
// Replace unwanted characters by underscores
$cleanFileName = preg_replace('/[' . self::UNSAFE_FILENAME_CHARACTER_EXPRESSION . '\\xC0-\\xFF]/', '_', trim($fileName));
}
// Strip trailing dots and return
$cleanFileName = rtrim($cleanFileName, '.');
if ($cleanFileName === '') {
throw new Exception\InvalidFileNameException(
'File name ' . $fileName . ' is invalid.',
1320288991
);
}
return $cleanFileName;
}
}
?>
<?php
/** @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher */
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
$signalSlotDispatcher->connect(
\TYPO3\CMS\Core\Resource\ResourceStorage::class, // Signal class name
'sanitizeFileName', // Signal name
\STUBR\Template\Resource\ResourceStorageExtended::class, // Slot class name
'sanitizeFileName', // Slot name
false
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment