1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
path.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\IO;
4
5use Bitrix\Main;
6use Bitrix\Main\Text;
7
11class Path
12{
14 const INVALID_FILENAME_CHARS = "\\/:*?\"'<>|~#&;";
15
16 //the pattern should be quoted, "|" is allowed below as a delimiter
17 const INVALID_FILENAME_BYTES = "\xE2\x80\xAE"; //Right-to-Left Override Unicode Character
18
19 protected static $physicalEncoding = '';
20 protected static $logicalEncoding = '';
21
22 public static function normalize($path)
23 {
24 if (!is_string($path) || $path == '')
25 {
26 return null;
27 }
28
29 //slashes doesn't matter for Windows
30 static $pattern = null, $tailPattern;
31 if (!$pattern)
32 {
33 if (strncasecmp(PHP_OS, "WIN", 3) == 0)
34 {
35 //windows
36 $pattern = "'[\\\\/]+'";
37 $tailPattern = "\0.\\/+ ";
38 }
39 else
40 {
41 //unix
42 $pattern = "'/+'";
43 $tailPattern = "\0/";
44 }
45 }
46 $pathTmp = preg_replace($pattern, "/", $path);
47
48 if (str_contains($pathTmp, "\0"))
49 {
50 throw new InvalidPathException($path);
51 }
52
53 if (preg_match("#(^|/)(\\.|\\.\\.)(/|\$)#", $pathTmp))
54 {
55 $pathTmpParts = explode('/', $pathTmp);
56 $pathStack = [];
57 foreach ($pathTmpParts as $pathPart)
58 {
59 if ($pathPart === '.')
60 {
61 continue;
62 }
63
64 if ($pathPart === "..")
65 {
66 if (array_pop($pathStack) === null)
67 {
68 throw new InvalidPathException($path);
69 }
70 }
71 else
72 {
73 $pathStack[] = $pathPart;
74 }
75 }
76 $pathTmp = implode("/", $pathStack);
77 }
78
79 $pathTmp = rtrim($pathTmp, $tailPattern);
80
81 if (str_starts_with($path, "/") && !str_starts_with($pathTmp, "/"))
82 {
83 $pathTmp = "/" . $pathTmp;
84 }
85
86 if ($pathTmp === '')
87 {
88 $pathTmp = "/";
89 }
90
91 return $pathTmp;
92 }
93
94 public static function getExtension($path)
95 {
97 if ($path != '')
98 {
100 if ($pos !== false)
101 {
102 return mb_substr($path, $pos + 1);
103 }
104 }
105 return '';
106 }
107
108 public static function getName($path)
109 {
110 $p = Text\UtfSafeString::getLastPosition($path, self::DIRECTORY_SEPARATOR);
111 if ($p !== false)
112 {
113 return mb_substr($path, $p + 1);
114 }
115
116 return $path;
117 }
118
119 public static function getDirectory($path)
120 {
121 return mb_substr($path, 0, -mb_strlen(self::getName($path)) - 1);
122 }
123
124 public static function convertLogicalToPhysical($path)
125 {
126 if (self::$physicalEncoding == '')
127 {
128 self::$physicalEncoding = self::getPhysicalEncoding();
129 }
130
131 if (self::$logicalEncoding == '')
132 {
133 self::$logicalEncoding = self::getLogicalEncoding();
134 }
135
136 if (self::$physicalEncoding == self::$logicalEncoding)
137 {
138 return $path;
139 }
140
141 return Text\Encoding::convertEncoding($path, self::$logicalEncoding, self::$physicalEncoding);
142 }
143
144 public static function convertPhysicalToLogical($path)
145 {
146 if (self::$physicalEncoding == '')
147 {
148 self::$physicalEncoding = self::getPhysicalEncoding();
149 }
150
151 if (self::$logicalEncoding == '')
152 {
153 self::$logicalEncoding = self::getLogicalEncoding();
154 }
155
156 if (self::$physicalEncoding == self::$logicalEncoding)
157 {
158 return $path;
159 }
160
161 return Text\Encoding::convertEncoding($path, self::$physicalEncoding, self::$logicalEncoding);
162 }
163
164 public static function convertLogicalToUri($path)
165 {
166 if (self::$logicalEncoding == '')
167 {
168 self::$logicalEncoding = self::getLogicalEncoding();
169 }
170
172
173 if ('utf-8' !== self::$logicalEncoding)
174 {
175 $path = Text\Encoding::convertEncoding($path, self::$logicalEncoding, 'utf-8');
176 }
177
178 return implode('/', array_map("rawurlencode", explode('/', $path)));
179 }
180
181 public static function convertPhysicalToUri($path)
182 {
183 if (self::$physicalEncoding == '')
184 {
185 self::$physicalEncoding = self::getPhysicalEncoding();
186 }
187
189
190 if ('utf-8' !== self::$physicalEncoding)
191 {
192 $path = Text\Encoding::convertEncoding($path, self::$physicalEncoding, 'utf-8');
193 }
194
195 return implode('/', array_map("rawurlencode", explode('/', $path)));
196 }
197
198 protected static function truncateIndexFile($path)
199 {
200 static $directoryIndex = null;
201
202 if ($directoryIndex === null)
203 {
204 $directoryIndex = self::getDirectoryIndexArray();
205 }
206
207 if (isset($directoryIndex[self::getName($path)]))
208 {
210 }
211
212 return $path;
213 }
214
215 public static function convertUriToPhysical($path)
216 {
217 if (self::$physicalEncoding == '')
218 {
219 self::$physicalEncoding = self::getPhysicalEncoding();
220 }
221
222 $path = implode('/', array_map("rawurldecode", explode('/', $path)));
223
224 if ('utf-8' !== self::$physicalEncoding)
225 {
226 $path = Text\Encoding::convertEncoding($path, 'utf-8', self::$physicalEncoding);
227 }
228
229 return $path;
230 }
231
232 protected static function getLogicalEncoding()
233 {
234 return "utf-8";
235 }
236
237 protected static function getPhysicalEncoding()
238 {
239 $physicalEncoding = defined("BX_FILE_SYSTEM_ENCODING") ? BX_FILE_SYSTEM_ENCODING : '';
240 if ($physicalEncoding == '')
241 {
242 if (strtoupper(substr(PHP_OS, 0, 3)) === "WIN")
243 {
244 $physicalEncoding = "windows-1251";
245 }
246 else
247 {
248 $physicalEncoding = "utf-8";
249 }
250 }
251 return mb_strtolower($physicalEncoding);
252 }
253
254 public static function combine(...$args)
255 {
256 $parts = [];
257 foreach ($args as $arg)
258 {
259 if (is_array($arg))
260 {
261 foreach ($arg as $v)
262 {
263 if (is_string($v) && $v != '')
264 {
265 $parts[] = $v;
266 }
267 }
268 }
269 elseif (is_string($arg) && $arg != '')
270 {
271 $parts[] = $arg;
272 }
273 }
274
275 if (!empty($parts))
276 {
277 $result = implode(self::DIRECTORY_SEPARATOR, $parts);
279
280 return $result;
281 }
282
283 return '';
284 }
285
286 public static function convertRelativeToAbsolute($relativePath)
287 {
288 if (!is_string($relativePath))
289 {
290 throw new Main\ArgumentTypeException("relativePath", "string");
291 }
292 if ($relativePath == '')
293 {
294 throw new Main\ArgumentNullException("relativePath");
295 }
296
297 return self::combine($_SERVER["DOCUMENT_ROOT"], $relativePath);
298 }
299
300 public static function convertSiteRelativeToAbsolute($relativePath, $site = null)
301 {
302 if (!is_string($relativePath) || $relativePath == '')
303 {
304 $site = SITE_ID;
305 }
306
308
309 return self::combine($basePath, $relativePath);
310 }
311
312 protected static function validateCommon($path)
313 {
314 if (!is_string($path))
315 {
316 return false;
317 }
318
319 if (trim($path) == '')
320 {
321 return false;
322 }
323
324 if (str_contains($path, "\0"))
325 {
326 return false;
327 }
328
329 if (preg_match("#(" . self::INVALID_FILENAME_BYTES . ")#", $path))
330 {
331 return false;
332 }
333
334 return true;
335 }
336
337 public static function validate($path)
338 {
339 if (!static::validateCommon($path))
340 {
341 return false;
342 }
343
344 return (preg_match("#^([a-z]:)?/([^\x01-\x1F" . preg_quote(self::INVALID_FILENAME_CHARS, "#") . "]+/?)*$#isD", $path) > 0);
345 }
346
347 public static function validateFilename($filename)
348 {
349 if (!static::validateCommon($filename))
350 {
351 return false;
352 }
353
354 return (preg_match("#^[^\x01-\x1F" . preg_quote(self::INVALID_FILENAME_CHARS, "#") . "]+$#isD", $filename) > 0);
355 }
356
362 public static function replaceInvalidFilename($filename, $callback)
363 {
364 return preg_replace_callback(
365 "#([\x01-\x1F" . preg_quote(self::INVALID_FILENAME_CHARS, "#") . "]|" . self::INVALID_FILENAME_BYTES . ")#",
366 $callback,
368 );
369 }
370
375 public static function randomizeInvalidFilename($filename)
376 {
377 return static::replaceInvalidFilename($filename,
378 function () {
379 return chr(rand(97, 122));
380 }
381 );
382 }
383
384 public static function isAbsolute($path)
385 {
386 return (str_starts_with($path, "/")) || preg_match("#^[a-z]:/#i", $path);
387 }
388
389 protected static function getDirectoryIndexArray()
390 {
391 static $directoryIndexDefault = ["index.php" => 1, "index.html" => 1, "index.htm" => 1, "index.phtml" => 1, "default.html" => 1, "index.php3" => 1];
392
393 $directoryIndex = Main\Config\Configuration::getValue("directory_index");
394 if ($directoryIndex !== null)
395 {
396 return $directoryIndex;
397 }
398
399 return $directoryIndexDefault;
400 }
401}
$path
Определения access_edit.php:21
$basePath
Определения include.php:41
static getValue($name)
Определения configuration.php:24
Определения path.php:12
static convertUriToPhysical($path)
Определения path.php:215
static validate($path)
Определения path.php:337
static convertPhysicalToLogical($path)
Определения path.php:144
static randomizeInvalidFilename($filename)
Определения path.php:375
static validateCommon($path)
Определения path.php:312
static normalize($path)
Определения path.php:22
static isAbsolute($path)
Определения path.php:384
static $logicalEncoding
Определения path.php:20
static getLogicalEncoding()
Определения path.php:232
static convertRelativeToAbsolute($relativePath)
Определения path.php:286
const DIRECTORY_SEPARATOR
Определения path.php:13
static convertPhysicalToUri($path)
Определения path.php:181
static getPhysicalEncoding()
Определения path.php:237
static convertLogicalToPhysical($path)
Определения path.php:124
static convertLogicalToUri($path)
Определения path.php:164
static combine(... $args)
Определения path.php:254
static convertSiteRelativeToAbsolute($relativePath, $site=null)
Определения path.php:300
static getDirectory($path)
Определения path.php:119
const INVALID_FILENAME_BYTES
Определения path.php:17
static getExtension($path)
Определения path.php:94
static validateFilename($filename)
Определения path.php:347
const INVALID_FILENAME_CHARS
Определения path.php:14
static getDirectoryIndexArray()
Определения path.php:389
static truncateIndexFile($path)
Определения path.php:198
static replaceInvalidFilename($filename, $callback)
Определения path.php:362
static $physicalEncoding
Определения path.php:19
static getName($path)
Определения path.php:108
static getDocumentRoot($siteId=null)
Определения site.php:36
static convertEncoding($data, $charsetFrom, $charsetTo)
Определения encoding.php:17
static getLastPosition($haystack, $needle)
Определения utfsafestring.php:12
$filename
Определения file_edit.php:47
$result
Определения get_property_values.php:14
$p
Определения group_list_element_edit.php:23
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(!Loader::includeModule('sale')) $pattern
Определения index.php:20
const SITE_ID
Определения sonet_set_content_view.php:12
$site
Определения yandex_run.php:614