Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Configuration.php
1<?php
2
4
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 {
46 $optionValue = $options[$optionName];
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 {
54 $optionValue = Ini::unformatInt($optionValue);
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 = [];
75 $configuration = \Bitrix\Main\Config\Configuration::getValue('ui');
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 {
107 return $result->addError(new UploaderError(UploaderError::IMAGE_TYPE_NOT_SUPPORTED));
108 }
109
110 if ($this->getImageMaxFileSize() !== null && $fileData->getSize() > $this->getImageMaxFileSize())
111 {
112 return $result->addError(
113 new UploaderError(
114 UploaderError::IMAGE_MAX_FILE_SIZE_EXCEEDED,
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(
127 UploaderError::IMAGE_MIN_FILE_SIZE_EXCEEDED,
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(
140 UploaderError::IMAGE_IS_TOO_SMALL,
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(
153 UploaderError::IMAGE_IS_TOO_BIG,
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
199 public function setAcceptedFileTypes(array $acceptedFileTypes): self
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(): array
229 {
230 $imageExtensions = explode(',', \CFile::getImageExtensions());
231
232 return array_map(function($extension) {
233 return '.' . ltrim($extension);
234 }, $imageExtensions);
235 }
236
237 public function getIgnoredFileNames(): array
238 {
239 return $this->ignoredFileNames;
240 }
241
242 public function setIgnoredFileNames(array $fileNames): self
243 {
244 $this->ignoredFileNames = [];
245 foreach ($fileNames as $fileName)
246 {
247 if (is_string($fileName) && mb_strlen($fileName) > 0)
248 {
249 $this->ignoredFileNames[] = mb_strtolower($fileName);
250 }
251 }
252
253 return $this;
254 }
255
256 public function getImageMinWidth(): int
257 {
258 return $this->imageMinWidth;
259 }
260
261 public function setImageMinWidth(int $imageMinWidth): self
262 {
263 $this->imageMinWidth = $imageMinWidth;
264
265 return $this;
266 }
267
268 public function getImageMinHeight(): int
269 {
270 return $this->imageMinHeight;
271 }
272
273 public function setImageMinHeight(int $imageMinHeight): self
274 {
275 $this->imageMinHeight = $imageMinHeight;
276
277 return $this;
278 }
279
280 public function getImageMaxWidth(): int
281 {
282 return $this->imageMaxWidth;
283 }
284
285 public function setImageMaxWidth(int $imageMaxWidth): self
286 {
287 $this->imageMaxWidth = $imageMaxWidth;
288
289 return $this;
290 }
291
292 public function getImageMaxHeight(): int
293 {
294 return $this->imageMaxHeight;
295 }
296
297 public function setImageMaxHeight(int $imageMaxHeight): self
298 {
299 $this->imageMaxHeight = $imageMaxHeight;
300
301 return $this;
302 }
303
304 public function getImageMaxFileSize(): ?int
305 {
306 return $this->imageMaxFileSize;
307 }
308
309 public function setImageMaxFileSize(?int $imageMaxFileSize): self
310 {
311 $this->imageMaxFileSize = $imageMaxFileSize;
312
313 return $this;
314 }
315
316 public function getImageMinFileSize(): int
317 {
318 return $this->imageMinFileSize;
319 }
320
321 public function setImageMinFileSize(int $imageMinFileSize): self
322 {
323 $this->imageMinFileSize = $imageMinFileSize;
324
325 return $this;
326 }
327
328 public function getIgnoreUnknownImageTypes(): bool
329 {
330 return $this->ignoreUnknownImageTypes;
331 }
332
333 public function setIgnoreUnknownImageTypes(bool $flag): self
334 {
335 $this->ignoreUnknownImageTypes = $flag;
336
337 return $this;
338 }
339
340 public function shouldTreatOversizeImageAsFile(): bool
341 {
342 return $this->treatOversizeImageAsFile;
343 }
344
345 public function setTreatOversizeImageAsFile(bool $flag): self
346 {
347 $this->treatOversizeImageAsFile = $flag;
348
349 return $this;
350 }
351}
static unformatInt(string $str)
Definition ini.php:19
setAcceptedFileTypes(array $acceptedFileTypes)
setImageMaxFileSize(?int $imageMaxFileSize)
shouldTreatImageAsFile(FileData|array $fileData)
setImageMinFileSize(int $imageMinFileSize)