PHP Classes
elePHPant
Icontem

File: gUrl.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cesar D. Rodas  >  guaranix URL  >  gUrl.php  >  Download  
File: gUrl.php
Role: Class source
Content type: text/plain
Description: The main class
Class: guaranix URL
Compare URLs and compute relative URIs
Author: By
Last change:
Date: 8 years ago
Size: 2,936 bytes
 

Contents

Class file image Download
<?
/*
 *************************************************************
 * guaranixURL *
 *************************************************************
 * The Author disclaims the copyright of this project *
 * You are free to do use this project for free with *
 * the only condition that do not forget that "Is better *
 * Give than Receive" *
 *************************************************************
 * Cesar Rodas <saddor@guaranix.com> *
 *************************************************************
*/

/*
 * Guaranix URL, this class takes two arguments a Base and
 * relative URL and mix it, and return a new URL.
 */
class gURL
{
    function
newURI($base=false, $rel=false)
    {
        if (
$base==false || $rel==false)
        {
           
printf("Error in %s line %d\n",__FILE__,__LINE__-3);
            return;
        }
       
$pBase = parse_url($base);
       
$pRel = parse_url($rel);
       

        if ( isset(
$pRel['scheme']) )
        {
           
/*
             * The relative URL is not relative, so return it.
             */
           
$ret = $rel;
        }
        else
        {
           
$ret = $pBase['scheme']."://".$pBase['host'];
           
$ret .= isset($pBase['port']) && $pBase['port']!=80 ? ":".$pBase['port']."/" : "/";
            if (
$rel[0] == "/")
            {
               
$ret .= substr($rel,1);
            }
            else
            {
               
$tmp = $this->Folder2Array($pBase['path']);
               
$tmp1 = $this->Folder2Array($pRel['path'],false);
               
                foreach(
$tmp1 as $id1 => $d1)
                    if (
$d1 == "..") unset($tmp[count($tmp)-1]);
                    else if (
$d1 == ".") continue;
                    else
$tmp[] = $d1;
               
$ret .= implode("/",$tmp);
               
$ret .= substr($rel,-1,1) == "/" ? "/" : "";
               
$ret .= isset($pRel['query']) ? "?".$pRel['query'] : "";
                unset(
$tmp,$tmp1); /* Release memory */
           
}
        }
        unset(
$pBase,$pRel);/* Release memory */
       
return $ret;
    }
   
    function
Folder2Array($path,$onlyDir=true)
    {
        if (
$path[0] == '/')
           
$path=substr($path,1);
       
$ret = explode("/",$path);
        if (
$onlyDir && substr(trim($path),-1,1) != "/")
           
/*
             * If onlyDir is true and the last letter isn't a /
             * seems that is not a Dir! so, delete it!
             */
           
unset( $ret[count($ret)-1] );
       
        if (
$ret[count($ret)-1]=="")
           
/*
             * If the last element of $ret is empty, delete it
             */
           
unset($ret[count($ret)-1]);
        return
$ret;
    }
}

?>