Skip to content

Commit cfe9f83

Browse files
committed
proxy copied from iacons.net
1 parent 6f37dc1 commit cfe9f83

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

proxy.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
/**
4+
* AJAX Cross Domain (PHP) Proxy 0.7
5+
* by Iacovos Constantinou (http://www.iacons.net)
6+
*
7+
* Released under CC-GNU GPL
8+
*/
9+
10+
/**
11+
* Enables or disables filtering for cross domain requests.
12+
* Recommended value: true
13+
*/
14+
define( 'CSAJAX_FILTERS', true );
15+
16+
/**
17+
* If set to true, $valid_requests should hold only domains i.e. a.example.com, b.example.com, usethisdomain.com
18+
* If set to false, $valid_requests should hold the whole URL ( without the parameters ) i.e. http://example.com/this/is/long/url/
19+
* Recommended value: false (for security reasons - do not forget that anyone can access your proxy)
20+
*/
21+
define( 'CSAJAX_FILTER_DOMAIN', false );
22+
23+
/**
24+
* Set debugging to true to receive additional messages - really helpful on development
25+
*/
26+
define( 'CSAJAX_DEBUG', false );
27+
28+
/**
29+
* A set of valid cross domain requests
30+
*/
31+
$valid_requests = array(
32+
// 'example.com'
33+
);
34+
35+
/* * * STOP EDITING HERE UNLESS YOU KNOW WHAT YOU ARE DOING * * */
36+
37+
// identify request headers
38+
$request_headers = array( );
39+
foreach ( $_SERVER as $key => $value ) {
40+
if ( substr( $key, 0, 5 ) == 'HTTP_' ) {
41+
$headername = str_replace( '_', ' ', substr( $key, 5 ) );
42+
$headername = str_replace( ' ', '-', ucwords( strtolower( $headername ) ) );
43+
if ( 'Host' != $headername ) {
44+
$request_headers[] = "$headername: $value";
45+
}
46+
}
47+
}
48+
49+
// identify request method, url and params
50+
$request_method = $_SERVER['REQUEST_METHOD'];
51+
$request_params = ( $request_method == 'GET' ) ? $_GET : $_POST;
52+
$request_url = urldecode( $_REQUEST['csurl'] );
53+
$p_request_url = parse_url( $request_url );
54+
unset( $request_params['csurl'] );
55+
56+
// ignore requests for proxy :)
57+
if ( preg_match( '!' . $_SERVER['SCRIPT_NAME'] . '!', $request_url ) || empty( $request_url ) || count( $p_request_url ) == 1 ) {
58+
csajax_debug_message( 'Invalid request - make sure that csurl variable is not empty' );
59+
exit;
60+
}
61+
62+
// check against valid requests
63+
if ( CSAJAX_FILTERS ) {
64+
$parsed = $p_request_url;
65+
if ( CSAJAX_FILTER_DOMAIN ) {
66+
if ( !in_array( $parsed['host'], $valid_requests ) ) {
67+
csajax_debug_message( 'Invalid domain - ' . $parsed['host'] . ' does not included in valid requests' );
68+
exit;
69+
}
70+
} else {
71+
$check_url = isset( $parsed['scheme'] ) ? $parsed['scheme'] . '://' : '';
72+
$check_url .= isset( $parsed['user'] ) ? $parsed['user'] . ($parsed['pass'] ? ':' . $parsed['pass'] : '') . '@' : '';
73+
$check_url .= isset( $parsed['host'] ) ? $parsed['host'] : '';
74+
$check_url .= isset( $parsed['port'] ) ? ':' . $parsed['port'] : '';
75+
$check_url .= isset( $parsed['path'] ) ? $parsed['path'] : '';
76+
if ( !in_array( $check_url, $valid_requests ) ) {
77+
csajax_debug_message( 'Invalid domain - ' . $request_url . ' does not included in valid requests' );
78+
exit;
79+
}
80+
}
81+
}
82+
83+
// append query string for GET requests
84+
if ( $request_method == 'GET' && count( $request_params ) > 0 && (!array_key_exists( 'query', $p_request_url ) || empty( $p_request_url['query'] ) ) ) {
85+
$request_url .= '?' . http_build_query( $request_params );
86+
}
87+
88+
// let the request begin
89+
$ch = curl_init( $request_url );
90+
curl_setopt( $ch, CURLOPT_HTTPHEADER, $request_headers ); // (re-)send headers
91+
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // return response
92+
curl_setopt( $ch, CURLOPT_HEADER, true ); // enabled response headers
93+
// add post data for POST requests
94+
if ( $request_method == 'POST' ) {
95+
curl_setopt( $ch, CURLOPT_POST, true );
96+
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $request_params ) );
97+
}
98+
99+
// retrieve response (headers and content)
100+
$response = curl_exec( $ch );
101+
curl_close( $ch );
102+
103+
// split response to header and content
104+
list($response_headers, $response_content) = preg_split( '/(\r\n){2}/', $response, 2 );
105+
106+
// (re-)send the headers
107+
$response_headers = preg_split( '/(\r\n){1}/', $response_headers );
108+
foreach ( $response_headers as $key => $response_header )
109+
if ( !preg_match( '/^(Transfer-Encoding):/', $response_header ) )
110+
header( $response_header );
111+
112+
// finally, output the content
113+
print($response_content );
114+
115+
function csajax_debug_message( $message )
116+
{
117+
if ( true == CSAJAX_DEBUG ) {
118+
print $message . PHP_EOL;
119+
}
120+
}

0 commit comments

Comments
 (0)