diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index a61518401c..9e1e1cae0e 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -3317,6 +3317,11 @@ function get_preg_expression($mode) case 'table_prefix': return '#^[a-zA-Z][a-zA-Z0-9_]*$#'; break; + + // Matches the predecing dot + case 'path_remove_dot_trailing_slash': + return '#^(?:(\.)?)+(?:(.+)?)+(?:([\\/\\\])$)#'; + break; } return ''; diff --git a/phpBB/phpbb/controller/helper.php b/phpBB/phpbb/controller/helper.php index e2932086db..fc19b855c0 100644 --- a/phpBB/phpbb/controller/helper.php +++ b/phpBB/phpbb/controller/helper.php @@ -140,8 +140,15 @@ class helper // If enable_mod_rewrite is false we need to replace the current front-end by app.php, otherwise we need to remove it. $base_url = str_replace('/' . $page_name, empty($this->config['enable_mod_rewrite']) ? '/app.' . $this->php_ext : '', $base_url); - // We need to update the base url to move to the directory of the app.php file. - $base_url = str_replace('/app.' . $this->php_ext, '/' . $this->phpbb_root_path . 'app.' . $this->php_ext, $base_url); + // We need to update the base url to move to the directory of the app.php file + if (empty($this->config['enable_mod_rewrite'])) + { + $base_url = str_replace('/app.' . $this->php_ext, '/' . $this->phpbb_root_path . 'app.' . $this->php_ext, $base_url); + } + else + { + $base_url .= preg_replace(get_preg_expression('path_remove_dot_trailing_slash'), '$2', $this->phpbb_root_path); + } $base_url = $this->filesystem->clean_path($base_url); diff --git a/tests/controller/helper_route_test.php b/tests/controller/common_helper_route.php similarity index 92% rename from tests/controller/helper_route_test.php rename to tests/controller/common_helper_route.php index e4ba6a3018..94c5337887 100644 --- a/tests/controller/helper_route_test.php +++ b/tests/controller/common_helper_route.php @@ -15,19 +15,49 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -class phpbb_controller_helper_route_test extends phpbb_test_case +abstract class phpbb_controller_common_helper_route extends phpbb_test_case { + protected $root_path; + public function setUp() { global $phpbb_dispatcher, $phpbb_root_path, $phpEx; + $this->extension_manager = new phpbb_mock_extension_manager( + dirname(__FILE__) . '/', + array( + 'vendor2/foo' => array( + 'ext_name' => 'vendor2/foo', + 'ext_active' => '1', + 'ext_path' => 'ext/vendor2/foo/', + ), + ) + ); + $this->generate_route_objects(); $phpbb_dispatcher = new phpbb_mock_event_dispatcher; - $this->user = new \phpbb\user('\phpbb\datetime'); + } + protected function get_phpbb_root_path() + { + return ''; + } + + protected function get_uri() + { + return '/app.php'; + } + + protected function get_script_name() + { + return 'app.php'; + } + + protected function generate_route_objects() + { $request = new phpbb_mock_request(); - $request->overwrite('SCRIPT_NAME', '/app.php', \phpbb\request\request_interface::SERVER); - $request->overwrite('SCRIPT_FILENAME', 'app.php', \phpbb\request\request_interface::SERVER); - $request->overwrite('REQUEST_URI', '/app.php', \phpbb\request\request_interface::SERVER); + $request->overwrite('SCRIPT_NAME', $this->get_uri(), \phpbb\request\request_interface::SERVER); + $request->overwrite('SCRIPT_FILENAME', $this->get_script_name(), \phpbb\request\request_interface::SERVER); + $request->overwrite('REQUEST_URI', $this->get_uri(), \phpbb\request\request_interface::SERVER); $request->overwrite('SERVER_NAME', 'localhost', \phpbb\request\request_interface::SERVER); $request->overwrite('SERVER_PORT', '80', \phpbb\request\request_interface::SERVER); @@ -35,14 +65,16 @@ class phpbb_controller_helper_route_test extends phpbb_test_case $request ); $this->filesystem = new \phpbb\filesystem(); - $phpbb_path_helper = new \phpbb\path_helper( + $this->phpbb_path_helper = new \phpbb\path_helper( $this->symfony_request, $this->filesystem, $this->getMock('\phpbb\request\request'), $phpbb_root_path, $phpEx ); + $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '0')); + $this->user = new \phpbb\user('\phpbb\datetime'); $container = new phpbb_mock_container_builder(); $cache_path = $phpbb_root_path . 'cache/twig'; @@ -50,7 +82,7 @@ class phpbb_controller_helper_route_test extends phpbb_test_case $loader = new \phpbb\template\twig\loader(''); $twig = new \phpbb\template\twig\environment( $this->config, - $phpbb_path_helper, + $this->phpbb_path_helper, $container, $cache_path, null, @@ -62,7 +94,7 @@ class phpbb_controller_helper_route_test extends phpbb_test_case 'autoescape' => false, ) ); - $this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $this->user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user))); + $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $this->user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user))); $container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig)); $this->extension_manager = new phpbb_mock_extension_manager( @@ -85,6 +117,8 @@ class phpbb_controller_helper_route_test extends phpbb_test_case $this->provider = new \phpbb\controller\provider(); $this->provider->find_routing_files($finder); $this->provider->find(dirname(__FILE__) . '/'); + // Set correct current phpBB root path + $this->root_path = $this->get_phpbb_root_path(); } public function helper_url_data_no_rewrite() @@ -122,7 +156,7 @@ class phpbb_controller_helper_route_test extends phpbb_test_case */ public function test_helper_url_no_rewrite($route, $params, $is_amp, $session_id, $expected, $description) { - $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, '', 'php', dirname(__FILE__) . '/'); + $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/'); $this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id)); } @@ -162,7 +196,7 @@ class phpbb_controller_helper_route_test 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->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, '', 'php', dirname(__FILE__) . '/'); + $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/'); $this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id)); } @@ -202,7 +236,7 @@ class phpbb_controller_helper_route_test 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->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, '', 'php', dirname(__FILE__) . '/'); + $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/'); $this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::ABSOLUTE_URL)); } @@ -242,7 +276,7 @@ class phpbb_controller_helper_route_test 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->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, '', 'php', dirname(__FILE__) . '/'); + $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/'); $this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::RELATIVE_PATH)); } @@ -282,7 +316,7 @@ class phpbb_controller_helper_route_test 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->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, '', 'php', dirname(__FILE__) . '/'); + $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/'); $this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::NETWORK_PATH)); } //TODO @@ -322,7 +356,7 @@ class phpbb_controller_helper_route_test 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->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, '', 'php', dirname(__FILE__) . '/'); + $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/'); $this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::ABSOLUTE_URL)); } @@ -362,7 +396,7 @@ class phpbb_controller_helper_route_test 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->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, '', 'php', dirname(__FILE__) . '/'); + $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/'); $this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::RELATIVE_PATH)); } @@ -402,7 +436,7 @@ class phpbb_controller_helper_route_test 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(array('enable_mod_rewrite' => '1')); - $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, '', 'php', dirname(__FILE__) . '/'); + $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $this->provider, $this->extension_manager, $this->symfony_request, $this->filesystem, $this->root_path, 'php', dirname(__FILE__) . '/'); $this->assertEquals($expected, $this->helper->route($route, $params, $is_amp, $session_id, UrlGeneratorInterface::NETWORK_PATH)); } } diff --git a/tests/controller/helper_route_adm_subdir_test.php b/tests/controller/helper_route_adm_subdir_test.php new file mode 100644 index 0000000000..f27ac81b04 --- /dev/null +++ b/tests/controller/helper_route_adm_subdir_test.php @@ -0,0 +1,33 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/common_helper_route.php'; + +class phpbb_controller_helper_route_adm_subdir_test extends phpbb_controller_common_helper_route +{ + protected function get_phpbb_root_path() + { + return './../../'; + } + + protected function get_uri() + { + return '/adm/subdir/index.php'; + } + + protected function get_script_name() + { + return 'index.php'; + } +} diff --git a/tests/controller/helper_route_adm_test.php b/tests/controller/helper_route_adm_test.php new file mode 100644 index 0000000000..86dc36ef1f --- /dev/null +++ b/tests/controller/helper_route_adm_test.php @@ -0,0 +1,33 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/common_helper_route.php'; + +class phpbb_controller_helper_route_adm_test extends phpbb_controller_common_helper_route +{ + protected function get_phpbb_root_path() + { + return './../'; + } + + protected function get_uri() + { + return '/adm/index.php'; + } + + protected function get_script_name() + { + return 'index.php'; + } +} diff --git a/tests/controller/helper_route_root_test.php b/tests/controller/helper_route_root_test.php new file mode 100644 index 0000000000..63a2f2f8f7 --- /dev/null +++ b/tests/controller/helper_route_root_test.php @@ -0,0 +1,33 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/common_helper_route.php'; + +class phpbb_controller_helper_route_test extends phpbb_controller_common_helper_route +{ + protected function get_phpbb_root_path() + { + return ''; + } + + protected function get_uri() + { + return '/app.php'; + } + + protected function get_script_name() + { + return 'app.php'; + } +} diff --git a/tests/controller/helper_route_unclean_path_test.php b/tests/controller/helper_route_unclean_path_test.php new file mode 100644 index 0000000000..9d8b62bc1c --- /dev/null +++ b/tests/controller/helper_route_unclean_path_test.php @@ -0,0 +1,33 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/common_helper_route.php'; + +class phpbb_controller_helper_route_unclean_path_test extends phpbb_controller_common_helper_route +{ + protected function get_phpbb_root_path() + { + return './../'; + } + + protected function get_uri() + { + return '/adm/../bertie/index.php'; + } + + protected function get_script_name() + { + return 'index.php'; + } +} diff --git a/tests/functions/get_preg_expression_test.php b/tests/functions/get_preg_expression_test.php new file mode 100644 index 0000000000..e74017d315 --- /dev/null +++ b/tests/functions/get_preg_expression_test.php @@ -0,0 +1,40 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_functions_get_preg_expression_test extends phpbb_test_case +{ + public function data_path_remove_dot_trailing_slash() + { + return array( + array('./../', '$2', '/..'), + array('/../', '$2', '/..'), + array('', '$2', ''), + array('./', '$2', ''), + array('/', '$2', ''), + array('./../../', '$2', '/../..'), + array('/../../', '$2', '/../..'), + array('./dir/', '$2', '/dir'), + array('./../dir/', '$2', '/../dir'), + ); + } + + /** + * @dataProvider data_path_remove_dot_trailing_slash + */ + public function test_path_remove_dot_trailing_slash($input, $replace, $expected) + { + $this->assertSame($expected, preg_replace(get_preg_expression('path_remove_dot_trailing_slash'), $replace, $input)); + } +}