<?php
// vim: set expandtab tabstop=4 shiftwidth=4:
/**
* apache_lookup_uri wrapper
*
* if apache_lookup_uri doesn't exists, it tries to simulate it
*
* @author Ilya Lebedev <ilya_AT_lebedev_DOT_net>
* @version 0.2
*/
class Apache_LookupUri {
/**
* Wrapper itself
*
* @param string object for lookup
* @return object identical to apache_lookup_uri
*/
function Apache_LookupUri ($uri) {
if (function_exists("apache_lookup_uri") && !defined("Apache_LookupUri_wrapper")) {
$l = apache_lookup_uri($uri);
foreach ($l as $k => $v) {
$this->$k = $v;
}
} else {
$this->method = "GET";
$this->request_time = time();
$this->unparsed_uri = $uri;
$this->uri = preg_replace("/\?.*/","",$uri);
$this->the_request = "GET ".$uri." HTTP/1.1";
$args = stristr($uri,"?");
$this->args = substr($args,1,strlen($args));
$res = $this->_parseURI($uri);
$this->filename = $res['fname'];
$this->path_info = $res['path'];
$this->content_type = $res['type'];
$this->status = (file_exists($this->filename)?"200":"404");
}
}
// When folder with specified name doesn't exists, but we can find file
// with the same name there, we can be sure that MultiViews is used and
// that file is request handler.
// Thus we will return it's name as handler.
//
// Only one requirement exists - filename (without extension) should be
// unique, otherwise first found file will be returned.
/**
* Parses URI
*
* @param string URI to parse
* @return array
*/
function _parseURI ($uri) {
//
// we can detect MultiViews by special REDIRECT_URL value
// where will be not the real script filename, but URL
//
$dr = $_SERVER['DOCUMENT_ROOT'];
$d = preg_replace("/\?.*/","",$uri);
if (file_exists($dr.$d)) {
$res[0] = $dr.$d;
$p = array("");
} else {
$rdr = explode("/",$d);
while ($rdr) {
$res = glob($dr.implode("/",$rdr).".*");
if (isset($res[0])) break;
$p[] = $rdr[sizeof($rdr)-1];
unset ($rdr[sizeof($rdr)-1]);
}
if (!$res) {
$res[0] = $dr.$d;
$p = array();
}
}
krsort ($p);
$r['fname'] = $res[0];
$r['path'] = ($p)?"/".implode ("/",$p):"";
$r['type'] = $this->_getType($r['fname']);
return $r;
}
/**
* Small wrapper for mime_content_type, very simple
*
* @var string thing to be typed
* @return string mime-type
*/
function _getType ($path) {
if (is_dir($path)) return "httpd/unix-directory";
if (function_exists("mime_content_type"))
return mime_content_type ($path);
else { //simple mime_content_type emulator
$d = pathinfo($path);
switch ($d['extension']) {
case "html" :
case "htm" : return "text/html";
case "php" :
case "php3" :
case "phtml":
case "phtm" : return "application/x-httpd-php";
default : return "text/plain";
}
}
}
}
if (!function_exists("apache_lookup_uri") && !defined("Apache_LookupUri_wrapper")) {
define("Apache_LookupUri_wrapper", true);
function apache_lookup_uri($uri) { return new Apache_LookupUri($uri); }
}
?>