<commit_before><?php


/**
 * A minimalistic XHTML PHP based template system implemented for SimpleSAMLphp.
 *
 * @author <NAME>, UNINETT AS. <<EMAIL>>
 * @package SimpleSAMLphp
 */


use JaimePerez\TwigConfigurableI18n\Twig\Environment as Twig_Environment;
use JaimePerez\TwigConfigurableI18n\Twig\Extensions\Extension\I18n as Twig_Extensions_Extension_I18n;


class SimpleSAML_XHTML_Template
{

    /**
     * The data associated with this template, accessible within the template itself.
     *
     * @var array
     */
    public $data = array();

    /**
     * A translator instance configured to work with this template.
     *
     * @var \SimpleSAML\Locale\Translate
     */
    private $translator;

    /**
     * The localization backend
     *
     * @var \SimpleSAML\Locale\Localization
     */
    private $localization;

    /**
     * The configuration to use in this template.
     *
     * @var SimpleSAML_Configuration
     */
    private $configuration;

    /**
     * The file to load in this template.
     *
     * @var string
     */
    private $template = 'default.php';

    /**
     * The twig environment.
     *
     * @var false|Twig_Environment
     */
    private $twig;

    /**
     * The template name.
     *
     * @var string
     */
    private $twig_template;

    /**
     * Current module, if any.
     */
    private $module;

    /**
     * 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
     */
    private $controller;


    /**
     * 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 defaults
     * to false.
     *
     * @var bool|array
     */
    private $theme;

    /**
     * Constructor
     *
     * @param SimpleSAML_Configuration $configuration Configuration object
     * @param string                   $template Which template file to load
     * @param string|null              $defaultDictionary The default dictionary where tags will come from.
     */
    public function __construct(\SimpleSAML_Configuration $configuration, $template, $defaultDictionary = null)
    {
        $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']) = self::findModuleAndTemplateName(
            $this->configuration->getString('theme.use', 'default')
        );

        // initialize internationalization system
        $this->translator = new SimpleSAML\Locale\Translate($configuration, $defaultDictionary);
        $this->localization = new \SimpleSAML\Locale\Localization($configuration);

        // check if we need to attach a theme controller
        $controller = $this->configuration->getString('theme.controller', false);
        if ($controller && class_exists($controller) &&
            class_implements($controller, '\SimpleSAML\XHTML\TemplateControllerInterface')
        ) {
            $this->controller = new $controller();
        }

        $this->twig = $this->setupTwig();
    }


    /**
     * 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)
    {
        if (strripos($templateName, '.twig')) {
            return $templateName;
        }
        $phppos = strripos($templateName, '.php');
        if ($phppos) {
            $templateName = substr($templateName, 0, $phppos);
        }
        $tplpos = strripos($templateName, '.tpl');
        if ($tplpos) {
            $templateName = substr($templateName, 0, $tplpos);
        }
        return $templateName.'.twig';
    }


    /**
     * 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()
    {
        $filename = $this->normalizeTemplateName($this->template);

        // get namespace if any
        list($namespace, $filename) = self::findModuleAndTemplateName($filename);
        $this->twig_template = ($namespace !== null) ? '@'.$namespace.'/'.$filename : $filename;
        $loader = new \Twig_Loader_Filesystem();
        $templateDirs = $this->findThemeTemplateDirs();
        if ($this->module) {
            $templateDirs[] = array($this->module => $this->getModuleTemplateDir($this->module));
        }
        if ($this->theme['module']) {
            $templateDirs[] = array($this->theme['module'] => $this->getModuleTemplateDir($this->theme['module']));
        }

        // default, themeless templates are checked last
        $templateDirs[] = array(
            \Twig_Loader_Filesystem::MAIN_NAMESPACE => $this->configuration->resolvePath('templates')
        );
        foreach ($templateDirs as $entry) {
            $loader->addPath($entry[key($entry)], key($entry));
        }
        return $loader;
    }


    /**
     * Setup twig.
     */
    private function setupTwig()
    {
        $auto_reload = $this->configuration->getBoolean('template.auto_reload', true);
        $cache = $this->configuration->getString('template.cache', false);
        // set up template paths<commit_msg>bugfix: Do not crash when a module that contains a theme doesn't have a "templates" directory.<commit_after><?php


/**
 * A minimalistic XHTML PHP based template system implemented for SimpleSAMLphp.
 *
 * @author <NAME>, UNINETT AS. <<EMAIL>>
 * @package SimpleSAMLphp
 */


use JaimePerez\TwigConfigurableI18n\Twig\Environment as Twig_Environment;
use JaimePerez\TwigConfigurableI18n\Twig\Extensions\Extension\I18n as Twig_Extensions_Extension_I18n;


class SimpleSAML_XHTML_Template
{

    /**
     * The data associated with this template, accessible within the template itself.
     *
     * @var array
     */
    public $data = array();

    /**
     * A translator instance configured to work with this template.
     *
     * @var \SimpleSAML\Locale\Translate
     */
    private $translator;

    /**
     * The localization backend
     *
     * @var \SimpleSAML\Locale\Localization
     */
    private $localization;

    /**
     * The configuration to use in this template.
     *
     * @var SimpleSAML_Configuration
     */
    private $configuration;

    /**
     * The file to load in this template.
     *
     * @var string
     */
    private $template = 'default.php';

    /**
     * The twig environment.
     *
     * @var false|Twig_Environment
     */
    private $twig;

    /**
     * The template name.
     *
     * @var string
     */
    private $twig_template;

    /**
     * Current module, if any.
     */
    private $module;

    /**
     * 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
     */
    private $controller;


    /**
     * 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 defaults
     * to false.
     *
     * @var bool|array
     */
    private $theme;

    /**
     * Constructor
     *
     * @param SimpleSAML_Configuration $configuration Configuration object
     * @param string                   $template Which template file to load
     * @param string|null              $defaultDictionary The default dictionary where tags will come from.
     */
    public function __construct(\SimpleSAML_Configuration $configuration, $template, $defaultDictionary = null)
    {
        $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']) = self::findModuleAndTemplateName(
            $this->configuration->getString('theme.use', 'default')
        );

        // initialize internationalization system
        $this->translator = new SimpleSAML\Locale\Translate($configuration, $defaultDictionary);
        $this->localization = new \SimpleSAML\Locale\Localization($configuration);

        // check if we need to attach a theme controller
        $controller = $this->configuration->getString('theme.controller', false);
        if ($controller && class_exists($controller) &&
            class_implements($controller, '\SimpleSAML\XHTML\TemplateControllerInterface')
        ) {
            $this->controller = new $controller();
        }

        $this->twig = $this->setupTwig();
    }


    /**
     * 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)
    {
        if (strripos($templateName, '.twig')) {
            return $templateName;
        }
        $phppos = strripos($templateName, '.php');
        if ($phppos) {
            $templateName = substr($templateName, 0, $phppos);
        }
        $tplpos = strripos($templateName, '.tpl');
        if ($tplpos) {
            $templateName = substr($templateName, 0, $tplpos);
        }
        return $templateName.'.twig';
    }


    /**
     * 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()
    {
        $filename = $this->normalizeTemplateName($this->template);

        // get namespace if any
        list($namespace, $filename) = self::findModuleAndTemplateName($filename);
        $this->twig_template = ($namespace !== null) ? '@'.$namespace.'/'.$filename : $filename;
        $loader = new \Twig_Loader_Filesystem();
        $templateDirs = $this->findThemeTemplateDirs();
        if ($this->module) {
            $templateDirs[] = array($this->module => $this->getModuleTemplateDir($this->module));
        }
        if ($this->theme['module']) {
            try {
                $templateDirs[] = array($this->theme['module'] => $this->getModuleTemplateDir($this->theme['module']));
            } catch (\InvalidArgumentException $e) {
                // either the module is not enabled or it has no "templates" directory, ignore
            }
        }

        // default, themeless templates are checked last
        $templateDirs[] = array(
            \Twig_Loader_Filesystem::MAIN_NAMESPACE => $this->configuration->resolvePath('templates')
        );
        foreach ($templateDirs as $entry) {
            $loader->addPath($entry[key($entry)], key($entry));
        }
        return $loader;
    }


    /**
     * Setup twig.
     */
    private function setupTwig()
    {
        $auto_reload = $this->configuration->getBoolean('template.auto_reload', true);
        $cache = $this->configuration->getString('template.cache', false);
        // set up template paths
{'fraction_non_alphanumeric': 0.11607917356675342, 'fraction_numerical': 0.0015771626843308888, 'mean_word_length': 2.463134898962316, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5087836', 'n_tokens_mistral': 3466, 'n_tokens_neox': 3361, 'n_words': 955}