Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
httprequest.php
1<?php
2
10namespace Bitrix\Main;
11
13
19class HttpRequest extends Request
20{
24 protected $queryString;
28 protected $postData;
32 protected $files;
36 protected $cookies;
40 protected $cookiesRaw;
44 protected $jsonData;
48 protected $headers;
49 protected $httpHost;
51
61 public function __construct(Server $server, array $queryString, array $postData, array $files, array $cookies)
62 {
63 $request = array_merge($queryString, $postData);
64 parent::__construct($server, $request);
65
66 $this->queryString = new Type\ParameterDictionary($queryString);
67 $this->postData = new Type\ParameterDictionary($postData);
68 $this->files = new Type\ParameterDictionary($files);
69 $this->cookiesRaw = new Type\ParameterDictionary($cookies);
70 $this->cookies = new Type\ParameterDictionary($this->prepareCookie($cookies));
71 $this->headers = $this->buildHttpHeaders($server);
72 $this->jsonData = new Type\ParameterDictionary();
73 }
74
75 private function buildHttpHeaders(Server $server)
76 {
77 $headers = new HttpHeaders();
78 foreach ($this->fetchHeaders($server) as $headerName => $value)
79 {
80 $headers->add($headerName, $value);
81 }
82
83 return $headers;
84 }
85
91 public function addFilter(Type\IRequestFilter $filter)
92 {
93 parent::addFilter($filter);
94
95 $filteredValues = $filter->filter([
96 'get' => $this->queryString->values,
97 'post' => $this->postData->values,
98 'files' => $this->files->values,
99 'cookie' => $this->cookiesRaw->values,
100 'json' => $this->jsonData->values,
101 ]);
102
103 if (isset($filteredValues['get']))
104 {
105 $this->queryString->setValuesNoDemand($filteredValues['get']);
106 }
107 if (isset($filteredValues['post']))
108 {
109 $this->postData->setValuesNoDemand($filteredValues['post']);
110 }
111 if (isset($filteredValues['files']))
112 {
113 $this->files->setValuesNoDemand($filteredValues['files']);
114 }
115 if (isset($filteredValues['cookie']))
116 {
117 $this->cookiesRaw->setValuesNoDemand($filteredValues['cookie']);
118 $this->cookies = new Type\ParameterDictionary($this->prepareCookie($filteredValues['cookie']));
119 }
120 if (isset($filteredValues['json']))
121 {
122 $this->jsonData->setValuesNoDemand($filteredValues['json']);
123 }
124
125 if (isset($filteredValues['get']) || isset($filteredValues['post']))
126 {
127 $this->setValuesNoDemand(array_merge($this->queryString->values, $this->postData->values));
128 }
129 }
130
137 public function getQuery($name)
138 {
139 return $this->queryString->get($name);
140 }
141
147 public function getQueryList()
148 {
149 return $this->queryString;
150 }
151
158 public function getPost($name)
159 {
160 return $this->postData->get($name);
161 }
162
168 public function getPostList()
169 {
170 return $this->postData;
171 }
172
179 public function getFile($name)
180 {
181 return $this->files->get($name);
182 }
183
189 public function getFileList()
190 {
191 return $this->files;
192 }
193
201 public function getHeader($name)
202 {
203 return $this->headers->get($name);
204 }
205
211 public function getHeaders()
212 {
213 return $this->headers;
214 }
215
222 public function getCookie($name)
223 {
224 return $this->cookies->get($name);
225 }
226
232 public function getCookieList()
233 {
234 return $this->cookies;
235 }
236
237 public function getCookieRaw($name)
238 {
239 return $this->cookiesRaw->get($name);
240 }
241
242 public function getCookieRawList()
243 {
244 return $this->cookiesRaw;
245 }
246
247 public function getJsonList()
248 {
249 return $this->jsonData;
250 }
251
252 public function getRemoteAddress()
253 {
254 return $this->server->get('REMOTE_ADDR');
255 }
256
261 public function getUserAgent()
262 {
263 return $this->server->get('HTTP_USER_AGENT');
264 }
265
266 public function getRequestUri()
267 {
268 return $this->server->getRequestUri();
269 }
270
271 public function getRequestMethod()
272 {
273 return $this->server->getRequestMethod();
274 }
275
281 public function getServerPort()
282 {
283 return $this->server->getServerPort();
284 }
285
286 public function isPost()
287 {
288 return ($this->getRequestMethod() == 'POST');
289 }
290
291 public function getAcceptedLanguages()
292 {
293 if ($this->acceptedLanguages === null)
294 {
295 $this->acceptedLanguages = [];
296
297 $acceptedLanguages = explode(',', $this->server->get('HTTP_ACCEPT_LANGUAGE'));
298 foreach ($acceptedLanguages as $language)
299 {
300 $lang = explode(';', $language);
301 $this->acceptedLanguages[] = $lang[0];
302 }
303 }
304
306 }
307
313 public function getRequestedPage()
314 {
315 if ($this->requestedPage === null)
316 {
317 if (($uri = $this->getRequestUri()) == '')
318 {
319 $this->requestedPage = parent::getRequestedPage();
320 }
321 else
322 {
323 $parsedUri = new Web\Uri("http://" . $this->server->getHttpHost() . $uri);
324 $this->requestedPage = static::normalize(static::decode($parsedUri->getPath()));
325 }
326 }
328 }
329
335 public function getDecodedUri()
336 {
337 $parsedUri = new Web\Uri("http://" . $this->server->getHttpHost() . $this->getRequestUri());
338
339 $uri = static::decode($parsedUri->getPath());
340
341 if (($query = $parsedUri->getQuery()) != '')
342 {
343 $uri .= "?" . $query;
344 }
345
346 return $uri;
347 }
348
349 protected static function decode($url)
350 {
351 return Text\Encoding::convertEncodingToCurrent(rawurldecode($url));
352 }
353
358 public function getHttpHost()
359 {
360 if ($this->httpHost === null)
361 {
362 //scheme can be anything, it's used only for parsing
363 $url = new Web\Uri("http://" . $this->server->getHttpHost());
364 $host = $url->getHost();
365 $host = trim($host, "\t\r\n\0 .");
366
367 $this->httpHost = $host;
368 }
369
370 return $this->httpHost;
371 }
372
373 public function isHttps()
374 {
375 if ($this->server->get("SERVER_PORT") == 443)
376 {
377 return true;
378 }
379
380 $https = $this->server->get("HTTPS");
381 if ($https != '' && strtolower($https) != "off")
382 {
383 //From the PHP manual: Set to a non-empty value if the script was queried through the HTTPS protocol.
384 //Note that when using ISAPI with IIS, the value will be off if the request was not made through the HTTPS protocol.
385 return true;
386 }
387
388 return (Config\Configuration::getValue("https_request") === true);
389 }
390
392 {
393 if ($queryString != '')
394 {
395 parse_str($queryString, $vars);
396
397 $this->values += $vars;
398 $this->queryString->values += $vars;
399 }
400 }
401
406 protected function prepareCookie(array $cookies)
407 {
408 static $cookiePrefix = null;
409 if ($cookiePrefix === null)
410 {
411 $cookiePrefix = Config\Option::get("main", "cookie_name", "BITRIX_SM") . "_";
412 }
413
414 $cookiePrefixLength = mb_strlen($cookiePrefix);
415
416 $cookiesCrypter = new Web\CookiesCrypter();
417 $cookiesNew = $cookiesToDecrypt = [];
418 foreach ($cookies as $name => $value)
419 {
420 if (mb_strpos($name, $cookiePrefix) !== 0)
421 {
422 continue;
423 }
424
425 $name = mb_substr($name, $cookiePrefixLength);
426 if (is_string($value) && $cookiesCrypter->shouldDecrypt($name, $value))
427 {
428 $cookiesToDecrypt[$name] = $value;
429 }
430 else
431 {
432 $cookiesNew[$name] = $value;
433 }
434 }
435
436 foreach ($cookiesToDecrypt as $name => $value)
437 {
438 $cookiesNew[$name] = $cookiesCrypter->decrypt($name, $value, $cookiesNew);
439 }
440
441 return $cookiesNew;
442 }
443
444 private function fetchHeaders(Server $server)
445 {
446 $headers = [];
447 foreach ($server as $name => $value)
448 {
449 if (str_starts_with($name, 'HTTP_'))
450 {
451 $headerName = substr($name, 5);
452 $headers[$headerName] = $value;
453 }
454 elseif (in_array($name, ['CONTENT_TYPE', 'CONTENT_LENGTH'], true))
455 {
456 $headers[$name] = $value;
457 }
458 }
459
460 return $this->normalizeHeaders($headers);
461 }
462
463 private function normalizeHeaders(array $headers)
464 {
465 $normalizedHeaders = [];
466 foreach ($headers as $name => $value)
467 {
468 $headerName = strtolower(str_replace('_', '-', $name));
469 $normalizedHeaders[$headerName] = $value;
470 }
471
472 return $normalizedHeaders;
473 }
474
475 protected static function normalize($path)
476 {
477 if (str_ends_with($path, "/"))
478 {
479 $path .= "index.php";
480 }
481
482 $path = IO\Path::normalize($path);
483
484 return $path;
485 }
486
492 public function getScriptFile()
493 {
494 $scriptName = $this->getScriptName();
495 if ($scriptName == "/bitrix/routing_index.php" || $scriptName == "/bitrix/urlrewrite.php" || $scriptName == "/404.php" || $scriptName == "/bitrix/virtual_file_system.php")
496 {
497 if (($v = $this->server->get("REAL_FILE_PATH")) != null)
498 {
499 $scriptName = $v;
500 }
501 }
502 return $scriptName;
503 }
504
509 public static function getSystemParameters()
510 {
511 static $params = [
512 "login",
513 "login_form",
514 "logout",
515 "register",
516 "forgot_password",
517 "change_password",
518 "confirm_registration",
519 "confirm_code",
520 "confirm_user_id",
521 "bitrix_include_areas",
522 "clear_cache",
523 "show_page_exec_time",
524 "show_include_exec_time",
525 "show_sql_stat",
526 "show_cache_stat",
527 "show_link_stat",
528 "sessid",
529 ];
530 return $params;
531 }
532
537 public static function getInput()
538 {
539 return file_get_contents("php://input");
540 }
541
546 public function getCookiesMode()
547 {
549 }
550
551 public function isJson(): bool
552 {
553 $contentType = $this->headers->getContentType();
554 if (!$contentType)
555 {
556 return false;
557 }
558 if ($contentType === 'application/json')
559 {
560 return true;
561 }
562
563 return str_contains($contentType, '+json');
564 }
565
569 public function decodeJson(): void
570 {
571 if ($this->isJson())
572 {
573 try
574 {
575 $json = Web\Json::decode(static::getInput());
576 if (is_array($json))
577 {
578 $this->jsonData = new Type\ParameterDictionary($json);
579 }
580 }
581 catch (ArgumentException)
582 {
583 }
584 }
585 }
586}
prepareCookie(array $cookies)
modifyByQueryString($queryString)
addFilter(Type\IRequestFilter $filter)
__construct(Server $server, array $queryString, array $postData, array $files, array $cookies)