Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
grabber.php
1<?php
3
8
9
13{
14 use Translate\Controller\ProcessParams;
15
16 const START_PATH = '/bitrix/modules';
17
18 const SETTING_ID = 'TRANSLATE_LANGPACK';
19
20 const ACTION_COLLECT = 'collect';
21 const ACTION_PACK = 'pack';
22 const ACTION_DOWNLOAD = 'download';
23 const ACTION_UPLOAD = 'upload';
24 const ACTION_EXTRACT = 'extract';
25 const ACTION_APPLY = 'apply';
26 const ACTION_APPLY_PUBLIC = 'apply_public';
27 const ACTION_FINALIZE = 'finalize';
28 const ACTION_PURGE = 'purge';
29 const ACTION_CANCEL = 'cancel';
30 const ACTION_CLEAR = 'clear';
31
33 private $archiveFilePath;
35 private $archiveFileType;
36
37
38
44 public function configureActions()
45 {
46 $configureActions = parent::configureActions();
48 $permissionSource = new Translate\Controller\CheckPermission(Translate\Permission::SOURCE);
49
50 $configureActions[self::ACTION_COLLECT] = [
51 'class' => Translate\Controller\Asset\Collect::class,
52 '+prefilters' => [
53 $permission
54 ],
55 ];
56 $configureActions[self::ACTION_EXTRACT] = [
57 'class' => Translate\Controller\Asset\Extract::class,
58 '+prefilters' => [
59 $permission,
60 $permissionSource
61 ],
62 ];
63 $configureActions[self::ACTION_APPLY] = [
64 'class' => Translate\Controller\Asset\Apply::class,
65 '+prefilters' => [
66 $permission,
67 $permissionSource
68 ],
69 ];
70 $configureActions[self::ACTION_APPLY_PUBLIC] = [
71 'class' => Translate\Controller\Asset\ApplyPublic::class,
72 '+prefilters' => [
73 $permission,
74 $permissionSource
75 ],
76 ];
77 $configureActions[self::ACTION_PACK] = [
78 'class' => Translate\Controller\Asset\Pack::class,
79 '+prefilters' => [
80 $permission
81 ],
82 ];
83 $configureActions[self::ACTION_UPLOAD] = [
84 '+prefilters' => [
85 $permission
86 ],
87 ];
88 $configureActions[self::ACTION_DOWNLOAD] = [
89 '-prefilters' => [
90 Main\Engine\ActionFilter\Csrf::class,
91 ],
92 '+prefilters' => [
93 $permission
94 ],
95 ];
96 $configureActions[self::ACTION_PURGE] = [
97 '+prefilters' => [
98 $permission
99 ],
100 ];
101 $configureActions[self::ACTION_CANCEL] = [
102 '+prefilters' => [
103 $permission
104 ],
105 ];
106 $configureActions[self::ACTION_CLEAR] = [
107 '+prefilters' => [
108 $permission
109 ],
110 ];
111 $configureActions[self::ACTION_FINALIZE] = [
112 '+prefilters' => [
113 $permission
114 ],
115 ];
116
117 return $configureActions;
118 }
119
125 protected function init()
126 {
127 parent::init();
128 $this->keepField(['archiveFilePath', 'archiveFileType']);
129 }
130
136 public function uploadAction(): array
137 {
138 $result = [];
139 $success = false;
140 if (
141 isset($_FILES, $_FILES['tarFile'], $_FILES['tarFile']['tmp_name']) &&
142 ($_FILES['tarFile']['error'] == \UPLOAD_ERR_OK) &&
143 \file_exists($_FILES['tarFile']['tmp_name'])
144 )
145 {
146 if (
147 (\filesize($_FILES['tarFile']['tmp_name']) > 0) &&
148 (
149 \mb_substr($_FILES['tarFile']['name'], -7) === '.tar.gz' ||
150 \mb_substr($_FILES['tarFile']['name'], -4) === '.tar'
151 )
152 )
153 {
154 if (\mb_substr($_FILES['tarFile']['name'], -7) === '.tar.gz')
155 {
156 $suffix = '.tar.gz';
157 }
158 else
159 {
160 $suffix = '.tar';
161 }
162
163 if ($this->moveUploadedFile($_FILES['tarFile'], $suffix))
164 {
165 $this->saveProgressParameters();
166 $success = ($this->hasErrors() === false);
167 }
168 }
169 else
170 {
171 $this->addError(new Main\Error(Loc::getMessage('TR_ERROR_TARFILE_EXTENTION')));
172 }
173 }
174 else
175 {
176 if ($_FILES['tarFile']['error'] == UPLOAD_ERR_INI_SIZE)
177 {
178 $this->addError(
179 new Main\Error(Loc::getMessage('TR_ERROR_UPLOAD_SIZE', [
180 '#SIZE#' => \CFile::formatSize(self::getMaxUploadSize())
181 ]))
182 );
183 }
184 else
185 {
186 $this->addError(new Main\Error(Loc::getMessage('TR_ERROR_TARFILE')));
187 }
188 }
189
190 if ($success)
191 {
192 $result['SUMMARY'] = Loc::getMessage('TR_IMPORT_UPLOAD_OK');
193 }
194
195 $result['STATUS'] = Translate\Controller\STATUS_COMPLETED;
196
197 return $result;
198 }
199
200
201
211 private function moveUploadedFile($postedFile, $suffix = '.tar', $timeToLive = 3): bool
212 {
213 if (
214 isset($postedFile['tmp_name']) &&
215 \file_exists($postedFile['tmp_name'])
216 )
217 {
219 $tmpFile = Translate\IO\File::generateTemporalFile('translate', $suffix, $timeToLive);
220 if (@\copy($postedFile['tmp_name'], $tmpFile->getPhysicalPath()))
221 {
222 $this->archiveFileType = $suffix;
223 $this->archiveFilePath = $tmpFile->getPhysicalPath();
224 return true;
225 }
226 }
227
228 $this->addError(new Main\Error(Loc::getMessage('TR_IMPORT_EMPTY_FILE_ERROR')));
229
230 return false;
231 }
232
233
239 public function finalizeAction(): array
240 {
241 $settings = $this->getProgressParameters();
242
243 // delete tmp files
244 if (!empty($settings['tmpFolderPath']))
245 {
246 $tempLanguageDir = new Translate\IO\Directory($settings['tmpFolderPath']);
247 if ($tempLanguageDir->isExists())
248 {
249 if ($tempLanguageDir->delete() !== true)
250 {
251 $this->addError(new Main\Error(Loc::getMessage('TR_ERROR_DELETE_TEMP_FOLDER')));
252 }
253 }
254 }
255
256 return [
257 'STATUS' => Translate\Controller\STATUS_COMPLETED
258 ];
259 }
260
261
267 public function clearAction(): array
268 {
269 return $this->purgeAction();
270 }
271
272
278 public function purgeAction(): array
279 {
280 $settings = $this->getProgressParameters();
281
282 if (!empty($settings['archiveFilePath']))
283 {
284 $path = new Main\IO\File($settings['archiveFilePath']);
285 if ($path->isExists())
286 {
287 $path->delete();
288 }
289 }
290
291 return [
292 'SUMMARY' => Loc::getMessage('TR_EXPORT_FILE_DROPPED'),
293 'STATUS' => Translate\Controller\STATUS_COMPLETED
294 ];
295 }
296
297
303 public function cancelAction(): array
304 {
305 $this->finalizeAction();
306 $this->purgeAction();
308
309 $cancelingAction = $this->request->get('cancelingAction');
310 $summary =
311 \in_array($cancelingAction, [self::ACTION_COLLECT, self::ACTION_PACK]) ?
312 Loc::getMessage('TR_EXPORT_ACTION_CANCELED') :
313 Loc::getMessage('TR_IMPORT_ACTION_CANCELED')
314 ;
315
316 return [
317 'SUMMARY' => $summary,
318 'STATUS' => Translate\Controller\STATUS_COMPLETED
319 ];
320 }
321
322
328 public function downloadAction()
329 {
330 $settings = $this->getProgressParameters();
331
332 if (!empty($settings['downloadParams']['filePath']) && !empty($settings['downloadParams']['fileName']))
333 {
334 $file = new Main\IO\File($settings['downloadParams']['filePath']);
335 if ($file->isExists())
336 {
337 $response = new Main\Engine\Response\File(
338 $file->getPath(),
339 $settings['downloadParams']['fileName'],
340 $settings['downloadParams']['fileType']
341 );
342
343 return $response;
344 }
345 }
346
347 $this->addError(new Error('File not found'));
348 }
349
350
357 {
358 return self::SETTING_ID;
359 }
360
361
367 public static function getMaxUploadSize()
368 {
369 static $maxUploadSize = -1;
370 if ($maxUploadSize < 0)
371 {
372 $maxUploadSize = \min(
373 \CUtil::unformat('32M'),
374 \CUtil::unformat(\ini_get('post_max_size')),
375 \CUtil::unformat(\ini_get('upload_max_filesize'))
376 );
377 }
378
379 return $maxUploadSize;
380 }
381}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29