Skip to content
Snippets Groups Projects

Get best content type

Merged Stein Magne Bjorklund requested to merge getBestContentType into master
8 files
+ 206
27
Compare changes
  • Side-by-side
  • Inline
Files
8
+ 32
19
@@ -124,39 +124,50 @@ class Utils
return $obj;
}
public static function getExtension($accept_string)
public static function getExtension($accept_string, array $configuration): string
{
global $conf;
$extension = "html";
foreach ($conf['http_accept'] as $ext => $accept_arr) {
if (in_array($accept_string, $accept_arr)) {
$extension = $ext;
foreach ($configuration as $extension => $extensions) {
if (in_array($accept_string, $extensions, true)) {
return $extension;
}
}
return $extension;
return 'html';
}
public static function getBestContentType($accept_string)
/**
* header from the current request, if there is one.
*
* @param $accept_string
* Contents of the Accept: header from the current request.
* @param array $configuration
* @return int|string
*/
public static function getBestContentType($accept_string, array $configuration)
{
global $conf;
$a = explode(",", $accept_string);
$b = array();
if ($a[0] === 'text/html' || $a[0] === 'text/css' || $a[0] === '*/*') {
return 'text/html';
}
$b = [];
foreach ($a as $v) {
foreach ($conf['http_accept'] as $formatTypeArray) {
if (strstr($v, ";")) {
$aux = explode(";", $v);
foreach ($configuration as $formatTypeArray) {
if (strpos($v, ";") !== false) {
$aux = explode(';', $v);
$aux[0] = trim($aux[0]);
if (in_array($aux[0], $formatTypeArray)) {
$b[$aux[0]] = floatval(trim(str_replace("q=", "", $aux[1])));
if (in_array($aux[0], $formatTypeArray, true)) {
$b[$aux[0]] = (float)trim(str_replace("q=", "", $aux[1]));
}
} else {
$value = trim($v);
if (in_array($value, $formatTypeArray)) {
if (in_array($value, $formatTypeArray, true)) {
$b[$value] = 1.0;
}
}
}
}
$a = $b;
arsort($a);
$ct = 'text/html';
@@ -164,9 +175,11 @@ class Utils
$ct = $k;
break;
}
if ($ct == null || $ct == "" || $ct == "*/*") {
$ct = 'text/html';
if ($ct === null || $ct === "" || $ct === "*/*") {
return 'text/html';
}
return $ct;
}
@@ -250,7 +263,7 @@ class Utils
global $conf;
global $lodspk;
$contentType = $lodspk['contentType'];
$extension = Utils::getExtension($contentType);
$extension = Utils::getExtension($contentType, $conf['http_accept']);
header('Content-Type: ' . $contentType);
if (isset($lodspk['resultRdf']) && $lodspk['resultRdf'] == true) {
Loading