Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ( isset( $_GET[ 'url' ] ) ) {
- /*
- ** Function converting a key-index array to a string
- */
- function arrayToString( $array, $format = '%s => %s', $delimiter = "\r\n" ) {
- $pieces = Array( );
- foreach ( $array as $key => $value ) {
- $pieces[ ] = sprintf( $format, $key, $value );
- }
- return implode( $delimiter, $pieces );
- }
- /*
- ** Make sure the url is correct and complete it if needed
- */
- // Get the urls
- $request_url = $_GET[ 'url' ];
- $script_url = 'http' . ( isset( $_SERVER['HTTPS'] ) ? 's' : '' ) . '://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER['SCRIPT_NAME'];
- /*
- ** Take care of the information to send with the request
- */
- // The POST data
- $request_content = arrayToString( $_POST, '%s=%s', '&' );
- // The headers
- $request_headers = arrayToString( apache_request_headers( ), '%s: %s' );
- // The method
- $request_method = $_SERVER[ 'REQUEST_METHOD' ];
- // Set default context so that it is used by both get_headers( ) and file_get_contens( )
- $context = Array(
- 'content' => $request_content,
- 'follow_location' => false,
- 'header' => $request_headers,
- 'method' => $request_method
- );
- stream_context_get_default(
- Array(
- 'http' => $context
- )
- );
- /*
- ** Send headers
- */
- // Get the headers as a key-index array
- $response_headers = get_headers( $request_url, true );
- // For each header
- foreach( $response_headers as $key => $value ) {
- if ( $key == '0' ) {// if it is the first ligne
- // split it to see the different parts
- $tmp = explode( ' ', $value );
- $response_version = array_shift( $tmp );
- $response_code = array_shift( $tmp );
- $response_message = implode( ' ', $tmp );
- // And send it
- header( $value );
- } else {// if it is a normal header
- if ( $key == 'Location' ) {// if it is Location
- // add the script url in front of it so that we continue to use the proxy
- $value = $script_url . '?url=' . $value;
- }
- // send the header
- header( $key . ': ' . $value );
- }
- }
- // Add the header to allow XDR from domain
- header( 'Access-Control-Allow-Origin: *' );
- /*
- ** Send content
- */
- // if there was no error
- if ( $response_code == '200' ) {
- // output the content
- echo file_get_contents( $request_url );
- } else {
- print_r( $response_headers );
- }
- } else {
- echo 'Query string missing!';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement