Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
redirect.php
1<?php
2
4
8
10{
12 private $url;
14 private $skipSecurity;
15
16 public function __construct($url, bool $skipSecurity = false)
17 {
18 parent::__construct();
19
20 $this
21 ->setStatus('302 Found')
22 ->setSkipSecurity($skipSecurity)
23 ->setUrl($url)
24 ;
25 }
26
30 public function getUrl()
31 {
32 return $this->url;
33 }
34
39 public function setUrl($url)
40 {
41 $this->url = $url;
42
43 return $this;
44 }
45
49 public function isSkippedSecurity(): bool
50 {
51 return $this->skipSecurity;
52 }
53
58 public function setSkipSecurity(bool $skipSecurity)
59 {
60 $this->skipSecurity = $skipSecurity;
61
62 return $this;
63 }
64
65 private function checkTrial(): bool
66 {
67 $isTrial =
68 defined("DEMO") && DEMO === "Y" &&
69 (
70 !defined("SITEEXPIREDATE") ||
71 !defined("OLDSITEEXPIREDATE") ||
72 SITEEXPIREDATE == '' ||
73 SITEEXPIREDATE != OLDSITEEXPIREDATE
74 )
75 ;
76
77 return $isTrial;
78 }
79
80 private function isExternalUrl($url): bool
81 {
82 return preg_match("'^(http://|https://|ftp://)'i", $url);
83 }
84
85 private function modifyBySecurity($url)
86 {
88 global $APPLICATION;
89
90 $isExternal = $this->isExternalUrl($url);
91 if (!$isExternal && !str_starts_with($url, "/"))
92 {
93 $url = $APPLICATION->GetCurDir() . $url;
94 }
95 //doubtful about &amp; and http response splitting defence
96 $url = str_replace(["&amp;", "\r", "\n"], ["&", "", ""], $url);
97
98 if (!defined("BX_UTF") && defined("LANG_CHARSET"))
99 {
100 $url = Encoding::convertEncoding($url, LANG_CHARSET, "UTF-8");
101 }
102
103 return $url;
104 }
105
106 private function processInternalUrl($url)
107 {
109 global $APPLICATION;
110 //store cookies for next hit (see CMain::GetSpreadCookieHTML())
111 $APPLICATION->StoreCookies();
112
113 $server = Context::getCurrent()->getServer();
114 $protocol = Context::getCurrent()->getRequest()->isHttps() ? "https" : "http";
115 $host = $server->getHttpHost();
116 $port = (int)$server->getServerPort();
117 if ($port !== 80 && $port !== 443 && $port > 0 && strpos($host, ":") === false)
118 {
119 $host .= ":" . $port;
120 }
121
122 return "{$protocol}://{$host}{$url}";
123 }
124
125 public function send()
126 {
127 if ($this->checkTrial())
128 {
129 die(Main\Localization\Loc::getMessage('MAIN_ENGINE_REDIRECT_TRIAL_EXPIRED'));
130 }
131
132 $url = $this->getUrl();
133 $isExternal = $this->isExternalUrl($url);
134 $url = $this->modifyBySecurity($url);
135
136
137 foreach (GetModuleEvents("main", "OnBeforeLocalRedirect", true) as $event)
138 {
139 ExecuteModuleEventEx($event, [&$url, $this->isSkippedSecurity(), &$isExternal, $this]);
140 }
141
142 if (!$isExternal)
143 {
144 $url = $this->processInternalUrl($url);
145 }
146
147 $this->addHeader('Location', $url);
148 foreach (GetModuleEvents("main", "OnLocalRedirect", true) as $event)
149 {
150 ExecuteModuleEventEx($event);
151 }
152
153 Main\Application::getInstance()->getKernelSession()["BX_REDIRECT_TIME"] = time();
154
155 parent::send();
156 }
157}
static getCurrent()
Definition context.php:241
setSkipSecurity(bool $skipSecurity)
Definition redirect.php:58
__construct($url, bool $skipSecurity=false)
Definition redirect.php:16
addHeader($name, $value='')
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29