Add better upload error handling if debug is enabled
authorMatthias Schmidt <gravatronics@live.com>
Sun, 13 Jan 2019 08:13:04 +0000 (09:13 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 13 Jan 2019 08:13:04 +0000 (09:13 +0100)
Close #2712

wcfsetup/install/files/lib/system/upload/AvatarUploadFileValidationStrategy.class.php
wcfsetup/install/files/lib/system/upload/DefaultUploadFileSaveStrategy.class.php
wcfsetup/install/files/lib/system/upload/UploadFile.class.php

index 5ce9c437129d45b41489d54d5e03d5245580a910..5d8f88ff5660d56595da52f054889486e28731e1 100644 (file)
@@ -27,6 +27,10 @@ class AvatarUploadFileValidationStrategy extends DefaultUploadFileValidationStra
                        }
                }
                catch (SystemException $e) {
+                       if (ENABLE_DEBUG_MODE) {
+                               throw $e;
+                       }
+                       
                        $uploadFile->setValidationErrorType('badImage');
                        return false;
                }
index a414a36e629fc783fcc1a2c4e7898cc661c0558d..c6696a382ed90428954bce0cc3183a1e562db0cb 100644 (file)
@@ -134,7 +134,14 @@ class DefaultUploadFileSaveStrategy implements IUploadFileSaveStrategy {
                }
                
                // move uploaded file
-               if (@move_uploaded_file($uploadFile->getLocation(), $object->getLocation())) {
+               if (ENABLE_DEBUG_MODE) {
+                       $successfulUpload = move_uploaded_file($uploadFile->getLocation(), $object->getLocation());
+               }
+               else {
+                       $successfulUpload = @move_uploaded_file($uploadFile->getLocation(), $object->getLocation());
+               }
+               
+               if ($successfulUpload) {
                        try {
                                $parameters = [
                                        'object' => $object,
@@ -224,6 +231,10 @@ class DefaultUploadFileSaveStrategy implements IUploadFileSaveStrategy {
                                $this->objects[$uploadFile->getInternalFileID()] = $object;
                        }
                        catch (SystemException $e) {
+                               if (ENABLE_DEBUG_MODE) {
+                                       throw $e;
+                               }
+                               
                                /** @var DatabaseObjectEditor $editor */
                                $editor = new $this->editorClassName($object);
                                $editor->delete();
@@ -240,6 +251,10 @@ class DefaultUploadFileSaveStrategy implements IUploadFileSaveStrategy {
                                $this->generateThumbnails($object);
                        }
                        catch (SystemException $e) {
+                               if (ENABLE_DEBUG_MODE) {
+                                       throw $e;
+                               }
+                               
                                /** @var DatabaseObjectEditor $editor */
                                $editor = new $this->editorClassName($object);
                                $editor->delete();
index 7d0a8a772f974570425ed08b66b3be05628239ef..364a7f606da509663b2a9b83491724e6c6f7e32e 100644 (file)
@@ -66,6 +66,8 @@ class UploadFile {
         * @param       integer         $filesize
         * @param       integer         $errorCode
         * @param       string          $mimeType
+        * 
+        * @throws      \Exception      if an error occurred during upload and debug mode is enabled
         */
        public function __construct($filename, $location, $filesize, $errorCode = 0, $mimeType = '') {
                if (preg_match('~^__wcf_([0-9]+)_(.*)~', $filename, $matches)) {
@@ -78,6 +80,31 @@ class UploadFile {
                $this->filesize = $filesize;
                $this->errorCode = $errorCode;
                $this->mimeType = $mimeType;
+               
+               if (ENABLE_DEBUG_MODE) {
+                       switch ($errorCode) {
+                               case UPLOAD_ERR_INI_SIZE:
+                                       throw new \Exception("The uploaded file is larger than PHP's `upload_max_filesize`.");
+                                       
+                               case UPLOAD_ERR_FORM_SIZE:
+                                       throw new \Exception("The uploaded file is larger than the form's `MAX_FILE_SIZE`.");
+                               
+                               case UPLOAD_ERR_PARTIAL:
+                                       throw new \Exception("The uploaded file was only partially uploaded.");
+                               
+                               case UPLOAD_ERR_NO_FILE:
+                                       throw new \Exception("No file was uploaded.");
+                               
+                               case UPLOAD_ERR_NO_TMP_DIR:
+                                       throw new \Exception("There is no temporary folder where PHP can save the file.");
+                               
+                               case UPLOAD_ERR_CANT_WRITE:
+                                       throw new \Exception("The uploaded file could not be written to disk.");
+                               
+                               case UPLOAD_ERR_EXTENSION:
+                                       throw new \Exception("A PHP extension stopped the file upload.");
+                       }
+               }
        }
        
        /**