handleRequest()
*/
static function process(HTTPResponse $response) {
if(self::$disabled) return;
$mimes = array(
"xhtml" => "application/xhtml+xml",
"html" => "text/html",
);
$q = array();
if(headers_sent()) {
$chosenFormat = "html";
} else if(isset($_GET['forceFormat'])) {
$chosenFormat = $_GET['forceFormat'];
} else {
// The W3C validator doesn't send an HTTP_ACCEPT header, but it can support xhtml. We put this special case in here so that
// designers don't get worried that their templates are HTML4.
if(isset($_SERVER['HTTP_USER_AGENT']) && substr($_SERVER['HTTP_USER_AGENT'], 0, 14) == 'W3C_Validator/') {
$chosenFormat = "xhtml";
} else {
foreach($mimes as $format => $mime) {
$regExp = '/' . str_replace(array('+','/'),array('\+','\/'), $mime) . '(;q=(\d+\.\d+))?/i';
if (isset($_SERVER['HTTP_ACCEPT']) && preg_match($regExp, $_SERVER['HTTP_ACCEPT'], $matches)) {
$preference = isset($matches[2]) ? $matches[2] : 1;
if(!isset($q[$preference])) $q[$preference] = $format;
}
}
if($q) {
// Get the preferred format
krsort($q);
$chosenFormat = reset($q);
} else {
$chosenFormat = "html";
}
}
}
$negotiator = new ContentNegotiator();
$negotiator->$chosenFormat( $response );
}
/**
* Only sends the HTTP Content-Type as "application/xhtml+xml"
* if the template starts with the typical ", , ).
*
* @param $response HTTPResponse
* @return string
* @todo More flexible tag and entity parsing through regular expressions or tag definition lists
*/
function xhtml(HTTPResponse $response) {
$content = $response->getBody();
// Only serve "pure" XHTML if the XML header is present
if(substr($content,0,5) == '<' . '?xml' ) {
$response->addHeader("Content-type", "application/xhtml+xml; charset=" . self::$encoding);
$response->addHeader("Vary" , "Accept");
$content = str_replace(' ',' ', $content);
$content = str_replace('
','
', $content);
$content = eregi_replace('(]*[^/>])>','\\1/>', $content);
$response->setBody($content);
} else {
return $this->html($response);
}
}
/*
* Sends HTTP Content-Type as "text/html", and replaces existing doctypes with
* HTML4.01 Strict.
* Replaces self-closing tags like with unclosed solitary tags like .
* Replaces all occurrences of "application/xhtml+xml" with "text/html" in the template.
* Removes "xmlns" attributes and any Pragmas.
*/
function html(HTTPResponse $response) {
$response->addHeader("Content-type", "text/html; charset=" . self::$encoding);
$response->addHeader("Vary", "Accept");
$content = $response->getBody();
$content = ereg_replace("<\\?xml[^>]+\\?>\n?",'',$content);
$content = str_replace(array('/>','xml:lang','application/xhtml+xml'),array('>','lang','text/html'), $content);
$content = ereg_replace(']+>', '', $content);
$content = ereg_replace('setBody($content);
}
protected static $disabled;
static function disable() {
self::$disabled = true;
}
}
?>