summaryrefslogtreecommitdiffstats
path: root/lib/SimpleSAML
diff options
context:
space:
mode:
authorJaime Perez Crespo <jaime.perez@uninett.no>2016-02-05 13:34:23 +0100
committerJaime Perez Crespo <jaime.perez@uninett.no>2016-02-05 13:34:23 +0100
commit79576e09f1a80b7749be871d42376d07f46204be (patch)
treefe8546028248c6c9f9e3178b295c57b61a9f210c /lib/SimpleSAML
parent6389e5c14d32d134ad94e4b72996de90b0786ea8 (diff)
downloadsimplesamlphp-79576e09f1a80b7749be871d42376d07f46204be.zip
simplesamlphp-79576e09f1a80b7749be871d42376d07f46204be.tar.gz
simplesamlphp-79576e09f1a80b7749be871d42376d07f46204be.tar.bz2
Reformat the new \SimpleSAML\Locale\Language class.
Diffstat (limited to 'lib/SimpleSAML')
-rw-r--r--lib/SimpleSAML/Locale/Language.php116
-rw-r--r--lib/SimpleSAML/XHTML/Template.php8
2 files changed, 67 insertions, 57 deletions
diff --git a/lib/SimpleSAML/Locale/Language.php b/lib/SimpleSAML/Locale/Language.php
index 11e5b6a..8f7695c 100644
--- a/lib/SimpleSAML/Locale/Language.php
+++ b/lib/SimpleSAML/Locale/Language.php
@@ -10,14 +10,14 @@
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.
*/
private static $defaultLanguageMap = array('nb' => 'no');
-
private $configuration = null;
private $availableLanguages = array('en');
private $language = null;
@@ -34,14 +34,18 @@ class Language {
*
* @param \SimpleSAML_Configuration $configuration Configuration object
*/
- function __construct(\SimpleSAML_Configuration $configuration) {
+ public function __construct(\SimpleSAML_Configuration $configuration)
+ {
$this->configuration = $configuration;
$this->availableLanguages = $this->configuration->getArray('language.available', array('en'));
$this->languageParameterName = $this->configuration->getString('language.parameter.name', 'language');
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 {
/**
* 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.
*/
- public function setLanguage($language, $setLanguageCookie = TRUE) {
+ public function setLanguage($language, $setLanguageCookie = true)
+ {
$language = strtolower($language);
- if (in_array($language, $this->availableLanguages, TRUE)) {
+ if (in_array($language, $this->availableLanguages, true)) {
$this->language = $language;
- if ($setLanguageCookie === TRUE) {
+ if ($setLanguageCookie === true) {
Language::setLanguageCookie($language);
}
}
}
+
/**
* 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
@@ -70,37 +76,37 @@ class Language {
* @return string The language selected by the user according to the processing rules specified, or the default
* language in any other case.
*/
- public function getLanguage() {
-
- // Language is set in object
+ public function getLanguage()
+ {
+ // language is set in object
if (isset($this->language)) {
return $this->language;
}
- // Run custom getLanguage function if defined
- $customFunction = $this->configuration->getArray('language.get_language_function', NULL);
+ // run custom getLanguage function if defined
+ $customFunction = $this->configuration->getArray('language.get_language_function', null);
if (isset($customFunction)) {
assert('is_callable($customFunction)');
$customLanguage = call_user_func($customFunction, $this);
- if ($customLanguage !== NULL && $customLanguage !== FALSE) {
+ if ($customLanguage !== null && $customLanguage !== false) {
return $customLanguage;
}
}
- // Language is provided in a stored COOKIE
+ // language is provided in a stored cookie
$languageCookie = Language::getLanguageCookie();
- if ($languageCookie !== NULL) {
+ if ($languageCookie !== null) {
$this->language = $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();
- if ($httpLanguage !== NULL) {
+ if ($httpLanguage !== null) {
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();
}
@@ -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
* in the header is available.
*/
- private function getHTTPLanguage() {
+ private function getHTTPLanguage()
+ {
$languageScore = \SimpleSAML_Utilities::getAcceptLanguage();
- /* For now we only use the default language map. We may use a configurable language map
- * in the future.
- */
+ // for now we only use the default language map. We may use a configurable language map in the future
$languageMap = self::$defaultLanguageMap;
- /* Find the available language with the best score. */
- $bestLanguage = NULL;
+ // find the available language with the best score
+ $bestLanguage = null;
$bestScore = -1.0;
- foreach($languageScore as $language => $score) {
+ foreach ($languageScore as $language => $score) {
- /* Apply the language map to the language code. */
- if(array_key_exists($language, $languageMap)) {
+ // apply the language map to the language code
+ if (array_key_exists($language, $languageMap)) {
$language = $languageMap[$language];
}
- if(!in_array($language, $this->availableLanguages, TRUE)) {
- /* Skip this language - we don't have it. */
+ if (!in_array($language, $this->availableLanguages, true)) {
+ // skip this language - we don't have it
continue;
}
- /* Some user agents use very limited precicion of the quality value, but order the
- * elements in descending order. Therefore we rely on the order of the output from
- * getAcceptLanguage() matching the order of the languages in the header when two
- * languages have the same quality.
+ /* Some user agents use very limited precicion of the quality value, but order the elements in descending
+ * order. Therefore we rely on the order of the output from getAcceptLanguage() matching the order of the
+ * languages in the header when two languages have the same quality.
*/
- if($score > $bestScore) {
+ if ($score > $bestScore) {
$bestLanguage = $language;
$bestScore = $score;
}
@@ -155,36 +159,41 @@ class Language {
*
* @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 a list of all languages available.
*
* @return array An array holding all the languages available.
*/
- public function getLanguageList() {
+ public function getLanguageList()
+ {
$thisLang = $this->getLanguage();
$lang = array();
- foreach ($this->availableLanguages AS $nl) {
+ foreach ($this->availableLanguages as $nl) {
$lang[$nl] = ($nl == $thisLang);
}
return $lang;
}
+
/**
* Check whether a language is right-to-left or not.
*
* @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());
$thisLang = $this->getLanguage();
if (in_array($thisLang, $rtlLanguages)) {
- return TRUE;
+ return true;
}
- return FALSE;
+ return false;
}
@@ -193,19 +202,20 @@ class Language {
*
* @return string|null The selected language or null if unset.
*/
- public static function getLanguageCookie() {
+ public static function getLanguageCookie()
+ {
$config = \SimpleSAML_Configuration::getInstance();
$availableLanguages = $config->getArray('language.available', array('en'));
$name = $config->getString('language.cookie.name', 'language');
if (isset($_COOKIE[$name])) {
- $language = strtolower((string)$_COOKIE[$name]);
- if (in_array($language, $availableLanguages, TRUE)) {
+ $language = strtolower((string) $_COOKIE[$name]);
+ if (in_array($language, $availableLanguages, true)) {
return $language;
}
}
- return NULL;
+ return null;
}
@@ -215,26 +225,26 @@ class Language {
*
* @param string $language The language set by the user.
*/
- public static function setLanguageCookie($language) {
+ public static function setLanguageCookie($language)
+ {
assert('is_string($language)');
$language = strtolower($language);
$config = \SimpleSAML_Configuration::getInstance();
$availableLanguages = $config->getArray('language.available', array('en'));
- if (!in_array($language, $availableLanguages, TRUE) || headers_sent()) {
+ if (!in_array($language, $availableLanguages, true) || headers_sent()) {
return;
}
$name = $config->getString('language.cookie.name', 'language');
$params = array(
- 'lifetime' => ($config->getInteger('language.cookie.lifetime', 60*60*24*900)),
- 'domain' => ($config->getString('language.cookie.domain', NULL)),
- 'path' => ($config->getString('language.cookie.path', '/')),
- 'httponly' => FALSE,
+ 'lifetime' => ($config->getInteger('language.cookie.lifetime', 60 * 60 * 24 * 900)),
+ 'domain' => ($config->getString('language.cookie.domain', null)),
+ 'path' => ($config->getString('language.cookie.path', '/')),
+ 'httponly' => false,
);
- \SimpleSAML_Utilities::setCookie($name, $language, $params, FALSE);
+ \SimpleSAML_Utilities::setCookie($name, $language, $params, false);
}
-
}
diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index c2ee848..fd1d44e 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -115,7 +115,7 @@ class SimpleSAML_XHTML_Template
$themeName = $tmp[0];
}
- // First check the current theme
+ // first check the current theme
if ($themeModule !== null) {
// .../module/<themeModule>/themes/<themeName>/<templateModule>/<templateName>
@@ -133,13 +133,13 @@ class SimpleSAML_XHTML_Template
return $filename;
}
- // Not found in current theme
+ // not found in current theme
SimpleSAML_Logger::debug(
$_SERVER['PHP_SELF'].' - Template: Could not find template file ['. $template.'] at ['.
$filename.'] - now trying the base template'
);
- // Try default theme
+ // try default theme
if ($templateModule !== 'default') {
// .../module/<templateModule>/templates/<templateName>
$filename = SimpleSAML_Module::getModuleDir($templateModule).'/templates/'.$templateName;
@@ -152,7 +152,7 @@ class SimpleSAML_XHTML_Template
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.']';
SimpleSAML_Logger::critical($_SERVER['PHP_SELF'].' - '.$error);