Prevent rewriting filenames for processed files
authorjoshuaruesweg <ruesweg@woltlab.com>
Tue, 1 Dec 2020 10:58:14 +0000 (11:58 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 1 Dec 2020 10:58:38 +0000 (11:58 +0100)
wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php
wcfsetup/install/files/lib/acp/form/StyleEditForm.class.php
wcfsetup/install/files/lib/data/style/StyleAction.class.php

index fb5e2b02b4264030ed8348a8ce49d4bc1361d164..666b21a5ceaaf8b50f8caba9206d11a478255a6d 100644 (file)
@@ -219,6 +219,12 @@ class StyleAddForm extends AbstractForm {
         */
        public $uploads = [];
        
+       /**
+        * @var (null|UploadField)[]
+        * @since 5.3
+        */
+       public $removedUploads = [];
+       
        /**
         * @var UploadField[]
         * @since 5.3
@@ -391,11 +397,11 @@ class StyleAddForm extends AbstractForm {
                // codemirror scroll offset
                if (isset($_POST['scrollOffsets']) && is_array($_POST['scrollOffsets'])) $this->scrollOffsets = ArrayUtil::toIntegerArray($_POST['scrollOffsets']); 
                
-               $this->uploads = [];
+               $this->uploads = $this->removedUploads = [];
                foreach (array_keys($this->getUploadFields()) as $field) {
                        $removedFiles = UploadHandler::getInstance()->getRemovedFiledByFieldId($field);
                        if (!empty($removedFiles)) {
-                               $this->uploads[$field] = null;
+                               $this->removedUploads = array_merge($this->removedUploads, $removedFiles);
                        }
                        
                        $files = UploadHandler::getInstance()->getFilesByFieldId($field);
@@ -816,6 +822,7 @@ class StyleAddForm extends AbstractForm {
                                'apiVersion' => $this->apiVersion
                        ]),
                        'uploads' => $this->uploads,
+                       'removedUploads' => $this->removedUploads,
                        'customAssets' => $this->customAssets,
                        'tmpHash' => $this->tmpHash,
                        'variables' => $this->variables,
index 04225a5a9d99c97c219d07dded42cf47d862a0a3..8f0ffe20ee9efe68ed2e86d07b4d6016107d6ec1 100644 (file)
@@ -251,6 +251,7 @@ class StyleEditForm extends StyleAddForm {
                                'apiVersion' => $this->apiVersion
                        ]),
                        'uploads' => $this->uploads,
+                       'removedUploads' => $this->removedUploads,
                        'customAssets' => $this->customAssets,
                        'tmpHash' => $this->tmpHash,
                        'variables' => $this->variables,
index c7b866ba44268f1e3520775993437f9652e3164f..c0f4aa528453294783254588cfcc6c6e6d57b67e 100644 (file)
@@ -184,13 +184,8 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction
                                /** @var \wcf\system\file\upload\UploadFile $file */
                                $file = $this->parameters['uploads'][$type];
                                
-                               if ($style->getVariable($type) && file_exists($style->getAssetPath().basename($style->getVariable($type)))) {
-                                       if (!$file || $style->getAssetPath().basename($style->getVariable($type)) !== $file->getLocation()) {
-                                               unlink($style->getAssetPath().basename($style->getVariable($type)));
-                                       }
-                               }
-                               
-                               if ($file !== null) {
+                               // Only save file, if it is not proccessed. 
+                               if ($file !== null && !$file->isProcessed()) {
                                        $fileLocation = $file->getLocation();
                                        $extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
                                        $newName = $type.'-'.\bin2hex(\random_bytes(4)).'.'.$extension;
@@ -199,12 +194,18 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction
                                        $this->parameters['variables'][$type] = $newName;
                                        $file->setProcessed($newLocation);
                                }
-                               else {
+                               else if ($file === null) {
                                        $this->parameters['variables'][$type] = '';
                                }
                        }
                }
                
+               foreach ($this->parameters['removedUploads'] as $removedUpload) {
+                       if (file_exists($removedUpload->getLocation())) {
+                               unlink($removedUpload->getLocation());
+                       }
+               }
+               
                $sql = "SELECT  variableID, variableName, defaultValue
                        FROM    wcf".WCF_N."_style_variable";
                $statement = WCF::getDB()->prepareStatement($sql);