1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
virtual_io_filesystem.php
См. документацию.
1<?
6 implements IBXVirtualIO, IBXGetErrors
7{
8 const directionEncode = 1;
9 const directionDecode = 2;
10 const invalidChars = "\\/:*?\"'<>|~#&;";
11
12 //the pattern should be quoted, "|" is allowed below as a delimiter
13 const invalidBytes = "\xE2\x80\xAE"; //Right-to-Left Override Unicode Character
14
15 private $arErrors = array();
16
17 public static function ConvertCharset($string, $direction = 1, $skipEvents = false)
18 {
19 $systemEncoding = defined("BX_FILE_SYSTEM_ENCODING") ? strtolower(BX_FILE_SYSTEM_ENCODING) : "";
20 if (empty($systemEncoding))
21 {
22 if (strtoupper(substr(PHP_OS, 0, 3)) === "WIN")
23 $systemEncoding = "windows-1251";
24 else
25 $systemEncoding = "utf-8";
26 }
27
28 $serverEncoding = "utf-8";
29
30 if ($serverEncoding == $systemEncoding)
31 return $string;
32
33 if ($direction == self::directionEncode)
34 $result = \Bitrix\Main\Text\Encoding::convertEncoding($string, $serverEncoding, $systemEncoding);
35 else
36 $result = \Bitrix\Main\Text\Encoding::convertEncoding($string, $systemEncoding, $serverEncoding);
37
38 if (
39 defined('BX_IO_Compartible')
40 && !$skipEvents
41 && (BX_IO_Compartible === 'Y')
42 )
43 {
44 $arEventParams = array(
45 'original' => $string,
46 'converted' => $result,
47 'direction' => $direction,
48 'systemEncoding' => $systemEncoding,
49 'serverEncoding' => $serverEncoding
50 );
51
52 foreach (GetModuleEvents("main", "BXVirtualIO_ConvertCharset", true) as $arEvent)
53 {
54 $evResult = ExecuteModuleEventEx($arEvent, array($arEventParams));
55 if ($evResult !== false)
56 {
57 $result = $evResult;
58 break;
59 }
60 }
61 }
62
63 return $result;
64 }
65
66 public function CombinePath()
67 {
68 $numArgs = func_num_args();
69 if ($numArgs <= 0)
70 return "";
71
72 $arParts = array();
73 for ($i = 0; $i < $numArgs; $i++)
74 {
75 $arg = func_get_arg($i);
76 if (empty($arg))
77 continue;
78
79 if (is_array($arg))
80 {
81 foreach ($arg as $v)
82 {
83 if (empty($v))
84 continue;
85 $arParts[] = $v;
86 }
87 }
88 else
89 {
90 $arParts[] = $arg;
91 }
92 }
93
94 $result = "";
95 foreach ($arParts as $part)
96 {
97 if (!empty($result))
98 $result .= "/";
99 $result .= $part;
100 }
101
102 $result = self::FormatPath($result);
103
104 return $result;
105 }
106
107 public function RelativeToAbsolutePath($relativePath)
108 {
109 if (empty($relativePath))
110 return null;
111
112 $basePath = $_SERVER["DOCUMENT_ROOT"];
113
114 return $this->CombinePath($basePath, $relativePath);
115 }
116
117 public function SiteRelativeToAbsolutePath($relativePath, $site = null)
118 {
119 if ((string)$site === "")
120 {
121 $site = SITE_ID;
122 }
123 else
124 {
125 $dbSite = CSite::GetByID($site);
126 $site = "";
127 if ($arSite = $dbSite->Fetch())
128 $site = $_REQUEST["site"];
129 if ((string)$site === "")
130 $site = SITE_ID;
131 }
132
133 $basePath = CSite::GetSiteDocRoot($site);
134
135 return $this->CombinePath($basePath, $relativePath);
136 }
137
138 public function GetPhysicalName($path)
139 {
141 }
142
144 {
145 return CBXVirtualIoFileSystem::ConvertCharset($path, self::directionDecode);
146 }
147
148 public function ExtractNameFromPath($path)
149 {
150 $path = rtrim($path, "\\/");
151 if (preg_match("#[^\\\\/]+$#", $path, $match))
152 $path = $match[0];
153 return $path;
154 }
155
156 public function ExtractPathFromPath($path)
157 {
158 return mb_substr($path, 0, -mb_strlen($this->ExtractNameFromPath($path)) - 1);
159 }
160
161 private function FormatPath($path)
162 {
163 if ($path == "")
164 return null;
165
166 //slashes doesn't matter for Windows
167 if(strncasecmp(PHP_OS, "WIN", 3) == 0)
168 {
169 //windows
170 $pattern = "'[\\\\/]+'";
171 $tailPattern = "\0.\\/+ ";
172 }
173 else
174 {
175 //unix
176 $pattern = "'[/]+'";
177 $tailPattern = "\0/";
178 }
179
180 $res = preg_replace($pattern, "/", $path);
181
182 if (str_contains($res, "\0"))
183 throw new \Bitrix\Main\IO\InvalidPathException($path);
184
185 $arPath = explode('/', $res);
186 $nPath = count($arPath);
187 $pathStack = array();
188
189 for ($i = 0; $i < $nPath; $i++)
190 {
191 if ($arPath[$i] === ".")
192 continue;
193 if (($arPath[$i] === '') && ($i !== ($nPath - 1)) && ($i !== 0))
194 continue;
195
196 if ($arPath[$i] === "..")
197 array_pop($pathStack);
198 else
199 array_push($pathStack, $arPath[$i]);
200 }
201
202 $res = implode("/", $pathStack);
203
204 $res = rtrim($res, $tailPattern);
205
206 if(str_starts_with($path, "/") && !str_starts_with($res, "/"))
207 $res = "/".$res;
208
209 if ($res === "")
210 $res = "/";
211
212 return $res;
213 }
214
215 protected static function ValidateCommon($path)
216 {
217 if (trim($path) == '')
218 {
219 return false;
220 }
221
222 if (str_contains($path, "\0"))
223 {
224 return false;
225 }
226
227 if(preg_match("#(".self::invalidBytes.")#", $path))
228 {
229 return false;
230 }
231
232 if(!mb_check_encoding($path))
233 {
234 return false;
235 }
236
237 return true;
238 }
239
241 {
242 if(mb_strlen($path) > 4096)
243 {
244 return false;
245 }
246
247 if(!static::ValidateCommon($path))
248 {
249 return false;
250 }
251
252 return (preg_match("#^([a-z]:)?/([^\x01-\x1F".preg_quote(self::invalidChars, "#")."]+/?)*$#isD", $path) > 0);
253 }
254
256 {
257 if(!static::ValidateCommon($filename))
258 {
259 return false;
260 }
261
262 return (preg_match("#^[^\x01-\x1F".preg_quote(self::invalidChars, "#")."]+$#isD", $filename) > 0);
263 }
264
266 {
267 return preg_replace_callback(
268 "#([\x01-\x1F".preg_quote(self::invalidChars, "#")."]|".self::invalidBytes.")#",
269 'CBXVirtualIoFileSystem::getRandomChar',
271 );
272 }
273
274 public static function getRandomChar()
275 {
276 return chr(rand(97, 122));
277 }
278
279 public function DirectoryExists($path)
280 {
282 return file_exists($path) && is_dir($path);
283 }
284
285 public function FileExists($path)
286 {
288 return file_exists($path) && is_file($path);
289 }
290
291 public function GetDirectory($path)
292 {
294 }
295
296 public function GetFile($path)
297 {
299 }
300
301 public function OpenFile($path, $mode)
302 {
303 $file = $this->GetFile($path);
304 return $file->Open($mode);
305 }
306
307 public function Delete($path)
308 {
309 $this->ClearErrors();
310
311 if (str_starts_with($path, $_SERVER["DOCUMENT_ROOT"]))
312 {
313 $pathTmp = substr($path, strlen($_SERVER["DOCUMENT_ROOT"]));
314 if (empty($pathTmp) || $pathTmp == '/')
315 {
316 $this->AddError("Can not delete the root folder of the project");
317 return false;
318 }
319 }
320
322
323 $f = true;
324 if (is_file($pathEncoded) || is_link($pathEncoded))
325 {
326 if (@unlink($pathEncoded))
327 return true;
328
329 $this->AddError(sprintf("Can not delete file '%s'", $path));
330 return false;
331 }
332 elseif (is_dir($pathEncoded))
333 {
334 if ($handle = opendir($pathEncoded))
335 {
336 while (($file = readdir($handle)) !== false)
337 {
338 if ($file == "." || $file == "..")
339 continue;
340
342 if (!$this->Delete($pathDecodedTmp))
343 $f = false;
344 }
345 closedir($handle);
346 }
347 if (!@rmdir($pathEncoded))
348 {
349 $this->AddError(sprintf("Can not delete directory '%s'", $path));
350 return false;
351 }
352
353 return $f;
354 }
355
356 $this->AddError("Unknown error");
357 return false;
358 }
359
360 private function CopyDirFiles($pathFrom, $pathTo, $bRewrite = true, $bDeleteAfterCopy = false)
361 {
362 $this->ClearErrors();
363
364 if (mb_strpos($pathTo."/", $pathFrom."/") === 0)
365 {
366 $this->AddError("Can not copy a file onto itself");
367 return false;
368 }
369
370 $pathFromEncoded = CBXVirtualIoFileSystem::ConvertCharset($pathFrom);
371 if (is_dir($pathFromEncoded))
372 {
373 $this->CreateDirectory($pathTo);
374 }
375 elseif (is_file($pathFromEncoded))
376 {
377 $this->CreateDirectory($this->ExtractPathFromPath($pathTo));
378
379 $pathToEncoded = CBXVirtualIoFileSystem::ConvertCharset($pathTo);
380 if (file_exists($pathToEncoded) && !$bRewrite)
381 {
382 $this->AddError(sprintf("The file '%s' already exists", $pathTo));
383 return false;
384 }
385
386 @copy($pathFromEncoded, $pathToEncoded);
387 if (is_file($pathToEncoded))
388 {
389 @chmod($pathToEncoded, BX_FILE_PERMISSIONS);
390
391 if ($bDeleteAfterCopy)
392 @unlink($pathFromEncoded);
393 }
394 else
395 {
396 $this->AddError(sprintf("Creation of file '%s' failed", $pathTo));
397 return false;
398 }
399
400 return true;
401 }
402 else
403 {
404 return true;
405 }
406
407 $pathToEncoded = CBXVirtualIoFileSystem::ConvertCharset($pathTo);
408 if ($handle = @opendir($pathFromEncoded))
409 {
410 while (($file = readdir($handle)) !== false)
411 {
412 if ($file == "." || $file == "..")
413 continue;
414
415 if (is_dir($pathFromEncoded."/".$file))
416 {
417 $pathFromDecodedTmp = CBXVirtualIoFileSystem::ConvertCharset($pathFromEncoded."/".$file, CBXVirtualIoFileSystem::directionDecode);
418 $pathToDecodedTmp = CBXVirtualIoFileSystem::ConvertCharset($pathToEncoded."/".$file, CBXVirtualIoFileSystem::directionDecode);
419 CopyDirFiles($pathFromDecodedTmp, $pathToDecodedTmp, $bRewrite, $bDeleteAfterCopy);
420 if ($bDeleteAfterCopy)
421 @rmdir($pathFromEncoded."/".$file);
422 }
423 elseif (is_file($pathFromEncoded."/".$file))
424 {
425 if (file_exists($pathToEncoded."/".$file) && !$bRewrite)
426 continue;
427
428 @copy($pathFromEncoded."/".$file, $pathToEncoded."/".$file);
429 @chmod($pathToEncoded."/".$file, BX_FILE_PERMISSIONS);
430
431 if ($bDeleteAfterCopy)
432 @unlink($pathFromEncoded."/".$file);
433 }
434 }
435 @closedir($handle);
436
437 if ($bDeleteAfterCopy)
438 @rmdir($pathFromEncoded);
439
440 return true;
441 }
442
443 $this->AddError(sprintf("Can not open directory '%s'", $pathFrom));
444 return false;
445 }
446
447 public function Copy($source, $target, $bRewrite = true)
448 {
449 return $this->CopyDirFiles($source, $target, $bRewrite, false);
450 }
451
452 public function Move($source, $target, $bRewrite = true)
453 {
454 return $this->CopyDirFiles($source, $target, $bRewrite, true);
455 }
456
457 public function Rename($source, $target)
458 {
459 $sourceEncoded = CBXVirtualIoFileSystem::ConvertCharset($source);
460 $targetEncoded = CBXVirtualIoFileSystem::ConvertCharset($target);
461 return rename($sourceEncoded, $targetEncoded);
462 }
463
464 public function CreateDirectory($path)
465 {
466 $fld = $this->GetDirectory($path);
467 if ($fld->Create())
468 return $fld;
469
470 return null;
471 }
472
473 function ClearCache()
474 {
475 clearstatcache();
476 }
477
478 public function GetErrors()
479 {
480 return $this->arErrors;
481 }
482
483 protected function AddError($error, $errorCode = "")
484 {
485 if (empty($error))
486 return;
487
488 $fs = (empty($errorCode) ? "%s" : "[%s] %s");
489 $this->arErrors[] = sprintf($fs, $error, $errorCode);
490 }
491
492 protected function ClearErrors()
493 {
494 $this->arErrors = array();
495 }
496}
497
502 extends CBXVirtualFile
503{
504 protected $pathEncoded = null;
505 private $arErrors = array();
506
507 protected function GetPathWithNameEncoded()
508 {
509 if (is_null($this->pathEncoded))
510 $this->pathEncoded = CBXVirtualIoFileSystem::ConvertCharset($this->path);
511
512 return $this->pathEncoded;
513 }
514
515 public function Open($mode)
516 {
517 $lmode = mb_strtolower(mb_substr($mode, 0, 1));
518 $bExists = $this->IsExists();
519
520 if (
521 ( $bExists && ($lmode !== 'x'))
522 || (!$bExists && ($lmode !== 'r'))
523 )
524 return fopen($this->GetPathWithNameEncoded(), $mode);
525
526 return null;
527 }
528
529 public function GetContents()
530 {
531 if ($this->IsExists())
532 return file_get_contents($this->GetPathWithNameEncoded());
533
534 return null;
535 }
536
537 public function PutContents($data)
538 {
539 $this->ClearErrors();
540
542 $dir = $io->CreateDirectory($this->GetPath());
543 if (is_null($dir))
544 {
545 $this->AddError(sprintf("Can not create directory '%s' or access denied", $this->GetPath()));
546 return false;
547 }
548
549 if ($this->IsExists() && !$this->IsWritable())
550 $this->MarkWritable();
551
552 $fd = fopen($this->GetPathWithNameEncoded(), "wb");
553 if (!$fd)
554 {
555 $this->AddError(sprintf("Can not open file '%s' for writing", $this->GetPathWithNameEncoded()));
556 return false;
557 }
558 if (fwrite($fd, $data) === false)
559 {
560 $this->AddError(sprintf("Can not write %d bytes to file '%s'", mb_strlen($data), $this->GetPathWithNameEncoded()));
561 fclose($fd);
562 return false;
563 }
564 fclose($fd);
565
566 return true;
567 }
568
569 public function GetFileSize()
570 {
571 if ($this->IsExists())
572 return intval(filesize($this->GetPathWithNameEncoded()));
573
574 return 0;
575 }
576
577 public function GetCreationTime()
578 {
579 if ($this->IsExists())
580 return filectime($this->GetPathWithNameEncoded());
581
582 return null;
583 }
584
585 public function GetModificationTime()
586 {
587 if ($this->IsExists())
588 return filemtime($this->GetPathWithNameEncoded());
589
590 return null;
591 }
592
593 public function GetLastAccessTime()
594 {
595 if ($this->IsExists())
596 return fileatime($this->GetPathWithNameEncoded());
597
598 return null;
599 }
600
601 public function IsWritable()
602 {
603 return is_writable($this->GetPathWithNameEncoded());
604 }
605
606 public function IsReadable()
607 {
608 return is_readable($this->GetPathWithNameEncoded());
609 }
610
611 public function MarkWritable()
612 {
613 if ($this->IsExists())
614 @chmod($this->GetPathWithNameEncoded(), BX_FILE_PERMISSIONS);
615 }
616
617 public function IsExists()
618 {
620 return $io->FileExists($this->path);
621 }
622
623 public function GetPermissions()
624 {
625 return fileperms($this->GetPathWithNameEncoded());
626 }
627
628 public function ReadFile()
629 {
630 return readfile($this->GetPathWithNameEncoded());
631 }
632
633 public function unlink()
634 {
635 return unlink($this->GetPathWithNameEncoded());
636 }
637
638 public function GetErrors()
639 {
640 return $this->arErrors;
641 }
642
643 protected function AddError($error, $errorCode = "")
644 {
645 if (empty($error))
646 return;
647
648 $fs = (empty($errorCode) ? "%s" : "[%s] %s");
649 $this->arErrors[] = sprintf($fs, $error, $errorCode);
650 }
651
652 protected function ClearErrors()
653 {
654 $this->arErrors = array();
655 }
656}
657
662 extends CBXVirtualDirectory
663{
664 protected $pathEncoded = null;
665 private $arErrors = array();
666
667 protected function GetPathWithNameEncoded()
668 {
669 if (is_null($this->pathEncoded))
670 $this->pathEncoded = CBXVirtualIoFileSystem::ConvertCharset($this->path);
671
672 return $this->pathEncoded;
673 }
674
678 public function GetChildren()
679 {
680 $arResult = array();
681
682 if (!$this->IsExists())
683 return $arResult;
684
685 if ($handle = opendir($this->GetPathWithNameEncoded()))
686 {
687 while (($file = readdir($handle)) !== false)
688 {
689 if ($file == "." || $file == "..")
690 continue;
691
693 if (is_dir($this->GetPathWithNameEncoded()."/".$file))
694 $arResult[] = new CBXVirtualDirectoryFileSystem($pathDecoded);
695 else
696 $arResult[] = new CBXVirtualFileFileSystem($pathDecoded);
697 }
698 closedir($handle);
699 }
700
701 return $arResult;
702 }
703
704 public function Create()
705 {
706 if (!file_exists($this->GetPathWithNameEncoded()))
707 return mkdir($this->GetPathWithNameEncoded(), BX_DIR_PERMISSIONS, true);
708 else
709 return is_dir($this->GetPathWithNameEncoded());
710 }
711
712 public function IsExists()
713 {
715 return $io->DirectoryExists($this->path);
716 }
717
718 public function MarkWritable()
719 {
720 if ($this->IsExists())
721 @chmod($this->GetPathWithNameEncoded(), BX_DIR_PERMISSIONS);
722 }
723
724 public function GetPermissions()
725 {
726 return fileperms($this->GetPathWithNameEncoded());
727 }
728
729 public function GetCreationTime()
730 {
731 if ($this->IsExists())
732 return filectime($this->GetPathWithNameEncoded());
733
734 return null;
735 }
736
737 public function GetModificationTime()
738 {
739 if ($this->IsExists())
740 return filemtime($this->GetPathWithNameEncoded());
741
742 return null;
743 }
744
745 public function GetLastAccessTime()
746 {
747 if ($this->IsExists())
748 return fileatime($this->GetPathWithNameEncoded());
749
750 return null;
751 }
752
753 public function IsEmpty()
754 {
755 if ($this->IsExists())
756 {
757 if ($handle = opendir($this->GetPathWithNameEncoded()))
758 {
759 while (($file = readdir($handle)) !== false)
760 {
761 if ($file != "." && $file != "..")
762 {
763 closedir($handle);
764 return false;
765 }
766 }
767 closedir($handle);
768 }
769 }
770 return true;
771 }
772
773 public function rmdir()
774 {
775 return rmdir($this->GetPathWithNameEncoded());
776 }
777
778 public function GetErrors()
779 {
780 return $this->arErrors;
781 }
782
783 protected function AddError($error, $errorCode = "")
784 {
785 if (empty($error))
786 return;
787
788 $fs = (empty($errorCode) ? "%s" : "[%s] %s");
789 $this->arErrors[] = sprintf($fs, $error, $errorCode);
790 }
791
792 protected function ClearErrors()
793 {
794 $this->arErrors = array();
795 }
796}
$path
Определения access_edit.php:21
$basePath
Определения include.php:41
$arResult
Определения generate_coupon.php:16
static convertEncoding($data, $charsetFrom, $charsetTo)
Определения encoding.php:17
AddError($error, $errorCode="")
Определения virtual_io_filesystem.php:783
GetPath()
Определения virtual_file.php:22
AddError($error, $errorCode="")
Определения virtual_io_filesystem.php:643
Определения virtual_file.php:46
GetPhysicalName($path)
Определения virtual_io_filesystem.php:138
RandomizeInvalidFilename($filename)
Определения virtual_io_filesystem.php:265
OpenFile($path, $mode)
Определения virtual_io_filesystem.php:301
AddError($error, $errorCode="")
Определения virtual_io_filesystem.php:483
ValidateFilenameString($filename)
Определения virtual_io_filesystem.php:255
const directionDecode
Определения virtual_io_filesystem.php:9
Rename($source, $target)
Определения virtual_io_filesystem.php:457
DirectoryExists($path)
Определения virtual_io_filesystem.php:279
static ValidateCommon($path)
Определения virtual_io_filesystem.php:215
Move($source, $target, $bRewrite=true)
Определения virtual_io_filesystem.php:452
SiteRelativeToAbsolutePath($relativePath, $site=null)
Определения virtual_io_filesystem.php:117
RelativeToAbsolutePath($relativePath)
Определения virtual_io_filesystem.php:107
static ConvertCharset($string, $direction=1, $skipEvents=false)
Определения virtual_io_filesystem.php:17
ExtractNameFromPath($path)
Определения virtual_io_filesystem.php:148
GetLogicalName($path)
Определения virtual_io_filesystem.php:143
ExtractPathFromPath($path)
Определения virtual_io_filesystem.php:156
const directionEncode
Определения virtual_io_filesystem.php:8
ValidatePathString($path)
Определения virtual_io_filesystem.php:240
static getRandomChar()
Определения virtual_io_filesystem.php:274
Copy($source, $target, $bRewrite=true)
Определения virtual_io_filesystem.php:447
CreateDirectory($path)
Определения virtual_io_filesystem.php:464
static GetInstance()
Определения virtual_io.php:60
$f
Определения component_props.php:52
$data['IS_AVAILABLE']
Определения .description.php:13
$filename
Определения file_edit.php:47
$arPath
Определения file_edit.php:72
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$handle
Определения include.php:55
$_REQUEST["admin_mnu_menu_id"]
Определения get_menu.php:8
$result
Определения get_property_values.php:14
Определения virtual_io.php:35
Определения virtual_io.php:8
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$io
Определения csv_new_run.php:98
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
CopyDirFiles($path_from, $path_to, $ReWrite=true, $Recursive=false, $bDeleteAfterCopy=false, $strExclude="")
Определения tools.php:2732
$direction
Определения prolog_auth_admin.php:25
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$dir
Определения quickway.php:303
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if(!Loader::includeModule('sale')) $pattern
Определения index.php:20
const SITE_ID
Определения sonet_set_content_view.php:12
$error
Определения subscription_card_product.php:20
path
Определения template_copy.php:201
$site
Определения yandex_run.php:614