<?php
/*
 * HTTP_URL
 *
 * Copyright (c) 2004 Colin Viebrock <colin@viebrock.ca>
 *
 * Usage of the works is permitted provided that this
 * instrument is retained with the works, so that any entity
 * that uses the works is notified of this instrument.
 *
 * DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
 */


require 'PEAR.php';
require 
'Net/Curl.php';


class 
HTTP_URL extends PEAR {

    var 
$url;

    var 
$raw_result;
    var 
$raw_header;
    var 
$raw_body;

    var 
$headers;
    var 
$refs = array();


    var 
$_ref_tags = array(
        
'frame'        => 'src',
        
'img'        => 'src',
        
'a'            => 'href',
        
'form'        => 'action',
        
'link'        => 'href',
        
'script'    => 'src'
    
);


    function 
HTTP_URL($url=NULL) {
        
$this->PEAR();
        
$this->reset();
        if (
$url!==NULL) {
            
$this->setUrl($url);
        }
    }


    function 
reset() {
        
$this->url NULL;

        
$this->raw_result NULL;
        
$this->raw_header NULL;
        
$this->raw_body NULL;

        
$this->headers NULL;
        
$this->refs = array();
    }


    function 
setURL($url) {
        
$this->reset();
        
$this->url $url;
    }


    function 
get() {
        
$c = new Net_Curl;
        
$c->url                $this->url;
        
$c->header            true;
        
$c->progress        false;
        
$c->follow_location    false;
        
$this->raw_result $c->execute();
        
$c->close();
        list(
$this->raw_header,$this->raw_body) = explode("\r\n\r\n"$this->raw_result2);

        
$this->parseHeader();

    }


    function 
parseHeader() {
        if (!
$this->raw_header) {
            return;
        }

        list(
$first,$rest) = explode("\r\n"$this->raw_header2);

        
$temp explode(' '$first3);
        
$this->headers['_Request-Protocol'] = $temp[0];
        
$this->headers['_Request-Code'] = $temp[1];
        
$this->headers['_Request-String'] = $temp[2];

        
$temp explode("\r\n"$rest);
        foreach(
$temp as $line) {
            list(
$h,$v) = explode(':'$line2);
            
$this->headers[trim($h)] = trim($v);
        }

        if (isset(
$this->headers['Date'])) {
            
$this->headers['_Date'] = strtotime($this->headers['Date']);
        }
        if (isset(
$this->headers['Last-Modified'])) {
            
$this->headers['_Last-Modified'] = strtotime($this->headers['Last-Modified']);
        }

        return 
true;

    }


    function 
parseBody() {
        if (!
$this->raw_body) {
            return;
        }


        
$this->refs = array();

        
/* don't need closing tags, so skip </....> */

        
if (preg_match_all('/<([^\/].*?)>/'$this->raw_body$m)) {

            foreach(
$m[1] as $tag) {
                
$tag trim($tag);
                foreach(
$this->_ref_tags as $reftag=>$srctag) {

                    if (!
is_array($this->refs[$reftag])) {
                        
$this->refs[$reftag] = array();
                    }

                    if (
preg_match('/^'.$reftag.'\s/i',$tag)) {
                        
$regex '/'.$srctag.'=(\S*)/i';
                        if (
preg_match($regex$tag$n)) {
                            
$this->refs[$reftag][] = $this->stripQuotes($n[1]);
                            break;
                        }
                    }
                }
            }
        }

        foreach (
$this->refs as $reftag=>$array) {
            
sort($array);
            
$this->refs[$reftag] = array_values(array_unique($array));
        }

        return 
true;

    }



    function 
stripQuotes($str) {
        
$q = array ('"''\'');
        foreach(
$q as $qc) {
            if ( 
substr($str,0,1)===$qc && substr($str,-1)===$qc ) {
                
$str substr($str,1,-1);
                break;
            }
        }
        return 
$str;
    }

}