Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
proxy.php
1
<?
2
3
namespace
Bitrix\Main\UI\ImageEditor
;
4
5
use
Bitrix\Main\Application
;
6
use
Bitrix\Main\Config\Option
;
7
use
Bitrix\Main\Web\Uri
;
8
use
Bitrix\Main\Web\HttpClient
;
9
14
class
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
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\Config\Option
Definition
option.php:15
Bitrix\Main\UI\ImageEditor\Proxy
Definition
proxy.php:15
Bitrix\Main\UI\ImageEditor\Proxy\isAllowedHost
isAllowedHost($host)
Definition
proxy.php:128
Bitrix\Main\UI\ImageEditor\Proxy\getUserAllowedHosts
getUserAllowedHosts()
Definition
proxy.php:106
Bitrix\Main\UI\ImageEditor\Proxy\getEnabledOption
static getEnabledOption()
Definition
proxy.php:162
Bitrix\Main\UI\ImageEditor\Proxy\getCurrentHttpHost
getCurrentHttpHost()
Definition
proxy.php:76
Bitrix\Main\UI\ImageEditor\Proxy\$allowedHosts
$allowedHosts
Definition
proxy.php:20
Bitrix\Main\UI\ImageEditor\Proxy\__construct
__construct($url, $allowedHosts=[])
Definition
proxy.php:28
Bitrix\Main\UI\ImageEditor\Proxy\output
output()
Definition
proxy.php:186
Bitrix\Main\UI\ImageEditor\Proxy\isAuthorized
static isAuthorized()
Definition
proxy.php:65
Bitrix\Main\UI\ImageEditor\Proxy\$uri
$uri
Definition
proxy.php:17
Bitrix\Main\UI\ImageEditor\Proxy\getResponse
static getResponse()
Definition
proxy.php:57
Bitrix\Main\UI\ImageEditor\Proxy\isEnabledForWhiteList
static isEnabledForWhiteList()
Definition
proxy.php:152
Bitrix\Main\UI\ImageEditor\Proxy\isEnabledForAll
isEnabledForAll()
Definition
proxy.php:142
Bitrix\Main\UI\ImageEditor\Proxy\getAllowedHosts
getAllowedHosts()
Definition
proxy.php:93
Bitrix\Main\UI\ImageEditor\Proxy\isEnabledForHost
isEnabledForHost($host)
Definition
proxy.php:181
Bitrix\Main\Web\HttpClient
Definition
httpclient.php:24
Bitrix\Main\Web\Uri
Definition
uri.php:17
Bitrix\Main\UI\ImageEditor
Definition
proxy.php:3
modules
main
lib
ui
imageeditor
proxy.php
Создано системой
1.10.0