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