Skip to content

Commit 5da4227

Browse files
authored
Merge pull request #28303 from nextcloud/fix/unifiedsearchcontroller_webroot
2 parents 9585c4f + 7c76e85 commit 5da4227

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

core/Controller/UnifiedSearchController.php

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

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

5863
$this->composer = $composer;
5964
$this->userSession = $userSession;
6065
$this->router = $router;
66+
$this->urlGenerator = $urlGenerator;
6167
}
6268

6369
/**
@@ -123,9 +129,17 @@ protected function getRouteInformation(string $url): array {
123129

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

127141
try {
128-
$parameters = $this->router->findMatchingRoute($urlParts['path']);
142+
$parameters = $this->router->findMatchingRoute($urlPath);
129143

130144
// contacts.PageController.index => contacts.Page.index
131145
$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
@@ -102,4 +102,10 @@ public function linkToDocs(string $key): string;
102102
* @since 13.0.0
103103
*/
104104
public function getBaseUrl(): string;
105+
106+
/**
107+
* @return string webroot part of the base url
108+
* @since 23.0.0
109+
*/
110+
public function getWebroot(): string;
105111
}

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)