1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
rule.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Security\W\Rules;
4
5use Bitrix\Main\IO\Path;
6use Bitrix\Main\Web\Uri;
7use Bitrix\Main\Security\W\Rules\Results\ModifyResult;
8
9abstract class Rule
10{
11 protected $path;
12
13 protected $context;
14
15 protected $keys;
16
17 protected $process;
18
19 protected $encoding;
20
21 public static function make(array $rule): ?static
22 {
23 $rule = static::prepareRuleParameters($rule);
24
25 return match ($rule['action'])
26 {
27 'intval' => new IntvalRule(
28 $rule['path'],
29 $rule['context'],
30 $rule['keys'],
31 $rule['process'],
32 $rule['encoding']
33 ),
34 'preg_replace' => new PregReplaceRule(
35 $rule['path'],
36 $rule['context'],
37 $rule['keys'],
38 $rule['process'],
39 $rule['encoding'],
40 $rule['pattern']
41 ),
42 'preg_match' => new PregMatchRule(
43 $rule['path'],
44 $rule['context'],
45 $rule['keys'],
46 $rule['process'],
47 $rule['encoding'],
48 $rule['pattern'],
49 $rule['post_action']
50 ),
51 'check_csrf' => new CsrfRule(
52 $rule['path'],
53 $rule['context'],
54 $rule['keys'],
55 $rule['process'],
56 $rule['encoding'],
57 $rule['pattern'],
58 ),
59 default => null,
60 };
61 }
62
63 protected static function prepareRuleParameters(array $parameters): array
64 {
65 if (is_string($parameters['action']))
66 {
67 $parameters['action'] = strtolower($parameters['action']);
68 }
69 elseif (is_array($parameters['action']))
70 {
71 $complexAction = $parameters['action'];
72
73 $parameters['action'] = $complexAction[0];
74 $parameters['post_action'] = $complexAction[1];
75 }
76
77 $parameters['encoding'] = !empty($parameters['encoding'])
78 ? $parameters['encoding']
79 : [];
80
81 if (is_string($parameters['encoding']))
82 {
83 $parameters['encoding'] = [$parameters['encoding']];
84 }
85
86 return $parameters;
87 }
88
96 {
97 $this->path = $path;
98 $this->context = $this->castContext($context);
99 $this->keys = $this->castKeys($keys);
100 $this->process = $process;
101 $this->encoding = $encoding;
102 }
103
104 public function evaluateValue($value)
105 {
106 if (!empty($this->encoding))
107 {
108 foreach ($this->encoding as $encodingType)
109 {
110 $value = match ($encodingType)
111 {
112 'gz' => gzdecode($value),
113 'base64' => base64_decode($value),
114 'url' => urldecode($value),
115 'hex' => hex2bin($value)
116 };
117 }
118 }
119
120 $result = $this->evaluate($value);
121
122 if (!empty($this->encoding) && $result instanceof ModifyResult)
123 {
124 $cleanValue = $result->getCleanValue();
125
126 foreach (array_reverse($this->encoding) as $encodingType)
127 {
128 $cleanValue = match ($encodingType)
129 {
130 'gz' => gzencode($cleanValue),
131 'base64' => base64_encode($cleanValue),
132 'url' => urlencode($cleanValue),
133 'hex' => bin2hex($cleanValue)
134 };
135 }
136
137 $result = new ModifyResult($cleanValue);
138 }
139
140 return $result;
141 }
142
143 abstract public function evaluate($value);
144
145 protected function castContext($context)
146 {
147 if (!is_array($context))
148 {
149 $context = [$context];
150 }
151
152 foreach ($context as $k => $v)
153 {
154 $context[$k] = strtolower($v);
155 }
156
157 return $context;
158 }
159
160 protected function castKeys($keys)
161 {
162 if (!is_array($keys))
163 {
164 $keys = [$keys];
165 }
166
167 return $keys;
168 }
169
170 public function matchKey(array $contextKey): bool
171 {
172 $contextKey = join('.', $contextKey);
173
174 foreach ($this->keys as $key)
175 {
176 //if ($key === $contextKey)
177 // bxu_files.validKey.phpinfo();die(); => bxu_files.*
178 if (fnmatch($key, $contextKey))
179 {
180 return true;
181 }
182 }
183
184 return false;
185 }
186
195 public function matchPath($uri)
196 {
197 if ($this->path === '*')
198 {
199 return true;
200 }
201
202 // normalize uri
203 $parsedUri = new Uri($uri);
204 $_uri = $parsedUri->getPath();
205
206 $_uri = rawurldecode($_uri);
207//
208// if (Application::hasInstance())
209// {
210// $_uri = Encoding::convertEncodingToCurrent($_uri);
211// }
212
213 if (str_ends_with($_uri, '/'))
214 {
215 $_uri .= 'index.php';
216 }
217
218 $_uri = Path::normalize($_uri);
219
220 // valid uris
221 $cleanUris[] = $_uri;
222
223 if (str_ends_with($_uri, '/index.php'))
224 {
225 $cleanUris[] = substr($_uri, 0, -9);
226 }
227 elseif (str_ends_with($_SERVER['SCRIPT_NAME'], '/index.php'))
228 {
229 $cleanUris[] = substr($_SERVER['SCRIPT_NAME'], 0, -9);
230 }
231
232 if ($_uri !== $_SERVER['SCRIPT_NAME'])
233 {
234 $cleanUris[] = $_SERVER['SCRIPT_NAME'];
235 }
236
237 // analyze
238 if (str_starts_with($this->path, '~'))
239 {
241 }
242 else
243 {
244 $pattern = '~^' . str_replace('~', '\~', preg_quote($this->path)) . '$~';
245 }
246
247 foreach ($cleanUris as $cleanUri)
248 {
249 if ($this->path === $cleanUri || preg_match($pattern, $cleanUri))
250 {
251 return true;
252 }
253 }
254
255 return false;
256 }
257
261 public function getPath()
262 {
263 return $this->path;
264 }
265
269 public function getContext()
270 {
271 return $this->context;
272 }
273
277 public function getKeys()
278 {
279 return $this->keys;
280 }
281
285 public function getProcess()
286 {
287 return $this->process;
288 }
289}
castContext($context)
Определения rule.php:145
matchKey(array $contextKey)
Определения rule.php:170
__construct($path, $context, $keys, $process, $encoding)
Определения rule.php:95
static make(array $rule)
Определения rule.php:21
matchPath($uri)
Определения rule.php:195
castKeys($keys)
Определения rule.php:160
evaluateValue($value)
Определения rule.php:104
static prepareRuleParameters(array $parameters)
Определения rule.php:63
Определения uri.php:17
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
if(!Loader::includeModule('sale')) $pattern
Определения index.php:20
$k
Определения template_pdf.php:567
path
Определения template_copy.php:201