|
|
@@ -694,26 +694,7 @@ class CakeRequest implements ArrayAccess {
|
|
|
* @return array An array of prefValue => array(content/types)
|
|
|
*/
|
|
|
public function parseAccept() {
|
|
|
- $accept = array();
|
|
|
- $header = explode(',', $this->header('accept'));
|
|
|
- foreach (array_filter($header) as $value) {
|
|
|
- $prefPos = strpos($value, ';');
|
|
|
- if ($prefPos !== false) {
|
|
|
- $prefValue = substr($value, strpos($value, '=') + 1);
|
|
|
- $value = trim(substr($value, 0, $prefPos));
|
|
|
- } else {
|
|
|
- $prefValue = '1.0';
|
|
|
- $value = trim($value);
|
|
|
- }
|
|
|
- if (!isset($accept[$prefValue])) {
|
|
|
- $accept[$prefValue] = array();
|
|
|
- }
|
|
|
- if ($prefValue) {
|
|
|
- $accept[$prefValue][] = $value;
|
|
|
- }
|
|
|
- }
|
|
|
- krsort($accept);
|
|
|
- return $accept;
|
|
|
+ return $this->_parseAcceptWithQualifier($this->header('accept'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -731,17 +712,50 @@ class CakeRequest implements ArrayAccess {
|
|
|
* @return If a $language is provided, a boolean. Otherwise the array of accepted languages.
|
|
|
*/
|
|
|
public static function acceptLanguage($language = null) {
|
|
|
- $accepts = preg_split('/[;,]/', self::header('Accept-Language'));
|
|
|
- foreach ($accepts as &$accept) {
|
|
|
- $accept = strtolower($accept);
|
|
|
- if (strpos($accept, '_') !== false) {
|
|
|
- $accept = str_replace('_', '-', $accept);
|
|
|
+ $raw = self::_parseAcceptWithQualifier(self::header('Accept-Language'));
|
|
|
+ $accept = array();
|
|
|
+ foreach ($raw as $qualifier => $languages) {
|
|
|
+ foreach ($languages as &$lang) {
|
|
|
+ if (strpos($lang, '_')) {
|
|
|
+ $lang = str_replace('_', '-', $lang);
|
|
|
+ }
|
|
|
+ $lang = strtolower($lang);
|
|
|
}
|
|
|
+ $accept = array_merge($accept, $languages);
|
|
|
}
|
|
|
if ($language === null) {
|
|
|
- return $accepts;
|
|
|
+ return $accept;
|
|
|
+ }
|
|
|
+ return in_array(strtolower($language), $accept);
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
+ * Parse Accept* headers with qualifier options
|
|
|
+ *
|
|
|
+ * @param string $header
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ protected static function _parseAcceptWithQualifier($header) {
|
|
|
+ $accept = array();
|
|
|
+ $header = explode(',', $header);
|
|
|
+ foreach (array_filter($header) as $value) {
|
|
|
+ $prefPos = strpos($value, ';');
|
|
|
+ if ($prefPos !== false) {
|
|
|
+ $prefValue = substr($value, strpos($value, '=') + 1);
|
|
|
+ $value = trim(substr($value, 0, $prefPos));
|
|
|
+ } else {
|
|
|
+ $prefValue = '1.0';
|
|
|
+ $value = trim($value);
|
|
|
+ }
|
|
|
+ if (!isset($accept[$prefValue])) {
|
|
|
+ $accept[$prefValue] = array();
|
|
|
+ }
|
|
|
+ if ($prefValue) {
|
|
|
+ $accept[$prefValue][] = $value;
|
|
|
+ }
|
|
|
}
|
|
|
- return in_array($language, $accepts);
|
|
|
+ krsort($accept);
|
|
|
+ return $accept;
|
|
|
}
|
|
|
|
|
|
/**
|