Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
proxy.php
1<?
2
4
9
14class Proxy
15{
17 protected $uri;
18
20 protected $allowedHosts = [];
21
28 public function __construct($url, $allowedHosts = [])
29 {
30 $response = static::getResponse();
31
32 if (!static::isAuthorized())
33 {
34 $response->setStatus(401)->flush();
35 die('Unauthorized');
36 }
37
38 if (is_array($allowedHosts))
39 {
40 $this->allowedHosts = array_filter($allowedHosts, function($item) {
41 return is_string($item) && !empty($item);
42 });
43 }
44
45 $this->uri = new Uri($url);
46
47 if (!$this->uri->getHost())
48 {
49 $this->uri->setHost(static::getCurrentHttpHost());
50 }
51 }
52
57 protected static function getResponse()
58 {
59 return Application::getInstance()->getContext()->getResponse();
60 }
61
65 protected static function isAuthorized()
66 {
67 global $USER;
68 return ($USER->isAuthorized() && check_bitrix_sessid());
69 }
70
76 protected function getCurrentHttpHost()
77 {
78 static $server = null;
79
80 if ($server === null)
81 {
82 $server = Application::getInstance()->getContext()->getServer();
83 }
84
85 return explode(':', $server->getHttpHost())[0];
86 }
87
93 protected function getAllowedHosts()
94 {
95 return array_merge(
96 [$this->getCurrentHttpHost()],
97 $this->allowedHosts,
98 $this->getUserAllowedHosts()
99 );
100 }
101
106 protected function getUserAllowedHosts()
107 {
108 static $hosts = null;
109
110 if ($hosts === null)
111 {
112 $hosts = Option::get('main', 'imageeditor_proxy_white_list', []);
113 if (is_string($hosts))
114 {
115 $hosts = unserialize($hosts, ['allowed_classes' => false]);
116 }
117 }
118
119 return $hosts;
120 }
121
128 protected function isAllowedHost($host)
129 {
130 return (
131 in_array($host, $this->getAllowedHosts()) ||
132 (in_array('*', $this->getAllowedHosts()) && $this->isEnabledForAll())
133 );
134 }
135
136
142 protected function isEnabledForAll()
143 {
144 return static::getEnabledOption() === 'Y';
145 }
146
152 protected static function isEnabledForWhiteList()
153 {
154 return static::getEnabledOption() === 'YWL';
155 }
156
162 protected static function getEnabledOption()
163 {
164 static $option = null;
165
166 if ($option === null)
167 {
168 $option = Option::get('main', 'imageeditor_proxy_enabled', 'N');
169 }
170
171 return $option;
172 }
173
181 protected function isEnabledForHost($host)
182 {
183 return static::isEnabledForWhiteList() && $this->isAllowedHost($host);
184 }
185
186 public function output()
187 {
188 $remoteHost = $this->uri->getHost();
189 $currentHost = static::getCurrentHttpHost();
190 $response = static::getResponse();
191
192 if ($this->isEnabledForAll() ||
193 $this->isEnabledForHost($remoteHost) ||
194 $remoteHost === $currentHost)
195 {
196 $client = new HttpClient();
197
198 //prevents proxy to LAN
199 $client->setPrivateIp(false);
200
201 $contents = $client->get($this->uri->getUri());
202 if($contents !== false)
203 {
204 $response->addHeader('Content-Type', $client->getContentType());
205 $response->flush($contents);
206 return;
207 }
208 }
209 $response->setStatus(404)->flush();
210 }
211}
__construct($url, $allowedHosts=[])
Definition proxy.php:28