stuppid_bot

Untitled

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