|
| 1 | +<?PHP |
| 2 | + |
| 3 | +// Script: Simple PHP Proxy: Get external HTML, JSON and more! |
| 4 | +// |
| 5 | +// *Version: 1.6, Last updated: 1/24/2009* |
| 6 | +// |
| 7 | +// Project Home - http://benalman.com/projects/php-simple-proxy/ |
| 8 | +// GitHub - http://github.com/cowboy/php-simple-proxy/ |
| 9 | +// Source - http://github.com/cowboy/php-simple-proxy/raw/master/ba-simple-proxy.php |
| 10 | +// |
| 11 | +// About: License |
| 12 | +// |
| 13 | +// Copyright (c) 2010 "Cowboy" Ben Alman, |
| 14 | +// Dual licensed under the MIT and GPL licenses. |
| 15 | +// http://benalman.com/about/license/ |
| 16 | +// |
| 17 | +// About: Examples |
| 18 | +// |
| 19 | +// This working example, complete with fully commented code, illustrates one way |
| 20 | +// in which this PHP script can be used. |
| 21 | +// |
| 22 | +// Simple - http://benalman.com/code/projects/php-simple-proxy/examples/simple/ |
| 23 | +// |
| 24 | +// About: Release History |
| 25 | +// |
| 26 | +// 1.6 - (1/24/2009) Now defaults to JSON mode, which can now be changed to |
| 27 | +// native mode by specifying ?mode=native. Native and JSONP modes are |
| 28 | +// disabled by default because of possible XSS vulnerability issues, but |
| 29 | +// are configurable in the PHP script along with a url validation regex. |
| 30 | +// 1.5 - (12/27/2009) Initial release |
| 31 | +// |
| 32 | +// Topic: GET Parameters |
| 33 | +// |
| 34 | +// Certain GET (query string) parameters may be passed into ba-simple-proxy.php |
| 35 | +// to control its behavior, this is a list of these parameters. |
| 36 | +// |
| 37 | +// url - The remote URL resource to fetch. Any GET parameters to be passed |
| 38 | +// through to the remote URL resource must be urlencoded in this parameter. |
| 39 | +// mode - If mode=native, the response will be sent using the same content |
| 40 | +// type and headers that the remote URL resource returned. If omitted, the |
| 41 | +// response will be JSON (or JSONP). <Native requests> and <JSONP requests> |
| 42 | +// are disabled by default, see <Configuration Options> for more information. |
| 43 | +// callback - If specified, the response JSON will be wrapped in this named |
| 44 | +// function call. This parameter and <JSONP requests> are disabled by |
| 45 | +// default, see <Configuration Options> for more information. |
| 46 | +// user_agent - This value will be sent to the remote URL request as the |
| 47 | +// `User-Agent:` HTTP request header. If omitted, the browser user agent |
| 48 | +// will be passed through. |
| 49 | +// send_cookies - If send_cookies=1, all cookies will be forwarded through to |
| 50 | +// the remote URL request. |
| 51 | +// send_session - If send_session=1 and send_cookies=1, the SID cookie will be |
| 52 | +// forwarded through to the remote URL request. |
| 53 | +// full_headers - If a JSON request and full_headers=1, the JSON response will |
| 54 | +// contain detailed header information. |
| 55 | +// full_status - If a JSON request and full_status=1, the JSON response will |
| 56 | +// contain detailed cURL status information, otherwise it will just contain |
| 57 | +// the `http_code` property. |
| 58 | +// |
| 59 | +// Topic: POST Parameters |
| 60 | +// |
| 61 | +// All POST parameters are automatically passed through to the remote URL |
| 62 | +// request. |
| 63 | +// |
| 64 | +// Topic: JSON requests |
| 65 | +// |
| 66 | +// This request will return the contents of the specified url in JSON format. |
| 67 | +// |
| 68 | +// Request: |
| 69 | +// |
| 70 | +// > ba-simple-proxy.php?url=http://example.com/ |
| 71 | +// |
| 72 | +// Response: |
| 73 | +// |
| 74 | +// > { "contents": "<html>...</html>", "headers": {...}, "status": {...} } |
| 75 | +// |
| 76 | +// JSON object properties: |
| 77 | +// |
| 78 | +// contents - (String) The contents of the remote URL resource. |
| 79 | +// headers - (Object) A hash of HTTP headers returned by the remote URL |
| 80 | +// resource. |
| 81 | +// status - (Object) A hash of status codes returned by cURL. |
| 82 | +// |
| 83 | +// Topic: JSONP requests |
| 84 | +// |
| 85 | +// This request will return the contents of the specified url in JSONP format |
| 86 | +// (but only if $enable_jsonp is enabled in the PHP script). |
| 87 | +// |
| 88 | +// Request: |
| 89 | +// |
| 90 | +// > ba-simple-proxy.php?url=http://example.com/&callback=foo |
| 91 | +// |
| 92 | +// Response: |
| 93 | +// |
| 94 | +// > foo({ "contents": "<html>...</html>", "headers": {...}, "status": {...} }) |
| 95 | +// |
| 96 | +// JSON object properties: |
| 97 | +// |
| 98 | +// contents - (String) The contents of the remote URL resource. |
| 99 | +// headers - (Object) A hash of HTTP headers returned by the remote URL |
| 100 | +// resource. |
| 101 | +// status - (Object) A hash of status codes returned by cURL. |
| 102 | +// |
| 103 | +// Topic: Native requests |
| 104 | +// |
| 105 | +// This request will return the contents of the specified url in the format it |
| 106 | +// was received in, including the same content-type and other headers (but only |
| 107 | +// if $enable_native is enabled in the PHP script). |
| 108 | +// |
| 109 | +// Request: |
| 110 | +// |
| 111 | +// > ba-simple-proxy.php?url=http://example.com/&mode=native |
| 112 | +// |
| 113 | +// Response: |
| 114 | +// |
| 115 | +// > <html>...</html> |
| 116 | +// |
| 117 | +// Topic: Notes |
| 118 | +// |
| 119 | +// * Assumes magic_quotes_gpc = Off in php.ini |
| 120 | +// |
| 121 | +// Topic: Configuration Options |
| 122 | +// |
| 123 | +// These variables can be manually edited in the PHP file if necessary. |
| 124 | +// |
| 125 | +// $enable_jsonp - Only enable <JSONP requests> if you really need to. If you |
| 126 | +// install this script on the same server as the page you're calling it |
| 127 | +// from, plain JSON will work. Defaults to false. |
| 128 | +// $enable_native - You can enable <Native requests>, but you should only do |
| 129 | +// this if you also whitelist specific URLs using $valid_url_regex, to avoid |
| 130 | +// possible XSS vulnerabilities. Defaults to false. |
| 131 | +// $valid_url_regex - This regex is matched against the url parameter to |
| 132 | +// ensure that it is valid. This setting only needs to be used if either |
| 133 | +// $enable_jsonp or $enable_native are enabled. Defaults to '/.*/' which |
| 134 | +// validates all URLs. |
| 135 | +// |
| 136 | +// ############################################################################ |
| 137 | + |
| 138 | +// Change these configuration options if needed, see above descriptions for info. |
| 139 | +$enable_jsonp = false; |
| 140 | +$enable_native = false; |
| 141 | +$valid_url_regex = '/.*/'; |
| 142 | + |
| 143 | +// ############################################################################ |
| 144 | + |
| 145 | +$url = $_GET['url']; |
| 146 | + |
| 147 | +if ( !$url ) { |
| 148 | + |
| 149 | + // Passed url not specified. |
| 150 | + $contents = 'ERROR: url not specified'; |
| 151 | + $status = array( 'http_code' => 'ERROR' ); |
| 152 | + |
| 153 | +} else if ( !preg_match( $valid_url_regex, $url ) ) { |
| 154 | + |
| 155 | + // Passed url doesn't match $valid_url_regex. |
| 156 | + $contents = 'ERROR: invalid url'; |
| 157 | + $status = array( 'http_code' => 'ERROR' ); |
| 158 | + |
| 159 | +} else { |
| 160 | + $ch = curl_init( $url ); |
| 161 | + |
| 162 | + if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) { |
| 163 | + curl_setopt( $ch, CURLOPT_POST, true ); |
| 164 | + curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST ); |
| 165 | + } |
| 166 | + |
| 167 | + if ( $_GET['send_cookies'] ) { |
| 168 | + $cookie = array(); |
| 169 | + foreach ( $_COOKIE as $key => $value ) { |
| 170 | + $cookie[] = $key . '=' . $value; |
| 171 | + } |
| 172 | + if ( $_GET['send_session'] ) { |
| 173 | + $cookie[] = SID; |
| 174 | + } |
| 175 | + $cookie = implode( '; ', $cookie ); |
| 176 | + |
| 177 | + curl_setopt( $ch, CURLOPT_COOKIE, $cookie ); |
| 178 | + } |
| 179 | + |
| 180 | + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); |
| 181 | + curl_setopt( $ch, CURLOPT_HEADER, true ); |
| 182 | + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
| 183 | + |
| 184 | + curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] ); |
| 185 | + |
| 186 | + list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 ); |
| 187 | + |
| 188 | + |
| 189 | + $status = curl_getinfo( $ch ); |
| 190 | + |
| 191 | + curl_close( $ch ); |
| 192 | +} |
| 193 | + |
| 194 | +// Split header text into an array. |
| 195 | +$header_text = preg_split( '/[\r\n]+/', $header ); |
| 196 | + |
| 197 | +if ( $_GET['mode'] == 'native' ) { |
| 198 | + if ( !$enable_native ) { |
| 199 | + $contents = 'ERROR: invalid mode'; |
| 200 | + $status = array( 'http_code' => 'ERROR' ); |
| 201 | + } |
| 202 | + |
| 203 | + // Propagate headers to response. |
| 204 | + foreach ( $header_text as $header ) { |
| 205 | + if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) { |
| 206 | + header( $header ); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + print $contents; |
| 211 | + |
| 212 | +} else { |
| 213 | + |
| 214 | + // $data will be serialized into JSON data. |
| 215 | + $data = array(); |
| 216 | + |
| 217 | + // Propagate all HTTP headers into the JSON data object. |
| 218 | + if ( $_GET['full_headers'] ) { |
| 219 | + $data['headers'] = array(); |
| 220 | + |
| 221 | + foreach ( $header_text as $header ) { |
| 222 | + preg_match( '/^(.+?):\s+(.*)$/', $header, $matches ); |
| 223 | + if ( $matches ) { |
| 224 | + $data['headers'][ $matches[1] ] = $matches[2]; |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + // Propagate all cURL request / response info to the JSON data object. |
| 230 | + if ( $_GET['full_status'] ) { |
| 231 | + $data['status'] = $status; |
| 232 | + } else { |
| 233 | + $data['status'] = array(); |
| 234 | + $data['status']['http_code'] = $status['http_code']; |
| 235 | + } |
| 236 | + |
| 237 | + // Set the JSON data object contents, decoding it from JSON if possible. |
| 238 | + $decoded_json = json_decode( $contents ); |
| 239 | + $data['contents'] = $decoded_json ? $decoded_json : $contents; |
| 240 | + |
| 241 | + // Generate appropriate content-type header. |
| 242 | + $is_xhr = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; |
| 243 | + header( 'Content-type: application/' . ( $is_xhr ? 'json' : 'x-javascript' ) ); |
| 244 | + |
| 245 | + // Get JSONP callback. |
| 246 | + $jsonp_callback = $enable_jsonp && isset($_GET['callback']) ? $_GET['callback'] : null; |
| 247 | + |
| 248 | + // Generate JSON/JSONP string |
| 249 | + $json = json_encode( $data ); |
| 250 | + |
| 251 | + print $jsonp_callback ? "$jsonp_callback($json)" : $json; |
| 252 | + |
| 253 | +} |
| 254 | + |
| 255 | +?> |
0 commit comments