#native_company# #native_desc#
#native_cta#

URL

By Dmitry V. Sabanin
on November 9, 2002

Version: 1.0

Type: Class

Category: HTTP

License: GNU General Public License

Description: Class that abstracts URL.

<?php
/*
* Class that abstracts URL;
* It can easily make URL's for dynamic scripts and also protects ur URL from "junk".
* With the word 'junk' i mean the situation when u add same variables with different values 
* on the different parts of the scripts.
* 
* Example of use:
* 	$action = new URL();
* 	$action->addVal("form_submitted","1");
* 	echo $action->Build();
* This will print ur current URL with new variable called form_submitted and equal to 1.
* If u want link to the URL u can i.e. do this:
* 	$newspage = new URL();
* 	$newspage->addVal("module","news");
* 	echo $newspage->A();
* 
* Changelog
* 14 march: 
* 	+ added Clear() method
* 	+ fixed error in Build() method
* 	+ comments to methods are added
*/


class URL
{
  var $Text;
  var $Scheme;
  var $Host;
  var $Port;
  var $Path;
  var $Query;
  var $Fragment;
  var $Dynamic;  // TRUE if URL have GET vars
  var $Valid;
  var $Args = array();
  var $Cache; // cached previous build of URL
  /**
   * URL::URL()
   * 
   * @param mixed $Text U can specify URL that different from current 
   * @return void
   **/
  function URL($Text = FALSE)
  {
    global $HTTP_SERVER_VARS;
    if ( $Text )
       $this->Text = $Text;
    else
        $this->Text = "http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"];
    if ( $this->parseURL() )
       $this->Valid = TRUE;
    else
       $this->Valid = FALSE;
  }
  /**
   * URL::parseURL()
   * This method is private, it parses URL on the parts.
   * @return boolean URL is right or not;
   **/
  function parseURL()
  {
           $tmp = parse_url( $this->Text );

           if ( isSet($tmp["scheme"]) )
              $this->Scheme = $tmp["scheme"];
           else
               return (FALSE);

           if ( isSet($tmp["host"]) )
              $this->Host = $tmp["host"];
           else
               return (FALSE);

           if ( isSet($tmp["path"]) )
              $this->Path = $tmp["path"];
           else
               return(FALSE);
        
           if ( isSet($tmp["query"]) )
              $this->Query  = $tmp["query"];

           if ( isSet($tmp["fragment"]) )
              $this->Fragment = $tmp["fragment"];
    
           if ( isSet($tmp["port"]) )
              $this->Port = $tmp["port"];
       
           unset($tmp);

            if ( isSet($this->Query) )
               $this->Dynamic = TRUE;
            else
                $this->Dynamic = FALSE;
        
            if ( $this->Dynamic )
            {
               $temp = explode("&",$this->Query);
               $i = 0;
               while ( list($key, $val) = each ($temp) )
               {
                     $tmp = explode("=",$val);
                     $this->Args[$tmp[0]] = $tmp[1];
                     $i++;
               }
            }
            return (TRUE);
  }
  /**
   * URL::dropVal()
   * Drops a key from URL.
   * @param mixed $key A key of value to drop.
   * @return string URL if ok or echo about error.
   **/
  function dropVal($key)
  {
    if ( isSet($this->Args[$key]) )
       $this->Args[$key] = NULL;
    else
        echo "Key not found.";
    return $this->Build();
  }
  /**
   * URL::Clear()
   * Clears previous URL and let u work with another.
   * @param string $Text This can be used to work with specific URL, not the current one.
   * @return void
   **/
  function Clear()
  {
  	$this->URL();
	empty($this->Args);
  }
  /**
   * URL::getVal()
   * Gets a value from URL. (Even that values that are from 
   * @param $key
   * @return mixed key if everything is OK, or echo'es that key is not found.
   **/
  function getVal ($key)
  {
    if ( isSet($this->Args[$key]) )
       return $this->Args[$key];
    else
        echo "Key not found.";
  }
  
  /**
   * URL::addVal()
   * Adds a key to URL.
   * @param mixed $key A key to add.
   * @param mixed $value Value of the key
   * @return string URL
   **/
  function addVal ($key, $value)
  {
    if ( !isSet($this->Args[$key]) )
       $this->Args[$key] = $value;
    else
        echo "Key already exists.";
    return $this->Build();
  }
  
  /**
   * URL::setVal()
   * 
   * @param mixed $key Key name for parameter.
   * @param mixed $value Value for parameter.
   * @param boolean $create Create new key if not exists.
   * @return Echo if key not found and builded URL if everything ok.
   **/
  function setVal ($key, $value, $create=FALSE)
  {
    if ( isSet($this->Args[$key]) or $create == TRUE)
       $this->Args[$key] = $value;
    else
        echo "Key not found.";
    return $this->Build();
  }
  
  /**
   * URL::is_Set()
   * Checks if key is set in the URL.
   * @param mixed $key 
   * @return boolean  
   **/
  function is_Set ($key)
  {
    if ( isSet($this->Args[$key]) )
       return TRUE;
    else
        return FALSE;
  }
  
  /**
   * URL::Build()
   * This is can be used to build a URL.
   * @param boolean $full Set to TRUE if u want full URL (with hostname etc)
   * @return string URL
   **/
  function Build ($full = FALSE)
  {
    $url = "";
    if ($full)
    {
      $url = $this->Scheme."://";
      $url .= $this->Host;
      if ( isSet($this->Port) )
         $url .= ":".$this->Port;
    }
    $url .= $this->Path;
    $i = 0;
	foreach ( $this->Args as $key => $value )
    {
          if ( $i == 0 ) $url .= "?";
          if ( !is_Null($value) )
          {
             $url .= $key."=".$value."&";
          }
          $i=1;
    } 
    if ( $i > 0 )
       {
         $url = substr_replace($url, '', -1);
       }
    if ( isSet($this->Fragment) )
       $url .= "#".$this->Fragment;
    $this->Cache = $url;
    return $url;
  }
  /**
   * URL::A()
   * Returns builded URL with <A> tags
   * @param string $name Name of link, it will be like this: <A HREF=...>$name</A>
   * @param boolean $full Set to TRUE if u want full URL (with hostname etc)
   * @return string URL with tags
   **/
  function A($name = FALSE, $full = FALSE)
  {
    $url = $this->Build($full);
    if ( !$name )
       $name = $url;
    return "<A HREF=".$url.">".$name."</A>";
  }
}
?>