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