Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / file / upload / UploadField.class.php
1 <?php
2 namespace wcf\system\file\upload;
3
4 /**
5 * An specific upload field.
6 *
7 * @author Joshua Ruesweg
8 * @copyright 2001-2019 WoltLab GmbH
9 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
10 * @package WoltLabSuite\Core\System\File\Upload
11 * @since 5.2
12 */
13 class UploadField {
14 /**
15 * The max number of files for this field.
16 * @var int
17 */
18 public $maxFiles = 10;
19
20 /**
21 * The intern field id. Should be unique for each form.
22 * @var string
23 */
24 public $fieldId;
25
26 /**
27 * The internalId for uploads.
28 * @var string|null
29 */
30 public $internalId = null;
31
32 /**
33 * This flag indicates whether only images can uploaded via this field.
34 * @var boolean
35 */
36 public $imageOnly = false;
37
38 /**
39 * This flag indicates whether only images can uploaded via this field.
40 * <strong>Heads up:</strong> SVG images can contain bad code, therefore do not
41 * use this option, outside the acp or check the file whether remote code is contained.
42 * @var boolean
43 */
44 public $allowSvgImage = false;
45
46 /**
47 * Acceptable file types.
48 * @var string[]|null
49 * @since 5.3
50 */
51 public $acceptableFiles = null;
52
53 /**
54 * UploadField constructor.
55 *
56 * @param string $fieldId
57 */
58 public function __construct($fieldId) {
59 $this->fieldId = $fieldId;
60 }
61
62 /**
63 * Indicates the support of multiple files.
64 *
65 * @return boolean
66 */
67 public function supportMultipleFiles() {
68 return $this->maxFiles === null || $this->maxFiles > 1;
69 }
70
71 /**
72 * Returns the max number of files.
73 *
74 * @return int
75 */
76 public function getMaxFiles() {
77 return $this->maxFiles;
78 }
79
80 /**
81 * Returns `true` if only images can be uploaded via this field and returns `false` otherwise.
82 *
83 * @return boolean
84 */
85 public function isImageOnly() {
86 return $this->imageOnly;
87 }
88
89 /**
90 * Returns true, if the field can contain svg images in the image only mode.
91 *
92 * @return boolean
93 */
94 public function svgImageAllowed() {
95 return $this->allowSvgImage;
96 }
97
98 /**
99 * Returns the fieldId.
100 *
101 * @return string
102 */
103 public function getFieldId() {
104 return $this->fieldId;
105 }
106
107 /**
108 * Sets the internalId for this field.
109 *
110 * @param string $internalId
111 */
112 public function setInternalId($internalId) {
113 $this->internalId = $internalId;
114 }
115
116 /**
117 * Returns the internalId of this field.
118 *
119 * @return string|null
120 */
121 public function getInternalId() {
122 return $this->internalId;
123 }
124
125 /**
126 * Sets the flag for `imageOnly`. This flag indicates whether only images
127 * can uploaded via this field. Other file types will be rejected during upload.
128 *
129 * If set to `true` will also set the acceptable types to `image/*`. If set to
130 * false it will clear the acceptable types if they are `image/*`.
131 *
132 * @param boolean $imageOnly
133 */
134 public function setImageOnly($imageOnly) {
135 $this->imageOnly = $imageOnly;
136
137 if ($imageOnly) {
138 $this->setAcceptableFiles(['image/*']);
139 }
140 else {
141 // Using == here is safe, because we match a single element array containing
142 // a scalar value.
143 if ($this->getAcceptableFiles() == ['image/*']) {
144 $this->setAcceptableFiles(null);
145 }
146 }
147 }
148
149 /**
150 * Sets the flag for `allowSvgImage`. This flag indicates whether
151 * SVG images should be handled as image, if the upload field is
152 * image only (if this field is not image only, this method will
153 * throw an exception).
154 *
155 * <strong>Heads up:</strong> SVG images can contain bad code, therefore do not
156 * use this option, outside the acp or check the file whether remote code is contained.
157 *
158 * @param boolean $allowSvgImage
159 *
160 * @throws \BadMethodCallException if the imageOnly flag isn't set to true
161 */
162 public function setAllowSvgImage($allowSvgImage) {
163 if (!$this->isImageOnly()) {
164 throw new \BadMethodCallException('Allowing SVG images is only relevant, if the `imageOnly` flag is set to `true`.');
165 }
166
167 $this->allowSvgImage = $allowSvgImage;
168 }
169
170 /**
171 * Specifies acceptable file types. Use null to not specify any restrictions.
172 *
173 * <strong>Heads up:</strong> This feature is used to improve user experience, by removing
174 * unacceptable files from the file picker. It does not validate the type of the uploaded
175 * file. You are responsible to perform (proper) validation on the server side.
176 *
177 * Valid values are specified as "Unique file type specifiers":
178 * - A case insensitive file extension starting with a dot.
179 * - A mime type.
180 * - `audio/*`
181 * - `image/*`
182 * - `video/*`
183 *
184 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers
185 * @param string[]|null $acceptableFiles
186 * @since 5.3
187 */
188 public function setAcceptableFiles($acceptableFiles = null) {
189 $this->acceptableFiles = $acceptableFiles;
190 }
191
192 /**
193 * Returns the acceptable file types.
194 *
195 * @return string[]|null
196 * @since 5.3
197 */
198 public function getAcceptableFiles() {
199 return $this->acceptableFiles;
200 }
201 }