Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simplesamlphp
Manage
Activity
Members
Labels
Plan
Jira
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Perun
Perun ProxyIdP
v1
simplesamlphp
Commits
79576e09
Commit
79576e09
authored
9 years ago
by
Jaime Perez Crespo
Browse files
Options
Downloads
Patches
Plain Diff
Reformat the new \SimpleSAML\Locale\Language class.
parent
6389e5c1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/SimpleSAML/Locale/Language.php
+63
-53
63 additions, 53 deletions
lib/SimpleSAML/Locale/Language.php
lib/SimpleSAML/XHTML/Template.php
+4
-4
4 additions, 4 deletions
lib/SimpleSAML/XHTML/Template.php
with
67 additions
and
57 deletions
lib/SimpleSAML/Locale/Language.php
+
63
−
53
View file @
79576e09
...
@@ -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
()
{
//
L
anguage is set in object
//
l
anguage is set in object
if
(
isset
(
$this
->
language
))
{
if
(
isset
(
$this
->
language
))
{
return
$this
->
language
;
return
$this
->
language
;
}
}
//
R
un custom getLanguage function if defined
//
r
un 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
;
}
}
}
}
//
L
anguage is provided in a stored
COOKIE
//
l
anguage 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
;
}
}
/
* C
heck if we can find a good language from the Accept-Language
http
header
. */
/
/ c
heck 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
;
}
}
//
L
anguage is not set, and we get the default language from the configuration
.
//
l
anguage 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
;
/
* F
ind the available language with the best score
. */
/
/ f
ind 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
)
{
/
* A
pply the language map to the language code
. */
/
/ a
pply 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
))
{
/
* S
kip this language - we don't have it
. */
/
/ s
kip 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
);
}
}
}
}
This diff is collapsed.
Click to expand it.
lib/SimpleSAML/XHTML/Template.php
+
4
−
4
View file @
79576e09
...
@@ -115,7 +115,7 @@ class SimpleSAML_XHTML_Template
...
@@ -115,7 +115,7 @@ class SimpleSAML_XHTML_Template
$themeName
=
$tmp
[
0
];
$themeName
=
$tmp
[
0
];
}
}
//
F
irst check the current theme
//
f
irst 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
;
}
}
//
N
ot found in current theme
//
n
ot 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'
);
);
//
T
ry default theme
//
t
ry 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
;
}
}
//
N
ot found in default template - log error and throw exception
//
n
ot 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
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment