[ Index ]

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1