2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2008-09-16 16:29:00 +02:00
|
|
|
* The content negotiator performs "text/html" or "application/xhtml+xml" switching.
|
2012-09-19 12:07:39 +02:00
|
|
|
* It does this through the public static function ContentNegotiator::process().
|
2008-09-16 16:29:00 +02:00
|
|
|
* By default, ContentNegotiator will comply to the Accept headers the clients
|
|
|
|
* sends along with the HTTP request, which is most likely "application/xhtml+xml"
|
|
|
|
* (see "Order of selection" below).
|
|
|
|
*
|
|
|
|
* Order of selection between html or xhtml is as follows:
|
|
|
|
* - if PHP has already sent the HTTP headers, default to "html" (we can't send HTTP Content-Type headers any longer)
|
|
|
|
* - if a GET variable ?forceFormat is set, it takes precedence (for testing purposes)
|
|
|
|
* - if the user agent is detected as W3C Validator we always deliver "xhtml"
|
|
|
|
* - if an HTTP Accept header is sent from the client, we respect its order (this is the most common case)
|
|
|
|
* - if none of the above matches, fallback is "html"
|
|
|
|
*
|
|
|
|
* ContentNegotiator doesn't enable you to send content as a true XML document
|
|
|
|
* through the "text/xml" or "application/xhtml+xml" Content-Type.
|
|
|
|
* Please see http://webkit.org/blog/68/understanding-html-xml-and-xhtml/ for further information.
|
2008-03-03 00:24:10 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage control
|
2008-09-16 16:29:00 +02:00
|
|
|
*
|
|
|
|
* @todo Check for correct XHTML doctype in xhtml()
|
|
|
|
* @todo Allow for other HTML4 doctypes (e.g. Transitional) in html()
|
2008-09-16 16:36:31 +02:00
|
|
|
* @todo Make content replacement and doctype setting two separately configurable behaviours - some
|
|
|
|
* devs might know what they're doing and don't want contentnegotiator messing with their HTML4 doctypes,
|
|
|
|
* but still find it useful to have self-closing tags removed.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ContentNegotiator {
|
2009-11-26 23:12:40 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected static $encoding = 'utf-8';
|
2009-11-26 23:12:40 +01:00
|
|
|
|
|
|
|
protected static $enabled = false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Set the character set encoding for this page. By default it's utf-8, but you could change it to, say,
|
|
|
|
* windows-1252, to improve interoperability with extended characters being imported from windows excel.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_encoding($encoding) {
|
2007-07-19 12:40:28 +02:00
|
|
|
self::$encoding = $encoding;
|
|
|
|
}
|
2009-11-26 23:16:02 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Return the character encoding set bhy ContentNegotiator::set_encoding(). It's recommended that all classes
|
|
|
|
* that need to specify the character set make use of this function.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_encoding() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return self::$encoding;
|
|
|
|
}
|
2009-11-26 23:16:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable content negotiation for all templates, not just those with the xml header.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function enable() {
|
2009-11-26 23:16:02 +01:00
|
|
|
self::$enabled = true;
|
|
|
|
}
|
2010-10-13 03:07:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable content negotiation for all templates, not just those with the xml header.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function disable() {
|
2010-10-13 03:07:35 +02:00
|
|
|
self::$enabled = false;
|
|
|
|
}
|
2009-11-26 23:16:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if negotation is enabled for the given response.
|
|
|
|
* By default, negotiation is only enabled for pages that have the xml header.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function enabled_for($response) {
|
2009-11-26 23:16:02 +01:00
|
|
|
$contentType = $response->getHeader("Content-Type");
|
|
|
|
|
|
|
|
// Disable content negotation for other content types
|
2012-09-26 23:34:00 +02:00
|
|
|
if($contentType && substr($contentType, 0,9) != 'text/html'
|
|
|
|
&& substr($contentType, 0,21) != 'application/xhtml+xml') {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-26 23:16:02 +01:00
|
|
|
|
|
|
|
if(self::$enabled) return true;
|
|
|
|
else return (substr($response->getBody(),0,5) == '<' . '?xml');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function process(SS_HTTPResponse $response) {
|
2009-01-05 07:19:48 +01:00
|
|
|
if(!self::enabled_for($response)) return;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$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 {
|
2012-09-26 23:34:00 +02:00
|
|
|
// 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.
|
2008-02-25 03:10:37 +01:00
|
|
|
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;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if($q) {
|
|
|
|
// Get the preferred format
|
|
|
|
krsort($q);
|
|
|
|
$chosenFormat = reset($q);
|
|
|
|
} else {
|
|
|
|
$chosenFormat = "html";
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$negotiator = new ContentNegotiator();
|
2007-08-17 05:09:46 +02:00
|
|
|
$negotiator->$chosenFormat( $response );
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-09-16 16:29:00 +02:00
|
|
|
/**
|
|
|
|
* Only sends the HTTP Content-Type as "application/xhtml+xml"
|
|
|
|
* if the template starts with the typical "<?xml" Pragma.
|
|
|
|
* Assumes that a correct doctype is set, and doesn't change or append to it.
|
|
|
|
* Replaces a few common tags and entities with their XHTML representations (<br>, <img>, ).
|
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @param $response SS_HTTPResponse
|
2008-09-16 16:29:00 +02:00
|
|
|
* @return string
|
|
|
|
* @todo More flexible tag and entity parsing through regular expressions or tag definition lists
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function xhtml(SS_HTTPResponse $response) {
|
2007-08-17 05:09:46 +02:00
|
|
|
$content = $response->getBody();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Only serve "pure" XHTML if the XML header is present
|
2007-08-17 05:09:46 +02:00
|
|
|
if(substr($content,0,5) == '<' . '?xml' ) {
|
2009-02-02 00:49:53 +01:00
|
|
|
$response->addHeader("Content-Type", "application/xhtml+xml; charset=" . self::$encoding);
|
2007-08-17 05:09:46 +02:00
|
|
|
$response->addHeader("Vary" , "Accept");
|
2009-10-31 01:16:54 +01:00
|
|
|
|
|
|
|
// Fix base tag
|
2010-10-19 03:20:07 +02:00
|
|
|
$content = preg_replace('/<base href="([^"]*)"><!--\[if[[^\]*]\] \/><!\[endif\]-->/',
|
|
|
|
'<base href="$1" />', $content);
|
2007-08-17 05:09:46 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$content = str_replace(' ',' ', $content);
|
|
|
|
$content = str_replace('<br>','<br />', $content);
|
2012-02-27 22:14:02 +01:00
|
|
|
$content = preg_replace('#(<img[^>]*[^/>])>#i', '\\1/>', $content);
|
2007-08-17 05:09:46 +02:00
|
|
|
|
|
|
|
$response->setBody($content);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
} else {
|
2007-08-17 05:09:46 +02:00
|
|
|
return $this->html($response);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-09-16 16:29:00 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sends HTTP Content-Type as "text/html", and replaces existing doctypes with
|
|
|
|
* HTML4.01 Strict.
|
|
|
|
* Replaces self-closing tags like <img /> with unclosed solitary tags like <img>.
|
|
|
|
* Replaces all occurrences of "application/xhtml+xml" with "text/html" in the template.
|
|
|
|
* Removes "xmlns" attributes and any <?xml> Pragmas.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function html(SS_HTTPResponse $response) {
|
2009-02-02 00:49:53 +01:00
|
|
|
$response->addHeader("Content-Type", "text/html; charset=" . self::$encoding);
|
2007-08-17 05:09:46 +02:00
|
|
|
$response->addHeader("Vary", "Accept");
|
|
|
|
|
|
|
|
$content = $response->getBody();
|
2009-01-05 07:19:48 +01:00
|
|
|
$hasXMLHeader = (substr($content,0,5) == '<' . '?xml' );
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-10-31 01:16:54 +01:00
|
|
|
// Fix base tag
|
2010-10-19 03:20:07 +02:00
|
|
|
$content = preg_replace('/<base href="([^"]*)" \/>/',
|
2009-10-31 01:16:54 +01:00
|
|
|
'<base href="$1"><!--[if lte IE 6]></base><![endif]-->', $content);
|
|
|
|
|
2012-02-27 22:14:02 +01:00
|
|
|
$content = preg_replace("#<\\?xml[^>]+\\?>\n?#", '', $content);
|
2007-07-19 12:40:28 +02:00
|
|
|
$content = str_replace(array('/>','xml:lang','application/xhtml+xml'),array('>','lang','text/html'), $content);
|
2009-01-05 07:19:48 +01:00
|
|
|
|
|
|
|
// Only replace the doctype in templates with the xml header
|
|
|
|
if($hasXMLHeader) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$content = preg_replace('/<!DOCTYPE[^>]+>/',
|
|
|
|
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
|
|
|
|
$content);
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
$content = preg_replace('/<html xmlns="[^"]+"/','<html ', $content);
|
2007-08-17 05:09:46 +02:00
|
|
|
|
|
|
|
$response->setBody($content);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-11-27 02:43:14 +01:00
|
|
|
|
|
|
|
}
|