<?
/**
* Class WebResource
* @author simone cosci
* @version 1.0
* @abstract This Class is called by a WebAnalizer
* using parse() method it will set pathinfo[] and params[] associative arrays
* es. http://www.domain.com/path/to/resource?var1=value&var2=value ...
* pathinfo:
* [scheme] => http
* [host] => www.domain.com
* [path] => /path/to/resource
*
* params:
* [var1] => value
* [var2] => value
*
* and get_response() return the HttpResponse object using HttpRequest object passed as argument
*/
$i = 0;
define('WST_RELATIVE_PATH',$i++);
define('WST_ABSOLUTE_PATH',$i++);
class WebResource
{
/**
* Path to analize
* @access public
* @var string
* @example path /path/to/file | http://domain/script
*/
var $path;
/**
* Current protocol to detect relatives path
* @access public
* @var string
*/
var $protocol;
/**
* Current host relatives path
* @access public
* @var string
*/
var $host;
/**
* Type of path
* @access public
* @var int
*/
var $type;
/**
* pathinfo() associative array return
* @access public
* @var array
*/
var $pathinfo;
/**
* Script or file name
* @var string
*/
var $filename;
/**
* Associative array of parameters in querystring
* @var array
*/
var $params;
/**
* Error
* @access public
* @var string error
*/
var $err;
/**
* WebAnalizer Construxtor
* @param string $path
* @access public
*/
function WebResource($path){
$this->path = $path;
$this->err = '';
}
function parse(){
$this->params = array();
$this->type = WST_RELATIVE_PATH;
if(strtolower(substr($this->path,0,7))=='http://' || strtolower(substr($this->path,0,8))=='https://') {
if(preg_match( '/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}((:[0-9]{1,5})?\/.*)?$/i' ,$this->path)) {
/* ABSOLUTE PATH */
$this->type = WST_ABSOLUTE_PATH;
$this->pathinfo = parse_url($this->path);
if(isset($this->pathinfo['path'])){
$path = '/'.$this->resolve($this->pathinfo['path']);
$this->pathinfo['path'] = str_replace('//','/',$path);
}else $this->pathinfo['path'] = '/';
if(!empty($this->pathinfo['query'])) parse_str($this->pathinfo['query'], $this->params);
}else{
$this->err = 'Invalid Url format: '.$this->path;
return false;
}
}else{
/* RELATIVE PATH */
$pathinfo = pathinfo($this->path);
$query = explode('?',$pathinfo['basename']);
if(count($query)>0){
$this->filename = array_shift($query);
$this->pathinfo['query'] = implode($query);
}else{
$this->filename = $query;
$this->pathinfo['query'] = '';
}
$this->pathinfo['scheme'] = $this->protocol;
$this->pathinfo['host'] = $this->host;
// echo __FILE__.' ('.__LINE__.') dirname:'.$pathinfo['dirname']."\n";
$path = $this->resolve($pathinfo['dirname']);
$path = '/'.$path.'/'.$this->filename;
$this->pathinfo['path'] = str_replace('//','/',$path);
// echo __FILE__.' ('.__LINE__.') path:'.$this->pathinfo['path']."\n";
if(!empty($this->pathinfo['query'])) parse_str($this->pathinfo['query'], $this->params);
}
return true;
}
/**
* @access public
* @return object HttpResponse (just Initialized)
* @var HttpRequest handle to HttpRequest object
*/
function get_response($myHttpRequest){
if(is_object($myHttpRequest) && get_class($myHttpRequest)=='httprequest'){
$myHttpRequest->host = $this->pathinfo['host'];
$myHttpRequest->server = gethostbyname($this->pathinfo['host']);
//echo $myHttpRequest->host."<br>";
$myHttpRequest->target = $this->pathinfo['path'];
$myHttpRequest->protocol = $this->pathinfo['scheme'];
$myHttpRequest->vars = $this->params;
if(!$myHttpRequest->Send()) return $myHttpRequest->err;
else return $myHttpRequest->response;
}else{
return 'parameter passed to get_response() method is not an instance of HttpRequest class ';
}
}
function resolve($path){
$pieces = explode('/',$path);
foreach($pieces as $k=>$piece){
if($piece=='.'){ unset($pieces[$k]); }
if($piece=='..'){ unset($pieces[$k]); unset($pieces[$k-1]); }
if(empty($piece)) unset($pieces[$k]);
}
return implode('/',$pieces);
}
}
?>