Use IURLGenerator function to get value of \OC::$WEBROOT global

Signed-off-by: Jonas Meurer <jonas@freesources.org>
This commit is contained in:
Jonas Meurer 2021-08-10 11:06:24 +02:00
parent 5f5bacde8f
commit 7c76e85dde
No known key found for this signature in database
GPG key ID: 5262E7FF491049FE
4 changed files with 27 additions and 2 deletions

View file

@ -33,6 +33,7 @@ use OCP\AppFramework\OCSController;
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;
@ -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;
}
/**
@ -127,7 +133,7 @@ class UnifiedSearchController extends OCSController {
// 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));
}

View file

@ -276,4 +276,11 @@ class URLGenerator implements IURLGenerator {
}
return $this->baseUrl;
}
/**
* @return string webroot part of the base url
*/
public function getWebroot(): string {
return \OC::$WEBROOT;
}
}

View file

@ -102,4 +102,10 @@ interface IURLGenerator {
* @since 13.0.0
*/
public function getBaseUrl(): string;
/**
* @return string webroot part of the base url
* @since 23.0.0
*/
public function getWebroot(): string;
}

View file

@ -179,6 +179,12 @@ class UrlGeneratorTest extends \Test\TestCase {
$this->assertEquals($expected, $actual);
}
public function testGetWebroot() {
\OC::$WEBROOT = '/nextcloud';
$actual = $this->urlGenerator->getWebroot();
$this->assertEquals(\OC::$WEBROOT, $actual);
}
/**
* @dataProvider provideOCSRoutes
*/