1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
postfiles.php
См. документацию.
1<?php
8
9namespace Bitrix\Sender\Internals;
10
11use Bitrix\Main\Application;
12use Bitrix\Main\Context;
13use Bitrix\Main\HttpRequest;
14use Bitrix\Sender\Internals\Model\FileInfoTable;
15
21{
23 protected $request;
24
26 protected $inputName;
27
35 public static function getFromContext($inputName, array $savedFiles = array())
36 {
37 $instance = new static($inputName);
38 return $instance->getFiles($savedFiles);
39 }
40
47 public function __construct($inputName, HttpRequest $request = null)
48 {
49 $this->inputName = $inputName;
50
51 if (!$request)
52 {
53 $request = Context::getCurrent()->getRequest();
54 }
55 $this->request = $request;
56 }
57
65 public function getFiles(array $savedFiles = [], array $files = [])
66 {
67 $result = array();
68
69 $newFiles = $this->getMediaLib($files);
70 $newFiles = array_merge($newFiles, $this->getPosted());
71 foreach($newFiles as $file)
72 {
73 if (!is_array($file))
74 {
75 continue;
76 }
77
78 $fileId = self::saveFile($file);
79 if ($fileId)
80 {
81 $result[] = $fileId;
82 }
83 }
84
85 $result = array_merge($result, $this->getExisted($files));
86
87 $filesToDelete = array_diff($savedFiles, $result);
88 $filesToDelete = array_merge($this->getDeleted(), $filesToDelete);
89 $filesToDelete = array_unique($filesToDelete);
90 foreach ($filesToDelete as $fileId)
91 {
92 \CFile::Delete($fileId);
93 FileInfoTable::delete($fileId);
94 }
95
96
97 return $result;
98 }
99
100 protected function getDeleted()
101 {
102 $result = array();
103 $del = $this->request->get($this->inputName . '_del');
104 if(!is_array($del))
105 {
106 return $result;
107 }
108
109 foreach($del as $file => $fileMarkDel)
110 {
111 $file = intval($file);
112 if($file>0)
113 {
114 $result[] = $file;
115 }
116 }
117
118 return $result;
119 }
120
121 protected function getPosted()
122 {
123 $result = array();
124 $fileList = $this->request->getFile($this->inputName);
125 if(!is_array($fileList))
126 {
127 return $result;
128 }
129
130 foreach($fileList as $attribute => $files)
131 {
132 if(!is_array($files))
133 {
134 continue;
135 }
136
137 foreach($files as $index => $value)
138 {
139 $result[$index][$attribute] = $value;
140 }
141 }
142
143 foreach($result as $index => $file)
144 {
145 if(!is_uploaded_file($file["tmp_name"]))
146 {
147 unset($result[$index]);
148 }
149 }
150
151 return $result;
152 }
153
160 public function getMediaLib(array $files = null)
161 {
162 //New from media library and file structure
163 $result = array();
164
165 if (empty($files))
166 {
167 $files = $this->request->get($this->inputName);
168 }
169 if(!is_array($files))
170 {
171 return $result;
172 }
173
174 foreach($files as $index => $value)
175 {
176 if (is_string($value) && preg_match("/^https?:\\/\\//", $value))
177 {
178 $result[$index] = \CFile::MakeFileArray($value);
179 }
180 else
181 {
182 if(is_array($value))
183 {
184 $filePath = $value['tmp_name'];
185 }
186 else
187 {
188 $filePath = $value;
189 }
190
191 $checkResult = self::checkAbsolutePath($filePath);
192
193 if(is_null($checkResult))
194 {
195 continue;
196 }
197
198 if($checkResult['isSuccess'])
199 {
201 $result[$index] = \CFile::MakeFileArray($io->GetPhysicalName($checkResult['absPath']));
202 if(is_array($value))
203 {
204 $result[$index]['name'] = $value['name'];
205 }
206 }
207
208 }
209 }
210
211 return $result;
212 }
213
219 public static function checkAbsolutePath($filePath)
220 {
221 $isCheckedSuccess = false;
223 $docRoot = Application::getDocumentRoot();
224 if(mb_strpos($filePath, \CTempFile::GetAbsoluteRoot()) === 0)
225 {
226 $absPath = $filePath;
227 }
228 elseif(mb_strpos($io->CombinePath($docRoot, $filePath), \CTempFile::GetAbsoluteRoot()) === 0)
229 {
230 $absPath = $io->CombinePath($docRoot, $filePath);
231 }
232 else
233 {
234 $absPath = $io->CombinePath(\CTempFile::GetAbsoluteRoot(), $filePath);
235 $isCheckedSuccess = true;
236 }
237
238 $absPath = realpath(str_replace("\\", "/", $absPath));
239 if (mb_strpos($absPath, realpath(\CTempFile::GetAbsoluteRoot())) !== 0)
240 {
241 return null;
242 }
243
244 if (!$isCheckedSuccess && $io->ValidatePathString($absPath) && $io->FileExists($absPath))
245 {
246 $docRoot = $io->CombinePath($docRoot, '/');
247 $relPath = str_replace($docRoot, '', $absPath);
248 $perm = $GLOBALS['APPLICATION']->GetFileAccessPermission($relPath);
249 if ($perm >= "W")
250 {
251 $isCheckedSuccess = true;
252 }
253 }
254
255 return [
256 'isSuccess' => $isCheckedSuccess,
257 'absPath' => $absPath
258 ];
259 }
260
267 public function getExisted(array $files = null)
268 {
269 $result = array();
270
271 if (empty($files))
272 {
273 $files = $this->request->get($this->inputName);
274 }
275 if(!is_array($files))
276 {
277 return $result;
278 }
279
280 foreach($files as $index => $value)
281 {
282 if (!is_numeric($index) || !is_numeric($value))
283 {
284 continue;
285 }
286
287 $file = \CFile::getByID($value)->fetch();
288 if (!$file || $file['MODULE_ID'] !== 'sender')
289 {
290 continue;
291 }
292
293 $result[] = (int) $value;
294 }
295
296 return $result;
297 }
298
305 public static function saveFile(array $file)
306 {
307 if($file["name"] == '' || intval($file["size"]) <= 0)
308 {
309 return null;
310 }
311
312 $pathHash = md5($file["tmp_name"]);
313 $sessionKey = 'sender_post_files';
314 if (!empty($_SESSION[$sessionKey][$pathHash]))
315 {
316 $fileId = (int) $_SESSION[$sessionKey][$pathHash];
317 return $fileId ?: null;
318 }
319
320 $file["MODULE_ID"] = "sender";
321 $fileId = (int) \CFile::saveFile($file, "sender", true);
322 if ($fileId)
323 {
324 $_SESSION[$sessionKey][$pathHash] = $fileId;
325 return $fileId;
326 }
327
328 return null;
329 }
330}
static delete($primary)
Определения datamanager.php:1644
Определения postfiles.php:21
getExisted(array $files=null)
Определения postfiles.php:267
$inputName
Определения postfiles.php:26
getMediaLib(array $files=null)
Определения postfiles.php:160
getPosted()
Определения postfiles.php:121
getFiles(array $savedFiles=[], array $files=[])
Определения postfiles.php:65
getDeleted()
Определения postfiles.php:100
static getFromContext($inputName, array $savedFiles=array())
Определения postfiles.php:35
static checkAbsolutePath($filePath)
Определения postfiles.php:219
__construct($inputName, HttpRequest $request=null)
Определения postfiles.php:47
static saveFile(array $file)
Определения postfiles.php:305
$request
Определения postfiles.php:23
static GetInstance()
Определения virtual_io.php:60
$relPath
Определения component_props2.php:52
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$perm
Определения options.php:169
$result
Определения get_property_values.php:14
$docRoot
Определения options.php:20
$io
Определения csv_new_run.php:98
$files
Определения mysql_to_pgsql.php:30
$GLOBALS['____1690880296']
Определения license.php:1
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$instance
Определения ps_b24_final.php:14