Skip to content
This repository has been archived by the owner on Aug 30, 2018. It is now read-only.

the set of features and fixes #14

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions proxy.php
Expand Up @@ -12,6 +12,7 @@
$destinationURL = 'http://www.otherdomain.com/backend.php';

// The only domain from which requests are authorized.
// optional, empty string allows access from for any referer
$RequestDomain = 'example.com';

// That's it for configuration!
Expand Down Expand Up @@ -45,7 +46,7 @@ function apache_request_headers() {
$req_parts = parse_url($_SERVER['HTTP_REFERER']);

// IF domain name matches the authorized domain, proceed with request.
if($req_parts["host"] == $RequestDomain) {
if(strlen($RequestDomain) ? $RequestDomain == $req_parts["host"] : TRUE) {
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "GET") {
$data=$_GET;
Expand All @@ -55,6 +56,11 @@ function apache_request_headers() {
$data = $HTTP_RAW_POST_DATA;
}
$response = proxy_request($destinationURL, ($method == "GET" ? $_GET : $_POST), $method);

if($response['status'] != 'ok') {
http_response_code($response['code']);
echo 'error: '. $response['error'];
} else {
$headerArray = explode("\r\n", $response['header']);
$is_gzip = false;
$is_chunked = false;
Expand All @@ -80,9 +86,10 @@ function apache_request_headers() {
$contents = gzdecode($contents);
}
echo $contents;
} else {
}
} else {
echo $domainName." is not an authorized domain.";
}
}


function proxy_request($url, $data, $method) {
Expand Down Expand Up @@ -114,14 +121,14 @@ function proxy_request($url, $data, $method) {
}

// extract host and path:
$ishttp = $url['scheme'] == 'http';
$ishttps = $url['scheme'] == 'https';
$host = $url['host'];
$path = $url['path'];
$port = $url['port'] > 0 ? $url['port'] : ($ishttps ? 443 : ($ishttp ? 80 : ''));

$fp = fsockopen($host, $port, $errno, $errstr, 30);

if ($url['scheme'] == 'http') {
$fp = fsockopen($host, 80, $errno, $errstr, 30);
} elseif ($url['scheme'] == 'https') {
$fp = fsockopen($host, 443, $errno, $errstr, 30);
}

if ($fp){
// send the request headers:
Expand Down Expand Up @@ -149,12 +156,13 @@ function proxy_request($url, $data, $method) {
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
$result .= fgets($fp, 1024);
}
}
else {
return array(
'status' => 'err',
'code' => $errno,
'error' => "$errstr ($errno)"
);
}
Expand Down