From 7ddfd4dbc2d5b5101cc483f762f5939fbaa42af4 Mon Sep 17 00:00:00 2001 From: Jonas Meurer Date: Tue, 3 Aug 2021 17:27:48 +0200 Subject: [PATCH 1/2] UnifiedSearchController: strip webroot from URL before finding a route This should fix route matching in UnifiedSearchController on setups with Nextcloud in a subfolder (webroot). Fixes: #24144 Signed-off-by: Jonas Meurer --- core/Controller/UnifiedSearchController.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/Controller/UnifiedSearchController.php b/core/Controller/UnifiedSearchController.php index c2949ce609581..2930e4840cecb 100644 --- a/core/Controller/UnifiedSearchController.php +++ b/core/Controller/UnifiedSearchController.php @@ -124,9 +124,17 @@ protected function getRouteInformation(string $url): array { if ($url !== '') { $urlParts = parse_url($url); + $urlPath = $urlParts['path']; + + // Optionally strip webroot from URL. Required for route matching on setups + // with Nextcloud in a webserver subfolder (webroot). + $webroot = \OC::$WEBROOT; + if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) { + $urlPath = substr($urlPath, strlen($webroot)); + } try { - $parameters = $this->router->findMatchingRoute($urlParts['path']); + $parameters = $this->router->findMatchingRoute($urlPath); // contacts.PageController.index => contacts.Page.index $route = $parameters['caller']; From d528243c0cba7d1dcb13a6dc75a28a9af8ff8e36 Mon Sep 17 00:00:00 2001 From: Jonas Meurer Date: Tue, 10 Aug 2021 11:06:24 +0200 Subject: [PATCH 2/2] Use IURLGenerator function to get value of `\OC::$WEBROOT` global Signed-off-by: Jonas Meurer --- core/Controller/UnifiedSearchController.php | 10 ++++++++-- lib/private/URLGenerator.php | 7 +++++++ lib/public/IURLGenerator.php | 6 ++++++ tests/lib/UrlGeneratorTest.php | 6 ++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/Controller/UnifiedSearchController.php b/core/Controller/UnifiedSearchController.php index 2930e4840cecb..c1e9c9d319002 100644 --- a/core/Controller/UnifiedSearchController.php +++ b/core/Controller/UnifiedSearchController.php @@ -34,6 +34,7 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\IUserSession; use OCP\Route\IRouter; use OCP\Search\ISearchQuery; @@ -50,15 +51,20 @@ class UnifiedSearchController extends OCSController { /** @var IRouter */ private $router; + /** @var IURLGenerator */ + private $urlGenerator; + public function __construct(IRequest $request, IUserSession $userSession, SearchComposer $composer, - IRouter $router) { + IRouter $router, + IURLGenerator $urlGenerator) { parent::__construct('core', $request); $this->composer = $composer; $this->userSession = $userSession; $this->router = $router; + $this->urlGenerator = $urlGenerator; } /** @@ -128,7 +134,7 @@ protected function getRouteInformation(string $url): array { // Optionally strip webroot from URL. Required for route matching on setups // with Nextcloud in a webserver subfolder (webroot). - $webroot = \OC::$WEBROOT; + $webroot = $this->urlGenerator->getWebroot(); if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) { $urlPath = substr($urlPath, strlen($webroot)); } diff --git a/lib/private/URLGenerator.php b/lib/private/URLGenerator.php index 6c68f5d805f60..12d3a89869601 100644 --- a/lib/private/URLGenerator.php +++ b/lib/private/URLGenerator.php @@ -276,4 +276,11 @@ public function getBaseUrl(): string { } return $this->baseUrl; } + + /** + * @return string webroot part of the base url + */ + public function getWebroot(): string { + return \OC::$WEBROOT; + } } diff --git a/lib/public/IURLGenerator.php b/lib/public/IURLGenerator.php index 81f5d4494846a..fda2a8a84dc33 100644 --- a/lib/public/IURLGenerator.php +++ b/lib/public/IURLGenerator.php @@ -103,4 +103,10 @@ public function linkToDocs(string $key): string; * @since 13.0.0 */ public function getBaseUrl(): string; + + /** + * @return string webroot part of the base url + * @since 23.0.0 + */ + public function getWebroot(): string; } diff --git a/tests/lib/UrlGeneratorTest.php b/tests/lib/UrlGeneratorTest.php index dd2eb2ddc631a..761f4b42eea75 100644 --- a/tests/lib/UrlGeneratorTest.php +++ b/tests/lib/UrlGeneratorTest.php @@ -179,6 +179,12 @@ public function testGetBaseUrl() { $this->assertEquals($expected, $actual); } + public function testGetWebroot() { + \OC::$WEBROOT = '/nextcloud'; + $actual = $this->urlGenerator->getWebroot(); + $this->assertEquals(\OC::$WEBROOT, $actual); + } + /** * @dataProvider provideOCSRoutes */