[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/phpbb/template/ -> asset.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\template;
  15  
  16  class asset
  17  {
  18      protected $components = array();
  19  
  20      /** @var \phpbb\path_helper **/
  21      protected $path_helper;
  22  
  23      /** @var \phpbb\filesystem\filesystem */
  24      protected $filesystem;
  25  
  26      /**
  27      * Constructor
  28      *
  29      * @param string $url URL
  30      * @param \phpbb\path_helper $path_helper Path helper object
  31      * @param \phpbb\filesystem\filesystem $filesystem
  32      */
  33  	public function __construct($url, \phpbb\path_helper $path_helper, \phpbb\filesystem\filesystem $filesystem)
  34      {
  35          $this->path_helper = $path_helper;
  36          $this->filesystem = $filesystem;
  37  
  38          $this->set_url($url);
  39      }
  40  
  41      /**
  42      * Set URL
  43      *
  44      * @param string $url URL
  45      */
  46  	public function set_url($url)
  47      {
  48          $this->components = parse_url($url);
  49      }
  50  
  51      /**
  52      * Convert URL components into string
  53      *
  54      * @param array $components URL components
  55      * @return string URL
  56      */
  57  	protected function join_url($components)
  58      {
  59          $path = '';
  60          if (isset($components['scheme']))
  61          {
  62              $path = $components['scheme'] === '' ? '//' : $components['scheme'] . '://';
  63          }
  64  
  65          if (isset($components['user']) || isset($components['pass']))
  66          {
  67              if ($path === '' && !isset($components['port']))
  68              {
  69                  $path = '//';
  70              }
  71              $path .= $components['user'];
  72              if (isset($components['pass']))
  73              {
  74                  $path .= ':' . $components['pass'];
  75              }
  76              $path .= '@';
  77          }
  78  
  79          if (isset($components['host']))
  80          {
  81              if ($path === '' && !isset($components['port']))
  82              {
  83                  $path = '//';
  84              }
  85              $path .= $components['host'];
  86              if (isset($components['port']))
  87              {
  88                  $path .= ':' . $components['port'];
  89              }
  90          }
  91  
  92          if (isset($components['path']))
  93          {
  94              $path .= $components['path'];
  95          }
  96  
  97          if (isset($components['query']))
  98          {
  99              $path .= '?' . $components['query'];
 100          }
 101  
 102          if (isset($components['fragment']))
 103          {
 104              $path .= '#' . $components['fragment'];
 105          }
 106  
 107          return $path;
 108      }
 109  
 110      /**
 111      * Get URL
 112      *
 113      * @return string URL
 114      */
 115  	public function get_url()
 116      {
 117          return $this->path_helper->update_web_root_path($this->join_url($this->components));
 118      }
 119  
 120      /**
 121      * Checks if URL is local and relative
 122      *
 123      * @return boolean True if URL is local and relative
 124      */
 125  	public function is_relative()
 126      {
 127          if (empty($this->components) || !isset($this->components['path']))
 128          {
 129              // Invalid URL
 130              return false;
 131          }
 132          return !isset($this->components['scheme']) && !isset($this->components['host']) && substr($this->components['path'], 0, 1) !== '/';
 133      }
 134  
 135      /**
 136      * Get path component of current URL
 137      *
 138      * @return string Path
 139      */
 140  	public function get_path()
 141      {
 142          return isset($this->components['path']) ? $this->components['path'] : '';
 143      }
 144  
 145      /**
 146      * Set path component
 147      *
 148      * @param string $path Path component
 149      * @param boolean $urlencode If true, parts of path should be encoded with rawurlencode()
 150      */
 151  	public function set_path($path, $urlencode = false)
 152      {
 153          // Since 1.7.0 Twig returns the real path of the file. We need it to be relative.
 154          $real_root_path = $this->filesystem->realpath($this->path_helper->get_phpbb_root_path()) . DIRECTORY_SEPARATOR;
 155  
 156          // If the asset is under the phpBB root path we need to remove its path and then prepend $phpbb_root_path
 157          if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path)
 158          {
 159              $path = $this->path_helper->get_phpbb_root_path() . str_replace('\\', '/', substr($path, strlen($real_root_path)));
 160          }
 161          else
 162          {
 163              // Else we make the path relative to the current working directory
 164              $real_root_path = $this->filesystem->realpath('.') . DIRECTORY_SEPARATOR;
 165              if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path)
 166              {
 167                  $path = str_replace('\\', '/', substr($path, strlen($real_root_path)));
 168              }
 169          }
 170  
 171          if ($urlencode)
 172          {
 173              $paths = explode('/', $path);
 174              foreach ($paths as &$dir)
 175              {
 176                  $dir = rawurlencode($dir);
 177              }
 178              $path = implode('/', $paths);
 179          }
 180  
 181          $this->components['path'] = $path;
 182      }
 183  
 184      /**
 185      * Add assets_version parameter to URL.
 186      * Parameter will not be added if assets_version already exists in URL
 187      *
 188      * @param string $version Version
 189      */
 190  	public function add_assets_version($version)
 191      {
 192          if (!isset($this->components['query']))
 193          {
 194              $this->components['query'] = 'assets_version=' . $version;
 195              return;
 196          }
 197          $query = $this->components['query'];
 198          if (!preg_match('/(^|[&;])assets_version=/', $query))
 199          {
 200              $this->components['query'] = $query . '&amp;assets_version=' . $version;
 201          }
 202      }
 203  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1