Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filetype.php
1<?php
2
4
8use CUserTypeManager;
9
10Loc::loadMessages(__FILE__);
11
16class FileType extends BaseType
17{
18 public const
19 USER_TYPE_ID = 'file',
20 RENDER_COMPONENT = 'bitrix:main.field.file';
21
25 public static function getDescription(): array
26 {
27 return [
28 'DESCRIPTION' => Loc::getMessage('USER_TYPE_FILE_DESCRIPTION'),
29 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_FILE,
30 ];
31 }
32
36 public static function getDbColumnType(): string
37 {
38 $connection = \Bitrix\Main\Application::getConnection();
39 $helper = $connection->getSqlHelper();
40 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\IntegerField('x'));
41 }
42
47 public static function prepareSettings(array $userField): array
48 {
49 $size = (int)($userField['SETTINGS']['SIZE'] ?? 0);
50 $resultExtensions = [];
51
52 $extensions = [];
53 if (isset($userField['SETTINGS']['EXTENSIONS']))
54 {
55 if(is_array($userField['SETTINGS']['EXTENSIONS']))
56 {
57 $extensions = $userField['SETTINGS']['EXTENSIONS'];
58 }
59 else
60 {
61 $extensions = explode(',', $userField['SETTINGS']['EXTENSIONS']);
62 }
63 }
64
65 foreach($extensions as $key => $extension)
66 {
67 if($extension === true)
68 {
69 $extension = trim($key);
70 }
71 else
72 {
73 $extension = trim($extension);
74 }
75
76 if(!empty($extension))
77 {
78 $resultExtensions[$extension] = true;
79 }
80 }
81
82 $targetBlank = (($userField['SETTINGS']['TARGET_BLANK'] ?? 'Y') === 'N' ? 'N' : 'Y');
83
84 return [
85 'SIZE' => ($size <= 1 ? 20 : ($size > 255 ? 225 : $size)),
86 'LIST_WIDTH' => (int)($userField['SETTINGS']['LIST_WIDTH'] ?? 0),
87 'LIST_HEIGHT' => (int)($userField['SETTINGS']['LIST_HEIGHT'] ?? 0),
88 'MAX_SHOW_SIZE' => (int)($userField['SETTINGS']['MAX_SHOW_SIZE'] ?? 0),
89 'MAX_ALLOWED_SIZE' => (int)($userField['SETTINGS']['MAX_ALLOWED_SIZE'] ?? 0),
90 'EXTENSIONS' => $resultExtensions,
91 'TARGET_BLANK' => $targetBlank
92 ];
93 }
94
95 public static function getFilterHtml(array $userField, ?array $additionalParameters): string
96 {
97 return '&nbsp;';
98 }
99
105 public static function checkFields(array $userField, $value): array
106 {
107 if(!is_array($value) && $value)
108 {
109 $fileInfo = \CFile::GetFileArray($value);
110 if($fileInfo)
111 {
112 $value = \CFile::MakeFileArray($fileInfo['SRC']);
113 }
114 }
115
116 $fieldName = HtmlFilter::encode(
117 empty($userField['EDIT_FORM_LABEL'])
118 ? $userField['FIELD_NAME']
119 : $userField['EDIT_FORM_LABEL']
120 );
121
122 if(
123 is_array($value)
124 && (!isset($value['tmp_name']) && !isset($value['old_id']) && !isset($value['type']))
125 )
126 {
127 return [
128 [
129 'id' => $userField['FIELD_NAME'],
130 'text' => Loc::getMessage('USER_TYPE_FILE_VALUE_IS_MULTIPLE', [
131 '#FIELD_NAME#' => $fieldName,
132 ]),
133 ],
134 ];
135 }
136
137 $msg = [];
138
139 if(is_array($value))
140 {
141 if(
142 $userField['SETTINGS']['MAX_ALLOWED_SIZE'] > 0
143 &&
144 $value['size'] > $userField['SETTINGS']['MAX_ALLOWED_SIZE']
145 )
146 {
147 $msg[] = [
148 'id' => $userField['FIELD_NAME'],
149 'text' => Loc::getMessage('USER_TYPE_FILE_MAX_SIZE_ERROR',
150 [
151 '#FIELD_NAME#' => $fieldName,
152 '#MAX_ALLOWED_SIZE#' => $userField['SETTINGS']['MAX_ALLOWED_SIZE']
153 ]
154 ),
155 ];
156 }
157
158 //Extention check
159 if(
160 is_array($userField['SETTINGS']['EXTENSIONS'])
161 &&
162 count($userField['SETTINGS']['EXTENSIONS'])
163 )
164 {
165 foreach($userField['SETTINGS']['EXTENSIONS'] as $ext => $tmp_val)
166 {
167 $userField['SETTINGS']['EXTENSIONS'][$ext] = $ext;
168 }
169 $error = \CFile::CheckFile(
170 $value,
171 0,
172 false,
173 implode(',', $userField['SETTINGS']['EXTENSIONS'])
174 );
175 }
176 else
177 {
178 $error = '';
179 }
180
181 if($error)
182 {
183 $msg[] = [
184 'id' => $userField['FIELD_NAME'],
185 'text' => $error,
186 ];
187 }
188
189 //For user without edit php permissions we allow only pictures upload
190 global $USER;
191 if(!is_object($USER) || !$USER->IsAdmin())
192 {
193 if(HasScriptExtension($value['name']))
194 {
195 $msg[] = [
196 'id' => $userField['FIELD_NAME'],
197 'text' => Loc::getMessage('FILE_BAD_TYPE') . ' (' . $value['name'] . ').'
198 ];
199 }
200 }
201 }
202
203 return $msg;
204 }
205
206 public static function onBeforeSave(array $userField, $value)
207 {
208 // old mechanism
209 if(is_array($value))
210 {
211 $userFieldValues = (is_array($userField['VALUE']) ? $userField['VALUE'] : [$userField['VALUE']]);
212 $valueHasOldId = !empty($value['old_id']);
213
214 //Protect from user manipulation
215 if($valueHasOldId)
216 {
217 $value['old_id'] = (is_array($value['old_id']) ? $value['old_id'] : [$value['old_id']]);
218 foreach ($value['old_id'] as $key => $oldId)
219 {
220 if(!in_array($oldId, $userFieldValues))
221 {
222 unset($value['old_id'][$key]);
223 }
224 }
225
226 if ($value['del'])
227 {
228 foreach ($value['old_id'] as $oldId)
229 {
230 \CFile::Delete($oldId);
231 }
232 $value['old_id'] = false;
233 }
234 }
235
236 if($value['error'])
237 {
238 return (is_array($value['old_id']) ? $value['old_id'][0] : $value['old_id']);
239 }
240
241 $value['MODULE_ID'] = 'main';
242
243 if ($valueHasOldId && is_array($value['old_id']))
244 {
245 foreach ($value['old_id'] as $oldId)
246 {
247 \CFile::Delete($oldId);
248 }
249 }
250
251 if (!empty($value['name']))
252 {
253 if (
254 $valueHasOldId
255 && is_array($value['old_id'])
256 && isset($value['ID'])
257 && in_array($value['ID'], $value['old_id'])
258 )
259 {
260 return $value['ID'];
261 }
262
263 return \CFile::SaveFile($value, 'uf');
264 }
265
266 return false;
267 }
268
269 // new mechanism - mail.file.input
270 $fileInputUtility = FileInputUtility::instance();
271 $controlId = $fileInputUtility->getUserFieldCid($userField);
272
273 if($value > 0)
274 {
275 $delResult = $fileInputUtility->checkDeletedFiles($controlId);
276 if(in_array($value, $delResult))
277 {
278 return false;
279 }
280
281 if (is_array($userField['VALUE']) && in_array($value, $userField['VALUE']))
282 {
283 return $value;
284 }
285
286 if (!is_array($userField['VALUE']) && (int)$userField['VALUE'] === $value)
287 {
288 return $value;
289 }
290
291 $checkResult = $fileInputUtility->checkFiles($controlId, [$value]);
292 if(!in_array($value, $checkResult))
293 {
294 $value = false;
295 }
296 }
297 return $value;
298 }
299
300 public static function onSearchIndex(array $userField): ?string
301 {
302 static $max_file_size = null;
303 $result = '';
304
305 if(is_array($userField['VALUE']))
306 {
307 $value = $userField['VALUE'];
308 }
309 else
310 {
311 $value = [$userField['VALUE']];
312 }
313
314 $value = array_filter($value, 'strlen');
315 if(!empty($value))
316 {
317 $value = array_map([static::class, 'getFileContent'], $value);
318 $result = implode('\r\n', $value);
319 }
320
321 return $result;
322 }
323
324 public static function getFileContent($fileId)
325 {
326 static $maxFileSize = null;
327
328 $file = \CFile::MakeFileArray($fileId);
329 if($file && isset($file['tmp_name']))
330 {
331 if(!isset($maxFileSize))
332 {
333 $optionInt = \COption::GetOptionInt(
334 'search',
335 'max_file_size',
336 0
337 );
338 $maxFileSize = $optionInt * 1024;
339 }
340
341 if($maxFileSize > 0 && $file['size'] > $maxFileSize)
342 {
343 return '';
344 }
345
346 $files = false;
347 $events = GetModuleEvents(
348 'search',
349 'OnSearchGetFileContent',
350 true
351 );
352 foreach($events as $event)
353 {
354 if($files = ExecuteModuleEventEx($event, [$file['tmp_name']]))
355 {
356 break;
357 }
358 }
359
360 if(is_array($files))
361 {
362 return $files['CONTENT'];
363 }
364 }
365
366 return '';
367 }
368
369 public static function getPublicEditMultiple(array $userField, ?array $additionalParameters = []): string
370 {
371 return parent::getPublicEdit($userField, $additionalParameters);
372 }
373
374 public static function canUseArrayValueForSingleField(): bool
375 {
376 return true;
377 }
378}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static encode($string, $flags=ENT_COMPAT, $doubleEncode=true)
static onBeforeSave(array $userField, $value)
Definition filetype.php:206
static getFilterHtml(array $userField, ?array $additionalParameters)
Definition filetype.php:95
static onSearchIndex(array $userField)
Definition filetype.php:300
static checkFields(array $userField, $value)
Definition filetype.php:105
static prepareSettings(array $userField)
Definition filetype.php:47
static getPublicEditMultiple(array $userField, ?array $additionalParameters=[])
Definition filetype.php:369