From 334b44057d0580af32d5ca74a1f1744962145af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 25 Jun 2017 14:22:54 +0200 Subject: [PATCH 01/26] [ticket/15253] Add storage abstraction PHPBB3-15253 --- phpBB/config/default/container/services.yml | 1 + .../default/container/services_storage.yml | 14 + .../storage/adapter/adapter_interface.php | 78 ++++ phpBB/phpbb/storage/adapter/local.php | 146 +++++++ phpBB/phpbb/storage/controller.php | 39 ++ phpBB/phpbb/storage/exception/exception.php | 42 ++ .../storage/exception/not_implemented.php | 42 ++ phpBB/phpbb/storage/helper.php | 365 ++++++++++++++++++ phpBB/phpbb/storage/storage.php | 66 ++++ tests/storage/adapter/local_test.php | 86 +++++ tests/storage/helper_clean_path_test.php | 52 +++ tests/storage/helper_is_absolute_test.php | 64 +++ tests/storage/helper_realpath_test.php | 83 ++++ 13 files changed, 1078 insertions(+) create mode 100644 phpBB/config/default/container/services_storage.yml create mode 100644 phpBB/phpbb/storage/adapter/adapter_interface.php create mode 100644 phpBB/phpbb/storage/adapter/local.php create mode 100644 phpBB/phpbb/storage/controller.php create mode 100644 phpBB/phpbb/storage/exception/exception.php create mode 100644 phpBB/phpbb/storage/exception/not_implemented.php create mode 100644 phpBB/phpbb/storage/helper.php create mode 100644 phpBB/phpbb/storage/storage.php create mode 100644 tests/storage/adapter/local_test.php create mode 100644 tests/storage/helper_clean_path_test.php create mode 100644 tests/storage/helper_is_absolute_test.php create mode 100644 tests/storage/helper_realpath_test.php diff --git a/phpBB/config/default/container/services.yml b/phpBB/config/default/container/services.yml index 17fa223dda..c917c948dd 100644 --- a/phpBB/config/default/container/services.yml +++ b/phpBB/config/default/container/services.yml @@ -25,6 +25,7 @@ imports: - { resource: services_profilefield.yml } - { resource: services_report.yml } - { resource: services_routing.yml } + - { resource: services_storage.yml } - { resource: services_text_formatter.yml } - { resource: services_text_reparser.yml } - { resource: services_twig.yml } diff --git a/phpBB/config/default/container/services_storage.yml b/phpBB/config/default/container/services_storage.yml new file mode 100644 index 0000000000..7d004ec9c6 --- /dev/null +++ b/phpBB/config/default/container/services_storage.yml @@ -0,0 +1,14 @@ +services: + storage.adapter.avatar: + class: phpbb\storage\adapter\local + arguments: + - '@filesystem' + - '%core.root_path%' + storage.avatar: + class: phpbb\storage\storage + arguments: + - '@storage.adapter.avatar' + storage.controller: + class: phpbb\storage\controller + arguments: + - '@service_container' diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php new file mode 100644 index 0000000000..01feb50179 --- /dev/null +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -0,0 +1,78 @@ + + * @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\storage\adapter; + +interface adapter_interface +{ + /** + * Dumps content into a file. + * + * @param string path The file to be written to. + * @param string content The data to write into the file. + * + * @throws \phpbb\storage\exception\exception When the file already exists + * When the file cannot be written + */ + public function put_contents($path, $content); + + /** + * Read the contents of a file + * + * @param string $path The file to read + * + * @throws \phpbb\storage\exception\exception When the file dont exists + * When cannot read file contents + */ + public function get_contents($path); + + /** + * Checks the existence of files or directories. + * + * @param string $path file/directory to check + * + * @return bool Returns true if all files/directories exist, false otherwise + */ + public function exists($path); + + /** + * Removes files or directories. + * + * @param string $path file/directory to remove + * + * @throws \phpbb\storage\exception\exception When removal fails. + */ + public function delete($path); + + /** + * Rename a file or a directory. + * + * @param string $path_orig The original file/direcotry + * @param string $path_dest The target file/directory + * + * @throws \phpbb\storage\exception\exception When target exists + * When file/directory cannot be renamed + */ + public function rename($path_orig, $path_dest); + + /** + * Copies a file. + * + * @param string $path_orig The original filename + * @param string $path_dest The target filename + * + * @throws \phpbb\storage\exception\exception When target exists + * When the file cannot be copied + */ + public function copy($path_orig, $path_dest); +} diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php new file mode 100644 index 0000000000..b2d8c58d36 --- /dev/null +++ b/phpBB/phpbb/storage/adapter/local.php @@ -0,0 +1,146 @@ + + * @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\storage\adapter; + +use phpbb\storage\exception\exception; +use phpbb\filesystem\filesystem_exception; + +class local implements adapter_interface +{ + /** + * Filesystem component + * + * @var \phpbb\filesystem\filesystem + */ + protected $filesystem; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + /** + * Constructor + */ + public function __construct($filesystem, $phpbb_root_path) + { + $this->filesystem = $filesystem; + $this->phpbb_root_path = $phpbb_root_path; + } + + /** + * {@inheritdoc} + */ + public function put_contents($path, $content) + { + if ($this->exists($this->phpbb_root_path.$path)) + { + throw new exception('', $path); // FILE_EXISTS + } + + try + { + $this->filesystem->dump_file($this->phpbb_root_path.$path, $content); + } + catch (filesystem_exception $e) + { + throw new exception('', $path, array(), $e); // CANNOT_DUMP_FILE + } + } + + /** + * {@inheritdoc} + */ + public function get_contents($path) + { + if (!$this->exists($this->phpbb_root_path.$path)) + { + throw new exception('', $path); // FILE_DONT_EXIST + } + + if (($content = @file_get_contents($this->phpbb_root_path.$path)) === false) + { + throw new exception('', $path); // CANNOT READ FILE + } + + return $content; + } + + /** + * {@inheritdoc} + */ + public function exists($path) + { + return $this->filesystem->exists($this->phpbb_root_path.$path); + } + + /** + * {@inheritdoc} + */ + public function delete($path) + { + try + { + $this->filesystem->remove($this->phpbb_root_path.$path); + } + catch (filesystem_exception $e) + { + throw new exception('', $path, array(), $e); // CANNOT DELETE + } + } + + /** + * {@inheritdoc} + */ + public function rename($path_orig, $path_dest) + { + try + { + $this->filesystem->rename($this->phpbb_root_path.$path_orig, $this->phpbb_root_path.$path_dest, false); + } + catch (filesystem_exception $e) + { + throw new exception('', $path_orig, array(), $e); // CANNOT_RENAME + } + } + + /** + * {@inheritdoc} + */ + public function copy($path_orig, $path_dest) + { + try + { + $this->filesystem->copy($this->phpbb_root_path.$path_orig, $this->phpbb_root_path.$path_dest, false); + } + catch (filesystem_exception $e) + { + throw new exception('', '', array(), $e); // CANNOT_COPY_FILES + } + } + + /** + * {@inheritdoc} + */ + public function create_dir($path) + { + try + { + $this->filesystem->mkdir($this->phpbb_root_path.$path); + } + catch (filesystem_exception $e) + { + throw new exception('', $path, array(), $e); // CANNOT_CREATE_DIRECTORY + } + } + +} diff --git a/phpBB/phpbb/storage/controller.php b/phpBB/phpbb/storage/controller.php new file mode 100644 index 0000000000..22f733faf7 --- /dev/null +++ b/phpBB/phpbb/storage/controller.php @@ -0,0 +1,39 @@ + + * @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\storage; + +class controller +{ + /** + * @var ContainerInterface + */ + protected $container; + + /** + * Constructor. + * + * @param ContainerInterface $container A ContainerInterface instance + */ + public function __construct(ContainerInterface $container) + { + $this->container = $container; + } + + public function get_file($storage, $file) + { + $storage = $this->container->get($storage); + + return $storage->get_contents($file); + } +} diff --git a/phpBB/phpbb/storage/exception/exception.php b/phpBB/phpbb/storage/exception/exception.php new file mode 100644 index 0000000000..ee42178961 --- /dev/null +++ b/phpBB/phpbb/storage/exception/exception.php @@ -0,0 +1,42 @@ + + * @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\storage\exception; + +class exception extends \phpbb\exception\runtime_exception +{ + /** + * Constructor + * + * @param string $message The Exception message to throw (must be a language variable). + * @param string $filename The file that caused the error. + * @param array $parameters The parameters to use with the language var. + * @param \Exception $previous The previous runtime_exception used for the runtime_exception chaining. + * @param integer $code The Exception code. + */ + public function __construct($message = "", $filename = '', $parameters = array(), \Exception $previous = null, $code = 0) + { + parent::__construct($message, array_merge(array('filename' => $filename), $parameters), $previous, $code); + } + + /** + * Returns the filename that triggered the error + * + * @return string + */ + public function get_filename() + { + $parameters = parent::get_parameters(); + return $parameters['filename']; + } +} diff --git a/phpBB/phpbb/storage/exception/not_implemented.php b/phpBB/phpbb/storage/exception/not_implemented.php new file mode 100644 index 0000000000..5504cfa76f --- /dev/null +++ b/phpBB/phpbb/storage/exception/not_implemented.php @@ -0,0 +1,42 @@ + + * @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\storage\exception; + +class not_implemented extends \phpbb\exception\runtime_exception +{ + /** + * Constructor + * + * @param string $message The Exception message to throw (must be a language variable). + * @param string $filename The file that caused the error. + * @param array $parameters The parameters to use with the language var. + * @param \Exception $previous The previous runtime_exception used for the runtime_exception chaining. + * @param integer $code The Exception code. + */ + public function __construct($message = "", $filename = '', $parameters = array(), \Exception $previous = null, $code = 0) + { + parent::__construct($message, array_merge(array('filename' => $filename), $parameters), $previous, $code); + } + + /** + * Returns the filename that triggered the error + * + * @return string + */ + public function get_filename() + { + $parameters = parent::get_parameters(); + return $parameters['filename']; + } +} diff --git a/phpBB/phpbb/storage/helper.php b/phpBB/phpbb/storage/helper.php new file mode 100644 index 0000000000..4c8dfe466b --- /dev/null +++ b/phpBB/phpbb/storage/helper.php @@ -0,0 +1,365 @@ + + * @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\storage; + +class helper +{ + /** + * Eliminates useless . and .. components from specified path. + * + * @param string $path Path to clean + * + * @return string Cleaned path + */ + public static 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] !== '.' && $filtered[sizeof($filtered) - 1] !== '..') + { + array_pop($filtered); + } + else + { + $filtered[] = $part; + } + } + $path = implode('/', $filtered); + return $path; + } + + /** + * Checks if a path is absolute or not + * + * @param string $path Path to check + * + * @return bool true if the path is absolute, false otherwise + */ + public static function is_absolute_path($path) + { + return (isset($path[0]) && $path[0] === '/' || preg_match('#^[a-z]:[/\\\]#i', $path)) ? true : false; + } + + /** + * Try to resolve real path when PHP's realpath failes to do so + * + * @param string $path + * @return bool|string + */ + protected static function phpbb_own_realpath($path) + { + + // Replace all directory separators with '/' + $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); + + $is_absolute_path = false; + $path_prefix = ''; + + if (self::is_absolute_path($path)) + { + $is_absolute_path = true; + } + else + { + if (function_exists('getcwd')) + { + $working_directory = str_replace(DIRECTORY_SEPARATOR, '/', getcwd()); + } + + // + // From this point on we really just guessing + // If chdir were called we screwed + // + else if (function_exists('debug_backtrace')) + { + $call_stack = debug_backtrace(0); + $working_directory = str_replace(DIRECTORY_SEPARATOR, '/', dirname($call_stack[sizeof($call_stack) - 1]['file'])); + } + else + { + // + // Assuming that the working directory is phpBB root + // we could use this as a fallback, when phpBB will use controllers + // everywhere this will be a safe assumption + // + //$dir_parts = explode(DIRECTORY_SEPARATOR, __DIR__); + //$namespace_parts = explode('\\', trim(__NAMESPACE__, '\\')); + + //$namespace_part_count = sizeof($namespace_parts); + + // Check if we still loading from root + //if (array_slice($dir_parts, -$namespace_part_count) === $namespace_parts) + //{ + // $working_directory = implode('/', array_slice($dir_parts, 0, -$namespace_part_count)); + //} + //else + //{ + // $working_directory = false; + //} + + $working_directory = false; + } + + if ($working_directory !== false) + { + $is_absolute_path = true; + $path = $working_directory . '/' . $path; + } + } + + if ($is_absolute_path) + { + if (defined('PHP_WINDOWS_VERSION_MAJOR')) + { + $path_prefix = $path[0] . ':'; + $path = substr($path, 2); + } + else + { + $path_prefix = ''; + } + } + + $resolved_path = self::resolve_path($path, $path_prefix, $is_absolute_path); + if ($resolved_path === false) + { + return false; + } + + if (!@file_exists($resolved_path) || (!@is_dir($resolved_path . '/') && !is_file($resolved_path))) + { + return false; + } + + // Return OS specific directory separators + $resolved = str_replace('/', DIRECTORY_SEPARATOR, $resolved_path); + + // Check for DIRECTORY_SEPARATOR at the end (and remove it!) + if (substr($resolved, -1) === DIRECTORY_SEPARATOR) + { + return substr($resolved, 0, -1); + } + + return $resolved; + } + + /** + * A wrapper for PHP's realpath + * + * Try to resolve realpath when PHP's realpath is not available, or + * known to be buggy. + * + * @param string $path Path to resolve + * + * @return string Resolved path + */ + public static function realpath($path) + { + if (!function_exists('realpath')) + { + return self::phpbb_own_realpath($path); + } + + $realpath = realpath($path); + + // Strangely there are provider not disabling realpath but returning strange values. :o + // We at least try to cope with them. + if ((!self::is_absolute_path($path) && $realpath === $path) || $realpath === false) + { + return self::phpbb_own_realpath($path); + } + + // Check for DIRECTORY_SEPARATOR at the end (and remove it!) + if (substr($realpath, -1) === DIRECTORY_SEPARATOR) + { + $realpath = substr($realpath, 0, -1); + } + + return $realpath; + } + + /** + * Given an existing path, convert it to a path relative to a given starting path + * + * @param string $end_path Absolute path of target + * @param string $start_path Absolute path where traversal begins + * + * @return string Path of target relative to starting path + */ + public static function make_path_relative($end_path, $start_path) + { + $symfony_filesystem = new \Symfony\Component\Filesystem\Filesystem(); + return $symfony_filesystem->makePathRelative($end_path, $start_path); + } + + /** + * Try to resolve symlinks in path + * + * @param string $path The path to resolve + * @param string $prefix The path prefix (on windows the drive letter) + * @param bool $absolute Whether or not the path is absolute + * @param bool $return_array Whether or not to return path parts + * + * @return string|array|bool returns the resolved path or an array of parts of the path if $return_array is true + * or false if path cannot be resolved + */ + protected static function resolve_path($path, $prefix = '', $absolute = false, $return_array = false) + { + if ($return_array) + { + $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); + } + + trim ($path, '/'); + $path_parts = explode('/', $path); + $resolved = array(); + $resolved_path = $prefix; + $file_found = false; + + foreach ($path_parts as $path_part) + { + if ($file_found) + { + return false; + } + + if (empty($path_part) || ($path_part === '.' && ($absolute || !empty($resolved)))) + { + continue; + } + else if ($absolute && $path_part === '..') + { + if (empty($resolved)) + { + // No directories above root + return false; + } + + array_pop($resolved); + $resolved_path = false; + } + else if ($path_part === '..' && !empty($resolved) && !in_array($resolved[sizeof($resolved) - 1], array('.', '..'))) + { + array_pop($resolved); + $resolved_path = false; + } + else + { + if ($resolved_path === false) + { + if (empty($resolved)) + { + $resolved_path = ($absolute) ? $prefix . '/' . $path_part : $path_part; + } + else + { + $tmp_array = $resolved; + if ($absolute) + { + array_unshift($tmp_array, $prefix); + } + + $resolved_path = implode('/', $tmp_array); + } + } + + $current_path = $resolved_path . '/' . $path_part; + + // Resolve symlinks + if (is_link($current_path)) + { + if (!function_exists('readlink')) + { + return false; + } + + $link = readlink($current_path); + + // Is link has an absolute path in it? + if (self::is_absolute_path($link)) + { + if (defined('PHP_WINDOWS_VERSION_MAJOR')) + { + $prefix = $link[0] . ':'; + $link = substr($link, 2); + } + else + { + $prefix = ''; + } + + $resolved = self::resolve_path($link, $prefix, true, true); + $absolute = true; + } + else + { + $resolved = self::resolve_path($resolved_path . '/' . $link, $prefix, $absolute, true); + } + + if (!$resolved) + { + return false; + } + + $resolved_path = false; + } + else if (is_dir($current_path . '/')) + { + $resolved[] = $path_part; + $resolved_path = $current_path; + } + else if (is_file($current_path)) + { + $resolved[] = $path_part; + $resolved_path = $current_path; + $file_found = true; + } + else + { + return false; + } + } + } + + // If at the end of the path there were a .. or . + // we need to build the path again. + // Only doing this when a string is expected in return + if ($resolved_path === false && $return_array === false) + { + if (empty($resolved)) + { + $resolved_path = ($absolute) ? $prefix . '/' : './'; + } + else + { + $tmp_array = $resolved; + if ($absolute) + { + array_unshift($tmp_array, $prefix); + } + + $resolved_path = implode('/', $tmp_array); + } + } + + return ($return_array) ? $resolved : $resolved_path; + } +} diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php new file mode 100644 index 0000000000..dc52004b3a --- /dev/null +++ b/phpBB/phpbb/storage/storage.php @@ -0,0 +1,66 @@ + + * @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\storage; + +use phpbb\storage\exception\exception; + +class storage +{ + protected $adapter; + + public function __construct($adapter) + { + $this->adapter = $adapter; + } + + public function put_contents($path, $content) + { + $this->adapter->put_contents($path, $content); + } + + public function get_contents($path) + { + $this->adapter->put_contents($path); + } + + public function exists($path) + { + return $this->adapter->exists($path); + } + + public function delete($path) + { + $this->adapter->delete($path); + } + + public function rename($path_orig, $path_dest) + { + $this->adapter->rename($path_orig, $path_dest); + } + + public function copy($path_orig, $path_dest) + { + $this->adapter->copy($path_orig, $path_dest); + } + + public function create_dir($path) + { + $this->adapter->create_dir($path); + } + + public function delete_dir($path) + { + $this->adapter->delete_dir($path); + } +} diff --git a/tests/storage/adapter/local_test.php b/tests/storage/adapter/local_test.php new file mode 100644 index 0000000000..ecc06cf688 --- /dev/null +++ b/tests/storage/adapter/local_test.php @@ -0,0 +1,86 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + + class phpbb_storage_adapter_local_test extends phpbb_test_case + { + protected $adapter; + + public function setUp() + { + parent::setUp(); + $this->adapter = new \phpbb\storage\adapter\local(); + } + + public function tearDown() + { + $this->adapter = null; + } + + public function test_put_contents() + { + $this->adapter->put_contents('file.txt', 'abc'); + $this->assertTrue(file_exists('file.txt')); + $this->assertEquals(file_get_contents('file.txt'), 'abc'); + unlink('file.txt'); + } + + public function test_get_contents() + { + file_put_contents('file.txt', 'abc'); + $this->assertEquals($this->adapter->get_contents('file.txt'), 'abc'); + unlink('file.txt'); + } + + public function test_exists() + { + // Exists with files + $this->assertTrue($this->adapter->exists(__DIR__.'/local_test.php')); + $this->assertFalse($this->adapter->exists(__DIR__.'/nonexistent_file.php')); + // exists with directory + $this->assertTrue($this->adapter->exists(__DIR__.'/../adapter')); + $this->assertFalse($this->adapter->exists(__DIR__.'/../nonexistet_folder')); + } + + public function test_delete() + { + // Delete with files + file_put_contents('file.txt', ''); + $this->assertTrue(file_exists('file.txt')); + $this->adapter->delete('file.txt'); + $this->assertFalse(file_exists('file.txt')); + // Delete with directories + mkdir('path/to/dir', 0777, true); + $this->assertTrue(file_exists('path/to/dir')); + $this->adapter->delete('path'); + $this->assertFalse(file_exists('path/to/dir')); + } + + public function test_rename() + { + file_put_contents('file.txt', ''); + $this->adapter->rename('file.txt', 'file2.txt'); + $this->assertFalse(file_exists('file.txt')); + $this->assertTrue(file_exists('file2.txt')); + unlink('file2.txt'); + } + + public function test_copy() + { + file_put_contents('file.txt', 'abc'); + $this->adapter->copy('file.txt', 'file2.txt'); + $this->assertEquals(file_get_contents('file.txt'), 'abc'); + $this->assertEquals(file_get_contents('file.txt'), 'abc'); + unlink('file.txt'); + unlink('file2.txt'); + } + } diff --git a/tests/storage/helper_clean_path_test.php b/tests/storage/helper_clean_path_test.php new file mode 100644 index 0000000000..ab9fabd0cb --- /dev/null +++ b/tests/storage/helper_clean_path_test.php @@ -0,0 +1,52 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +class phpbb_storage_helper_clean_path_test extends phpbb_test_case +{ + + public function setUp() + { + parent::setUp(); + } + + public function clean_path_data() + { + return array( + array('foo', 'foo'), + array('foo/bar', 'foo/bar'), + array('foo/bar/', 'foo/bar/'), + array('foo/./bar', 'foo/bar'), + array('foo/./././bar', 'foo/bar'), + array('foo/bar/.', 'foo/bar'), + array('./foo/bar', './foo/bar'), + array('../foo/bar', '../foo/bar'), + array('./../foo/bar', './../foo/bar'), + array('././../foo/bar', './../foo/bar'), + array('one/two/three', 'one/two/three'), + array('one/two/../three', 'one/three'), + array('one/../two/three', 'two/three'), + array('one/two/..', 'one'), + array('one/two/../', 'one/'), + array('one/two/../three/../four', 'one/four'), + array('one/two/three/../../four', 'one/four'), + ); + } + + /** + * @dataProvider clean_path_data + */ + public function test_clean_path($input, $expected) + { + $this->assertEquals($expected, \phpbb\storage\helper::clean_path($input)); + } +} diff --git a/tests/storage/helper_is_absolute_test.php b/tests/storage/helper_is_absolute_test.php new file mode 100644 index 0000000000..05a048cb91 --- /dev/null +++ b/tests/storage/helper_is_absolute_test.php @@ -0,0 +1,64 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_storage_helper_is_absolute_test extends phpbb_test_case +{ + + public function setUp() + { + parent::setUp(); + } + + static public function is_absolute_data() + { + return array( + // Empty + array('', false), + + // Absolute unix style + array('/etc/phpbb', true), + // Unix does not support \ so that is not an absolute path + array('\etc\phpbb', false), + + // Absolute windows style + array('c:\windows', true), + array('C:\Windows', true), + array('c:/windows', true), + array('C:/Windows', true), + + // Executable + array('etc/phpbb', false), + array('explorer.exe', false), + + // Relative subdir + array('Windows\System32', false), + array('Windows\System32\explorer.exe', false), + array('Windows/System32', false), + array('Windows/System32/explorer.exe', false), + + // Relative updir + array('..\Windows\System32', false), + array('..\Windows\System32\explorer.exe', false), + array('../Windows/System32', false), + array('../Windows/System32/explorer.exe', false), + ); + } + + /** + * @dataProvider is_absolute_data + */ + public function test_is_absolute($path, $expected) + { + $this->assertEquals($expected, \phpbb\storage\helper::is_absolute_path($path)); + } +} diff --git a/tests/storage/helper_realpath_test.php b/tests/storage/helper_realpath_test.php new file mode 100644 index 0000000000..1cc5e4b1ce --- /dev/null +++ b/tests/storage/helper_realpath_test.php @@ -0,0 +1,83 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_storage_helper_realpath_test extends phpbb_test_case +{ + protected static $storage_helper_phpbb_own_realpath; + + static public function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + self::$storage_helper_phpbb_own_realpath = new ReflectionMethod('\phpbb\storage\helper', 'phpbb_own_realpath'); + self::$storage_helper_phpbb_own_realpath->setAccessible(true); + } + + public function setUp() + { + parent::setUp(); + } + + public function realpath_resolve_absolute_without_symlinks_data() + { + return array( + // Constant data + array(__DIR__, __DIR__), + array(__DIR__ . '/../storage/../storage', __DIR__), + array(__DIR__ . '/././', __DIR__), + array(__DIR__ . '/non_existent', false), + + array(__FILE__, __FILE__), + array(__FILE__ . '../', false), + ); + } + + public function realpath_resolve_relative_without_symlinks_data() + { + if (!function_exists('getcwd')) + { + return array(); + } + + $relative_path = \phpbb\storage\helper::make_path_relative(__DIR__, getcwd()); + + return array( + array($relative_path, __DIR__), + array($relative_path . '../storage/../storage', __DIR__), + array($relative_path . '././', __DIR__), + + array($relative_path . 'helper_realpath_test.php', __FILE__), + ); + } + + /** + * @dataProvider realpath_resolve_absolute_without_symlinks_data + */ + public function test_realpath_absolute_without_links($path, $expected) + { + $this->assertEquals($expected, self::$storage_helper_phpbb_own_realpath->invoke(null, $path)); + } + + /** + * @dataProvider realpath_resolve_relative_without_symlinks_data + */ + public function test_realpath_relative_without_links($path, $expected) + { + if (!function_exists('getcwd')) + { + $this->markTestSkipped('phpbb_own_realpath() cannot be tested with relative paths: getcwd is not available.'); + } + + $this->assertEquals($expected, self::$storage_helper_phpbb_own_realpath->invoke(null, $path)); + } +} From 0d14c856305a05df138d0518af06d34fc0f2b879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 25 Jun 2017 15:08:40 +0200 Subject: [PATCH 02/26] [ticket/15253] Use path from adapter PHPBB3-15253 --- .../default/container/services_storage.yml | 2 + phpBB/phpbb/storage/adapter/local.php | 31 ++++++++------- phpBB/phpbb/storage/controller.php | 39 ------------------- 3 files changed, 20 insertions(+), 52 deletions(-) delete mode 100644 phpBB/phpbb/storage/controller.php diff --git a/phpBB/config/default/container/services_storage.yml b/phpBB/config/default/container/services_storage.yml index 7d004ec9c6..83b013a1c2 100644 --- a/phpBB/config/default/container/services_storage.yml +++ b/phpBB/config/default/container/services_storage.yml @@ -2,8 +2,10 @@ services: storage.adapter.avatar: class: phpbb\storage\adapter\local arguments: + - '@config' - '@filesystem' - '%core.root_path%' + - 'avatar_path' storage.avatar: class: phpbb\storage\storage arguments: diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index b2d8c58d36..00b51c64f0 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -25,16 +25,21 @@ class local implements adapter_interface */ protected $filesystem; - /** @var string phpBB root path */ - protected $phpbb_root_path; + /** @var string path */ + protected $root_path; /** * Constructor */ - public function __construct($filesystem, $phpbb_root_path) + public function __construct(\phpbb\config\config $config, \phpbb\filesystem\filesystem $filesystem, $root_path, $path_key) { $this->filesystem = $filesystem; - $this->phpbb_root_path = $phpbb_root_path; + $this->root_path = $root_path.$config[$path_key]; + + if(substr($this->root_path, -1, 1) != DIRECTORY_SEPARATOR) + { + $this->root_path = $this->root_path.DIRECTORY_SEPARATOR; + } } /** @@ -42,14 +47,14 @@ class local implements adapter_interface */ public function put_contents($path, $content) { - if ($this->exists($this->phpbb_root_path.$path)) + if ($this->exists($this->root_path.$path)) { throw new exception('', $path); // FILE_EXISTS } try { - $this->filesystem->dump_file($this->phpbb_root_path.$path, $content); + $this->filesystem->dump_file($this->root_path.$path, $content); } catch (filesystem_exception $e) { @@ -62,12 +67,12 @@ class local implements adapter_interface */ public function get_contents($path) { - if (!$this->exists($this->phpbb_root_path.$path)) + if (!$this->exists($this->root_path.$path)) { throw new exception('', $path); // FILE_DONT_EXIST } - if (($content = @file_get_contents($this->phpbb_root_path.$path)) === false) + if (($content = @file_get_contents($this->root_path.$path)) === false) { throw new exception('', $path); // CANNOT READ FILE } @@ -80,7 +85,7 @@ class local implements adapter_interface */ public function exists($path) { - return $this->filesystem->exists($this->phpbb_root_path.$path); + return $this->filesystem->exists($this->root_path.$path); } /** @@ -90,7 +95,7 @@ class local implements adapter_interface { try { - $this->filesystem->remove($this->phpbb_root_path.$path); + $this->filesystem->remove($this->root_path.$path); } catch (filesystem_exception $e) { @@ -105,7 +110,7 @@ class local implements adapter_interface { try { - $this->filesystem->rename($this->phpbb_root_path.$path_orig, $this->phpbb_root_path.$path_dest, false); + $this->filesystem->rename($this->root_path.$path_orig, $this->root_path.$path_dest, false); } catch (filesystem_exception $e) { @@ -120,7 +125,7 @@ class local implements adapter_interface { try { - $this->filesystem->copy($this->phpbb_root_path.$path_orig, $this->phpbb_root_path.$path_dest, false); + $this->filesystem->copy($this->root_path.$path_orig, $this->root_path.$path_dest, false); } catch (filesystem_exception $e) { @@ -135,7 +140,7 @@ class local implements adapter_interface { try { - $this->filesystem->mkdir($this->phpbb_root_path.$path); + $this->filesystem->mkdir($this->root_path.$path); } catch (filesystem_exception $e) { diff --git a/phpBB/phpbb/storage/controller.php b/phpBB/phpbb/storage/controller.php deleted file mode 100644 index 22f733faf7..0000000000 --- a/phpBB/phpbb/storage/controller.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @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\storage; - -class controller -{ - /** - * @var ContainerInterface - */ - protected $container; - - /** - * Constructor. - * - * @param ContainerInterface $container A ContainerInterface instance - */ - public function __construct(ContainerInterface $container) - { - $this->container = $container; - } - - public function get_file($storage, $file) - { - $storage = $this->container->get($storage); - - return $storage->get_contents($file); - } -} From afb804fe1efabd72b2eb4364d9a2652e80a8ae27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 25 Jun 2017 17:01:22 +0200 Subject: [PATCH 03/26] [ticket/15253] Fix exception route PHPBB3-15253 --- phpBB/phpbb/storage/adapter/local.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index 00b51c64f0..dcc6d57793 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -14,7 +14,7 @@ namespace phpbb\storage\adapter; use phpbb\storage\exception\exception; -use phpbb\filesystem\filesystem_exception; +use phpbb\filesystem\exception\filesystem_exception; class local implements adapter_interface { @@ -31,10 +31,10 @@ class local implements adapter_interface /** * Constructor */ - public function __construct(\phpbb\config\config $config, \phpbb\filesystem\filesystem $filesystem, $root_path, $path_key) + public function __construct(\phpbb\config\config $config, \phpbb\filesystem\filesystem $filesystem, $phpbb_root_path, $path_key) { $this->filesystem = $filesystem; - $this->root_path = $root_path.$config[$path_key]; + $this->root_path = $phpbb_root_path.$config[$path_key]; if(substr($this->root_path, -1, 1) != DIRECTORY_SEPARATOR) { @@ -47,7 +47,7 @@ class local implements adapter_interface */ public function put_contents($path, $content) { - if ($this->exists($this->root_path.$path)) + if ($this->exists($path)) { throw new exception('', $path); // FILE_EXISTS } @@ -67,7 +67,7 @@ class local implements adapter_interface */ public function get_contents($path) { - if (!$this->exists($this->root_path.$path)) + if (!$this->exists($path)) { throw new exception('', $path); // FILE_DONT_EXIST } From 91163d7ec3ddd2039847dda687d44d4abe07328a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 25 Jun 2017 19:53:40 +0200 Subject: [PATCH 04/26] [ticket/15253] Update test PHPBB3-15253 --- phpBB/phpbb/storage/adapter/local.php | 2 +- phpBB/phpbb/storage/storage.php | 2 -- tests/storage/adapter/local_test.php | 24 ++++++++++++++++-------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index dcc6d57793..52597f9bca 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -36,7 +36,7 @@ class local implements adapter_interface $this->filesystem = $filesystem; $this->root_path = $phpbb_root_path.$config[$path_key]; - if(substr($this->root_path, -1, 1) != DIRECTORY_SEPARATOR) + if (substr($this->root_path, -1, 1) != DIRECTORY_SEPARATOR) { $this->root_path = $this->root_path.DIRECTORY_SEPARATOR; } diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index dc52004b3a..1f5e751bbb 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -13,8 +13,6 @@ namespace phpbb\storage; -use phpbb\storage\exception\exception; - class storage { protected $adapter; diff --git a/tests/storage/adapter/local_test.php b/tests/storage/adapter/local_test.php index ecc06cf688..0ae62a85a1 100644 --- a/tests/storage/adapter/local_test.php +++ b/tests/storage/adapter/local_test.php @@ -16,10 +16,17 @@ protected $adapter; public function setUp() - { - parent::setUp(); - $this->adapter = new \phpbb\storage\adapter\local(); - } + { + parent::setUp(); + + $config = new \phpbb\config\config(array( + 'test_path' => '.', + )); + $filesystem = new \phpbb\filesystem\filesystem(); + $phpbb_root_path = getcwd().DIRECTORY_SEPARATOR; + $path_key = 'test_path'; + $this->adapter = new \phpbb\storage\adapter\local($config, $filesystem, $phpbb_root_path, $path_key); + } public function tearDown() { @@ -44,11 +51,11 @@ public function test_exists() { // Exists with files - $this->assertTrue($this->adapter->exists(__DIR__.'/local_test.php')); - $this->assertFalse($this->adapter->exists(__DIR__.'/nonexistent_file.php')); + $this->assertTrue($this->adapter->exists('README.md')); + $this->assertFalse($this->adapter->exists('nonexistent_file.php')); // exists with directory - $this->assertTrue($this->adapter->exists(__DIR__.'/../adapter')); - $this->assertFalse($this->adapter->exists(__DIR__.'/../nonexistet_folder')); + $this->assertTrue($this->adapter->exists('phpBB')); + $this->assertFalse($this->adapter->exists('nonexistet_folder')); } public function test_delete() @@ -83,4 +90,5 @@ unlink('file.txt'); unlink('file2.txt'); } + } From 21c9b0eeae4252291af5c5b1ffd03fdb68e238b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sun, 25 Jun 2017 22:18:47 +0200 Subject: [PATCH 05/26] [ticket/15253] Fix coding style PHPBB3-15253 --- phpBB/phpbb/storage/adapter/local.php | 18 +++++++++--------- tests/storage/adapter/local_test.php | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index 52597f9bca..4673dbca6e 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -34,11 +34,11 @@ class local implements adapter_interface public function __construct(\phpbb\config\config $config, \phpbb\filesystem\filesystem $filesystem, $phpbb_root_path, $path_key) { $this->filesystem = $filesystem; - $this->root_path = $phpbb_root_path.$config[$path_key]; + $this->root_path = $phpbb_root_path . $config[$path_key]; if (substr($this->root_path, -1, 1) != DIRECTORY_SEPARATOR) { - $this->root_path = $this->root_path.DIRECTORY_SEPARATOR; + $this->root_path = $this->root_path . DIRECTORY_SEPARATOR; } } @@ -54,7 +54,7 @@ class local implements adapter_interface try { - $this->filesystem->dump_file($this->root_path.$path, $content); + $this->filesystem->dump_file($this->root_path . $path, $content); } catch (filesystem_exception $e) { @@ -72,7 +72,7 @@ class local implements adapter_interface throw new exception('', $path); // FILE_DONT_EXIST } - if (($content = @file_get_contents($this->root_path.$path)) === false) + if (($content = @file_get_contents($this->root_path . $path)) === false) { throw new exception('', $path); // CANNOT READ FILE } @@ -85,7 +85,7 @@ class local implements adapter_interface */ public function exists($path) { - return $this->filesystem->exists($this->root_path.$path); + return $this->filesystem->exists($this->root_path . $path); } /** @@ -95,7 +95,7 @@ class local implements adapter_interface { try { - $this->filesystem->remove($this->root_path.$path); + $this->filesystem->remove($this->root_path . $path); } catch (filesystem_exception $e) { @@ -110,7 +110,7 @@ class local implements adapter_interface { try { - $this->filesystem->rename($this->root_path.$path_orig, $this->root_path.$path_dest, false); + $this->filesystem->rename($this->root_path . $path_orig, $this->root_path . $path_dest, false); } catch (filesystem_exception $e) { @@ -125,7 +125,7 @@ class local implements adapter_interface { try { - $this->filesystem->copy($this->root_path.$path_orig, $this->root_path.$path_dest, false); + $this->filesystem->copy($this->root_path . $path_orig, $this->root_path . $path_dest, false); } catch (filesystem_exception $e) { @@ -140,7 +140,7 @@ class local implements adapter_interface { try { - $this->filesystem->mkdir($this->root_path.$path); + $this->filesystem->mkdir($this->root_path . $path); } catch (filesystem_exception $e) { diff --git a/tests/storage/adapter/local_test.php b/tests/storage/adapter/local_test.php index 0ae62a85a1..bed30aa77f 100644 --- a/tests/storage/adapter/local_test.php +++ b/tests/storage/adapter/local_test.php @@ -23,7 +23,7 @@ 'test_path' => '.', )); $filesystem = new \phpbb\filesystem\filesystem(); - $phpbb_root_path = getcwd().DIRECTORY_SEPARATOR; + $phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR; $path_key = 'test_path'; $this->adapter = new \phpbb\storage\adapter\local($config, $filesystem, $phpbb_root_path, $path_key); } From ecb79539f417c3a527e954deff538d8b4cc12945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Mon, 26 Jun 2017 15:49:31 +0200 Subject: [PATCH 06/26] [ticket/15253] Use storage helper methods instead of filesystem methods PHPBB3-15253 --- phpBB/common.php | 3 +- phpBB/config/default/container/services.yml | 3 - .../default/container/services_content.yml | 2 - .../default/container/services_extensions.yml | 1 - .../default/container/services_routing.yml | 1 - .../default/container/services_storage.yml | 16 -- .../default/container/services_twig.yml | 2 - phpBB/config/installer/container/services.yml | 2 - phpBB/develop/create_schema_files.php | 2 +- phpBB/develop/mysql_upgrader.php | 2 +- phpBB/includes/bbcode.php | 4 +- phpBB/includes/functions.php | 10 +- phpBB/includes/functions_compatibility.php | 50 ++---- phpBB/includes/functions_messenger.php | 4 +- phpBB/phpbb/composer/installer.php | 2 +- phpBB/phpbb/di/container_builder.php | 4 +- phpBB/phpbb/di/extension/core.php | 3 +- phpBB/phpbb/extension/di/extension_base.php | 3 +- phpBB/phpbb/extension/manager.php | 6 +- phpBB/phpbb/filesystem/filesystem.php | 168 +----------------- .../phpbb/filesystem/filesystem_interface.php | 8 + phpBB/phpbb/finder.php | 7 +- phpBB/phpbb/install/helper/database.php | 2 +- .../install_database/task/create_schema.php | 2 +- .../task/create_schema_file.php | 2 +- phpBB/phpbb/path_helper.php | 17 +- phpBB/phpbb/routing/file_locator.php | 5 +- phpBB/phpbb/routing/helper.php | 11 +- phpBB/phpbb/session.php | 8 +- phpBB/phpbb/template/asset.php | 11 +- phpBB/phpbb/template/twig/loader.php | 24 +-- phpBB/phpbb/viewonline_helper.php | 10 +- tests/avatar/manager_test.php | 1 - tests/console/cron/cron_list_test.php | 1 - tests/console/cron/run_test.php | 3 - tests/controller/common_helper_route.php | 23 ++- tests/controller/controller_test.php | 2 +- tests/cron/manager_test.php | 1 - tests/dbal/migrator_test.php | 1 - .../ext/vendor/enabled_4/di/extension.php | 3 +- tests/email/email_parsing_test.php | 3 +- tests/extension/finder_test.php | 3 +- tests/extension/manager_test.php | 1 - tests/extension/metadata_manager_test.php | 4 +- tests/filesystem/clean_path_test.php | 54 ------ tests/filesystem/is_absolute_test.php | 68 ------- tests/filesystem/realpath_test.php | 90 ---------- tests/functions/build_url_test.php | 1 - tests/pagination/pagination_test.php | 5 +- tests/path_helper/path_helper_test.php | 16 +- tests/security/redirect_test.php | 1 - tests/template/includephp_test.php | 3 +- tests/template/template_allfolder_test.php | 3 +- tests/template/template_events_test.php | 3 +- tests/template/template_includecss_test.php | 3 +- tests/template/template_test_case.php | 6 +- .../template/template_test_case_with_tree.php | 3 +- .../phpbb_database_test_case.php | 2 +- ...phpbb_database_test_connection_manager.php | 2 +- .../phpbb_functional_test_case.php | 1 - .../phpbb_session_test_case.php | 4 +- tests/test_framework/phpbb_ui_test_case.php | 1 - 62 files changed, 109 insertions(+), 598 deletions(-) delete mode 100644 phpBB/config/default/container/services_storage.yml delete mode 100644 tests/filesystem/clean_path_test.php delete mode 100644 tests/filesystem/is_absolute_test.php delete mode 100644 tests/filesystem/realpath_test.php diff --git a/phpBB/common.php b/phpBB/common.php index 31b45df68a..46098ff44c 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -65,8 +65,7 @@ if (!defined('PHPBB_INSTALLED')) // Eliminate . and .. from the path require($phpbb_root_path . 'phpbb/filesystem.' . $phpEx); - $phpbb_filesystem = new phpbb\filesystem\filesystem(); - $script_path = $phpbb_filesystem->clean_path($script_path); + $script_path = \phpbb\storage\helper::clean_path($script_path); $url = (($secure) ? 'https://' : 'http://') . $server_name; diff --git a/phpBB/config/default/container/services.yml b/phpBB/config/default/container/services.yml index c917c948dd..2f5a4bd5a6 100644 --- a/phpBB/config/default/container/services.yml +++ b/phpBB/config/default/container/services.yml @@ -25,7 +25,6 @@ imports: - { resource: services_profilefield.yml } - { resource: services_report.yml } - { resource: services_routing.yml } - - { resource: services_storage.yml } - { resource: services_text_formatter.yml } - { resource: services_text_reparser.yml } - { resource: services_twig.yml } @@ -106,7 +105,6 @@ services: file_locator: class: phpbb\routing\file_locator arguments: - - '@filesystem' - '%core.root_path%' group_helper: @@ -130,7 +128,6 @@ services: class: phpbb\path_helper arguments: - '@symfony_request' - - '@filesystem' - '@request' - '%core.root_path%' - '%core.php_ext%' diff --git a/phpBB/config/default/container/services_content.yml b/phpBB/config/default/container/services_content.yml index 602fd25f4e..63e370f9b5 100644 --- a/phpBB/config/default/container/services_content.yml +++ b/phpBB/config/default/container/services_content.yml @@ -68,5 +68,3 @@ services: viewonline_helper: class: phpbb\viewonline_helper - arguments: - - '@filesystem' diff --git a/phpBB/config/default/container/services_extensions.yml b/phpBB/config/default/container/services_extensions.yml index e07ec1fd09..53e36f0fda 100644 --- a/phpBB/config/default/container/services_extensions.yml +++ b/phpBB/config/default/container/services_extensions.yml @@ -5,7 +5,6 @@ services: - '@service_container' - '@dbal.conn' - '@config' - - '@filesystem' - '%tables.ext%' - '%core.root_path%' - '%core.php_ext%' diff --git a/phpBB/config/default/container/services_routing.yml b/phpBB/config/default/container/services_routing.yml index 0bf0a33ab4..cb397eab85 100644 --- a/phpBB/config/default/container/services_routing.yml +++ b/phpBB/config/default/container/services_routing.yml @@ -23,7 +23,6 @@ services: - '@router' - '@symfony_request' - '@request' - - '@filesystem' - '%core.root_path%' - '%core.php_ext%' diff --git a/phpBB/config/default/container/services_storage.yml b/phpBB/config/default/container/services_storage.yml deleted file mode 100644 index 83b013a1c2..0000000000 --- a/phpBB/config/default/container/services_storage.yml +++ /dev/null @@ -1,16 +0,0 @@ -services: - storage.adapter.avatar: - class: phpbb\storage\adapter\local - arguments: - - '@config' - - '@filesystem' - - '%core.root_path%' - - 'avatar_path' - storage.avatar: - class: phpbb\storage\storage - arguments: - - '@storage.adapter.avatar' - storage.controller: - class: phpbb\storage\controller - arguments: - - '@service_container' diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index a9b5b6d4cd..e9c0360436 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -24,8 +24,6 @@ services: template.twig.loader: class: phpbb\template\twig\loader - arguments: - - '@filesystem' template.twig.extensions.collection: class: phpbb\di\service_collection diff --git a/phpBB/config/installer/container/services.yml b/phpBB/config/installer/container/services.yml index 7203c0ab10..080b8a48e3 100644 --- a/phpBB/config/installer/container/services.yml +++ b/phpBB/config/installer/container/services.yml @@ -31,7 +31,6 @@ services: file_locator: class: phpbb\routing\file_locator arguments: - - '@filesystem' - '%core.root_path%' kernel_exception_subscriber: @@ -50,7 +49,6 @@ services: class: phpbb\path_helper arguments: - '@symfony_request' - - '@filesystem' - '@request' - '%core.root_path%' - '%core.php_ext%' diff --git a/phpBB/develop/create_schema_files.php b/phpBB/develop/create_schema_files.php index 6e0a082045..2dcb6e4a34 100644 --- a/phpBB/develop/create_schema_files.php +++ b/phpBB/develop/create_schema_files.php @@ -44,7 +44,7 @@ require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx); $phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx); $phpbb_class_loader->register(); -$finder = new \phpbb\finder(new \phpbb\filesystem\filesystem(), $phpbb_root_path); +$finder = new \phpbb\finder($phpbb_root_path); $classes = $finder->core_path('phpbb/') ->directory('/db/migration/data') ->get_classes(); diff --git a/phpBB/develop/mysql_upgrader.php b/phpBB/develop/mysql_upgrader.php index 276c010e84..a1e4b76d9c 100644 --- a/phpBB/develop/mysql_upgrader.php +++ b/phpBB/develop/mysql_upgrader.php @@ -62,7 +62,7 @@ echo "USE $dbname;$newline$newline"; @set_time_limit(0); -$finder = new \phpbb\finder(new \phpbb\filesystem\filesystem(), $phpbb_root_path); +$finder = new \phpbb\finder($phpbb_root_path); $classes = $finder->core_path('phpbb/') ->directory('/db/migration/data') ->get_classes(); diff --git a/phpBB/includes/bbcode.php b/phpBB/includes/bbcode.php index 6e40eef1d5..3869abd689 100644 --- a/phpBB/includes/bbcode.php +++ b/phpBB/includes/bbcode.php @@ -156,9 +156,7 @@ class bbcode $phpbb_container->get('path_helper'), $phpbb_container->getParameter('core.cache_dir'), $phpbb_container->get('ext.manager'), - new \phpbb\template\twig\loader( - $phpbb_filesystem - ) + new \phpbb\template\twig\loader() ), $phpbb_container->getParameter('core.cache_dir'), $phpbb_container->get('user'), diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 248adb2b7c..adfe3c01a2 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -3479,15 +3479,7 @@ function phpbb_filter_root_path($errfile) if (empty($root_path)) { - if ($phpbb_filesystem) - { - $root_path = $phpbb_filesystem->realpath(dirname(__FILE__) . '/../'); - } - else - { - $filesystem = new \phpbb\filesystem\filesystem(); - $root_path = $filesystem->realpath(dirname(__FILE__) . '/../'); - } + $root_path = \phpbb\storage\helper::realpath(dirname(__FILE__) . '/../'); } return str_replace(array($root_path, '\\'), array('[ROOT]', '/'), $errfile); diff --git a/phpBB/includes/functions_compatibility.php b/phpBB/includes/functions_compatibility.php index fdb524dfec..9bf052f479 100644 --- a/phpBB/includes/functions_compatibility.php +++ b/phpBB/includes/functions_compatibility.php @@ -87,7 +87,7 @@ function phpbb_check_hash($password, $hash) /** * Eliminates useless . and .. components from specified path. * -* Deprecated, use filesystem class instead +* Deprecated, use storage helper class instead * * @param string $path Path to clean * @return string Cleaned path @@ -96,36 +96,12 @@ function phpbb_check_hash($password, $hash) */ function phpbb_clean_path($path) { - global $phpbb_path_helper, $phpbb_container; - - if (!$phpbb_path_helper && $phpbb_container) + if (!class_exists('\phpbb\storage\helper')) { - /* @var $phpbb_path_helper \phpbb\path_helper */ - $phpbb_path_helper = $phpbb_container->get('path_helper'); - } - else if (!$phpbb_path_helper) - { - global $phpbb_root_path, $phpEx; - - // The container is not yet loaded, use a new instance - if (!class_exists('\phpbb\path_helper')) - { - require($phpbb_root_path . 'phpbb/path_helper.' . $phpEx); - } - - $request = new phpbb\request\request(); - $phpbb_path_helper = new phpbb\path_helper( - new phpbb\symfony_request( - $request - ), - new phpbb\filesystem\filesystem(), - $request, - $phpbb_root_path, - $phpEx - ); + require($phpbb_root_path . 'phpbb/storage/helper.' . $phpEx); } - return $phpbb_path_helper->clean_path($path); + return \phpbb\storage\helper::clean_path($path); } /** @@ -463,25 +439,31 @@ function phpbb_is_writable($file) * @param string $path Path to check absoluteness of * @return boolean * - * @deprecated 3.2.0-dev use \phpbb\filesystem\filesystem::is_absolute_path() instead + * @deprecated 3.2.0-dev use \phpbb\storage\helper::is_absolute_path() instead */ function phpbb_is_absolute($path) { - global $phpbb_filesystem; + if (!class_exists('\phpbb\storage\helper')) + { + require($phpbb_root_path . 'phpbb/storage/helper.' . $phpEx); + } - return $phpbb_filesystem->is_absolute_path($path); + return \phpbb\storage\helper::is_absolute_path($path); } /** * A wrapper for realpath * - * @deprecated 3.2.0-dev use \phpbb\filesystem\filesystem::realpath() instead + * @deprecated 3.2.0-dev use \phpbb\storage\helper::realpath() instead */ function phpbb_realpath($path) { - global $phpbb_filesystem; + if (!class_exists('\phpbb\storage\helper')) + { + require($phpbb_root_path . 'phpbb/storage/helper.' . $phpEx); + } - return $phpbb_filesystem->realpath($path); + return \phpbb\storage\helper::realpath($path); } /** diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index 0b8c32c8bd..65fa8a4175 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -699,9 +699,7 @@ class messenger $phpbb_container->get('path_helper'), $phpbb_container->getParameter('core.template.cache_path'), $phpbb_container->get('ext.manager'), - new \phpbb\template\twig\loader( - $phpbb_container->get('filesystem') - ), + new \phpbb\template\twig\loader(), $phpbb_dispatcher, array() ); diff --git a/phpBB/phpbb/composer/installer.php b/phpBB/phpbb/composer/installer.php index 6de57541fb..6bab30eb9e 100644 --- a/phpBB/phpbb/composer/installer.php +++ b/phpBB/phpbb/composer/installer.php @@ -108,7 +108,7 @@ class installer $this->root_path = $root_path; $this->request = $request; - putenv('COMPOSER_HOME=' . $filesystem->realpath($root_path) . '/store/composer'); + putenv('COMPOSER_HOME=' . \phpbb\storage\helper::realpath($root_path) . '/store/composer'); } /** diff --git a/phpBB/phpbb/di/container_builder.php b/phpBB/phpbb/di/container_builder.php index c4cb64144b..47a858d605 100644 --- a/phpBB/phpbb/di/container_builder.php +++ b/phpBB/phpbb/di/container_builder.php @@ -13,7 +13,6 @@ namespace phpbb\di; -use phpbb\filesystem\filesystem; use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; use Symfony\Component\Config\ConfigCache; use Symfony\Component\Config\FileLocator; @@ -180,8 +179,7 @@ class container_builder $this->register_ext_compiler_pass(); } - $filesystem = new filesystem(); - $loader = new YamlFileLoader($this->container, new FileLocator($filesystem->realpath($this->get_config_path()))); + $loader = new YamlFileLoader($this->container, new FileLocator(\phpbb\storage\helper::realpath($this->get_config_path()))); $loader->load($this->container->getParameter('core.environment') . '/config.yml'); $this->inject_custom_parameters(); diff --git a/phpBB/phpbb/di/extension/core.php b/phpBB/phpbb/di/extension/core.php index 64c3dafcad..84148eced3 100644 --- a/phpBB/phpbb/di/extension/core.php +++ b/phpBB/phpbb/di/extension/core.php @@ -53,8 +53,7 @@ class core extends Extension */ public function load(array $configs, ContainerBuilder $container) { - $filesystem = new \phpbb\filesystem\filesystem(); - $loader = new YamlFileLoader($container, new FileLocator($filesystem->realpath($this->config_path))); + $loader = new YamlFileLoader($container, new FileLocator(\phpbb\storage\helper::realpath($this->config_path))); $loader->load($container->getParameter('core.environment') . '/container/environment.yml'); $config = $this->getConfiguration($configs, $container); diff --git a/phpBB/phpbb/extension/di/extension_base.php b/phpBB/phpbb/extension/di/extension_base.php index ba74615e70..e6fd5688cc 100644 --- a/phpBB/phpbb/extension/di/extension_base.php +++ b/phpBB/phpbb/extension/di/extension_base.php @@ -94,8 +94,7 @@ class extension_base extends Extension if ($services_directory && $services_file) { - $filesystem = new \phpbb\filesystem\filesystem(); - $loader = new YamlFileLoader($container, new FileLocator($filesystem->realpath($services_directory))); + $loader = new YamlFileLoader($container, new FileLocator(\phpbb\storage\helper::realpath($services_directory))); $loader->load($services_file); } } diff --git a/phpBB/phpbb/extension/manager.php b/phpBB/phpbb/extension/manager.php index 4b4109bd85..a98e9c27c0 100644 --- a/phpBB/phpbb/extension/manager.php +++ b/phpBB/phpbb/extension/manager.php @@ -40,14 +40,13 @@ class manager * @param ContainerInterface $container A container * @param \phpbb\db\driver\driver_interface $db A database connection * @param \phpbb\config\config $config Config object - * @param \phpbb\filesystem\filesystem_interface $filesystem * @param string $extension_table The name of the table holding extensions * @param string $phpbb_root_path Path to the phpbb includes directory. * @param string $php_ext php file extension, defaults to php * @param \phpbb\cache\service $cache A cache instance or null * @param string $cache_name The name of the cache variable, defaults to _ext */ - public function __construct(ContainerInterface $container, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\filesystem\filesystem_interface $filesystem, $extension_table, $phpbb_root_path, $php_ext = 'php', \phpbb\cache\service $cache = null, $cache_name = '_ext') + public function __construct(ContainerInterface $container, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, $extension_table, $phpbb_root_path, $php_ext = 'php', \phpbb\cache\service $cache = null, $cache_name = '_ext') { $this->cache = $cache; $this->cache_name = $cache_name; @@ -55,7 +54,6 @@ class manager $this->container = $container; $this->db = $db; $this->extension_table = $extension_table; - $this->filesystem = $filesystem; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; @@ -619,7 +617,7 @@ class manager */ public function get_finder($use_all_available = false) { - $finder = new \phpbb\finder($this->filesystem, $this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder'); + $finder = new \phpbb\finder($this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder'); if ($use_all_available) { $finder->set_extensions(array_keys($this->all_available())); diff --git a/phpBB/phpbb/filesystem/filesystem.php b/phpBB/phpbb/filesystem/filesystem.php index 3f39448f05..30a16bc756 100644 --- a/phpBB/phpbb/filesystem/filesystem.php +++ b/phpBB/phpbb/filesystem/filesystem.php @@ -162,26 +162,7 @@ class filesystem implements filesystem_interface */ public function clean_path($path) { - $exploded = explode('/', $path); - $filtered = array(); - foreach ($exploded as $part) - { - if ($part === '.' && !empty($filtered)) - { - continue; - } - - if ($part === '..' && !empty($filtered) && $filtered[count($filtered) - 1] !== '.' && $filtered[count($filtered) - 1] !== '..') - { - array_pop($filtered); - } - else - { - $filtered[] = $part; - } - } - $path = implode('/', $filtered); - return $path; + return \phpbb\storage\helper::clean_path($path); } /** @@ -227,7 +208,7 @@ class filesystem implements filesystem_interface */ public function is_absolute_path($path) { - return (isset($path[0]) && $path[0] === '/' || preg_match('#^[a-z]:[/\\\]#i', $path)) ? true : false; + return \phpbb\storage\helper::is_absolute_path($path); } /** @@ -305,7 +286,7 @@ class filesystem implements filesystem_interface */ public function make_path_relative($end_path, $start_path) { - return $this->symfony_filesystem->makePathRelative($end_path, $start_path); + return \phpbb\storage\helper::make_path_relative($end_path, $start_path); } /** @@ -639,6 +620,8 @@ class filesystem implements filesystem_interface /** * Try to resolve real path when PHP's realpath failes to do so * + * @deprecated 3.3.0-a1 (To be removed: 4.0.0) + * * @param string $path * @return bool|string */ @@ -764,6 +747,8 @@ class filesystem implements filesystem_interface /** * Try to resolve symlinks in path * + * @deprecated 3.3.0-a1 (To be removed: 4.0.0) + * * @param string $path The path to resolve * @param string $prefix The path prefix (on windows the drive letter) * @param bool $absolute Whether or not the path is absolute @@ -774,143 +759,6 @@ class filesystem implements filesystem_interface */ protected function resolve_path($path, $prefix = '', $absolute = false, $return_array = false) { - if ($return_array) - { - $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); - } - - trim ($path, '/'); - $path_parts = explode('/', $path); - $resolved = array(); - $resolved_path = $prefix; - $file_found = false; - - foreach ($path_parts as $path_part) - { - if ($file_found) - { - return false; - } - - if (empty($path_part) || ($path_part === '.' && ($absolute || !empty($resolved)))) - { - continue; - } - else if ($absolute && $path_part === '..') - { - if (empty($resolved)) - { - // No directories above root - return false; - } - - array_pop($resolved); - $resolved_path = false; - } - else if ($path_part === '..' && !empty($resolved) && !in_array($resolved[count($resolved) - 1], array('.', '..'))) - { - array_pop($resolved); - $resolved_path = false; - } - else - { - if ($resolved_path === false) - { - if (empty($resolved)) - { - $resolved_path = ($absolute) ? $prefix . '/' . $path_part : $path_part; - } - else - { - $tmp_array = $resolved; - if ($absolute) - { - array_unshift($tmp_array, $prefix); - } - - $resolved_path = implode('/', $tmp_array); - } - } - - $current_path = $resolved_path . '/' . $path_part; - - // Resolve symlinks - if (is_link($current_path)) - { - if (!function_exists('readlink')) - { - return false; - } - - $link = readlink($current_path); - - // Is link has an absolute path in it? - if ($this->is_absolute_path($link)) - { - if (defined('PHP_WINDOWS_VERSION_MAJOR')) - { - $prefix = $link[0] . ':'; - $link = substr($link, 2); - } - else - { - $prefix = ''; - } - - $resolved = $this->resolve_path($link, $prefix, true, true); - $absolute = true; - } - else - { - $resolved = $this->resolve_path($resolved_path . '/' . $link, $prefix, $absolute, true); - } - - if (!$resolved) - { - return false; - } - - $resolved_path = false; - } - else if (is_dir($current_path . '/')) - { - $resolved[] = $path_part; - $resolved_path = $current_path; - } - else if (is_file($current_path)) - { - $resolved[] = $path_part; - $resolved_path = $current_path; - $file_found = true; - } - else - { - return false; - } - } - } - - // If at the end of the path there were a .. or . - // we need to build the path again. - // Only doing this when a string is expected in return - if ($resolved_path === false && $return_array === false) - { - if (empty($resolved)) - { - $resolved_path = ($absolute) ? $prefix . '/' : './'; - } - else - { - $tmp_array = $resolved; - if ($absolute) - { - array_unshift($tmp_array, $prefix); - } - - $resolved_path = implode('/', $tmp_array); - } - } - - return ($return_array) ? $resolved : $resolved_path; + return \phpbb\storage\helper::resolve_path($path, $prefix, $absolute, $return_array); } } diff --git a/phpBB/phpbb/filesystem/filesystem_interface.php b/phpBB/phpbb/filesystem/filesystem_interface.php index 1093be2499..ec1462dabf 100644 --- a/phpBB/phpbb/filesystem/filesystem_interface.php +++ b/phpBB/phpbb/filesystem/filesystem_interface.php @@ -89,6 +89,8 @@ interface filesystem_interface /** * Eliminates useless . and .. components from specified path. * + * @deprecated 3.3.0-a1 (To be removed: 4.0.0) + * * @param string $path Path to clean * * @return string Cleaned path @@ -132,6 +134,8 @@ interface filesystem_interface /** * Checks if a path is absolute or not * + * @deprecated 3.3.0-a1 (To be removed: 4.0.0) + * * @param string $path Path to check * * @return bool true if the path is absolute, false otherwise @@ -161,6 +165,8 @@ interface filesystem_interface /** * Given an existing path, convert it to a path relative to a given starting path * + * @deprecated 3.3.0-a1 (To be removed: 4.0.0) + * * @param string $end_path Absolute path of target * @param string $start_path Absolute path where traversal begins * @@ -228,6 +234,8 @@ interface filesystem_interface /** * A wrapper for PHP's realpath * + * @deprecated 3.3.0-a1 (To be removed: 4.0.0) + * * Try to resolve realpath when PHP's realpath is not available, or * known to be buggy. * diff --git a/phpBB/phpbb/finder.php b/phpBB/phpbb/finder.php index 1f1d931880..a1d44c6587 100644 --- a/phpBB/phpbb/finder.php +++ b/phpBB/phpbb/finder.php @@ -19,7 +19,6 @@ namespace phpbb; class finder { protected $extensions; - protected $filesystem; protected $phpbb_root_path; protected $cache; protected $php_ext; @@ -48,16 +47,14 @@ class finder /** * Creates a new finder instance with its dependencies * - * @param \phpbb\filesystem\filesystem_interface $filesystem Filesystem instance * @param string $phpbb_root_path Path to the phpbb root directory * @param \phpbb\cache\service $cache A cache instance or null * @param string $php_ext php file extension * @param string $cache_name The name of the cache variable, defaults to * _ext_finder */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, $phpbb_root_path = '', \phpbb\cache\service $cache = null, $php_ext = 'php', $cache_name = '_ext_finder') + public function __construct($phpbb_root_path = '', \phpbb\cache\service $cache = null, $php_ext = 'php', $cache_name = '_ext_finder') { - $this->filesystem = $filesystem; $this->phpbb_root_path = $phpbb_root_path; $this->cache = $cache; $this->php_ext = $php_ext; @@ -244,7 +241,7 @@ class finder */ protected function sanitise_directory($directory) { - $directory = $this->filesystem->clean_path($directory); + $directory = \phpbb\storage\helper::clean_path($directory); $dir_len = strlen($directory); if ($dir_len > 1 && $directory[$dir_len - 1] === '/') diff --git a/phpBB/phpbb/install/helper/database.php b/phpBB/phpbb/install/helper/database.php index ad0f3dd3cd..3f70536a82 100644 --- a/phpBB/phpbb/install/helper/database.php +++ b/phpBB/phpbb/install/helper/database.php @@ -329,7 +329,7 @@ class database // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea if ($dbms_info['SCHEMA'] === 'sqlite' - && stripos($this->filesystem->realpath($dbhost), $this->filesystem->realpath($this->phpbb_root_path) === 0)) + && stripos(\phpbb\storage\helper::realpath($dbhost), \phpbb\storage\helper::realpath($this->phpbb_root_path) === 0)) { $errors[] = array( 'title' =>'INST_ERR_DB_FORUM_PATH', diff --git a/phpBB/phpbb/install/module/install_database/task/create_schema.php b/phpBB/phpbb/install/module/install_database/task/create_schema.php index a5635d5dbe..fb97caa59a 100644 --- a/phpBB/phpbb/install/module/install_database/task/create_schema.php +++ b/phpBB/phpbb/install/module/install_database/task/create_schema.php @@ -183,7 +183,7 @@ class create_schema extends \phpbb\install\task_base include ($this->phpbb_root_path . 'includes/constants.' . $this->php_ext); } - $finder = new \phpbb\finder($this->filesystem, $this->phpbb_root_path, null, $this->php_ext); + $finder = new \phpbb\finder($this->phpbb_root_path, null, $this->php_ext); $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes(); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($this->db, true); diff --git a/phpBB/phpbb/install/module/install_database/task/create_schema_file.php b/phpBB/phpbb/install/module/install_database/task/create_schema_file.php index b6d6ece17f..81db66e11e 100644 --- a/phpBB/phpbb/install/module/install_database/task/create_schema_file.php +++ b/phpBB/phpbb/install/module/install_database/task/create_schema_file.php @@ -117,7 +117,7 @@ class create_schema_file extends \phpbb\install\task_base include ($this->phpbb_root_path . 'includes/constants.' . $this->php_ext); } - $finder = new \phpbb\finder($this->filesystem, $this->phpbb_root_path, null, $this->php_ext); + $finder = new \phpbb\finder($this->phpbb_root_path, null, $this->php_ext); $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes(); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($this->db, true); diff --git a/phpBB/phpbb/path_helper.php b/phpBB/phpbb/path_helper.php index 154361ef64..16dd9aa038 100644 --- a/phpBB/phpbb/path_helper.php +++ b/phpBB/phpbb/path_helper.php @@ -21,9 +21,6 @@ class path_helper /** @var \phpbb\symfony_request */ protected $symfony_request; - /** @var \phpbb\filesystem\filesystem_interface */ - protected $filesystem; - /** @var \phpbb\request\request_interface */ protected $request; @@ -43,16 +40,14 @@ class path_helper * Constructor * * @param \phpbb\symfony_request $symfony_request - * @param \phpbb\filesystem\filesystem_interface $filesystem * @param \phpbb\request\request_interface $request * @param string $phpbb_root_path Relative path to phpBB root * @param string $php_ext PHP file extension * @param mixed $adm_relative_path Relative path admin path to adm/ root */ - public function __construct(\phpbb\symfony_request $symfony_request, \phpbb\filesystem\filesystem_interface $filesystem, \phpbb\request\request_interface $request, $phpbb_root_path, $php_ext, $adm_relative_path = null) + public function __construct(\phpbb\symfony_request $symfony_request, \phpbb\request\request_interface $request, $phpbb_root_path, $php_ext, $adm_relative_path = null) { $this->symfony_request = $symfony_request; - $this->filesystem = $filesystem; $this->request = $request; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; @@ -117,7 +112,7 @@ class path_helper $path = substr($path, 8); } - return $this->filesystem->clean_path($web_root_path . $path); + return \phpbb\storage\helper::clean_path($web_root_path . $path); } return $path; @@ -163,7 +158,7 @@ class path_helper // We do not need to escape $path_info, $request_uri and $script_name because we can not find their content in the result. // Path info (e.g. /foo/bar) - $path_info = $this->filesystem->clean_path($this->symfony_request->getPathInfo()); + $path_info = \phpbb\storage\helper::clean_path($this->symfony_request->getPathInfo()); // Full request URI (e.g. phpBB/app.php/foo/bar) $request_uri = $this->symfony_request->getRequestUri(); @@ -178,7 +173,7 @@ class path_helper */ if ($path_info === '/' && preg_match('/app\.' . $this->php_ext . '\/$/', $request_uri)) { - return $this->web_root_path = $this->filesystem->clean_path('./../' . $this->phpbb_root_path); + return $this->web_root_path = \phpbb\storage\helper::clean_path('./../' . $this->phpbb_root_path); } /* @@ -235,7 +230,7 @@ class path_helper } // Prepend ../ to the phpbb_root_path as many times as / exists in path_info - $this->web_root_path = $this->filesystem->clean_path( + $this->web_root_path = \phpbb\storage\helper::clean_path( './' . str_repeat('../', $corrections) . $this->phpbb_root_path ); return $this->web_root_path; @@ -326,7 +321,7 @@ class path_helper // Add length of URL delimiter to position $path = substr($url, $delimiter_position + 3); - return $scheme . $this->filesystem->clean_path($path); + return $scheme . \phpbb\storage\helper::clean_path($path); } /** diff --git a/phpBB/phpbb/routing/file_locator.php b/phpBB/phpbb/routing/file_locator.php index 64efcc6c76..bdf3a2145c 100644 --- a/phpBB/phpbb/routing/file_locator.php +++ b/phpBB/phpbb/routing/file_locator.php @@ -13,19 +13,18 @@ namespace phpbb\routing; -use phpbb\filesystem\filesystem_interface; use Symfony\Component\Config\FileLocator; class file_locator extends FileLocator { - public function __construct(filesystem_interface $filesystem, $paths = []) + public function __construct($paths = []) { $paths = (array) $paths; $absolute_paths = []; foreach ($paths as $path) { - $absolute_paths[] = $filesystem->realpath($path); + $absolute_paths[] = \phpbb\storage\helper::realpath($path); } parent::__construct($absolute_paths); diff --git a/phpBB/phpbb/routing/helper.php b/phpBB/phpbb/routing/helper.php index c15608dce5..ea8835d362 100644 --- a/phpBB/phpbb/routing/helper.php +++ b/phpBB/phpbb/routing/helper.php @@ -43,11 +43,6 @@ class helper */ protected $request; - /** - * @var \phpbb\filesystem The filesystem object - */ - protected $filesystem; - /** * phpBB root path * @var string @@ -67,17 +62,15 @@ class helper * @param \phpbb\routing\router $router phpBB router * @param \phpbb\symfony_request $symfony_request Symfony Request object * @param \phpbb\request\request_interface $request phpBB request object - * @param \phpbb\filesystem\filesystem $filesystem The filesystem object * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP file extension */ - public function __construct(\phpbb\config\config $config, \phpbb\routing\router $router, \phpbb\symfony_request $symfony_request, \phpbb\request\request_interface $request, \phpbb\filesystem\filesystem $filesystem, $phpbb_root_path, $php_ext) + public function __construct(\phpbb\config\config $config, \phpbb\routing\router $router, \phpbb\symfony_request $symfony_request, \phpbb\request\request_interface $request, $phpbb_root_path, $php_ext) { $this->config = $config; $this->router = $router; $this->symfony_request = $symfony_request; $this->request = $request; - $this->filesystem = $filesystem; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; } @@ -140,7 +133,7 @@ class helper } } - $base_url = $this->request->escape($this->filesystem->clean_path($base_url), true); + $base_url = $this->request->escape(\phpbb\storage\helper::clean_path($base_url), true); $context->setBaseUrl($base_url); diff --git a/phpBB/phpbb/session.php b/phpBB/phpbb/session.php index 68d36e5c57..4446e3defd 100644 --- a/phpBB/phpbb/session.php +++ b/phpBB/phpbb/session.php @@ -38,7 +38,7 @@ class session */ static function extract_current_page($root_path) { - global $request, $symfony_request, $phpbb_filesystem; + global $request, $symfony_request; $page_array = array(); @@ -85,15 +85,15 @@ class session $page_name = (substr($script_name, -1, 1) == '/') ? '' : basename($script_name); $page_name = urlencode(htmlspecialchars($page_name)); - $symfony_request_path = $phpbb_filesystem->clean_path($symfony_request->getPathInfo()); + $symfony_request_path = \phpbb\storage\helper::clean_path($symfony_request->getPathInfo()); if ($symfony_request_path !== '/') { $page_name .= str_replace('%2F', '/', urlencode($symfony_request_path)); } // current directory within the phpBB root (for example: adm) - $root_dirs = explode('/', str_replace('\\', '/', $phpbb_filesystem->realpath($root_path))); - $page_dirs = explode('/', str_replace('\\', '/', $phpbb_filesystem->realpath('./'))); + $root_dirs = explode('/', str_replace('\\', '/', \phpbb\storage\helper::realpath($root_path))); + $page_dirs = explode('/', str_replace('\\', '/', \phpbb\storage\helper::realpath('./'))); $intersection = array_intersect_assoc($root_dirs, $page_dirs); $root_dirs = array_diff_assoc($root_dirs, $intersection); diff --git a/phpBB/phpbb/template/asset.php b/phpBB/phpbb/template/asset.php index cb00f16549..b7469c7e06 100644 --- a/phpBB/phpbb/template/asset.php +++ b/phpBB/phpbb/template/asset.php @@ -20,20 +20,15 @@ class asset /** @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) + public function __construct($url, \phpbb\path_helper $path_helper) { $this->path_helper = $path_helper; - $this->filesystem = $filesystem; $this->set_url($url); } @@ -158,7 +153,7 @@ class asset 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; + $real_root_path = \phpbb\storage\helper::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) @@ -168,7 +163,7 @@ class asset else { // Else we make the path relative to the current working directory - $real_root_path = $this->filesystem->realpath('.') . DIRECTORY_SEPARATOR; + $real_root_path = \phpbb\storage\helper::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))); diff --git a/phpBB/phpbb/template/twig/loader.php b/phpBB/phpbb/template/twig/loader.php index c13e3ee298..6fa7eda2cc 100644 --- a/phpBB/phpbb/template/twig/loader.php +++ b/phpBB/phpbb/template/twig/loader.php @@ -20,24 +20,6 @@ class loader extends \Twig_Loader_Filesystem { protected $safe_directories = array(); - /** - * @var \phpbb\filesystem\filesystem_interface - */ - protected $filesystem; - - /** - * Constructor - * - * @param \phpbb\filesystem\filesystem_interface $filesystem - * @param string|array $paths - */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, $paths = array()) - { - $this->filesystem = $filesystem; - - parent::__construct($paths, $this->filesystem->realpath(dirname(__FILE__))); - } - /** * Set safe directories * @@ -67,7 +49,7 @@ class loader extends \Twig_Loader_Filesystem */ public function addSafeDirectory($directory) { - $directory = $this->filesystem->realpath($directory); + $directory = \phpbb\storage\helper::realpath($directory); if ($directory !== false) { @@ -107,7 +89,7 @@ class loader extends \Twig_Loader_Filesystem */ public function addPath($path, $namespace = self::MAIN_NAMESPACE) { - return parent::addPath($this->filesystem->realpath($path), $namespace); + return parent::addPath(\phpbb\storage\helper::realpath($path), $namespace); } /** @@ -147,7 +129,7 @@ class loader extends \Twig_Loader_Filesystem // can now check if we're within a "safe" directory // Find the real path of the directory the file is in - $directory = $this->filesystem->realpath(dirname($file)); + $directory = \phpbb\storage\helper::realpath(dirname($file)); if ($directory === false) { diff --git a/phpBB/phpbb/viewonline_helper.php b/phpBB/phpbb/viewonline_helper.php index 89915f2228..e034b20a56 100644 --- a/phpBB/phpbb/viewonline_helper.php +++ b/phpBB/phpbb/viewonline_helper.php @@ -18,15 +18,11 @@ namespace phpbb; */ class viewonline_helper { - /** @var \phpbb\filesystem\filesystem_interface */ - protected $filesystem; - /** - * @param \phpbb\filesystem\filesystem_interface $filesystem phpBB's filesystem service + * */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem) + public function __construct() { - $this->filesystem = $filesystem; } /** @@ -37,7 +33,7 @@ class viewonline_helper */ public function get_user_page($session_page) { - $session_page = $this->filesystem->clean_path($session_page); + $session_page = \phpbb\storage\helper::clean_path($session_page); if (strpos($session_page, './') === 0) { $session_page = substr($session_page, 2); diff --git a/tests/avatar/manager_test.php b/tests/avatar/manager_test.php index ca9803bb8a..762fd776e3 100644 --- a/tests/avatar/manager_test.php +++ b/tests/avatar/manager_test.php @@ -44,7 +44,6 @@ class phpbb_avatar_manager_test extends \phpbb_database_test_case new \phpbb\symfony_request( new phpbb_mock_request() ), - $filesystem, $this->createMock('\phpbb\request\request'), $phpbb_root_path, $phpEx diff --git a/tests/console/cron/cron_list_test.php b/tests/console/cron/cron_list_test.php index bfc897ac2e..931713cd3c 100644 --- a/tests/console/cron/cron_list_test.php +++ b/tests/console/cron/cron_list_test.php @@ -97,7 +97,6 @@ class phpbb_console_command_cron_list_test extends phpbb_test_case $mock_router, new \phpbb\symfony_request($request), $request, - new \phpbb\filesystem\filesystem(), $phpbb_root_path, $pathEx ); diff --git a/tests/console/cron/run_test.php b/tests/console/cron/run_test.php index 76c7bb27c4..81e4e69f39 100644 --- a/tests/console/cron/run_test.php +++ b/tests/console/cron/run_test.php @@ -73,7 +73,6 @@ class phpbb_console_command_cron_run_test extends phpbb_database_test_case $mock_router, new \phpbb\symfony_request($request), $request, - new \phpbb\filesystem\filesystem(), $phpbb_root_path, $phpEx ); @@ -147,7 +146,6 @@ class phpbb_console_command_cron_run_test extends phpbb_database_test_case $mock_router, new \phpbb\symfony_request($request), $request, - new \phpbb\filesystem\filesystem(), $phpbb_root_path, $phpEx ); @@ -188,7 +186,6 @@ class phpbb_console_command_cron_run_test extends phpbb_database_test_case $mock_router, new \phpbb\symfony_request($request), $request, - new \phpbb\filesystem\filesystem(), $phpbb_root_path, $phpEx ); diff --git a/tests/controller/common_helper_route.php b/tests/controller/common_helper_route.php index ea2bc042b1..3c732ff49b 100644 --- a/tests/controller/common_helper_route.php +++ b/tests/controller/common_helper_route.php @@ -91,7 +91,6 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case $this->filesystem = new \phpbb\filesystem\filesystem(); $this->phpbb_path_helper = new \phpbb\path_helper( $this->symfony_request, - $this->filesystem, $this->request, $phpbb_root_path, $phpEx @@ -106,7 +105,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case $container->setParameter('core.environment', PHPBB_ENVIRONMENT); $cache_path = $phpbb_root_path . 'cache/twig'; $context = new \phpbb\template\context(); - $loader = new \phpbb\template\twig\loader($this->filesystem, ''); + $loader = new \phpbb\template\twig\loader(''); $twig = new \phpbb\template\twig\environment( $this->config, $this->filesystem, @@ -137,7 +136,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case ); $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader( - new \phpbb\routing\file_locator($this->filesystem, dirname(__FILE__) . '/') + new \phpbb\routing\file_locator(dirname(__FILE__) . '/') ); $resources_locator = new \phpbb\routing\resources_locator\default_resources_locator(dirname(__FILE__) . '/', PHPBB_ENVIRONMENT, $this->extension_manager); $this->router = new phpbb_mock_router($container, $resources_locator, $loader, dirname(__FILE__) . '/', 'php'); @@ -185,7 +184,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case public function test_helper_url_no_rewrite($route, $params, $is_amp, $session_id, $expected, $description) { $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '0')); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id), $description); } @@ -229,7 +228,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case public function test_helper_url_with_rewrite($route, $params, $is_amp, $session_id, $expected, $description) { $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '1')); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id), $description); } @@ -273,7 +272,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case public function test_helper_url_absolute($route, $params, $is_amp, $session_id, $expected, $description) { $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '0')); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::ABSOLUTE_URL), $description); } @@ -317,7 +316,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case public function test_helper_url_relative_path($route, $params, $is_amp, $session_id, $expected, $description) { $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '0')); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::RELATIVE_PATH), $description); } @@ -361,7 +360,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case public function test_helper_url_network($route, $params, $is_amp, $session_id, $expected, $description) { $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '0')); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::NETWORK_PATH), $description); } @@ -405,7 +404,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case public function test_helper_url_absolute_with_rewrite($route, $params, $is_amp, $session_id, $expected, $description) { $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '1')); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::ABSOLUTE_URL), $description); } @@ -446,7 +445,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case public function test_helper_url_relative_path_with_rewrite($route, $params, $is_amp, $session_id, $expected, $description) { $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '1')); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::RELATIVE_PATH), $description); } @@ -490,7 +489,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case public function test_helper_url_network_with_rewrite($route, $params, $is_amp, $session_id, $expected, $description) { $this->config = new \phpbb\config\config(['enable_mod_rewrite' => '1']); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::NETWORK_PATH), $description); } @@ -522,7 +521,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case 'server_protocol' => $server_protocol, )); - $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->filesystem, $this->root_path, 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $this->router, $this->symfony_request, $this->request, $this->root_path, 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->symfony_request, $this->request, $this->routing_helper); static::assertEquals($expected, $this->helper->route('controller1', array(), false, false, $type)); } diff --git a/tests/controller/controller_test.php b/tests/controller/controller_test.php index d921d0eade..3bab158ac8 100644 --- a/tests/controller/controller_test.php +++ b/tests/controller/controller_test.php @@ -45,7 +45,7 @@ class phpbb_controller_controller_test extends phpbb_test_case $container->setParameter('core.environment', PHPBB_ENVIRONMENT); $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader( - new \phpbb\routing\file_locator(new \phpbb\filesystem\filesystem(), dirname(__FILE__) . '/') + new \phpbb\routing\file_locator(dirname(__FILE__) . '/') ); $resources_locator = new \phpbb\routing\resources_locator\default_resources_locator(dirname(__FILE__) . '/', PHPBB_ENVIRONMENT, $this->extension_manager); $router = new phpbb_mock_router($container, $resources_locator, $loader, dirname(__FILE__) . '/', 'php'); diff --git a/tests/cron/manager_test.php b/tests/cron/manager_test.php index 610f662b7d..e1699e6486 100644 --- a/tests/cron/manager_test.php +++ b/tests/cron/manager_test.php @@ -97,7 +97,6 @@ class phpbb_cron_manager_test extends \phpbb_test_case $mock_router, new \phpbb\symfony_request($request), $request, - new \phpbb\filesystem\filesystem(), $phpbb_root_path, $phpEx ); diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index f7275a4bbe..10d6b7d2e6 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -67,7 +67,6 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $container, $this->db, $this->config, - new phpbb\filesystem\filesystem(), 'phpbb_ext', dirname(__FILE__) . '/../../phpBB/', 'php', diff --git a/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php b/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php index 8e5ed6c52c..5e5f07c777 100644 --- a/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php +++ b/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php @@ -25,8 +25,7 @@ class extension extends extension_base { protected function load_services(ContainerBuilder $container) { - $filesystem = new \phpbb\filesystem\filesystem(); - $loader = new YamlFileLoader($container, new FileLocator($filesystem->realpath($this->ext_path))); + $loader = new YamlFileLoader($container, new FileLocator(\phpbb\storage\helper::realpath($this->ext_path))); $loader->load('environment.yml'); } } diff --git a/tests/email/email_parsing_test.php b/tests/email/email_parsing_test.php index 8fdfe3035e..245c2c3a58 100644 --- a/tests/email/email_parsing_test.php +++ b/tests/email/email_parsing_test.php @@ -39,7 +39,6 @@ class phpbb_email_parsing_test extends phpbb_test_case $filesystem = new \phpbb\filesystem\filesystem(); $phpbb_path_helper = new \phpbb\path_helper( $symfony_request, - $filesystem, $request, $phpbb_root_path, $phpEx @@ -80,7 +79,7 @@ class phpbb_email_parsing_test extends phpbb_test_case $phpbb_path_helper, $cache_path, null, - new \phpbb\template\twig\loader($filesystem, ''), + new \phpbb\template\twig\loader(''), new \phpbb\event\dispatcher($phpbb_container), array( 'cache' => false, diff --git a/tests/extension/finder_test.php b/tests/extension/finder_test.php index 71de2c2fc5..84bd1ef610 100644 --- a/tests/extension/finder_test.php +++ b/tests/extension/finder_test.php @@ -243,7 +243,7 @@ class phpbb_extension_finder_test extends phpbb_test_case public function test_get_classes_create_cache() { $cache = new phpbb_mock_cache; - $finder = new \phpbb\finder(new \phpbb\filesystem\filesystem(), dirname(__FILE__) . '/', $cache, 'php', '_custom_cache_name'); + $finder = new \phpbb\finder(dirname(__FILE__) . '/', $cache, 'php', '_custom_cache_name'); $finder->set_extensions(array_keys($this->extension_manager->all_enabled())); $files = $finder->suffix('_class.php')->get_files(); @@ -283,7 +283,6 @@ class phpbb_extension_finder_test extends phpbb_test_case ); $finder = new \phpbb\finder( - new \phpbb\filesystem\filesystem(), dirname(__FILE__) . '/', new phpbb_mock_cache(array( '_ext_finder' => array( diff --git a/tests/extension/manager_test.php b/tests/extension/manager_test.php index f619d4c19d..a1765775d8 100644 --- a/tests/extension/manager_test.php +++ b/tests/extension/manager_test.php @@ -176,7 +176,6 @@ class phpbb_extension_manager_test extends phpbb_database_test_case $container, $db, $config, - new \phpbb\filesystem\filesystem(), 'phpbb_ext', dirname(__FILE__) . '/', $php_ext, diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index bb272b4df3..5887fd974d 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -52,13 +52,12 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $container = new phpbb_mock_container_builder(); $cache_path = $this->phpbb_root_path . 'cache/twig'; $context = new \phpbb\template\context(); - $loader = new \phpbb\template\twig\loader(new \phpbb\filesystem\filesystem(), ''); + $loader = new \phpbb\template\twig\loader(''); $filesystem = new \phpbb\filesystem\filesystem(); $phpbb_path_helper = new \phpbb\path_helper( new \phpbb\symfony_request( new phpbb_mock_request() ), - $filesystem, $this->createMock('\phpbb\request\request'), $this->phpbb_root_path, $this->phpEx @@ -97,7 +96,6 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $container, $this->db, $this->config, - new \phpbb\filesystem\filesystem(), 'phpbb_ext', $this->phpbb_root_path, $this->phpEx, diff --git a/tests/filesystem/clean_path_test.php b/tests/filesystem/clean_path_test.php deleted file mode 100644 index d2dec424b4..0000000000 --- a/tests/filesystem/clean_path_test.php +++ /dev/null @@ -1,54 +0,0 @@ - -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ - -class phpbb_filesystem_clean_path_test extends phpbb_test_case -{ - protected $filesystem; - - public function setUp() - { - parent::setUp(); - $this->filesystem = new \phpbb\filesystem\filesystem(); - } - - public function clean_path_data() - { - return array( - array('foo', 'foo'), - array('foo/bar', 'foo/bar'), - array('foo/bar/', 'foo/bar/'), - array('foo/./bar', 'foo/bar'), - array('foo/./././bar', 'foo/bar'), - array('foo/bar/.', 'foo/bar'), - array('./foo/bar', './foo/bar'), - array('../foo/bar', '../foo/bar'), - array('./../foo/bar', './../foo/bar'), - array('././../foo/bar', './../foo/bar'), - array('one/two/three', 'one/two/three'), - array('one/two/../three', 'one/three'), - array('one/../two/three', 'two/three'), - array('one/two/..', 'one'), - array('one/two/../', 'one/'), - array('one/two/../three/../four', 'one/four'), - array('one/two/three/../../four', 'one/four'), - ); - } - - /** - * @dataProvider clean_path_data - */ - public function test_clean_path($input, $expected) - { - $this->assertEquals($expected, $this->filesystem->clean_path($input)); - } -} diff --git a/tests/filesystem/is_absolute_test.php b/tests/filesystem/is_absolute_test.php deleted file mode 100644 index 7a50989b74..0000000000 --- a/tests/filesystem/is_absolute_test.php +++ /dev/null @@ -1,68 +0,0 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -class phpbb_filesystem_is_absolute_test extends phpbb_test_case -{ - /** @var \phpbb\filesystem\filesystem_interface */ - protected $filesystem; - - public function setUp() - { - parent::setUp(); - - $this->filesystem = new \phpbb\filesystem\filesystem(); - } - - static public function is_absolute_data() - { - return array( - // Empty - array('', false), - - // Absolute unix style - array('/etc/phpbb', true), - // Unix does not support \ so that is not an absolute path - array('\etc\phpbb', false), - - // Absolute windows style - array('c:\windows', true), - array('C:\Windows', true), - array('c:/windows', true), - array('C:/Windows', true), - - // Executable - array('etc/phpbb', false), - array('explorer.exe', false), - - // Relative subdir - array('Windows\System32', false), - array('Windows\System32\explorer.exe', false), - array('Windows/System32', false), - array('Windows/System32/explorer.exe', false), - - // Relative updir - array('..\Windows\System32', false), - array('..\Windows\System32\explorer.exe', false), - array('../Windows/System32', false), - array('../Windows/System32/explorer.exe', false), - ); - } - - /** - * @dataProvider is_absolute_data - */ - public function test_is_absolute($path, $expected) - { - $this->assertEquals($expected, $this->filesystem->is_absolute_path($path)); - } -} diff --git a/tests/filesystem/realpath_test.php b/tests/filesystem/realpath_test.php deleted file mode 100644 index d994935f94..0000000000 --- a/tests/filesystem/realpath_test.php +++ /dev/null @@ -1,90 +0,0 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -class phpbb_filesystem_realpath_test extends phpbb_test_case -{ - static protected $filesystem_own_realpath; - - /** @var \phpbb\filesystem\filesystem_interface */ - protected $filesystem; - - static public function setUpBeforeClass() - { - parent::setUpBeforeClass(); - - $reflection_class = new ReflectionClass('\phpbb\filesystem\filesystem'); - self::$filesystem_own_realpath = $reflection_class->getMethod('phpbb_own_realpath'); - self::$filesystem_own_realpath->setAccessible(true); - } - - public function setUp() - { - parent::setUp(); - - $this->filesystem = new \phpbb\filesystem\filesystem(); - } - - public function realpath_resolve_absolute_without_symlinks_data() - { - return array( - // Constant data - array(__DIR__, __DIR__), - array(__DIR__ . '/../filesystem/../filesystem', __DIR__), - array(__DIR__ . '/././', __DIR__), - array(__DIR__ . '/non_existent', false), - - array(__FILE__, __FILE__), - array(__FILE__ . '../', false), - ); - } - - public function realpath_resolve_relative_without_symlinks_data() - { - if (!function_exists('getcwd')) - { - return array(); - } - - $filesystem = new \phpbb\filesystem\filesystem(); - $relative_path = $filesystem->make_path_relative(__DIR__, getcwd()); - - return array( - array($relative_path, __DIR__), - array($relative_path . '../filesystem/../filesystem', __DIR__), - array($relative_path . '././', __DIR__), - - array($relative_path . 'realpath_test.php', __FILE__), - ); - } - - /** - * @dataProvider realpath_resolve_absolute_without_symlinks_data - */ - public function test_realpath_absolute_without_links($path, $expected) - { - $this->assertEquals($expected, self::$filesystem_own_realpath->invoke($this->filesystem, $path)); - } - - /** - * @dataProvider realpath_resolve_relative_without_symlinks_data - */ - public function test_realpath_relative_without_links($path, $expected) - { - if (!function_exists('getcwd')) - { - $this->markTestSkipped('phpbb_own_realpath() cannot be tested with relative paths: getcwd is not available.'); - } - - $this->assertEquals($expected, self::$filesystem_own_realpath->invoke($this->filesystem, $path)); - } -} diff --git a/tests/functions/build_url_test.php b/tests/functions/build_url_test.php index a528cec5ae..95b20050d7 100644 --- a/tests/functions/build_url_test.php +++ b/tests/functions/build_url_test.php @@ -27,7 +27,6 @@ class phpbb_build_url_test extends phpbb_test_case new \phpbb\symfony_request( new phpbb_mock_request() ), - new \phpbb\filesystem\filesystem(), $this->createMock('\phpbb\request\request'), $phpbb_root_path, 'php' diff --git a/tests/pagination/pagination_test.php b/tests/pagination/pagination_test.php index 3b1f1a45c3..a9c78059a5 100644 --- a/tests/pagination/pagination_test.php +++ b/tests/pagination/pagination_test.php @@ -39,12 +39,11 @@ class phpbb_pagination_pagination_test extends phpbb_template_template_test_case $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '1')); - $filesystem = new \phpbb\filesystem\filesystem(); $manager = new phpbb_mock_extension_manager(dirname(__FILE__) . '/', array()); $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader( - new \phpbb\routing\file_locator($filesystem, dirname(__FILE__) . '/') + new \phpbb\routing\file_locator(dirname(__FILE__) . '/') ); $resources_locator = new \phpbb\routing\resources_locator\default_resources_locator(dirname(__FILE__) . '/', PHPBB_ENVIRONMENT, $manager); $router = new phpbb_mock_router(new phpbb_mock_container_builder(), $resources_locator, $loader, dirname(__FILE__) . '/', 'php'); @@ -58,7 +57,7 @@ class phpbb_pagination_pagination_test extends phpbb_template_template_test_case $request ); - $this->routing_helper = new \phpbb\routing\helper($this->config, $router, $symfony_request, $request, $filesystem, '', 'php'); + $this->routing_helper = new \phpbb\routing\helper($this->config, $router, $symfony_request, $request, '', 'php'); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $symfony_request, $request, $this->routing_helper); $this->pagination = new \phpbb\pagination($this->template, $this->user, $this->helper, $phpbb_dispatcher); } diff --git a/tests/path_helper/path_helper_test.php b/tests/path_helper/path_helper_test.php index 585d62847e..29dee8cced 100644 --- a/tests/path_helper/path_helper_test.php +++ b/tests/path_helper/path_helper_test.php @@ -21,14 +21,12 @@ class phpbb_path_helper_test extends phpbb_test_case { parent::setUp(); - $filesystem = new \phpbb\filesystem\filesystem(); - $this->set_phpbb_root_path($filesystem); + $this->set_phpbb_root_path(); $this->path_helper = new \phpbb\path_helper( new \phpbb\symfony_request( new phpbb_mock_request() ), - new \phpbb\filesystem\filesystem(), $this->createMock('\phpbb\request\request'), $this->phpbb_root_path, 'php' @@ -43,9 +41,9 @@ class phpbb_path_helper_test extends phpbb_test_case * any time we wish to use it in one of these functions (and * also in general for everything else) */ - public function set_phpbb_root_path($filesystem) + public function set_phpbb_root_path() { - $this->phpbb_root_path = $filesystem->clean_path(dirname(__FILE__) . '/../../phpBB/'); + $this->phpbb_root_path = \phpbb\storage\helper::clean_path(dirname(__FILE__) . '/../../phpBB/'); } public function test_get_web_root_path() @@ -56,8 +54,7 @@ class phpbb_path_helper_test extends phpbb_test_case public function basic_update_web_root_path_data() { - $filesystem = new \phpbb\filesystem\filesystem(); - $this->set_phpbb_root_path($filesystem); + $this->set_phpbb_root_path(); return array( array( @@ -75,7 +72,7 @@ class phpbb_path_helper_test extends phpbb_test_case ), array( $this->phpbb_root_path . $this->phpbb_root_path . 'test.php', - $filesystem->clean_path($this->phpbb_root_path . $this->phpbb_root_path . 'test.php'), + \phpbb\storage\helper::clean_path($this->phpbb_root_path . $this->phpbb_root_path . 'test.php'), ), ); } @@ -90,7 +87,7 @@ class phpbb_path_helper_test extends phpbb_test_case public function update_web_root_path_data() { - $this->set_phpbb_root_path(new \phpbb\filesystem\filesystem()); + $this->set_phpbb_root_path(); return array( array( @@ -195,7 +192,6 @@ class phpbb_path_helper_test extends phpbb_test_case $path_helper = new \phpbb\path_helper( $symfony_request, - new \phpbb\filesystem\filesystem(), $this->createMock('\phpbb\request\request'), $this->phpbb_root_path, 'php' diff --git a/tests/security/redirect_test.php b/tests/security/redirect_test.php index b912256cd5..b71f487e4f 100644 --- a/tests/security/redirect_test.php +++ b/tests/security/redirect_test.php @@ -66,7 +66,6 @@ class phpbb_security_redirect_test extends phpbb_security_test_base new \phpbb\symfony_request( new phpbb_mock_request() ), - new \phpbb\filesystem\filesystem(), $this->createMock('\phpbb\request\request'), $this->phpbb_root_path, 'php' diff --git a/tests/template/includephp_test.php b/tests/template/includephp_test.php index 722e10e42d..7d34252673 100644 --- a/tests/template/includephp_test.php +++ b/tests/template/includephp_test.php @@ -39,9 +39,8 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case { global $phpbb_root_path; - $filesystem = new \phpbb\filesystem\filesystem(); $path_to_php = str_replace('\\', '/', dirname(__FILE__)) . '/templates/_dummy_include.php.inc'; - $this->assertTrue($filesystem->is_absolute_path($path_to_php)); + $this->assertTrue(\phpbb\storage\helper::is_absolute_path($path_to_php)); $template_text = "Path is absolute.\n"; $cache_dir = $phpbb_root_path . 'cache/'; diff --git a/tests/template/template_allfolder_test.php b/tests/template/template_allfolder_test.php index e7e4f21321..17a57fba35 100644 --- a/tests/template/template_allfolder_test.php +++ b/tests/template/template_allfolder_test.php @@ -39,7 +39,6 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case new \phpbb\symfony_request( new phpbb_mock_request() ), - $filesystem, $this->createMock('\phpbb\request\request'), $phpbb_root_path, $phpEx @@ -59,7 +58,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case $container = new phpbb_mock_container_builder(); $cache_path = $phpbb_root_path . 'cache/twig'; $context = new \phpbb\template\context(); - $loader = new \phpbb\template\twig\loader(new \phpbb\filesystem\filesystem(), ''); + $loader = new \phpbb\template\twig\loader(''); $twig = new \phpbb\template\twig\environment( $config, $filesystem, diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index 6bd4180e09..024bdc57ce 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -144,7 +144,6 @@ Zeta test event in all', new \phpbb\symfony_request( new phpbb_mock_request() ), - new \phpbb\filesystem\filesystem(), $this->createMock('\phpbb\request\request'), $phpbb_root_path, $phpEx @@ -153,7 +152,7 @@ Zeta test event in all', $container = new phpbb_mock_container_builder(); $cache_path = $phpbb_root_path . 'cache/twig'; $context = new \phpbb\template\context(); - $loader = new \phpbb\template\twig\loader(new \phpbb\filesystem\filesystem(), ''); + $loader = new \phpbb\template\twig\loader(''); $twig = new \phpbb\template\twig\environment( $config, $filesystem, diff --git a/tests/template/template_includecss_test.php b/tests/template/template_includecss_test.php index a0992e084e..66d4f5cbde 100644 --- a/tests/template/template_includecss_test.php +++ b/tests/template/template_includecss_test.php @@ -34,7 +34,6 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te new \phpbb\symfony_request( new phpbb_mock_request() ), - $filesystem, $this->createMock('\phpbb\request\request'), $phpbb_root_path, $phpEx @@ -45,7 +44,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $container = new phpbb_mock_container_builder(); $cache_path = $phpbb_root_path . 'cache/twig'; $context = new \phpbb\template\context(); - $loader = new \phpbb\template\twig\loader(new \phpbb\filesystem\filesystem(), ''); + $loader = new \phpbb\template\twig\loader(''); $twig = new \phpbb\template\twig\environment( $config, $filesystem, diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index 78fa59c956..ae17688190 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -86,8 +86,12 @@ class phpbb_template_template_test_case extends phpbb_test_case new \phpbb\symfony_request( new phpbb_mock_request() ), +<<<<<<< HEAD $filesystem, $this->createMock('\phpbb\request\request'), +======= + $this->getMock('\phpbb\request\request'), +>>>>>>> [ticket/15253] Use storage helper methods instead of filesystem methods $phpbb_root_path, $phpEx ); @@ -97,7 +101,7 @@ class phpbb_template_template_test_case extends phpbb_test_case $container = new phpbb_mock_container_builder(); $cache_path = $phpbb_root_path . 'cache/twig'; $context = new \phpbb\template\context(); - $loader = new \phpbb\template\twig\loader(new \phpbb\filesystem\filesystem(), ''); + $loader = new \phpbb\template\twig\loader(''); $twig = new \phpbb\template\twig\environment( $config, $filesystem, diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php index b0c362b8d0..aa71bf6ed6 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -28,7 +28,6 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat new \phpbb\symfony_request( new phpbb_mock_request() ), - $filesystem, $this->createMock('\phpbb\request\request'), $phpbb_root_path, $phpEx @@ -40,7 +39,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat $container = new phpbb_mock_container_builder(); $cache_path = $phpbb_root_path . 'cache/twig'; $context = new \phpbb\template\context(); - $loader = new \phpbb\template\twig\loader(new \phpbb\filesystem\filesystem(), ''); + $loader = new \phpbb\template\twig\loader(''); $twig = new \phpbb\template\twig\environment( $config, $filesystem, diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index df7c669865..be0cec2b1f 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -58,7 +58,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test $setup_extensions = static::setup_extensions(); - $finder = new \phpbb\finder(new \phpbb\filesystem\filesystem(), $phpbb_root_path, null, $phpEx); + $finder = new \phpbb\finder($phpbb_root_path, null, $phpEx); $finder->core_path('phpbb/db/migration/data/'); if (!empty($setup_extensions)) { diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index b58e9c752b..9dc47a38ba 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -366,7 +366,7 @@ class phpbb_database_test_connection_manager { global $phpbb_root_path, $phpEx, $table_prefix; - $finder = new \phpbb\finder(new \phpbb\filesystem\filesystem(), $phpbb_root_path, null, $phpEx); + $finder = new \phpbb\finder($phpbb_root_path, null, $phpEx); $classes = $finder->core_path('phpbb/db/migration/data/') ->get_classes(); diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index cdf9174b02..59d9890b0d 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -259,7 +259,6 @@ class phpbb_functional_test_case extends phpbb_test_case $container, $db, $config, - new phpbb\filesystem\filesystem(), self::$config['table_prefix'] . 'ext', dirname(__FILE__) . '/', $phpEx, diff --git a/tests/test_framework/phpbb_session_test_case.php b/tests/test_framework/phpbb_session_test_case.php index 1e71ca6e69..207659b3bd 100644 --- a/tests/test_framework/phpbb_session_test_case.php +++ b/tests/test_framework/phpbb_session_test_case.php @@ -29,14 +29,12 @@ abstract class phpbb_session_test_case extends phpbb_database_test_case { parent::setUp(); - global $symfony_request, $phpbb_filesystem, $phpbb_path_helper, $request, $phpbb_root_path, $phpEx; + global $symfony_request, $phpbb_path_helper, $request, $phpbb_root_path, $phpEx; $symfony_request = new \phpbb\symfony_request( new phpbb_mock_request() ); - $phpbb_filesystem = new \phpbb\filesystem\filesystem(); $phpbb_path_helper = new \phpbb\path_helper( $symfony_request, - $phpbb_filesystem, $this->createMock('\phpbb\request\request'), $phpbb_root_path, $phpEx diff --git a/tests/test_framework/phpbb_ui_test_case.php b/tests/test_framework/phpbb_ui_test_case.php index d38d14f45c..f8a35c75a5 100644 --- a/tests/test_framework/phpbb_ui_test_case.php +++ b/tests/test_framework/phpbb_ui_test_case.php @@ -382,7 +382,6 @@ class phpbb_ui_test_case extends phpbb_test_case $container, $db, $config, - new phpbb\filesystem(), $user, self::$config['table_prefix'] . 'ext', dirname(__FILE__) . '/', From 8dbbf74550af7973385d2599cb339e4ce108176a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 10:17:57 +0200 Subject: [PATCH 07/26] [ticket/15253] Add experimental annotation PHPBB3-15253 --- phpBB/phpbb/storage/adapter/local.php | 3 +++ phpBB/phpbb/storage/storage.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index 4673dbca6e..dab6fbc34f 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -16,6 +16,9 @@ namespace phpbb\storage\adapter; use phpbb\storage\exception\exception; use phpbb\filesystem\exception\filesystem_exception; +/** + * @internal Experimental + */ class local implements adapter_interface { /** diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 1f5e751bbb..7f0ea4a6dc 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -13,6 +13,9 @@ namespace phpbb\storage; +/** + * @internal Experimental + */ class storage { protected $adapter; From 603a8c51da9ef19d2cab9c8299f3e48392f81c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 10:27:32 +0200 Subject: [PATCH 08/26] [ticket/15253] Move storage helper to filesystem PHPBB3-15253 --- phpBB/common.php | 2 +- phpBB/includes/functions.php | 2 +- phpBB/includes/functions_compatibility.php | 22 +- phpBB/phpbb/composer/installer.php | 2 +- phpBB/phpbb/di/container_builder.php | 2 +- phpBB/phpbb/di/extension/core.php | 2 +- phpBB/phpbb/extension/di/extension_base.php | 2 +- phpBB/phpbb/filesystem/filesystem.php | 8 +- phpBB/phpbb/finder.php | 2 +- phpBB/phpbb/install/helper/database.php | 2 +- phpBB/phpbb/path_helper.php | 10 +- phpBB/phpbb/routing/file_locator.php | 2 +- phpBB/phpbb/routing/helper.php | 2 +- phpBB/phpbb/session.php | 6 +- phpBB/phpbb/storage/helper.php | 365 ------------------ phpBB/phpbb/template/asset.php | 4 +- phpBB/phpbb/template/twig/loader.php | 6 +- phpBB/phpbb/viewonline_helper.php | 2 +- .../ext/vendor/enabled_4/di/extension.php | 2 +- tests/path_helper/path_helper_test.php | 4 +- tests/storage/helper_clean_path_test.php | 52 --- tests/storage/helper_is_absolute_test.php | 64 --- tests/storage/helper_realpath_test.php | 83 ---- tests/template/includephp_test.php | 2 +- 24 files changed, 43 insertions(+), 607 deletions(-) delete mode 100644 phpBB/phpbb/storage/helper.php delete mode 100644 tests/storage/helper_clean_path_test.php delete mode 100644 tests/storage/helper_is_absolute_test.php delete mode 100644 tests/storage/helper_realpath_test.php diff --git a/phpBB/common.php b/phpBB/common.php index 46098ff44c..7ace4cf12a 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -65,7 +65,7 @@ if (!defined('PHPBB_INSTALLED')) // Eliminate . and .. from the path require($phpbb_root_path . 'phpbb/filesystem.' . $phpEx); - $script_path = \phpbb\storage\helper::clean_path($script_path); + $script_path = \phpbb\filesystem\helper::clean_path($script_path); $url = (($secure) ? 'https://' : 'http://') . $server_name; diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index adfe3c01a2..5bf86276c8 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -3479,7 +3479,7 @@ function phpbb_filter_root_path($errfile) if (empty($root_path)) { - $root_path = \phpbb\storage\helper::realpath(dirname(__FILE__) . '/../'); + $root_path = \phpbb\filesystem\helper::realpath(dirname(__FILE__) . '/../'); } return str_replace(array($root_path, '\\'), array('[ROOT]', '/'), $errfile); diff --git a/phpBB/includes/functions_compatibility.php b/phpBB/includes/functions_compatibility.php index 9bf052f479..e1ff4004ff 100644 --- a/phpBB/includes/functions_compatibility.php +++ b/phpBB/includes/functions_compatibility.php @@ -96,12 +96,12 @@ function phpbb_check_hash($password, $hash) */ function phpbb_clean_path($path) { - if (!class_exists('\phpbb\storage\helper')) + if (!class_exists('\phpbb\filesystem\helper')) { - require($phpbb_root_path . 'phpbb/storage/helper.' . $phpEx); + require($phpbb_root_path . 'phpbb/filesystem/helper.' . $phpEx); } - return \phpbb\storage\helper::clean_path($path); + return \phpbb\filesystem\helper::clean_path($path); } /** @@ -439,31 +439,31 @@ function phpbb_is_writable($file) * @param string $path Path to check absoluteness of * @return boolean * - * @deprecated 3.2.0-dev use \phpbb\storage\helper::is_absolute_path() instead + * @deprecated 3.2.0-dev use \phpbb\filesystem\helper::is_absolute_path() instead */ function phpbb_is_absolute($path) { - if (!class_exists('\phpbb\storage\helper')) + if (!class_exists('\phpbb\filesystem\helper')) { - require($phpbb_root_path . 'phpbb/storage/helper.' . $phpEx); + require($phpbb_root_path . 'phpbb/filesystem/helper.' . $phpEx); } - return \phpbb\storage\helper::is_absolute_path($path); + return \phpbb\filesystem\helper::is_absolute_path($path); } /** * A wrapper for realpath * - * @deprecated 3.2.0-dev use \phpbb\storage\helper::realpath() instead + * @deprecated 3.2.0-dev use \phpbb\filesystem\helper::realpath() instead */ function phpbb_realpath($path) { - if (!class_exists('\phpbb\storage\helper')) + if (!class_exists('\phpbb\filesystem\helper')) { - require($phpbb_root_path . 'phpbb/storage/helper.' . $phpEx); + require($phpbb_root_path . 'phpbb/filesystem/helper.' . $phpEx); } - return \phpbb\storage\helper::realpath($path); + return \phpbb\filesystem\helper::realpath($path); } /** diff --git a/phpBB/phpbb/composer/installer.php b/phpBB/phpbb/composer/installer.php index 6bab30eb9e..e96a39c4b8 100644 --- a/phpBB/phpbb/composer/installer.php +++ b/phpBB/phpbb/composer/installer.php @@ -108,7 +108,7 @@ class installer $this->root_path = $root_path; $this->request = $request; - putenv('COMPOSER_HOME=' . \phpbb\storage\helper::realpath($root_path) . '/store/composer'); + putenv('COMPOSER_HOME=' . \phpbb\filesystem\helper::realpath($root_path) . '/store/composer'); } /** diff --git a/phpBB/phpbb/di/container_builder.php b/phpBB/phpbb/di/container_builder.php index 47a858d605..307f03f072 100644 --- a/phpBB/phpbb/di/container_builder.php +++ b/phpBB/phpbb/di/container_builder.php @@ -179,7 +179,7 @@ class container_builder $this->register_ext_compiler_pass(); } - $loader = new YamlFileLoader($this->container, new FileLocator(\phpbb\storage\helper::realpath($this->get_config_path()))); + $loader = new YamlFileLoader($this->container, new FileLocator(\phpbb\filesystem\helper::realpath($this->get_config_path()))); $loader->load($this->container->getParameter('core.environment') . '/config.yml'); $this->inject_custom_parameters(); diff --git a/phpBB/phpbb/di/extension/core.php b/phpBB/phpbb/di/extension/core.php index 84148eced3..e3c9967b7b 100644 --- a/phpBB/phpbb/di/extension/core.php +++ b/phpBB/phpbb/di/extension/core.php @@ -53,7 +53,7 @@ class core extends Extension */ public function load(array $configs, ContainerBuilder $container) { - $loader = new YamlFileLoader($container, new FileLocator(\phpbb\storage\helper::realpath($this->config_path))); + $loader = new YamlFileLoader($container, new FileLocator(\phpbb\filesystem\helper::realpath($this->config_path))); $loader->load($container->getParameter('core.environment') . '/container/environment.yml'); $config = $this->getConfiguration($configs, $container); diff --git a/phpBB/phpbb/extension/di/extension_base.php b/phpBB/phpbb/extension/di/extension_base.php index e6fd5688cc..a8f8ca0776 100644 --- a/phpBB/phpbb/extension/di/extension_base.php +++ b/phpBB/phpbb/extension/di/extension_base.php @@ -94,7 +94,7 @@ class extension_base extends Extension if ($services_directory && $services_file) { - $loader = new YamlFileLoader($container, new FileLocator(\phpbb\storage\helper::realpath($services_directory))); + $loader = new YamlFileLoader($container, new FileLocator(\phpbb\filesystem\helper::realpath($services_directory))); $loader->load($services_file); } } diff --git a/phpBB/phpbb/filesystem/filesystem.php b/phpBB/phpbb/filesystem/filesystem.php index 30a16bc756..01e0bb2cb2 100644 --- a/phpBB/phpbb/filesystem/filesystem.php +++ b/phpBB/phpbb/filesystem/filesystem.php @@ -162,7 +162,7 @@ class filesystem implements filesystem_interface */ public function clean_path($path) { - return \phpbb\storage\helper::clean_path($path); + return \phpbb\filesystem\helper::clean_path($path); } /** @@ -208,7 +208,7 @@ class filesystem implements filesystem_interface */ public function is_absolute_path($path) { - return \phpbb\storage\helper::is_absolute_path($path); + return \phpbb\filesystem\helper::is_absolute_path($path); } /** @@ -286,7 +286,7 @@ class filesystem implements filesystem_interface */ public function make_path_relative($end_path, $start_path) { - return \phpbb\storage\helper::make_path_relative($end_path, $start_path); + return \phpbb\filesystem\helper::make_path_relative($end_path, $start_path); } /** @@ -759,6 +759,6 @@ class filesystem implements filesystem_interface */ protected function resolve_path($path, $prefix = '', $absolute = false, $return_array = false) { - return \phpbb\storage\helper::resolve_path($path, $prefix, $absolute, $return_array); + return \phpbb\filesystem\helper::resolve_path($path, $prefix, $absolute, $return_array); } } diff --git a/phpBB/phpbb/finder.php b/phpBB/phpbb/finder.php index a1d44c6587..57ae046dc3 100644 --- a/phpBB/phpbb/finder.php +++ b/phpBB/phpbb/finder.php @@ -241,7 +241,7 @@ class finder */ protected function sanitise_directory($directory) { - $directory = \phpbb\storage\helper::clean_path($directory); + $directory = \phpbb\filesystem\helper::clean_path($directory); $dir_len = strlen($directory); if ($dir_len > 1 && $directory[$dir_len - 1] === '/') diff --git a/phpBB/phpbb/install/helper/database.php b/phpBB/phpbb/install/helper/database.php index 3f70536a82..6dbea3735c 100644 --- a/phpBB/phpbb/install/helper/database.php +++ b/phpBB/phpbb/install/helper/database.php @@ -329,7 +329,7 @@ class database // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea if ($dbms_info['SCHEMA'] === 'sqlite' - && stripos(\phpbb\storage\helper::realpath($dbhost), \phpbb\storage\helper::realpath($this->phpbb_root_path) === 0)) + && stripos(\phpbb\filesystem\helper::realpath($dbhost), \phpbb\filesystem\helper::realpath($this->phpbb_root_path) === 0)) { $errors[] = array( 'title' =>'INST_ERR_DB_FORUM_PATH', diff --git a/phpBB/phpbb/path_helper.php b/phpBB/phpbb/path_helper.php index 16dd9aa038..8abb62d511 100644 --- a/phpBB/phpbb/path_helper.php +++ b/phpBB/phpbb/path_helper.php @@ -112,7 +112,7 @@ class path_helper $path = substr($path, 8); } - return \phpbb\storage\helper::clean_path($web_root_path . $path); + return \phpbb\filesystem\helper::clean_path($web_root_path . $path); } return $path; @@ -158,7 +158,7 @@ class path_helper // We do not need to escape $path_info, $request_uri and $script_name because we can not find their content in the result. // Path info (e.g. /foo/bar) - $path_info = \phpbb\storage\helper::clean_path($this->symfony_request->getPathInfo()); + $path_info = \phpbb\filesystem\helper::clean_path($this->symfony_request->getPathInfo()); // Full request URI (e.g. phpBB/app.php/foo/bar) $request_uri = $this->symfony_request->getRequestUri(); @@ -173,7 +173,7 @@ class path_helper */ if ($path_info === '/' && preg_match('/app\.' . $this->php_ext . '\/$/', $request_uri)) { - return $this->web_root_path = \phpbb\storage\helper::clean_path('./../' . $this->phpbb_root_path); + return $this->web_root_path = \phpbb\filesystem\helper::clean_path('./../' . $this->phpbb_root_path); } /* @@ -230,7 +230,7 @@ class path_helper } // Prepend ../ to the phpbb_root_path as many times as / exists in path_info - $this->web_root_path = \phpbb\storage\helper::clean_path( + $this->web_root_path = \phpbb\filesystem\helper::clean_path( './' . str_repeat('../', $corrections) . $this->phpbb_root_path ); return $this->web_root_path; @@ -321,7 +321,7 @@ class path_helper // Add length of URL delimiter to position $path = substr($url, $delimiter_position + 3); - return $scheme . \phpbb\storage\helper::clean_path($path); + return $scheme . \phpbb\filesystem\helper::clean_path($path); } /** diff --git a/phpBB/phpbb/routing/file_locator.php b/phpBB/phpbb/routing/file_locator.php index bdf3a2145c..cf4520ddae 100644 --- a/phpBB/phpbb/routing/file_locator.php +++ b/phpBB/phpbb/routing/file_locator.php @@ -24,7 +24,7 @@ class file_locator extends FileLocator foreach ($paths as $path) { - $absolute_paths[] = \phpbb\storage\helper::realpath($path); + $absolute_paths[] = \phpbb\filesystem\helper::realpath($path); } parent::__construct($absolute_paths); diff --git a/phpBB/phpbb/routing/helper.php b/phpBB/phpbb/routing/helper.php index ea8835d362..9c4ead98d2 100644 --- a/phpBB/phpbb/routing/helper.php +++ b/phpBB/phpbb/routing/helper.php @@ -133,7 +133,7 @@ class helper } } - $base_url = $this->request->escape(\phpbb\storage\helper::clean_path($base_url), true); + $base_url = $this->request->escape(\phpbb\filesystem\helper::clean_path($base_url), true); $context->setBaseUrl($base_url); diff --git a/phpBB/phpbb/session.php b/phpBB/phpbb/session.php index 4446e3defd..3a07fa8384 100644 --- a/phpBB/phpbb/session.php +++ b/phpBB/phpbb/session.php @@ -85,15 +85,15 @@ class session $page_name = (substr($script_name, -1, 1) == '/') ? '' : basename($script_name); $page_name = urlencode(htmlspecialchars($page_name)); - $symfony_request_path = \phpbb\storage\helper::clean_path($symfony_request->getPathInfo()); + $symfony_request_path = \phpbb\filesystem\helper::clean_path($symfony_request->getPathInfo()); if ($symfony_request_path !== '/') { $page_name .= str_replace('%2F', '/', urlencode($symfony_request_path)); } // current directory within the phpBB root (for example: adm) - $root_dirs = explode('/', str_replace('\\', '/', \phpbb\storage\helper::realpath($root_path))); - $page_dirs = explode('/', str_replace('\\', '/', \phpbb\storage\helper::realpath('./'))); + $root_dirs = explode('/', str_replace('\\', '/', \phpbb\filesystem\helper::realpath($root_path))); + $page_dirs = explode('/', str_replace('\\', '/', \phpbb\filesystem\helper::realpath('./'))); $intersection = array_intersect_assoc($root_dirs, $page_dirs); $root_dirs = array_diff_assoc($root_dirs, $intersection); diff --git a/phpBB/phpbb/storage/helper.php b/phpBB/phpbb/storage/helper.php deleted file mode 100644 index 4c8dfe466b..0000000000 --- a/phpBB/phpbb/storage/helper.php +++ /dev/null @@ -1,365 +0,0 @@ - - * @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\storage; - -class helper -{ - /** - * Eliminates useless . and .. components from specified path. - * - * @param string $path Path to clean - * - * @return string Cleaned path - */ - public static 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] !== '.' && $filtered[sizeof($filtered) - 1] !== '..') - { - array_pop($filtered); - } - else - { - $filtered[] = $part; - } - } - $path = implode('/', $filtered); - return $path; - } - - /** - * Checks if a path is absolute or not - * - * @param string $path Path to check - * - * @return bool true if the path is absolute, false otherwise - */ - public static function is_absolute_path($path) - { - return (isset($path[0]) && $path[0] === '/' || preg_match('#^[a-z]:[/\\\]#i', $path)) ? true : false; - } - - /** - * Try to resolve real path when PHP's realpath failes to do so - * - * @param string $path - * @return bool|string - */ - protected static function phpbb_own_realpath($path) - { - - // Replace all directory separators with '/' - $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); - - $is_absolute_path = false; - $path_prefix = ''; - - if (self::is_absolute_path($path)) - { - $is_absolute_path = true; - } - else - { - if (function_exists('getcwd')) - { - $working_directory = str_replace(DIRECTORY_SEPARATOR, '/', getcwd()); - } - - // - // From this point on we really just guessing - // If chdir were called we screwed - // - else if (function_exists('debug_backtrace')) - { - $call_stack = debug_backtrace(0); - $working_directory = str_replace(DIRECTORY_SEPARATOR, '/', dirname($call_stack[sizeof($call_stack) - 1]['file'])); - } - else - { - // - // Assuming that the working directory is phpBB root - // we could use this as a fallback, when phpBB will use controllers - // everywhere this will be a safe assumption - // - //$dir_parts = explode(DIRECTORY_SEPARATOR, __DIR__); - //$namespace_parts = explode('\\', trim(__NAMESPACE__, '\\')); - - //$namespace_part_count = sizeof($namespace_parts); - - // Check if we still loading from root - //if (array_slice($dir_parts, -$namespace_part_count) === $namespace_parts) - //{ - // $working_directory = implode('/', array_slice($dir_parts, 0, -$namespace_part_count)); - //} - //else - //{ - // $working_directory = false; - //} - - $working_directory = false; - } - - if ($working_directory !== false) - { - $is_absolute_path = true; - $path = $working_directory . '/' . $path; - } - } - - if ($is_absolute_path) - { - if (defined('PHP_WINDOWS_VERSION_MAJOR')) - { - $path_prefix = $path[0] . ':'; - $path = substr($path, 2); - } - else - { - $path_prefix = ''; - } - } - - $resolved_path = self::resolve_path($path, $path_prefix, $is_absolute_path); - if ($resolved_path === false) - { - return false; - } - - if (!@file_exists($resolved_path) || (!@is_dir($resolved_path . '/') && !is_file($resolved_path))) - { - return false; - } - - // Return OS specific directory separators - $resolved = str_replace('/', DIRECTORY_SEPARATOR, $resolved_path); - - // Check for DIRECTORY_SEPARATOR at the end (and remove it!) - if (substr($resolved, -1) === DIRECTORY_SEPARATOR) - { - return substr($resolved, 0, -1); - } - - return $resolved; - } - - /** - * A wrapper for PHP's realpath - * - * Try to resolve realpath when PHP's realpath is not available, or - * known to be buggy. - * - * @param string $path Path to resolve - * - * @return string Resolved path - */ - public static function realpath($path) - { - if (!function_exists('realpath')) - { - return self::phpbb_own_realpath($path); - } - - $realpath = realpath($path); - - // Strangely there are provider not disabling realpath but returning strange values. :o - // We at least try to cope with them. - if ((!self::is_absolute_path($path) && $realpath === $path) || $realpath === false) - { - return self::phpbb_own_realpath($path); - } - - // Check for DIRECTORY_SEPARATOR at the end (and remove it!) - if (substr($realpath, -1) === DIRECTORY_SEPARATOR) - { - $realpath = substr($realpath, 0, -1); - } - - return $realpath; - } - - /** - * Given an existing path, convert it to a path relative to a given starting path - * - * @param string $end_path Absolute path of target - * @param string $start_path Absolute path where traversal begins - * - * @return string Path of target relative to starting path - */ - public static function make_path_relative($end_path, $start_path) - { - $symfony_filesystem = new \Symfony\Component\Filesystem\Filesystem(); - return $symfony_filesystem->makePathRelative($end_path, $start_path); - } - - /** - * Try to resolve symlinks in path - * - * @param string $path The path to resolve - * @param string $prefix The path prefix (on windows the drive letter) - * @param bool $absolute Whether or not the path is absolute - * @param bool $return_array Whether or not to return path parts - * - * @return string|array|bool returns the resolved path or an array of parts of the path if $return_array is true - * or false if path cannot be resolved - */ - protected static function resolve_path($path, $prefix = '', $absolute = false, $return_array = false) - { - if ($return_array) - { - $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); - } - - trim ($path, '/'); - $path_parts = explode('/', $path); - $resolved = array(); - $resolved_path = $prefix; - $file_found = false; - - foreach ($path_parts as $path_part) - { - if ($file_found) - { - return false; - } - - if (empty($path_part) || ($path_part === '.' && ($absolute || !empty($resolved)))) - { - continue; - } - else if ($absolute && $path_part === '..') - { - if (empty($resolved)) - { - // No directories above root - return false; - } - - array_pop($resolved); - $resolved_path = false; - } - else if ($path_part === '..' && !empty($resolved) && !in_array($resolved[sizeof($resolved) - 1], array('.', '..'))) - { - array_pop($resolved); - $resolved_path = false; - } - else - { - if ($resolved_path === false) - { - if (empty($resolved)) - { - $resolved_path = ($absolute) ? $prefix . '/' . $path_part : $path_part; - } - else - { - $tmp_array = $resolved; - if ($absolute) - { - array_unshift($tmp_array, $prefix); - } - - $resolved_path = implode('/', $tmp_array); - } - } - - $current_path = $resolved_path . '/' . $path_part; - - // Resolve symlinks - if (is_link($current_path)) - { - if (!function_exists('readlink')) - { - return false; - } - - $link = readlink($current_path); - - // Is link has an absolute path in it? - if (self::is_absolute_path($link)) - { - if (defined('PHP_WINDOWS_VERSION_MAJOR')) - { - $prefix = $link[0] . ':'; - $link = substr($link, 2); - } - else - { - $prefix = ''; - } - - $resolved = self::resolve_path($link, $prefix, true, true); - $absolute = true; - } - else - { - $resolved = self::resolve_path($resolved_path . '/' . $link, $prefix, $absolute, true); - } - - if (!$resolved) - { - return false; - } - - $resolved_path = false; - } - else if (is_dir($current_path . '/')) - { - $resolved[] = $path_part; - $resolved_path = $current_path; - } - else if (is_file($current_path)) - { - $resolved[] = $path_part; - $resolved_path = $current_path; - $file_found = true; - } - else - { - return false; - } - } - } - - // If at the end of the path there were a .. or . - // we need to build the path again. - // Only doing this when a string is expected in return - if ($resolved_path === false && $return_array === false) - { - if (empty($resolved)) - { - $resolved_path = ($absolute) ? $prefix . '/' : './'; - } - else - { - $tmp_array = $resolved; - if ($absolute) - { - array_unshift($tmp_array, $prefix); - } - - $resolved_path = implode('/', $tmp_array); - } - } - - return ($return_array) ? $resolved : $resolved_path; - } -} diff --git a/phpBB/phpbb/template/asset.php b/phpBB/phpbb/template/asset.php index b7469c7e06..6acd816db5 100644 --- a/phpBB/phpbb/template/asset.php +++ b/phpBB/phpbb/template/asset.php @@ -153,7 +153,7 @@ class asset 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 = \phpbb\storage\helper::realpath($this->path_helper->get_phpbb_root_path()) . DIRECTORY_SEPARATOR; + $real_root_path = \phpbb\filesystem\helper::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) @@ -163,7 +163,7 @@ class asset else { // Else we make the path relative to the current working directory - $real_root_path = \phpbb\storage\helper::realpath('.') . DIRECTORY_SEPARATOR; + $real_root_path = \phpbb\filesystem\helper::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))); diff --git a/phpBB/phpbb/template/twig/loader.php b/phpBB/phpbb/template/twig/loader.php index 6fa7eda2cc..121c93b170 100644 --- a/phpBB/phpbb/template/twig/loader.php +++ b/phpBB/phpbb/template/twig/loader.php @@ -49,7 +49,7 @@ class loader extends \Twig_Loader_Filesystem */ public function addSafeDirectory($directory) { - $directory = \phpbb\storage\helper::realpath($directory); + $directory = \phpbb\filesystem\helper::realpath($directory); if ($directory !== false) { @@ -89,7 +89,7 @@ class loader extends \Twig_Loader_Filesystem */ public function addPath($path, $namespace = self::MAIN_NAMESPACE) { - return parent::addPath(\phpbb\storage\helper::realpath($path), $namespace); + return parent::addPath(\phpbb\filesystem\helper::realpath($path), $namespace); } /** @@ -129,7 +129,7 @@ class loader extends \Twig_Loader_Filesystem // can now check if we're within a "safe" directory // Find the real path of the directory the file is in - $directory = \phpbb\storage\helper::realpath(dirname($file)); + $directory = \phpbb\filesystem\helper::realpath(dirname($file)); if ($directory === false) { diff --git a/phpBB/phpbb/viewonline_helper.php b/phpBB/phpbb/viewonline_helper.php index e034b20a56..ee23190db8 100644 --- a/phpBB/phpbb/viewonline_helper.php +++ b/phpBB/phpbb/viewonline_helper.php @@ -33,7 +33,7 @@ class viewonline_helper */ public function get_user_page($session_page) { - $session_page = \phpbb\storage\helper::clean_path($session_page); + $session_page = \phpbb\filesystem\helper::clean_path($session_page); if (strpos($session_page, './') === 0) { $session_page = substr($session_page, 2); diff --git a/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php b/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php index 5e5f07c777..cd94902276 100644 --- a/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php +++ b/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php @@ -25,7 +25,7 @@ class extension extends extension_base { protected function load_services(ContainerBuilder $container) { - $loader = new YamlFileLoader($container, new FileLocator(\phpbb\storage\helper::realpath($this->ext_path))); + $loader = new YamlFileLoader($container, new FileLocator(\phpbb\filesystem\helper::realpath($this->ext_path))); $loader->load('environment.yml'); } } diff --git a/tests/path_helper/path_helper_test.php b/tests/path_helper/path_helper_test.php index 29dee8cced..96b8460f04 100644 --- a/tests/path_helper/path_helper_test.php +++ b/tests/path_helper/path_helper_test.php @@ -43,7 +43,7 @@ class phpbb_path_helper_test extends phpbb_test_case */ public function set_phpbb_root_path() { - $this->phpbb_root_path = \phpbb\storage\helper::clean_path(dirname(__FILE__) . '/../../phpBB/'); + $this->phpbb_root_path = \phpbb\filesystem\helper::clean_path(dirname(__FILE__) . '/../../phpBB/'); } public function test_get_web_root_path() @@ -72,7 +72,7 @@ class phpbb_path_helper_test extends phpbb_test_case ), array( $this->phpbb_root_path . $this->phpbb_root_path . 'test.php', - \phpbb\storage\helper::clean_path($this->phpbb_root_path . $this->phpbb_root_path . 'test.php'), + \phpbb\filesystem\helper::clean_path($this->phpbb_root_path . $this->phpbb_root_path . 'test.php'), ), ); } diff --git a/tests/storage/helper_clean_path_test.php b/tests/storage/helper_clean_path_test.php deleted file mode 100644 index ab9fabd0cb..0000000000 --- a/tests/storage/helper_clean_path_test.php +++ /dev/null @@ -1,52 +0,0 @@ - -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ - -class phpbb_storage_helper_clean_path_test extends phpbb_test_case -{ - - public function setUp() - { - parent::setUp(); - } - - public function clean_path_data() - { - return array( - array('foo', 'foo'), - array('foo/bar', 'foo/bar'), - array('foo/bar/', 'foo/bar/'), - array('foo/./bar', 'foo/bar'), - array('foo/./././bar', 'foo/bar'), - array('foo/bar/.', 'foo/bar'), - array('./foo/bar', './foo/bar'), - array('../foo/bar', '../foo/bar'), - array('./../foo/bar', './../foo/bar'), - array('././../foo/bar', './../foo/bar'), - array('one/two/three', 'one/two/three'), - array('one/two/../three', 'one/three'), - array('one/../two/three', 'two/three'), - array('one/two/..', 'one'), - array('one/two/../', 'one/'), - array('one/two/../three/../four', 'one/four'), - array('one/two/three/../../four', 'one/four'), - ); - } - - /** - * @dataProvider clean_path_data - */ - public function test_clean_path($input, $expected) - { - $this->assertEquals($expected, \phpbb\storage\helper::clean_path($input)); - } -} diff --git a/tests/storage/helper_is_absolute_test.php b/tests/storage/helper_is_absolute_test.php deleted file mode 100644 index 05a048cb91..0000000000 --- a/tests/storage/helper_is_absolute_test.php +++ /dev/null @@ -1,64 +0,0 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -class phpbb_storage_helper_is_absolute_test extends phpbb_test_case -{ - - public function setUp() - { - parent::setUp(); - } - - static public function is_absolute_data() - { - return array( - // Empty - array('', false), - - // Absolute unix style - array('/etc/phpbb', true), - // Unix does not support \ so that is not an absolute path - array('\etc\phpbb', false), - - // Absolute windows style - array('c:\windows', true), - array('C:\Windows', true), - array('c:/windows', true), - array('C:/Windows', true), - - // Executable - array('etc/phpbb', false), - array('explorer.exe', false), - - // Relative subdir - array('Windows\System32', false), - array('Windows\System32\explorer.exe', false), - array('Windows/System32', false), - array('Windows/System32/explorer.exe', false), - - // Relative updir - array('..\Windows\System32', false), - array('..\Windows\System32\explorer.exe', false), - array('../Windows/System32', false), - array('../Windows/System32/explorer.exe', false), - ); - } - - /** - * @dataProvider is_absolute_data - */ - public function test_is_absolute($path, $expected) - { - $this->assertEquals($expected, \phpbb\storage\helper::is_absolute_path($path)); - } -} diff --git a/tests/storage/helper_realpath_test.php b/tests/storage/helper_realpath_test.php deleted file mode 100644 index 1cc5e4b1ce..0000000000 --- a/tests/storage/helper_realpath_test.php +++ /dev/null @@ -1,83 +0,0 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -class phpbb_storage_helper_realpath_test extends phpbb_test_case -{ - protected static $storage_helper_phpbb_own_realpath; - - static public function setUpBeforeClass() - { - parent::setUpBeforeClass(); - - self::$storage_helper_phpbb_own_realpath = new ReflectionMethod('\phpbb\storage\helper', 'phpbb_own_realpath'); - self::$storage_helper_phpbb_own_realpath->setAccessible(true); - } - - public function setUp() - { - parent::setUp(); - } - - public function realpath_resolve_absolute_without_symlinks_data() - { - return array( - // Constant data - array(__DIR__, __DIR__), - array(__DIR__ . '/../storage/../storage', __DIR__), - array(__DIR__ . '/././', __DIR__), - array(__DIR__ . '/non_existent', false), - - array(__FILE__, __FILE__), - array(__FILE__ . '../', false), - ); - } - - public function realpath_resolve_relative_without_symlinks_data() - { - if (!function_exists('getcwd')) - { - return array(); - } - - $relative_path = \phpbb\storage\helper::make_path_relative(__DIR__, getcwd()); - - return array( - array($relative_path, __DIR__), - array($relative_path . '../storage/../storage', __DIR__), - array($relative_path . '././', __DIR__), - - array($relative_path . 'helper_realpath_test.php', __FILE__), - ); - } - - /** - * @dataProvider realpath_resolve_absolute_without_symlinks_data - */ - public function test_realpath_absolute_without_links($path, $expected) - { - $this->assertEquals($expected, self::$storage_helper_phpbb_own_realpath->invoke(null, $path)); - } - - /** - * @dataProvider realpath_resolve_relative_without_symlinks_data - */ - public function test_realpath_relative_without_links($path, $expected) - { - if (!function_exists('getcwd')) - { - $this->markTestSkipped('phpbb_own_realpath() cannot be tested with relative paths: getcwd is not available.'); - } - - $this->assertEquals($expected, self::$storage_helper_phpbb_own_realpath->invoke(null, $path)); - } -} diff --git a/tests/template/includephp_test.php b/tests/template/includephp_test.php index 7d34252673..e156be5c23 100644 --- a/tests/template/includephp_test.php +++ b/tests/template/includephp_test.php @@ -40,7 +40,7 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case global $phpbb_root_path; $path_to_php = str_replace('\\', '/', dirname(__FILE__)) . '/templates/_dummy_include.php.inc'; - $this->assertTrue(\phpbb\storage\helper::is_absolute_path($path_to_php)); + $this->assertTrue(\phpbb\filesystem\helper::is_absolute_path($path_to_php)); $template_text = "Path is absolute.\n"; $cache_dir = $phpbb_root_path . 'cache/'; From de288be1b301578910c6ae08306523b01d33999d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 10:27:47 +0200 Subject: [PATCH 09/26] [ticket/15253] Move storage helper to filesystem PHPBB3-15253 --- phpBB/phpbb/filesystem/helper.php | 365 +++++++++++++++++++ tests/filesystem/helper_clean_path_test.php | 52 +++ tests/filesystem/helper_is_absolute_test.php | 64 ++++ tests/filesystem/helper_realpath_test.php | 83 +++++ 4 files changed, 564 insertions(+) create mode 100644 phpBB/phpbb/filesystem/helper.php create mode 100644 tests/filesystem/helper_clean_path_test.php create mode 100644 tests/filesystem/helper_is_absolute_test.php create mode 100644 tests/filesystem/helper_realpath_test.php diff --git a/phpBB/phpbb/filesystem/helper.php b/phpBB/phpbb/filesystem/helper.php new file mode 100644 index 0000000000..2549a49929 --- /dev/null +++ b/phpBB/phpbb/filesystem/helper.php @@ -0,0 +1,365 @@ + + * @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\filesystem; + +class helper +{ + /** + * Eliminates useless . and .. components from specified path. + * + * @param string $path Path to clean + * + * @return string Cleaned path + */ + public static 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] !== '.' && $filtered[sizeof($filtered) - 1] !== '..') + { + array_pop($filtered); + } + else + { + $filtered[] = $part; + } + } + $path = implode('/', $filtered); + return $path; + } + + /** + * Checks if a path is absolute or not + * + * @param string $path Path to check + * + * @return bool true if the path is absolute, false otherwise + */ + public static function is_absolute_path($path) + { + return (isset($path[0]) && $path[0] === '/' || preg_match('#^[a-z]:[/\\\]#i', $path)) ? true : false; + } + + /** + * Try to resolve real path when PHP's realpath failes to do so + * + * @param string $path + * @return bool|string + */ + protected static function phpbb_own_realpath($path) + { + + // Replace all directory separators with '/' + $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); + + $is_absolute_path = false; + $path_prefix = ''; + + if (self::is_absolute_path($path)) + { + $is_absolute_path = true; + } + else + { + if (function_exists('getcwd')) + { + $working_directory = str_replace(DIRECTORY_SEPARATOR, '/', getcwd()); + } + + // + // From this point on we really just guessing + // If chdir were called we screwed + // + else if (function_exists('debug_backtrace')) + { + $call_stack = debug_backtrace(0); + $working_directory = str_replace(DIRECTORY_SEPARATOR, '/', dirname($call_stack[sizeof($call_stack) - 1]['file'])); + } + else + { + // + // Assuming that the working directory is phpBB root + // we could use this as a fallback, when phpBB will use controllers + // everywhere this will be a safe assumption + // + //$dir_parts = explode(DIRECTORY_SEPARATOR, __DIR__); + //$namespace_parts = explode('\\', trim(__NAMESPACE__, '\\')); + + //$namespace_part_count = sizeof($namespace_parts); + + // Check if we still loading from root + //if (array_slice($dir_parts, -$namespace_part_count) === $namespace_parts) + //{ + // $working_directory = implode('/', array_slice($dir_parts, 0, -$namespace_part_count)); + //} + //else + //{ + // $working_directory = false; + //} + + $working_directory = false; + } + + if ($working_directory !== false) + { + $is_absolute_path = true; + $path = $working_directory . '/' . $path; + } + } + + if ($is_absolute_path) + { + if (defined('PHP_WINDOWS_VERSION_MAJOR')) + { + $path_prefix = $path[0] . ':'; + $path = substr($path, 2); + } + else + { + $path_prefix = ''; + } + } + + $resolved_path = self::resolve_path($path, $path_prefix, $is_absolute_path); + if ($resolved_path === false) + { + return false; + } + + if (!@file_exists($resolved_path) || (!@is_dir($resolved_path . '/') && !is_file($resolved_path))) + { + return false; + } + + // Return OS specific directory separators + $resolved = str_replace('/', DIRECTORY_SEPARATOR, $resolved_path); + + // Check for DIRECTORY_SEPARATOR at the end (and remove it!) + if (substr($resolved, -1) === DIRECTORY_SEPARATOR) + { + return substr($resolved, 0, -1); + } + + return $resolved; + } + + /** + * A wrapper for PHP's realpath + * + * Try to resolve realpath when PHP's realpath is not available, or + * known to be buggy. + * + * @param string $path Path to resolve + * + * @return string Resolved path + */ + public static function realpath($path) + { + if (!function_exists('realpath')) + { + return self::phpbb_own_realpath($path); + } + + $realpath = realpath($path); + + // Strangely there are provider not disabling realpath but returning strange values. :o + // We at least try to cope with them. + if ((!self::is_absolute_path($path) && $realpath === $path) || $realpath === false) + { + return self::phpbb_own_realpath($path); + } + + // Check for DIRECTORY_SEPARATOR at the end (and remove it!) + if (substr($realpath, -1) === DIRECTORY_SEPARATOR) + { + $realpath = substr($realpath, 0, -1); + } + + return $realpath; + } + + /** + * Given an existing path, convert it to a path relative to a given starting path + * + * @param string $end_path Absolute path of target + * @param string $start_path Absolute path where traversal begins + * + * @return string Path of target relative to starting path + */ + public static function make_path_relative($end_path, $start_path) + { + $symfony_filesystem = new \Symfony\Component\Filesystem\Filesystem(); + return $symfony_filesystem->makePathRelative($end_path, $start_path); + } + + /** + * Try to resolve symlinks in path + * + * @param string $path The path to resolve + * @param string $prefix The path prefix (on windows the drive letter) + * @param bool $absolute Whether or not the path is absolute + * @param bool $return_array Whether or not to return path parts + * + * @return string|array|bool returns the resolved path or an array of parts of the path if $return_array is true + * or false if path cannot be resolved + */ + protected static function resolve_path($path, $prefix = '', $absolute = false, $return_array = false) + { + if ($return_array) + { + $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); + } + + trim ($path, '/'); + $path_parts = explode('/', $path); + $resolved = array(); + $resolved_path = $prefix; + $file_found = false; + + foreach ($path_parts as $path_part) + { + if ($file_found) + { + return false; + } + + if (empty($path_part) || ($path_part === '.' && ($absolute || !empty($resolved)))) + { + continue; + } + else if ($absolute && $path_part === '..') + { + if (empty($resolved)) + { + // No directories above root + return false; + } + + array_pop($resolved); + $resolved_path = false; + } + else if ($path_part === '..' && !empty($resolved) && !in_array($resolved[sizeof($resolved) - 1], array('.', '..'))) + { + array_pop($resolved); + $resolved_path = false; + } + else + { + if ($resolved_path === false) + { + if (empty($resolved)) + { + $resolved_path = ($absolute) ? $prefix . '/' . $path_part : $path_part; + } + else + { + $tmp_array = $resolved; + if ($absolute) + { + array_unshift($tmp_array, $prefix); + } + + $resolved_path = implode('/', $tmp_array); + } + } + + $current_path = $resolved_path . '/' . $path_part; + + // Resolve symlinks + if (is_link($current_path)) + { + if (!function_exists('readlink')) + { + return false; + } + + $link = readlink($current_path); + + // Is link has an absolute path in it? + if (self::is_absolute_path($link)) + { + if (defined('PHP_WINDOWS_VERSION_MAJOR')) + { + $prefix = $link[0] . ':'; + $link = substr($link, 2); + } + else + { + $prefix = ''; + } + + $resolved = self::resolve_path($link, $prefix, true, true); + $absolute = true; + } + else + { + $resolved = self::resolve_path($resolved_path . '/' . $link, $prefix, $absolute, true); + } + + if (!$resolved) + { + return false; + } + + $resolved_path = false; + } + else if (is_dir($current_path . '/')) + { + $resolved[] = $path_part; + $resolved_path = $current_path; + } + else if (is_file($current_path)) + { + $resolved[] = $path_part; + $resolved_path = $current_path; + $file_found = true; + } + else + { + return false; + } + } + } + + // If at the end of the path there were a .. or . + // we need to build the path again. + // Only doing this when a string is expected in return + if ($resolved_path === false && $return_array === false) + { + if (empty($resolved)) + { + $resolved_path = ($absolute) ? $prefix . '/' : './'; + } + else + { + $tmp_array = $resolved; + if ($absolute) + { + array_unshift($tmp_array, $prefix); + } + + $resolved_path = implode('/', $tmp_array); + } + } + + return ($return_array) ? $resolved : $resolved_path; + } +} diff --git a/tests/filesystem/helper_clean_path_test.php b/tests/filesystem/helper_clean_path_test.php new file mode 100644 index 0000000000..21a4dfe238 --- /dev/null +++ b/tests/filesystem/helper_clean_path_test.php @@ -0,0 +1,52 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +class phpbb_filesystem_helper_clean_path_test extends phpbb_test_case +{ + + public function setUp() + { + parent::setUp(); + } + + public function clean_path_data() + { + return array( + array('foo', 'foo'), + array('foo/bar', 'foo/bar'), + array('foo/bar/', 'foo/bar/'), + array('foo/./bar', 'foo/bar'), + array('foo/./././bar', 'foo/bar'), + array('foo/bar/.', 'foo/bar'), + array('./foo/bar', './foo/bar'), + array('../foo/bar', '../foo/bar'), + array('./../foo/bar', './../foo/bar'), + array('././../foo/bar', './../foo/bar'), + array('one/two/three', 'one/two/three'), + array('one/two/../three', 'one/three'), + array('one/../two/three', 'two/three'), + array('one/two/..', 'one'), + array('one/two/../', 'one/'), + array('one/two/../three/../four', 'one/four'), + array('one/two/three/../../four', 'one/four'), + ); + } + + /** + * @dataProvider clean_path_data + */ + public function test_clean_path($input, $expected) + { + $this->assertEquals($expected, \phpbb\filesystem\helper::clean_path($input)); + } +} diff --git a/tests/filesystem/helper_is_absolute_test.php b/tests/filesystem/helper_is_absolute_test.php new file mode 100644 index 0000000000..52b5d9e1b9 --- /dev/null +++ b/tests/filesystem/helper_is_absolute_test.php @@ -0,0 +1,64 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_filesystem_helper_is_absolute_test extends phpbb_test_case +{ + + public function setUp() + { + parent::setUp(); + } + + static public function is_absolute_data() + { + return array( + // Empty + array('', false), + + // Absolute unix style + array('/etc/phpbb', true), + // Unix does not support \ so that is not an absolute path + array('\etc\phpbb', false), + + // Absolute windows style + array('c:\windows', true), + array('C:\Windows', true), + array('c:/windows', true), + array('C:/Windows', true), + + // Executable + array('etc/phpbb', false), + array('explorer.exe', false), + + // Relative subdir + array('Windows\System32', false), + array('Windows\System32\explorer.exe', false), + array('Windows/System32', false), + array('Windows/System32/explorer.exe', false), + + // Relative updir + array('..\Windows\System32', false), + array('..\Windows\System32\explorer.exe', false), + array('../Windows/System32', false), + array('../Windows/System32/explorer.exe', false), + ); + } + + /** + * @dataProvider is_absolute_data + */ + public function test_is_absolute($path, $expected) + { + $this->assertEquals($expected, \phpbb\filesystem\helper::is_absolute_path($path)); + } +} diff --git a/tests/filesystem/helper_realpath_test.php b/tests/filesystem/helper_realpath_test.php new file mode 100644 index 0000000000..754d7631a4 --- /dev/null +++ b/tests/filesystem/helper_realpath_test.php @@ -0,0 +1,83 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_filesystem_helper_realpath_test extends phpbb_test_case +{ + protected static $filesystem_helper_phpbb_own_realpath; + + static public function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + self::$filesystem_helper_phpbb_own_realpath = new ReflectionMethod('\phpbb\filesystem\helper', 'phpbb_own_realpath'); + self::$filesystem_helper_phpbb_own_realpath->setAccessible(true); + } + + public function setUp() + { + parent::setUp(); + } + + public function realpath_resolve_absolute_without_symlinks_data() + { + return array( + // Constant data + array(__DIR__, __DIR__), + array(__DIR__ . '/../filesystem/../filesystem', __DIR__), + array(__DIR__ . '/././', __DIR__), + array(__DIR__ . '/non_existent', false), + + array(__FILE__, __FILE__), + array(__FILE__ . '../', false), + ); + } + + public function realpath_resolve_relative_without_symlinks_data() + { + if (!function_exists('getcwd')) + { + return array(); + } + + $relative_path = \phpbb\filesystem\helper::make_path_relative(__DIR__, getcwd()); + + return array( + array($relative_path, __DIR__), + array($relative_path . '../filesystem/../filesystem', __DIR__), + array($relative_path . '././', __DIR__), + + array($relative_path . 'helper_realpath_test.php', __FILE__), + ); + } + + /** + * @dataProvider realpath_resolve_absolute_without_symlinks_data + */ + public function test_realpath_absolute_without_links($path, $expected) + { + $this->assertEquals($expected, self::$filesystem_helper_phpbb_own_realpath->invoke(null, $path)); + } + + /** + * @dataProvider realpath_resolve_relative_without_symlinks_data + */ + public function test_realpath_relative_without_links($path, $expected) + { + if (!function_exists('getcwd')) + { + $this->markTestSkipped('phpbb_own_realpath() cannot be tested with relative paths: getcwd is not available.'); + } + + $this->assertEquals($expected, self::$filesystem_helper_phpbb_own_realpath->invoke(null, $path)); + } +} From 94ee3dc8b0f0962e864ec791d67653b54cdfad75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 10:41:30 +0200 Subject: [PATCH 10/26] [ticket/15253] Fix code style PHPBB3-15253 --- .../exception/filesystem_exception.php | 8 +++--- phpBB/phpbb/filesystem/helper.php | 5 ++-- phpBB/phpbb/storage/adapter/local.php | 8 ++++-- phpBB/phpbb/storage/exception/exception.php | 8 +++--- .../storage/exception/not_implemented.php | 25 +------------------ 5 files changed, 20 insertions(+), 34 deletions(-) diff --git a/phpBB/phpbb/filesystem/exception/filesystem_exception.php b/phpBB/phpbb/filesystem/exception/filesystem_exception.php index d68fa9adf3..ddff8046e5 100644 --- a/phpBB/phpbb/filesystem/exception/filesystem_exception.php +++ b/phpBB/phpbb/filesystem/exception/filesystem_exception.php @@ -13,7 +13,9 @@ namespace phpbb\filesystem\exception; -class filesystem_exception extends \phpbb\exception\runtime_exception +use phpbb\exception\runtime_exception; + +class filesystem_exception extends runtime_exception { /** * Constructor @@ -24,7 +26,7 @@ class filesystem_exception extends \phpbb\exception\runtime_exception * @param \Exception $previous The previous runtime_exception used for the runtime_exception chaining. * @param integer $code The Exception code. */ - public function __construct($message = "", $filename = '', $parameters = array(), \Exception $previous = null, $code = 0) + public function __construct($message = '', $filename = '', $parameters = array(), \Exception $previous = null, $code = 0) { parent::__construct($message, array_merge(array('filename' => $filename), $parameters), $previous, $code); } @@ -36,7 +38,7 @@ class filesystem_exception extends \phpbb\exception\runtime_exception */ public function get_filename() { - $parameters = parent::get_parameters(); + $parameters = $this->get_parameters(); return $parameters['filename']; } } diff --git a/phpBB/phpbb/filesystem/helper.php b/phpBB/phpbb/filesystem/helper.php index 2549a49929..a8a792bebc 100644 --- a/phpBB/phpbb/filesystem/helper.php +++ b/phpBB/phpbb/filesystem/helper.php @@ -13,6 +13,8 @@ namespace phpbb\filesystem; +use Symfony\Component\Filesystem\Filesystem as symfony_filesystem; + class helper { /** @@ -66,7 +68,6 @@ class helper */ protected static function phpbb_own_realpath($path) { - // Replace all directory separators with '/' $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); @@ -206,7 +207,7 @@ class helper */ public static function make_path_relative($end_path, $start_path) { - $symfony_filesystem = new \Symfony\Component\Filesystem\Filesystem(); + $symfony_filesystem = new symfony_filesystem(); return $symfony_filesystem->makePathRelative($end_path, $start_path); } diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index dab6fbc34f..5d0651c466 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -15,6 +15,8 @@ namespace phpbb\storage\adapter; use phpbb\storage\exception\exception; use phpbb\filesystem\exception\filesystem_exception; +use phpbb\config\config; +use phpbb\filesystem\filesystem; /** * @internal Experimental @@ -34,7 +36,7 @@ class local implements adapter_interface /** * Constructor */ - public function __construct(\phpbb\config\config $config, \phpbb\filesystem\filesystem $filesystem, $phpbb_root_path, $path_key) + public function __construct(config $config, filesystem $filesystem, $phpbb_root_path, $path_key) { $this->filesystem = $filesystem; $this->root_path = $phpbb_root_path . $config[$path_key]; @@ -75,7 +77,9 @@ class local implements adapter_interface throw new exception('', $path); // FILE_DONT_EXIST } - if (($content = @file_get_contents($this->root_path . $path)) === false) + $content = @file_get_contents($this->root_path . $path); + + if ($content === false) { throw new exception('', $path); // CANNOT READ FILE } diff --git a/phpBB/phpbb/storage/exception/exception.php b/phpBB/phpbb/storage/exception/exception.php index ee42178961..4eca403cc1 100644 --- a/phpBB/phpbb/storage/exception/exception.php +++ b/phpBB/phpbb/storage/exception/exception.php @@ -13,7 +13,9 @@ namespace phpbb\storage\exception; -class exception extends \phpbb\exception\runtime_exception +use phpbb\exception\runtime_exception; + +class exception extends runtime_exception { /** * Constructor @@ -24,7 +26,7 @@ class exception extends \phpbb\exception\runtime_exception * @param \Exception $previous The previous runtime_exception used for the runtime_exception chaining. * @param integer $code The Exception code. */ - public function __construct($message = "", $filename = '', $parameters = array(), \Exception $previous = null, $code = 0) + public function __construct($message = '', $filename = '', $parameters = [], \Exception $previous = null, $code = 0) { parent::__construct($message, array_merge(array('filename' => $filename), $parameters), $previous, $code); } @@ -36,7 +38,7 @@ class exception extends \phpbb\exception\runtime_exception */ public function get_filename() { - $parameters = parent::get_parameters(); + $parameters = $this->get_parameters(); return $parameters['filename']; } } diff --git a/phpBB/phpbb/storage/exception/not_implemented.php b/phpBB/phpbb/storage/exception/not_implemented.php index 5504cfa76f..df535048e5 100644 --- a/phpBB/phpbb/storage/exception/not_implemented.php +++ b/phpBB/phpbb/storage/exception/not_implemented.php @@ -13,30 +13,7 @@ namespace phpbb\storage\exception; -class not_implemented extends \phpbb\exception\runtime_exception +class not_implemented extends exception { - /** - * Constructor - * - * @param string $message The Exception message to throw (must be a language variable). - * @param string $filename The file that caused the error. - * @param array $parameters The parameters to use with the language var. - * @param \Exception $previous The previous runtime_exception used for the runtime_exception chaining. - * @param integer $code The Exception code. - */ - public function __construct($message = "", $filename = '', $parameters = array(), \Exception $previous = null, $code = 0) - { - parent::__construct($message, array_merge(array('filename' => $filename), $parameters), $previous, $code); - } - /** - * Returns the filename that triggered the error - * - * @return string - */ - public function get_filename() - { - $parameters = parent::get_parameters(); - return $parameters['filename']; - } } From 8bbec30748f740d882df60c47ca66ba7f64be183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 11:15:46 +0200 Subject: [PATCH 11/26] [ticket/15253] Add language strings PHPBB3-15253 --- phpBB/language/en/common.php | 9 +++++++++ phpBB/phpbb/storage/adapter/local.php | 16 ++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php index e2198655c9..1414f0fd1a 100644 --- a/phpBB/language/en/common.php +++ b/phpBB/language/en/common.php @@ -727,6 +727,15 @@ $lang = array_merge($lang, array( 'SUBJECT' => 'Subject', 'SUBMIT' => 'Submit', + 'STORAGE_FILE_EXISTS' => 'File already exists.', + 'STORAGE_FILE_NO_EXIST' => 'File does not exist.', + 'STORAGE_CANNOT_WRITE_FILE' => 'Can not write to file.', + 'STORAGE_CANNOT_READ_FILE' => 'Can not read file.', + 'STORAGE_CANNOT_DELETE' => 'Can not delete file or folder.', + 'STORAGE_CANNOT_RENAME' => 'Can not rename file or folder.', + 'STORAGE_CANNOT_COPY' => 'Can not copy file or folder.', + 'STORAGE_CANNOT_CREATE_DIR' => 'Can not create directory.', + 'TB' => 'TB', 'TERMS_USE' => 'Terms of use', 'TEST_CONNECTION' => 'Test connection', diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index 5d0651c466..68b9ce0c2c 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -54,7 +54,7 @@ class local implements adapter_interface { if ($this->exists($path)) { - throw new exception('', $path); // FILE_EXISTS + throw new exception('STORAGE_FILE_EXISTS', $path); } try @@ -63,7 +63,7 @@ class local implements adapter_interface } catch (filesystem_exception $e) { - throw new exception('', $path, array(), $e); // CANNOT_DUMP_FILE + throw new exception('STORAGE_CANNOT_WRITE_FILE', $path, array(), $e); } } @@ -74,14 +74,14 @@ class local implements adapter_interface { if (!$this->exists($path)) { - throw new exception('', $path); // FILE_DONT_EXIST + throw new exception('STORAGE_FILE_NO_EXIST', $path); } $content = @file_get_contents($this->root_path . $path); if ($content === false) { - throw new exception('', $path); // CANNOT READ FILE + throw new exception('STORAGE_CANNOT_READ_FILE', $path); } return $content; @@ -106,7 +106,7 @@ class local implements adapter_interface } catch (filesystem_exception $e) { - throw new exception('', $path, array(), $e); // CANNOT DELETE + throw new exception('STORAGE_CANNOT_DELETE', $path, array(), $e); } } @@ -121,7 +121,7 @@ class local implements adapter_interface } catch (filesystem_exception $e) { - throw new exception('', $path_orig, array(), $e); // CANNOT_RENAME + throw new exception('STORAGE_CANNOT_RENAME', $path_orig, array(), $e); } } @@ -136,7 +136,7 @@ class local implements adapter_interface } catch (filesystem_exception $e) { - throw new exception('', '', array(), $e); // CANNOT_COPY_FILES + throw new exception('STORAGE_CANNOT_COPY', $path_orig, array(), $e); } } @@ -151,7 +151,7 @@ class local implements adapter_interface } catch (filesystem_exception $e) { - throw new exception('', $path, array(), $e); // CANNOT_CREATE_DIRECTORY + throw new exception('STORAGE_CANNOT_CREATE_DIR', $path, array(), $e); } } From b1755d9daca435934b4a5928211b319abf67a810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 11:47:25 +0200 Subject: [PATCH 12/26] [ticket/15253] Update imports PHPBB3-15253 --- phpBB/phpbb/composer/installer.php | 3 +- phpBB/phpbb/di/container_builder.php | 3 +- phpBB/phpbb/di/extension/core.php | 3 +- phpBB/phpbb/extension/di/extension_base.php | 3 +- phpBB/phpbb/filesystem/filesystem.php | 29 ++++++++++--------- phpBB/phpbb/finder.php | 4 ++- phpBB/phpbb/install/helper/database.php | 3 +- phpBB/phpbb/path_helper.php | 12 ++++---- phpBB/phpbb/routing/file_locator.php | 3 +- phpBB/phpbb/routing/helper.php | 3 +- phpBB/phpbb/session.php | 8 +++-- phpBB/phpbb/template/asset.php | 6 ++-- phpBB/phpbb/template/twig/loader.php | 8 +++-- phpBB/phpbb/viewonline_helper.php | 4 ++- .../ext/vendor/enabled_4/di/extension.php | 3 +- tests/filesystem/helper_clean_path_test.php | 4 ++- tests/filesystem/helper_is_absolute_test.php | 4 ++- tests/filesystem/helper_realpath_test.php | 6 ++-- tests/path_helper/path_helper_test.php | 6 ++-- tests/template/includephp_test.php | 4 ++- 20 files changed, 75 insertions(+), 44 deletions(-) diff --git a/phpBB/phpbb/composer/installer.php b/phpBB/phpbb/composer/installer.php index e96a39c4b8..3a03e29ef5 100644 --- a/phpBB/phpbb/composer/installer.php +++ b/phpBB/phpbb/composer/installer.php @@ -29,6 +29,7 @@ use phpbb\exception\runtime_exception; use phpbb\filesystem\filesystem; use phpbb\request\request; use Seld\JsonLint\ParsingException; +use phpbb\filesystem\helper as filesystem_helper; /** * Class to install packages through composer while freezing core dependencies. @@ -108,7 +109,7 @@ class installer $this->root_path = $root_path; $this->request = $request; - putenv('COMPOSER_HOME=' . \phpbb\filesystem\helper::realpath($root_path) . '/store/composer'); + putenv('COMPOSER_HOME=' . filesystem_helper::realpath($root_path) . '/store/composer'); } /** diff --git a/phpBB/phpbb/di/container_builder.php b/phpBB/phpbb/di/container_builder.php index 307f03f072..d267b11820 100644 --- a/phpBB/phpbb/di/container_builder.php +++ b/phpBB/phpbb/di/container_builder.php @@ -24,6 +24,7 @@ use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Finder\Finder; use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass; +use phpbb\filesystem\helper as filesystem_helper; class container_builder { @@ -179,7 +180,7 @@ class container_builder $this->register_ext_compiler_pass(); } - $loader = new YamlFileLoader($this->container, new FileLocator(\phpbb\filesystem\helper::realpath($this->get_config_path()))); + $loader = new YamlFileLoader($this->container, new FileLocator(filesystem_helper::realpath($this->get_config_path()))); $loader->load($this->container->getParameter('core.environment') . '/config.yml'); $this->inject_custom_parameters(); diff --git a/phpBB/phpbb/di/extension/core.php b/phpBB/phpbb/di/extension/core.php index e3c9967b7b..75a037871f 100644 --- a/phpBB/phpbb/di/extension/core.php +++ b/phpBB/phpbb/di/extension/core.php @@ -19,6 +19,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use phpbb\filesystem\helper as filesystem_helper; /** * Container core extension @@ -53,7 +54,7 @@ class core extends Extension */ public function load(array $configs, ContainerBuilder $container) { - $loader = new YamlFileLoader($container, new FileLocator(\phpbb\filesystem\helper::realpath($this->config_path))); + $loader = new YamlFileLoader($container, new FileLocator(filesystem_helper::realpath($this->config_path))); $loader->load($container->getParameter('core.environment') . '/container/environment.yml'); $config = $this->getConfiguration($configs, $container); diff --git a/phpBB/phpbb/extension/di/extension_base.php b/phpBB/phpbb/extension/di/extension_base.php index a8f8ca0776..97033e7ddb 100644 --- a/phpBB/phpbb/extension/di/extension_base.php +++ b/phpBB/phpbb/extension/di/extension_base.php @@ -18,6 +18,7 @@ use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use phpbb\filesystem\helper as filesystem_helper; /** * Container core extension @@ -94,7 +95,7 @@ class extension_base extends Extension if ($services_directory && $services_file) { - $loader = new YamlFileLoader($container, new FileLocator(\phpbb\filesystem\helper::realpath($services_directory))); + $loader = new YamlFileLoader($container, new FileLocator(filesystem_helper::realpath($services_directory))); $loader->load($services_file); } } diff --git a/phpBB/phpbb/filesystem/filesystem.php b/phpBB/phpbb/filesystem/filesystem.php index 01e0bb2cb2..80474cb0a2 100644 --- a/phpBB/phpbb/filesystem/filesystem.php +++ b/phpBB/phpbb/filesystem/filesystem.php @@ -13,6 +13,7 @@ namespace phpbb\filesystem; +use Symfony\Component\Filesystem\Exception\IOException; use phpbb\filesystem\exception\filesystem_exception; /** @@ -60,7 +61,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->chgrp($files, $group, $recursive); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { // Try to recover filename // By the time this is written that is at the end of the message @@ -146,7 +147,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->chown($files, $user, $recursive); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { // Try to recover filename // By the time this is written that is at the end of the message @@ -162,7 +163,7 @@ class filesystem implements filesystem_interface */ public function clean_path($path) { - return \phpbb\filesystem\helper::clean_path($path); + return helper::clean_path($path); } /** @@ -174,7 +175,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->copy($origin_file, $target_file, $override); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { throw new filesystem_exception('CANNOT_COPY_FILES', '', array(), $e); } @@ -189,7 +190,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->dumpFile($filename, $content); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { throw new filesystem_exception('CANNOT_DUMP_FILE', $filename, array(), $e); } @@ -208,7 +209,7 @@ class filesystem implements filesystem_interface */ public function is_absolute_path($path) { - return \phpbb\filesystem\helper::is_absolute_path($path); + return helper::is_absolute_path($path); } /** @@ -286,7 +287,7 @@ class filesystem implements filesystem_interface */ public function make_path_relative($end_path, $start_path) { - return \phpbb\filesystem\helper::make_path_relative($end_path, $start_path); + return helper::make_path_relative($end_path, $start_path); } /** @@ -298,7 +299,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->mirror($origin_dir, $target_dir, $iterator, $options); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { $msg = $e->getMessage(); $filename = substr($msg, strpos($msg, '"'), strrpos($msg, '"')); @@ -316,7 +317,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->mkdir($dirs, $mode); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { $msg = $e->getMessage(); $filename = substr($msg, strpos($msg, '"'), strrpos($msg, '"')); @@ -499,7 +500,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->remove($files); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { // Try to recover filename // By the time this is written that is at the end of the message @@ -519,7 +520,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->rename($origin, $target, $overwrite); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { $msg = $e->getMessage(); $filename = substr($msg, strpos($msg, '"'), strrpos($msg, '"')); @@ -537,7 +538,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->symlink($origin_dir, $target_dir, $copy_on_windows); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { throw new filesystem_exception('CANNOT_CREATE_SYMLINK', $origin_dir, array(), $e); } @@ -552,7 +553,7 @@ class filesystem implements filesystem_interface { $this->symfony_filesystem->touch($files, $time, $access_time); } - catch (\Symfony\Component\Filesystem\Exception\IOException $e) + catch (IOException $e) { // Try to recover filename // By the time this is written that is at the end of the message @@ -759,6 +760,6 @@ class filesystem implements filesystem_interface */ protected function resolve_path($path, $prefix = '', $absolute = false, $return_array = false) { - return \phpbb\filesystem\helper::resolve_path($path, $prefix, $absolute, $return_array); + return helper::resolve_path($path, $prefix, $absolute, $return_array); } } diff --git a/phpBB/phpbb/finder.php b/phpBB/phpbb/finder.php index 57ae046dc3..9c956c6179 100644 --- a/phpBB/phpbb/finder.php +++ b/phpBB/phpbb/finder.php @@ -13,6 +13,8 @@ namespace phpbb; +use phpbb\filesystem\helper as filesystem_helper; + /** * The finder provides a simple way to locate files in the core and a set of extensions */ @@ -241,7 +243,7 @@ class finder */ protected function sanitise_directory($directory) { - $directory = \phpbb\filesystem\helper::clean_path($directory); + $directory = filesystem_helper::clean_path($directory); $dir_len = strlen($directory); if ($dir_len > 1 && $directory[$dir_len - 1] === '/') diff --git a/phpBB/phpbb/install/helper/database.php b/phpBB/phpbb/install/helper/database.php index 6dbea3735c..7ea02a12a4 100644 --- a/phpBB/phpbb/install/helper/database.php +++ b/phpBB/phpbb/install/helper/database.php @@ -14,6 +14,7 @@ namespace phpbb\install\helper; use phpbb\install\exception\invalid_dbms_exception; +use phpbb\filesystem\helper as filesystem_helper; /** * Database related general functionality for installer @@ -329,7 +330,7 @@ class database // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea if ($dbms_info['SCHEMA'] === 'sqlite' - && stripos(\phpbb\filesystem\helper::realpath($dbhost), \phpbb\filesystem\helper::realpath($this->phpbb_root_path) === 0)) + && stripos(filesystem_helper::realpath($dbhost), filesystem_helper::realpath($this->phpbb_root_path) === 0)) { $errors[] = array( 'title' =>'INST_ERR_DB_FORUM_PATH', diff --git a/phpBB/phpbb/path_helper.php b/phpBB/phpbb/path_helper.php index 8abb62d511..0d5034024d 100644 --- a/phpBB/phpbb/path_helper.php +++ b/phpBB/phpbb/path_helper.php @@ -13,6 +13,8 @@ namespace phpbb; +use phpbb\filesystem\helper as filesystem_helper; + /** * A class with various functions that are related to paths, files and the filesystem */ @@ -112,7 +114,7 @@ class path_helper $path = substr($path, 8); } - return \phpbb\filesystem\helper::clean_path($web_root_path . $path); + return filesystem_helper::clean_path($web_root_path . $path); } return $path; @@ -158,7 +160,7 @@ class path_helper // We do not need to escape $path_info, $request_uri and $script_name because we can not find their content in the result. // Path info (e.g. /foo/bar) - $path_info = \phpbb\filesystem\helper::clean_path($this->symfony_request->getPathInfo()); + $path_info = filesystem_helper::clean_path($this->symfony_request->getPathInfo()); // Full request URI (e.g. phpBB/app.php/foo/bar) $request_uri = $this->symfony_request->getRequestUri(); @@ -173,7 +175,7 @@ class path_helper */ if ($path_info === '/' && preg_match('/app\.' . $this->php_ext . '\/$/', $request_uri)) { - return $this->web_root_path = \phpbb\filesystem\helper::clean_path('./../' . $this->phpbb_root_path); + return $this->web_root_path = filesystem_helper::clean_path('./../' . $this->phpbb_root_path); } /* @@ -230,7 +232,7 @@ class path_helper } // Prepend ../ to the phpbb_root_path as many times as / exists in path_info - $this->web_root_path = \phpbb\filesystem\helper::clean_path( + $this->web_root_path = filesystem_helper::clean_path( './' . str_repeat('../', $corrections) . $this->phpbb_root_path ); return $this->web_root_path; @@ -321,7 +323,7 @@ class path_helper // Add length of URL delimiter to position $path = substr($url, $delimiter_position + 3); - return $scheme . \phpbb\filesystem\helper::clean_path($path); + return $scheme . filesystem_helper::clean_path($path); } /** diff --git a/phpBB/phpbb/routing/file_locator.php b/phpBB/phpbb/routing/file_locator.php index cf4520ddae..bd2c1850bb 100644 --- a/phpBB/phpbb/routing/file_locator.php +++ b/phpBB/phpbb/routing/file_locator.php @@ -14,6 +14,7 @@ namespace phpbb\routing; use Symfony\Component\Config\FileLocator; +use phpbb\filesystem\helper as filesystem_helper; class file_locator extends FileLocator { @@ -24,7 +25,7 @@ class file_locator extends FileLocator foreach ($paths as $path) { - $absolute_paths[] = \phpbb\filesystem\helper::realpath($path); + $absolute_paths[] = filesystem_helper::realpath($path); } parent::__construct($absolute_paths); diff --git a/phpBB/phpbb/routing/helper.php b/phpBB/phpbb/routing/helper.php index 9c4ead98d2..3d38105aa3 100644 --- a/phpBB/phpbb/routing/helper.php +++ b/phpBB/phpbb/routing/helper.php @@ -15,6 +15,7 @@ namespace phpbb\routing; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RequestContext; +use phpbb\filesystem\helper as filesystem_helper; /** * Controller helper class, contains methods that do things for controllers @@ -133,7 +134,7 @@ class helper } } - $base_url = $this->request->escape(\phpbb\filesystem\helper::clean_path($base_url), true); + $base_url = $this->request->escape(filesystem_helper::clean_path($base_url), true); $context->setBaseUrl($base_url); diff --git a/phpBB/phpbb/session.php b/phpBB/phpbb/session.php index 3a07fa8384..bb3dd01629 100644 --- a/phpBB/phpbb/session.php +++ b/phpBB/phpbb/session.php @@ -13,6 +13,8 @@ namespace phpbb; +use phpbb\filesystem\helper as filesystem_helper; + /** * Session class */ @@ -85,15 +87,15 @@ class session $page_name = (substr($script_name, -1, 1) == '/') ? '' : basename($script_name); $page_name = urlencode(htmlspecialchars($page_name)); - $symfony_request_path = \phpbb\filesystem\helper::clean_path($symfony_request->getPathInfo()); + $symfony_request_path = filesystem_helper::clean_path($symfony_request->getPathInfo()); if ($symfony_request_path !== '/') { $page_name .= str_replace('%2F', '/', urlencode($symfony_request_path)); } // current directory within the phpBB root (for example: adm) - $root_dirs = explode('/', str_replace('\\', '/', \phpbb\filesystem\helper::realpath($root_path))); - $page_dirs = explode('/', str_replace('\\', '/', \phpbb\filesystem\helper::realpath('./'))); + $root_dirs = explode('/', str_replace('\\', '/', filesystem_helper::realpath($root_path))); + $page_dirs = explode('/', str_replace('\\', '/', filesystem_helper::realpath('./'))); $intersection = array_intersect_assoc($root_dirs, $page_dirs); $root_dirs = array_diff_assoc($root_dirs, $intersection); diff --git a/phpBB/phpbb/template/asset.php b/phpBB/phpbb/template/asset.php index 6acd816db5..fb70fbb24e 100644 --- a/phpBB/phpbb/template/asset.php +++ b/phpBB/phpbb/template/asset.php @@ -13,6 +13,8 @@ namespace phpbb\template; +use phpbb\filesystem\helper as filesystem_helper; + class asset { protected $components = array(); @@ -153,7 +155,7 @@ class asset 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 = \phpbb\filesystem\helper::realpath($this->path_helper->get_phpbb_root_path()) . DIRECTORY_SEPARATOR; + $real_root_path = filesystem_helper::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) @@ -163,7 +165,7 @@ class asset else { // Else we make the path relative to the current working directory - $real_root_path = \phpbb\filesystem\helper::realpath('.') . DIRECTORY_SEPARATOR; + $real_root_path = filesystem_helper::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))); diff --git a/phpBB/phpbb/template/twig/loader.php b/phpBB/phpbb/template/twig/loader.php index 121c93b170..607dfcd078 100644 --- a/phpBB/phpbb/template/twig/loader.php +++ b/phpBB/phpbb/template/twig/loader.php @@ -13,6 +13,8 @@ namespace phpbb\template\twig; +use phpbb\filesystem\helper as filesystem_helper; + /** * Twig Template loader */ @@ -49,7 +51,7 @@ class loader extends \Twig_Loader_Filesystem */ public function addSafeDirectory($directory) { - $directory = \phpbb\filesystem\helper::realpath($directory); + $directory = filesystem_helper::realpath($directory); if ($directory !== false) { @@ -89,7 +91,7 @@ class loader extends \Twig_Loader_Filesystem */ public function addPath($path, $namespace = self::MAIN_NAMESPACE) { - return parent::addPath(\phpbb\filesystem\helper::realpath($path), $namespace); + return parent::addPath(filesystem_helper::realpath($path), $namespace); } /** @@ -129,7 +131,7 @@ class loader extends \Twig_Loader_Filesystem // can now check if we're within a "safe" directory // Find the real path of the directory the file is in - $directory = \phpbb\filesystem\helper::realpath(dirname($file)); + $directory = filesystem_helper::realpath(dirname($file)); if ($directory === false) { diff --git a/phpBB/phpbb/viewonline_helper.php b/phpBB/phpbb/viewonline_helper.php index ee23190db8..8df1f484d8 100644 --- a/phpBB/phpbb/viewonline_helper.php +++ b/phpBB/phpbb/viewonline_helper.php @@ -13,6 +13,8 @@ namespace phpbb; +use phpbb\filesystem\helper as filesystem_helper; + /** * Class to handle viewonline related tasks */ @@ -33,7 +35,7 @@ class viewonline_helper */ public function get_user_page($session_page) { - $session_page = \phpbb\filesystem\helper::clean_path($session_page); + $session_page = filesystem_helper::clean_path($session_page); if (strpos($session_page, './') === 0) { $session_page = substr($session_page, 2); diff --git a/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php b/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php index cd94902276..2164077d8e 100644 --- a/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php +++ b/tests/di/fixtures/ext/vendor/enabled_4/di/extension.php @@ -17,6 +17,7 @@ use phpbb\extension\di\extension_base; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use phpbb\filesystem\helper as filesystem_helper; /** * Container core extension @@ -25,7 +26,7 @@ class extension extends extension_base { protected function load_services(ContainerBuilder $container) { - $loader = new YamlFileLoader($container, new FileLocator(\phpbb\filesystem\helper::realpath($this->ext_path))); + $loader = new YamlFileLoader($container, new FileLocator(filesystem_helper::realpath($this->ext_path))); $loader->load('environment.yml'); } } diff --git a/tests/filesystem/helper_clean_path_test.php b/tests/filesystem/helper_clean_path_test.php index 21a4dfe238..6b723cbc50 100644 --- a/tests/filesystem/helper_clean_path_test.php +++ b/tests/filesystem/helper_clean_path_test.php @@ -11,6 +11,8 @@ * */ +use phpbb\filesystem\helper as filesystem_helper; + class phpbb_filesystem_helper_clean_path_test extends phpbb_test_case { @@ -47,6 +49,6 @@ class phpbb_filesystem_helper_clean_path_test extends phpbb_test_case */ public function test_clean_path($input, $expected) { - $this->assertEquals($expected, \phpbb\filesystem\helper::clean_path($input)); + $this->assertEquals($expected, filesystem_helper::clean_path($input)); } } diff --git a/tests/filesystem/helper_is_absolute_test.php b/tests/filesystem/helper_is_absolute_test.php index 52b5d9e1b9..6105f48ca0 100644 --- a/tests/filesystem/helper_is_absolute_test.php +++ b/tests/filesystem/helper_is_absolute_test.php @@ -11,6 +11,8 @@ * */ + use phpbb\filesystem\helper as filesystem_helper; + class phpbb_filesystem_helper_is_absolute_test extends phpbb_test_case { @@ -59,6 +61,6 @@ class phpbb_filesystem_helper_is_absolute_test extends phpbb_test_case */ public function test_is_absolute($path, $expected) { - $this->assertEquals($expected, \phpbb\filesystem\helper::is_absolute_path($path)); + $this->assertEquals($expected, filesystem_helper::is_absolute_path($path)); } } diff --git a/tests/filesystem/helper_realpath_test.php b/tests/filesystem/helper_realpath_test.php index 754d7631a4..7dde12e82f 100644 --- a/tests/filesystem/helper_realpath_test.php +++ b/tests/filesystem/helper_realpath_test.php @@ -11,6 +11,8 @@ * */ + use phpbb\filesystem\helper as filesystem_helper; + class phpbb_filesystem_helper_realpath_test extends phpbb_test_case { protected static $filesystem_helper_phpbb_own_realpath; @@ -19,7 +21,7 @@ class phpbb_filesystem_helper_realpath_test extends phpbb_test_case { parent::setUpBeforeClass(); - self::$filesystem_helper_phpbb_own_realpath = new ReflectionMethod('\phpbb\filesystem\helper', 'phpbb_own_realpath'); + self::$filesystem_helper_phpbb_own_realpath = new ReflectionMethod('filesystem_helper', 'phpbb_own_realpath'); self::$filesystem_helper_phpbb_own_realpath->setAccessible(true); } @@ -49,7 +51,7 @@ class phpbb_filesystem_helper_realpath_test extends phpbb_test_case return array(); } - $relative_path = \phpbb\filesystem\helper::make_path_relative(__DIR__, getcwd()); + $relative_path = filesystem_helper::make_path_relative(__DIR__, getcwd()); return array( array($relative_path, __DIR__), diff --git a/tests/path_helper/path_helper_test.php b/tests/path_helper/path_helper_test.php index 96b8460f04..f22606843c 100644 --- a/tests/path_helper/path_helper_test.php +++ b/tests/path_helper/path_helper_test.php @@ -11,6 +11,8 @@ * */ +use phpbb\filesystem\helper as filesystem_helper; + class phpbb_path_helper_test extends phpbb_test_case { /** @var \phpbb\path_helper */ @@ -43,7 +45,7 @@ class phpbb_path_helper_test extends phpbb_test_case */ public function set_phpbb_root_path() { - $this->phpbb_root_path = \phpbb\filesystem\helper::clean_path(dirname(__FILE__) . '/../../phpBB/'); + $this->phpbb_root_path = filesystem_helper::clean_path(dirname(__FILE__) . '/../../phpBB/'); } public function test_get_web_root_path() @@ -72,7 +74,7 @@ class phpbb_path_helper_test extends phpbb_test_case ), array( $this->phpbb_root_path . $this->phpbb_root_path . 'test.php', - \phpbb\filesystem\helper::clean_path($this->phpbb_root_path . $this->phpbb_root_path . 'test.php'), + filesystem_helper::clean_path($this->phpbb_root_path . $this->phpbb_root_path . 'test.php'), ), ); } diff --git a/tests/template/includephp_test.php b/tests/template/includephp_test.php index e156be5c23..baf2eed11b 100644 --- a/tests/template/includephp_test.php +++ b/tests/template/includephp_test.php @@ -11,6 +11,8 @@ * */ +use phpbb\filesystem\helper as filesystem_helper; + require_once dirname(__FILE__) . '/template_test_case.php'; class phpbb_template_includephp_test extends phpbb_template_template_test_case @@ -40,7 +42,7 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case global $phpbb_root_path; $path_to_php = str_replace('\\', '/', dirname(__FILE__)) . '/templates/_dummy_include.php.inc'; - $this->assertTrue(\phpbb\filesystem\helper::is_absolute_path($path_to_php)); + $this->assertTrue(filesystem_helper::is_absolute_path($path_to_php)); $template_text = "Path is absolute.\n"; $cache_dir = $phpbb_root_path . 'cache/'; From 45334eadb6363add197c54483f1205baef461710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 12:16:39 +0200 Subject: [PATCH 13/26] [ticket/15253] Update tests PHPBB3-15253 --- tests/filesystem/helper_clean_path_test.php | 36 +++++++------- tests/filesystem/helper_is_absolute_test.php | 52 ++++++++++---------- tests/filesystem/helper_realpath_test.php | 30 +++++------ tests/storage/adapter/local_test.php | 28 +++++++---- 4 files changed, 73 insertions(+), 73 deletions(-) diff --git a/tests/filesystem/helper_clean_path_test.php b/tests/filesystem/helper_clean_path_test.php index 6b723cbc50..fd9ed605c8 100644 --- a/tests/filesystem/helper_clean_path_test.php +++ b/tests/filesystem/helper_clean_path_test.php @@ -23,25 +23,23 @@ class phpbb_filesystem_helper_clean_path_test extends phpbb_test_case public function clean_path_data() { - return array( - array('foo', 'foo'), - array('foo/bar', 'foo/bar'), - array('foo/bar/', 'foo/bar/'), - array('foo/./bar', 'foo/bar'), - array('foo/./././bar', 'foo/bar'), - array('foo/bar/.', 'foo/bar'), - array('./foo/bar', './foo/bar'), - array('../foo/bar', '../foo/bar'), - array('./../foo/bar', './../foo/bar'), - array('././../foo/bar', './../foo/bar'), - array('one/two/three', 'one/two/three'), - array('one/two/../three', 'one/three'), - array('one/../two/three', 'two/three'), - array('one/two/..', 'one'), - array('one/two/../', 'one/'), - array('one/two/../three/../four', 'one/four'), - array('one/two/three/../../four', 'one/four'), - ); + yield ['foo', 'foo']; + yield ['foo/bar', 'foo/bar']; + yield ['foo/bar/', 'foo/bar/']; + yield ['foo/./bar', 'foo/bar']; + yield ['foo/./././bar', 'foo/bar']; + yield ['foo/bar/.', 'foo/bar']; + yield ['./foo/bar', './foo/bar']; + yield ['../foo/bar', '../foo/bar']; + yield ['./../foo/bar', './../foo/bar']; + yield ['././../foo/bar', './../foo/bar']; + yield ['one/two/three', 'one/two/three']; + yield ['one/two/../three', 'one/three']; + yield ['one/../two/three', 'two/three']; + yield ['one/two/..', 'one']; + yield ['one/two/../', 'one/']; + yield ['one/two/../three/../four', 'one/four']; + yield ['one/two/three/../../four', 'one/four']; } /** diff --git a/tests/filesystem/helper_is_absolute_test.php b/tests/filesystem/helper_is_absolute_test.php index 6105f48ca0..e7562bfaee 100644 --- a/tests/filesystem/helper_is_absolute_test.php +++ b/tests/filesystem/helper_is_absolute_test.php @@ -11,7 +11,7 @@ * */ - use phpbb\filesystem\helper as filesystem_helper; +use phpbb\filesystem\helper as filesystem_helper; class phpbb_filesystem_helper_is_absolute_test extends phpbb_test_case { @@ -23,37 +23,35 @@ class phpbb_filesystem_helper_is_absolute_test extends phpbb_test_case static public function is_absolute_data() { - return array( - // Empty - array('', false), + // Empty + yield ['', false]; - // Absolute unix style - array('/etc/phpbb', true), - // Unix does not support \ so that is not an absolute path - array('\etc\phpbb', false), + // Absolute unix style + yield ['/etc/phpbb', true]; + // Unix does not support \ so that is not an absolute path + yield ['\etc\phpbb', false]; - // Absolute windows style - array('c:\windows', true), - array('C:\Windows', true), - array('c:/windows', true), - array('C:/Windows', true), + // Absolute windows style + yield ['c:\windows', true]; + yield ['C:\Windows', true]; + yield ['c:/windows', true]; + yield ['C:/Windows', true]; - // Executable - array('etc/phpbb', false), - array('explorer.exe', false), + // Executable + yield ['etc/phpbb', false]; + yield ['explorer.exe', false]; - // Relative subdir - array('Windows\System32', false), - array('Windows\System32\explorer.exe', false), - array('Windows/System32', false), - array('Windows/System32/explorer.exe', false), + // Relative subdir + yield ['Windows\System32', false]; + yield ['Windows\System32\explorer.exe', false]; + yield ['Windows/System32', false]; + yield ['Windows/System32/explorer.exe', false]; - // Relative updir - array('..\Windows\System32', false), - array('..\Windows\System32\explorer.exe', false), - array('../Windows/System32', false), - array('../Windows/System32/explorer.exe', false), - ); + // Relative updir + yield ['..\Windows\System32', false]; + yield ['..\Windows\System32\explorer.exe', false]; + yield ['../Windows/System32', false]; + yield ['../Windows/System32/explorer.exe', false]; } /** diff --git a/tests/filesystem/helper_realpath_test.php b/tests/filesystem/helper_realpath_test.php index 7dde12e82f..9f2d5513df 100644 --- a/tests/filesystem/helper_realpath_test.php +++ b/tests/filesystem/helper_realpath_test.php @@ -11,7 +11,7 @@ * */ - use phpbb\filesystem\helper as filesystem_helper; +use phpbb\filesystem\helper as filesystem_helper; class phpbb_filesystem_helper_realpath_test extends phpbb_test_case { @@ -21,7 +21,7 @@ class phpbb_filesystem_helper_realpath_test extends phpbb_test_case { parent::setUpBeforeClass(); - self::$filesystem_helper_phpbb_own_realpath = new ReflectionMethod('filesystem_helper', 'phpbb_own_realpath'); + self::$filesystem_helper_phpbb_own_realpath = new ReflectionMethod('\phpbb\filesystem\helper', 'phpbb_own_realpath'); self::$filesystem_helper_phpbb_own_realpath->setAccessible(true); } @@ -32,16 +32,14 @@ class phpbb_filesystem_helper_realpath_test extends phpbb_test_case public function realpath_resolve_absolute_without_symlinks_data() { - return array( - // Constant data - array(__DIR__, __DIR__), - array(__DIR__ . '/../filesystem/../filesystem', __DIR__), - array(__DIR__ . '/././', __DIR__), - array(__DIR__ . '/non_existent', false), + // Constant data + yield [__DIR__, __DIR__]; + yield [__DIR__ . '/../filesystem/../filesystem', __DIR__]; + yield [__DIR__ . '/././', __DIR__]; + yield [__DIR__ . '/non_existent', false]; - array(__FILE__, __FILE__), - array(__FILE__ . '../', false), - ); + yield [__FILE__, __FILE__]; + yield [__FILE__ . '../', false]; } public function realpath_resolve_relative_without_symlinks_data() @@ -53,13 +51,11 @@ class phpbb_filesystem_helper_realpath_test extends phpbb_test_case $relative_path = filesystem_helper::make_path_relative(__DIR__, getcwd()); - return array( - array($relative_path, __DIR__), - array($relative_path . '../filesystem/../filesystem', __DIR__), - array($relative_path . '././', __DIR__), + yield [$relative_path, __DIR__]; + yield [$relative_path . '../filesystem/../filesystem', __DIR__]; + yield [$relative_path . '././', __DIR__]; - array($relative_path . 'helper_realpath_test.php', __FILE__), - ); + yield [$relative_path . 'helper_realpath_test.php', __FILE__]; } /** diff --git a/tests/storage/adapter/local_test.php b/tests/storage/adapter/local_test.php index bed30aa77f..42b4c5929b 100644 --- a/tests/storage/adapter/local_test.php +++ b/tests/storage/adapter/local_test.php @@ -28,6 +28,14 @@ $this->adapter = new \phpbb\storage\adapter\local($config, $filesystem, $phpbb_root_path, $path_key); } + public function data_test_exists() + { + yield ['README.md', true]; + yield ['nonexistent_file.php', false]; + yield ['phpBB/phpbb', true]; + yield ['nonexistent/folder', false]; + } + public function tearDown() { $this->adapter = null; @@ -48,24 +56,24 @@ unlink('file.txt'); } - public function test_exists() + /** + * @dataProvider data_test_exists + */ + public function test_exists($path, $expected) { - // Exists with files - $this->assertTrue($this->adapter->exists('README.md')); - $this->assertFalse($this->adapter->exists('nonexistent_file.php')); - // exists with directory - $this->assertTrue($this->adapter->exists('phpBB')); - $this->assertFalse($this->adapter->exists('nonexistet_folder')); + $this->assertSame($expected, $this->adapter->exists($path)); } - public function test_delete() + public function test_delete_file() { - // Delete with files file_put_contents('file.txt', ''); $this->assertTrue(file_exists('file.txt')); $this->adapter->delete('file.txt'); $this->assertFalse(file_exists('file.txt')); - // Delete with directories + } + + public function test_delete_folder() + { mkdir('path/to/dir', 0777, true); $this->assertTrue(file_exists('path/to/dir')); $this->adapter->delete('path'); From 9ff25b5970d8665ba55485d9991e5c7126a92382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 12:20:41 +0200 Subject: [PATCH 14/26] [ticket/15253] Replace realpath with helper PHPBB3-15253 --- phpBB/phpbb/filesystem/filesystem.php | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/phpBB/phpbb/filesystem/filesystem.php b/phpBB/phpbb/filesystem/filesystem.php index 80474cb0a2..7754a4107b 100644 --- a/phpBB/phpbb/filesystem/filesystem.php +++ b/phpBB/phpbb/filesystem/filesystem.php @@ -468,27 +468,7 @@ class filesystem implements filesystem_interface */ public function realpath($path) { - if (!function_exists('realpath')) - { - return $this->phpbb_own_realpath($path); - } - - $realpath = realpath($path); - - // Strangely there are provider not disabling realpath but returning strange values. :o - // We at least try to cope with them. - if ((!$this->is_absolute_path($path) && $realpath === $path) || $realpath === false) - { - return $this->phpbb_own_realpath($path); - } - - // Check for DIRECTORY_SEPARATOR at the end (and remove it!) - if (substr($realpath, -1) === DIRECTORY_SEPARATOR) - { - $realpath = substr($realpath, 0, -1); - } - - return $realpath; + return helper::realpath($path); } /** From 425fb5b09d2ca87d12533087493739f8bba93ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 12:34:39 +0200 Subject: [PATCH 15/26] [ticket/15253] Fix test for Date: Tue, 27 Jun 2017 17:06:54 +0200 Subject: [PATCH 16/26] [ticket/15253] Make symfony_filesystem variable static PHPBB3-15253 --- phpBB/phpbb/filesystem/helper.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/filesystem/helper.php b/phpBB/phpbb/filesystem/helper.php index a8a792bebc..1f434bac50 100644 --- a/phpBB/phpbb/filesystem/helper.php +++ b/phpBB/phpbb/filesystem/helper.php @@ -17,6 +17,12 @@ use Symfony\Component\Filesystem\Filesystem as symfony_filesystem; class helper { + + /** + * @var \Symfony\Component\Filesystem\Filesystem + */ + protected static $symfony_filesystem; + /** * Eliminates useless . and .. components from specified path. * @@ -207,8 +213,7 @@ class helper */ public static function make_path_relative($end_path, $start_path) { - $symfony_filesystem = new symfony_filesystem(); - return $symfony_filesystem->makePathRelative($end_path, $start_path); + self::get_symfony_filesystem()->makePathRelative($end_path, $start_path); } /** @@ -363,4 +368,19 @@ class helper return ($return_array) ? $resolved : $resolved_path; } + + /** + * Get an instance of symfony's filesystem object. + * + * @return \Symfony\Component\Filesystem\Filesystem Symfony filesystem + */ + protected static function get_symfony_filesystem() + { + if (self::$symfony_filesystem === null) + { + self::$symfony_filesystem = new symfony_filesystem(); + } + + return self::$symfony_filesystem; + } } From a01d33b1fa103b3bb6b827718ae98a4ec5a14932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Tue, 27 Jun 2017 21:35:51 +0200 Subject: [PATCH 17/26] [ticket/15253] Add missing return PHPBB3-15253 --- phpBB/phpbb/filesystem/helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/filesystem/helper.php b/phpBB/phpbb/filesystem/helper.php index 1f434bac50..6dd58008dc 100644 --- a/phpBB/phpbb/filesystem/helper.php +++ b/phpBB/phpbb/filesystem/helper.php @@ -213,7 +213,7 @@ class helper */ public static function make_path_relative($end_path, $start_path) { - self::get_symfony_filesystem()->makePathRelative($end_path, $start_path); + return self::get_symfony_filesystem()->makePathRelative($end_path, $start_path); } /** From 436410761ddd917a5bad5f7284cd11299e48389b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Wed, 28 Jun 2017 13:33:19 +0200 Subject: [PATCH 18/26] [ticket/15253] Fix code PHPBB3-15253 --- phpBB/includes/functions_compatibility.php | 15 --------------- phpBB/phpbb/filesystem/helper.php | 5 ++--- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/phpBB/includes/functions_compatibility.php b/phpBB/includes/functions_compatibility.php index e1ff4004ff..6104ed7f60 100644 --- a/phpBB/includes/functions_compatibility.php +++ b/phpBB/includes/functions_compatibility.php @@ -96,11 +96,6 @@ function phpbb_check_hash($password, $hash) */ function phpbb_clean_path($path) { - if (!class_exists('\phpbb\filesystem\helper')) - { - require($phpbb_root_path . 'phpbb/filesystem/helper.' . $phpEx); - } - return \phpbb\filesystem\helper::clean_path($path); } @@ -443,11 +438,6 @@ function phpbb_is_writable($file) */ function phpbb_is_absolute($path) { - if (!class_exists('\phpbb\filesystem\helper')) - { - require($phpbb_root_path . 'phpbb/filesystem/helper.' . $phpEx); - } - return \phpbb\filesystem\helper::is_absolute_path($path); } @@ -458,11 +448,6 @@ function phpbb_is_absolute($path) */ function phpbb_realpath($path) { - if (!class_exists('\phpbb\filesystem\helper')) - { - require($phpbb_root_path . 'phpbb/filesystem/helper.' . $phpEx); - } - return \phpbb\filesystem\helper::realpath($path); } diff --git a/phpBB/phpbb/filesystem/helper.php b/phpBB/phpbb/filesystem/helper.php index 6dd58008dc..ec684ee396 100644 --- a/phpBB/phpbb/filesystem/helper.php +++ b/phpBB/phpbb/filesystem/helper.php @@ -17,7 +17,6 @@ use Symfony\Component\Filesystem\Filesystem as symfony_filesystem; class helper { - /** * @var \Symfony\Component\Filesystem\Filesystem */ @@ -234,7 +233,7 @@ class helper $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); } - trim ($path, '/'); + trim($path, '/'); $path_parts = explode('/', $path); $resolved = array(); $resolved_path = $prefix; @@ -366,7 +365,7 @@ class helper } } - return ($return_array) ? $resolved : $resolved_path; + return $return_array ? $resolved : $resolved_path; } /** From 7bf1a27db415798dc705366015f562bd5c65f9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Wed, 28 Jun 2017 18:50:08 +0200 Subject: [PATCH 19/26] [ticket/15253] Add old tests PHPBB3-15253 --- tests/filesystem/clean_path_test.php | 54 ++++++++++++++++ tests/filesystem/is_absolute_test.php | 68 ++++++++++++++++++++ tests/filesystem/realpath_test.php | 90 +++++++++++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 tests/filesystem/clean_path_test.php create mode 100644 tests/filesystem/is_absolute_test.php create mode 100644 tests/filesystem/realpath_test.php diff --git a/tests/filesystem/clean_path_test.php b/tests/filesystem/clean_path_test.php new file mode 100644 index 0000000000..d2dec424b4 --- /dev/null +++ b/tests/filesystem/clean_path_test.php @@ -0,0 +1,54 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +class phpbb_filesystem_clean_path_test extends phpbb_test_case +{ + protected $filesystem; + + public function setUp() + { + parent::setUp(); + $this->filesystem = new \phpbb\filesystem\filesystem(); + } + + public function clean_path_data() + { + return array( + array('foo', 'foo'), + array('foo/bar', 'foo/bar'), + array('foo/bar/', 'foo/bar/'), + array('foo/./bar', 'foo/bar'), + array('foo/./././bar', 'foo/bar'), + array('foo/bar/.', 'foo/bar'), + array('./foo/bar', './foo/bar'), + array('../foo/bar', '../foo/bar'), + array('./../foo/bar', './../foo/bar'), + array('././../foo/bar', './../foo/bar'), + array('one/two/three', 'one/two/three'), + array('one/two/../three', 'one/three'), + array('one/../two/three', 'two/three'), + array('one/two/..', 'one'), + array('one/two/../', 'one/'), + array('one/two/../three/../four', 'one/four'), + array('one/two/three/../../four', 'one/four'), + ); + } + + /** + * @dataProvider clean_path_data + */ + public function test_clean_path($input, $expected) + { + $this->assertEquals($expected, $this->filesystem->clean_path($input)); + } +} diff --git a/tests/filesystem/is_absolute_test.php b/tests/filesystem/is_absolute_test.php new file mode 100644 index 0000000000..7a50989b74 --- /dev/null +++ b/tests/filesystem/is_absolute_test.php @@ -0,0 +1,68 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_filesystem_is_absolute_test extends phpbb_test_case +{ + /** @var \phpbb\filesystem\filesystem_interface */ + protected $filesystem; + + public function setUp() + { + parent::setUp(); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + } + + static public function is_absolute_data() + { + return array( + // Empty + array('', false), + + // Absolute unix style + array('/etc/phpbb', true), + // Unix does not support \ so that is not an absolute path + array('\etc\phpbb', false), + + // Absolute windows style + array('c:\windows', true), + array('C:\Windows', true), + array('c:/windows', true), + array('C:/Windows', true), + + // Executable + array('etc/phpbb', false), + array('explorer.exe', false), + + // Relative subdir + array('Windows\System32', false), + array('Windows\System32\explorer.exe', false), + array('Windows/System32', false), + array('Windows/System32/explorer.exe', false), + + // Relative updir + array('..\Windows\System32', false), + array('..\Windows\System32\explorer.exe', false), + array('../Windows/System32', false), + array('../Windows/System32/explorer.exe', false), + ); + } + + /** + * @dataProvider is_absolute_data + */ + public function test_is_absolute($path, $expected) + { + $this->assertEquals($expected, $this->filesystem->is_absolute_path($path)); + } +} diff --git a/tests/filesystem/realpath_test.php b/tests/filesystem/realpath_test.php new file mode 100644 index 0000000000..d994935f94 --- /dev/null +++ b/tests/filesystem/realpath_test.php @@ -0,0 +1,90 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_filesystem_realpath_test extends phpbb_test_case +{ + static protected $filesystem_own_realpath; + + /** @var \phpbb\filesystem\filesystem_interface */ + protected $filesystem; + + static public function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + $reflection_class = new ReflectionClass('\phpbb\filesystem\filesystem'); + self::$filesystem_own_realpath = $reflection_class->getMethod('phpbb_own_realpath'); + self::$filesystem_own_realpath->setAccessible(true); + } + + public function setUp() + { + parent::setUp(); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + } + + public function realpath_resolve_absolute_without_symlinks_data() + { + return array( + // Constant data + array(__DIR__, __DIR__), + array(__DIR__ . '/../filesystem/../filesystem', __DIR__), + array(__DIR__ . '/././', __DIR__), + array(__DIR__ . '/non_existent', false), + + array(__FILE__, __FILE__), + array(__FILE__ . '../', false), + ); + } + + public function realpath_resolve_relative_without_symlinks_data() + { + if (!function_exists('getcwd')) + { + return array(); + } + + $filesystem = new \phpbb\filesystem\filesystem(); + $relative_path = $filesystem->make_path_relative(__DIR__, getcwd()); + + return array( + array($relative_path, __DIR__), + array($relative_path . '../filesystem/../filesystem', __DIR__), + array($relative_path . '././', __DIR__), + + array($relative_path . 'realpath_test.php', __FILE__), + ); + } + + /** + * @dataProvider realpath_resolve_absolute_without_symlinks_data + */ + public function test_realpath_absolute_without_links($path, $expected) + { + $this->assertEquals($expected, self::$filesystem_own_realpath->invoke($this->filesystem, $path)); + } + + /** + * @dataProvider realpath_resolve_relative_without_symlinks_data + */ + public function test_realpath_relative_without_links($path, $expected) + { + if (!function_exists('getcwd')) + { + $this->markTestSkipped('phpbb_own_realpath() cannot be tested with relative paths: getcwd is not available.'); + } + + $this->assertEquals($expected, self::$filesystem_own_realpath->invoke($this->filesystem, $path)); + } +} From 9b28b21f9afb8a7f5fe6151dfc93f1616617c62d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Wed, 28 Jun 2017 19:03:17 +0200 Subject: [PATCH 20/26] [ticket/15253] Fix method visibility PHPBB3-15253 --- phpBB/phpbb/filesystem/helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/filesystem/helper.php b/phpBB/phpbb/filesystem/helper.php index ec684ee396..66b7ef380e 100644 --- a/phpBB/phpbb/filesystem/helper.php +++ b/phpBB/phpbb/filesystem/helper.php @@ -226,7 +226,7 @@ class helper * @return string|array|bool returns the resolved path or an array of parts of the path if $return_array is true * or false if path cannot be resolved */ - protected static function resolve_path($path, $prefix = '', $absolute = false, $return_array = false) + public static function resolve_path($path, $prefix = '', $absolute = false, $return_array = false) { if ($return_array) { From 01a28d0bd4aceb711a936f05120e1e22dddf4bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Sat, 1 Jul 2017 13:25:49 +0200 Subject: [PATCH 21/26] [ticket/15253] PHP 7.2 compatibility PHPBB3-15253 --- phpBB/phpbb/filesystem/helper.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/filesystem/helper.php b/phpBB/phpbb/filesystem/helper.php index 66b7ef380e..0bb20eb32d 100644 --- a/phpBB/phpbb/filesystem/helper.php +++ b/phpBB/phpbb/filesystem/helper.php @@ -40,7 +40,7 @@ class helper continue; } - if ($part === '..' && !empty($filtered) && $filtered[sizeof($filtered) - 1] !== '.' && $filtered[sizeof($filtered) - 1] !== '..') + if ($part === '..' && !empty($filtered) && $filtered[count($filtered) - 1] !== '.' && $filtered[count($filtered) - 1] !== '..') { array_pop($filtered); } @@ -97,7 +97,7 @@ class helper else if (function_exists('debug_backtrace')) { $call_stack = debug_backtrace(0); - $working_directory = str_replace(DIRECTORY_SEPARATOR, '/', dirname($call_stack[sizeof($call_stack) - 1]['file'])); + $working_directory = str_replace(DIRECTORY_SEPARATOR, '/', dirname($call_stack[count($call_stack) - 1]['file'])); } else { @@ -109,7 +109,7 @@ class helper //$dir_parts = explode(DIRECTORY_SEPARATOR, __DIR__); //$namespace_parts = explode('\\', trim(__NAMESPACE__, '\\')); - //$namespace_part_count = sizeof($namespace_parts); + //$namespace_part_count = count($namespace_parts); // Check if we still loading from root //if (array_slice($dir_parts, -$namespace_part_count) === $namespace_parts) @@ -261,7 +261,7 @@ class helper array_pop($resolved); $resolved_path = false; } - else if ($path_part === '..' && !empty($resolved) && !in_array($resolved[sizeof($resolved) - 1], array('.', '..'))) + else if ($path_part === '..' && !empty($resolved) && !in_array($resolved[count($resolved) - 1], array('.', '..'))) { array_pop($resolved); $resolved_path = false; From 30e0015ca9679bea38295c3590d48a34f93698d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Fri, 14 Jul 2017 13:40:21 +0200 Subject: [PATCH 22/26] [ticket/15253] Fix get contents method PHPBB3-15253 --- phpBB/phpbb/storage/adapter/adapter_interface.php | 2 ++ phpBB/phpbb/storage/storage.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index 01feb50179..55b2687344 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -33,6 +33,8 @@ interface adapter_interface * * @throws \phpbb\storage\exception\exception When the file dont exists * When cannot read file contents + * @return string Returns file contents + * */ public function get_contents($path); diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 7f0ea4a6dc..91ff7cc5e3 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -32,7 +32,7 @@ class storage public function get_contents($path) { - $this->adapter->put_contents($path); + return $this->adapter->get_contents($path); } public function exists($path) From b8483c582d764c6ff3676bf7567adfb423217667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Wed, 19 Jul 2017 14:06:27 +0200 Subject: [PATCH 23/26] [ticket/15253] Remove delete dir method PHPBB3-15253 --- phpBB/phpbb/storage/storage.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/phpBB/phpbb/storage/storage.php b/phpBB/phpbb/storage/storage.php index 91ff7cc5e3..f532f4d95f 100644 --- a/phpBB/phpbb/storage/storage.php +++ b/phpBB/phpbb/storage/storage.php @@ -59,9 +59,4 @@ class storage { $this->adapter->create_dir($path); } - - public function delete_dir($path) - { - $this->adapter->delete_dir($path); - } } From c18c40aaa5c555d4d837a9163cc209f47e284b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Wed, 19 Jul 2017 14:07:29 +0200 Subject: [PATCH 24/26] [ticket/15253] Add create dir method PHPBB3-15253 --- phpBB/phpbb/storage/adapter/adapter_interface.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index 55b2687344..1b0ca04438 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -77,4 +77,13 @@ interface adapter_interface * When the file cannot be copied */ public function copy($path_orig, $path_dest); + + /** + * Creates a directory recursively. + * + * @param string $path The directory path + * + * @throws \phpbb\storage\exception\exception On any directory creation failure + */ + public function create_dir($path); } From c3a422bdeb4259406a3e9fab2dd0fa9c73dddef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Thu, 20 Jul 2017 17:08:07 +0200 Subject: [PATCH 25/26] [ticket/15253] Fix rebase PHPBB3-15253 --- tests/template/template_test_case.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index ae17688190..bcb6f4cde7 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -86,12 +86,7 @@ class phpbb_template_template_test_case extends phpbb_test_case new \phpbb\symfony_request( new phpbb_mock_request() ), -<<<<<<< HEAD - $filesystem, $this->createMock('\phpbb\request\request'), -======= - $this->getMock('\phpbb\request\request'), ->>>>>>> [ticket/15253] Use storage helper methods instead of filesystem methods $phpbb_root_path, $phpEx ); From 3399594946b47059551708df2da8370a5e240e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Calvo?= Date: Thu, 20 Jul 2017 17:30:59 +0200 Subject: [PATCH 26/26] [ticket/15253] Fix code style PHPBB3-15253 --- phpBB/phpbb/storage/adapter/adapter_interface.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/storage/adapter/adapter_interface.php b/phpBB/phpbb/storage/adapter/adapter_interface.php index 1b0ca04438..70eb6f1cc7 100644 --- a/phpBB/phpbb/storage/adapter/adapter_interface.php +++ b/phpBB/phpbb/storage/adapter/adapter_interface.php @@ -80,10 +80,10 @@ interface adapter_interface /** * Creates a directory recursively. - * + * * @param string $path The directory path - * + * * @throws \phpbb\storage\exception\exception On any directory creation failure - */ + */ public function create_dir($path); }