Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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.24 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      * Set safe directories
 025      *
 026      * @param array $directories Array of directories that are safe (empty to clear)
 027      * @return \Twig_Loader_Filesystem
 028      */
 029      public function setSafeDirectories($directories = array())
 030      {
 031          $this->safe_directories = array();
 032   
 033          if (!empty($directories))
 034          {
 035              foreach ($directories as $directory)
 036              {
 037                  $this->addSafeDirectory($directory);
 038              }
 039          }
 040   
 041          return $this;
 042      }
 043   
 044      /**
 045      * Add safe directory
 046      *
 047      * @param string $directory Directory that should be added
 048      * @return \Twig_Loader_Filesystem
 049      */
 050      public function addSafeDirectory($directory)
 051      {
 052          $directory = phpbb_realpath($directory);
 053   
 054          if ($directory !== false)
 055          {
 056              $this->safe_directories[] = $directory;
 057          }
 058   
 059          return $this;
 060      }
 061   
 062      /**
 063      * Get current safe directories
 064      *
 065      * @return array
 066      */
 067      public function getSafeDirectories()
 068      {
 069          return $this->safe_directories;
 070      }
 071   
 072      /**
 073      * Override for parent::validateName()
 074      *
 075      * This is done because we added support for safe directories, and when Twig
 076      *    findTemplate() is called, validateName() is called first, which would
 077      *    always throw an exception if the file is outside of the configured
 078      *    template directories.
 079      */
 080      protected function validateName($name)
 081      {
 082          return;
 083      }
 084   
 085      /**
 086      * Find the template
 087      *
 088      * Override for Twig_Loader_Filesystem::findTemplate to add support
 089      *    for loading from safe directories.
 090      */
 091      protected function findTemplate($name)
 092      {
 093          $name = (string) $name;
 094   
 095          // normalize name
 096          $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
 097   
 098          // If this is in the cache we can skip the entire process below
 099          //    as it should have already been validated
 100          if (isset($this->cache[$name])) {
 101              return $this->cache[$name];
 102          }
 103   
 104          // First, find the template name. The override above of validateName
 105          //    causes the validateName process to be skipped for this call
 106          $file = parent::findTemplate($name);
 107   
 108          try
 109          {
 110              // Try validating the name (which may throw an exception)
 111              parent::validateName($name);
 112          }
 113          catch (\Twig_Error_Loader $e)
 114          {
 115              if (strpos($e->getRawMessage(), 'Looks like you try to load a template outside configured directories') === 0)
 116              {
 117                  // Ok, so outside of the configured template directories, we
 118                  //    can now check if we're within a "safe" directory
 119   
 120                  // Find the real path of the directory the file is in
 121                  $directory = phpbb_realpath(dirname($file));
 122   
 123                  if ($directory === false)
 124                  {
 125                      // Some sort of error finding the actual path, must throw the exception
 126                      throw $e;
 127                  }
 128   
 129                  foreach ($this->safe_directories as $safe_directory)
 130                  {
 131                      if (strpos($directory, $safe_directory) === 0)
 132                      {
 133                          // The directory being loaded is below a directory
 134                          // that is "safe". We're good to load it!
 135                          return $file;
 136                      }
 137                  }
 138              }
 139   
 140              // Not within any safe directories
 141              throw $e;
 142          }
 143   
 144          // No exception from validateName, safe to load.
 145          return $file;
 146      }
 147  }
 148