0); } public static function preferred_accept($types, $explicit_check = FALSE) { $mime_types = array(); $max_q = 0; $preferred = FALSE; foreach (array_unique($types) as $type) { $mime_types[$type] = request::accepts_at_quality($type, $explicit_check); } foreach ($mime_types as $type => $q) { if ($q > $max_q) { $max_q = $q; $preferred = $type; } } return $preferred; } public static function accepts_at_quality($type = NULL, $explicit_check = FALSE) { request::parse_accept_header(); $type = strtolower((string) $type); if (strpos($type, '/') === FALSE) { $q = 0; foreach ((array) Kohana::config('mimes.'.$type) as $type) { $q2 = request::accepts_at_quality($type, $explicit_check); $q = ($q2 > $q) ? $q2 : $q; } return $q; } $type = explode('/', $type, 2); if (isset(request::$accept_types[$type[0]][$type[1]])) return request::$accept_types[$type[0]][$type[1]]; if ($explicit_check === FALSE AND isset(request::$accept_types[$type[0]]['*'])) return request::$accept_types[$type[0]]['*']; if ($explicit_check === FALSE AND isset(request::$accept_types['*']['*'])) return request::$accept_types['*']['*']; return 0; } protected static function parse_accept_header() { if (request::$accept_types !== NULL) return; request::$accept_types = array(); if (empty($_SERVER['HTTP_ACCEPT'])) { request::$accept_types['*']['*'] = 1; return; } foreach (explode(',', str_replace(array("\r", "\n"), '', $_SERVER['HTTP_ACCEPT'])) as $accept_entry) { $accept_entry = explode(';', trim($accept_entry), 2); $type = explode('/', $accept_entry[0], 2); if ( ! isset($type[1])) continue; $q = (isset($accept_entry[1]) AND preg_match('~\bq\s*+=\s*+([.0-9]+)~', $accept_entry[1], $match)) ? (float) $match[1] : 1; if ( ! isset(request::$accept_types[$type[0]][$type[1]]) OR $q > request::$accept_types[$type[0]][$type[1]]) { request::$accept_types[$type[0]][$type[1]] = $q; } } } }