Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

loader.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 3.92 KiB


001  <?php
002  /**
003  *
004  * This file is part of the phpBB Forum Software package.
005  *
006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
007  * @license GNU General Public License, version 2 (GPL-2.0)
008  *
009  * For full copyright and license information, please see
010  * the docs/CREDITS.txt file.
011  *
012  */
013   
014  namespace phpbb\template\twig;
015   
016  /**
017  * Twig Template loader
018  */
019  class loader extends \Twig_Loader_Filesystem
020  {
021      protected $safe_directories = array();
022   
023      /**
024       * @var \phpbb\filesystem\filesystem_interface
025       */
026      protected $filesystem;
027   
028      /**
029       * Constructor
030       *
031       * @param \phpbb\filesystem\filesystem_interface $filesystem
032       * @param string|array    $paths
033       */
034      public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, $paths = array())
035      {
036          $this->filesystem = $filesystem;
037   
038          parent::__construct($paths);
039      }
040   
041      /**
042      * Set safe directories
043      *
044      * @param array $directories Array of directories that are safe (empty to clear)
045      * @return \Twig_Loader_Filesystem
046      */
047      public function setSafeDirectories($directories = array())
048      {
049          $this->safe_directories = array();
050   
051          if (!empty($directories))
052          {
053              foreach ($directories as $directory)
054              {
055                  $this->addSafeDirectory($directory);
056              }
057          }
058   
059          return $this;
060      }
061   
062      /**
063      * Add safe directory
064      *
065      * @param string $directory Directory that should be added
066      * @return \Twig_Loader_Filesystem
067      */
068      public function addSafeDirectory($directory)
069      {
070          $directory = $this->filesystem->realpath($directory);
071   
072          if ($directory !== false)
073          {
074              $this->safe_directories[] = $directory;
075          }
076   
077          return $this;
078      }
079   
080      /**
081      * Get current safe directories
082      *
083      * @return array
084      */
085      public function getSafeDirectories()
086      {
087          return $this->safe_directories;
088      }
089   
090      /**
091      * Override for parent::validateName()
092      *
093      * This is done because we added support for safe directories, and when Twig
094      *    findTemplate() is called, validateName() is called first, which would
095      *    always throw an exception if the file is outside of the configured
096      *    template directories.
097      */
098      protected function validateName($name)
099      {
100          return;
101      }
102   
103      /**
104       * Adds a realpath call to fix a BC break in Twig 1.26 (https://github.com/twigphp/Twig/issues/2145)
105       *
106       * {@inheritdoc}
107       */
108      public function addPath($path, $namespace = self::MAIN_NAMESPACE)
109      {
110          return parent::addPath($this->filesystem->realpath($path), $namespace);
111      }
112   
113      /**
114      * Find the template
115      *
116      * Override for Twig_Loader_Filesystem::findTemplate to add support
117      *    for loading from safe directories.
118      */
119      protected function findTemplate($name)
120      {
121          $name = (string) $name;
122   
123          // normalize name
124          $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
125   
126          // If this is in the cache we can skip the entire process below
127          //    as it should have already been validated
128          if (isset($this->cache[$name]))
129          {
130              return $this->cache[$name];
131          }
132   
133          // First, find the template name. The override above of validateName
134          //    causes the validateName process to be skipped for this call
135          $file = parent::findTemplate($name);
136   
137          try
138          {
139              // Try validating the name (which may throw an exception)
140              parent::validateName($name);
141          }
142          catch (\Twig_Error_Loader $e)
143          {
144              if (strpos($e->getRawMessage(), 'Looks like you try to load a template outside configured directories') === 0)
145              {
146                  // Ok, so outside of the configured template directories, we
147                  //    can now check if we're within a "safe" directory
148   
149                  // Find the real path of the directory the file is in
150                  $directory = $this->filesystem->realpath(dirname($file));
151   
152                  if ($directory === false)
153                  {
154                      // Some sort of error finding the actual path, must throw the exception
155                      throw $e;
156                  }
157   
158                  foreach ($this->safe_directories as $safe_directory)
159                  {
160                      if (strpos($directory, $safe_directory) === 0)
161                      {
162                          // The directory being loaded is below a directory
163                          // that is "safe". We're good to load it!
164                          return $file;
165                      }
166                  }
167              }
168   
169              // Not within any safe directories
170              throw $e;
171          }
172   
173          // No exception from validateName, safe to load.
174          return $file;
175      }
176  }
177