-
-
Save evelaguti/1f3d86c38f3b93ec7b093f898463ec6f to your computer and use it in GitHub Desktop.
simple CORS proxy in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// able the file to accept requests from * | |
header('Access-Control-Allow-Origin: *'); | |
error_reporting( error_reporting() & ~E_NOTICE ); // evil | |
// config | |
$enable_jsonp = false; | |
$enable_native = false; | |
$valid_url_regex = '/.*/'; | |
// ############################################################################ | |
$url = $_GET['url']; | |
if ( !$url ) { | |
// Passed url not specified. | |
$contents = 'ERROR: url not specified'; | |
$status = array( 'http_code' => 'ERROR' ); | |
} else if ( !preg_match( $valid_url_regex, $url ) ) { | |
// Passed url doesn't match $valid_url_regex. | |
$contents = 'ERROR: invalid url'; | |
$status = array( 'http_code' => 'ERROR' ); | |
} else { | |
$ch = curl_init( $url ); | |
// @lf get domain from url and keep it around | |
$parts = parse_url( $url ); | |
$domain = $parts['scheme']."://".$parts['host']; | |
if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) { | |
curl_setopt( $ch, CURLOPT_POST, true ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST ); | |
} | |
if ( $_GET['send_cookies'] ) { | |
$cookie = array(); | |
foreach ( $_COOKIE as $key => $value ) { | |
$cookie[] = $key . '=' . $value; | |
} | |
if ( $_GET['send_session'] ) { | |
$cookie[] = SID; | |
} | |
$cookie = implode( '; ', $cookie ); | |
curl_setopt( $ch, CURLOPT_COOKIE, $cookie ); | |
} | |
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); | |
curl_setopt( $ch, CURLOPT_HEADER, true ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_ENCODING ,""); // @lf guess encoding automagically | |
curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] ); | |
list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 ); | |
// @lf filter any relative urls and replace them with absolute urls | |
$rep['/href="(?!https?:\/\/)(?!data:)(?!#)/'] = 'href="'.$domain; | |
$rep['/src="(?!https?:\/\/)(?!data:)(?!#)/'] = 'src="'.$domain; | |
$rep['/href=\'(?!https?:\/\/)(?!data:)(?!#)/'] = 'href="'.$domain; | |
$rep['/src=\'(?!https?:\/\/)(?!data:)(?!#)/'] = 'src="'.$domain; | |
$rep['/@import[\n+\s+]"\//'] = '@import "'.$domain; | |
$rep['/@import[\n+\s+]"\./'] = '@import "'.$domain; | |
// @lf warning: clears previous contents | |
$contents = preg_replace(array_keys($rep), array_values($rep), $contents); | |
$status = curl_getinfo( $ch ); | |
curl_close( $ch ); | |
echo $contents; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment