stuppid_bot

Untitled

Jun 7th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.63 KB | None | 0 0
  1. // <?php
  2. function uncompress($content, $encoding) {
  3.     switch ($encoding) {
  4.         case 'gzip':
  5.             return gzdecode($content);
  6.            
  7.         case 'deflate':
  8.             return gzinflate($content);
  9.     }
  10.    
  11.     return $content;
  12. }
  13.  
  14. /**
  15.  * Отправляет http-запрос от имени сервера.
  16.  *
  17.  * @param string $method
  18.  * @param string $url
  19.  * @param array $options['headers'] дополнительные заголовки $name => $value
  20.  * @param array $options['allowed_statuses'] список разрешенных кодов состояния
  21.  * @param string $options['body'] тело запроса
  22.  * @param float $options['timeout'] тайм-аут соединения в секундах
  23.  * @return mixed
  24.  */
  25. function proxy_request( $method, $url, $options = array() ) {
  26.     $method = strtoupper($method);
  27.     $parts = @parse_url($url);
  28.    
  29.     if ($parts === false) {
  30.         die('url malformed');
  31.     }
  32.    
  33.     $scheme = isset($parts['scheme']) ? strtolower($parts['scheme']) : 'http';
  34.        
  35.     if ($scheme != 'http' && $scheme != 'https') {
  36.         die('only http and https are allowed');
  37.     }
  38.    
  39.     $protocol = $scheme == 'http' ? 'tcp' : 'ssl';      
  40.     $host = strtolower( isset($parts['host']) ? $parts['host'] : getenv('HTTP_HOST') );
  41.     $port = isset($parts['port']) ? $parts['port'] : ($protocol == 'tcp' ? 80 : 443);
  42.     $uri = (isset($parts['path']) ? $parts['path'] : '/') . (isset($parts['query']) ? '?' . $parts['query'] : '');  
  43.     $request = "$method $uri HTTP/1.1\r\n";
  44.     $request .= "Host: $host\r\n";
  45.     $headers = isset($options['headers']) ? $options['headers'] : array();
  46.    
  47.     foreach ($headers as $k => $v) {
  48.         $request .= "$k: $v\r\n";
  49.     }
  50.    
  51.     $request .= "Connection: close\r\n\r\n";
  52.    
  53.     if ( isset($options['body']) ) {
  54.         $request .= $options['body'];
  55.     }
  56.    
  57.     $timeout = isset($options['timeout']) ? (float) $options['timeout'] : ini_get('default_socket_timeout');
  58.     $fp = @fsockopen("$protocol://$host", $port, $errno, $errstr, $timeout);
  59.    
  60.     if (!$fp) {
  61.         die("could not connect: $errstr ($errno)");
  62.     }
  63.    
  64.     fwrite($fp, $request);  
  65.     $cur = fgets($fp);
  66.    
  67.     if ( !preg_match('#^HTTP/(1\.[01]) ([1-5]\d{2})(.*)$#', $cur, $matches) ) {
  68.         die('bad response');
  69.     }
  70.    
  71.     if ( isset($options['allowed_statuses']) && !in_array($matches[2], $options['allowed_statuses']) ) {
  72.         die('bad status code');
  73.     }
  74.    
  75.     $response = array();
  76.     $response['scheme'] = $scheme;
  77.     $response['host'] = $host;
  78.     $response['port'] = $port;
  79.     $response['uri'] = $uri;
  80.     $response['http_version'] = $matches[1];
  81.     $response['status'] = $matches[2];  
  82.     $response['status_text'] = trim($matches[3]);
  83.     $headers = array();
  84.        
  85.     while ( !feof($fp) ) {
  86.         $line = rtrim( fgets($fp) );
  87.        
  88.         if ($line === '') {
  89.             break;
  90.         }
  91.        
  92.         list($name, $value) = explode(': ', $line, 2);
  93.         $name = strtolower($name);
  94.        
  95.         if ($name == 'set-cookie') {
  96.             if ( !isset($headers[$name]) ) {
  97.                 $headers[$name] = array();
  98.             }
  99.            
  100.             $headers[$name][] = $value;
  101.         }
  102.         else if ( isset($headers[$name]) ) {
  103.             $headers[$name] .= " ,$value";
  104.         }
  105.         else {
  106.             $headers[$name] = $value;
  107.         }
  108.     }
  109.    
  110.     $body = '';
  111.     $transfer_encoding = isset($headers['transfer-encoding']) ? strtolower($headers['transfer-encoding']) : '';
  112.     $content_encoding = isset($headers['content-encoding']) ? strtolower($headers['content-encoding']) : '';
  113.     $content_length = @$headers['content-length'];
  114.    
  115.     if ($transfer_encoding == 'chunked') {
  116.         while ( !feof($fp) ) {
  117.             $block = rtrim( fgets($fp) );
  118.             $size = (int) base_convert($block, 16, 10);
  119.             // fread почему-то не читает строку указанного размера
  120.             $chunk = stream_get_contents($fp, $size);
  121.             $body .= uncompress($chunk, $content_encoding);
  122.             // пропускаем \r\n
  123.             fread($fp, 2);
  124.         }
  125.     }
  126.     else {  
  127.         if ( is_int($content_length) ) {
  128.             $body = stream_get_contents($fp, $content_length);
  129.         }
  130.         else {
  131.             while ( !feof($fp) ) {
  132.                 $body .= fread($fp, 4096);
  133.             }
  134.         }
  135.        
  136.         $body = uncompress($body, $content_encoding);
  137.     }
  138.    
  139.     $response['headers'] = $headers;
  140.     $response['body'] = $body;  
  141.     return $response;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment