Skip to content

Commit

Permalink
re-implemented renameFormUploads to directly manipulate the $_FILES a…
Browse files Browse the repository at this point in the history
…rray (saves a ton of handling)
  • Loading branch information
bennyborn committed Oct 19, 2021
1 parent faf02ee commit 713d374
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
26 changes: 7 additions & 19 deletions src/Resources/contao/classes/CheckFilenames.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Contao\Database;
use Contao\DataContainer;
use Contao\FilesModel;
use Contao\Input;
use Contao\Message;
use Contao\StringUtil;
use Contao\System;
Expand Down Expand Up @@ -123,29 +124,16 @@ public function renameFiles( $arrFiles, $addSuffix=false ) {
*
* @return Contao\Widget
*/
public function renameFormUploads( $objWidget, $formId, $arrData, $objForm ) {
public function renameFormUploads( $objWidget, $formId, $formData, $form ) {

if( $objWidget->storeFile && !empty($_SESSION['FILES'][$objWidget->name]) && !$objWidget->doNotSanitize ) {
if( Input::post('FORM_SUBMIT') == $formId ) {

// the tmp_name could be outside of the Contao root dir (see #10)
try {
if( $objWidget->storeFile && !empty($_FILES[$objWidget->name]) && !$objWidget->doNotSanitize ) {

$tempPath = StringUtil::stripRootDir($_SESSION['FILES'][$objWidget->name]['tmp_name']);

} catch( \InvalidArgumentException $e ) {
return $objWidget;
}

// rename file and change entry in dbafs
$aRenamed = $this->renameFiles([$tempPath], $objWidget->doNotOverwrite);

if( array_key_exists($tempPath, $aRenamed) ) {

$newPath = $aRenamed[$tempPath];
$info = pathinfo($_FILES[$objWidget->name]['name']);
$newFileName = self::sanitizeFileOrFolderName($info['filename'], $info) . '.' . strtolower($info['extension']);

// change session
$_SESSION['FILES'][$objWidget->name]['name'] = basename($newPath);
$_SESSION['FILES'][$objWidget->name]['tmp_name'] = $newPath;
$_FILES[$objWidget->name]['name'] = $newFileName;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* Hooks
*/
$GLOBALS['TL_HOOKS']['postUpload'][] = ['\numero2\ProperFilenames\CheckFilenames', 'renameFiles'];
$GLOBALS['TL_HOOKS']['validateFormField'][] = ['\numero2\ProperFilenames\CheckFilenames', 'renameFormUploads'];
$GLOBALS['TL_HOOKS']['loadFormField'][] = ['\numero2\ProperFilenames\CheckFilenames', 'renameFormUploads'];

0 comments on commit 713d374

Please sign in to comment.