Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
path.php
1<?php
2namespace Bitrix\Translate\IO;
3
6
7class Path extends Main\IO\Path
8{
16 public static function tidy(string $path): string
17 {
18 $modifier = Translate\Config::isUtfMode() ? 'u' : '';
19
20 return \preg_replace("#[\\\\\\/]+#".$modifier, self::DIRECTORY_SEPARATOR, $path);
21 }
22
30 public static function secure(string $path): string
31 {
32 $modifier = Translate\Config::isUtfMode() ? 'u' : '';
33
34 return \preg_replace("#\.\.+[\/\\\]+#i".$modifier, '', $path);
35 }
36
45 public static function isLangDir(string $path, bool $additionalCheck = false): bool
46 {
47 $modifier = Translate\Config::isUtfMode() ? 'u' : '';
48 if (\preg_match("#/lang/([^/]*?)(/|\$)#".$modifier, $path, $match))
49 {
50 foreach (Translate\IGNORE_LANG_NAMES as $check)
51 {
52 if (\mb_strpos($path, '/lang/'.$match[1].'/'.$check.'/') !== false)
53 {
54 return false;
55 }
56 }
57 if ($additionalCheck)
58 {
59 $arr = \explode(self::DIRECTORY_SEPARATOR, $path);
60 $langKey = \array_search('lang', $arr) + 1;
61
62 return \array_key_exists($langKey, $arr) && $arr[$langKey] <> '';
63 }
64
65 return true;
66 }
67
68 return false;
69 }
70
78 public static function extractLangId(string $path): ?string
79 {
80 $arr = \explode(self::DIRECTORY_SEPARATOR, $path);
81 $pos = \array_search('lang', $arr);
82 if ($pos !== false && !empty($arr[$pos + 1]))
83 {
84 return $arr[$pos + 1];
85 }
86
87 return null;
88 }
89
98 public static function replaceLangId(string $path, string $langId): string
99 {
100 $modifier = Translate\Config::isUtfMode() ? 'u' : '';
101
102 return \preg_replace("#^(.*?/lang/)([^/]*?)(/|$)#".$modifier, "\\1$langId\\3", $path);
103 }
104
105
114 public static function removeLangId(string $path, ?array $langs = null): string
115 {
116 static $defLangs = [];
117 if (empty($langs))
118 {
119 if (empty($defLangs))
120 {
121 $defLangs = \array_unique(\array_merge(
122 Translate\Config::getDefaultLanguages(),
123 Translate\Config::getEnabledLanguages()
124 ));
125 }
126 $langs = $defLangs;
127 }
128 $arr = \explode(self::DIRECTORY_SEPARATOR, $path);
129 if (\in_array('lang', $arr))
130 {
131 $langKey = \array_search('lang', $arr) + 1;
132 if (\in_array($arr[$langKey], $langs))
133 {
134 unset($arr[$langKey]);
135 }
136 $path = \implode(self::DIRECTORY_SEPARATOR, $arr);
137 }
138
139 return $path;
140 }
141
151 public static function addLangId(string $path, string $langId, ?array $langs = null): string
152 {
153 $pathTemp = self::removeLangId($path, $langs);
154
155 $arr = \explode('/', $pathTemp);
156 if (\in_array('lang', $arr))
157 {
158 $arr1 = array();
159 foreach($arr as $d)
160 {
161 $arr1[] = $d;
162 if ($d === 'lang')
163 {
164 $arr1[] = $langId;
165 }
166 }
167 $path = \implode('/', $arr1);
168 }
169
170 return $path;
171 }
172
180 public static function checkCreatePath(string $path): bool
181 {
182 $path = self::normalize($path);
183 $path = \rtrim($path, self::DIRECTORY_SEPARATOR);
184
185 if($path == '')
186 {
187 //current folder always exists
188 return true;
189 }
190
191 if (!\file_exists($path))
192 {
193 return \mkdir($path, \BX_DIR_PERMISSIONS, true);
194 }
195
196 return \is_dir($path);
197 }
198
206 public static function isPhpFile(string $path, bool $checkExistence = false): bool
207 {
208 return $checkExistence
209 ? (\mb_substr($path, -4) === '.php') && \is_file($path)
210 : (\mb_substr($path, -4) === '.php');
211 }
212}
static normalize($path)
Definition path.php:26
static replaceLangId(string $path, string $langId)
Definition path.php:98
static secure(string $path)
Definition path.php:30
static tidy(string $path)
Definition path.php:16
static isLangDir(string $path, bool $additionalCheck=false)
Definition path.php:45
static extractLangId(string $path)
Definition path.php:78
static isPhpFile(string $path, bool $checkExistence=false)
Definition path.php:206
static checkCreatePath(string $path)
Definition path.php:180
static addLangId(string $path, string $langId, ?array $langs=null)
Definition path.php:151
static removeLangId(string $path, ?array $langs=null)
Definition path.php:114