mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/11362] Move phpbb_clean_path into a simple filesystem service
PHPBB3-11362
This commit is contained in:
parent
1fd5be859e
commit
869e00a23b
4 changed files with 68 additions and 38 deletions
|
@ -142,9 +142,13 @@ services:
|
||||||
- @ext.manager
|
- @ext.manager
|
||||||
- %core.root_path%
|
- %core.root_path%
|
||||||
- @cache.driver
|
- @cache.driver
|
||||||
|
- @filesystem
|
||||||
- .%core.php_ext%
|
- .%core.php_ext%
|
||||||
- _ext_finder
|
- _ext_finder
|
||||||
|
|
||||||
|
filesystem:
|
||||||
|
class: phpbb_filesystem
|
||||||
|
|
||||||
groupposition.legend:
|
groupposition.legend:
|
||||||
class: phpbb_groupposition_legend
|
class: phpbb_groupposition_legend
|
||||||
arguments:
|
arguments:
|
||||||
|
|
52
phpBB/includes/filesystem.php
Normal file
52
phpBB/includes/filesystem.php
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package phpBB3
|
||||||
|
* @copyright (c) 2013 phpBB Group
|
||||||
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @ignore
|
||||||
|
*/
|
||||||
|
if (!defined('IN_PHPBB'))
|
||||||
|
{
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class with various functions that are related to paths, files and the filesystem
|
||||||
|
* @package phpBB3
|
||||||
|
*/
|
||||||
|
class phpbb_filesystem
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Eliminates useless . and .. components from specified path.
|
||||||
|
*
|
||||||
|
* @param string $path Path to clean
|
||||||
|
* @return string Cleaned path
|
||||||
|
*/
|
||||||
|
public function clean_path($path)
|
||||||
|
{
|
||||||
|
$exploded = explode('/', $path);
|
||||||
|
$filtered = array();
|
||||||
|
foreach ($exploded as $part)
|
||||||
|
{
|
||||||
|
if ($part === '.' && !empty($filtered))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($part === '..' && !empty($filtered) && $filtered[sizeof($filtered) - 1] !== '..')
|
||||||
|
{
|
||||||
|
array_pop($filtered);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$filtered[] = $part;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$path = implode('/', $filtered);
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1046,36 +1046,6 @@ else
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Eliminates useless . and .. components from specified path.
|
|
||||||
*
|
|
||||||
* @param string $path Path to clean
|
|
||||||
* @return string Cleaned path
|
|
||||||
*/
|
|
||||||
function phpbb_clean_path($path)
|
|
||||||
{
|
|
||||||
$exploded = explode('/', $path);
|
|
||||||
$filtered = array();
|
|
||||||
foreach ($exploded as $part)
|
|
||||||
{
|
|
||||||
if ($part === '.' && !empty($filtered))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($part === '..' && !empty($filtered) && $filtered[sizeof($filtered) - 1] !== '..')
|
|
||||||
{
|
|
||||||
array_pop($filtered);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$filtered[] = $part;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$path = implode('/', $filtered);
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
// functions used for building option fields
|
// functions used for building option fields
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -7,11 +7,17 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
|
class phpbb_filesystem_clean_path_test extends phpbb_test_case
|
||||||
|
|
||||||
class phpbb_clean_path_test extends phpbb_test_case
|
|
||||||
{
|
{
|
||||||
public function clean_path_test_data()
|
protected $filesystem;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->filesystem = new phpbb_filesystem();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clean_path_data()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array('foo', 'foo'),
|
array('foo', 'foo'),
|
||||||
|
@ -33,12 +39,10 @@ class phpbb_clean_path_test extends phpbb_test_case
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider clean_path_test_data
|
* @dataProvider clean_path_data
|
||||||
*/
|
*/
|
||||||
public function test_clean_path($input, $expected)
|
public function test_clean_path($input, $expected)
|
||||||
{
|
{
|
||||||
$output = phpbb_clean_path($input);
|
$this->assertEquals($expected, $this->filesystem->clean_path($input));
|
||||||
|
|
||||||
$this->assertEquals($expected, $output);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue