Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
path_helper.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb;
015
016 /**
017 * A class with various functions that are related to paths, files and the filesystem
018 */
019 class path_helper
020 {
021 /** @var \phpbb\symfony_request */
022 protected $symfony_request;
023
024 /** @var \phpbb\filesystem */
025 protected $filesystem;
026
027 /** @var \phpbb\request\request_interface */
028 protected $request;
029
030 /** @var string */
031 protected $phpbb_root_path;
032
033 /** @var string */
034 protected $adm_relative_path;
035
036 /** @var string */
037 protected $php_ext;
038
039 /** @var string */
040 protected $web_root_path;
041
042 /**
043 * Constructor
044 *
045 * @param \phpbb\symfony_request $symfony_request
046 * @param \phpbb\filesystem $filesystem
047 * @param \phpbb\request\request_interface $request
048 * @param string $phpbb_root_path Relative path to phpBB root
049 * @param string $php_ext PHP file extension
050 * @param mixed $adm_relative_path Relative path admin path to adm/ root
051 */
052 public function __construct(\phpbb\symfony_request $symfony_request, \phpbb\filesystem $filesystem, \phpbb\request\request_interface $request, $phpbb_root_path, $php_ext, $adm_relative_path = null)
053 {
054 $this->symfony_request = $symfony_request;
055 $this->filesystem = $filesystem;
056 $this->request = $request;
057 $this->phpbb_root_path = $phpbb_root_path;
058 $this->php_ext = $php_ext;
059 $this->adm_relative_path = $adm_relative_path;
060 }
061
062 /**
063 * Get the phpBB root path
064 *
065 * @return string
066 */
067 public function get_phpbb_root_path()
068 {
069 return $this->phpbb_root_path;
070 }
071
072 /**
073 * Get the adm root path
074 *
075 * @return string
076 */
077 public function get_adm_relative_path()
078 {
079 return $this->adm_relative_path;
080 }
081
082 /**
083 * Get the php extension
084 *
085 * @return string
086 */
087 public function get_php_ext()
088 {
089 return $this->php_ext;
090 }
091
092 /**
093 * Update a web path to the correct relative root path
094 *
095 * This replaces $phpbb_root_path . some_url with
096 * get_web_root_path() . some_url
097 *
098 * @param string $path The path to be updated
099 * @return string
100 */
101 public function update_web_root_path($path)
102 {
103 if (strpos($path, $this->phpbb_root_path) === 0)
104 {
105 $path = substr($path, strlen($this->phpbb_root_path));
106
107 $web_root_path = $this->get_web_root_path();
108 if (substr($web_root_path, -8) === 'app.php/' && substr($path, 0, 7) === 'app.php')
109 {
110 $path = substr($path, 8);
111 }
112
113 return $this->filesystem->clean_path($web_root_path . $path);
114 }
115
116 return $path;
117 }
118
119 /**
120 * Strips away the web root path and prepends the normal root path
121 *
122 * This replaces get_web_root_path() . some_url with
123 * $phpbb_root_path . some_url
124 *
125 * @param string $path The path to be updated
126 * @return string
127 */
128 public function remove_web_root_path($path)
129 {
130 if (strpos($path, $this->get_web_root_path()) === 0)
131 {
132 $path = substr($path, strlen($this->get_web_root_path()));
133
134 return $this->phpbb_root_path . $path;
135 }
136
137 return $path;
138 }
139
140 /**
141 * Get a relative root path from the current URL
142 *
143 * @return string
144 */
145 public function get_web_root_path()
146 {
147 if ($this->symfony_request === null)
148 {
149 return $this->phpbb_root_path;
150 }
151
152 if (null !== $this->web_root_path)
153 {
154 return $this->web_root_path;
155 }
156
157 // Path info (e.g. /foo/bar)
158 $path_info = $this->filesystem->clean_path($this->symfony_request->getPathInfo());
159
160 // Full request URI (e.g. phpBB/app.php/foo/bar)
161 $request_uri = $this->symfony_request->getRequestUri();
162
163 // Script name URI (e.g. phpBB/app.php)
164 $script_name = $this->symfony_request->getScriptName();
165
166 /*
167 * If the path info is empty but we're using app.php, then we
168 * might be using an empty route like app.php/ which is
169 * supported by symfony's routing
170 */
171 if ($path_info === '/' && preg_match('/app\.' . $this->php_ext . '\/$/', $request_uri))
172 {
173 return $this->web_root_path = $this->filesystem->clean_path('./../' . $this->phpbb_root_path);
174 }
175
176 /*
177 * If the path info is empty (single /), then we're not using
178 * a route like app.php/foo/bar
179 */
180 if ($path_info === '/')
181 {
182 return $this->web_root_path = $this->phpbb_root_path;
183 }
184
185 /*
186 * Check AJAX request:
187 * If the current request is a AJAX we need to fix the paths.
188 * We need to get the root path based on the Referer, so we can use
189 * the generated URLs in the template of the Referer. If we do not
190 * generate the relative path based on the Referer, but based on the
191 * currently requested URL, the generated URLs will not point to the
192 * intended locations:
193 * Referer desired URL desired relative root path
194 * memberlist.php faq.php ./
195 * memberlist.php app.php/foo/bar ./
196 * app.php/foo memberlist.php ../
197 * app.php/foo app.php/fox ../
198 * app.php/foo/bar memberlist.php ../../
199 * ../page.php memberlist.php ./phpBB/
200 * ../sub/page.php memberlist.php ./../phpBB/
201 *
202 * The referer must be specified as a parameter in the query.
203 */
204 if ($this->request->is_ajax() && $this->symfony_request->get('_referer'))
205 {
206 $referer_web_root_path = $this->get_web_root_path_from_ajax_referer(
207 $this->symfony_request->get('_referer'),
208 $this->symfony_request->getSchemeAndHttpHost() . $this->symfony_request->getBasePath()
209 );
210 return $this->web_root_path = $this->phpbb_root_path . $referer_web_root_path;
211 }
212
213 // How many corrections might we need?
214 $corrections = substr_count($path_info, '/');
215
216 /*
217 * If the script name (e.g. phpBB/app.php) does not exists in the
218 * requestUri (e.g. phpBB/app.php/foo/template), then we are rewriting
219 * the URL. So we must reduce the slash count by 1.
220 */
221 if (strpos($request_uri, $script_name) !== 0)
222 {
223 $corrections--;
224 }
225
226 // Prepend ../ to the phpbb_root_path as many times as / exists in path_info
227 $this->web_root_path = $this->filesystem->clean_path(
228 './' . str_repeat('../', $corrections) . $this->phpbb_root_path
229 );
230 return $this->web_root_path;
231 }
232
233 /**
234 * Get the web root path of the referer form an ajax request
235 *
236 * @param string $absolute_referer_url
237 * @param string $absolute_board_url
238 * @return string
239 */
240 public function get_web_root_path_from_ajax_referer($absolute_referer_url, $absolute_board_url)
241 {
242 // If the board URL is in the beginning of the referer, this means
243 // we the referer is in the board URL or a subdirectory of it.
244 // So we just need to count the / (slashes) in the left over part of
245 // the referer and prepend ../ the the current root_path, to get the
246 // web root path of the referer.
247 if (strpos($absolute_referer_url, $absolute_board_url) === 0)
248 {
249 $relative_referer_path = substr($absolute_referer_url, strlen($absolute_board_url));
250 $has_params = strpos($relative_referer_path, '?');
251 if ($has_params !== false)
252 {
253 $relative_referer_path = substr($relative_referer_path, 0, $has_params);
254 }
255 $corrections = substr_count($relative_referer_path, '/');
256 return $this->phpbb_root_path . str_repeat('../', $corrections - 1);
257 }
258
259 // If not, it's a bit more complicated. We go to the parent directory
260 // of the referer until we find the remaining referer in the board URL.
261 // Foreach directory we need to add a ../ to the fixed root_path.
262 // When we finally found it, we need to remove the remaining referer
263 // from the board URL, to get the boards root path.
264 // If the then append these two strings, we get our fixed web root path.
265 $fixed_root_path = '';
266 $referer_dir = $absolute_referer_url;
267 $has_params = strpos($referer_dir, '?');
268 if ($has_params !== false)
269 {
270 $referer_dir = substr($referer_dir, 0, $has_params);
271 }
272
273 // If we do not find a slash at the end of the referer, we come
274 // from a file. So the first dirname() does not need a traversal
275 // path correction.
276 if (substr($referer_dir, -1) !== '/')
277 {
278 $referer_dir = dirname($referer_dir);
279 }
280
281 while (strpos($absolute_board_url, $referer_dir) !== 0)
282 {
283 $fixed_root_path .= '../';
284 $referer_dir = dirname($referer_dir);
285 }
286
287 $fixed_root_path .= substr($absolute_board_url, strlen($referer_dir) + 1);
288 // Add trailing slash
289 return $this->phpbb_root_path . $fixed_root_path . '/';
290 }
291
292 /**
293 * Eliminates useless . and .. components from specified URL
294 *
295 * @param string $url URL to clean
296 *
297 * @return string Cleaned URL
298 */
299 public function clean_url($url)
300 {
301 $delimiter_position = strpos($url, '://');
302 // URL should contain :// but it shouldn't start with it.
303 // Do not clean URLs that do not fit these constraints.
304 if (empty($delimiter_position))
305 {
306 return $url;
307 }
308 $scheme = substr($url, 0, $delimiter_position) . '://';
309 // Add length of URL delimiter to position
310 $path = substr($url, $delimiter_position + 3);
311
312 return $scheme . $this->filesystem->clean_path($path);
313 }
314
315 /**
316 * Glue URL parameters together
317 *
318 * @param array $params URL parameters in the form of array(name => value)
319 * @return string Returns the glued string, e.g. name1=value1&name2&name3=value3
320 */
321 public function glue_url_params($params)
322 {
323 $_params = array();
324
325 foreach ($params as $key => $value)
326 {
327 // some parameters do not have value
328 if ($value !== null)
329 {
330 $_params[] = $key . '=' . $value;
331 }
332 else
333 {
334 $_params[] = $key;
335 }
336 }
337 return implode('&', $_params);
338 }
339
340 /**
341 * Get the base and parameters of a URL
342 *
343 * @param string $url URL to break apart
344 * @param bool $is_amp Is the parameter separator &. Defaults to true.
345 * @return array Returns the base and parameters in the form of array('base' => string, 'params' => array(name => value))
346 */
347 public function get_url_parts($url, $is_amp = true)
348 {
349 $separator = ($is_amp) ? '&' : '&';
350 $params = array();
351
352 if (strpos($url, '?') !== false)
353 {
354 $base = substr($url, 0, strpos($url, '?'));
355 $args = substr($url, strlen($base) + 1);
356 $args = ($args) ? explode($separator, $args) : array();
357
358 foreach ($args as $argument)
359 {
360 if (empty($argument))
361 {
362 continue;
363 }
364
365 // some parameters don't have value
366 if (strpos($argument, '=') !== false)
367 {
368 list($key, $value) = explode('=', $argument, 2);
369 }
370 else
371 {
372 $key = $argument;
373 $value = null;
374 }
375
376 if ($key === '')
377 {
378 continue;
379 }
380
381 $params[$key] = $value;
382 }
383 }
384 else
385 {
386 $base = $url;
387 }
388
389 return array(
390 'base' => $base,
391 'params' => $params,
392 );
393 }
394
395 /**
396 * Strip parameters from an already built URL.
397 *
398 * @param string $url URL to strip parameters from
399 * @param array|string $strip Parameters to strip.
400 * @param bool $is_amp Is the parameter separator &. Defaults to true.
401 * @return string Returns the new URL.
402 */
403 public function strip_url_params($url, $strip, $is_amp = true)
404 {
405 $url_parts = $this->get_url_parts($url, $is_amp);
406 $params = $url_parts['params'];
407
408 if (!is_array($strip))
409 {
410 $strip = array($strip);
411 }
412
413 if (!empty($params))
414 {
415 // Strip the parameters off
416 foreach ($strip as $param)
417 {
418 unset($params[$param]);
419 }
420 }
421
422 return $url_parts['base'] . (($params) ? '?' . $this->glue_url_params($params) : '');
423 }
424
425 /**
426 * Append parameters to an already built URL.
427 *
428 * @param string $url URL to append parameters to
429 * @param array $new_params Parameters to add in the form of array(name => value)
430 * @param bool $is_amp Is the parameter separator &. Defaults to true.
431 * @return string Returns the new URL.
432 */
433 public function append_url_params($url, $new_params, $is_amp = true)
434 {
435 $url_parts = $this->get_url_parts($url, $is_amp);
436 $params = array_merge($url_parts['params'], $new_params);
437
438 // Move the sid to the end if it's set
439 if (isset($params['sid']))
440 {
441 $sid = $params['sid'];
442 unset($params['sid']);
443 $params['sid'] = $sid;
444 }
445
446 return $url_parts['base'] . (($params) ? '?' . $this->glue_url_params($params) : '');
447 }
448 }
449