getHeader("Content-Type");
// Disable content negotiation for other content types
if ($contentType
&& substr($contentType ?? '', 0, 9) != 'text/html'
&& substr($contentType ?? '', 0, 21) != 'application/xhtml+xml'
) {
return false;
}
if (ContentNegotiator::getEnabled()) {
return true;
} else {
return (substr($response->getBody() ?? '', 0, 5) == '<' . '?xml');
}
}
/**
* Gets the current enabled status, if it is not set this will fallback to config
*
* @return bool
*/
public static function getEnabled()
{
if (isset(static::$current_enabled)) {
return static::$current_enabled;
}
return Config::inst()->get(static::class, 'enabled');
}
/**
* Sets the current enabled status
*
* @param bool $enabled
*/
public static function setEnabled($enabled)
{
static::$current_enabled = $enabled;
}
/**
* @param HTTPResponse $response
*/
public static function process(HTTPResponse $response)
{
if (!self::enabled_for($response)) {
return;
}
$mimes = [
"xhtml" => "application/xhtml+xml",
"html" => "text/html",
];
$q = [];
if (headers_sent()) {
$chosenFormat = static::config()->get('default_format');
} elseif (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(['+', '/'], ['\+', '\/'], $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 = Config::inst()->get(static::class, 'default_format');
}
}
}
$negotiator = new ContentNegotiator();
$negotiator->$chosenFormat($response);
}
/**
* Check user defined content type and use it, if it's empty use the strict application/xhtml+xml.
* Replaces a few common tags and entities with their XHTML representations (
, ,
* , checked, selected).
*
* @param HTTPResponse $response
*
* @todo Search for more xhtml replacement
*/
public function xhtml(HTTPResponse $response)
{
$content = $response->getBody();
$encoding = Config::inst()->get('SilverStripe\\Control\\ContentNegotiator', 'encoding');
$contentType = Config::inst()->get('SilverStripe\\Control\\ContentNegotiator', 'content_type');
if (empty($contentType)) {
$response->addHeader("Content-Type", "application/xhtml+xml; charset=" . $encoding);
} else {
$response->addHeader("Content-Type", $contentType . "; charset=" . $encoding);
}
$response->addHeader("Vary", "Accept");
// Fix base tag
$content = preg_replace(
'/
', '
', $content ?? '');
$content = str_replace('