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

Reformat the new \SimpleSAML\Locale\Language class.

parent 6389e5c1
No related branches found
No related tags found
No related merge requests found
...@@ -10,14 +10,14 @@ ...@@ -10,14 +10,14 @@
namespace SimpleSAML\Locale; namespace SimpleSAML\Locale;
class Language { class Language
{
/** /**
* This is the default language map. It is used to map languages codes from the user agent to other language codes. * This is the default language map. It is used to map languages codes from the user agent to other language codes.
*/ */
private static $defaultLanguageMap = array('nb' => 'no'); private static $defaultLanguageMap = array('nb' => 'no');
private $configuration = null; private $configuration = null;
private $availableLanguages = array('en'); private $availableLanguages = array('en');
private $language = null; private $language = null;
...@@ -34,14 +34,18 @@ class Language { ...@@ -34,14 +34,18 @@ class Language {
* *
* @param \SimpleSAML_Configuration $configuration Configuration object * @param \SimpleSAML_Configuration $configuration Configuration object
*/ */
function __construct(\SimpleSAML_Configuration $configuration) { public function __construct(\SimpleSAML_Configuration $configuration)
{
$this->configuration = $configuration; $this->configuration = $configuration;
$this->availableLanguages = $this->configuration->getArray('language.available', array('en')); $this->availableLanguages = $this->configuration->getArray('language.available', array('en'));
$this->languageParameterName = $this->configuration->getString('language.parameter.name', 'language'); $this->languageParameterName = $this->configuration->getString('language.parameter.name', 'language');
if (isset($_GET[$this->languageParameterName])) { if (isset($_GET[$this->languageParameterName])) {
$this->setLanguage($_GET[$this->languageParameterName], $this->configuration->getBoolean('language.parameter.setcookie', TRUE)); $this->setLanguage(
$_GET[$this->languageParameterName],
$this->configuration->getBoolean('language.parameter.setcookie', true)
);
} }
} }
...@@ -49,19 +53,21 @@ class Language { ...@@ -49,19 +53,21 @@ class Language {
/** /**
* This method will set a cookie for the user's browser to remember what language was selected. * This method will set a cookie for the user's browser to remember what language was selected.
* *
* @param string $language Language code for the language to set. * @param string $language Language code for the language to set.
* @param boolean $setLanguageCookie Whether to set the language cookie or not. Defaults to true. * @param boolean $setLanguageCookie Whether to set the language cookie or not. Defaults to true.
*/ */
public function setLanguage($language, $setLanguageCookie = TRUE) { public function setLanguage($language, $setLanguageCookie = true)
{
$language = strtolower($language); $language = strtolower($language);
if (in_array($language, $this->availableLanguages, TRUE)) { if (in_array($language, $this->availableLanguages, true)) {
$this->language = $language; $this->language = $language;
if ($setLanguageCookie === TRUE) { if ($setLanguageCookie === true) {
Language::setLanguageCookie($language); Language::setLanguageCookie($language);
} }
} }
} }
/** /**
* This method will return the language selected by the user, or the default language. It looks first for a cached * This method will return the language selected by the user, or the default language. It looks first for a cached
* language code, then checks for a language cookie, then it tries to calculate the preferred language from HTTP * language code, then checks for a language cookie, then it tries to calculate the preferred language from HTTP
...@@ -70,37 +76,37 @@ class Language { ...@@ -70,37 +76,37 @@ class Language {
* @return string The language selected by the user according to the processing rules specified, or the default * @return string The language selected by the user according to the processing rules specified, or the default
* language in any other case. * language in any other case.
*/ */
public function getLanguage() { public function getLanguage()
{
// Language is set in object // language is set in object
if (isset($this->language)) { if (isset($this->language)) {
return $this->language; return $this->language;
} }
// Run custom getLanguage function if defined // run custom getLanguage function if defined
$customFunction = $this->configuration->getArray('language.get_language_function', NULL); $customFunction = $this->configuration->getArray('language.get_language_function', null);
if (isset($customFunction)) { if (isset($customFunction)) {
assert('is_callable($customFunction)'); assert('is_callable($customFunction)');
$customLanguage = call_user_func($customFunction, $this); $customLanguage = call_user_func($customFunction, $this);
if ($customLanguage !== NULL && $customLanguage !== FALSE) { if ($customLanguage !== null && $customLanguage !== false) {
return $customLanguage; return $customLanguage;
} }
} }
// Language is provided in a stored COOKIE // language is provided in a stored cookie
$languageCookie = Language::getLanguageCookie(); $languageCookie = Language::getLanguageCookie();
if ($languageCookie !== NULL) { if ($languageCookie !== null) {
$this->language = $languageCookie; $this->language = $languageCookie;
return $languageCookie; return $languageCookie;
} }
/* Check if we can find a good language from the Accept-Language http header. */ // check if we can find a good language from the Accept-Language HTTP header
$httpLanguage = $this->getHTTPLanguage(); $httpLanguage = $this->getHTTPLanguage();
if ($httpLanguage !== NULL) { if ($httpLanguage !== null) {
return $httpLanguage; return $httpLanguage;
} }
// Language is not set, and we get the default language from the configuration. // language is not set, and we get the default language from the configuration
return $this->getDefaultLanguage(); return $this->getDefaultLanguage();
} }
...@@ -111,36 +117,34 @@ class Language { ...@@ -111,36 +117,34 @@ class Language {
* @return string The preferred language based on the Accept-Language HTTP header, or null if none of the languages * @return string The preferred language based on the Accept-Language HTTP header, or null if none of the languages
* in the header is available. * in the header is available.
*/ */
private function getHTTPLanguage() { private function getHTTPLanguage()
{
$languageScore = \SimpleSAML_Utilities::getAcceptLanguage(); $languageScore = \SimpleSAML_Utilities::getAcceptLanguage();
/* For now we only use the default language map. We may use a configurable language map // for now we only use the default language map. We may use a configurable language map in the future
* in the future.
*/
$languageMap = self::$defaultLanguageMap; $languageMap = self::$defaultLanguageMap;
/* Find the available language with the best score. */ // find the available language with the best score
$bestLanguage = NULL; $bestLanguage = null;
$bestScore = -1.0; $bestScore = -1.0;
foreach($languageScore as $language => $score) { foreach ($languageScore as $language => $score) {
/* Apply the language map to the language code. */ // apply the language map to the language code
if(array_key_exists($language, $languageMap)) { if (array_key_exists($language, $languageMap)) {
$language = $languageMap[$language]; $language = $languageMap[$language];
} }
if(!in_array($language, $this->availableLanguages, TRUE)) { if (!in_array($language, $this->availableLanguages, true)) {
/* Skip this language - we don't have it. */ // skip this language - we don't have it
continue; continue;
} }
/* Some user agents use very limited precicion of the quality value, but order the /* Some user agents use very limited precicion of the quality value, but order the elements in descending
* elements in descending order. Therefore we rely on the order of the output from * order. Therefore we rely on the order of the output from getAcceptLanguage() matching the order of the
* getAcceptLanguage() matching the order of the languages in the header when two * languages in the header when two languages have the same quality.
* languages have the same quality.
*/ */
if($score > $bestScore) { if ($score > $bestScore) {
$bestLanguage = $language; $bestLanguage = $language;
$bestScore = $score; $bestScore = $score;
} }
...@@ -155,36 +159,41 @@ class Language { ...@@ -155,36 +159,41 @@ class Language {
* *
* @return string The default language that has been configured. Defaults to english if not configured. * @return string The default language that has been configured. Defaults to english if not configured.
*/ */
public function getDefaultLanguage() { public function getDefaultLanguage()
{
return $this->configuration->getString('language.default', 'en'); return $this->configuration->getString('language.default', 'en');
} }
/** /**
* Return a list of all languages available. * Return a list of all languages available.
* *
* @return array An array holding all the languages available. * @return array An array holding all the languages available.
*/ */
public function getLanguageList() { public function getLanguageList()
{
$thisLang = $this->getLanguage(); $thisLang = $this->getLanguage();
$lang = array(); $lang = array();
foreach ($this->availableLanguages AS $nl) { foreach ($this->availableLanguages as $nl) {
$lang[$nl] = ($nl == $thisLang); $lang[$nl] = ($nl == $thisLang);
} }
return $lang; return $lang;
} }
/** /**
* Check whether a language is right-to-left or not. * Check whether a language is right-to-left or not.
* *
* @return boolean True if the language is right-to-left, false otherwise. * @return boolean True if the language is right-to-left, false otherwise.
*/ */
public function isLanguageRTL() { public function isLanguageRTL()
{
$rtlLanguages = $this->configuration->getArray('language.rtl', array()); $rtlLanguages = $this->configuration->getArray('language.rtl', array());
$thisLang = $this->getLanguage(); $thisLang = $this->getLanguage();
if (in_array($thisLang, $rtlLanguages)) { if (in_array($thisLang, $rtlLanguages)) {
return TRUE; return true;
} }
return FALSE; return false;
} }
...@@ -193,19 +202,20 @@ class Language { ...@@ -193,19 +202,20 @@ class Language {
* *
* @return string|null The selected language or null if unset. * @return string|null The selected language or null if unset.
*/ */
public static function getLanguageCookie() { public static function getLanguageCookie()
{
$config = \SimpleSAML_Configuration::getInstance(); $config = \SimpleSAML_Configuration::getInstance();
$availableLanguages = $config->getArray('language.available', array('en')); $availableLanguages = $config->getArray('language.available', array('en'));
$name = $config->getString('language.cookie.name', 'language'); $name = $config->getString('language.cookie.name', 'language');
if (isset($_COOKIE[$name])) { if (isset($_COOKIE[$name])) {
$language = strtolower((string)$_COOKIE[$name]); $language = strtolower((string) $_COOKIE[$name]);
if (in_array($language, $availableLanguages, TRUE)) { if (in_array($language, $availableLanguages, true)) {
return $language; return $language;
} }
} }
return NULL; return null;
} }
...@@ -215,26 +225,26 @@ class Language { ...@@ -215,26 +225,26 @@ class Language {
* *
* @param string $language The language set by the user. * @param string $language The language set by the user.
*/ */
public static function setLanguageCookie($language) { public static function setLanguageCookie($language)
{
assert('is_string($language)'); assert('is_string($language)');
$language = strtolower($language); $language = strtolower($language);
$config = \SimpleSAML_Configuration::getInstance(); $config = \SimpleSAML_Configuration::getInstance();
$availableLanguages = $config->getArray('language.available', array('en')); $availableLanguages = $config->getArray('language.available', array('en'));
if (!in_array($language, $availableLanguages, TRUE) || headers_sent()) { if (!in_array($language, $availableLanguages, true) || headers_sent()) {
return; return;
} }
$name = $config->getString('language.cookie.name', 'language'); $name = $config->getString('language.cookie.name', 'language');
$params = array( $params = array(
'lifetime' => ($config->getInteger('language.cookie.lifetime', 60*60*24*900)), 'lifetime' => ($config->getInteger('language.cookie.lifetime', 60 * 60 * 24 * 900)),
'domain' => ($config->getString('language.cookie.domain', NULL)), 'domain' => ($config->getString('language.cookie.domain', null)),
'path' => ($config->getString('language.cookie.path', '/')), 'path' => ($config->getString('language.cookie.path', '/')),
'httponly' => FALSE, 'httponly' => false,
); );
\SimpleSAML_Utilities::setCookie($name, $language, $params, FALSE); \SimpleSAML_Utilities::setCookie($name, $language, $params, false);
} }
} }
...@@ -115,7 +115,7 @@ class SimpleSAML_XHTML_Template ...@@ -115,7 +115,7 @@ class SimpleSAML_XHTML_Template
$themeName = $tmp[0]; $themeName = $tmp[0];
} }
// First check the current theme // first check the current theme
if ($themeModule !== null) { if ($themeModule !== null) {
// .../module/<themeModule>/themes/<themeName>/<templateModule>/<templateName> // .../module/<themeModule>/themes/<themeName>/<templateModule>/<templateName>
...@@ -133,13 +133,13 @@ class SimpleSAML_XHTML_Template ...@@ -133,13 +133,13 @@ class SimpleSAML_XHTML_Template
return $filename; return $filename;
} }
// Not found in current theme // not found in current theme
SimpleSAML_Logger::debug( SimpleSAML_Logger::debug(
$_SERVER['PHP_SELF'].' - Template: Could not find template file ['. $template.'] at ['. $_SERVER['PHP_SELF'].' - Template: Could not find template file ['. $template.'] at ['.
$filename.'] - now trying the base template' $filename.'] - now trying the base template'
); );
// Try default theme // try default theme
if ($templateModule !== 'default') { if ($templateModule !== 'default') {
// .../module/<templateModule>/templates/<templateName> // .../module/<templateModule>/templates/<templateName>
$filename = SimpleSAML_Module::getModuleDir($templateModule).'/templates/'.$templateName; $filename = SimpleSAML_Module::getModuleDir($templateModule).'/templates/'.$templateName;
...@@ -152,7 +152,7 @@ class SimpleSAML_XHTML_Template ...@@ -152,7 +152,7 @@ class SimpleSAML_XHTML_Template
return $filename; return $filename;
} }
// Not found in default template - log error and throw exception // not found in default template - log error and throw exception
$error = 'Template: Could not find template file ['.$template.'] at ['.$filename.']'; $error = 'Template: Could not find template file ['.$template.'] at ['.$filename.']';
SimpleSAML_Logger::critical($_SERVER['PHP_SELF'].' - '.$error); SimpleSAML_Logger::critical($_SERVER['PHP_SELF'].' - '.$error);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment