1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Configuration.php
См. документацию.
1<?php
2
3namespace Bitrix\UI\FileUploader;
4
5use Bitrix\Main\Config\Ini;
6use Bitrix\Main\Result;
7
9{
10 protected ?int $maxFileSize = 256 * 1024 * 1024;
11 protected int $minFileSize = 0;
12 protected bool $acceptOnlyImages = false;
13 protected array $acceptedFileTypes = [];
14 protected array $ignoredFileNames = ['.ds_store', 'thumbs.db', 'desktop.ini'];
15 protected int $imageMinWidth = 1;
16 protected int $imageMinHeight = 1;
17 protected int $imageMaxWidth = 7000;
18 protected int $imageMaxHeight = 7000;
19 protected ?int $imageMaxFileSize = 48 * 1024 * 1024;
20 protected int $imageMinFileSize = 0;
21 protected bool $treatOversizeImageAsFile = false;
22 protected bool $ignoreUnknownImageTypes = false;
23
24 public function __construct(array $options = [])
25 {
26 $optionNames = [
27 'maxFileSize',
28 'minFileSize',
29 'imageMinWidth',
30 'imageMinHeight',
31 'imageMaxWidth',
32 'imageMaxHeight',
33 'imageMaxFileSize',
34 'imageMinFileSize',
35 'acceptOnlyImages',
36 'acceptedFileTypes',
37 'ignoredFileNames',
38 ];
39
40 $globalSettings = static::getGlobalSettings();
41 foreach ($optionNames as $optionName)
42 {
43 $setter = 'set' . ucfirst($optionName);
44 if (array_key_exists($optionName, $options))
45 {
47 $this->$setter($optionValue);
48 }
49 else if (array_key_exists($optionName, $globalSettings))
50 {
51 $optionValue = $globalSettings[$optionName];
52 if (is_string($optionValue) && preg_match('/FileSize/i', $optionName))
53 {
55 }
56
57 $this->$setter($optionValue);
58 }
59 }
60
61 if (isset($options['ignoreUnknownImageTypes']) && is_bool($options['ignoreUnknownImageTypes']))
62 {
63 $this->setIgnoreUnknownImageTypes($options['ignoreUnknownImageTypes']);
64 }
65
66 if (isset($options['treatOversizeImageAsFile']) && is_bool($options['treatOversizeImageAsFile']))
67 {
68 $this->setTreatOversizeImageAsFile($options['treatOversizeImageAsFile']);
69 }
70 }
71
72 public static function getGlobalSettings(): array
73 {
74 $settings = [];
76 if (isset($configuration['uploader']['settings']) && is_array($configuration['uploader']['settings']))
77 {
78 $settings = $configuration['uploader']['settings'];
79 }
80
81 return $settings;
82 }
83
84 public function shouldTreatImageAsFile(FileData | array $fileData): bool
85 {
87 {
88 return false;
89 }
90
91 if (!$fileData->isImage())
92 {
93 return true;
94 }
95
96 $result = $this->validateImage($fileData);
97
98 return !$result->isSuccess();
99 }
100
101 public function validateImage(FileData $fileData): Result
102 {
103 $result = new Result();
104
105 if (($fileData->getWidth() === 0 || $fileData->getHeight() === 0) && !$this->getIgnoreUnknownImageTypes())
106 {
108 }
109
110 if ($this->getImageMaxFileSize() !== null && $fileData->getSize() > $this->getImageMaxFileSize())
111 {
112 return $result->addError(
113 new UploaderError(
115 [
116 'imageMaxFileSize' => \CFile::formatSize($this->getImageMaxFileSize()),
117 'imageMaxFileSizeInBytes' => $this->getImageMaxFileSize(),
118 ]
119 )
120 );
121 }
122
123 if ($fileData->getSize() < $this->getImageMinFileSize())
124 {
125 return $result->addError(
126 new UploaderError(
128 [
129 'imageMinFileSize' => \CFile::formatSize($this->getImageMinFileSize()),
130 'imageMinFileSizeInBytes' => $this->getImageMinFileSize(),
131 ]
132 )
133 );
134 }
135
136 if ($fileData->getWidth() < $this->getImageMinWidth() || $fileData->getHeight() < $this->getImageMinHeight())
137 {
138 return $result->addError(
139 new UploaderError(
141 [
142 'minWidth' => $this->getImageMinWidth(),
143 'minHeight' => $this->getImageMinHeight(),
144 ]
145 )
146 );
147 }
148
149 if ($fileData->getWidth() > $this->getImageMaxWidth() || $fileData->getHeight() > $this->getImageMaxHeight())
150 {
151 return $result->addError(
152 new UploaderError(
154 [
155 'maxWidth' => $this->getImageMaxWidth(),
156 'maxHeight' => $this->getImageMaxHeight(),
157 ]
158 )
159 );
160 }
161
162 return $result;
163 }
164
165 public function getMaxFileSize(): ?int
166 {
167 return $this->maxFileSize;
168 }
169
170 public function setMaxFileSize(?int $maxFileSize): self
171 {
172 $this->maxFileSize = $maxFileSize;
173
174 return $this;
175 }
176
177 public function getMinFileSize(): int
178 {
179 return $this->minFileSize;
180 }
181
182 public function setMinFileSize(int $minFileSize): self
183 {
184 $this->minFileSize = $minFileSize;
185
186 return $this;
187 }
188
189 public function shouldAcceptOnlyImages(): bool
190 {
191 return $this->acceptOnlyImages;
192 }
193
194 public function getAcceptedFileTypes(): array
195 {
196 return $this->acceptedFileTypes;
197 }
198
200 {
201 $this->acceptedFileTypes = $acceptedFileTypes;
202 $this->acceptOnlyImages = false;
203
204 return $this;
205 }
206
207 public function setAcceptOnlyImages(bool $flag = true): self
208 {
209 $this->acceptOnlyImages = $flag;
210
211 if ($flag)
212 {
213 $this->acceptOnlyImages();
214 }
215
216 return $this;
217 }
218
219 public function acceptOnlyImages(): self
220 {
221 $imageExtensions = static::getImageExtensions();
222 $this->setAcceptedFileTypes($imageExtensions);
223 $this->acceptOnlyImages = true;
224
225 return $this;
226 }
227
228 public static function getImageExtensions(bool $withDot = true): array
229 {
230 $imageExtensions = explode(',', \CFile::getImageExtensions());
231
232 return array_map(function($extension) use($withDot) {
233 return ($withDot ? '.' : '') . trim($extension);
234 }, $imageExtensions);
235 }
236
237 public static function getVideoExtensions(bool $withDot = true): array
238 {
239 $extensions = [
240 'avi',
241 'wmv',
242 'mp4',
243 'mov',
244 'webm',
245 'flv',
246 'm4v',
247 'mkv',
248 'vob',
249 '3gp',
250 'ogv',
251 'h264',
252 ];
253
254 if ($withDot)
255 {
256 return array_map(function($extension) {
257 return '.' . $extension;
258 }, $extensions);
259 }
260
261 return $extensions;
262 }
263
264 public function getIgnoredFileNames(): array
265 {
266 return $this->ignoredFileNames;
267 }
268
269 public function setIgnoredFileNames(array $fileNames): self
270 {
271 $this->ignoredFileNames = [];
272 foreach ($fileNames as $fileName)
273 {
274 if (is_string($fileName) && mb_strlen($fileName) > 0)
275 {
276 $this->ignoredFileNames[] = mb_strtolower($fileName);
277 }
278 }
279
280 return $this;
281 }
282
283 public function getImageMinWidth(): int
284 {
285 return $this->imageMinWidth;
286 }
287
288 public function setImageMinWidth(int $imageMinWidth): self
289 {
290 $this->imageMinWidth = $imageMinWidth;
291
292 return $this;
293 }
294
295 public function getImageMinHeight(): int
296 {
297 return $this->imageMinHeight;
298 }
299
300 public function setImageMinHeight(int $imageMinHeight): self
301 {
302 $this->imageMinHeight = $imageMinHeight;
303
304 return $this;
305 }
306
307 public function getImageMaxWidth(): int
308 {
309 return $this->imageMaxWidth;
310 }
311
312 public function setImageMaxWidth(int $imageMaxWidth): self
313 {
314 $this->imageMaxWidth = $imageMaxWidth;
315
316 return $this;
317 }
318
319 public function getImageMaxHeight(): int
320 {
321 return $this->imageMaxHeight;
322 }
323
324 public function setImageMaxHeight(int $imageMaxHeight): self
325 {
326 $this->imageMaxHeight = $imageMaxHeight;
327
328 return $this;
329 }
330
331 public function getImageMaxFileSize(): ?int
332 {
333 return $this->imageMaxFileSize;
334 }
335
336 public function setImageMaxFileSize(?int $imageMaxFileSize): self
337 {
338 $this->imageMaxFileSize = $imageMaxFileSize;
339
340 return $this;
341 }
342
343 public function getImageMinFileSize(): int
344 {
345 return $this->imageMinFileSize;
346 }
347
348 public function setImageMinFileSize(int $imageMinFileSize): self
349 {
350 $this->imageMinFileSize = $imageMinFileSize;
351
352 return $this;
353 }
354
355 public function getIgnoreUnknownImageTypes(): bool
356 {
357 return $this->ignoreUnknownImageTypes;
358 }
359
360 public function setIgnoreUnknownImageTypes(bool $flag): self
361 {
362 $this->ignoreUnknownImageTypes = $flag;
363
364 return $this;
365 }
366
367 public function shouldTreatOversizeImageAsFile(): bool
368 {
369 return $this->treatOversizeImageAsFile;
370 }
371
372 public function setTreatOversizeImageAsFile(bool $flag): self
373 {
374 $this->treatOversizeImageAsFile = $flag;
375
376 return $this;
377 }
378}
static getValue($name)
Определения configuration.php:24
static unformatInt(string $str)
Определения ini.php:19
setImageMinWidth(int $imageMinWidth)
Определения Configuration.php:288
static getImageExtensions(bool $withDot=true)
Определения Configuration.php:228
validateImage(FileData $fileData)
Определения Configuration.php:101
setImageMaxHeight(int $imageMaxHeight)
Определения Configuration.php:324
__construct(array $options=[])
Определения Configuration.php:24
setAcceptOnlyImages(bool $flag=true)
Определения Configuration.php:207
setIgnoreUnknownImageTypes(bool $flag)
Определения Configuration.php:360
setAcceptedFileTypes(array $acceptedFileTypes)
Определения Configuration.php:199
setImageMaxFileSize(?int $imageMaxFileSize)
Определения Configuration.php:336
setIgnoredFileNames(array $fileNames)
Определения Configuration.php:269
setMinFileSize(int $minFileSize)
Определения Configuration.php:182
shouldTreatImageAsFile(FileData|array $fileData)
Определения Configuration.php:84
static getVideoExtensions(bool $withDot=true)
Определения Configuration.php:237
setImageMinFileSize(int $imageMinFileSize)
Определения Configuration.php:348
setImageMaxWidth(int $imageMaxWidth)
Определения Configuration.php:312
setMaxFileSize(?int $maxFileSize)
Определения Configuration.php:170
setTreatOversizeImageAsFile(bool $flag)
Определения Configuration.php:372
setImageMinHeight(int $imageMinHeight)
Определения Configuration.php:300
const IMAGE_MAX_FILE_SIZE_EXCEEDED
Определения UploaderError.php:18
const IMAGE_MIN_FILE_SIZE_EXCEEDED
Определения UploaderError.php:19
$options
Определения commerceml2.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$settings
Определения product_settings.php:43
$fileName
Определения quickway.php:305
$optionName
Определения options.php:1735
$optionValue
Определения options.php:3512