aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/network.php38
1 files changed, 38 insertions, 0 deletions
diff --git a/include/network.php b/include/network.php
index b85a16d21..fe9a9aa2b 100644
--- a/include/network.php
+++ b/include/network.php
@@ -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;
+} \ No newline at end of file