Completed
Push — master ( b0b1c6...4fec92 )
by Fabien
54:17
created
Classes/Dimension.php 1 patch
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -16,39 +16,39 @@
 block discarded – undo
16 16
  */
17 17
 class Dimension
18 18
 {
19
-    /**
20
-     * @var int
21
-     */
22
-    protected $width = 0;
23
-
24
-    /**
25
-     * @var int
26
-     */
27
-    protected $height = 0;
28
-
29
-    /**
30
-     * @param string $dimension
31
-     */
32
-    public function __construct($dimension)
33
-    {
34
-        $dimensions = GeneralUtility::trimExplode('x', $dimension);
35
-        $this->width = empty($dimensions[0]) ? 0 : $dimensions[0];
36
-        $this->height = empty($dimensions[1]) ? 0 : $dimensions[1];
37
-    }
38
-
39
-    /**
40
-     * @return mixed
41
-     */
42
-    public function getWidth()
43
-    {
44
-        return $this->width;
45
-    }
46
-
47
-    /**
48
-     * @return int
49
-     */
50
-    public function getHeight()
51
-    {
52
-        return $this->height;
53
-    }
19
+	/**
20
+	 * @var int
21
+	 */
22
+	protected $width = 0;
23
+
24
+	/**
25
+	 * @var int
26
+	 */
27
+	protected $height = 0;
28
+
29
+	/**
30
+	 * @param string $dimension
31
+	 */
32
+	public function __construct($dimension)
33
+	{
34
+		$dimensions = GeneralUtility::trimExplode('x', $dimension);
35
+		$this->width = empty($dimensions[0]) ? 0 : $dimensions[0];
36
+		$this->height = empty($dimensions[1]) ? 0 : $dimensions[1];
37
+	}
38
+
39
+	/**
40
+	 * @return mixed
41
+	 */
42
+	public function getWidth()
43
+	{
44
+		return $this->width;
45
+	}
46
+
47
+	/**
48
+	 * @return int
49
+	 */
50
+	public function getHeight()
51
+	{
52
+		return $this->height;
53
+	}
54 54
 }
Please login to merge, or discard this patch.
Classes/FileUpload/FormUtility.php 1 patch
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -16,53 +16,53 @@
 block discarded – undo
16 16
  */
17 17
 class FormUtility implements SingletonInterface
18 18
 {
19
-    /**
20
-     * Returns a class instance.
21
-     *
22
-     * @return \Fab\Media\FileUpload\FormUtility|object
23
-     */
24
-    public static function getInstance()
25
-    {
26
-        return GeneralUtility::makeInstance(\Fab\Media\FileUpload\FormUtility::class);
27
-    }
19
+	/**
20
+	 * Returns a class instance.
21
+	 *
22
+	 * @return \Fab\Media\FileUpload\FormUtility|object
23
+	 */
24
+	public static function getInstance()
25
+	{
26
+		return GeneralUtility::makeInstance(\Fab\Media\FileUpload\FormUtility::class);
27
+	}
28 28
 
29
-    /**
30
-     * Tells whether the content type is valid.
31
-     *
32
-     * @return bool
33
-     */
34
-    public function hasValidContentType()
35
-    {
36
-        return isset($GLOBALS['_SERVER']['CONTENT_TYPE']);
37
-    }
29
+	/**
30
+	 * Tells whether the content type is valid.
31
+	 *
32
+	 * @return bool
33
+	 */
34
+	public function hasValidContentType()
35
+	{
36
+		return isset($GLOBALS['_SERVER']['CONTENT_TYPE']);
37
+	}
38 38
 
39
-    /**
40
-     * Tells whether the form is multiparted, e.g "multipart/form-data"
41
-     *
42
-     * @return bool
43
-     */
44
-    public function isMultiparted()
45
-    {
46
-        return strpos(strtolower($GLOBALS['_SERVER']['CONTENT_TYPE']), 'multipart/form-data') === 0;
47
-    }
39
+	/**
40
+	 * Tells whether the form is multiparted, e.g "multipart/form-data"
41
+	 *
42
+	 * @return bool
43
+	 */
44
+	public function isMultiparted()
45
+	{
46
+		return strpos(strtolower($GLOBALS['_SERVER']['CONTENT_TYPE']), 'multipart/form-data') === 0;
47
+	}
48 48
 
49
-    /**
50
-     * Tells whether the form is URL encoded, e.g "application/x-www-form-urlencoded; charset=UTF-8"
51
-     *
52
-     * @return bool
53
-     */
54
-    public function isUrlEncoded()
55
-    {
56
-        return strpos(strtolower($GLOBALS['_SERVER']['CONTENT_TYPE']), 'application/x-www-form-urlencoded') === 0;
57
-    }
49
+	/**
50
+	 * Tells whether the form is URL encoded, e.g "application/x-www-form-urlencoded; charset=UTF-8"
51
+	 *
52
+	 * @return bool
53
+	 */
54
+	public function isUrlEncoded()
55
+	{
56
+		return strpos(strtolower($GLOBALS['_SERVER']['CONTENT_TYPE']), 'application/x-www-form-urlencoded') === 0;
57
+	}
58 58
 
59
-    /**
60
-     * Tells whether the form is octet streamed, e.g "application/x-www-form-urlencoded; charset=UTF-8"
61
-     *
62
-     * @return bool
63
-     */
64
-    public function isOctetStreamed()
65
-    {
66
-        return strpos(strtolower($GLOBALS['_SERVER']['CONTENT_TYPE']), 'application/octet-stream') === 0;
67
-    }
59
+	/**
60
+	 * Tells whether the form is octet streamed, e.g "application/x-www-form-urlencoded; charset=UTF-8"
61
+	 *
62
+	 * @return bool
63
+	 */
64
+	public function isOctetStreamed()
65
+	{
66
+		return strpos(strtolower($GLOBALS['_SERVER']['CONTENT_TYPE']), 'application/octet-stream') === 0;
67
+	}
68 68
 }
Please login to merge, or discard this patch.
Classes/FileUpload/UploadedFileAbstract.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -18,141 +18,141 @@
 block discarded – undo
18 18
  */
19 19
 abstract class UploadedFileAbstract implements UploadedFileInterface
20 20
 {
21
-    /**
22
-     * @var string
23
-     */
24
-    protected $uploadFolder;
25
-
26
-    /**
27
-     * @var string
28
-     */
29
-    protected $inputName;
30
-
31
-    /**
32
-     * @var string
33
-     */
34
-    protected $name;
35
-
36
-    /**
37
-     * Get the file type.
38
-     *
39
-     * @return int
40
-     */
41
-    public function getType()
42
-    {
43
-        $this->checkFileExistence();
44
-
45
-        // this basically extracts the mimetype and guess the filetype based
46
-        // on the first part of the mimetype works for 99% of all cases, and
47
-        // we don't need to make an SQL statement like EXT:media does currently
48
-        $mimeType = $this->getMimeType();
49
-        list($fileType) = explode('/', $mimeType);
50
-        switch (strtolower($fileType)) {
51
-            case 'text':
52
-                $type = File::FILETYPE_TEXT;
53
-                break;
54
-            case 'image':
55
-                $type = File::FILETYPE_IMAGE;
56
-                break;
57
-            case 'audio':
58
-                $type = File::FILETYPE_AUDIO;
59
-                break;
60
-            case 'video':
61
-                $type = File::FILETYPE_VIDEO;
62
-                break;
63
-            case 'application':
64
-            case 'software':
65
-                $type = File::FILETYPE_APPLICATION;
66
-                break;
67
-            default:
68
-                $type = File::FILETYPE_UNKNOWN;
69
-        }
70
-        return $type;
71
-    }
72
-
73
-    /**
74
-     * Get the file with its absolute path.
75
-     *
76
-     * @return string
77
-     */
78
-    public function getFileWithAbsolutePath()
79
-    {
80
-        return $this->uploadFolder . DIRECTORY_SEPARATOR . $this->name;
81
-    }
82
-
83
-    /**
84
-     * Get the file's public URL.
85
-     *
86
-     * @return string
87
-     */
88
-    public function getPublicUrl()
89
-    {
90
-        $fileNameAndPath = str_replace(Environment::getPublicPath() . '/', '', $this->getFileWithAbsolutePath());
91
-        return '/' . ltrim($fileNameAndPath, '/');
92
-    }
93
-
94
-    /**
95
-     * @return string
96
-     */
97
-    public function getInputName()
98
-    {
99
-        return $this->inputName;
100
-    }
101
-
102
-    /**
103
-     * @param string $inputName
104
-     * @return UploadedFileInterface
105
-     */
106
-    public function setInputName($inputName)
107
-    {
108
-        $this->inputName = $inputName;
109
-        return $this;
110
-    }
111
-
112
-    /**
113
-     * @return string
114
-     */
115
-    public function getUploadFolder()
116
-    {
117
-        return $this->uploadFolder;
118
-    }
119
-
120
-    /**
121
-     * @param string $uploadFolder
122
-     * @return UploadedFileInterface
123
-     */
124
-    public function setUploadFolder($uploadFolder)
125
-    {
126
-        $this->uploadFolder = $uploadFolder;
127
-        return $this;
128
-    }
129
-
130
-    /**
131
-     * @return string
132
-     */
133
-    public function getName()
134
-    {
135
-        return $this->name;
136
-    }
137
-
138
-    /**
139
-     * @param string $name
140
-     * @return UploadedFileInterface
141
-     */
142
-    public function setName($name)
143
-    {
144
-        $this->name = $name;
145
-        return $this;
146
-    }
147
-
148
-    /**
149
-     * Check whether the file exists.
150
-     */
151
-    protected function checkFileExistence()
152
-    {
153
-        if (!is_file($this->getFileWithAbsolutePath())) {
154
-            $message = sprintf('File not found at "%s". Did you save it?', $this->getFileWithAbsolutePath());
155
-            throw new MissingFileException($message, 1361786958);
156
-        }
157
-    }
21
+	/**
22
+	 * @var string
23
+	 */
24
+	protected $uploadFolder;
25
+
26
+	/**
27
+	 * @var string
28
+	 */
29
+	protected $inputName;
30
+
31
+	/**
32
+	 * @var string
33
+	 */
34
+	protected $name;
35
+
36
+	/**
37
+	 * Get the file type.
38
+	 *
39
+	 * @return int
40
+	 */
41
+	public function getType()
42
+	{
43
+		$this->checkFileExistence();
44
+
45
+		// this basically extracts the mimetype and guess the filetype based
46
+		// on the first part of the mimetype works for 99% of all cases, and
47
+		// we don't need to make an SQL statement like EXT:media does currently
48
+		$mimeType = $this->getMimeType();
49
+		list($fileType) = explode('/', $mimeType);
50
+		switch (strtolower($fileType)) {
51
+			case 'text':
52
+				$type = File::FILETYPE_TEXT;
53
+				break;
54
+			case 'image':
55
+				$type = File::FILETYPE_IMAGE;
56
+				break;
57
+			case 'audio':
58
+				$type = File::FILETYPE_AUDIO;
59
+				break;
60
+			case 'video':
61
+				$type = File::FILETYPE_VIDEO;
62
+				break;
63
+			case 'application':
64
+			case 'software':
65
+				$type = File::FILETYPE_APPLICATION;
66
+				break;
67
+			default:
68
+				$type = File::FILETYPE_UNKNOWN;
69
+		}
70
+		return $type;
71
+	}
72
+
73
+	/**
74
+	 * Get the file with its absolute path.
75
+	 *
76
+	 * @return string
77
+	 */
78
+	public function getFileWithAbsolutePath()
79
+	{
80
+		return $this->uploadFolder . DIRECTORY_SEPARATOR . $this->name;
81
+	}
82
+
83
+	/**
84
+	 * Get the file's public URL.
85
+	 *
86
+	 * @return string
87
+	 */
88
+	public function getPublicUrl()
89
+	{
90
+		$fileNameAndPath = str_replace(Environment::getPublicPath() . '/', '', $this->getFileWithAbsolutePath());
91
+		return '/' . ltrim($fileNameAndPath, '/');
92
+	}
93
+
94
+	/**
95
+	 * @return string
96
+	 */
97
+	public function getInputName()
98
+	{
99
+		return $this->inputName;
100
+	}
101
+
102
+	/**
103
+	 * @param string $inputName
104
+	 * @return UploadedFileInterface
105
+	 */
106
+	public function setInputName($inputName)
107
+	{
108
+		$this->inputName = $inputName;
109
+		return $this;
110
+	}
111
+
112
+	/**
113
+	 * @return string
114
+	 */
115
+	public function getUploadFolder()
116
+	{
117
+		return $this->uploadFolder;
118
+	}
119
+
120
+	/**
121
+	 * @param string $uploadFolder
122
+	 * @return UploadedFileInterface
123
+	 */
124
+	public function setUploadFolder($uploadFolder)
125
+	{
126
+		$this->uploadFolder = $uploadFolder;
127
+		return $this;
128
+	}
129
+
130
+	/**
131
+	 * @return string
132
+	 */
133
+	public function getName()
134
+	{
135
+		return $this->name;
136
+	}
137
+
138
+	/**
139
+	 * @param string $name
140
+	 * @return UploadedFileInterface
141
+	 */
142
+	public function setName($name)
143
+	{
144
+		$this->name = $name;
145
+		return $this;
146
+	}
147
+
148
+	/**
149
+	 * Check whether the file exists.
150
+	 */
151
+	protected function checkFileExistence()
152
+	{
153
+		if (!is_file($this->getFileWithAbsolutePath())) {
154
+			$message = sprintf('File not found at "%s". Did you save it?', $this->getFileWithAbsolutePath());
155
+			throw new MissingFileException($message, 1361786958);
156
+		}
157
+	}
158 158
 }
Please login to merge, or discard this patch.
Classes/FileUpload/UploadManager.php 1 patch
Indentation   +354 added lines, -354 removed lines patch added patch discarded remove patch
@@ -24,358 +24,358 @@
 block discarded – undo
24 24
  */
25 25
 class UploadManager
26 26
 {
27
-    public const UPLOAD_FOLDER = 'typo3temp/pics';
28
-
29
-    /**
30
-     * @var int|null|string
31
-     */
32
-    protected $sizeLimit;
33
-
34
-    /**
35
-     * @var string
36
-     */
37
-    protected $uploadFolder;
38
-
39
-    /**
40
-     * @var FormUtility
41
-     */
42
-    protected $formUtility;
43
-
44
-    /**
45
-     * @var ResourceStorage
46
-     */
47
-    protected $storage;
48
-
49
-    /**
50
-     * Name of the file input in the DOM.
51
-     *
52
-     * @var string
53
-     */
54
-    protected $inputName = 'qqfile';
55
-
56
-    /**
57
-     * @param ResourceStorage $storage
58
-     * @return UploadManager
59
-     */
60
-    public function __construct($storage = null)
61
-    {
62
-        $this->initializeUploadFolder();
63
-
64
-        // max file size in bytes
65
-        $this->sizeLimit = GeneralUtility::getMaxUploadFileSize() * 1024;
66
-        $this->checkServerSettings();
67
-
68
-        $this->formUtility = FormUtility::getInstance();
69
-        $this->storage = $storage;
70
-    }
71
-
72
-    /**
73
-     * Handle the uploaded file.
74
-     *
75
-     * @return UploadedFileInterface
76
-     */
77
-    public function handleUpload()
78
-    {
79
-        /** @var $uploadedFile UploadedFileInterface */
80
-        $uploadedFile = false;
81
-        if ($this->formUtility->isMultiparted()) {
82
-            // Default case
83
-            $uploadedFile = GeneralUtility::makeInstance(MultipartedFile::class);
84
-        } elseif ($this->formUtility->isOctetStreamed()) {
85
-            // Fine Upload plugin would use it if forceEncoded = false and paramsInBody = false
86
-            $uploadedFile = GeneralUtility::makeInstance(StreamedFile::class);
87
-        } elseif ($this->formUtility->isUrlEncoded()) {
88
-            // Used for image resizing in BE
89
-            $uploadedFile = GeneralUtility::makeInstance(Base64File::class);
90
-        }
91
-
92
-        if (!$uploadedFile) {
93
-            $this->throwException('Could not instantiate an upload object... No file was uploaded?');
94
-        }
95
-
96
-        $fileName = $this->getFileName($uploadedFile);
97
-
98
-        $this->checkFileSize($uploadedFile->getSize());
99
-        $this->checkFileAllowed($fileName);
100
-
101
-        $saved = $uploadedFile->setInputName($this->inputName)
102
-            ->setUploadFolder($this->uploadFolder)
103
-            ->setName($fileName)
104
-            ->save();
105
-
106
-        if (!$saved) {
107
-            $this->throwException('Could not save uploaded file. The upload was cancelled, or server error encountered');
108
-        }
109
-
110
-        // Optimize file if the uploaded file is an image.
111
-        if ($uploadedFile->getType() == File::FILETYPE_IMAGE) {
112
-            $uploadedFile = ImageOptimizer::getInstance($this->storage)->optimize($uploadedFile);
113
-        }
114
-        return $uploadedFile;
115
-    }
116
-
117
-    /**
118
-     * Internal function that checks if server's may sizes match the
119
-     * object's maximum size for uploads.
120
-     *
121
-     * @return void
122
-     */
123
-    protected function checkServerSettings()
124
-    {
125
-        $postSize = $this->toBytes(ini_get('post_max_size'));
126
-
127
-        $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
128
-
129
-        if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit) {
130
-            $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
131
-            $this->throwException('increase post_max_size and upload_max_filesize to ' . $size);
132
-        }
133
-    }
134
-
135
-    /**
136
-     * Convert a given size with units to bytes.
137
-     *
138
-     * @param string $str
139
-     * @return int|string
140
-     */
141
-    protected function toBytes($str)
142
-    {
143
-        $val = trim($str);
144
-        $last = strtolower($str[strlen($str) - 1]);
145
-        switch ($last) {
146
-            case 'g':
147
-                $val *= 1024;
148
-                // no break
149
-            case 'm':
150
-                $val *= 1024;
151
-                // no break
152
-            case 'k':
153
-                $val *= 1024;
154
-        }
155
-        return $val;
156
-    }
157
-
158
-    /**
159
-     * Return a file name given an uploaded file
160
-     *
161
-     * @param UploadedFileInterface $uploadedFile
162
-     * @return string
163
-     */
164
-    public function getFileName(UploadedFileInterface $uploadedFile)
165
-    {
166
-        $pathInfo = pathinfo($uploadedFile->getOriginalName());
167
-        $fileName = $this->sanitizeFileName($pathInfo['filename']);
168
-        $fileNameWithExtension = $fileName;
169
-        if (!empty($pathInfo['extension'])) {
170
-            $fileNameWithExtension = sprintf('%s.%s', $fileName, $pathInfo['extension']);
171
-        }
172
-        return $fileNameWithExtension;
173
-    }
174
-
175
-    /**
176
-     * Check whether the file size does not exceed the allowed limit
177
-     *
178
-     * @param int $size
179
-     */
180
-    public function checkFileSize($size)
181
-    {
182
-        if ($size == 0) {
183
-            $this->throwException('File is empty');
184
-        }
185
-
186
-        if ($size > $this->sizeLimit) {
187
-            $this->throwException('File is too large');
188
-        }
189
-    }
190
-
191
-    /**
192
-     * Check whether the file is allowed
193
-     *
194
-     * @param string $fileName
195
-     */
196
-    public function checkFileAllowed($fileName)
197
-    {
198
-        $isAllowed = $this->checkFileExtensionPermission($fileName);
199
-        if (!$isAllowed) {
200
-            $these = PermissionUtility::getInstance()->getAllowedExtensionList();
201
-            $this->throwException('File has an invalid extension, it should be one of ' . $these . '.');
202
-        }
203
-    }
204
-
205
-    /**
206
-     * If the fileName is given, check it against the
207
-     * TYPO3_CONF_VARS[BE][fileDenyPattern] + and if the file extension is allowed
208
-     *
209
-     * @see \TYPO3\CMS\Core\Resource\ResourceStorage->checkFileExtensionPermission($fileName);
210
-     * @param string $fileName Full filename
211
-     * @return boolean true if extension/filename is allowed
212
-     */
213
-    public function checkFileExtensionPermission($fileName)
214
-    {
215
-        $isAllowed = GeneralUtility::makeInstance(FileNameValidator::class)->isValid($fileName);
216
-        if ($isAllowed) {
217
-            $fileInfo = GeneralUtility::split_fileref($fileName);
218
-            // Set up the permissions for the file extension
219
-            $fileExtensionPermissions = $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']['webspace'];
220
-            $fileExtensionPermissions['allow'] = StringUtility::uniqueList(strtolower($fileExtensionPermissions['allow']));
221
-            $fileExtensionPermissions['deny'] = StringUtility::uniqueList(strtolower($fileExtensionPermissions['deny']));
222
-            $fileExtension = strtolower($fileInfo['fileext']);
223
-            if ($fileExtension !== '') {
224
-                // If the extension is found amongst the allowed types, we return true immediately
225
-                if ($fileExtensionPermissions['allow'] === '*' || GeneralUtility::inList($fileExtensionPermissions['allow'], $fileExtension)) {
226
-                    return true;
227
-                }
228
-                // If the extension is found amongst the denied types, we return false immediately
229
-                if ($fileExtensionPermissions['deny'] === '*' || GeneralUtility::inList($fileExtensionPermissions['deny'], $fileExtension)) {
230
-                    return false;
231
-                }
232
-                // If no match we return true
233
-                return true;
234
-            } else {
235
-                if ($fileExtensionPermissions['allow'] === '*') {
236
-                    return true;
237
-                }
238
-                if ($fileExtensionPermissions['deny'] === '*') {
239
-                    return false;
240
-                }
241
-                return true;
242
-            }
243
-        }
244
-        return false;
245
-    }
246
-
247
-    /**
248
-     * Sanitize the file name for the web.
249
-     * It has been noticed issues when letting done this work by FAL. Give it a little hand.
250
-     *
251
-     * @see https://github.com/alixaxel/phunction/blob/master/phunction/Text.php#L252
252
-     * @param string $fileName
253
-     * @param string $slug
254
-     * @param string $extra
255
-     * @return string
256
-     */
257
-    public function sanitizeFileName($fileName, $slug = '-', $extra = null)
258
-    {
259
-        return trim(preg_replace('~[^0-9a-z_' . preg_quote($extra, '~') . ']+~i', $slug, $this->unAccent($fileName)), $slug);
260
-    }
261
-
262
-    /**
263
-     * Remove accent from a string
264
-     *
265
-     * @see https://github.com/alixaxel/phunction/blob/master/phunction/Text.php#L297
266
-     * @param $string
267
-     * @return string
268
-     */
269
-    protected function unAccent($string)
270
-    {
271
-        $searches = array('ç', 'æ', 'œ', 'á', 'é', 'í', 'ó', 'ú', 'à', 'è', 'ì', 'ò', 'ù', 'ä', 'ë', 'ï', 'ö', 'ü', 'ÿ', 'â', 'ê', 'î', 'ô', 'û', 'å', 'e', 'i', 'ø', 'u');
272
-        $replaces = array('c', 'ae', 'oe', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'y', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u');
273
-        $sanitizedString = str_replace($searches, $replaces, $string);
274
-
275
-        if (extension_loaded('intl') === true) {
276
-            $sanitizedString = \Normalizer::normalize($sanitizedString, \Normalizer::FORM_KD);
277
-        }
278
-        return $sanitizedString;
279
-    }
280
-
281
-    /**
282
-     * @throws FailedFileUploadException
283
-     * @param string $message
284
-     */
285
-    protected function throwException($message)
286
-    {
287
-        throw new FailedFileUploadException($message, 1357510420);
288
-    }
289
-
290
-    /**
291
-     * Initialize Upload Folder.
292
-     *
293
-     * @return void
294
-     */
295
-    protected function initializeUploadFolder()
296
-    {
297
-        $this->uploadFolder = Environment::getPublicPath() . '/' . self::UPLOAD_FOLDER;
298
-
299
-        // Initialize the upload folder for file transfer and create it if not yet existing
300
-        if (!file_exists($this->uploadFolder)) {
301
-            GeneralUtility::mkdir($this->uploadFolder);
302
-        }
303
-
304
-        // Check whether the upload folder is writable
305
-        if (!is_writable($this->uploadFolder)) {
306
-            $this->throwException("Server error. Upload directory isn't writable.");
307
-        }
308
-    }
309
-
310
-    /**
311
-     * @return int|null|string
312
-     */
313
-    public function getSizeLimit()
314
-    {
315
-        return $this->sizeLimit;
316
-    }
317
-
318
-    /**
319
-     * @param int|null|string $sizeLimit
320
-     * @return $this
321
-     */
322
-    public function setSizeLimit($sizeLimit)
323
-    {
324
-        $this->sizeLimit = $sizeLimit;
325
-        return $this;
326
-    }
327
-
328
-    /**
329
-     * @return string
330
-     */
331
-    public function getUploadFolder()
332
-    {
333
-        return $this->uploadFolder;
334
-    }
335
-
336
-    /**
337
-     * @param string $uploadFolder
338
-     * @return $this
339
-     */
340
-    public function setUploadFolder($uploadFolder)
341
-    {
342
-        $this->uploadFolder = $uploadFolder;
343
-        return $this;
344
-    }
345
-
346
-    /**
347
-     * @return string
348
-     */
349
-    public function getInputName()
350
-    {
351
-        return $this->inputName;
352
-    }
353
-
354
-    /**
355
-     * @param string $inputName
356
-     * @return $this
357
-     */
358
-    public function setInputName($inputName)
359
-    {
360
-        $this->inputName = $inputName;
361
-        return $this;
362
-    }
363
-
364
-    /**
365
-     * @return ResourceStorage
366
-     */
367
-    public function getStorage()
368
-    {
369
-        return $this->storage;
370
-    }
371
-
372
-    /**
373
-     * @param ResourceStorage $storage
374
-     * @return $this
375
-     */
376
-    public function setStorage($storage)
377
-    {
378
-        $this->storage = $storage;
379
-        return $this;
380
-    }
27
+	public const UPLOAD_FOLDER = 'typo3temp/pics';
28
+
29
+	/**
30
+	 * @var int|null|string
31
+	 */
32
+	protected $sizeLimit;
33
+
34
+	/**
35
+	 * @var string
36
+	 */
37
+	protected $uploadFolder;
38
+
39
+	/**
40
+	 * @var FormUtility
41
+	 */
42
+	protected $formUtility;
43
+
44
+	/**
45
+	 * @var ResourceStorage
46
+	 */
47
+	protected $storage;
48
+
49
+	/**
50
+	 * Name of the file input in the DOM.
51
+	 *
52
+	 * @var string
53
+	 */
54
+	protected $inputName = 'qqfile';
55
+
56
+	/**
57
+	 * @param ResourceStorage $storage
58
+	 * @return UploadManager
59
+	 */
60
+	public function __construct($storage = null)
61
+	{
62
+		$this->initializeUploadFolder();
63
+
64
+		// max file size in bytes
65
+		$this->sizeLimit = GeneralUtility::getMaxUploadFileSize() * 1024;
66
+		$this->checkServerSettings();
67
+
68
+		$this->formUtility = FormUtility::getInstance();
69
+		$this->storage = $storage;
70
+	}
71
+
72
+	/**
73
+	 * Handle the uploaded file.
74
+	 *
75
+	 * @return UploadedFileInterface
76
+	 */
77
+	public function handleUpload()
78
+	{
79
+		/** @var $uploadedFile UploadedFileInterface */
80
+		$uploadedFile = false;
81
+		if ($this->formUtility->isMultiparted()) {
82
+			// Default case
83
+			$uploadedFile = GeneralUtility::makeInstance(MultipartedFile::class);
84
+		} elseif ($this->formUtility->isOctetStreamed()) {
85
+			// Fine Upload plugin would use it if forceEncoded = false and paramsInBody = false
86
+			$uploadedFile = GeneralUtility::makeInstance(StreamedFile::class);
87
+		} elseif ($this->formUtility->isUrlEncoded()) {
88
+			// Used for image resizing in BE
89
+			$uploadedFile = GeneralUtility::makeInstance(Base64File::class);
90
+		}
91
+
92
+		if (!$uploadedFile) {
93
+			$this->throwException('Could not instantiate an upload object... No file was uploaded?');
94
+		}
95
+
96
+		$fileName = $this->getFileName($uploadedFile);
97
+
98
+		$this->checkFileSize($uploadedFile->getSize());
99
+		$this->checkFileAllowed($fileName);
100
+
101
+		$saved = $uploadedFile->setInputName($this->inputName)
102
+			->setUploadFolder($this->uploadFolder)
103
+			->setName($fileName)
104
+			->save();
105
+
106
+		if (!$saved) {
107
+			$this->throwException('Could not save uploaded file. The upload was cancelled, or server error encountered');
108
+		}
109
+
110
+		// Optimize file if the uploaded file is an image.
111
+		if ($uploadedFile->getType() == File::FILETYPE_IMAGE) {
112
+			$uploadedFile = ImageOptimizer::getInstance($this->storage)->optimize($uploadedFile);
113
+		}
114
+		return $uploadedFile;
115
+	}
116
+
117
+	/**
118
+	 * Internal function that checks if server's may sizes match the
119
+	 * object's maximum size for uploads.
120
+	 *
121
+	 * @return void
122
+	 */
123
+	protected function checkServerSettings()
124
+	{
125
+		$postSize = $this->toBytes(ini_get('post_max_size'));
126
+
127
+		$uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
128
+
129
+		if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit) {
130
+			$size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
131
+			$this->throwException('increase post_max_size and upload_max_filesize to ' . $size);
132
+		}
133
+	}
134
+
135
+	/**
136
+	 * Convert a given size with units to bytes.
137
+	 *
138
+	 * @param string $str
139
+	 * @return int|string
140
+	 */
141
+	protected function toBytes($str)
142
+	{
143
+		$val = trim($str);
144
+		$last = strtolower($str[strlen($str) - 1]);
145
+		switch ($last) {
146
+			case 'g':
147
+				$val *= 1024;
148
+				// no break
149
+			case 'm':
150
+				$val *= 1024;
151
+				// no break
152
+			case 'k':
153
+				$val *= 1024;
154
+		}
155
+		return $val;
156
+	}
157
+
158
+	/**
159
+	 * Return a file name given an uploaded file
160
+	 *
161
+	 * @param UploadedFileInterface $uploadedFile
162
+	 * @return string
163
+	 */
164
+	public function getFileName(UploadedFileInterface $uploadedFile)
165
+	{
166
+		$pathInfo = pathinfo($uploadedFile->getOriginalName());
167
+		$fileName = $this->sanitizeFileName($pathInfo['filename']);
168
+		$fileNameWithExtension = $fileName;
169
+		if (!empty($pathInfo['extension'])) {
170
+			$fileNameWithExtension = sprintf('%s.%s', $fileName, $pathInfo['extension']);
171
+		}
172
+		return $fileNameWithExtension;
173
+	}
174
+
175
+	/**
176
+	 * Check whether the file size does not exceed the allowed limit
177
+	 *
178
+	 * @param int $size
179
+	 */
180
+	public function checkFileSize($size)
181
+	{
182
+		if ($size == 0) {
183
+			$this->throwException('File is empty');
184
+		}
185
+
186
+		if ($size > $this->sizeLimit) {
187
+			$this->throwException('File is too large');
188
+		}
189
+	}
190
+
191
+	/**
192
+	 * Check whether the file is allowed
193
+	 *
194
+	 * @param string $fileName
195
+	 */
196
+	public function checkFileAllowed($fileName)
197
+	{
198
+		$isAllowed = $this->checkFileExtensionPermission($fileName);
199
+		if (!$isAllowed) {
200
+			$these = PermissionUtility::getInstance()->getAllowedExtensionList();
201
+			$this->throwException('File has an invalid extension, it should be one of ' . $these . '.');
202
+		}
203
+	}
204
+
205
+	/**
206
+	 * If the fileName is given, check it against the
207
+	 * TYPO3_CONF_VARS[BE][fileDenyPattern] + and if the file extension is allowed
208
+	 *
209
+	 * @see \TYPO3\CMS\Core\Resource\ResourceStorage->checkFileExtensionPermission($fileName);
210
+	 * @param string $fileName Full filename
211
+	 * @return boolean true if extension/filename is allowed
212
+	 */
213
+	public function checkFileExtensionPermission($fileName)
214
+	{
215
+		$isAllowed = GeneralUtility::makeInstance(FileNameValidator::class)->isValid($fileName);
216
+		if ($isAllowed) {
217
+			$fileInfo = GeneralUtility::split_fileref($fileName);
218
+			// Set up the permissions for the file extension
219
+			$fileExtensionPermissions = $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']['webspace'];
220
+			$fileExtensionPermissions['allow'] = StringUtility::uniqueList(strtolower($fileExtensionPermissions['allow']));
221
+			$fileExtensionPermissions['deny'] = StringUtility::uniqueList(strtolower($fileExtensionPermissions['deny']));
222
+			$fileExtension = strtolower($fileInfo['fileext']);
223
+			if ($fileExtension !== '') {
224
+				// If the extension is found amongst the allowed types, we return true immediately
225
+				if ($fileExtensionPermissions['allow'] === '*' || GeneralUtility::inList($fileExtensionPermissions['allow'], $fileExtension)) {
226
+					return true;
227
+				}
228
+				// If the extension is found amongst the denied types, we return false immediately
229
+				if ($fileExtensionPermissions['deny'] === '*' || GeneralUtility::inList($fileExtensionPermissions['deny'], $fileExtension)) {
230
+					return false;
231
+				}
232
+				// If no match we return true
233
+				return true;
234
+			} else {
235
+				if ($fileExtensionPermissions['allow'] === '*') {
236
+					return true;
237
+				}
238
+				if ($fileExtensionPermissions['deny'] === '*') {
239
+					return false;
240
+				}
241
+				return true;
242
+			}
243
+		}
244
+		return false;
245
+	}
246
+
247
+	/**
248
+	 * Sanitize the file name for the web.
249
+	 * It has been noticed issues when letting done this work by FAL. Give it a little hand.
250
+	 *
251
+	 * @see https://github.com/alixaxel/phunction/blob/master/phunction/Text.php#L252
252
+	 * @param string $fileName
253
+	 * @param string $slug
254
+	 * @param string $extra
255
+	 * @return string
256
+	 */
257
+	public function sanitizeFileName($fileName, $slug = '-', $extra = null)
258
+	{
259
+		return trim(preg_replace('~[^0-9a-z_' . preg_quote($extra, '~') . ']+~i', $slug, $this->unAccent($fileName)), $slug);
260
+	}
261
+
262
+	/**
263
+	 * Remove accent from a string
264
+	 *
265
+	 * @see https://github.com/alixaxel/phunction/blob/master/phunction/Text.php#L297
266
+	 * @param $string
267
+	 * @return string
268
+	 */
269
+	protected function unAccent($string)
270
+	{
271
+		$searches = array('ç', 'æ', 'œ', 'á', 'é', 'í', 'ó', 'ú', 'à', 'è', 'ì', 'ò', 'ù', 'ä', 'ë', 'ï', 'ö', 'ü', 'ÿ', 'â', 'ê', 'î', 'ô', 'û', 'å', 'e', 'i', 'ø', 'u');
272
+		$replaces = array('c', 'ae', 'oe', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'y', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u');
273
+		$sanitizedString = str_replace($searches, $replaces, $string);
274
+
275
+		if (extension_loaded('intl') === true) {
276
+			$sanitizedString = \Normalizer::normalize($sanitizedString, \Normalizer::FORM_KD);
277
+		}
278
+		return $sanitizedString;
279
+	}
280
+
281
+	/**
282
+	 * @throws FailedFileUploadException
283
+	 * @param string $message
284
+	 */
285
+	protected function throwException($message)
286
+	{
287
+		throw new FailedFileUploadException($message, 1357510420);
288
+	}
289
+
290
+	/**
291
+	 * Initialize Upload Folder.
292
+	 *
293
+	 * @return void
294
+	 */
295
+	protected function initializeUploadFolder()
296
+	{
297
+		$this->uploadFolder = Environment::getPublicPath() . '/' . self::UPLOAD_FOLDER;
298
+
299
+		// Initialize the upload folder for file transfer and create it if not yet existing
300
+		if (!file_exists($this->uploadFolder)) {
301
+			GeneralUtility::mkdir($this->uploadFolder);
302
+		}
303
+
304
+		// Check whether the upload folder is writable
305
+		if (!is_writable($this->uploadFolder)) {
306
+			$this->throwException("Server error. Upload directory isn't writable.");
307
+		}
308
+	}
309
+
310
+	/**
311
+	 * @return int|null|string
312
+	 */
313
+	public function getSizeLimit()
314
+	{
315
+		return $this->sizeLimit;
316
+	}
317
+
318
+	/**
319
+	 * @param int|null|string $sizeLimit
320
+	 * @return $this
321
+	 */
322
+	public function setSizeLimit($sizeLimit)
323
+	{
324
+		$this->sizeLimit = $sizeLimit;
325
+		return $this;
326
+	}
327
+
328
+	/**
329
+	 * @return string
330
+	 */
331
+	public function getUploadFolder()
332
+	{
333
+		return $this->uploadFolder;
334
+	}
335
+
336
+	/**
337
+	 * @param string $uploadFolder
338
+	 * @return $this
339
+	 */
340
+	public function setUploadFolder($uploadFolder)
341
+	{
342
+		$this->uploadFolder = $uploadFolder;
343
+		return $this;
344
+	}
345
+
346
+	/**
347
+	 * @return string
348
+	 */
349
+	public function getInputName()
350
+	{
351
+		return $this->inputName;
352
+	}
353
+
354
+	/**
355
+	 * @param string $inputName
356
+	 * @return $this
357
+	 */
358
+	public function setInputName($inputName)
359
+	{
360
+		$this->inputName = $inputName;
361
+		return $this;
362
+	}
363
+
364
+	/**
365
+	 * @return ResourceStorage
366
+	 */
367
+	public function getStorage()
368
+	{
369
+		return $this->storage;
370
+	}
371
+
372
+	/**
373
+	 * @param ResourceStorage $storage
374
+	 * @return $this
375
+	 */
376
+	public function setStorage($storage)
377
+	{
378
+		$this->storage = $storage;
379
+		return $this;
380
+	}
381 381
 }
Please login to merge, or discard this patch.
Classes/FileUpload/ImageOptimizerInterface.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -14,11 +14,11 @@
 block discarded – undo
14 14
  */
15 15
 interface ImageOptimizerInterface
16 16
 {
17
-    /**
18
-     * Optimize the given uploaded image
19
-     *
20
-     * @param UploadedFileInterface $uploadedFile
21
-     * @return UploadedFileInterface
22
-     */
23
-    public function optimize($uploadedFile);
17
+	/**
18
+	 * Optimize the given uploaded image
19
+	 *
20
+	 * @param UploadedFileInterface $uploadedFile
21
+	 * @return UploadedFileInterface
22
+	 */
23
+	public function optimize($uploadedFile);
24 24
 }
Please login to merge, or discard this patch.
Classes/FileUpload/StreamedFile.php 1 patch
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -18,93 +18,93 @@
 block discarded – undo
18 18
  */
19 19
 class StreamedFile extends UploadedFileAbstract
20 20
 {
21
-    /**
22
-     * @var string
23
-     */
24
-    protected $inputName = 'qqfile';
21
+	/**
22
+	 * @var string
23
+	 */
24
+	protected $inputName = 'qqfile';
25 25
 
26
-    /**
27
-     * @var string
28
-     */
29
-    protected $uploadFolder;
26
+	/**
27
+	 * @var string
28
+	 */
29
+	protected $uploadFolder;
30 30
 
31
-    /**
32
-     * @var string
33
-     */
34
-    protected $name;
31
+	/**
32
+	 * @var string
33
+	 */
34
+	protected $name;
35 35
 
36
-    /**
37
-     * Save the file to the specified path
38
-     *
39
-     * @throws EmptyPropertyException
40
-     * @return boolean true on success
41
-     */
42
-    public function save()
43
-    {
44
-        if (is_null($this->uploadFolder)) {
45
-            throw new EmptyPropertyException('Upload folder is not defined', 1361787579);
46
-        }
36
+	/**
37
+	 * Save the file to the specified path
38
+	 *
39
+	 * @throws EmptyPropertyException
40
+	 * @return boolean true on success
41
+	 */
42
+	public function save()
43
+	{
44
+		if (is_null($this->uploadFolder)) {
45
+			throw new EmptyPropertyException('Upload folder is not defined', 1361787579);
46
+		}
47 47
 
48
-        if (is_null($this->name)) {
49
-            throw new EmptyPropertyException('File name is not defined', 1361787580);
50
-        }
48
+		if (is_null($this->name)) {
49
+			throw new EmptyPropertyException('File name is not defined', 1361787580);
50
+		}
51 51
 
52
-        $input = fopen("php://input", "r");
53
-        $temp = tmpfile();
54
-        $realSize = stream_copy_to_stream($input, $temp);
55
-        fclose($input);
52
+		$input = fopen("php://input", "r");
53
+		$temp = tmpfile();
54
+		$realSize = stream_copy_to_stream($input, $temp);
55
+		fclose($input);
56 56
 
57
-        if ($realSize != $this->getSize()) {
58
-            return false;
59
-        }
57
+		if ($realSize != $this->getSize()) {
58
+			return false;
59
+		}
60 60
 
61
-        $target = fopen($this->getFileWithAbsolutePath(), "w");
62
-        fseek($temp, 0, SEEK_SET);
63
-        stream_copy_to_stream($temp, $target);
64
-        fclose($target);
61
+		$target = fopen($this->getFileWithAbsolutePath(), "w");
62
+		fseek($temp, 0, SEEK_SET);
63
+		stream_copy_to_stream($temp, $target);
64
+		fclose($target);
65 65
 
66
-        return true;
67
-    }
66
+		return true;
67
+	}
68 68
 
69
-    /**
70
-     * Get the original file name.
71
-     *
72
-     * @return string
73
-     */
74
-    public function getOriginalName()
75
-    {
76
-        return $_GET[$this->inputName];
77
-    }
69
+	/**
70
+	 * Get the original file name.
71
+	 *
72
+	 * @return string
73
+	 */
74
+	public function getOriginalName()
75
+	{
76
+		return $_GET[$this->inputName];
77
+	}
78 78
 
79
-    /**
80
-     * Get the file size
81
-     *
82
-     * @throws \Exception
83
-     * @return integer file-size in byte
84
-     */
85
-    public function getSize()
86
-    {
87
-        if (isset($GLOBALS['_SERVER']['CONTENT_LENGTH'])) {
88
-            return (int)$GLOBALS['_SERVER']['CONTENT_LENGTH'];
89
-        } else {
90
-            throw new \Exception('Getting content length is not supported.');
91
-        }
92
-    }
79
+	/**
80
+	 * Get the file size
81
+	 *
82
+	 * @throws \Exception
83
+	 * @return integer file-size in byte
84
+	 */
85
+	public function getSize()
86
+	{
87
+		if (isset($GLOBALS['_SERVER']['CONTENT_LENGTH'])) {
88
+			return (int)$GLOBALS['_SERVER']['CONTENT_LENGTH'];
89
+		} else {
90
+			throw new \Exception('Getting content length is not supported.');
91
+		}
92
+	}
93 93
 
94
-    /**
95
-     * Get MIME type of file.
96
-     *
97
-     * @return string|boolean MIME type. eg, text/html, false on error
98
-     */
99
-    public function getMimeType()
100
-    {
101
-        $this->checkFileExistence();
102
-        if (function_exists('finfo_file')) {
103
-            $fileInfo = new \finfo();
104
-            return $fileInfo->file($this->getFileWithAbsolutePath(), FILEINFO_MIME_TYPE);
105
-        } elseif (function_exists('mime_content_type')) {
106
-            return mime_content_type($this->getFileWithAbsolutePath());
107
-        }
108
-        return false;
109
-    }
94
+	/**
95
+	 * Get MIME type of file.
96
+	 *
97
+	 * @return string|boolean MIME type. eg, text/html, false on error
98
+	 */
99
+	public function getMimeType()
100
+	{
101
+		$this->checkFileExistence();
102
+		if (function_exists('finfo_file')) {
103
+			$fileInfo = new \finfo();
104
+			return $fileInfo->file($this->getFileWithAbsolutePath(), FILEINFO_MIME_TYPE);
105
+		} elseif (function_exists('mime_content_type')) {
106
+			return mime_content_type($this->getFileWithAbsolutePath());
107
+		}
108
+		return false;
109
+	}
110 110
 }
Please login to merge, or discard this patch.
Classes/FileUpload/ImageOptimizer.php 1 patch
Indentation   +70 added lines, -70 removed lines patch added patch discarded remove patch
@@ -17,81 +17,81 @@
 block discarded – undo
17 17
  */
18 18
 class ImageOptimizer implements SingletonInterface
19 19
 {
20
-    /**
21
-     * @var array
22
-     */
23
-    protected $optimizers = [];
20
+	/**
21
+	 * @var array
22
+	 */
23
+	protected $optimizers = [];
24 24
 
25
-    /**
26
-     * @var ResourceStorage
27
-     */
28
-    protected $storage;
25
+	/**
26
+	 * @var ResourceStorage
27
+	 */
28
+	protected $storage;
29 29
 
30
-    /**
31
-     * Returns a class instance.
32
-     *
33
-     * @return ImageOptimizer
34
-     * @throws \InvalidArgumentException
35
-     * @param ResourceStorage $storage
36
-     */
37
-    public static function getInstance($storage = null)
38
-    {
39
-        return GeneralUtility::makeInstance(self::class, $storage);
40
-    }
30
+	/**
31
+	 * Returns a class instance.
32
+	 *
33
+	 * @return ImageOptimizer
34
+	 * @throws \InvalidArgumentException
35
+	 * @param ResourceStorage $storage
36
+	 */
37
+	public static function getInstance($storage = null)
38
+	{
39
+		return GeneralUtility::makeInstance(self::class, $storage);
40
+	}
41 41
 
42
-    /**
43
-     * Constructor
44
-     *
45
-     * @return ImageOptimizer
46
-     * @param ResourceStorage $storage
47
-     */
48
-    public function __construct($storage = null)
49
-    {
50
-        $this->storage = $storage;
51
-        $this->add('Fab\Media\FileUpload\Optimizer\Resize');
52
-        $this->add('Fab\Media\FileUpload\Optimizer\Rotate');
53
-    }
42
+	/**
43
+	 * Constructor
44
+	 *
45
+	 * @return ImageOptimizer
46
+	 * @param ResourceStorage $storage
47
+	 */
48
+	public function __construct($storage = null)
49
+	{
50
+		$this->storage = $storage;
51
+		$this->add('Fab\Media\FileUpload\Optimizer\Resize');
52
+		$this->add('Fab\Media\FileUpload\Optimizer\Rotate');
53
+	}
54 54
 
55
-    /**
56
-     * Register a new optimizer
57
-     *
58
-     * @param string $className
59
-     * @return void
60
-     */
61
-    public function add($className)
62
-    {
63
-        $this->optimizers[] = $className;
64
-    }
55
+	/**
56
+	 * Register a new optimizer
57
+	 *
58
+	 * @param string $className
59
+	 * @return void
60
+	 */
61
+	public function add($className)
62
+	{
63
+		$this->optimizers[] = $className;
64
+	}
65 65
 
66
-    /**
67
-     * Un-register a new optimizer
68
-     *
69
-     * @param string $className
70
-     * @return void
71
-     */
72
-    public function remove($className)
73
-    {
74
-        if (in_array($className, $this->optimizers)) {
75
-            $key = array_search($className, $this->optimizers);
76
-            unset($this->optimizers[$key]);
77
-        }
78
-    }
66
+	/**
67
+	 * Un-register a new optimizer
68
+	 *
69
+	 * @param string $className
70
+	 * @return void
71
+	 */
72
+	public function remove($className)
73
+	{
74
+		if (in_array($className, $this->optimizers)) {
75
+			$key = array_search($className, $this->optimizers);
76
+			unset($this->optimizers[$key]);
77
+		}
78
+	}
79 79
 
80
-    /**
81
-     * Optimize an image
82
-     *
83
-     * @param UploadedFileInterface $uploadedFile
84
-     * @return UploadedFileInterface
85
-     * @throws \InvalidArgumentException
86
-     */
87
-    public function optimize(UploadedFileInterface $uploadedFile)
88
-    {
89
-        foreach ($this->optimizers as $optimizer) {
90
-            /** @var $optimizer \Fab\Media\FileUpload\ImageOptimizerInterface */
91
-            $optimizer = GeneralUtility::makeInstance($optimizer, $this->storage);
92
-            $uploadedFile = $optimizer->optimize($uploadedFile);
93
-        }
80
+	/**
81
+	 * Optimize an image
82
+	 *
83
+	 * @param UploadedFileInterface $uploadedFile
84
+	 * @return UploadedFileInterface
85
+	 * @throws \InvalidArgumentException
86
+	 */
87
+	public function optimize(UploadedFileInterface $uploadedFile)
88
+	{
89
+		foreach ($this->optimizers as $optimizer) {
90
+			/** @var $optimizer \Fab\Media\FileUpload\ImageOptimizerInterface */
91
+			$optimizer = GeneralUtility::makeInstance($optimizer, $this->storage);
92
+			$uploadedFile = $optimizer->optimize($uploadedFile);
93
+		}
94 94
 
95
-        return $uploadedFile;
96
-    }
95
+		return $uploadedFile;
96
+	}
97 97
 }
Please login to merge, or discard this patch.
Classes/FileUpload/Base64File.php 1 patch
Indentation   +111 added lines, -111 removed lines patch added patch discarded remove patch
@@ -16,115 +16,115 @@
 block discarded – undo
16 16
  */
17 17
 class Base64File extends UploadedFileAbstract
18 18
 {
19
-    /**
20
-     * @var string
21
-     */
22
-    protected $inputName = 'qqfile';
23
-
24
-    /**
25
-     * @var string
26
-     */
27
-    protected $uploadFolder;
28
-
29
-    /**
30
-     * @var string
31
-     */
32
-    protected $name;
33
-
34
-    /**
35
-     * @var string
36
-     */
37
-    protected $image;
38
-
39
-    /**
40
-     * @var string
41
-     */
42
-    protected $extension;
43
-
44
-    /**
45
-     * @return \Fab\Media\FileUpload\Base64File
46
-     */
47
-    public function __construct()
48
-    {
49
-        // Processes the encoded image data and returns the decoded image
50
-        $encodedImage = GeneralUtility::_POST($this->inputName);
51
-        if (preg_match('/^data:image\/(jpg|jpeg|png)/i', $encodedImage, $matches)) {
52
-            $this->extension = $matches[1];
53
-        } else {
54
-            return false;
55
-        }
56
-
57
-        // Remove the mime-type header
58
-        $data = reset(array_reverse(explode('base64,', $encodedImage)));
59
-
60
-        // Use strict mode to prevent characters from outside the base64 range
61
-        $this->image = base64_decode($data, true);
62
-
63
-        if (!$this->image) {
64
-            return false;
65
-        }
66
-
67
-        $this->setName(uniqid() . '.' . $this->extension);
68
-    }
69
-
70
-    /**
71
-     * Save the file to the specified path
72
-     *
73
-     * @throws EmptyPropertyException
74
-     * @return boolean true on success
75
-     */
76
-    public function save()
77
-    {
78
-        if (is_null($this->uploadFolder)) {
79
-            throw new EmptyPropertyException('Upload folder is not defined', 1362587741);
80
-        }
81
-
82
-        if (is_null($this->name)) {
83
-            throw new EmptyPropertyException('File name is not defined', 1362587742);
84
-        }
85
-
86
-        return file_put_contents($this->getFileWithAbsolutePath(), $this->image) > 0;
87
-    }
88
-
89
-    /**
90
-     * Get the original file name.
91
-     *
92
-     * @return string
93
-     */
94
-    public function getOriginalName()
95
-    {
96
-        return $this->getName();
97
-    }
98
-
99
-    /**
100
-     * Get the file size
101
-     *
102
-     * @throws \Exception
103
-     * @return integer file-size in byte
104
-     */
105
-    public function getSize()
106
-    {
107
-        if (isset($GLOBALS['_SERVER']['CONTENT_LENGTH'])) {
108
-            return (int)$GLOBALS['_SERVER']['CONTENT_LENGTH'];
109
-        } else {
110
-            throw new \Exception('Getting content length is not supported.');
111
-        }
112
-    }
113
-
114
-    /**
115
-     * Get MIME type of file.
116
-     *
117
-     * @return string|boolean MIME type. eg, text/html, false on error
118
-     */
119
-    public function getMimeType()
120
-    {
121
-        $this->checkFileExistence();
122
-        if (function_exists('finfo_file')) {
123
-            $fileInfo = new \finfo();
124
-            return $fileInfo->file($this->getFileWithAbsolutePath(), FILEINFO_MIME_TYPE);
125
-        } elseif (function_exists('mime_content_type')) {
126
-            return mime_content_type($this->getFileWithAbsolutePath());
127
-        }
128
-        return false;
129
-    }
19
+	/**
20
+	 * @var string
21
+	 */
22
+	protected $inputName = 'qqfile';
23
+
24
+	/**
25
+	 * @var string
26
+	 */
27
+	protected $uploadFolder;
28
+
29
+	/**
30
+	 * @var string
31
+	 */
32
+	protected $name;
33
+
34
+	/**
35
+	 * @var string
36
+	 */
37
+	protected $image;
38
+
39
+	/**
40
+	 * @var string
41
+	 */
42
+	protected $extension;
43
+
44
+	/**
45
+	 * @return \Fab\Media\FileUpload\Base64File
46
+	 */
47
+	public function __construct()
48
+	{
49
+		// Processes the encoded image data and returns the decoded image
50
+		$encodedImage = GeneralUtility::_POST($this->inputName);
51
+		if (preg_match('/^data:image\/(jpg|jpeg|png)/i', $encodedImage, $matches)) {
52
+			$this->extension = $matches[1];
53
+		} else {
54
+			return false;
55
+		}
56
+
57
+		// Remove the mime-type header
58
+		$data = reset(array_reverse(explode('base64,', $encodedImage)));
59
+
60
+		// Use strict mode to prevent characters from outside the base64 range
61
+		$this->image = base64_decode($data, true);
62
+
63
+		if (!$this->image) {
64
+			return false;
65
+		}
66
+
67
+		$this->setName(uniqid() . '.' . $this->extension);
68
+	}
69
+
70
+	/**
71
+	 * Save the file to the specified path
72
+	 *
73
+	 * @throws EmptyPropertyException
74
+	 * @return boolean true on success
75
+	 */
76
+	public function save()
77
+	{
78
+		if (is_null($this->uploadFolder)) {
79
+			throw new EmptyPropertyException('Upload folder is not defined', 1362587741);
80
+		}
81
+
82
+		if (is_null($this->name)) {
83
+			throw new EmptyPropertyException('File name is not defined', 1362587742);
84
+		}
85
+
86
+		return file_put_contents($this->getFileWithAbsolutePath(), $this->image) > 0;
87
+	}
88
+
89
+	/**
90
+	 * Get the original file name.
91
+	 *
92
+	 * @return string
93
+	 */
94
+	public function getOriginalName()
95
+	{
96
+		return $this->getName();
97
+	}
98
+
99
+	/**
100
+	 * Get the file size
101
+	 *
102
+	 * @throws \Exception
103
+	 * @return integer file-size in byte
104
+	 */
105
+	public function getSize()
106
+	{
107
+		if (isset($GLOBALS['_SERVER']['CONTENT_LENGTH'])) {
108
+			return (int)$GLOBALS['_SERVER']['CONTENT_LENGTH'];
109
+		} else {
110
+			throw new \Exception('Getting content length is not supported.');
111
+		}
112
+	}
113
+
114
+	/**
115
+	 * Get MIME type of file.
116
+	 *
117
+	 * @return string|boolean MIME type. eg, text/html, false on error
118
+	 */
119
+	public function getMimeType()
120
+	{
121
+		$this->checkFileExistence();
122
+		if (function_exists('finfo_file')) {
123
+			$fileInfo = new \finfo();
124
+			return $fileInfo->file($this->getFileWithAbsolutePath(), FILEINFO_MIME_TYPE);
125
+		} elseif (function_exists('mime_content_type')) {
126
+			return mime_content_type($this->getFileWithAbsolutePath());
127
+		}
128
+		return false;
129
+	}
130 130
 }
Please login to merge, or discard this patch.
Classes/FileUpload/Optimizer/Resize.php 2 patches
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -22,110 +22,110 @@
 block discarded – undo
22 22
  */
23 23
 class Resize implements ImageOptimizerInterface
24 24
 {
25
-    /**
26
-     * @var GifBuilder
27
-     */
28
-    protected $gifCreator;
25
+	/**
26
+	 * @var GifBuilder
27
+	 */
28
+	protected $gifCreator;
29 29
 
30
-    /**
31
-     * @var ResourceStorage
32
-     */
33
-    protected $storage;
30
+	/**
31
+	 * @var ResourceStorage
32
+	 */
33
+	protected $storage;
34 34
 
35
-    /**
36
-     * Constructor
37
-     */
38
-    public function __construct($storage = null)
39
-    {
40
-        $this->storage = $storage;
41
-        $this->gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
42
-        $this->gifCreator->absPrefix = Environment::getPublicPath() . '/';
43
-    }
35
+	/**
36
+	 * Constructor
37
+	 */
38
+	public function __construct($storage = null)
39
+	{
40
+		$this->storage = $storage;
41
+		$this->gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
42
+		$this->gifCreator->absPrefix = Environment::getPublicPath() . '/';
43
+	}
44 44
 
45
-    /**
46
-     * Optimize the given uploaded image.
47
-     *
48
-     * @param UploadedFileInterface $uploadedFile
49
-     * @return UploadedFileInterface
50
-     */
51
-    public function optimize($uploadedFile)
52
-    {
53
-        $imageInfo = getimagesize($uploadedFile->getFileWithAbsolutePath());
45
+	/**
46
+	 * Optimize the given uploaded image.
47
+	 *
48
+	 * @param UploadedFileInterface $uploadedFile
49
+	 * @return UploadedFileInterface
50
+	 */
51
+	public function optimize($uploadedFile)
52
+	{
53
+		$imageInfo = getimagesize($uploadedFile->getFileWithAbsolutePath());
54 54
 
55
-        $currentWidth = $imageInfo[0];
56
-        $currentHeight = $imageInfo[1];
55
+		$currentWidth = $imageInfo[0];
56
+		$currentHeight = $imageInfo[1];
57 57
 
58
-        // resize an image if this one is bigger than telling by the settings.
59
-        if (is_object($this->storage)) {
60
-            $storageRecord = $this->storage->getStorageRecord();
61
-        } else {
62
-            // Will only work in the BE for now.
63
-            $storage = $this->getMediaModule()->getCurrentStorage();
64
-            $storageRecord = $storage->getStorageRecord();
65
-        }
58
+		// resize an image if this one is bigger than telling by the settings.
59
+		if (is_object($this->storage)) {
60
+			$storageRecord = $this->storage->getStorageRecord();
61
+		} else {
62
+			// Will only work in the BE for now.
63
+			$storage = $this->getMediaModule()->getCurrentStorage();
64
+			$storageRecord = $storage->getStorageRecord();
65
+		}
66 66
 
67
-        if (strlen($storageRecord['maximum_dimension_original_image']) > 0) {
68
-            /** @var Dimension $imageDimension */
69
-            $imageDimension = GeneralUtility::makeInstance(Dimension::class, $storageRecord['maximum_dimension_original_image']);
70
-            if ($currentWidth > $imageDimension->getWidth() || $currentHeight > $imageDimension->getHeight()) {
71
-                // resize taking the width as reference
72
-                $this->resize($uploadedFile->getFileWithAbsolutePath(), $imageDimension->getWidth(), $imageDimension->getHeight());
73
-            }
74
-        }
75
-        return $uploadedFile;
76
-    }
67
+		if (strlen($storageRecord['maximum_dimension_original_image']) > 0) {
68
+			/** @var Dimension $imageDimension */
69
+			$imageDimension = GeneralUtility::makeInstance(Dimension::class, $storageRecord['maximum_dimension_original_image']);
70
+			if ($currentWidth > $imageDimension->getWidth() || $currentHeight > $imageDimension->getHeight()) {
71
+				// resize taking the width as reference
72
+				$this->resize($uploadedFile->getFileWithAbsolutePath(), $imageDimension->getWidth(), $imageDimension->getHeight());
73
+			}
74
+		}
75
+		return $uploadedFile;
76
+	}
77 77
 
78
-    /**
79
-     * Resize an image according to given parameter.
80
-     *
81
-     * @throws \Exception
82
-     * @param string $fileNameAndPath
83
-     * @param int $width
84
-     * @param int $height
85
-     * @return void
86
-     */
87
-    public function resize($fileNameAndPath, $width = 0, $height = 0)
88
-    {
89
-        // Skip profile of the image
90
-        $imParams = '###SkipStripProfile###';
91
-        $options = array(
92
-            'maxW' => $width,
93
-            'maxH' => $height,
94
-        );
78
+	/**
79
+	 * Resize an image according to given parameter.
80
+	 *
81
+	 * @throws \Exception
82
+	 * @param string $fileNameAndPath
83
+	 * @param int $width
84
+	 * @param int $height
85
+	 * @return void
86
+	 */
87
+	public function resize($fileNameAndPath, $width = 0, $height = 0)
88
+	{
89
+		// Skip profile of the image
90
+		$imParams = '###SkipStripProfile###';
91
+		$options = array(
92
+			'maxW' => $width,
93
+			'maxH' => $height,
94
+		);
95 95
 
96
-        $tempFileInfo = $this->gifCreator->imageMagickConvert($fileNameAndPath, '', '', '', $imParams, '', $options, true);
97
-        if ($tempFileInfo) {
98
-            // Overwrite original file
99
-            @unlink($fileNameAndPath);
100
-            @rename($tempFileInfo[3], $fileNameAndPath);
101
-        }
102
-    }
96
+		$tempFileInfo = $this->gifCreator->imageMagickConvert($fileNameAndPath, '', '', '', $imParams, '', $options, true);
97
+		if ($tempFileInfo) {
98
+			// Overwrite original file
99
+			@unlink($fileNameAndPath);
100
+			@rename($tempFileInfo[3], $fileNameAndPath);
101
+		}
102
+	}
103 103
 
104
-    /**
105
-     * Escapes a file name so it can safely be used on the command line.
106
-     *
107
-     * @see \TYPO3\CMS\Core\Imaging\GraphicalFunctions
108
-     * @param string $inputName filename to safeguard, must not be empty
109
-     * @return string $inputName escaped as needed
110
-     */
111
-    protected function wrapFileName($inputName)
112
-    {
113
-        if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
114
-            $currentLocale = setlocale(LC_CTYPE, 0);
115
-            setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
116
-        }
117
-        $escapedInputName = escapeshellarg($inputName);
118
-        if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
119
-            setlocale(LC_CTYPE, $currentLocale);
120
-        }
121
-        return $escapedInputName;
122
-    }
104
+	/**
105
+	 * Escapes a file name so it can safely be used on the command line.
106
+	 *
107
+	 * @see \TYPO3\CMS\Core\Imaging\GraphicalFunctions
108
+	 * @param string $inputName filename to safeguard, must not be empty
109
+	 * @return string $inputName escaped as needed
110
+	 */
111
+	protected function wrapFileName($inputName)
112
+	{
113
+		if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
114
+			$currentLocale = setlocale(LC_CTYPE, 0);
115
+			setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
116
+		}
117
+		$escapedInputName = escapeshellarg($inputName);
118
+		if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
119
+			setlocale(LC_CTYPE, $currentLocale);
120
+		}
121
+		return $escapedInputName;
122
+	}
123 123
 
124
-    /**
125
-     * @return MediaModule|object
126
-     */
127
-    protected function getMediaModule()
128
-    {
129
-        return GeneralUtility::makeInstance(MediaModule::class);
130
-    }
124
+	/**
125
+	 * @return MediaModule|object
126
+	 */
127
+	protected function getMediaModule()
128
+	{
129
+		return GeneralUtility::makeInstance(MediaModule::class);
130
+	}
131 131
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -39,7 +39,7 @@
 block discarded – undo
39 39
     {
40 40
         $this->storage = $storage;
41 41
         $this->gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
42
-        $this->gifCreator->absPrefix = Environment::getPublicPath() . '/';
42
+        $this->gifCreator->absPrefix = Environment::getPublicPath().'/';
43 43
     }
44 44
 
45 45
     /**
Please login to merge, or discard this patch.