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