Skip to content

Commit

Permalink
Merge pull request #41616 from nextcloud/bugfix/noid/fix-ocs-absolute…
Browse files Browse the repository at this point in the history
…-URLs-with-ignore-front-controller

fix: Fix linkToOCSRouteAbsolute() without index.php and with subfolder
  • Loading branch information
nickvergessen authored Nov 20, 2023
2 parents 0da05fc + 572ea18 commit e5b9967
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
15 changes: 12 additions & 3 deletions lib/private/URLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,25 @@ public function linkToRouteAbsolute(string $routeName, array $arguments = []): s
}

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);
}

Expand Down
22 changes: 15 additions & 7 deletions tests/lib/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,34 @@ public function testGetWebroot() {
/**
* @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'],
];
}

Expand Down

0 comments on commit e5b9967

Please sign in to comment.