2 namespace wcf\system\file\upload
;
5 * An specific upload field.
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
15 * The max number of files for this field.
18 public $maxFiles = 10;
21 * The intern field id. Should be unique for each form.
27 * The internalId for uploads.
30 public $internalId = null;
33 * This flag indicates whether only images can uploaded via this field.
36 public $imageOnly = false;
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.
44 public $allowSvgImage = false;
47 * Acceptable file types.
51 public $acceptableFiles = null;
54 * UploadField constructor.
56 * @param string $fieldId
58 public function __construct($fieldId) {
59 $this->fieldId
= $fieldId;
63 * Indicates the support of multiple files.
67 public function supportMultipleFiles() {
68 return $this->maxFiles
=== null ||
$this->maxFiles
> 1;
72 * Returns the max number of files.
76 public function getMaxFiles() {
77 return $this->maxFiles
;
81 * Returns `true` if only images can be uploaded via this field and returns `false` otherwise.
85 public function isImageOnly() {
86 return $this->imageOnly
;
90 * Returns true, if the field can contain svg images in the image only mode.
94 public function svgImageAllowed() {
95 return $this->allowSvgImage
;
99 * Returns the fieldId.
103 public function getFieldId() {
104 return $this->fieldId
;
108 * Sets the internalId for this field.
110 * @param string $internalId
112 public function setInternalId($internalId) {
113 $this->internalId
= $internalId;
117 * Returns the internalId of this field.
119 * @return string|null
121 public function getInternalId() {
122 return $this->internalId
;
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.
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/*`.
132 * @param boolean $imageOnly
134 public function setImageOnly($imageOnly) {
135 $this->imageOnly
= $imageOnly;
138 $this->setAcceptableFiles(['image/*']);
141 // Using == here is safe, because we match a single element array containing
143 if ($this->getAcceptableFiles() == ['image/*']) {
144 $this->setAcceptableFiles(null);
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).
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.
158 * @param boolean $allowSvgImage
160 * @throws \BadMethodCallException if the imageOnly flag isn't set to true
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`.');
167 $this->allowSvgImage
= $allowSvgImage;
171 * Specifies acceptable file types. Use null to not specify any restrictions.
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.
177 * Valid values are specified as "Unique file type specifiers":
178 * - A case insensitive file extension starting with a dot.
184 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers
185 * @param string[]|null $acceptableFiles
188 public function setAcceptableFiles($acceptableFiles = null) {
189 $this->acceptableFiles
= $acceptableFiles;
193 * Returns the acceptable file types.
195 * @return string[]|null
198 public function getAcceptableFiles() {
199 return $this->acceptableFiles
;