Newer
Older
Andreas Åkre Solberg
committed
require_once((isset($SIMPLESAML_INCPREFIX)?$SIMPLESAML_INCPREFIX:'') . 'SimpleSAML/Configuration.php');
require_once((isset($SIMPLESAML_INCPREFIX)?$SIMPLESAML_INCPREFIX:'') . 'SimpleSAML/Logger.php');
Andreas Åkre Solberg
committed
Andreas Åkre Solberg
committed
* A minimalistic XHTML PHP based template system implemented for simpleSAMLphp.
*
Andreas Åkre Solberg
committed
* @author Andreas Åkre Solberg, UNINETT AS. <andreas.solberg@uninett.no>
Andreas Åkre Solberg
committed
* @package simpleSAMLphp
* @version $Id$
*/
class SimpleSAML_XHTML_Template {
private $configuration = null;
private $template = 'default.php';
private $language = null;
Andreas Åkre Solberg
committed
private $langtext = null;
public $data = null;
Andreas Åkre Solberg
committed
function __construct(SimpleSAML_Configuration $configuration, $template, $languagefile = null) {
$this->configuration = $configuration;
$this->template = $template;
Andreas Åkre Solberg
committed
$this->data['baseurlpath'] = $this->configuration->getBaseURL();
Andreas Åkre Solberg
committed
if (!empty($languagefile)) $this->includeLanguageFile($languagefile);
public function setLanguage($language) {
$this->language = $language;
Andreas Åkre Solberg
committed
// setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly ]]]]]] )
// time()+60*60*24*900 expires 900 days from now.
setcookie('language', $language, time()+60*60*24*900);
}
public function getLanguage() {
Andreas Åkre Solberg
committed
// Language is set in object
if (isset($this->language)) {
return $this->language;
Andreas Åkre Solberg
committed
// Language is provided in query string
} else if (isset($_GET['language'])) {
$this->setLanguage($_GET['language']);
Andreas Åkre Solberg
committed
// Language is provided in a stored COOKIE
} else if (isset($_COOKIE['language'])) {
$this->language = $_COOKIE['language'];
Andreas Åkre Solberg
committed
// Language is not set, and we get the default language from the configuration.
Andreas Åkre Solberg
committed
return $this->getDefaultLanguage('language.default');
}
return $this->language;
}
Andreas Åkre Solberg
committed
private function getDefaultLanguage() {
return $this->configuration->getValue('language.default', 'en');
}
private function getLanguageList() {
$availableLanguages = $this->configuration->getValue('language.available');
$thisLang = $this->getLanguage();
$lang = array();
foreach ($availableLanguages AS $nl) {
$lang[$nl] = ($nl == $thisLang);
}
return $lang;
}
private function includeAtTemplateBase($file) {
$data = $this->data;
Andreas Åkre Solberg
committed
$filebase = $this->configuration->getPathValue('templatedir');
include($filebase . $file);
}
private function includeAtLanguageBase($file) {
$data = $this->data;
Andreas Åkre Solberg
committed
$filebase = $this->configuration->getPathValue('templatedir') . $this->getLanguage() . '/' ;
Andreas Åkre Solberg
committed
if (!file_exists($filebase . $file)) {
Andreas Åkre Solberg
committed
$filebase = $this->configuration->getPathValue('templatedir') .
Andreas Åkre Solberg
committed
$this->configuration->getValue('language.default') . '/';
if (!file_exists($filebase . $file) ) {
Andreas Åkre Solberg
committed
SimpleSAML_Logger::error($_SERVER['PHP_SELF'].' - Template: Could not find template file [' . $this->template . '] at [' . $filename . ']');
Andreas Åkre Solberg
committed
return;
}
}
include($filebase . $file);
}
Andreas Åkre Solberg
committed
Andreas Åkre Solberg
committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Include text in the current language.
*
* @param $tag A name tag of the string that should be returned.
* @param $fallbacktag If set to true and string was not found in any languages, return the tag it self.
* @param $fallbackdefault If not found in selected language fallback to default language.
* @param $replacements An associative array of keys that should be replaced with values in the translated string.
* @param $striptags Should HTML tags be stripped from the translation
*/
private function t($tag, $fallbacktag = true, $fallbackdefault = true, $replacements = null, $striptags = false) {
if (empty($this->langtext) || !is_array($this->langtext)) {
SimpleSAML_Logger::error('Template: No language text loaded. Looking up [' . $tag . ']');
return $this->t_not_translated($tag, $fallbacktag);
}
# echo 'LANGTEXT: ';
# print_r($this->langtext);
$selected_language = $this->getLanguage();
$default_language = $this->getDefaultLanguage();
if (array_key_exists($tag, $this->langtext) ) {
/**
* Look up translation of tag in the selected language
*/
if (array_key_exists($selected_language, $this->langtext[$tag])) {
return $this->langtext[$tag][$selected_language];
/**
* Look up translation of tag in the default language, only if fallbackdefault = true (method parameter)
*/
} elseif($fallbackdefault && array_key_exists($default_language, $this->langtext[$tag])) {
SimpleSAML_Logger::error('Template: Looking up [' . $tag . ']: not found in language [' . $selected_language . '] using default [' . $default_language . '].');
return $this->langtext[$tag][$default_language];
}
}
SimpleSAML_Logger::error('Template: Looking up [' . $tag . ']: not translated at all.');
return $this->t_not_translated($tag, $fallbacktag);
}
/**
* Return the string that should be used when no translation was found.
*
* @param $tag A name tag of the string that should be returned.
* @param $fallbacktag If set to true and string was not found in any languages, return
* the tag it self. If false return null.
*/
private function t_not_translated($tag, $fallbacktag) {
if ($fallbacktag) {
return 'not translated (' . $tag . ')';
} else {
return null;
}
}
Andreas Åkre Solberg
committed
/**
* Include language file from the dictionaries directory.
*/
Andreas Åkre Solberg
committed
private function includeLanguageFile($file) {
Andreas Åkre Solberg
committed
$filebase = $this->configuration->getPathValue('dictionarydir');
Andreas Åkre Solberg
committed
SimpleSAML_Logger::info('Template: Loading [' . $filebase . $file . ']');
Andreas Åkre Solberg
committed
if (!file_exists($filebase . $file)) {
Andreas Åkre Solberg
committed
SimpleSAML_Logger::error($_SERVER['PHP_SELF'].' - Template: Could not find template file [' . $this->template . '] at [' . $filebase . $file . ']');
Andreas Åkre Solberg
committed
return;
}
include($filebase . $file);
if (isset($lang)) {
Andreas Åkre Solberg
committed
if (is_array($this->langtext)) {
SimpleSAML_Logger::info('Template: Merging language array. Loading [' . $file . ']');
$this->langtext = array_merge($this->langtext, $lang);
} else {
SimpleSAML_Logger::info('Template: Setting new language array. Loading [' . $file . ']');
$this->langtext = $lang;
Andreas Åkre Solberg
committed
}
}
Andreas Åkre Solberg
committed
Andreas Åkre Solberg
committed
}
Andreas Åkre Solberg
committed
/**
* Show the template to the user.
*/
public function show() {
Andreas Åkre Solberg
committed
Andreas Åkre Solberg
committed
Andreas Åkre Solberg
committed
$filename = $this->configuration->getPathValue('templatedir') .
$this->configuration->getPathValue('template.use') . '/' . $this->template;
Andreas Åkre Solberg
committed
if (!file_exists($filename)) {
SimpleSAML_Logger::warning($_SERVER['PHP_SELF'].' - Template: Could not find template file [' . $this->template . '] at [' . $filename . '] - now trying the base template');
$filename = $this->configuration->getPathValue('templatedir') .
$this->configuration->getPathValue('template.base') . '/' . $this->template;
if (!file_exists($filename)) {
Andreas Åkre Solberg
committed
SimpleSAML_Logger::critical($_SERVER['PHP_SELF'].' - Template: Could not find template file [' . $this->template . '] at [' . $filename . ']');
Andreas Åkre Solberg
committed
echo 'Fatal error: Could not find template file [' . $this->template . '] at [' . $filename . ']';
exit(0);
}
require_once($filename);
}
}
?>