Skip to content

Commit a65cc1d

Browse files
author
Till Brehm
committed
Implemented proxy script to fix #4688
1 parent 8ca68cb commit a65cc1d

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2007-2008, Till Brehm, projektfarm Gmbh and Gabriel Kaufmann, TYPOworx.de
5+
6+
All rights reserved.
7+
8+
Redistribution and use in source and binary forms, with or without modification,
9+
are permitted provided that the following conditions are met:
10+
11+
* Redistributions of source code must retain the above copyright notice,
12+
this list of conditions and the following disclaimer.
13+
* Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
* Neither the name of ISPConfig nor the names of its contributors
17+
may be used to endorse or promote products derived from this software without
18+
specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
require_once '../../lib/config.inc.php';
33+
require_once '../../lib/app.inc.php';
34+
35+
//* Check permissions for module
36+
$app->auth->check_module_permissions('monitor');
37+
38+
39+
$app->uses('getconf');
40+
$server_config = $app->getconf->get_server_config($_SESSION['monitor']['server_id'], 'server');
41+
42+
43+
$context = !empty($_GET['context']) ? trim($_GET['context']) : '';
44+
45+
$proxy_url = '';
46+
$http_user = '';
47+
$http_password = '';
48+
if(isset($server_config[$context . '_url']))
49+
{
50+
$proxy_url = $server_config[$context . '_url'];
51+
$proxy_url = str_replace('[SERVERNAME]', $server_config['hostname'], $proxy_url);
52+
53+
if(isset($_GET['url']))
54+
{
55+
$proxy_url .= urldecode($_GET['url']);
56+
}
57+
58+
$http_user = trim($server_config[$context . '_user']);
59+
$http_password = trim($server_config[$context . '_password']);
60+
}
61+
else
62+
{
63+
header('HTTP/1.1 500');
64+
echo 'Invalid Context-Parameter.';
65+
exit;
66+
}
67+
68+
$response = null;
69+
70+
try
71+
{
72+
if(isset($http_user) || isset($http_password))
73+
{
74+
$proxy_url = str_replace('://', sprintf('://%s:%s@', $http_user, $http_password), $proxy_url);
75+
}
76+
77+
if(empty($proxy_url))
78+
{
79+
header('HTTP/1.1 500');
80+
echo 'Invalid/Empty request.';
81+
exit;
82+
}
83+
84+
$ch = curl_init($proxy_url);
85+
if($ch)
86+
{
87+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
88+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
89+
curl_setopt($ch, CURLOPT_HEADER, false);
90+
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
91+
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
92+
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
93+
94+
//curl_setopt($ch, CURLOPT_VERBOSE, true);
95+
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
96+
97+
$response = curl_exec($ch);
98+
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
99+
100+
if(empty($response_code) || empty($response))
101+
{
102+
throw new \Exception('HTTP Sub-Request failed.');
103+
}
104+
105+
// HTML-Rewrites
106+
if(strpos($response, '<html') !== false)
107+
{
108+
$response = preg_replace_callback(
109+
'/( href=)([\'"])([^"\']*)/', function($match) {
110+
$attribute = trim($match[1]);
111+
$quoteChar = trim($match[2]);
112+
$url = trim($match[3]);
113+
114+
if($url === '.')
115+
{
116+
$url = '/';
117+
}
118+
119+
if(strpos($url, '/') === false)
120+
{
121+
$url = '/' . $url;
122+
}
123+
124+
return sprintf(
125+
' %s%s%s%surl=%s%',
126+
$attribute,
127+
$quoteChar,
128+
$_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') ? '&' : '?',
129+
urlencode($url),
130+
$quoteChar
131+
);
132+
},
133+
$response
134+
);
135+
}
136+
echo $response;
137+
}
138+
else
139+
{
140+
header('HTTP/1.1 500');
141+
echo 'PHP-Curl error.';
142+
exit;
143+
}
144+
}
145+
catch(\Exception $e)
146+
{
147+
header('HTTP/1.1 503 Service Temporarily Unavailable');
148+
header('Status: 503 Service Temporarily Unavailable');
149+
header('Retry-After: 300');
150+
echo 'Service Temporarely unavailable!';
151+
exit;
152+
}

0 commit comments

Comments
 (0)