<?php

/*
 *    Http agent
 *
 *    version: 0.1
 *    developed: Karoly Gossler
 *    mail: connor [at) connor (dot] hu
 *    web: blog.connor.hu
 *    licence: GPLv2 :: http://www.gnu.org/licenses/gpl.txt
 */

class http_agent
{
  public 
$load_cookies '';
  public 
$save_cookies '';
  public 
$result '';
  public 
$post_data false;
  public 
$referer false;
    public 
$debug false;
  public 
$url '';
  private 
$redirect_couter 0;
  private 
$url_parts '';
  private 
$headers = array();

  public function 
__construct($url)
  {
    
$this->url $url;
    if (!
defined('NL'))
    {
      
define('NL'"\n");
    }
        if (!
function_exists('gzdecode')) {
            function 
gzdecode ($data) {
                
// Check if data is GZIP'ed
                
if (strlen($data) < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) {
                    return 
false;
                }

                
// Remove first 10 bytes
                
$data substr($data10);

                
// Return regular data
                
return gzinflate($data);
            }
        }
    }

    public function 
get_headers()
    {
        return 
$this->headers;
    }

  private function 
get_header()
  {
    
$this->url_parts parse_url($this->url);

    if (
$this->url_parts['path'] === null)
    {
      
$this->url_parts['path'] = '/';
    }
    if (
$this->url_parts['query'] !== null)
    {
      
$this->url_parts['path'] .= ('?'$this->url_parts['query']);
    }

    
$cookies '';
    if (
$this->load_cookies !== false && is_file($this->load_cookies))
    {
      
$cookies trim(implode(''file($this->load_cookies)));
    }

    if (
$this->post_data !== false)
    {
      
$header 'POST '$this->url_parts['path'] .' HTTP/1.1'NL;
    }
    else
    {
      
$header 'GET '$this->url_parts['path'] .' HTTP/1.1'NL;
    }
    
$header .= 'Host: '$this->url_parts['host'] . NL;

        if (
$this->referer !== false)
        
$header .= 'Referer: '$this->referer NL;

        
$header .= 'Keep-Alive: 300'NL;
        
$header .= 'Connection: keep-alive'NL;
        
$header .= 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'NL;
        
$header .= 'Accept-Language: hu'NL;
        
$header .= 'Accept-Encoding: gzip,deflate'NL;
    
$header .= 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; hu; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4'NL;

    if (
$cookies)
    {
      
$header .= 'Cookie: '$cookies NL;
    }
    if (
$this->post_data !== false)
    {
      foreach (
$this->post_data as $name => $value)
      {
                
$post_data .= '&'$name .'='urlencode($value);
      }
            
$post_data substr($post_data1);
            
$header .= 'Content-Type: application/x-www-form-urlencoded' .NL;
      
$header .= 'Content-Length: 'strlen($post_data) .NL;
      
$header .= NL;
      
$header .= $post_data;
    }
    else
    {
      
$header .= NL;
    }
    return 
$header;
  }

  public function 
fetch()
  {
    
$header $this->get_header();

        if (
$this->debug == true)
        {
            
print_r($header);
        }
        echo 
$this->url;
        echo 
"\n";

    if (
$this->url_parts['scheme'] == 'http')
    {
      
$prefix '';
      
$port 80;
    }
    elseif (
$this->url_parts['scheme'] == 'https')
    {
      
$prefix 'ssl://';
      
$port 443;
    }

    
$t fsockopen($prefix $this->url_parts['host'], $port$errno$errstr12);
    if (
$t === false)
    {
      exit;
    }

        
fputs($t$header);

    
$just_headers false;
    
$result '';

    while (!
feof($t))
    {
      
$result .= fread($t1024);
      if (
$just_headers !== false && strpos($result'Connection: ') !== false)
      {
        break;
      }
    }
    
fclose($t);

        
$isheader true;
    foreach (
explode("\n"$result) as $line)
    {
      if (
strlen(trim($line)) === 0)
      {
        
$isheader false;
        continue;
      }
      if (
$isheader)
      {
        list(
$name$value) = $this->header_parse($line);
        
$name strtolower($name);
        
$this->headers[$name] = $value;
        
        if (
$name == 'location') {
          
$this->redirect_couter++;
          if (
$this->redirect_couter 10)
            return;

                    
// todo rendes location kezelés
                    
if (substr($value04) == 'http')
                        
$this->url trim($value);
                    else
                        
$this->url $this->url_parts['scheme'] .'://'$this->url_parts['host'] . trim($value);

                    
$this->fetch();

          return;
        }
      }
      else
      {
        
$content .= $line "\n";
      }
    }

        if (
$this->headers['set-cookie'] && $this->save_cookies)
        {
            
file_put_contents($this->save_cookiessubstr($this->headers['set-cookie'], 0strpos($this->headers['set-cookie'], ';')));
        }

        if (
trim($this->headers['content-encoding']) == 'gzip')
            
$content gzdecode($content);

    
$this->result $content;
  }

  private function 
header_parse($header)
  {
    if (
substr($header04) == 'HTTP')
    {
      return array(
0$header);
    }
    return array(
substr($header0strpos($header':')), substr($headerstrpos($header':')+2));
  }
}

?>