|
<?php
|
|
/*
|
|
* Initially based on php5rp <http://code.google.com/p/php5rp/>, originally
|
|
* published under the (new) BSD license,
|
|
* http://www.opensource.org/licenses/bsd-license.php
|
|
*/
|
|
|
|
class ProxyHandler
|
|
{
|
|
private $url;
|
|
private $proxy_url;
|
|
private $remote_base_url;
|
|
private $translated_url;
|
|
private $curl_handler;
|
|
private $client_headers=array();
|
|
|
|
public $result;
|
|
public $http_code;
|
|
|
|
function __construct($local_base_url, $remote_base_url, $proxied_url) {
|
|
$this->result = '';
|
|
$this->location = '';
|
|
|
|
$this->url = $local_base_url;
|
|
$this->remote_base_url = $remote_base_url;
|
|
if ($this->remote_base_url[strlen($this->remote_base_url)-1] != '/') {
|
|
$this->remote_base_url .= '/';
|
|
}
|
|
$this->proxy_url = $remote_base_url . $proxied_url;
|
|
|
|
// extract domainnames (for cookie-proxy)
|
|
$local_host = parse_url($local_base_url, PHP_URL_HOST);
|
|
list($local_hostname,$local_domainname) = explode('.', $local_host, 2);
|
|
$remote_host = parse_url($remote_base_url, PHP_URL_HOST);
|
|
list($remote_hostname,$remote_domainname) = explode('.', $remote_host, 2);
|
|
$this->local_domainname=$local_domainname;
|
|
$this->remote_domainname=$remote_domainname;
|
|
|
|
$this->translated_url = $this->proxy_url;
|
|
|
|
$this->curl_handler = curl_init($this->translated_url);
|
|
|
|
// Set various options
|
|
$this->setCurlOption(CURLOPT_RETURNTRANSFER, true);
|
|
$this->setCurlOption(CURLOPT_VERBOSE, false);
|
|
$this->setCurlOption(CURLOPT_BINARYTRANSFER, true); // For images, etc.
|
|
$this->setCurlOption(CURLOPT_ENCODING, 'identity');
|
|
$this->setCurlOption(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
|
|
$this->setCurlOption(CURLOPT_WRITEFUNCTION, array($this,'readResponse'));
|
|
$this->setCurlOption(CURLOPT_HEADERFUNCTION, array($this,'readHeaders'));
|
|
|
|
// Process post data.
|
|
if (count($_POST)) {
|
|
// Empty the post data
|
|
$post=array();
|
|
|
|
// Set the post data
|
|
$this->setCurlOption(CURLOPT_POST, true);
|
|
|
|
// Encode and form the post data
|
|
foreach($_POST as $key=>$value) {
|
|
$post[$key] = $value;
|
|
}
|
|
|
|
$this->setCurlOption(CURLOPT_POSTFIELDS, $post);
|
|
|
|
unset($post);
|
|
}
|
|
elseif ($_SERVER['REQUEST_METHOD'] !== 'GET') // Default request method is 'get'
|
|
{
|
|
// Set the request method
|
|
$this->setCurlOption(CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
|
|
}
|
|
|
|
// Handle the client headers.
|
|
$this->handleClientHeaders();
|
|
|
|
}
|
|
|
|
public function setClientHeader($header) {
|
|
$this->client_headers[] = $header;
|
|
}
|
|
|
|
// Executes the proxy.
|
|
public function execute() {
|
|
$this->setCurlOption(CURLOPT_HTTPHEADER, $this->client_headers);
|
|
curl_exec($this->curl_handler);
|
|
}
|
|
|
|
public function close() {
|
|
$info = curl_getinfo($this->curl_handler);
|
|
$this->http_code = $info['http_code'];
|
|
if ($this->http_code == 301 || $this->http_code == 302 || $this->http_code == 303) {
|
|
$this->result = 'redirect to ' . $this->location;
|
|
}
|
|
curl_close($this->curl_handler);
|
|
}
|
|
|
|
public function getCurlInfo() {
|
|
return curl_getinfo($this->curl_handler);
|
|
}
|
|
|
|
public function setCurlOption($option, $value) {
|
|
curl_setopt($this->curl_handler, $option, $value);
|
|
}
|
|
|
|
protected function readHeaders(&$cu, $string) {
|
|
$length = strlen($string);
|
|
if (preg_match(',^Location:,', $string)) {
|
|
$string = str_replace($this->remote_base_url, $this->url, $string);
|
|
if (! preg_match(',http,', $string)) {
|
|
$string = str_replace('//', '/', $string);
|
|
}
|
|
$this->location = $string;
|
|
return $length;
|
|
}
|
|
if (preg_match(',^Set-Cookie:,', $string)) {
|
|
// cookie-proxy
|
|
$string = str_replace('domain=' . $this->remote_domainname, 'domain=' . $this->local_domainname, $string);
|
|
header(rtrim($string));
|
|
}
|
|
return $length;
|
|
}
|
|
|
|
protected function handleClientHeaders() {
|
|
$headers = apache_request_headers();
|
|
|
|
foreach ($headers as $header => $value) {
|
|
switch($header) {
|
|
case 'Host':
|
|
break;
|
|
case 'Content-Length':
|
|
break;
|
|
default:
|
|
$this->setClientHeader(sprintf('%s: %s', $header, $value));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function readResponse(&$cu, $string) {
|
|
$length = strlen($string);
|
|
$this->result .= $string;
|
|
return $length;
|
|
}
|
|
}
|
|
|
|
?>
|