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

Reformat SimpleSAML_XHTML_Template.

parent 8af147c8
No related branches found
No related tags found
No related merge requests found
<?php <?php
/** /**
* A minimalistic XHTML PHP based template system implemented for SimpleSAMLphp. * A minimalistic XHTML PHP based template system implemented for SimpleSAMLphp.
* *
...@@ -51,16 +52,21 @@ class SimpleSAML_XHTML_Template ...@@ -51,16 +52,21 @@ class SimpleSAML_XHTML_Template
$this->template = $template; $this->template = $template;
$this->data['baseurlpath'] = $this->configuration->getBaseURL(); $this->data['baseurlpath'] = $this->configuration->getBaseURL();
$this->translator = new \SimpleSAML\Locale\Translate($configuration, $defaultDictionary); $this->translator = new \SimpleSAML\Locale\Translate($configuration, $defaultDictionary);
$this->useTwig = $this->setupTwig(); $this->useTwig = $this->setupTwig();
} }
/*
* Normalize template-name /**
* *param $templateName Template * Normalize the name of the template to one of the possible alternatives.
*
* @param string $templateName The template name to normalize.
* @return string The filename we need to look for.
*/ */
private function normalizeTemplateName($templateName) private function normalizeTemplateName($templateName)
{ {
if (strripos($templateName, '.twig.html')) { return $templateName; } if (strripos($templateName, '.twig.html')) {
return $templateName;
}
$phppos = strripos($templateName, '.php'); $phppos = strripos($templateName, '.php');
if ($phppos) { if ($phppos) {
$templateName = substr($templateName, 0, $phppos); $templateName = substr($templateName, 0, $phppos);
...@@ -72,13 +78,21 @@ class SimpleSAML_XHTML_Template ...@@ -72,13 +78,21 @@ class SimpleSAML_XHTML_Template
return $templateName.'.twig.html'; return $templateName.'.twig.html';
} }
/**
* Set up the places where twig can look for templates.
*
* @return Twig_Loader_Filesystem|false The twig template loader or false if the template does not exist.
* @throws Twig_Error_Loader In case a failure occurs.
*/
private function setupTwigTemplatepaths() private function setupTwigTemplatepaths()
{ {
$filename = $this->normalizeTemplateName($this->template); $filename = $this->normalizeTemplateName($this->template);
// get namespace if any // get namespace if any
$namespace = ''; $namespace = '';
$split = explode(':', $filename, 2); $split = explode(':', $filename, 2);
if (count($split)===2) { if (count($split) === 2) {
$namespace = $split[0]; $namespace = $split[0];
$filename = $split[1]; $filename = $split[1];
} }
...@@ -87,31 +101,39 @@ class SimpleSAML_XHTML_Template ...@@ -87,31 +101,39 @@ class SimpleSAML_XHTML_Template
foreach ($this->findModuleTemplateDirs() as $module => $templateDir) { foreach ($this->findModuleTemplateDirs() as $module => $templateDir) {
$loader->prependPath($templateDir, $module); $loader->prependPath($templateDir, $module);
} }
if (!$loader->exists($this->twig_template)) { return false; } if (!$loader->exists($this->twig_template)) {
return false;
}
return $loader; return $loader;
} }
/** /**
* Setup twig * Setup twig.
*/ */
private function setupTwig() private function setupTwig()
{ {
$cache = $this->configuration->getString('template.cache', $this->configuration->resolvePath('cache')); $cache = $this->configuration->getString('template.cache', $this->configuration->resolvePath('cache'));
// check if template exists // check if template exists
$loader = $this->setupTwigTemplatepaths(); $loader = $this->setupTwigTemplatepaths();
if (!$loader) { return false; } if (!$loader) {
return false;
}
$auto_reload = $this->configuration->getBoolean('template.auto_reload', false); $auto_reload = $this->configuration->getBoolean('template.auto_reload', false);
$this->twig = new \Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => $auto_reload)); $this->twig = new \Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => $auto_reload));
return true; return true;
} }
private function findModuleTemplateDirs() private function findModuleTemplateDirs()
{ {
$all_modules = \SimpleSAML\Module::getModules(); $all_modules = \SimpleSAML\Module::getModules();
$modules = array(); $modules = array();
foreach ($all_modules as $module) { foreach ($all_modules as $module) {
if (!\SimpleSAML\Module::isModuleEnabled($module)) { continue; } if (!\SimpleSAML\Module::isModuleEnabled($module)) {
continue;
}
$moduledir = \SimpleSAML\Module::getModuleDir($module); $moduledir = \SimpleSAML\Module::getModuleDir($module);
// check if module has a /templates dir, if so, append // check if module has a /templates dir, if so, append
$templatedir = $moduledir.'/templates'; $templatedir = $moduledir.'/templates';
...@@ -122,6 +144,7 @@ class SimpleSAML_XHTML_Template ...@@ -122,6 +144,7 @@ class SimpleSAML_XHTML_Template
return $modules; return $modules;
} }
/** /**
* Return the internal translator object used by this template. * Return the internal translator object used by this template.
* *
...@@ -132,54 +155,55 @@ class SimpleSAML_XHTML_Template ...@@ -132,54 +155,55 @@ class SimpleSAML_XHTML_Template
return $this->translator; return $this->translator;
} }
/** /**
* Generate languagebar * Generate languagebar
*/ */
private function generateLanguageBar() private function generateLanguageBar()
{ {
$languages = $this->translator->getLanguage()->getLanguageList(); $languages = $this->translator->getLanguage()->getLanguageList();
$langmap = NULL; $langmap = null;
if ( count($languages) > 1 ) { if (count($languages) > 1) {
// TODO: this array should not be defined here // TODO: this array should not be defined here
$langnames = array( $langnames = array(
'no' => 'Bokmål', // Norwegian Bokmål 'no' => 'Bokmål', // Norwegian Bokmål
'nn' => 'Nynorsk', // Norwegian Nynorsk 'nn' => 'Nynorsk', // Norwegian Nynorsk
'se' => 'Sámegiella', // Northern Sami 'se' => 'Sámegiella', // Northern Sami
'sam' => 'Åarjelh-saemien giele', // Southern Sami 'sam' => 'Åarjelh-saemien giele', // Southern Sami
'da' => 'Dansk', // Danish 'da' => 'Dansk', // Danish
'en' => 'English', 'en' => 'English',
'de' => 'Deutsch', // German 'de' => 'Deutsch', // German
'sv' => 'Svenska', // Swedish 'sv' => 'Svenska', // Swedish
'fi' => 'Suomeksi', // Finnish 'fi' => 'Suomeksi', // Finnish
'es' => 'Español', // Spanish 'es' => 'Español', // Spanish
'fr' => 'Français', // French 'fr' => 'Français', // French
'it' => 'Italiano', // Italian 'it' => 'Italiano', // Italian
'nl' => 'Nederlands', // Dutch 'nl' => 'Nederlands', // Dutch
'lb' => 'Lëtzebuergesch', // Luxembourgish 'lb' => 'Lëtzebuergesch', // Luxembourgish
'cs' => 'Čeština', // Czech 'cs' => 'Čeština', // Czech
'sl' => 'Slovenščina', // Slovensk 'sl' => 'Slovenščina', // Slovensk
'lt' => 'Lietuvių kalba', // Lithuanian 'lt' => 'Lietuvių kalba', // Lithuanian
'hr' => 'Hrvatski', // Croatian 'hr' => 'Hrvatski', // Croatian
'hu' => 'Magyar', // Hungarian 'hu' => 'Magyar', // Hungarian
'pl' => 'Język polski', // Polish 'pl' => 'Język polski', // Polish
'pt' => 'Português', // Portuguese 'pt' => 'Português', // Portuguese
'pt-br' => 'Português brasileiro', // Portuguese 'pt-br' => 'Português brasileiro', // Portuguese
'ru' => 'русский язык', // Russian 'ru' => 'русский язык', // Russian
'et' => 'eesti keel', // Estonian 'et' => 'eesti keel', // Estonian
'tr' => 'Türkçe', // Turkish 'tr' => 'Türkçe', // Turkish
'el' => 'ελληνικά', // Greek 'el' => 'ελληνικά', // Greek
'ja' => '日本語', // Japanese 'ja' => '日本語', // Japanese
'zh' => '简体中文', // Chinese (simplified) 'zh' => '简体中文', // Chinese (simplified)
'zh-tw' => '繁體中文', // Chinese (traditional) 'zh-tw' => '繁體中文', // Chinese (traditional)
'ar' => 'العربية', // Arabic 'ar' => 'العربية', // Arabic
'fa' => 'پارسی', // Persian 'fa' => 'پارسی', // Persian
'ur' => 'اردو', // Urdu 'ur' => 'اردو', // Urdu
'he' => 'עִבְרִית', // Hebrew 'he' => 'עִבְרִית', // Hebrew
'id' => 'Bahasa Indonesia', // Indonesian 'id' => 'Bahasa Indonesia', // Indonesian
'sr' => 'Srpski', // Serbian 'sr' => 'Srpski', // Serbian
'lv' => 'Latviešu', // Latvian 'lv' => 'Latviešu', // Latvian
'ro' => 'Românește', // Romanian 'ro' => 'Românește', // Romanian
'eu' => 'Euskara', // Basque 'eu' => 'Euskara', // Basque
); );
$parameterName = $this->getTranslator()->getLanguage()->getLanguageParameterName(); $parameterName = $this->getTranslator()->getLanguage()->getLanguageParameterName();
$langmap = array(); $langmap = array();
...@@ -188,7 +212,8 @@ class SimpleSAML_XHTML_Template ...@@ -188,7 +212,8 @@ class SimpleSAML_XHTML_Template
$langname = $langnames[$lang]; $langname = $langnames[$lang];
$url = false; $url = false;
if (!$current) { if (!$current) {
$url = htmlspecialchars(\SimpleSAML\Utils\HTTP::addURLParameters('', array($parameterName => $lang))); $url = htmlspecialchars(\SimpleSAML\Utils\HTTP::addURLParameters('',
array($parameterName => $lang)));
} }
$langmap[$langname] = $url; $langmap[$langname] = $url;
} }
...@@ -196,6 +221,7 @@ class SimpleSAML_XHTML_Template ...@@ -196,6 +221,7 @@ class SimpleSAML_XHTML_Template
return $langmap; return $langmap;
} }
/** /**
* Set some default context * Set some default context
*/ */
...@@ -207,7 +233,7 @@ class SimpleSAML_XHTML_Template ...@@ -207,7 +233,7 @@ class SimpleSAML_XHTML_Template
$this->data['hideLanguageBar'] = false; $this->data['hideLanguageBar'] = false;
} }
// get languagebar // get languagebar
$this->data['languageBar'] = NULL; $this->data['languageBar'] = null;
if ($this->data['hideLanguageBar'] === false) { if ($this->data['hideLanguageBar'] === false) {
$languageBar = $this->generateLanguageBar(); $languageBar = $this->generateLanguageBar();
if (is_null($languageBar)) { if (is_null($languageBar)) {
...@@ -227,8 +253,7 @@ class SimpleSAML_XHTML_Template ...@@ -227,8 +253,7 @@ class SimpleSAML_XHTML_Template
// set RTL // set RTL
$this->data['isRTL'] = false; $this->data['isRTL'] = false;
if ($this->translator->getLanguage()->isLanguageRTL()) if ($this->translator->getLanguage()->isLanguageRTL()) {
{
$this->data['isRTL'] = true; $this->data['isRTL'] = true;
} }
} }
...@@ -242,9 +267,7 @@ class SimpleSAML_XHTML_Template ...@@ -242,9 +267,7 @@ class SimpleSAML_XHTML_Template
if ($this->useTwig) { if ($this->useTwig) {
$this->twigDefaultContext(); $this->twigDefaultContext();
echo $this->twig->render($this->twig_template, $this->data); echo $this->twig->render($this->twig_template, $this->data);
} } else {
else
{
$filename = $this->findTemplatePath($this->template); $filename = $this->findTemplatePath($this->template);
require($filename); require($filename);
} }
...@@ -266,7 +289,7 @@ class SimpleSAML_XHTML_Template ...@@ -266,7 +289,7 @@ class SimpleSAML_XHTML_Template
* *
* @throws Exception If the template file couldn't be found. * @throws Exception If the template file couldn't be found.
*/ */
private function findTemplatePath($template, $throw_exception=true) private function findTemplatePath($template, $throw_exception = true)
{ {
assert('is_string($template)'); assert('is_string($template)');
...@@ -308,7 +331,7 @@ class SimpleSAML_XHTML_Template ...@@ -308,7 +331,7 @@ class SimpleSAML_XHTML_Template
// 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'
); );
...@@ -332,11 +355,9 @@ class SimpleSAML_XHTML_Template ...@@ -332,11 +355,9 @@ class SimpleSAML_XHTML_Template
\SimpleSAML_Logger::critical($_SERVER['PHP_SELF'].' - '.$error); \SimpleSAML_Logger::critical($_SERVER['PHP_SELF'].' - '.$error);
throw new Exception($error); throw new Exception($error);
} } else {
else
{
// missing template expected, return NULL // missing template expected, return NULL
return NULL; return null;
} }
} }
...@@ -373,6 +394,7 @@ class SimpleSAML_XHTML_Template ...@@ -373,6 +394,7 @@ class SimpleSAML_XHTML_Template
/** /**
* @param $language * @param $language
* @param bool $setLanguageCookie * @param bool $setLanguageCookie
*
* @deprecated This method will be removed in SSP 2.0. Please use \SimpleSAML\Locale\Language::setLanguage() * @deprecated This method will be removed in SSP 2.0. Please use \SimpleSAML\Locale\Language::setLanguage()
* instead. * instead.
*/ */
...@@ -395,6 +417,7 @@ class SimpleSAML_XHTML_Template ...@@ -395,6 +417,7 @@ class SimpleSAML_XHTML_Template
/** /**
* @param $language * @param $language
*
* @deprecated This method will be removed in SSP 2.0. Please use \SimpleSAML\Locale\Language::setLanguageCookie() * @deprecated This method will be removed in SSP 2.0. Please use \SimpleSAML\Locale\Language::setLanguageCookie()
* instead. * instead.
*/ */
...@@ -403,10 +426,12 @@ class SimpleSAML_XHTML_Template ...@@ -403,10 +426,12 @@ class SimpleSAML_XHTML_Template
\SimpleSAML\Locale\Language::setLanguageCookie($language); \SimpleSAML\Locale\Language::setLanguageCookie($language);
} }
/** /**
* Wraps Language->getLanguageList * Wraps Language->getLanguageList
*/ */
private function getLanguageList() { private function getLanguageList()
{
return $this->translator->getLanguage()->getLanguageList(); return $this->translator->getLanguage()->getLanguageList();
} }
...@@ -440,7 +465,8 @@ class SimpleSAML_XHTML_Template ...@@ -440,7 +465,8 @@ class SimpleSAML_XHTML_Template
* This function can be used to include headers and footers etc. * This function can be used to include headers and footers etc.
* *
*/ */
private function includeAtTemplateBase($file) { private function includeAtTemplateBase($file)
{
$data = $this->data; $data = $this->data;
$filename = $this->findTemplatePath($file); $filename = $this->findTemplatePath($file);
...@@ -465,6 +491,7 @@ class SimpleSAML_XHTML_Template ...@@ -465,6 +491,7 @@ class SimpleSAML_XHTML_Template
/** /**
* @param $file * @param $file
* @param null $otherConfig * @param null $otherConfig
*
* @deprecated This method will be removed in SSP 2.0. Please use * @deprecated This method will be removed in SSP 2.0. Please use
* \SimpleSAML\Locale\Translate::includeLanguageFile() instead. * \SimpleSAML\Locale\Translate::includeLanguageFile() instead.
*/ */
...@@ -477,7 +504,8 @@ class SimpleSAML_XHTML_Template ...@@ -477,7 +504,8 @@ class SimpleSAML_XHTML_Template
/** /**
* Wrap Language->isLanguageRTL * Wrap Language->isLanguageRTL
*/ */
private function isLanguageRTL() { private function isLanguageRTL()
{
return $this->translator->getLanguage()->isLanguageRTL(); return $this->translator->getLanguage()->isLanguageRTL();
} }
...@@ -487,6 +515,7 @@ class SimpleSAML_XHTML_Template ...@@ -487,6 +515,7 @@ class SimpleSAML_XHTML_Template
* *
* @param array $def The array holding string definitions. * @param array $def The array holding string definitions.
* @param array $lang The array holding translations for every string. * @param array $lang The array holding translations for every string.
*
* @return array The recursive merge of both arrays. * @return array The recursive merge of both arrays.
* @deprecated This method will be removed in SimpleSAMLphp 2.0. Please use array_merge_recursive() instead. * @deprecated This method will be removed in SimpleSAMLphp 2.0. Please use array_merge_recursive() instead.
*/ */
......
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