Skip to content
Snippets Groups Projects
Template.php 22.3 KiB
Newer Older
 * A minimalistic XHTML PHP based template system implemented for SimpleSAMLphp.
 * @package SimpleSAMLphp
declare(strict_types=1);

Tim van Dijen's avatar
Tim van Dijen committed
namespace SimpleSAML\XHTML;

use Exception;
use InvalidArgumentException;
use SimpleSAML\Assert\Assert;
Tim van Dijen's avatar
Tim van Dijen committed
use SimpleSAML\Configuration;
use SimpleSAML\Error;
Tim van Dijen's avatar
Tim van Dijen committed
use SimpleSAML\Locale\Language;
Tim van Dijen's avatar
Tim van Dijen committed
use SimpleSAML\Locale\Translate;
use SimpleSAML\Locale\TwigTranslator;
Tim van Dijen's avatar
Tim van Dijen committed
use SimpleSAML\Logger;
use SimpleSAML\Module;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\Response;
use Twig\Error\RuntimeError;
use Twig\Extra\Intl\IntlExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;
use function class_exists;
use function count;
use function date;
use function explode;
use function hash;
use function in_array;
use function is_null;
use function key;
use function ksort;
use function strripos;
use function strtolower;
use function strval;
use function substr;

/**
 * The content-property is set upstream, but this is not recognized by Psalm
 * @psalm-suppress PropertyNotSetInConstructor
 */
class Template extends Response
    /**
     * The data associated with this template, accessible within the template itself.
     *
     * @var array
     */
    public array $data = [];
    /**
     * A translator instance configured to work with this template.
     *
     * @var \SimpleSAML\Locale\Translate
     */
    private Translate $translator;
    /**
     * The localization backend
     *
     * @var \SimpleSAML\Locale\Localization
     */
    private Localization $localization;
     * @var \SimpleSAML\Configuration
    private Configuration $configuration;
    private string $template = 'default.php';
Hanne Moa's avatar
Hanne Moa committed

     * @var \Twig\Environment
    private \Twig\Environment $twig;
    /**
     * The template name.
     *
     * @var string
     */
    private string $twig_template;
    /**
     * Current module, if any.
     * @var string|null
    private ?string $module = null;
    /**
     * A template controller, if any.
     *
     * Used to intercept certain parts of the template handling, while keeping away unwanted/unexpected hooks. Set
     * the 'theme.controller' configuration option to a class that implements the
     * \SimpleSAML\XHTML\TemplateControllerInterface interface to use it.
     * @var \SimpleSAML\XHTML\TemplateControllerInterface|null
    private ?TemplateControllerInterface $controller = null;
    /**
     * Whether we are using a non-default theme or not.
     *
     * If we are using a theme, this variable holds an array with two keys: "module" and "name", those being the name
     * of the module and the name of the theme, respectively. If we are using the default theme, the variable has
     * the 'default' string in the "name" key, and 'null' in the "module" key.
     * @var array
    private array $theme = ['module' => null, 'name' => 'default'];
     * @var \Symfony\Component\Filesystem\Filesystem
     */
    private Filesystem $fileSystem;

Hanne Moa's avatar
Hanne Moa committed
    /**
     * Constructor
     *
     * @param \SimpleSAML\Configuration $configuration Configuration object
     * @param string                   $template Which template file to load
Hanne Moa's avatar
Hanne Moa committed
     */
    public function __construct(Configuration $configuration, string $template)
Hanne Moa's avatar
Hanne Moa committed
        $this->configuration = $configuration;
        $this->template = $template;
        // TODO: do not remove the slash from the beginning, change the templates instead!
        $this->data['baseurlpath'] = ltrim($this->configuration->getBasePath(), '/');

        // parse module and template name
        list($this->module) = $this->findModuleAndTemplateName($template);

        // parse config to find theme and module theme is in, if any
        list($this->theme['module'], $this->theme['name']) = $this->findModuleAndTemplateName(
            $this->configuration->getOptionalString('theme.use', 'default')
        $this->translator = new Translate($configuration);
Tim van Dijen's avatar
Tim van Dijen committed
        $this->localization = new Localization($configuration);
Tim van Dijen's avatar
Tim van Dijen committed
        // check if we need to attach a theme controller
        $controller = $this->configuration->getOptionalString('theme.controller', null);
        if ($controller !== null) {
            if (
                class_exists($controller)
                && in_array(TemplateControllerInterface::class, class_implements($controller))
            ) {
                /** @var \SimpleSAML\XHTML\TemplateControllerInterface $this->controller */
                $this->controller = new $controller();
            } else {
                throw new Error\ConfigurationError(
                    'Invalid controller was configured in `theme.controller`. ' .
                    ' Make sure the class exists and implements the TemplateControllerInterface.'
                );
            }
        $this->fileSystem = new Filesystem();
Tim van Dijen's avatar
Tim van Dijen committed
        $this->twig = $this->setupTwig();
        $this->charset = 'UTF-8';
    /**
     * Return the URL of an asset, including a cache-buster parameter that depends on the last modification time of
     * the original file.
     * @param string $asset
     * @param string|null $module
     * @return string
     */
    public function asset(string $asset, string $module = null): string
        $baseDir = $this->configuration->getBaseDir();
        if (is_null($module)) {
            $file = $baseDir . 'www/assets/' . $asset;
            $basePath = $this->configuration->getBasePath();
            $path = $basePath . 'assets/' . $asset;
            $file = $baseDir . 'modules/' . $module . '/www/assets/' . $asset;
            $path = Module::getModuleUrl($module . '/assets/' . $asset);
        if (!$this->fileSystem->exists($file)) {
            // don't be too harsh if an asset is missing, just pretend it's there...
            return $path;
        $file = new File($file);

        $tag = $this->configuration->getVersion();
        if ($tag === 'master') {
            $tag = strval($file->getMtime());
        $tag = substr(hash('md5', $tag), 0, 5);
        return $path . '?tag=' . $tag;
    /**
     * Get the normalized template name.
     *
     * @return string The name of the template to use.
     */
Tim van Dijen's avatar
Tim van Dijen committed
    public function getTemplateName(): string
     * 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(string $templateName): string
        if (strripos($templateName, '.twig')) {
            return $templateName;
        }
Tim van Dijen's avatar
Tim van Dijen committed
        return $templateName . '.twig';

    /**
     * Set up the places where twig can look for templates.
     *
     * @return TemplateLoader The twig template loader or false if the template does not exist.
Tim van Dijen's avatar
Tim van Dijen committed
     * @throws \Twig\Error\LoaderError In case a failure occurs.
    private function setupTwigTemplatepaths(): TemplateLoader
    {
        $filename = $this->normalizeTemplateName($this->template);
        // get namespace if any
        list($namespace, $filename) = $this->findModuleAndTemplateName($filename);
        $this->twig_template = ($namespace !== null) ? '@' . $namespace . '/' . $filename : $filename;
        $loader = new TemplateLoader();
        $templateDirs = $this->findThemeTemplateDirs();
        if ($this->module && $this->module != 'core') {
Jaime Pérez Crespo's avatar
Jaime Pérez Crespo committed
            $modDir = TemplateLoader::getModuleTemplateDir($this->module);
            $templateDirs[] = [$this->module => $modDir];
            $templateDirs[] = ['__parent__' => $modDir];
                $templateDirs[] = [
                    $this->theme['module'] => TemplateLoader::getModuleTemplateDir($this->theme['module'])
            } catch (InvalidArgumentException $e) {
                // either the module is not enabled or it has no "templates" directory, ignore
            }
        $templateDirs[] = ['core' => TemplateLoader::getModuleTemplateDir('core')];

        // default, themeless templates are checked last
        $templateDirs[] = [
            FilesystemLoader::MAIN_NAMESPACE => $this->configuration->resolvePath('templates')
        foreach ($templateDirs as $entry) {
            $loader->addPath($entry[key($entry)], key($entry));
Hanne Moa's avatar
Hanne Moa committed
    /**
     * @return \Twig\Environment
     * @throws \Exception if the template does not exist
Hanne Moa's avatar
Hanne Moa committed
     */
    private function setupTwig(): Environment
Hanne Moa's avatar
Hanne Moa committed
    {
        $auto_reload = $this->configuration->getOptionalBoolean('template.auto_reload', true);
        $cache = $this->configuration->getOptionalString('template.cache', null);
        // set up template paths
        $loader = $this->setupTwigTemplatepaths();
        // abort if twig template does not exist
        if (!$loader->exists($this->twig_template)) {
            throw new Exception('Template-file \"' . $this->getTemplateName() . '\" does not exist.');
        // load extra i18n domains
        if ($this->module) {
            $this->localization->addModuleDomain($this->module);
        }
        if ($this->theme['module'] !== null && $this->theme['module'] !== $this->module) {
            $this->localization->addModuleDomain($this->theme['module']);
        }
            'auto_reload' => $auto_reload,
            'cache' => $cache ?? false,
Tim van Dijen's avatar
Tim van Dijen committed
            'strict_variables' => true,
        $twig = new Environment($loader, $options);
        $twigTranslator = new TwigTranslator([Translate::class, 'translateSingularGettext']);
        $twig->addExtension(new TranslationExtension($twigTranslator));
        $twig->addExtension(new IntlExtension());
        $twig->addFunction(new TwigFunction('moduleURL', [Module::class, 'getModuleURL']));

        $langParam = $this->configuration->getOptionalString('language.parameter.name', 'language');
        $twig->addGlobal('languageParameterName', $langParam);
        $twig->addGlobal('currentLanguage', $this->translator->getLanguage()->getLanguage());
        $twig->addGlobal('isRTL', false); // language RTL configuration
        if ($this->translator->getLanguage()->isLanguageRTL()) {
            $twig->addGlobal('isRTL', true);
        }
        $queryParams = $_GET; // add query parameters, in case we need them in the template
        if (isset($queryParams[$langParam])) {
            unset($queryParams[$langParam]);
        }
        $twig->addGlobal('queryParams', $queryParams);
        $twig->addGlobal('templateId', str_replace('.twig', '', $this->normalizeTemplateName($this->template)));
        $twig->addGlobal('isProduction', $this->configuration->getOptionalBoolean('production', true));
        $twig->addGlobal('baseurlpath', ltrim($this->configuration->getBasePath(), '/'));
        // add a filter for translations out of arrays
        $twig->addFilter(
                'translateFromArray',
Tim van Dijen's avatar
Tim van Dijen committed
                [Translate::class, 'translateFromArray'],
                ['needs_context' => true]
        // add a filter for preferred entity name
        $twig->addFilter(
            new TwigFilter(
                'entityDisplayName',
                [$this, 'getEntityDisplayName'],
            )
        );
        // add an asset() function
        $twig->addFunction(new TwigFunction('asset', [$this, 'asset']));
        if ($this->controller !== null) {
            $this->controller->setUpTwig($twig);
        }

        return $twig;
Hanne Moa's avatar
Hanne Moa committed
    }
Hanne Moa's avatar
Hanne Moa committed

    /**
     * Add overriding templates from the configured theme.
Hanne Moa's avatar
Hanne Moa committed
     *
     * @return array An array of module => templatedir lookups.
Hanne Moa's avatar
Hanne Moa committed
     */
    private function findThemeTemplateDirs(): array
        if (!isset($this->theme['module'])) {
            // no module involved

        // setup directories & namespaces
        $themeDir = Module::getModuleDir($this->theme['module']) . '/themes/' . $this->theme['name'];

        if (!$this->fileSystem->exists($themeDir)) {
Tim van Dijen's avatar
Tim van Dijen committed
            Logger::warning(
                sprintf('Theme directory for theme "%s" (%s) does not exist.', $this->theme['name'], $themeDir),
Tim van Dijen's avatar
Tim van Dijen committed
            );
        $finder = new Finder();
        $finder->directories()->in($themeDir)->depth(0);
        if (!$finder->hasResults()) {
            Logger::warning(sprintf(
                'Theme directory for theme "%s" (%s) is not readable or is empty.',
                $this->theme['name'],
                $themeDir,
            ));
            return [];
        }
        $themeTemplateDirs = [];
        foreach ($finder as $entry) {
            // set correct name for the default namespace
            $ns = ($entry->getFileName() === 'default') ? FilesystemLoader::MAIN_NAMESPACE : $entry->getFileName();
            $themeTemplateDirs[] = [$ns => strval($entry)];
        return $themeTemplateDirs;
    /**
     * Get the template directory of a module, if it exists.
     * @param string $module
     * @return string The templates directory of a module
     * @throws \InvalidArgumentException If the module is not enabled or it has no templates directory.
    private function getModuleTemplateDir(string $module): string
Tim van Dijen's avatar
Tim van Dijen committed
        if (!Module::isModuleEnabled($module)) {
            throw new InvalidArgumentException('The module \'' . $module . '\' is not enabled.');
Tim van Dijen's avatar
Tim van Dijen committed
        $moduledir = Module::getModuleDir($module);
        // check if module has a /templates dir, if so, append
        $templateDir = $moduledir . '/templates';
        $file = new File($templateDir);
        if (!$file->isDir()) {
            throw new InvalidArgumentException('The module \'' . $module . '\' has no templates directory.');
        return $templateDir;
    }


    /**
     * Add the templates from a given module.
     *
     * Note that the module must be installed, enabled, and contain a "templates" directory.
     *
     * @param string $module The module where we need to search for templates.
     * @throws \InvalidArgumentException If the module is not enabled or it has no templates directory.
    public function addTemplatesFromModule(string $module): void
        $dir = TemplateLoader::getModuleTemplateDir($module);
Tim van Dijen's avatar
Tim van Dijen committed
        /** @var \Twig\Loader\FilesystemLoader $loader */
        $loader = $this->twig->getLoader();
        $loader->addPath($dir, $module);
Hanne Moa's avatar
Hanne Moa committed
    /**
     * Generate an array for its use in the language bar, indexed by the ISO 639-2 codes of the languages available,
     * containing their localized names and the URL that should be used in order to change to that language.
     *
     * @return array|null The array containing information of all available languages.
Hanne Moa's avatar
Hanne Moa committed
     */
    private function generateLanguageBar(): ?array
Hanne Moa's avatar
Hanne Moa committed
    {
        $languages = $this->translator->getLanguage()->getLanguageList();
        ksort($languages);
        $langmap = null;
        if (count($languages) > 1) {
Hanne Moa's avatar
Hanne Moa committed
            $parameterName = $this->getTranslator()->getLanguage()->getLanguageParameterName();
            $langmap = [];
            foreach ($languages as $lang => $current) {
Hanne Moa's avatar
Hanne Moa committed
                $lang = strtolower($lang);
                $langname = $this->translator->getLanguage()->getLanguageLocalizedName($lang);
Hanne Moa's avatar
Hanne Moa committed
                $url = false;
                if (!$current) {
                    $httpUtils = new Utils\HTTP();
                    $url = $httpUtils->addURLParameters(
                        [$parameterName => $lang]
Hanne Moa's avatar
Hanne Moa committed
                }
                $langmap[$lang] = [
Hanne Moa's avatar
Hanne Moa committed
            }
        }
        return $langmap;
    }

Hanne Moa's avatar
Hanne Moa committed
    /**
     * Set some default context
     */
    private function twigDefaultContext(): void
Hanne Moa's avatar
Hanne Moa committed
    {
        // show language bar by default
        if (!isset($this->data['hideLanguageBar'])) {
            $this->data['hideLanguageBar'] = false;
        }
        // get languagebar
        $this->data['languageBar'] = null;
Hanne Moa's avatar
Hanne Moa committed
        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';
        }

        $this->data['year'] = date('Y');
        $this->data['header'] = $this->configuration->getOptionalString('theme.header', 'SimpleSAMLphp');
    /**
     * Helper function for locale extraction: just compile but not display
     * this template. This is not generally useful, getContents() will normally
     * compile and display the template in one step.
     */
    public function compile(): void
    {
        $this->twig->load($this->twig_template);
    }
Hanne Moa's avatar
Hanne Moa committed
    /**
     * Get the contents produced by this template.
     *
     * @return string The HTML rendered by this template, as a string.
     * @throws \Exception if the template cannot be found.
Hanne Moa's avatar
Hanne Moa committed
     */
Tim van Dijen's avatar
Tim van Dijen committed
        $this->twigDefaultContext();
Tim van Dijen's avatar
Tim van Dijen committed
        if ($this->controller) {
            $this->controller->display($this->data);
Hanne Moa's avatar
Hanne Moa committed
        }
Tim van Dijen's avatar
Tim van Dijen committed
        try {
            return $this->twig->render($this->twig_template, $this->data);
        } catch (RuntimeError $e) {
            throw new Error\Exception(substr($e->getMessage(), 0, -1) . ' in ' . $this->template, 0, $e);
     * @return $this This response.
     * @throws \Exception if the template cannot be found.
     *
     * Note: No return type possible due to upstream limitations
Sascha Grossenbacher's avatar
Sascha Grossenbacher committed
    public function send(): static
    {
        $this->content = $this->getContents();
        return parent::send();
    }


    /**
     * Find module the template is in, if any
     *
     * @param string $template The relative path from the theme directory to the template file.
     *
     * @return array An array with the name of the module and template
     */
    private function findModuleAndTemplateName(string $template): array
    {
        $tmp = explode(':', $template, 2);
        return (count($tmp) === 2) ? [$tmp[0], $tmp[1]] : [null, $tmp[0]];
    /**
     * Return the internal translator object used by this template.
     *
     * @return \SimpleSAML\Locale\Translate The translator that will be used with this template.
     */
    public function getTranslator(): Translate
    {
        return $this->translator;
Hanne Moa's avatar
Hanne Moa committed
    }
    /**
     * Return the internal localization object used by this template.
     *
     * @return \SimpleSAML\Locale\Localization The localization object that will be used with this template.
     */
    public function getLocalization(): Localization
    /**
     * Get the current instance of Twig in use.
     *
     * @return \Twig\Environment The Twig instance in use.
    public function getTwig(): \Twig\Environment
Tim van Dijen's avatar
Tim van Dijen committed
     * @return string[]
    private function getLanguageList(): array
Jaime Perez Crespo's avatar
Jaime Perez Crespo committed
        return $this->translator->getLanguage()->getLanguageList();
     *
     * @return bool
    private function isLanguageRTL(): bool
Jaime Perez Crespo's avatar
Jaime Perez Crespo committed
        return $this->translator->getLanguage()->isLanguageRTL();

    /**
     * Search through entity metadata to find the best display name for this
     * entity. It will search in order for the current language, default
     * language and fallback language for the DisplayName, name, OrganizationDisplayName
     * and OrganizationName; the first one found is considered the best match.
     * If nothing found, will return the entityId.
     */
    public function getEntityDisplayName(array $data): string
    {
        $tryLanguages = $this->translator->getLanguage()->getPreferredLanguages();

Tim van Dijen's avatar
Tim van Dijen committed
        foreach ($tryLanguages as $language) {
            if (isset($data['UIInfo']['DisplayName'][$language])) {
                return $data['UIInfo']['DisplayName'][$language];
            } elseif (isset($data['name'][$language])) {
                return $data['name'][$language];
            } elseif (isset($data['OrganizationDisplayName'][$language])) {
                return $data['OrganizationDisplayName'][$language];
            } elseif (isset($data['OrganizationName'][$language])) {
                return $data['OrganizationName'][$language];
            }
        }
        return $data['entityid'];
    }

    /**
     * Search through entity metadata to find the best value for a
     * specific property. It will search in order for the current language, default
     * language and fallback language; it will return the property value (which
Tim van Dijen's avatar
Tim van Dijen committed
     * can be a string, array or other type allowed in metadata, if not found it
    public function getEntityPropertyTranslation(string $property, array $data)
    {
        $tryLanguages = $this->translator->getLanguage()->getPreferredLanguages();

Tim van Dijen's avatar
Tim van Dijen committed
        foreach ($tryLanguages as $language) {
            if (isset($data[$property][$language])) {
                return $data[$property][$language];
            }
        }

        return null;
    }