Advertisement
Guest User

Maniquí

a guest
Jul 8th, 2010
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.85 KB | None | 0 0
  1. <?php
  2.  
  3. class ProxyHandler
  4. {
  5.     private $url;
  6.     private $proxy_url;
  7.     private $proxy_host;
  8.     private $proxy_proto;
  9.     private $translated_url;
  10.     private $curl_handler;
  11.     private $cache_control=false;
  12.     private $pragma=false;
  13.     private $client_headers=array();
  14.  
  15.     function __construct($url, $proxy_url) {
  16.         // Strip the trailing '/' from the URLs so they are the same.
  17.         $this->url = rtrim($url,'/');
  18.         $this->proxy_url =  rtrim($proxy_url,'/');
  19.  
  20.         // Parse all the parameters for the URL
  21.         if (isset($_SERVER['PATH_INFO'])) {
  22.             $proxy_url .= $_SERVER['PATH_INFO'];
  23.         }
  24.         else {
  25.             // Add the '/' at the end
  26.             $proxy_url .= '/';
  27.         }
  28.  
  29.         if ($_SERVER['QUERY_STRING'] !== '') {
  30.             $proxy_url .= "?{$_SERVER['QUERY_STRING']}";
  31.         }
  32.  
  33.         $this->translated_url = $proxy_url;
  34.  
  35.         $this->curl_handler = curl_init($this->translated_url);
  36.  
  37.         // Set various options
  38.         $this->setCurlOption(CURLOPT_RETURNTRANSFER, true);
  39.         $this->setCurlOption(CURLOPT_BINARYTRANSFER, true); // For images, etc.
  40.         $this->setCurlOption(CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
  41.         $this->setCurlOption(CURLOPT_WRITEFUNCTION, array($this,'readResponse'));
  42.         $this->setCurlOption(CURLOPT_HEADERFUNCTION, array($this,'readHeaders'));
  43.  
  44.         // Process post data.
  45.         if (count($_POST)) {
  46.             // Empty the post data
  47.             $post=array();
  48.  
  49.             // Set the post data
  50.             $this->setCurlOption(CURLOPT_POST, true);
  51.  
  52.             // Encode and form the post data
  53.             foreach($_POST as $key=>$value) {
  54.                 $post[] = urlencode($key)."=".urlencode($value);
  55.             }
  56.  
  57.             $this->setCurlOption(CURLOPT_POSTFIELDS, implode('&',$post));
  58.  
  59.             unset($post);
  60.         }
  61.         elseif ($_SERVER['REQUEST_METHOD'] !== 'GET') { // Default request method is 'get' {
  62.             // Set the request method
  63.             $this->setCurlOption(CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
  64.         }
  65.  
  66.         // Handle the client headers.
  67.         $this->handleClientHeaders();
  68.  
  69.     }
  70.  
  71.     public function setClientHeader($header) {
  72.         $this->client_headers[] = $header;
  73.     }
  74.  
  75.     // Executes the proxy.
  76.     public function execute() {
  77.         $this->setCurlOption(CURLOPT_HTTPHEADER, $this->client_headers);
  78.         curl_exec($this->curl_handler);
  79.     }
  80.  
  81.     // Get the information about the request.
  82.     // Should not be called before exec.
  83.     public function getCurlInfo() {
  84.         return curl_getinfo($this->curl_handler);
  85.     }
  86.  
  87.     // Sets a curl option.
  88.     public function setCurlOption($option, $value) {
  89.         curl_setopt($this->curl_handler, $option, $value);
  90.     }
  91.  
  92.     protected function readHeaders(&$cu, $string) {
  93.         $length = strlen($string);
  94.         if (preg_match(',^Location:,', $string)) {
  95.             $string = str_replace($this->proxy_url, $this->url, $string);
  96.         }
  97.         elseif(preg_match(',^Cache-Control:,', $string)) {
  98.             $this->cache_control = true;
  99.         }
  100.         elseif(preg_match(',^Pragma:,', $string)) {
  101.             $this->pragma = true;
  102.         }
  103.         if ($string !== "\r\n") {
  104.             header(rtrim($string));
  105.  
  106.         }
  107.         return $length;
  108.     }
  109.  
  110.     protected function handleClientHeaders() {
  111.  
  112.         //Patching for when PHP does't run as an Apache module
  113.         if(!function_exists('apache_request_headers')) {
  114.             function apache_request_headers() {
  115.                 $headers = array();
  116.                 foreach($_SERVER as $key => $value) {
  117.                     if(substr($key, 0, 5) == 'HTTP_') {
  118.                         $headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
  119.                     }
  120.                 }
  121.                 return $headers;
  122.             }
  123.         }
  124.  
  125.         $headers = apache_request_headers();
  126.  
  127.         foreach ($headers as $header => $value) {
  128.             switch($header) {
  129.                 case 'Host':
  130.                     break;
  131.                 default:
  132.                     $this->setClientHeader(sprintf('%s: %s', $header, $value));
  133.                     break;
  134.             }
  135.         }
  136.     }
  137.  
  138.     protected function readResponse(&$cu, $string) {
  139.         static $headersParsed = false;
  140.  
  141.         // Clear the Cache-Control and Pragma headers
  142.         // if they aren't passed from the proxy application.
  143.         if ($headersParsed === false) {
  144.             if (!$this->cache_control) {
  145.                 header('Cache-Control: ');
  146.             }
  147.             if (!$this->pragma) {
  148.                 header('Pragma: ');
  149.             }
  150.             $headersParsed = true;
  151.         }
  152.         $length = strlen($string);
  153.         echo $string;
  154.         return $length;
  155.     }
  156. }
  157.  
  158. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement