Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable22] UnifiedSearchController: strip webroot from URL before finding a route #28454

Merged
merged 2 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions core/Controller/UnifiedSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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;
Expand All @@ -49,15 +50,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;
}

/**
Expand Down Expand Up @@ -123,9 +129,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 = $this->urlGenerator->getWebroot();
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'];
Expand Down
7 changes: 7 additions & 0 deletions lib/private/URLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 6 additions & 0 deletions lib/public/IURLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,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;
}
6 changes: 6 additions & 0 deletions tests/lib/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down