Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
6 / 8
CRAP
85.19% covered (warning)
85.19%
46 / 54
asset
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
6 / 8
38.98
85.19% covered (warning)
85.19%
46 / 54
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 set_url
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 join_url
0.00% covered (danger)
0.00%
0 / 1
21.34
69.57% covered (warning)
69.57%
16 / 23
 get_url
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 is_relative
0.00% covered (danger)
0.00%
0 / 1
5.93
66.67% covered (warning)
66.67%
2 / 3
 get_path
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 set_path
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
13 / 13
 add_assets_version
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\template;
class asset
{
    protected $components = array();
    /** @var \phpbb\path_helper **/
    protected $path_helper;
    /** @var \phpbb\filesystem\filesystem */
    protected $filesystem;
    /**
    * Constructor
    *
    * @param string $url URL
    * @param \phpbb\path_helper $path_helper Path helper object
    * @param \phpbb\filesystem\filesystem $filesystem
    */
    public function __construct($url, \phpbb\path_helper $path_helper, \phpbb\filesystem\filesystem $filesystem)
    {
        $this->path_helper = $path_helper;
        $this->filesystem = $filesystem;
        $this->set_url($url);
    }
    /**
    * Set URL
    *
    * @param string $url URL
    */
    public function set_url($url)
    {
        $this->components = parse_url($url);
    }
    /**
    * Convert URL components into string
    *
    * @param array $components URL components
    * @return string URL
    */
    protected function join_url($components)
    {
        $path = '';
        if (isset($components['scheme']))
        {
            $path = $components['scheme'] === '' ? '//' : $components['scheme'] . '://';
        }
        if (isset($components['user']) || isset($components['pass']))
        {
            if ($path === '' && !isset($components['port']))
            {
                $path = '//';
            }
            $path .= $components['user'];
            if (isset($components['pass']))
            {
                $path .= ':' . $components['pass'];
            }
            $path .= '@';
        }
        if (isset($components['host']))
        {
            if ($path === '' && !isset($components['port']))
            {
                $path = '//';
            }
            $path .= $components['host'];
            if (isset($components['port']))
            {
                $path .= ':' . $components['port'];
            }
        }
        if (isset($components['path']))
        {
            $path .= $components['path'];
        }
        if (isset($components['query']))
        {
            $path .= '?' . $components['query'];
        }
        if (isset($components['fragment']))
        {
            $path .= '#' . $components['fragment'];
        }
        return $path;
    }
    /**
    * Get URL
    *
    * @return string URL
    */
    public function get_url()
    {
        return $this->path_helper->update_web_root_path($this->join_url($this->components));
    }
    /**
    * Checks if URL is local and relative
    *
    * @return boolean True if URL is local and relative
    */
    public function is_relative()
    {
        if (empty($this->components) || !isset($this->components['path']))
        {
            // Invalid URL
            return false;
        }
        return !isset($this->components['scheme']) && !isset($this->components['host']) && substr($this->components['path'], 0, 1) !== '/';
    }
    /**
    * Get path component of current URL
    *
    * @return string Path
    */
    public function get_path()
    {
        return isset($this->components['path']) ? $this->components['path'] : '';
    }
    /**
    * Set path component
    *
    * @param string $path Path component
    * @param boolean $urlencode If true, parts of path should be encoded with rawurlencode()
    */
    public function set_path($path, $urlencode = false)
    {
        // Since 1.7.0 Twig returns the real path of the file. We need it to be relative.
        $real_root_path = $this->filesystem->realpath($this->path_helper->get_phpbb_root_path()) . DIRECTORY_SEPARATOR;
        // If the asset is under the phpBB root path we need to remove its path and then prepend $phpbb_root_path
        if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path)
        {
            $path = $this->path_helper->get_phpbb_root_path() . str_replace('\\', '/', substr($path, strlen($real_root_path)));
        }
        else
        {
            // Else we make the path relative to the current working directory
            $real_root_path = $this->filesystem->realpath('.') . DIRECTORY_SEPARATOR;
            if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path)
            {
                $path = str_replace('\\', '/', substr($path, strlen($real_root_path)));
            }
        }
        if ($urlencode)
        {
            $paths = explode('/', $path);
            foreach ($paths as &$dir)
            {
                $dir = rawurlencode($dir);
            }
            $path = implode('/', $paths);
        }
        $this->components['path'] = $path;
    }
    /**
    * Add assets_version parameter to URL.
    * Parameter will not be added if assets_version already exists in URL
    *
    * @param string $version Version
    */
    public function add_assets_version($version)
    {
        if (!isset($this->components['query']))
        {
            $this->components['query'] = 'assets_version=' . $version;
            return;
        }
        $query = $this->components['query'];
        if (!preg_match('/(^|[&;])assets_version=/', $query))
        {
            $this->components['query'] = $query . '&amp;assets_version=' . $version;
        }
    }
}