1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
FieldFileUploaderController.php
См. документацию.
1<?php
2
4
12use CUser;
14
16{
17 protected function isAuthorized(): bool
18 {
19 $currentUser = (isset($USER) && $USER instanceof CUser) ? $USER : new CUser();
20
21 return $currentUser->IsAuthorized();
22 }
23
24 public function __construct(array $options)
25 {
26 $options = [
27 'id' => (isset($options['id']) && $options['id'] > 0) ? (int)$options['id'] : 0,
28 'cid' => (
29 (
30 isset($options['cid'])
31 && is_string($options['cid'])
32 && preg_match('/^[a-f01-9]{32}$/', $options['cid'])
33 )
34 ? $options['cid']
35 : ''
36 ),
37 'entityId' => (isset($options['entityId']) && is_string($options['entityId'])) ? $options['entityId'] : '',
38 'fieldName' =>
39 (isset($options['fieldName']) && is_string($options['fieldName']))
40 ? $options['fieldName']
41 : ''
42 ,
43 'multiple' => (isset($options['multiple']) && is_bool($options['multiple']) && $options['multiple']),
44 'signedFileId' => (isset($options['signedFileId']) && is_string($options['signedFileId']))
45 ? $options['signedFileId']
46 : ''
47 ];
48
49 parent::__construct($options);
50 }
51
52 public function isAvailable(): bool
53 {
54 return $this->isAuthorized();
55 }
56
57 protected function parseFileExtensions(array $extensionsSetting): array
58 {
59 $result = [];
60 foreach($extensionsSetting as $key => $extension)
61 {
62 if($extension === true)
63 {
64 $extension = trim((string)$key);
65 }
66 else
67 {
68 $extension = trim((string)$extension);
69 }
70 $dotPos = mb_strrpos($extension, '.');
71 if ($dotPos !== false)
72 {
73 $extension = mb_substr($extension, $dotPos + 1);
74 }
75 if($extension !== '')
76 {
77 $result[".$extension"] = true;
78 }
79 }
80 if (!empty($result))
81 {
82 $result = array_keys($result);
83 }
84
85 return $result;
86 }
87
89 {
92
93 $configuration = new Configuration();
94
95 $isSetMaxAllowedSize = false;
96 $fieldSettings = null;
97 $fieldName = $this->getOption('fieldName', '');
98 $fieldInfo =
99 $USER_FIELD_MANAGER->GetUserFields(
100 $this->getOption('entityId', ''),
101 0,
102 LANGUAGE_ID,
103 false,
104 [$this->getOption('fieldName', '')]
105 )
106 ;
107 if (
108 isset($fieldInfo[$fieldName])
109 && is_array($fieldInfo[$fieldName])
110 && isset($fieldInfo[$fieldName]['SETTINGS'])
111 && is_array($fieldInfo[$fieldName]['SETTINGS'])
112 )
113 {
114 $fieldSettings = $fieldInfo[$fieldName]['SETTINGS'];
115 if (
116 isset($fieldSettings['MAX_ALLOWED_SIZE'])
117 && $fieldSettings['MAX_ALLOWED_SIZE'] > 0
118 )
119 {
120 $configuration->setMaxFileSize((int)$fieldSettings['MAX_ALLOWED_SIZE']);
121 $isSetMaxAllowedSize = true;
122 }
123 if (
124 isset($fieldSettings['EXTENSIONS'])
125 && is_array($fieldSettings['EXTENSIONS'])
126 && !empty($fieldSettings['EXTENSIONS'])
127 )
128 {
129 $fileExtensions = $this->parseFileExtensions($fieldSettings['EXTENSIONS']);
130 if (!empty($fileExtensions))
131 {
132 $configuration->setAcceptedFileTypes($fileExtensions);
133 }
134 }
135 }
136
137 if (!$isSetMaxAllowedSize)
138 {
139 $configuration->setMaxFileSize(null);
140 }
141
142 $configuration->setTreatOversizeImageAsFile(true);
143
144 return $configuration;
145 }
146
147 public function canUpload()
148 {
149 $cid = $this->getOptions()['cid'] ?? null;
150
151 return $cid && FileInputUtility::instance()->isCidRegistered($cid);
152 }
153
154 protected function isFileInputUtilityAccessible(): bool
155 {
156 return FileInputUtility::instance()->isAccessible();
157 }
158
159 protected function getControlId(): string
160 {
161 $options = $this->getOptions();
162 $userField = [
163 'ID' => $options['id'] ?? 0,
164 'ENTITY_ID' => $options['entityId'] ?? '',
165 'FIELD_NAME' => $options['fieldName'] ?? '',
166 'MULTIPLE' => $options['multiple'] ? 'Y' : 'N',
167 ];
168
169 return FileInputUtility::instance()->getUserFieldCid($userField);
170 }
171
172 protected function registerControl(string $controlId): string
173 {
174 return FileInputUtility::instance()->registerControl($this->getOption('cid', ''), $controlId);
175 }
176
177 protected function registerFile(string $cid, int $fileId)
178 {
179 FileInputUtility::instance()->registerFile($cid, $fileId);
180 }
181
182 protected function registerUploaderFile(int $fileId, string $tempFileToken): void
183 {
184 if ($this->isFileInputUtilityAccessible())
185 {
186 $controlId = $this->getControlId();
187 $cid = $this->registerControl($controlId);
188
189 if ($fileId > 0)
190 {
191 $this->registerFile($cid, $fileId);
192 $this->registerTemporaryFileData($fileId, $controlId, $cid, $tempFileToken);
193 }
194 }
195 }
196
197 protected function checkFiles(array $fileIds): array
198 {
199 return FileInputUtility::instance()->checkFiles($this->getControlId(), $fileIds);
200 }
201
202 public function canView(): bool
203 {
204 return true;
205 }
206
208 {
209 $options = $this->getOptions();
210
211 if ($options['signedFileId']) // view mode
212 {
213 foreach ($files as $file)
214 {
215 $fileSigner = (new UploaderFileSigner($options['entityId'], $options['fieldName']));
216
217 if ($fileSigner->verify($options['signedFileId'], $file->getId()))
218 {
219 $file->markAsOwn();
220 }
221 }
222 }
223
224 if ($options['cid']) // edit mode
225 {
226 foreach ($files as $file)
227 {
228 $fileIds[] = $file->getId();
229 }
230
231 $fileIds = $this->checkFiles($fileIds);
232
233 foreach ($files as $file)
234 {
235 if (in_array($file->getId(), $fileIds, true))
236 {
237 $file->markAsOwn();
238 }
239 }
240 }
241 }
242
243 public function canRemove(): bool
244 {
245 return true;
246 }
247
248 public function onUploadComplete(UploadResult $uploadResult): void
249 {
250 $fileInfo = $uploadResult->getFileInfo();
251
252 if ($fileInfo === null)
253 {
254 return;
255 }
256
257 $session = Application::getInstance()->getSession();
258 $session->save();
259 $session->start();
260
261 $fileId = $fileInfo->getFileId();
262 $downloadUrl = $fileInfo->getPreviewUrl();
263 if (is_string($downloadUrl) && $downloadUrl !== '')
264 {
265 $fileInfo->setDownloadUrl('');
266 }
267 $previewUrl = $fileInfo->getPreviewUrl();
268 if (is_string($previewUrl) && $previewUrl !== '')
269 {
270 $fileInfo->setPreviewUrl('', 0, 0);
271 }
272 $this->registerUploaderFile($fileId, $uploadResult->getToken());
273 $fileInfo->setCustomData(['realFileId' => $fileId]);
274 }
275
276 private function registerTemporaryFileData(int $fileId, string $controlId, string $cid, string $tempFileToken): void
277 {
278 UploadedFilesRegistry::getInstance()->registerFile($fileId, $controlId, $cid, $tempFileToken);
279 }
280}
static getInstance()
Определения application.php:98
registerUploaderFile(int $fileId, string $tempFileToken)
verifyFileOwner(FileOwnershipCollection $files)
getOption(string $option, $defaultValue=null)
Определения UploaderController.php:58
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
global $USER_FIELD_MANAGER
Определения attempt.php:6
$result
Определения get_property_values.php:14
global $USER
Определения csv_new_run.php:40
$files
Определения mysql_to_pgsql.php:30
if(empty($signedUserToken)) $key
Определения quickway.php:257