Login   Register  
PHP Classes
elePHPant
Icontem

File: convertir_enlace_inc.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Patricio Cardó  >  Link converter  >  convertir_enlace_inc.php  >  Download  
File: convertir_enlace_inc.php
Role: Class source
Content type: text/plain
Description: class file
Class: Link converter
Convert URLs between absolute and relative paths
Author: By
Last change: fixed url detection in 'desde_url' method
Date: 2010-07-27 08:21
Size: 3,017 bytes
 

Contents

Class file image Download
<?php
class convertir_enlace
{
     public 
$log = array();
     public 
$este = array();
     public 
$es "";
     private 
$re_dir '%[\w\%\d\-]+/%i';
     private 
$re_prev '%\.\./%i';
     function 
dir_enlace(){
        
$this->este["raiz"] = str_replace('\x5c','/',$_SERVER["DOCUMENT_ROOT"]);
        
$this->este["relativa"] = str_replace('\x5c','/',dirname($_SERVER["PHP_SELF"]));
      
        if(isset(
$_SERVER["HTTP_REFERER"]) && is_string($_SERVER["HTTP_REFERER"])){
            
$httpas preg_replace('%'.preg_quote($_SERVER["SCRIPT_NAME"]).'.*$%','',$_SERVER["HTTP_REFERER"]);            
        }else{
             
$prot = (empty($_SERVER["HTTPS"])||$_SERVER["HTTPS"]=='off')?"http":"https";
             
$httpas $prot."://".$_SERVER["SERVER_NAME"].'/'.substr($this->este["relativa"],1).'/';
        }
        
$this->este["url"] = str_replace('\x5c','/',$httpas);
        
$this->este["url_arr"] = parse_url($this->este["url"]);
        
        
$this->este["ruta"] = $this->este["raiz"].$this->este["relativa"];
        
$este_inf parse_url($this->este["raiz"]);
        
$this->este["esquema"] = $este_inf["scheme"];
    }
    function 
convertir_enlace(){
         
$link = (func_num_args()>0)?func_get_arg(0):$_SERVER["PHP_SELF"];
         
$toUrl = (func_num_args()>1)?func_get_arg(1):true;
        
$this->dir_enlace();
         if(
$toUrl){
            
$this->es $this->desde_ruta($link);
        }else{ 
// to relative path;
            
$this->es $this->desde_url($link);
        }
    }
    function 
desde_ruta($link){
        
$info parse_url($link);
        
$este_inf parse_url($this->este["ruta"]);
        if(!isset(
$info["scheme"]) || $info["scheme"]==$this->este["esquema"]){
             if(isset(
$info["scheme"])){
                
$link preg_replace('%'.preg_quote($this->este["raiz"]).'/%ims','',$link);
            }
            
$info pathinfo($link);
            
$dirname str_replace('\x5c','/',$info["dirname"]);
            
$dirname preg_replace('%([^\.])\./%','$1',$info["dirname"]);
            
$retr preg_match_all($this->re_prev,$info["dirname"]."/",$retrasos);
            
$rtrn true;
            
$urldir "/";

            if(
$retr>0){
                
$prof preg_match_all($this->re_dir,$this->este["url_arr"]["path"],$dirs);
                if(
$retr>$prof){
                    
$this->log[] = "Path is deeper than url in ".$link;
                    
$rtrn false;
                }else{
                    for(
$p=0;$p<($prof-$retr);$p++){
                        
$urldir .= $dirs[0][$p];
                    }
                }
                
$link preg_replace($this->re_prev,'',$link);
            }
            if(
$rtrn){
                
$urldir .= preg_replace($this->re_prev,'',$link);
                
$rtrn $this->este["url_arr"]["scheme"]."://".$this->este["url_arr"]["host"].$urldir;
            }
        }else{
            
$rtrn $link;            
        }
        return 
$rtrn;        
    }
    function 
desde_url($link){
        
$info parse_url($link);
        if(isset(
$info["host"])){
            
$a_este str_split($this->este["url"]);
            
$a_link str_split($link);
            
$debe implode("",array_diff_assoc($a_link,$a_este));
            
$sobra implode("",array_diff_assoc($a_este,$a_link));
            
$sobra preg_replace('%//$%','/',$sobra);
            
$profundiza = (strlen($sobra)>0)?preg_replace($this->re_dir,'../',$sobra):"";

            
$rtrn $profundiza.$debe;            
        }else{
            
$rtrn $link;
        }
        return 
$rtrn;
    }
}
?>