diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2023-12-04 21:13:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-04 21:13:14 +0100 |
commit | 870f2274285f81f9549f82bae2b9f9fc1f79cd40 (patch) | |
tree | 69314f1ef8a9e0d5f47265eb574d4882c6343aa9 | |
parent | c4ede819f78c7a86060e503776bf12f39d4e0fd0 (diff) | |
parent | 5d12f1231975ab7249ad554f0d0a3c8d2eb67b13 (diff) | |
download | nextcloud-server-870f2274285f81f9549f82bae2b9f9fc1f79cd40.tar.gz nextcloud-server-870f2274285f81f9549f82bae2b9f9fc1f79cd40.zip |
Merge pull request #41626 from nextcloud/backport/41616/stable27
[stable27] fix: Fix linkToOCSRouteAbsolute() without index.php and with subfolder
-rw-r--r-- | lib/private/URLGenerator.php | 15 | ||||
-rw-r--r-- | tests/lib/UrlGeneratorTest.php | 22 |
2 files changed, 27 insertions, 10 deletions
diff --git a/lib/private/URLGenerator.php b/lib/private/URLGenerator.php index 2410b8a9147..5745cde274e 100644 --- a/lib/private/URLGenerator.php +++ b/lib/private/URLGenerator.php @@ -116,16 +116,25 @@ class URLGenerator implements IURLGenerator { } public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string { + // Returns `/subfolder/index.php/ocsapp/…` with `'htaccess.IgnoreFrontController' => false` in config.php + // And `/subfolder/ocsapp/…` with `'htaccess.IgnoreFrontController' => true` in config.php $route = $this->router->generate('ocs.'.$routeName, $arguments, false); - $indexPhpPos = strpos($route, '/index.php/'); - if ($indexPhpPos !== false) { - $route = substr($route, $indexPhpPos + 10); + // Cut off `/subfolder` + if (\OC::$WEBROOT !== '' && str_starts_with($route, \OC::$WEBROOT)) { + $route = substr($route, \strlen(\OC::$WEBROOT)); } + if (str_starts_with($route, '/index.php/')) { + $route = substr($route, 10); + } + + // Remove `ocsapp/` bit $route = substr($route, 7); + // Prefix with ocs/v2.php endpoint $route = '/ocs/v2.php' . $route; + // Turn into an absolute URL return $this->getAbsoluteURL($route); } diff --git a/tests/lib/UrlGeneratorTest.php b/tests/lib/UrlGeneratorTest.php index b95cb115217..740f02e3e7d 100644 --- a/tests/lib/UrlGeneratorTest.php +++ b/tests/lib/UrlGeneratorTest.php @@ -192,26 +192,34 @@ class UrlGeneratorTest extends \Test\TestCase { /** * @dataProvider provideOCSRoutes */ - public function testLinkToOCSRouteAbsolute(string $route, string $expected) { + public function testLinkToOCSRouteAbsolute(string $route, bool $ignoreFrontController, string $expected): void { $this->mockBaseUrl(); \OC::$WEBROOT = '/nextcloud'; $this->router->expects($this->once()) ->method('generate') - ->willReturnCallback(function ($routeName, $parameters) { + ->willReturnCallback(function (string $routeName, array $parameters) use ($ignoreFrontController) { if ($routeName === 'ocs.core.OCS.getCapabilities') { - return '/index.php/ocsapp/cloud/capabilities'; + if (!$ignoreFrontController) { + return '/nextcloud/index.php/ocsapp/cloud/capabilities'; + } + return '/nextcloud/ocsapp/cloud/capabilities'; } elseif ($routeName === 'ocs.core.WhatsNew.dismiss') { - return '/index.php/ocsapp/core/whatsnew'; + if (!$ignoreFrontController) { + return '/nextcloud/index.php/ocsapp/core/whatsnew'; + } + return '/nextcloud/ocsapp/core/whatsnew'; } }); $result = $this->urlGenerator->linkToOCSRouteAbsolute($route); $this->assertEquals($expected, $result); } - public function provideOCSRoutes() { + public function provideOCSRoutes(): array { return [ - ['core.OCS.getCapabilities', 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'], - ['core.WhatsNew.dismiss', 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'], + ['core.OCS.getCapabilities', false, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'], + ['core.OCS.getCapabilities', true, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'], + ['core.WhatsNew.dismiss', false, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'], + ['core.WhatsNew.dismiss', true, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'], ]; } |