Verzeichnisstruktur phpBB-3.3.16


Veröffentlicht
27.04.2026

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

twig.php

Zuletzt modifiziert: 01.05.2026, 11:26 - Dateigröße: 9.68 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  use phpbb\template\exception\user_object_not_available;
 017   
 018  /**
 019  * Twig Template class.
 020  */
 021  class twig extends \phpbb\template\base
 022  {
 023      /**
 024      * Path of the cache directory for the template
 025      *
 026      * Cannot be changed during runtime.
 027      *
 028      * @var string
 029      */
 030      private $cachepath = '';
 031   
 032      /**
 033      * phpBB path helper
 034      * @var \phpbb\path_helper
 035      */
 036      protected $path_helper;
 037   
 038      /**
 039      * phpBB root path
 040      * @var string
 041      */
 042      protected $phpbb_root_path;
 043   
 044      /**
 045      * PHP file extension
 046      * @var string
 047      */
 048      protected $php_ext;
 049   
 050      /**
 051      * phpBB config instance
 052      * @var \phpbb\config\config
 053      */
 054      protected $config;
 055   
 056      /**
 057      * Current user
 058      * @var \phpbb\user
 059      */
 060      protected $user;
 061   
 062      /**
 063      * Extension manager.
 064      *
 065      * @var \phpbb\extension\manager
 066      */
 067      protected $extension_manager;
 068   
 069      /**
 070      * Twig Environment
 071      *
 072      * @var \Twig\Environment
 073      */
 074      protected $twig;
 075   
 076      /**
 077      * Constructor.
 078      *
 079      * @param \phpbb\path_helper $path_helper
 080      * @param \phpbb\config\config $config
 081      * @param \phpbb\template\context $context template context
 082      * @param \phpbb\template\twig\environment $twig_environment
 083      * @param string $cache_path
 084      * @param \phpbb\user|null $user
 085      * @param array|\ArrayAccess $extensions
 086      * @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked
 087      */
 088      public function __construct(\phpbb\path_helper $path_helper, $config, \phpbb\template\context $context, \phpbb\template\twig\environment $twig_environment, $cache_path, \phpbb\user $user = null, $extensions = array(), \phpbb\extension\manager $extension_manager = null)
 089      {
 090          $this->path_helper = $path_helper;
 091          $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
 092          $this->php_ext = $path_helper->get_php_ext();
 093          $this->config = $config;
 094          $this->user = $user;
 095          $this->context = $context;
 096          $this->extension_manager = $extension_manager;
 097          $this->cachepath = $cache_path;
 098          $this->twig = $twig_environment;
 099   
 100          foreach ($extensions as $extension)
 101          {
 102              if (!$this->twig->hasExtension(get_class($extension)))
 103              {
 104                  $this->twig->addExtension($extension);
 105              }
 106          }
 107   
 108          // Add admin namespace
 109          if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/'))
 110          {
 111              $this->twig->getLoader()->setPaths($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin');
 112          }
 113      }
 114   
 115      /**
 116      * Clear the cache
 117      *
 118      * @return \phpbb\template\template
 119      */
 120      public function clear_cache()
 121      {
 122          if (is_dir($this->cachepath))
 123          {
 124              $this->twig->clearCacheFiles();
 125          }
 126   
 127          return $this;
 128      }
 129   
 130      /**
 131      * Get the style tree of the style preferred by the current user
 132      *
 133      * @return array Style tree, most specific first
 134      *
 135      * @throws user_object_not_available    When user service was not set
 136      */
 137      public function get_user_style()
 138      {
 139          if ($this->user === null)
 140          {
 141              throw new user_object_not_available();
 142          }
 143   
 144          $style_list = array(
 145              $this->user->style['style_path'],
 146          );
 147   
 148          if ($this->user->style['style_parent_id'])
 149          {
 150              $style_list = array_merge($style_list, array_reverse(explode('/', $this->user->style['style_parent_tree'])));
 151          }
 152   
 153          return $style_list;
 154      }
 155   
 156      /**
 157      * Set style location based on (current) user's chosen style.
 158      *
 159      * @param array $style_directories The directories to add style paths for
 160      *     E.g. array('ext/foo/bar/styles', 'styles')
 161      *     Default: array('styles') (phpBB's style directory)
 162      * @return \phpbb\template\template $this
 163      */
 164      public function set_style($style_directories = array('styles'))
 165      {
 166          if ($style_directories !== array('styles') && $this->twig->getLoader()->getPaths('core') === array())
 167          {
 168              // We should set up the core styles path since not already setup
 169              $this->set_style();
 170          }
 171   
 172          $names = $this->get_user_style();
 173          // Add 'all' folder to $names array
 174          //    It allows extensions to load a template file from 'all' folder,
 175          //    if a style doesn't include it.
 176          $names[] = 'all';
 177   
 178          $paths = array();
 179          foreach ($style_directories as $directory)
 180          {
 181              foreach ($names as $name)
 182              {
 183                  $path = $this->phpbb_root_path . trim($directory, '/') . "/{$name}/";
 184                  $template_path = $path . 'template/';
 185                  $theme_path = $path . 'theme/';
 186   
 187                  $is_valid_dir = false;
 188                  if (is_dir($template_path))
 189                  {
 190                      $is_valid_dir = true;
 191                      $paths[] = $template_path;
 192                  }
 193                  if (is_dir($theme_path))
 194                  {
 195                      $is_valid_dir = true;
 196                      $paths[] = $theme_path;
 197                  }
 198   
 199                  if ($is_valid_dir)
 200                  {
 201                      // Add the base style directory as a safe directory
 202                      $this->twig->getLoader()->addSafeDirectory($path);
 203                  }
 204              }
 205          }
 206   
 207          // If we're setting up the main phpBB styles directory and the core
 208          // namespace isn't setup yet, we will set it up now
 209          if ($style_directories === array('styles') && $this->twig->getLoader()->getPaths('core') === array())
 210          {
 211              // Set up the core style paths namespace
 212              $this->twig->getLoader()->setPaths($paths, 'core');
 213          }
 214   
 215          $this->set_custom_style($names, $paths);
 216   
 217          return $this;
 218      }
 219   
 220      /**
 221      * Set custom style location (able to use directory outside of phpBB).
 222      *
 223      * Note: Templates are still compiled to phpBB's cache directory.
 224      *
 225      * @param string|array $names Array of names (or detailed names) or string of name of template(s) in inheritance tree order, used by extensions.
 226      *    E.g. array(
 227      *            'name'         => 'adm',
 228      *            'ext_path'     => 'adm/style/',
 229      *        )
 230      * @param string|array $paths Array of style paths, relative to current root directory
 231      * @return \phpbb\template\template $this
 232      */
 233      public function set_custom_style($names, $paths)
 234      {
 235          $paths = (is_string($paths)) ? array($paths) : $paths;
 236          $names = (is_string($names)) ? array($names) : $names;
 237   
 238          // Set as __main__ namespace
 239          $this->twig->getLoader()->setPaths($paths);
 240   
 241          // Add all namespaces for all extensions
 242          if ($this->extension_manager instanceof \phpbb\extension\manager)
 243          {
 244              $names[] = 'all';
 245   
 246              foreach ($this->extension_manager->all_enabled() as $ext_namespace => $ext_path)
 247              {
 248                  // namespaces cannot contain /
 249                  $namespace = str_replace('/', '_', $ext_namespace);
 250                  $paths = array();
 251   
 252                  foreach ($names as $template_dir)
 253                  {
 254                      if (is_array($template_dir))
 255                      {
 256                          if (isset($template_dir['ext_path']))
 257                          {
 258                              $ext_style_template_path = $ext_path . $template_dir['ext_path'];
 259                              $ext_style_path = dirname($ext_style_template_path);
 260                              $ext_style_theme_path = $ext_style_path . 'theme/';
 261                          }
 262                          else
 263                          {
 264                              $ext_style_path = $ext_path . 'styles/' . $template_dir['name'] . '/';
 265                              $ext_style_template_path = $ext_style_path . 'template/';
 266                              $ext_style_theme_path = $ext_style_path . 'theme/';
 267                          }
 268                      }
 269                      else
 270                      {
 271                          $ext_style_path = $ext_path . 'styles/' . $template_dir . '/';
 272                          $ext_style_template_path = $ext_style_path . 'template/';
 273                          $ext_style_theme_path = $ext_style_path . 'theme/';
 274                      }
 275   
 276                      $is_valid_dir = false;
 277                      if (is_dir($ext_style_template_path))
 278                      {
 279                          $is_valid_dir = true;
 280                          $paths[] = $ext_style_template_path;
 281                      }
 282                      if (is_dir($ext_style_theme_path))
 283                      {
 284                          $is_valid_dir = true;
 285                          $paths[] = $ext_style_theme_path;
 286                      }
 287   
 288                      if ($is_valid_dir)
 289                      {
 290                          // Add the base style directory as a safe directory
 291                          $this->twig->getLoader()->addSafeDirectory($ext_style_path);
 292                      }
 293                  }
 294   
 295                  $this->twig->getLoader()->setPaths($paths, $namespace);
 296              }
 297          }
 298   
 299          return $this;
 300      }
 301   
 302      /**
 303      * Display a template for provided handle.
 304      *
 305      * The template will be loaded and compiled, if necessary, first.
 306      *
 307      * This function calls hooks.
 308      *
 309      * @param string $handle Handle to display
 310      * @return \phpbb\template\template $this
 311      */
 312      public function display($handle)
 313      {
 314          $result = $this->call_hook($handle, __FUNCTION__);
 315          if ($result !== false)
 316          {
 317              return $result[0];
 318          }
 319   
 320          $this->twig->display($this->get_filename_from_handle($handle), $this->get_template_vars());
 321   
 322          return $this;
 323      }
 324   
 325      /**
 326      * Display the handle and assign the output to a template variable
 327      * or return the compiled result.
 328      *
 329      * @param string $handle Handle to operate on
 330      * @param string $template_var Template variable to assign compiled handle to
 331      * @param bool $return_content If true return compiled handle, otherwise assign to $template_var
 332      * @return \phpbb\template\template|string if $return_content is true return string of the compiled handle, otherwise return $this
 333      */
 334      public function assign_display($handle, $template_var = '', $return_content = true)
 335      {
 336          if ($return_content)
 337          {
 338              return $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars());
 339          }
 340   
 341          $this->assign_var($template_var, $this->twig->render($this->get_filename_from_handle($handle), $this->get_template_vars()));
 342   
 343          return $this;
 344      }
 345   
 346      /**
 347      * Get template vars in a format Twig will use (from the context)
 348      *
 349      * @return array
 350      */
 351      protected function get_template_vars()
 352      {
 353          $context_vars = $this->context->get_data_ref();
 354   
 355          $vars = array_merge(
 356              $context_vars['.'][0], // To get normal vars
 357              array(
 358                  'definition'    => new \phpbb\template\twig\definition(),
 359                  'loops'            => $context_vars, // To get loops
 360              )
 361          );
 362   
 363          if ($this->user instanceof \phpbb\user)
 364          {
 365              $vars['user'] = $this->user;
 366          }
 367   
 368          // cleanup
 369          unset($vars['loops']['.']);
 370   
 371          // Inject in the main context the value added by assign_block_vars() to be able to use directly the Twig loops.
 372          foreach ($vars['loops'] as $key => &$value)
 373          {
 374              $vars[$key] = $value;
 375          }
 376   
 377          return $vars;
 378      }
 379   
 380      /**
 381      * {@inheritdoc}
 382      */
 383      public function get_source_file_for_handle($handle)
 384      {
 385          return $this->twig->getLoader()->getCacheKey($this->get_filename_from_handle($handle));
 386      }
 387  }
 388