1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
uri.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main\Web;
11
12use Bitrix\Main;
13use Bitrix\Main\Text\Encoding;
14use Psr\Http\Message\UriInterface;
15
16class Uri implements \JsonSerializable, UriInterface
17{
18 protected $scheme = '';
19 protected $host = '';
20 protected $port = null;
21 protected $user = '';
22 protected $pass = '';
23 protected $path = '';
24 protected $query = '';
25 protected $fragment = '';
26
30 public function __construct($url)
31 {
32 if (str_starts_with($url, '/'))
33 {
34 //we don't support "current scheme" e.g. "//host/path"
35 $url = '/' . ltrim($url, '/');
36 }
37
38 $parsedUrl = parse_url($url);
39
40 if ($parsedUrl !== false)
41 {
42 $this->scheme = strtolower($parsedUrl['scheme'] ?? '');
43 $this->setHost($parsedUrl['host'] ?? '');
44 $this->port = $parsedUrl['port'] ?? null;
45 $this->setUser($parsedUrl['user'] ?? '');
46 $this->setPass($parsedUrl['pass'] ?? '');
47 $this->path = $parsedUrl['path'] ?? '';
48 $this->query = $parsedUrl['query'] ?? '';
49 $this->fragment = $parsedUrl['fragment'] ?? '';
50 }
51 }
52
56 public function getUrl()
57 {
58 return $this->getLocator();
59 }
60
65 public function getLocator()
66 {
67 $uri = '';
68
69 $scheme = $this->getScheme();
70 if ($scheme != '')
71 {
72 $uri .= $scheme . ':';
73 }
74
75 $authority = $this->getAuthority();
76 $path = $this->getPath();
77 if ($authority != '')
78 {
79 $uri .= '//' . $authority;
80
81 if (!str_starts_with($path, '/'))
82 {
83 $uri .= '/';
84 }
85 $uri .= $path;
86 }
87 else
88 {
89 $uri .= $path;
90 }
91
92 $query = $this->getQuery();
93 if ($query != '')
94 {
95 $uri .= '?' . $query;
96 }
97
98 return $uri;
99 }
100
105 public function getUri()
106 {
107 $url = $this->getLocator();
108 $fragment = $this->getFragment();
109
110 if ($fragment != '')
111 {
112 $url .= '#' . $fragment;
113 }
114
115 return $url;
116 }
117
121 public function getFragment(): string
122 {
123 return $this->fragment;
124 }
125
129 public function getHost(): string
130 {
131 return $this->host;
132 }
133
139 public function setHost($host)
140 {
141 $this->host = strtolower($host);
142 return $this;
143 }
144
149 public function getPass()
150 {
151 return rawurlencode($this->pass);
152 }
153
159 public function setPass($pass)
160 {
161 $this->pass = rawurldecode($pass);
162 return $this;
163 }
164
168 public function getPath(): string
169 {
170 // TODO: make it work as described
171 return $this->path;
172 }
173
179 public function setPath($path)
180 {
181 $this->path = $path;
182 return $this;
183 }
184
189 public function getPathQuery()
190 {
191 $pathQuery = $this->getPath();
192
193 if ($pathQuery == '')
194 {
195 $pathQuery = '/';
196 }
197
198 $query = $this->getQuery();
199
200 if ($query != '')
201 {
202 $pathQuery .= '?' . $query;
203 }
204
205 return $pathQuery;
206 }
207
211 public function getPort(): ?int
212 {
213 if ($this->port === null)
214 {
215 switch ($this->getScheme())
216 {
217 case 'https':
218 return 443;
219 case 'http':
220 return 80;
221 default:
222 return null;
223 }
224 }
225 return (int)$this->port;
226 }
227
231 public function getQuery(): string
232 {
233 return $this->query;
234 }
235
239 public function getScheme(): string
240 {
241 return $this->scheme;
242 }
243
248 public function getUser()
249 {
250 return rawurlencode($this->user);
251 }
252
258 public function setUser($user)
259 {
260 $this->user = rawurldecode($user);
261 return $this;
262 }
263
269 protected static function parseParams($params)
270 {
271 $data = preg_replace_callback(
272 '/(?:^|(?<=&))[^=[]+/',
273 function($match)
274 {
275 return bin2hex(urldecode($match[0]));
276 },
277 $params
278 );
279
280 parse_str($data, $values);
281
282 return array_combine(array_map('hex2bin', array_keys($values)), $values);
283 }
284
291 public function deleteParams(array $params, $preserveDots = false)
292 {
293 $query = $this->getQuery();
294 if ($query != '')
295 {
296 if ($preserveDots)
297 {
298 $currentParams = static::parseParams($query);
299 }
300 else
301 {
302 $currentParams = [];
303 parse_str($query, $currentParams);
304 }
305
306 foreach($params as $param)
307 {
308 unset($currentParams[$param]);
309 }
310
311 $this->query = http_build_query($currentParams, '', '&', PHP_QUERY_RFC3986);
312 }
313 return $this;
314 }
315
322 public function addParams(array $params, $preserveDots = false)
323 {
324 $currentParams = [];
325 $query = $this->getQuery();
326
327 if ($query != '')
328 {
329 if ($preserveDots)
330 {
331 $currentParams = static::parseParams($query);
332 }
333 else
334 {
335 parse_str($query, $currentParams);
336 }
337 }
338
339 $currentParams = array_replace($currentParams, $params);
340
341 $this->query = http_build_query($currentParams, '', '&', PHP_QUERY_RFC3986);
342
343 return $this;
344 }
345
349 public function __toString(): string
350 {
351 return $this->getUri();
352 }
353
361 public function jsonSerialize(): string
362 {
363 return $this->getUri();
364 }
365
370 public function convertToPunycode()
371 {
372 $host = \CBXPunycode::ToASCII($this->getHost(), $encodingErrors);
373
374 if (!empty($encodingErrors))
375 {
376 return new Main\Error(implode("\n", $encodingErrors));
377 }
378
379 $this->setHost($host);
380
381 return $host;
382 }
383
388 public function convertToUnicode()
389 {
390 $host = \CBXPunycode::ToUnicode($this->getHost(), $encodingErrors);
391
392 if (!empty($encodingErrors))
393 {
394 return new Main\Error(implode("\n", $encodingErrors));
395 }
396
397 $this->setHost($host);
398
399 return $host;
400 }
401
405 public function isPathTraversal(): bool
406 {
407 return (bool)preg_match("#(?:/|2f|^|\\\\|5c)(?:(?:%0*(25)*2e)|\\.){2,}(?:/|%0*(25)*2f|\\\\|%0*(25)*5c|$)#i", $this->getPath());
408 }
409
416 public static function urnEncode($str, $charset = 'UTF-8')
417 {
418 $result = '';
419 $parts = preg_split("#(://|:\\d+/|/|\\?|=|&)#", $str, -1, PREG_SPLIT_DELIM_CAPTURE);
420
421 if ($charset === false)
422 {
423 foreach ($parts as $i => $part)
424 {
425 $result .= ($i % 2) ? $part : rawurlencode($part);
426 }
427 }
428 else
429 {
430 $currentCharset = Main\Context::getCurrent()->getCulture()->getCharset();
431 foreach ($parts as $i => $part)
432 {
433 $result .= ($i % 2) ? $part : rawurlencode(Encoding::convertEncoding($part, $currentCharset, $charset));
434 }
435 }
436 return $result;
437 }
438
445 public static function urnDecode($str, $charset = false)
446 {
447 $result = '';
448 $parts = preg_split("#(://|:\\d+/|/|\\?|=|&)#", $str, -1, PREG_SPLIT_DELIM_CAPTURE);
449
450 if ($charset === false)
451 {
452 foreach ($parts as $i => $part)
453 {
454 $result .= ($i % 2) ? $part : rawurldecode($part);
455 }
456 }
457 else
458 {
459 $currentCharset = Main\Context::getCurrent()->getCulture()->getCharset();
460 foreach ($parts as $i => $part)
461 {
462 $result .= ($i % 2) ? $part : rawurldecode(Encoding::convertEncoding($part, $charset, $currentCharset));
463 }
464 }
465 return $result;
466 }
467
473 public function toAbsolute(string $host = null): Uri
474 {
475 if ($this->host == '')
476 {
477 $request = Main\HttpContext::getCurrent()->getRequest();
478
479 $this->scheme = $request->isHttps() ? 'https' : 'http';
480
481 if ($host !== null)
482 {
483 $this->host = preg_replace('/:(443|80)$/', '', $host);
484 }
485 else
486 {
487 $this->host = $request->getHttpHost();
488 }
489 }
490 return $this;
491 }
492
500 public function resolveRelativeUri(Uri $base): Uri
501 {
502 if (empty($this->scheme))
503 {
504 if (empty($this->getAuthority()))
505 {
506 if (empty($this->getPath()))
507 {
508 $this->setPath($base->getPath());
509
510 if (empty($this->getQuery()))
511 {
512 $this->query = $base->getQuery();
513 }
514 }
515 else
516 {
517 if (!str_starts_with($this->getPath(), '/'))
518 {
519 $basePath = $base->getPath();
520
521 if (!empty($base->getAuthority()) && empty($basePath))
522 {
523 $this->setPath('/' . $this->getPath());
524 }
525 else
526 {
527 if (($p = strrpos($basePath, '/')) !== false)
528 {
529 $this->setPath(substr($basePath, 0, $p + 1) . $this->getPath());
530 }
531 }
532 }
533 }
534
535 // authority
536 $this->setUser($base->getUser());
537 $this->setPass($base->getPass());
538 $this->setHost($base->getHost());
539 $this->port = $base->getPort();
540 }
541
542 $this->scheme = $base->getScheme();
543 }
544
545 return $this;
546 }
547
551 public function getAuthority(): string
552 {
553 $authority = '';
554
555 $userInfo = $this->getUserInfo();
556 if ($userInfo != '')
557 {
558 $authority = $userInfo . '@';
559 }
560
561 $host = $this->getHost();
562 if ($host != '')
563 {
564 $authority .= $host;
565 $port = $this->getPort();
566
567 if ($port !== null)
568 {
569 if (($this->scheme == 'http' && $port != 80) || ($this->scheme == 'https' && $port != 443))
570 {
571 $authority .= ':' . $port;
572 }
573 }
574 }
575
576 return $authority;
577 }
578
582 public function getUserInfo(): string
583 {
584 $user = $this->getUser();
585
586 if ($user != '')
587 {
588 $password = $this->getPass();
589 return $user . ($password != '' ? ':' . $password : '');
590 }
591
592 return '';
593 }
594
598 public function withScheme(string $scheme): UriInterface
599 {
600 $new = clone $this;
601 $new->scheme = $scheme;
602
603 return $new;
604 }
605
609 public function withUserInfo(string $user, ?string $password = null): UriInterface
610 {
611 $new = clone $this;
612 $new
613 ->setUser($user)
614 ->setPass((string)$password)
615 ;
616
617 return $new;
618 }
619
623 public function withHost(string $host): UriInterface
624 {
625 $new = clone $this;
626 $new->setHost($host);
627
628 return $new;
629 }
630
634 public function withPort(?int $port): UriInterface
635 {
636 $new = clone $this;
637 $new->port = $port;
638
639 return $new;
640 }
641
645 public function withPath(string $path): UriInterface
646 {
647 $new = clone $this;
648 $new->setPath($path);
649
650 return $new;
651 }
652
656 public function withQuery(string $query): UriInterface
657 {
658 $new = clone $this;
659 $new->query = $query;
660
661 return $new;
662 }
663
667 public function withFragment(string $fragment): UriInterface
668 {
669 $new = clone $this;
670 $new->fragment = $fragment;
671
672 return $new;
673 }
674}
$basePath
Определения include.php:41
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
static getCurrent()
Определения context.php:239
Определения error.php:15
Определения uri.php:17
$path
Определения uri.php:23
$pass
Определения uri.php:22
$scheme
Определения uri.php:18
getAuthority()
Определения uri.php:551
getUri()
Определения uri.php:105
withFragment(string $fragment)
Определения uri.php:667
getPath()
Определения uri.php:168
setHost($host)
Определения uri.php:139
getHost()
Определения uri.php:129
getLocator()
Определения uri.php:65
resolveRelativeUri(Uri $base)
Определения uri.php:500
setPass($pass)
Определения uri.php:159
setUser($user)
Определения uri.php:258
withUserInfo(string $user, ?string $password=null)
Определения uri.php:609
getQuery()
Определения uri.php:231
$user
Определения uri.php:21
static urnDecode($str, $charset=false)
Определения uri.php:445
setPath($path)
Определения uri.php:179
toAbsolute(string $host=null)
Определения uri.php:473
$host
Определения uri.php:19
__toString()
Определения uri.php:349
getUserInfo()
Определения uri.php:582
withScheme(string $scheme)
Определения uri.php:598
withQuery(string $query)
Определения uri.php:656
getScheme()
Определения uri.php:239
convertToUnicode()
Определения uri.php:388
isPathTraversal()
Определения uri.php:405
static parseParams($params)
Определения uri.php:269
$fragment
Определения uri.php:25
getFragment()
Определения uri.php:121
deleteParams(array $params, $preserveDots=false)
Определения uri.php:291
$port
Определения uri.php:20
getPass()
Определения uri.php:149
convertToPunycode()
Определения uri.php:370
withHost(string $host)
Определения uri.php:623
getPathQuery()
Определения uri.php:189
addParams(array $params, $preserveDots=false)
Определения uri.php:322
getUrl()
Определения uri.php:56
withPath(string $path)
Определения uri.php:645
jsonSerialize()
Определения uri.php:361
withPort(?int $port)
Определения uri.php:634
getUser()
Определения uri.php:248
$query
Определения uri.php:24
__construct($url)
Определения uri.php:30
getPort()
Определения uri.php:211
static urnEncode($str, $charset='UTF-8')
Определения uri.php:416
static ToUnicode($domainName, &$arErrors)
Определения punycode.php:65
static ToASCII($domainName, &$arErrors)
Определения punycode.php:44
$str
Определения commerceml2.php:63
$data['IS_AVAILABLE']
Определения .description.php:13
$new
Определения file_edit.php:48
</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
$p
Определения group_list_element_edit.php:23
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
$password
Определения mysql_to_pgsql.php:34
$i
Определения factura.php:643
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
path
Определения template_copy.php:201
$url
Определения iframe.php:7