Advertisement
shinobininja

ninjutsu proxy 2

Jun 26th, 2016
1,594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. <?PHP
  2.  
  3. error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
  4. @ini_set('html_errors','0');
  5. @ini_set('display_errors','0');
  6. @ini_set('display_startup_errors','0');
  7. @ini_set('log_errors','0');
  8. @set_time_limit(0);
  9. @clearstatcache();
  10. // Change these configuration options if needed, see above descriptions for info.
  11. $enable_jsonp = false;
  12. $enable_native = true;
  13. $valid_url_regex = '/.*/';
  14.  
  15. // ############################################################################
  16.  
  17. $url = $_GET['url'];
  18.  
  19. if ( !$url ) {
  20.  
  21. // Passed url not specified.
  22. $contents = 'ERROR: url not specified';
  23. $status = array( 'http_code' => 'ERROR' );
  24.  
  25. } else if ( !preg_match( $valid_url_regex, $url ) ) {
  26.  
  27. // Passed url doesn't match $valid_url_regex.
  28. $contents = 'ERROR: invalid url';
  29. $status = array( 'http_code' => 'ERROR' );
  30.  
  31. } else {
  32. $ch = curl_init( $url );
  33.  
  34. if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) {
  35. curl_setopt( $ch, CURLOPT_POST, true );
  36. curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST );
  37. }
  38.  
  39. if ( $_GET['send_cookies'] ) {
  40. $cookie = array();
  41. foreach ( $_COOKIE as $key => $value ) {
  42. $cookie[] = $key . '=' . $value;
  43. }
  44. if ( $_GET['send_session'] ) {
  45. $cookie[] = SID;
  46. }
  47. $cookie = implode( '; ', $cookie );
  48.  
  49. curl_setopt( $ch, CURLOPT_COOKIE, $cookie );
  50. }
  51.  
  52. curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  53. curl_setopt( $ch, CURLOPT_HEADER, true );
  54. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  55.  
  56. curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] );
  57.  
  58. list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 );
  59.  
  60. $status = curl_getinfo( $ch );
  61.  
  62. curl_close( $ch );
  63. }
  64.  
  65. // Split header text into an array.
  66. $header_text = preg_split( '/[\r\n]+/', $header );
  67.  
  68. if ( $_GET['mode'] == 'native' ) {
  69. if ( !$enable_native ) {
  70. $contents = 'ERROR: invalid mode';
  71. $status = array( 'http_code' => 'ERROR' );
  72. }
  73.  
  74. // Propagate headers to response.
  75. foreach ( $header_text as $header ) {
  76. if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) {
  77. header( $header );
  78. }
  79. }
  80.  
  81. print $contents;
  82.  
  83. } else {
  84.  
  85. // $data will be serialized into JSON data.
  86. $data = array();
  87.  
  88. // Propagate all HTTP headers into the JSON data object.
  89. if ( $_GET['full_headers'] ) {
  90. $data['headers'] = array();
  91.  
  92. foreach ( $header_text as $header ) {
  93. preg_match( '/^(.+?):\s+(.*)$/', $header, $matches );
  94. if ( $matches ) {
  95. $data['headers'][ $matches[1] ] = $matches[2];
  96. }
  97. }
  98. }
  99.  
  100. // Propagate all cURL request / response info to the JSON data object.
  101. if ( $_GET['full_status'] ) {
  102. $data['status'] = $status;
  103. } else {
  104. $data['status'] = array();
  105. $data['status']['http_code'] = $status['http_code'];
  106. }
  107.  
  108. // Set the JSON data object contents, decoding it from JSON if possible.
  109. $decoded_json = json_decode( $contents );
  110. $data['contents'] = $decoded_json ? $decoded_json : $contents;
  111.  
  112. // Generate appropriate content-type header.
  113. $is_xhr = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
  114. header( 'Content-type: application/' . ( $is_xhr ? 'json' : 'x-javascript' ) );
  115.  
  116. // Get JSONP callback.
  117. $jsonp_callback = $enable_jsonp && isset($_GET['callback']) ? $_GET['callback'] : null;
  118.  
  119. // Generate JSON/JSONP string
  120. $json = json_encode( $data );
  121.  
  122. print $jsonp_callback ? "$jsonp_callback($json)" : $json;
  123.  
  124. }
  125.  
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement