Skip to content
Snippets Groups Projects
Commit 7d723a1d authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Move SimpleSAML_Utilities:::getAcceptLanguage() to...

Move SimpleSAML_Utilities:::getAcceptLanguage() to SimpleSAML\Utils\HTTP::getAcceptLanguage() and deprecate the former.
parent 4b77d2c6
No related branches found
No related tags found
No related merge requests found
......@@ -522,71 +522,10 @@ class SimpleSAML_Utilities {
/**
* This function parses the Accept-Language http header and returns an associative array with each
* language and the score for that language.
*
* If an language includes a region, then the result will include both the language with the region
* and the language without the region.
*
* The returned array will be in the same order as the input.
*
* @return An associative array with each language and the score for that language.
* @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML\Utils\HTTP::getAcceptLanguage() instead.
*/
public static function getAcceptLanguage() {
if(!array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
/* No Accept-Language header - return empty set. */
return array();
}
$languages = explode(',', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
$ret = array();
foreach($languages as $l) {
$opts = explode(';', $l);
$l = trim(array_shift($opts)); /* The language is the first element.*/
$q = 1.0;
/* Iterate over all options, and check for the quality option. */
foreach($opts as $o) {
$o = explode('=', $o);
if(count($o) < 2) {
/* Skip option with no value. */
continue;
}
$name = trim($o[0]);
$value = trim($o[1]);
if($name === 'q') {
$q = (float)$value;
}
}
/* Remove the old key to ensure that the element is added to the end. */
unset($ret[$l]);
/* Set the quality in the result. */
$ret[$l] = $q;
if(strpos($l, '-')) {
/* The language includes a region part. */
/* Extract the language without the region. */
$l = explode('-', $l);
$l = $l[0];
/* Add this language to the result (unless it is defined already). */
if(!array_key_exists($l, $ret)) {
$ret[$l] = $q;
}
}
}
return $ret;
return \SimpleSAML\Utils\HTTP::getAcceptLanguage();
}
......
......@@ -207,6 +207,74 @@ class HTTP
}
/**
* This function parses the Accept-Language HTTP header and returns an associative array with each language and the
* score for that language. If a language includes a region, then the result will include both the language with
* the region and the language without the region.
*
* The returned array will be in the same order as the input.
*
* @return array An associative array with each language and the score for that language.
*
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
*/
public static function getAcceptLanguage()
{
if (!array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
// no Accept-Language header, return an empty set
return array();
}
$languages = explode(',', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
$ret = array();
foreach ($languages as $l) {
$opts = explode(';', $l);
$l = trim(array_shift($opts)); // the language is the first element
$q = 1.0;
// iterate over all options, and check for the quality option
foreach ($opts as $o) {
$o = explode('=', $o);
if (count($o) < 2) {
// skip option with no value
continue;
}
$name = trim($o[0]);
$value = trim($o[1]);
if ($name === 'q') {
$q = (float) $value;
}
}
// remove the old key to ensure that the element is added to the end
unset($ret[$l]);
// set the quality in the result
$ret[$l] = $q;
if (strpos($l, '-')) {
// the language includes a region part
// extract the language without the region
$l = explode('-', $l);
$l = $l[0];
// add this language to the result (unless it is defined already)
if (!array_key_exists($l, $ret)) {
$ret[$l] = $q;
}
}
}
return $ret;
}
/**
* Retrieve the base URL of the SimpleSAMLphp installation. The URL will always end with a '/'. For example:
* https://idp.example.org/simplesaml/
......
......@@ -141,7 +141,7 @@ class SimpleSAML_XHTML_Template {
* languages in the header were available.
*/
private function getHTTPLanguage() {
$languageScore = SimpleSAML_Utilities::getAcceptLanguage();
$languageScore = \SimpleSAML\Utils\HTTP::getAcceptLanguage();
/* For now we only use the default language map. We may use a configurable language map
* in the future.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment