[ Index ]

PHP Cross Reference of phpBB-3.2.11-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          if (version_compare(PHP_VERSION, '5.4.7') < 0 && substr($url, 0, 2) === '//')
  49          {
  50              // Workaround for PHP 5.4.6 and older bug #62844 - add fake scheme and then remove it
  51              $this->components = parse_url('http:' . $url);
  52              $this->components['scheme'] = '';
  53              return;
  54          }
  55          $this->components = parse_url($url);
  56      }
  57  
  58      /**
  59      * Convert URL components into string
  60      *
  61      * @param array $components URL components
  62      * @return string URL
  63      */
  64  	protected function join_url($components)
  65      {
  66          $path = '';
  67          if (isset($components['scheme']))
  68          {
  69              $path = $components['scheme'] === '' ? '//' : $components['scheme'] . '://';
  70          }
  71  
  72          if (isset($components['user']) || isset($components['pass']))
  73          {
  74              if ($path === '' && !isset($components['port']))
  75              {
  76                  $path = '//';
  77              }
  78              $path .= $components['user'];
  79              if (isset($components['pass']))
  80              {
  81                  $path .= ':' . $components['pass'];
  82              }
  83              $path .= '@';
  84          }
  85  
  86          if (isset($components['host']))
  87          {
  88              if ($path === '' && !isset($components['port']))
  89              {
  90                  $path = '//';
  91              }
  92              $path .= $components['host'];
  93              if (isset($components['port']))
  94              {
  95                  $path .= ':' . $components['port'];
  96              }
  97          }
  98  
  99          if (isset($components['path']))
 100          {
 101              $path .= $components['path'];
 102          }
 103  
 104          if (isset($components['query']))
 105          {
 106              $path .= '?' . $components['query'];
 107          }
 108  
 109          if (isset($components['fragment']))
 110          {
 111              $path .= '#' . $components['fragment'];
 112          }
 113  
 114          return $path;
 115      }
 116  
 117      /**
 118      * Get URL
 119      *
 120      * @return string URL
 121      */
 122  	public function get_url()
 123      {
 124          return $this->path_helper->update_web_root_path($this->join_url($this->components));
 125      }
 126  
 127      /**
 128      * Checks if URL is local and relative
 129      *
 130      * @return boolean True if URL is local and relative
 131      */
 132  	public function is_relative()
 133      {
 134          if (empty($this->components) || !isset($this->components['path']))
 135          {
 136              // Invalid URL
 137              return false;
 138          }
 139          return !isset($this->components['scheme']) && !isset($this->components['host']) && substr($this->components['path'], 0, 1) !== '/';
 140      }
 141  
 142      /**
 143      * Get path component of current URL
 144      *
 145      * @return string Path
 146      */
 147  	public function get_path()
 148      {
 149          return isset($this->components['path']) ? $this->components['path'] : '';
 150      }
 151  
 152      /**
 153      * Set path component
 154      *
 155      * @param string $path Path component
 156      * @param boolean $urlencode If true, parts of path should be encoded with rawurlencode()
 157      */
 158  	public function set_path($path, $urlencode = false)
 159      {
 160          // Since 1.7.0 Twig returns the real path of the file. We need it to be relative.
 161          $real_root_path = $this->filesystem->realpath($this->path_helper->get_phpbb_root_path()) . DIRECTORY_SEPARATOR;
 162  
 163          // If the asset is under the phpBB root path we need to remove its path and then prepend $phpbb_root_path
 164          if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path)
 165          {
 166              $path = $this->path_helper->get_phpbb_root_path() . str_replace('\\', '/', substr($path, strlen($real_root_path)));
 167          }
 168          else
 169          {
 170              // Else we make the path relative to the current working directory
 171              $real_root_path = $this->filesystem->realpath('.') . DIRECTORY_SEPARATOR;
 172              if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path)
 173              {
 174                  $path = str_replace('\\', '/', substr($path, strlen($real_root_path)));
 175              }
 176          }
 177  
 178          if ($urlencode)
 179          {
 180              $paths = explode('/', $path);
 181              foreach ($paths as &$dir)
 182              {
 183                  $dir = rawurlencode($dir);
 184              }
 185              $path = implode('/', $paths);
 186          }
 187  
 188          $this->components['path'] = $path;
 189      }
 190  
 191      /**
 192      * Add assets_version parameter to URL.
 193      * Parameter will not be added if assets_version already exists in URL
 194      *
 195      * @param string $version Version
 196      */
 197  	public function add_assets_version($version)
 198      {
 199          if (!isset($this->components['query']))
 200          {
 201              $this->components['query'] = 'assets_version=' . $version;
 202              return;
 203          }
 204          $query = $this->components['query'];
 205          if (!preg_match('/(^|[&;])assets_version=/', $query))
 206          {
 207              $this->components['query'] = $query . '&amp;assets_version=' . $version;
 208          }
 209      }
 210  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1