<?
 
class EBrowser {

  var 
$cookies,
  var 
$headers;
  var 
$html;

  function 
EBrowser() {

   
$this->cookies FALSE;
   
$this->headers FALSE;
   
$this->html FALSE;

  }
//end of func

  
function GetPage($hostas$referer="www.somehost.lt"$query=""$method="GET") {

  
// query is query string like key1=val1&key2=val2 (without <?>)

   // we can't POST empty query, let's change method
   if(empty($query)) $method = "GET";

   // let's parse url into parts
   $this->_parseURL($hostas, $URL);

    /* Jungiamasi per 80 porta, nurodome timeout'a 60 s. */

    $jungtis = fsockopen($URL['Host'], 80, &$errno, &$errstr, 60);

    /* Pranesame apie klaida, jei nepavyko prisijungti */

        if(!$jungtis) die("Can't connect to host " . $URL['Host']);

        $page = substr($URL['Path'], 1);

        // let's add params
        if($method == "GET" && !empty($query)) $page .= "?" . $query;

    /* Ka norime gauti is serverio */
        $uzklausa = "$method /" . $page .  " HTTP/1.0\r\n";
    /* Referer - is kur atejome i si puslapi */
        $uzklausa .= "Referer: $referer\r\n";
        $uzklausa .= "Proxy-Connection: Keep-Alive\r\n";
    /* Nurodome narsykle */
        $uzklausa .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)\r\n";
    /* Na ir dar keletas uzklausos eiluciu, kurios padetu jei ant IP yra keli puslapiai */
        $uzklausa .= "Host: " . $URL['Host'] . "\r\n";
        $uzklausa .= "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png\r\n";
        $uzklausa .= "Accept-Language: de,en,x-ns1_sbnManqNhQ,x-ns2r3709OnmPe2\r\n";
        $uzklausa .= "Accept-Charset: iso-8859-1,*,utf-8\r\n";

        if(isset($this->cookies)) $this->_setCookies($uzklausa);

        if(!empty($query) && $method != "GET") {
         $uzklausa .= "Content-type: application/x-www-form-urlencoded\r\n";
         $uzklausa .= "Content-length: " . strlen($query) . "\r\n";
        }//end of of

        $uzklausa .= "\r\n";

        if(!empty($query) && $method != "GET") $uzklausa .= $query;

    /* Pasiunciame suformuota uzklausa */

    fputs($jungtis, $uzklausa);

    /* Gauname duomenis per is socket'o */

        $atsakymas = "";

        while(!feof($jungtis)) {
          $str = fgets($jungtis,12800);
          $atsakymas .= $str;
        }//end of while

     /* Graziname atsakyma */

    $this->_splitResponse($atsakymas);

 }//end of func

  function _parseURL($URL, &$URL_INFO) {

   $URL_INFO = FALSE;

   preg_match("/^((.*?):\/\/)?(([^:]*):([^@]*)@)?([^\/:]*)(:([^\/]*))?([^\?#]*\/?)?(\?([^?#]*))?(#(.*))?$/", $URL, $aURL);
   list(,, $URL_INFO['Protocol'],, $URL_INFO['Username'], $URL_INFO['Password'], $URL_INFO['Host'],, $URL_INFO['Port'], $URL_INFO['Path'],, $URL_INFO['Query'],, $URL_INFO['Anchor']) = $aURL;

  }//end of func

  function _splitResponse($response) {

   // headers and html is imploded with \r\n\r\n
   list($this->headrs, $this->html) = explode("\r\n\r\n", $response);

  }//end of func

  function _getCookies($headers) {

   preg_match_all("/^Set-Cookie: (.*)$/im", $this->headers, $matches);

   if($matches) {

    foreach($matches[1] as $cookie) {

     $k = FALSE;
     $v = FALSE;

     // cookie have additional parametres as path or smth
     if(strstr($cookie, ";")) {

       $cookieparts = explode(";", $cookie);

      foreach($cookieparts as $part){

       list($k, $v) = explode("=", $part);

       if($k != "expires" && $k != "domain" && $k != "path" && $k != "secure") {
        $this->cookies[] = $k . "=" . $v;
        break;
       }//end of if

      }//end of foreach

     }//end of if

     // cookie have only key=val
     else {

      list($k, $v) = explode("=", $cookie);

       if($k != "expires" && $k != "domain" && $k != "path" && $k != "secure") {
        $this->cookies[] = $k . "=" . $v;
       }//end of if

     }//end of else

    }//end of foreach

   }//end of if

   else unset($this->cookies);

  }//end of func

  function _setCookies(&$headers) {

   if(isset($this->cookies)) {

     $headers .= "Cookie: ";

     foreach($this->cookies as $cookie) {
      $headers .= $cookie . "; ";
     }//end of while

     // let's remove last ["; "]
     $headers  = substr($headers, 0, strlen($headers) - 2);

     $headers .="\r\n";

   }//end of if

  }//end of func

 }// end of class