Added support for avatar uploads
authorMarcel Werk <burntime@woltlab.com>
Sun, 12 Aug 2012 15:53:35 +0000 (17:53 +0200)
committerMarcel Werk <burntime@woltlab.com>
Sun, 12 Aug 2012 15:53:35 +0000 (17:53 +0200)
wcfsetup/install/files/lib/system/upload/DefaultUploadFileValidationStrategy.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/upload/IUploadFileValidationStrategy.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/upload/UploadFile.class.php
wcfsetup/install/files/lib/system/upload/UploadHandler.class.php

diff --git a/wcfsetup/install/files/lib/system/upload/DefaultUploadFileValidationStrategy.class.php b/wcfsetup/install/files/lib/system/upload/DefaultUploadFileValidationStrategy.class.php
new file mode 100644 (file)
index 0000000..274b72d
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+namespace wcf\system\upload;
+
+/**
+ * Provides a default implementation for validation strategies.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2012 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.upload
+ * @category   Community Framework
+ */
+class DefaultUploadFileValidationStrategy implements IUploadFileValidationStrategy {
+       /**
+        * allowed max size
+        * @var integer
+        */
+       protected $maxFilesize = 0;
+       
+       /**
+        * allowed file extensions
+        * @var array<string>
+        */
+       protected $fileExtensions = array();
+       
+       /**
+        * Creates a new DefaultUploadFileValidationStrategy object.
+        * 
+        * @param       integer         $maxFilesize
+        * @param       array<string>   $fileExtensions
+        */
+       public function __construct($maxFilesize, array $fileExtensions) {
+               $this->maxFilesize = $maxFilesize;
+               $this->fileExtensions = $fileExtensions;
+       }
+       
+       /**
+        * @see wcf\system\upload\IUploadFileValidationStrategy::validate()
+        */
+       public function validate(UploadFile $uploadFile) {
+               if ($uploadFile->getErrorCode() != 0) {
+                       $uploadFile->setValidationErrorType('uploadFailed');
+                       return false;
+               }
+               
+               if ($uploadFile->getFilesize() > $this->maxFilesize) {
+                       $uploadFile->setValidationErrorType('tooLarge');
+                       return false;
+               }
+               
+               if (!in_array($uploadFile->getFileExtension(), $this->fileExtensions)) {
+                       $uploadFile->setValidationErrorType('invalidExtension');
+                       return false;
+               }
+               
+               return true;
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/upload/IUploadFileValidationStrategy.class.php b/wcfsetup/install/files/lib/system/upload/IUploadFileValidationStrategy.class.php
new file mode 100644 (file)
index 0000000..3e74cb8
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace wcf\system\upload;
+
+/**
+ * Interface for file upload validation strategies.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2012 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.upload
+ * @category   Community Framework
+ */
+interface IUploadFileValidationStrategy {
+       /**
+        * Validates the given file. Returns true on success, otherwise false.
+        * 
+        * @param       wcf\system\upload\UploadFile            $uploadFile
+        * @return      boolean
+        */
+       public function validate(UploadFile $uploadFile);
+}
index d3e05352d83b0e9dd02dba97b1058cfd5871abf7..b52a2be925e6577cc75331bd19e9739f7242f84f 100644 (file)
@@ -125,29 +125,12 @@ class UploadFile {
        }
        
        /**
-        * Validates the uploaded file. Returns true on success, otherwise false.
+        * Sets the validation error type.
         * 
-        * @param       integer         $maxFilesize
-        * @param       array<string>   $fileExtensions
-        * @return      boolean
-        */
-       public function validateFile($maxFilesize, array $fileExtensions) {
-               if ($this->errorCode != 0) {
-                       $this->validationErrorType = 'uploadFailed';
-                       return false;
-               }
-               
-               if ($this->getFilesize() > $maxFilesize) {
-                       $this->validationErrorType = 'tooLarge';
-                       return false;
-               }
-               
-               if (!in_array($this->getFileExtension(), $fileExtensions)) {
-                       $this->validationErrorType = 'invalidExtension';
-                       return false;
-               }
-               
-               return true;
+        * @param       string          $validationErrorType
+        */
+       public function setValidationErrorType($validationErrorType) {
+               $this->validationErrorType = $validationErrorType;
        }
        
        /**
index d15d8cc2539feb2139a022297468ecd27b15dca7..6613b2c94d06f07e765a069904188fcf6cf22dee 100644 (file)
@@ -58,10 +58,10 @@ class UploadHandler {
         * @param       array<string>   $fileExtensions
         * @return      boolean
         */
-       public function validateFiles($maxFilesize, array $fileExtensions) {
+       public function validateFiles(IUploadFileValidationStrategy $validationStrategy) {
                $result = true;
                foreach ($this->files as $file) {
-                       if (!$file->validateFile($maxFilesize, $fileExtensions)) {
+                       if (!$validationStrategy->validate($file)) {
                                $this->erroneousFiles[] = $file;
                                $result = false;
                        }