Passed
Push — master ( bc5e3d...bcd331 )
by Stanislav
06:16 queued 03:38
created

url::get_uri()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 23
ccs 10
cts 11
cp 0.9091
rs 9.8666
cc 3
nc 3
nop 1
crap 3.0067
1
<?php
2
3
/**
4
*
5
* @package phpBB Gallery
6
* @copyright (c) 2014 nickvergessen
7
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
*
9
*/
10
11
namespace phpbbgallery\core;
12
13
class url
14
{
15
	/**
16
	* Path from the gallery root, back to phpbb's root
17
	*/
18
	private $phpbb_root_path = '../';
19
20
	/**
21
	* Path from the phpbb root, into admin's root
22
	*/
23
	private $phpbb_admin_path = 'adm/';
24
25
	/**
26
	* Path from the phpbb root, into gallery's file root
27
	*/
28
	private $phpbb_gallery_file_path = 'files/phpbbgallery/';
29
30
	/**
31
	* Path from the phpbb root, into gallery's root
32
	*/
33
	private $phpbb_gallery_path = 'gallery/';
34
35
	const IMAGE_PATH = 'images/';
36
	const UPLOAD_PATH = 'core/source/';
37
	const THUMBNAIL_PATH = 'core/mini/';
38
	const MEDIUM_PATH = 'core/medium/';
39
	const IMPORT_PATH = 'import/';
40
41
	private $phpbb_gallery_relative = '';
42
	private $phpbb_gallery_full_path = '';
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param \phpbb\template\template $template
48
	 * @param \phpbb\request\request   $request
49
	 * @param \phpbb\config\config     $config
50
	 * @param                          $phpbb_root_path
51
	 * @param                          $php_ext
52
	 * @param string                   $phpbb_admin_path
53
	 */
54 105
	public function __construct(\phpbb\template\template $template, \phpbb\request\request $request, \phpbb\config\config $config, $phpbb_root_path, $php_ext, $phpbb_admin_path = 'adm/')
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
	{
56 105
		$this->template = $template;
0 ignored issues
show
Bug Best Practice introduced by
The property template does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57 105
		$this->request = $request;
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58 105
		$this->config = $config;
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59 105
		$this->phpbb_root_path = $phpbb_root_path;
60 105
		$this->phpbb_admin_path = $this->phpbb_root_path . $phpbb_admin_path;
61 105
		$this->php_ext = '.' . $php_ext;
0 ignored issues
show
Bug Best Practice introduced by
The property php_ext does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
63 105
		$this->phpbb_gallery_relative = self::beautiful_path($this->phpbb_root_path . $this->phpbb_gallery_path);
64 105
		$this->phpbb_gallery_full_path = self::beautiful_path(generate_board_url() . '/' . $this->phpbb_gallery_path, true);
0 ignored issues
show
Bug introduced by
The function generate_board_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
		$this->phpbb_gallery_full_path = self::beautiful_path(/** @scrutinizer ignore-call */ generate_board_url() . '/' . $this->phpbb_gallery_path, true);
Loading history...
65 105
	}
66
67 6
	public function path($directory = 'gallery')
68
	{
69 6
		switch ($directory)
70
		{
71 6
			case 'gallery':
72 3
				return $this->phpbb_gallery_relative;
73 5
			case 'ext':
74 1
				return $this->phpbb_root_path . 'ext/phpbbgallery/core/';
75 5
			case 'phpbb':
76 5
				return $this->phpbb_root_path;
77 3
			case 'admin':
78 1
				return $this->phpbb_admin_path;
79 3
			case 'relative':
80 1
				return $this->phpbb_gallery_path;
81 3
			case 'full':
82 3
				return $this->phpbb_gallery_full_path;
83 1
			case 'board':
84 1
				return generate_board_url() . '/';
0 ignored issues
show
Bug introduced by
The function generate_board_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
				return /** @scrutinizer ignore-call */ generate_board_url() . '/';
Loading history...
85
86 1
			case 'images':
87 1
				return $this->phpbb_root_path . 'ext/phpbbgallery/core/' . self::IMAGE_PATH;
88 1
			case 'upload':
89 1
				return $this->phpbb_root_path . $this->phpbb_gallery_file_path . self::UPLOAD_PATH;
90 1
			case 'thumbnail':
91 1
				return $this->phpbb_root_path . $this->phpbb_gallery_file_path . self::THUMBNAIL_PATH;
92 1
			case 'medium':
93 1
				return $this->phpbb_root_path . $this->phpbb_gallery_file_path . self::MEDIUM_PATH;
94 1
			case 'import':
95 1
				return $this->phpbb_root_path . $this->phpbb_gallery_file_path . self::IMPORT_PATH;
96
97
				// stupid phpbb-upload class prepends the rootpath itself.
98 1
			case 'upload_noroot':
99 1
				return $this->phpbb_gallery_file_path . self::UPLOAD_PATH;
100 1
			case 'thumbnail_noroot':
101 1
				return $this->phpbb_gallery_file_path . self::THUMBNAIL_PATH;
102 1
			case 'medium_noroot':
103 1
				return $this->phpbb_gallery_file_path . self::MEDIUM_PATH;
104 1
			case 'import_noroot':
105 1
				return $this->phpbb_gallery_file_path . self::IMPORT_PATH;
106
		}
107
108 1
		return false;
109
	}
110
111 4
	public function append_sid()
112
	{
113 4
		$args = func_get_args();
114 4
		if (is_array($args[0]))
115
		{
116
			// Little problem from the duplicated call to func_get_args();
117 1
			$args = $args[0];
118
		}
119
120 4
		if (in_array($args[0], array('phpbb', 'admin', 'relative', 'full', 'board', 'ext')))
121
		{
122 3
			$mode = array_shift($args);
123 3
			$args[0] = $this->path($mode) . $this->phpEx_file($args[0]);
0 ignored issues
show
Bug introduced by
Are you sure $this->path($mode) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
			$args[0] = /** @scrutinizer ignore-type */ $this->path($mode) . $this->phpEx_file($args[0]);
Loading history...
124
		}
125
		else
126
		{
127 2
			$args[0] = $this->path() . $this->phpEx_file($args[0]);
128
		}
129
130 4
		if (isset($args[1]))
131
		{
132 4
			$args[1] .= '';//@todo: phpbb_gallery::$display_popup;
133
		}
134
135
		$params = $args + array(
136 4
			0	=> '',
137
			1	=> '',//@todo: phpbb_gallery::$display_popup,
138
			2	=> true,
139
			3	=> false,
140
		);
141
142 4
		return append_sid($params[0], $params[1], $params[2], $params[3]);
0 ignored issues
show
Bug introduced by
The function append_sid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
		return /** @scrutinizer ignore-call */ append_sid($params[0], $params[1], $params[2], $params[3]);
Loading history...
143
	}
144
145 7
	public function show_image($image_id, $size = 'medium')
146
	{
147 7
		return $this->phpbb_gallery_full_path . 'image/' . $image_id . '/' . $size;
148
	}
149
150 1
	public function show_album($album_id)
151
	{
152 1
		return $this->phpbb_gallery_full_path . 'album/' . $album_id;
153
	}
154
155
	/**
156
	 * Removes the sid and replaces &amp; with normal &
157
	 * @param $path
158
	 * @param $file
159
	 * @param bool $params
160
	 * @param bool $is_amp
161
	 * @return string
162
	 */
163 1
	public function create_link($path, $file, $params = false, $is_amp = true)
164
	{
165 1
		if ($is_amp && !is_array($params))
166
		{
167 1
			$params = implode('&', explode('&amp;', $params));
0 ignored issues
show
Bug introduced by
$params of type boolean is incompatible with the type string expected by parameter $string of explode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
			$params = implode('&', explode('&amp;', /** @scrutinizer ignore-type */ $params));
Loading history...
168
		}
169
170 1
		return $this->append_sid($path, $file, $params, false, '');
171
	}
172
173
	public function redirect()
174
	{
175
		redirect($this->append_sid(func_get_args()));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
		/** @scrutinizer ignore-call */ 
176
  redirect($this->append_sid(func_get_args()));
Loading history...
176
	}
177
178 6
	public function phpEx_file($file)
179
	{
180 6
		if ((substr($file, -1) == '/') || (strlen($file) == 0))
181
		{
182
			// it's no file, so no .php here.
183 1
			return $file;
184
		}
185
186
		/*if ($file == 'image_page')
187
		{
188
			//@todo
189
			$file = 'viewimage';
190
		}*/
191
192 6
		return $file . $this->php_ext;
193
	}
194
195 1
	public function _include($file, $path = 'gallery', $sub_directory = 'includes/')
196
	{
197 1
		if (!is_array($file))
198
		{
199 1
			include($this->path($path) . $sub_directory . $this->phpEx_file($file));
0 ignored issues
show
Bug introduced by
Are you sure $this->path($path) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
			include(/** @scrutinizer ignore-type */ $this->path($path) . $sub_directory . $this->phpEx_file($file));
Loading history...
200
		}
201
		else
202
		{
203
			foreach ($file as $real_file)
204
			{
205
				$this->_include($real_file, $path, $sub_directory);
206
			}
207
		}
208 1
	}
209
210
	public function _file_exists($file, $path = 'gallery', $sub_directory = 'includes/')
211
	{
212
		return file_exists($this->path($path) . $sub_directory . $this->phpEx_file($file));
0 ignored issues
show
Bug introduced by
Are you sure $this->path($path) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

212
		return file_exists(/** @scrutinizer ignore-type */ $this->path($path) . $sub_directory . $this->phpEx_file($file));
Loading history...
213
	}
214
215
	public function _is_writable($file, $path = 'gallery', $sub_directory = 'includes/')
216
	{
217
		return phpbb_is_writable($this->path($path) . $sub_directory . $this->phpEx_file($file));
0 ignored issues
show
Bug introduced by
The function phpbb_is_writable was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
		return /** @scrutinizer ignore-call */ phpbb_is_writable($this->path($path) . $sub_directory . $this->phpEx_file($file));
Loading history...
Bug introduced by
Are you sure $this->path($path) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

217
		return phpbb_is_writable(/** @scrutinizer ignore-type */ $this->path($path) . $sub_directory . $this->phpEx_file($file));
Loading history...
218
	}
219
220
	public function _return_file($file, $path = 'gallery', $sub_directory = 'includes/')
221
	{
222
		return $this->path($path) . $sub_directory . $this->phpEx_file($file);
0 ignored issues
show
Bug introduced by
Are you sure $this->path($path) of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

222
		return /** @scrutinizer ignore-type */ $this->path($path) . $sub_directory . $this->phpEx_file($file);
Loading history...
223
	}
224
225
	/**
226
	* Creates beautiful relative path from ugly relative path
227
	* Resolves .. (up directory)
228
	*
229
	* @author	bantu		based on phpbb_own_realpath() by Chris Smith
230
	* @license	http://opensource.org/licenses/gpl-license.php GNU Public License
231
	*
232
	* @param	string		ugly path e.g. "../community/../gallery/"
0 ignored issues
show
Bug introduced by
The type phpbbgallery\core\ugly was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
233
	* @param	bool		is it a full url, so we need to fix teh http:// at the beginning?
0 ignored issues
show
Bug introduced by
The type phpbbgallery\core\is was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
234
	* @return	string		beautiful path e.g. "../gallery/"
235
	*/
236 105
	static public function beautiful_path($path, $is_full_url = false)
237
	{
238
		// Remove any repeated slashes
239 105
		$path = preg_replace('#/{2,}#', '/', $path);
240
241 105
		if ($is_full_url)
242
		{
243
			// Fix the double slash, which we just removed.
244 105
			if (strpos($path, 'https:/') === 0)
245
			{
246 1
				$path = 'https://' . substr($path, 7);
247
			}
248 105
			else if (strpos($path, 'http:/') === 0)
249
			{
250 105
				$path = 'http://' . substr($path, 6);
251
			}
252
		}
253
254
		// Break path into pieces
255 105
		$bits = explode('/', $path);
256
257
		// Lets get looping, run over and resolve any .. (up directory)
258 105
		for ($i = 0, $max = sizeof($bits); $i < $max; $i++)
259
		{
260 105
			if ($bits[$i] == '..' && isset($bits[$i - 1]) && $bits[$i - 1][0] != '.')
261
			{
262
				// We found a .. and we are able to traverse upwards ...
263 1
				unset($bits[$i]);
264 1
				unset($bits[$i - 1]);
265
266 1
				$i -= 2;
267 1
				$max -= 2;
268
269 1
				$bits = array_values($bits);
270
			}
271
		}
272
273 105
		return implode('/', $bits);
274
	}
275
276
	/**
277
	* Custom meta_refresh implementation
278
	* @param	int		$time	Time in seconds.
279
	* @param	string	$route	Route generated by $helper->route
280
	*/
281 1
	public function meta_refresh($time, $route)
282
	{
283
		// For XHTML compatibility we change back & to &amp;
284 1
		$route = str_replace('&', '&amp;', $route);
285 1
		$this->template->assign_vars(array(
286 1
			'META' => '<meta http-equiv="refresh" content="' . $time . '; url=' . $route . '" />')
287
		);
288 1
	}
289
290
	/**
291
	 * Get URI (prepend domain name to route)
292
	 *
293
	 * @param    string $route Route generated by $helper->route
294
	 *                         return string URI
295
	 * @return string
296
	 */
297 1
	public function get_uri($route)
298
	{
299 1
		$url = $this->config['server_name'];
300 1
		if ($this->config['force_server_vars'] == 1)
301
		{
302
			$url = $this->config['server_protocol'] . $url;
303
		}
304
		else
305
		{
306 1
			$is_secure = $this->request->server('HTTPS', '');
307 1
			if ($is_secure == 'on')
308
			{
309 1
				$url = 'https://' . $url;
310
			}
311
			else
312
			{
313 1
				$url = 'http://' . $url;
314
			}
315
		}
316 1
		$split = parse_url($url);
317
318 1
		$uri = $split['scheme'] . '://' . $split['host'] . $route;
319 1
		return $uri;
320
	}
321
}
322