provide content-type matching ability for activitypub
This commit is contained in:
parent
0d062251b6
commit
15e836b7dd
@ -1896,3 +1896,41 @@ function service_plink($contact, $guid) {
|
||||
return $x['plink'];
|
||||
}
|
||||
|
||||
function getBestSupportedMimeType($mimeTypes = null, $acceptedTypes = false) {
|
||||
// Values will be stored in this array
|
||||
|
||||
if($acceptedTypes === false)
|
||||
$acceptedTypes = $_SERVER['HTTP_ACCEPT'];
|
||||
|
||||
$AcceptTypes = Array ();
|
||||
|
||||
// Accept header is case insensitive, and whitespace isn’t important
|
||||
$accept = strtolower(str_replace(' ', '', $acceptedTypes));
|
||||
// divide it into parts in the place of a ","
|
||||
$accept = explode(',', $accept);
|
||||
foreach ($accept as $a) {
|
||||
// the default quality is 1.
|
||||
$q = 1;
|
||||
// check if there is a different quality
|
||||
if (strpos($a, ';q=')) {
|
||||
// divide "mime/type;q=X" into two parts: "mime/type" i "X"
|
||||
list($a, $q) = explode(';q=', $a);
|
||||
}
|
||||
// mime-type $a is accepted with the quality $q
|
||||
// WARNING: $q == 0 means, that mime-type isn’t supported!
|
||||
$AcceptTypes[$a] = $q;
|
||||
}
|
||||
arsort($AcceptTypes);
|
||||
|
||||
// if no parameter was passed, just return parsed data
|
||||
if (!$mimeTypes) return $AcceptTypes;
|
||||
|
||||
$mimeTypes = array_map('strtolower', (array)$mimeTypes);
|
||||
|
||||
// let’s check our supported types:
|
||||
foreach ($AcceptTypes as $mime => $q) {
|
||||
if ($q && in_array($mime, $mimeTypes)) return $mime;
|
||||
}
|
||||
// no mime-type found
|
||||
return null;
|
||||
}
|
Reference in New Issue
Block a user