-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAjaxProxy.php
92 lines (80 loc) · 2.45 KB
/
AjaxProxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* @file ajaxproxy.php
* @brief ajaxproxy class to circumvent same origin policy for cross domain ajax calls
*
* @author Frank Hellenkamp <[email protected]>
**/
namespace Depage\Http;
require_once(__DIR__ . "/Request.php");
require_once(__DIR__ . "/Response.php");
class AjaxProxy {
// {{{ variables()
protected $headerBlacklist = array(
'Accept-Encoding',
'Connection',
'Content-Length',
'Content-Type',
'Transfer-Encoding',
'Host',
);
// }}}
// {{{ constructor()
public function __construct($destinationURL) {
$this->destinationURL = $destinationURL;
}
// }}}
// {{{ getHeaders()
public function getHeaders($asKeyVal = true) {
// Function is from: http://www.electrictoolbox.com/php-get-headers-sent-from-browser/
$headers = array();
foreach($_SERVER as $key => $value) {
if(substr($key, 0, 5) == 'HTTP_') {
$name = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
if ($asKeyVal) {
$headers[$name] = $value;
} else {
$headers[] = "$name: $value";
}
}
}
return $headers;
}
// }}}
// {{{ filterHeaders()
public function filterHeaders($headers) {
$filtered = array();
foreach($headers as $header) {
$add = true;
foreach ($this->headerBlacklist as $a) {
if (stripos($header, $a) === 0) {
$add = false;
}
}
if ($add) {
$filtered[] = $header;
}
}
return $filtered;
}
// }}}
// {{{ forwardRequest()
public function forwardRequest() {
$headers = $this->getHeaders(false);
$headers[] = 'X-Forwarded-For: ' . Request::getRequestIp();
$headers = $this->filterHeaders($headers);
$request = new Request($this->destinationURL);
$request->setPostData($_POST);
$request->setHeaders($headers);
$response = $request->execute();
// send headers
$headers = $this->filterHeaders($response->headers);
foreach($headers as $header) {
header($header, stripos($header, "Set-Cookie") !== 0);
}
// send body
echo($response);
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */