Skip to content

Commit 5c89061

Browse files
Merge pull request #28455 from nextcloud/backport/28303/stable21
[stable21] UnifiedSearchController: strip webroot from URL before finding a route
2 parents ed5472d + d528243 commit 5c89061

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

core/Controller/UnifiedSearchController.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCP\AppFramework\Http;
3535
use OCP\AppFramework\Http\DataResponse;
3636
use OCP\IRequest;
37+
use OCP\IURLGenerator;
3738
use OCP\IUserSession;
3839
use OCP\Route\IRouter;
3940
use OCP\Search\ISearchQuery;
@@ -50,15 +51,20 @@ class UnifiedSearchController extends OCSController {
5051
/** @var IRouter */
5152
private $router;
5253

54+
/** @var IURLGenerator */
55+
private $urlGenerator;
56+
5357
public function __construct(IRequest $request,
5458
IUserSession $userSession,
5559
SearchComposer $composer,
56-
IRouter $router) {
60+
IRouter $router,
61+
IURLGenerator $urlGenerator) {
5762
parent::__construct('core', $request);
5863

5964
$this->composer = $composer;
6065
$this->userSession = $userSession;
6166
$this->router = $router;
67+
$this->urlGenerator = $urlGenerator;
6268
}
6369

6470
/**
@@ -124,9 +130,17 @@ protected function getRouteInformation(string $url): array {
124130

125131
if ($url !== '') {
126132
$urlParts = parse_url($url);
133+
$urlPath = $urlParts['path'];
134+
135+
// Optionally strip webroot from URL. Required for route matching on setups
136+
// with Nextcloud in a webserver subfolder (webroot).
137+
$webroot = $this->urlGenerator->getWebroot();
138+
if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) {
139+
$urlPath = substr($urlPath, strlen($webroot));
140+
}
127141

128142
try {
129-
$parameters = $this->router->findMatchingRoute($urlParts['path']);
143+
$parameters = $this->router->findMatchingRoute($urlPath);
130144

131145
// contacts.PageController.index => contacts.Page.index
132146
$route = $parameters['caller'];

lib/private/URLGenerator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,11 @@ public function getBaseUrl(): string {
276276
}
277277
return $this->baseUrl;
278278
}
279+
280+
/**
281+
* @return string webroot part of the base url
282+
*/
283+
public function getWebroot(): string {
284+
return \OC::$WEBROOT;
285+
}
279286
}

lib/public/IURLGenerator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,10 @@ public function linkToDocs(string $key): string;
103103
* @since 13.0.0
104104
*/
105105
public function getBaseUrl(): string;
106+
107+
/**
108+
* @return string webroot part of the base url
109+
* @since 23.0.0
110+
*/
111+
public function getWebroot(): string;
106112
}

tests/lib/UrlGeneratorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ public function testGetBaseUrl() {
179179
$this->assertEquals($expected, $actual);
180180
}
181181

182+
public function testGetWebroot() {
183+
\OC::$WEBROOT = '/nextcloud';
184+
$actual = $this->urlGenerator->getWebroot();
185+
$this->assertEquals(\OC::$WEBROOT, $actual);
186+
}
187+
182188
/**
183189
* @dataProvider provideOCSRoutes
184190
*/

0 commit comments

Comments
 (0)