Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
disk.php
1<?php
3
4class Disk
5{
6 public const FILE_PREFIX_HREF = '#diskFile';
7 public const FILE_MASK_HREF = '(file:)?#diskFile([\d]+)';
8
17 public static function sanitizeContent(string $content, ?string $oldContent = null, bool &$replaced = false): string
18 {
19 if (!\Bitrix\Main\Loader::includeModule('disk'))
20 {
21 return $content;
22 }
23
24 $existsFiles = [];
25
26 if ($oldContent)
27 {
28 if (preg_match_all('/' . self::FILE_MASK_HREF . '/i', $oldContent, $matches))
29 {
30 foreach ($matches[2] as $objectId)
31 {
32 $existsFiles[] = $objectId;
33 }
34 }
35 }
36
37 if (preg_match_all('/' . self::FILE_MASK_HREF . '/i', $content, $matches))
38 {
39 foreach ($matches[2] as $i => $objectId)
40 {
41 if ($oldContent && in_array($objectId, $existsFiles))
42 {
43 continue;
44 }
45
46 $file = \Bitrix\Disk\BaseObject::loadById($objectId);
47 if ($file)
48 {
49 $securityContext = $file->getStorage()->getCurrentUserSecurityContext();
50 if ($file->canRead($securityContext))
51 {
52 continue;
53 }
54 }
55
56 $replaced = true;
57 $content = str_replace($matches[0][$i], '""', $content);
58 }
59 }
60
61 return $content;
62 }
63
72 public static function getFileInfo(int $objectId, bool $checkRights = true, bool $copyInLocalStorage = false): ?array
73 {
74 if ($objectId && \Bitrix\Main\Loader::includeModule('disk'))
75 {
76 $file = \Bitrix\Disk\BaseObject::loadById($objectId);
77 if ($file)
78 {
79 if ($checkRights)
80 {
81 $securityContext = $file->getStorage()->getCurrentUserSecurityContext();
82 if (!$file->canRead($securityContext))
83 {
84 return null;
85 }
86 }
87
88 if ($copyInLocalStorage)
89 {
90 $currentUserId = \Bitrix\Landing\Manager::getUserId();
91 $userStorage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId($currentUserId);
92 if (!$userStorage)
93 {
94 return null;
95 }
96
97 $folder = $userStorage->getFolderForSavedFiles();
98 if (!$folder)
99 {
100 return null;
101 }
102
103 if (
104 $file->getStorageId() !== $userStorage->getId() ||
105 (int)$file->getCreateUser()->getId() !== $currentUserId
106 )
107 {
108 $newFile = $file->getRealObject()->copyTo($folder, $currentUserId, true);
109 if ($file->getRealObject()->getName() != $file->getName())
110 {
111 $newFile->renameInternal($file->getName(), true);
112 }
113 if (!$newFile)
114 {
115 return null;
116 }
117
118 return [
119 'ID' => $newFile->getRealObject()->getFileId(),
120 'OBJECT_ID' => $newFile->getId(),
121 'NAME' => $newFile->getName()
122 ];
123 }
124 }
125
126 return [
127 'ID' => $file->getRealObject()->getFileId(),
128 'OBJECT_ID' => $file->getId(),
129 'NAME' => $file->getName()
130 ];
131 }
132 }
133
134 return null;
135 }
136}
static sanitizeContent(string $content, ?string $oldContent=null, bool &$replaced=false)
Definition disk.php:17
static getFileInfo(int $objectId, bool $checkRights=true, bool $copyInLocalStorage=false)
Definition disk.php:72