Skip to content
Snippets Groups Projects
Commit 83a9cfa8 authored by Hanne Moa's avatar Hanne Moa
Browse files

Start of Twigification

parent 71262edc
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.
* *
...@@ -46,14 +45,32 @@ class SimpleSAML_XHTML_Template ...@@ -46,14 +45,32 @@ class SimpleSAML_XHTML_Template
* @param string $template Which template file to load * @param string $template Which template file to load
* @param string|null $defaultDictionary The default dictionary where tags will come from. * @param string|null $defaultDictionary The default dictionary where tags will come from.
*/ */
public function __construct(SimpleSAML_Configuration $configuration, $template, $defaultDictionary = null) public function __construct(\SimpleSAML_Configuration $configuration, $template, $defaultDictionary = null)
{ {
$this->configuration = $configuration; $this->configuration = $configuration;
$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();
} }
/**
* Setup twig
*/
private function setupTwig()
{
$twig_filename = basename($this->template, '.php').'.twig.html';
$filename = $this->findTemplatePath($twig_filename, $throw_exception=false);
// twig not in use for this page
if (is_null($filename)) {
return false;
}
$cache = $this->configuration->resolvePath('cache');
$this->template = $twig_filename;
$loader = new \Twig_Loader_Filesystem(array(dirname($filename), $this->configuration->resolvePath('templates')));
$this->twig = new \Twig_Environment($loader, array('cache' => $cache));
return true;
}
/** /**
* Return the internal translator object used by this template. * Return the internal translator object used by this template.
...@@ -65,14 +82,121 @@ class SimpleSAML_XHTML_Template ...@@ -65,14 +82,121 @@ class SimpleSAML_XHTML_Template
return $this->translator; return $this->translator;
} }
/**
* Generate languagebar
*/
private function generateLanguageBar()
{
$languages = $this->translator->getLanguage()->getLanguageList();
$langmap = NULL;
if ( count($languages) > 1 ) {
$langnames = array(
'no' => 'Bokmål', // Norwegian Bokmål
'nn' => 'Nynorsk', // Norwegian Nynorsk
'se' => 'Sámegiella', // Northern Sami
'sam' => 'Åarjelh-saemien giele', // Southern Sami
'da' => 'Dansk', // Danish
'en' => 'English',
'de' => 'Deutsch', // German
'sv' => 'Svenska', // Swedish
'fi' => 'Suomeksi', // Finnish
'es' => 'Español', // Spanish
'fr' => 'Français', // French
'it' => 'Italiano', // Italian
'nl' => 'Nederlands', // Dutch
'lb' => 'Lëtzebuergesch', // Luxembourgish
'cs' => 'Čeština', // Czech
'sl' => 'Slovenščina', // Slovensk
'lt' => 'Lietuvių kalba', // Lithuanian
'hr' => 'Hrvatski', // Croatian
'hu' => 'Magyar', // Hungarian
'pl' => 'Język polski', // Polish
'pt' => 'Português', // Portuguese
'pt-br' => 'Português brasileiro', // Portuguese
'ru' => 'русский язык', // Russian
'et' => 'eesti keel', // Estonian
'tr' => 'Türkçe', // Turkish
'el' => 'ελληνικά', // Greek
'ja' => '日本語', // Japanese
'zh' => '简体中文', // Chinese (simplified)
'zh-tw' => '繁體中文', // Chinese (traditional)
'ar' => 'العربية', // Arabic
'fa' => 'پارسی', // Persian
'ur' => 'اردو', // Urdu
'he' => 'עִבְרִית', // Hebrew
'id' => 'Bahasa Indonesia', // Indonesian
'sr' => 'Srpski', // Serbian
'lv' => 'Latviešu', // Latvian
'ro' => 'Românește', // Romanian
'eu' => 'Euskara', // Basque
);
$parameterName = $this->getTranslator()->getLanguage()->getLanguageParameterName();
$langmap = array();
foreach ($languages AS $lang => $current) {
$lang = strtolower($lang);
$langname = $langnames[$lang];
$url = false;
if (!$current) {
$url = htmlspecialchars(\SimpleSAML\Utils\HTTP::addURLParameters('', array($parameterName => $lang)));
}
$langmap[$langname] = $url;
}
}
return $langmap;
}
/**
* Set some default context
*/
private function twigDefaultContext()
{
$this->data['currentLanguage'] = $this->translator->getLanguage()->getLanguage();
// show language bar by default
if (!isset($this->data['hideLanguageBar'])) {
$this->data['hideLanguageBar'] = false;
}
// get languagebar
$this->data['languageBar'] = NULL;
if ($this->data['hideLanguageBar'] === false) {
$languageBar = $this->generateLanguageBar();
if (is_null($languageBar)) {
$this->data['hideLanguageBar'] = true;
} else {
$this->data['languageBar'] = $languageBar;
}
}
// assure that there is a <title> and <h1>
if (isset($this->data['header']) && !isset($this->data['pagetitle'])) {
$this->data['pagetitle'] = $this->data['header'];
}
if (!isset($this->data['pagetitle'])) {
$this->data['pagetitle'] = 'SimpleSAMLphp';
}
// set RTL
$this->data['isRTL'] = false;
if ($this->translator->getLanguage()->isLanguageRTL())
{
$this->data['isRTL'] = true;
}
}
/** /**
* Show the template to the user. * Show the template to the user.
*/ */
public function show() public function show()
{ {
$filename = $this->findTemplatePath($this->template); if ($this->useTwig) {
require($filename); $this->twigDefaultContext();
echo $this->twig->render($this->template, $this->data);
}
else
{
$filename = $this->findTemplatePath($this->template);
require($filename);
}
} }
...@@ -91,7 +215,7 @@ class SimpleSAML_XHTML_Template ...@@ -91,7 +215,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) private function findTemplatePath($template, $throw_exception=true)
{ {
assert('is_string($template)'); assert('is_string($template)');
...@@ -117,11 +241,11 @@ class SimpleSAML_XHTML_Template ...@@ -117,11 +241,11 @@ class SimpleSAML_XHTML_Template
if ($themeModule !== null) { if ($themeModule !== null) {
// .../module/<themeModule>/themes/<themeName>/<templateModule>/<templateName> // .../module/<themeModule>/themes/<themeName>/<templateModule>/<templateName>
$filename = SimpleSAML_Module::getModuleDir($themeModule). $filename = \SimpleSAML\Module::getModuleDir($themeModule).
'/themes/'.$themeName.'/'.$templateModule.'/'.$templateName; '/themes/'.$themeName.'/'.$templateModule.'/'.$templateName;
} elseif ($templateModule !== 'default') { } elseif ($templateModule !== 'default') {
// .../module/<templateModule>/templates/<themeName>/<templateName> // .../module/<templateModule>/templates/<themeName>/<templateName>
$filename = SimpleSAML_Module::getModuleDir($templateModule).'/templates/'.$templateName; $filename = \SimpleSAML\Module::getModuleDir($templateModule).'/templates/'.$templateName;
} else { } else {
// .../templates/<theme>/<templateName> // .../templates/<theme>/<templateName>
$filename = $this->configuration->getPathValue('templatedir', 'templates/').$templateName; $filename = $this->configuration->getPathValue('templatedir', 'templates/').$templateName;
...@@ -132,7 +256,7 @@ class SimpleSAML_XHTML_Template ...@@ -132,7 +256,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'
); );
...@@ -140,7 +264,7 @@ class SimpleSAML_XHTML_Template ...@@ -140,7 +264,7 @@ class SimpleSAML_XHTML_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;
} else { } else {
// .../templates/<templateName> // .../templates/<templateName>
$filename = $this->configuration->getPathValue('templatedir', 'templates/').'/'.$templateName; $filename = $this->configuration->getPathValue('templatedir', 'templates/').'/'.$templateName;
...@@ -150,11 +274,19 @@ class SimpleSAML_XHTML_Template ...@@ -150,11 +274,19 @@ class SimpleSAML_XHTML_Template
return $filename; return $filename;
} }
// not found in default template - log error and throw exception // not found in default template
$error = 'Template: Could not find template file ['.$template.'] at ['.$filename.']'; if ($throw_exception) {
SimpleSAML_Logger::critical($_SERVER['PHP_SELF'].' - '.$error); // log error and throw exception
$error = 'Template: Could not find template file ['.$template.'] at ['.$filename.']';
\SimpleSAML_Logger::critical($_SERVER['PHP_SELF'].' - '.$error);
throw new Exception($error); throw new Exception($error);
}
else
{
// missing template expected, return NULL
return NULL;
}
} }
...@@ -241,10 +373,10 @@ class SimpleSAML_XHTML_Template ...@@ -241,10 +373,10 @@ class SimpleSAML_XHTML_Template
/** /**
* Temporary wrapper for SimpleSAML\Locale\Translate::getPreferredTranslation(). * Temporary wrapper for \SimpleSAML\Locale\Translate::getPreferredTranslation().
* *
* @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::getPreferredTranslation() instead. * \SimpleSAML\Locale\Translate::getPreferredTranslation() instead.
*/ */
public function getTranslation($translations) public function getTranslation($translations)
{ {
......
<hr>
<img src="/{{ baseurlpath }}resources/icons/ssplogo-fish-small.png" alt="Small fish logo" style="float: right">
Copyright © 2007-2015 <a href="http://uninett.no/">UNINETT AS</a>
<br style="clear: right">
<div id="header">
<h1><a style="text-decoration: none; color: white" href="/{{ baseurlpath }}">{{ pagetitle }}</a></h1>
</div>
{% if not hideLanguageBar %}
<div id="languagebar">
{% for language, param in languageBar %}
{% if not loop.first %}|{% endif %}
{% if param %}<a href="{{ param }}">{{ language }}</a>{% else %}{{ language }}{% endif %}
{% endfor %}
</div>
{% endif %}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0">
<script type="text/javascript" src="/{{ baseurlpath }}resources/script.js"></script>
<title>{{ pagetitle }}</title>
<link rel="stylesheet" type="text/css" href="/{{ baseurlpath }}resources/default.css">
<link rel="icon" type="image/icon" href="/{{ baseurlpath }}resources/icons/favicon.ico">
<link rel="stylesheet" media="screen" type="text/css" href="/{{ baseurlpath }}resources/uitheme1.8/jquery-ui.css">
{% if isRTL %}<link rel="stylesheet" type="text/css" href="/{{ baseurlpath }}resources/default-rtl.css" />{% endif %}
<meta name="robots" content="noindex, nofollow">
{% block preload %}{% endblock %}
</head>
<body>
<div id="wrap">
{% block header %}{% include "_header.twig.html" %}{% endblock %}
<div id="content">
{% block content %}{% endblock %}
</div><!-- #content -->
<div id="footer">
{% block footer %}{% include "_footer.twig.html" %}{% endblock %}
</div>
</div><!-- #wrap -->
{% block postload %}{% endblock %}
</body></html>
{% extends "base.twig.html" %}
{% block content %}
<div id="portalmenu" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul class="tabset_tabs ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
<li class="ui-state-default ui-corner-top"><a href="http://simplesaml.arrakis.uninett.no/simplesaml/module.php/core/frontpage_welcome.php">Welcome</a></li>
<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active"><a href="#">Configuration</a></li>
<li class="ui-state-default ui-corner-top"><a href="http://simplesaml.arrakis.uninett.no/simplesaml/module.php/core/frontpage_auth.php">Authentication</a></li>
<li class="ui-state-default ui-corner-top"><a href="http://simplesaml.arrakis.uninett.no/simplesaml/module.php/core/frontpage_federation.php">Federation</a></li>
</ul>
<div id="portalcontent" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
<div style="clear: both" class="enablebox mini">
<table>
<tr class="disabled"><td>SAML 2.0 IdP</td>
<td><img src="/{{ baseurlpath }}resources/icons/silk/delete.png" alt="disabled" /></td></tr>
<tr class="disabled"><td>Shib 1.3 IdP</td>
<td><img src="/{{ baseurlpath }}resources/icons/silk/delete.png" alt="disabled" /></td></tr>
</table>
</div>
<h2>Configuration</h2>
<ul>
{% for url, page in adminpages %}
<li></li>
<a href="{{ url }}">{{ page }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endblock content %}
{% extends "base.twig.html" %}
{% block content %}
<p>This page exists as a sandbox to play with twig without affecting anything else. The template is in ./templates.</p>
<p>{{ sometext }}</p>
<p>Current locale set: {{ currentLanguage }}</p>
{% endblock content %}
<?php
require_once('../_include.php');
// Load SimpleSAMLphp, configuration
$config = SimpleSAML_Configuration::getInstance();
$session = SimpleSAML_Session::getSessionFromRequest();
// Check if valid local session exists..
//SimpleSAML\Utils\Auth::requireAdmin();
$adminpages = array(
'hostnames.php' => 'Diagnostics on hostname, port and protocol',
'phpinfo.php' => 'PHP info',
'../module.php/sanitycheck/index.php' => 'Sanity check of your SimpleSAMLphp setup',
'sandbox.php' => 'Sandbox for testing changes to layout and css',
);
$template = new SimpleSAML_XHTML_Template($config, 'index.php');
$template->data['pagetitle'] = 'Admin';
$template->data['adminpages'] = $adminpages;
$template->data['remaining'] = $session->getAuthData('admin', 'Expire') - time();
$template->data['valid'] = 'na';
$template->data['logout'] = null;
$template->show();
<?php
require_once('../_include.php');
// Load SimpleSAMLphp, configuration
$config = SimpleSAML_Configuration::getInstance();
$session = SimpleSAML_Session::getSessionFromRequest();
// Check if valid local session exists..
//SimpleSAML\Utils\Auth::requireAdmin();
$template = new SimpleSAML_XHTML_Template($config, 'sandbox.php');
$template->data['pagetitle'] = 'Sandbox';
$template->data['sometext'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur.';
$template->data['remaining'] = $session->getAuthData('admin', 'Expire') - time();
$template->data['logout'] = null;
$template->show();
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