<?php

define
('xmlEXT''xml');
define('xslEXT''xsl');

//get local path
$self_path preg_replace("!index\.php!","",getenv('SCRIPT_NAME'));
$query_string preg_replace("!^".$self_path."!","",getenv('QUERY_STRING'));

if (
preg_match("/_/"getenv('QUERY_STRING'))) {
    
header("HTTP/1.1 301 Moved Permanently");
    
header("Location: http://blog.othree.net".preg_replace("/_/""-"getenv('QUERY_STRING')));
    exit();
}

if (
$query_string == "source/")
{
    
highlight_file(__FILE__);
    exit;
}

//$style = ($_COOKIE['style'])?$_COOKIE['style']:'sky';
$style "default";
/*if (preg_match("/about\/styles\/[^\.]+/", $query_string))
{
    $style = substr($query_string,13);
    if (is_file("stylesheets/".$style."/main.css"))
    {
        setcookie ("style", $style, time() + 60*60*24*30, "/", "blog.othree.net");
    }
    else
    {
        $style = ($_COOKIE['style'])?$_COOKIE['style']:'sky';    
    }
    $query_string = "about/styles/";
}*/


//get target file path
$format = (preg_match("/(\w+)$/",$query_string,$matches))?$matches[0]:'xhtml';
if (
preg_match("/^feeds/"$query_string)) $format "xml";
$query_string preg_replace("/[\.\/]?(".$format.")?$/","",$query_string);
$targetfile = (($query_string == "")?"index":$query_string).".".(($format == "xhtml")?xmlEXT:$format);
if (!
is_file($targetfile))
    
$targetfile = (($query_string == "")?"index":$query_string)."/index.".(($format == "xhtml")?xmlEXT:$format);
if (!
is_file($targetfile))
    
$targetfile $query_string.".rss".".xml";
if (!
is_file($targetfile)) {
          include(
"error.php");
    exit;
}

//format

//xsl transforma
if (preg_match("/application\/xhtml\+xml/",getenv('HTTP_ACCEPT')) || preg_match("/W3C/"getenv('HTTP_USER_AGENT'))) $mime 'xhtml';
else 
$mime 'html';

if (
preg_match("/^xhtml$/",$format)) $output_html xmlplusxslt($targetfile"main.".xslEXT$mime);
else
{
    
$handle fopen($targetfile"r");
    
$output_html fread($handlefilesize($targetfile));
    
fclose($handle);
}

//compitable fix for IE
$patterns = array("!<(script|textarea)((\s+\w+=(\w+|\"[^\"]*\"|\'[^\']*\'))*)\/>!",
    
"!([\"\'])\/>!",
    
"! *<(\/?(pre|code))> *!is",
    
"!\s*<(\/?a)((\s+\w+=(\w+|\"[^\"]*\"|\'[^\']*\'))*)>\s*!");

$replacements = array("<\\1\\2></\\1>",
    
"\\1 />",
    
"<\\1>",
    
"<\\1\\2>");

//compitable fix for nonIE
if (!preg_match("/IE/"getenv('HTTP_USER_AGENT')))
{
    
array_push($patterns"/\s*<(\/?(pre|code))>\s*/is");
    
array_push($replacements"<\\1>");
}

$output_html preg_replace($patterns,$replacements,$output_html);


//content type

if ($format == "xml")
{
    if (
preg_match("/IE/"getenv('HTTP_USER_AGENT'))) sheader("Content-type: text/xml; charset=UTF-8");
    else 
sheader("Content-type: application/xml; charset=UTF-8");
}
else if (
preg_match("/application\/xhtml\+xml/",getenv('HTTP_ACCEPT')) || preg_match("/W3C/"getenv('HTTP_USER_AGENT')))
{
    
$output_html preg_replace("/\s*<meta http-equiv=.*>/","",$output_html);
    
//<meta http-equiv /> does not need for XHTML 1.1
    
sheader("Content-type: application/xhtml+xml; charset=UTF-8");
}
else
{
    
$patterns = array("/<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>\n/",
        
"/<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.1\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml11\/DTD\/xhtml11.dtd\">/",
        
"/application\/xhtml\+xml/",
        
"/xml:lang=\"([a-z]{2}(\-[A-Z]{2})?+)\"/",
        
"/<\!\[CDATA\[/",
        
"/\]\]>/");
    
$replacements = array("",
        
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
        
"text/html",
        
"lang=\"\\1\" xml:lang=\"\\1\"",
        
"<!--",
        
"-->");
    
$output_html preg_replace($patterns,$replacements,$output_html);
    
//change to XHTML 1.0 strict
    
sheader("Content-type: text/html; charset=UTF-8");
}

echo 
$output_html;

//xsl transform function

function xmlplusxslt($xml$xsl$mime)
{
    if (
preg_match("/^5/",phpversion()) && (extension_loaded('xsl') || dl("xsl".$suffix)))
    {
        
$xmlo = new DOMDocument// from /ext/dom
        
$xmlo->load($xml);

        
$xslo = new DOMDocument;
        
$xslo->load($xsl);

        
/* Configure the transformer */
        
$proc = new XSLTProcessor;
        
$proc->importStyleSheet($xslo); // attach the xsl rules
        
$proc->setParameter('blog.othree.net''ext''');
        
$proc->setParameter('blog.othree.net''mime'$mime);
        
//$proc->setParameter('blog.othree.net', 'style', $style);
        
$html $proc->transformToXML($xmlo); // actual transformation
    
}
    else
    {
        
$html "XSL Transform Failed: Function not Found.";
    }

    return 
$html;
}

// send header function

function sheader ($hc)
{
//    if (!headers_sent()) header($hc);
    
if (!headers_sent())
    {
//        header('pics-label: (PICS-1.1 "http://www.ticrf.org.tw/chinese/html/06-rating-v11.htm" l gen true for "http://blog.othree.net" r (s 3 l 3 v 3 o 3))');
        
header($hc);
    }

}

?>