<?php
// This class only works with Apache webserver and .htaccess errorpages enabled!
class rewriter {
// The first field of the array is not needed, the others are the parm given by the url seperated by '/'
// i.e. http://localhost/rewriter/page1.html is pared to
/*
Array
(
[] =>
[var1] => rewriter
[var2] => first
)
*/
var $varnames = array("","var1","var2");
var $vars = array();
function rewriter($config=0) {
if(is_array($config)) $this->varnames = $config;
}
// This function parse the given string or $REQUEST_URI to the array as above.
// If $merge is true the result will be merged into $_REQUEST superglobal array!
function decode($str="",$merge=false) {
if(strlen($str) == 0) $str = $REQUEST_URI;
$values=explode("/",$str);
$this->vars=array_flip($this->varnames);
$i=0;
foreach($values as $value) {
$ptn=explode(".",$value);
$this->vars[$this->varnames[$i]] = $ptn[0];
$i++;
}
if($merge) {array_merge($_REQUEST, $this->vars);}
else return $this->vars;
}
// This function encodes the given array to an URI that can be decoded with the decode function.
// Each field of $arr is a seperate 'folder',
// $last is the 'filetype',
// $base is the real folder where the controler script is available to the web
function encode($arr,$last="",$base="") {
if(strlen($base) != 0 )$str=$base."/";
foreach($arr as $value) {
$i++;
$str.=$value;
if($i != count($arr)) $str.="/";
else $str.=$last;
}
return $str;
}
// Telling the Browser that everything is ok.... ;)
function ok() {
@header("HTTP/1.1 200 OK");
@header("Status: 200 OK", TRUE);
}
}
?>