Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
structure.php
1<?php
2
4
10use Bitrix\Disk\Driver;
12use Bitrix\Disk\Internals\ObjectTable;
13use CTempFile;
14use CFile;
15
17{
18 public const CODE_CONFIGURATION_FILES_LIST = 'CONFIGURATION_FILES_LIST';
19 public const CODE_FILES_LIST = 'FILES_LIST';
20 public const CODE_FILES_SMALL_LIST = 'FILES_SMALL_LIST';
21 public const CODE_UNPACK_FILE_PREFIX = 'UNPACK_FILE_';
22 public const CODE_CUSTOM_FILE = 'CUSTOM_FILE_';
23 private static $maxAgentTime = 10;
24 private static $fileDescriptionDelete = 'configuration_delete';
25 private $context;
26 private $setting;
27 private $zipMimeType = [
28 'application/zip',
29 'application/x-zip-compressed'
30 ];
34 public function __construct($context)
35 {
36 $this->context = $context;
37 $this->setting = new Setting($context);
38 $this->setting->addMultipleCode(self::CODE_CONFIGURATION_FILES_LIST);
39 $this->setting->addMultipleCode(self::CODE_FILES_LIST);
40 $this->setting->addMultipleCode(self::CODE_FILES_SMALL_LIST);
41 }
42
48 public function getFolder()
49 {
50 $folder = $this->setting->get('FOLDER');
51 if (empty($folder))
52 {
53 $folder = CTempFile::GetDirectoryName(
54 4,
55 [
56 'rest',
57 uniqid($this->context, true)
58 ]
59 );
60 if (CheckDirPath($folder))
61 {
62 $this->setting->set('FOLDER', $folder);
63 }
64 else
65 {
66 $folder = false;
67 }
68 }
69
70 return $folder ?: false;
71 }
72
81 public function saveContent($type, $code, $content)
82 {
83 $return = false;
84
85 try
86 {
87 if (is_array($content))
88 {
89 $content = Json::encode($content);
90 }
91 elseif (!is_string($content))
92 {
93 return $return;
94 }
95
96 $path = ($type === false ? '' : $type . '/') . $code . Helper::CONFIGURATION_FILE_EXTENSION;
97
98 $id = CFile::SaveFile(
99 [
100 'name' => $path,
101 'MODULE_ID' => 'rest',
102 'content' => $content,
103 'description' => self::$fileDescriptionDelete
104 ],
105 'configuration/export'
106 );
107
108 if ($id > 0)
109 {
110 $return = $this->saveConfigurationFile($id, $path);
111 }
112 }
113 catch (\Exception $e)
114 {
115 }
116
117 return $return;
118 }
119
126 private function saveConfigurationFile($id, $name)
127 {
128 $id = (int) $id;
129 $file = [
130 'ID' => $id,
131 'NAME' => $name
132 ];
133
134 return $this->setting->set(self::CODE_CONFIGURATION_FILES_LIST, $file);
135 }
136
141 public function getConfigurationFileList()
142 {
143 return $this->setting->get(self::CODE_CONFIGURATION_FILES_LIST);
144 }
145
152 public function saveFile($id, $additionalData = [])
153 {
154 $additionalData['ID'] = (int) $id;
155 return $this->setting->set(self::CODE_FILES_LIST, $additionalData);
156 }
157
162 public function getFileList()
163 {
164 return $this->setting->get(self::CODE_FILES_LIST);
165 }
166
173 public function unpack($fileInfo)
174 {
175 $result = false;
176 $fileContent = File::getFileContents($fileInfo["tmp_name"]);
177 if ($fileContent)
178 {
179 $type = (in_array($fileInfo["type"], $this->zipMimeType)) ? 'ZIP' : 'TAR.GZ';
180 $folder = $this->getFolder();
181 $filePath = $folder.'archive_tmp';
182
183 File::putFileContents($filePath, $fileContent);
184 $archive = \CBXArchive::GetArchive($filePath, $type);
185 $res = $archive->Unpack($folder);
186 if ($res)
187 {
188 $this->initUnpackFilesList();
189 $result = true;
190 }
191 File::deleteFile($filePath);
192 }
193
194 return $result;
195 }
196
201 public function unpackSmallFiles($content): bool
202 {
203 $result = true;
204 try
205 {
206 $files = Json::decode($content);
207 if (is_array($files))
208 {
209 $folder = $this->getFolder();
210 foreach ($files as $file)
211 {
212 if (!empty($file['CONTENT']))
213 {
214 $id = (int)$file['ID'];
215 if ($id > 0)
216 {
217 File::putFileContents(
218 $folder . Helper::STRUCTURE_FILES_NAME . '/' . $id,
219 base64_decode($file['CONTENT'])
220 );
221 unset($file['CONTENT']);
222 if (File::isFileExists($folder . Helper::STRUCTURE_FILES_NAME . '/' . $id))
223 {
224 $file['PATH'] = $folder . Helper::STRUCTURE_FILES_NAME . '/' . $id;
225 $this->setting->set(self::CODE_UNPACK_FILE_PREFIX . $id, $file);
226 }
227 }
228 }
229 }
230 }
231 }
232 catch (\Exception $e)
233 {
234 $result = false;
235 }
236
237 return $result;
238 }
239
240 private function initUnpackFilesList()
241 {
242 $folder = $this->getFolder();
244 {
245 $content = File::getFileContents(
246 $folder
249 );
250 $this->unpackSmallFiles($content);
251 }
252
253 if (File::isFileExists($folder.Helper::STRUCTURE_FILES_NAME.Helper::CONFIGURATION_FILE_EXTENSION))
254 {
255 $content = File::getFileContents($folder.Helper::STRUCTURE_FILES_NAME.Helper::CONFIGURATION_FILE_EXTENSION);
256 try
257 {
258 $files = Json::decode($content);
259 if (is_array($files))
260 {
261 foreach ($files as $file)
262 {
263 $id = (int) $file['ID'];
264 if ($id > 0 && File::isFileExists($folder . Helper::STRUCTURE_FILES_NAME . '/' . $id))
265 {
266 $file['PATH'] = $folder . Helper::STRUCTURE_FILES_NAME . '/' . $id;
267 $this->setting->set(self::CODE_UNPACK_FILE_PREFIX . $id, $file);
268 }
269 }
270 }
271 }
272 catch (\Exception $e)
273 {
274 }
275 }
276 }
277
285 public function addFileList(array $filesInfo, array $files): bool
286 {
287 foreach ($filesInfo as $file)
288 {
289 $id = (int) $file['ID'];
290 if ($id > 0 && $files[$id])
291 {
292 $file['PATH'] = $files[$id];
293 $this->setting->set(self::CODE_UNPACK_FILE_PREFIX . $id, $file);
294 }
295 }
296
297 return true;
298 }
299
307 public function setUnpackFilesFromDisk($folderId, $storageParams)
308 {
309 $result = false;
310 if (Loader::includeModule('disk'))
311 {
312 try
313 {
314 $storage = Driver::getInstance()->addStorageIfNotExist(
315 $storageParams
316 );
317 if ($storage)
318 {
319 $folder = $storage->getChild(
320 [
321 '=ID' => $folderId
322 ]
323 );
324 if ($folder)
325 {
326 $file = $folder->getChild(
327 [
329 ]
330 );
331 if ($file && $file->getFileId() > 0)
332 {
333 $server = Application::getInstance()->getContext()->getServer();
334 $documentRoot = $server->getDocumentRoot();
335 $filePath = $documentRoot . CFile::GetPath($file->getFileId());
336 if (File::isFileExists($filePath))
337 {
338 $content = File::getFileContents($filePath);
339 $fileList = Json::decode($content);
340 if ($fileList)
341 {
342 $subFolder = $folder->getChild(
343 [
345 ]
346 );
347 if ($subFolder)
348 {
349 $fileList = array_column($fileList, null, 'ID');
350 $fakeSecurityContext = Driver::getInstance()->getFakeSecurityContext();
351 $folderFiles = $subFolder->getChildren(
352 $fakeSecurityContext,
353 [
354 'filter' => [
355 '=TYPE' => ObjectTable::TYPE_FILE
356 ]
357 ]
358 );
359
360 foreach ($folderFiles as $file)
361 {
362 $id = $file->getOriginalName();
363 if (!empty($fileList[$id]))
364 {
365 $path = $documentRoot . CFile::GetPath(
366 $file->getFileId()
367 );
368 if (File::isFileExists($path))
369 {
370 $saveFile = $fileList[$id];
371 $saveFile['PATH'] = $path;
372 $this->setting->set(self::CODE_UNPACK_FILE_PREFIX . $file->getFileId(), $saveFile);
373 }
374 }
375 }
376
377 $result = true;
378 }
379 }
380 }
381 }
382 }
383 }
384 }
385 catch (\Exception $e)
386 {
387 }
388 }
389 return $result;
390 }
391
398 public function getUnpackFile($id)
399 {
400 return $this->setting->get(self::CODE_UNPACK_FILE_PREFIX . (int) $id);
401 }
402
408 public function addSmallFile($info, $content)
409 {
410 $info['CONTENT'] = base64_encode($content);
411 $this->setting->set(self::CODE_FILES_SMALL_LIST, $info);
412 }
413
417 public function listSmallFile()
418 {
419 return $this->setting->get(self::CODE_FILES_SMALL_LIST);
420 }
421
428 public function setArchiveName($name)
429 {
430 $result = false;
431 $name = preg_replace('/[^a-zA-Z0-9_]/', '', $name);
432 if (!empty($name))
433 {
434 $result = $this->setting->set(Setting::SETTING_EXPORT_ARCHIVE_NAME, $name);
435 }
436
437 return $result;
438 }
439
444 public function getArchiveName()
445 {
446 $result = $this->setting->get(Setting::SETTING_EXPORT_ARCHIVE_NAME);
447
448 return is_string($result) ? $result : '';
449 }
450
455 public static function clearContentAgent()
456 {
457 $deleteDate = new DateTime();
458 $deleteDate->add('-2 days');
459
460 $res = StorageTable::getList(
461 [
462 'filter' => [
463 '<CREATE_TIME' => $deleteDate
464 ],
465 'limit' => 100,
466 ]
467 );
468 while ($item = $res->fetch())
469 {
470 StorageTable::deleteFile($item);
471 StorageTable::delete($item['ID']);
472 }
473
474 return '\Bitrix\Rest\Configuration\Structure::clearContentAgent();';
475 }
476}
addFileList(array $filesInfo, array $files)
saveContent($type, $code, $content)
Definition structure.php:81
setUnpackFilesFromDisk($folderId, $storageParams)
saveFile($id, $additionalData=[])