Skip to content

Commit 6d764cb

Browse files
committed
Add function to generate urls for OCS routes
fixes #11617 The OCS routes are only absolute for now as they are often exposed to the outside anyway and are on a different endpoint than index.php in anyway. Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent 877823e commit 6d764cb

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/private/URLGenerator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ public function linkToRouteAbsolute(string $routeName, array $arguments = array(
9191
return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
9292
}
9393

94+
public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
95+
$route = \OC::$server->getRouter()->generate('ocs.'.$routeName, $arguments, false);
96+
97+
if (strpos($route, '/index.php') === 0) {
98+
$route = substr($route, 10);
99+
}
100+
101+
$route = substr($route, 7);
102+
$route = '/ocs/v2.php' . $route;
103+
104+
return $this->getAbsoluteURL($route);
105+
}
106+
94107
/**
95108
* Creates an url
96109
* @param string $app app

lib/public/IURLGenerator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public function linkToRoute(string $routeName, array $arguments = array()): stri
5050
*/
5151
public function linkToRouteAbsolute(string $routeName, array $arguments = array()): string;
5252

53+
/**
54+
* @param string $routeName
55+
* @param array $arguments
56+
* @return string
57+
* @since 15.0.0
58+
*/
59+
public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string;
60+
5361
/**
5462
* Returns an URL for an image or file
5563
* @param string $appName the name of the app

tests/lib/UrlGeneratorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,22 @@ public function testGetBaseUrl() {
162162
$this->assertEquals($expected, $actual);
163163
}
164164

165+
/**
166+
* @dataProvider provideOCSRoutes
167+
*/
168+
public function testLinkToOCSRouteAbsolute(string $route, string $expected) {
169+
$this->mockBaseUrl();
170+
\OC::$WEBROOT = '/owncloud';
171+
$result = $this->urlGenerator->linkToOCSRouteAbsolute($route);
172+
$this->assertEquals($expected, $result);
173+
}
174+
175+
public function provideOCSRoutes() {
176+
return [
177+
['core.OCS.getCapabilities', 'http://localhost/owncloud/ocs/v2.php/cloud/capabilities'],
178+
['core.WhatsNew.dismiss', 'http://localhost/owncloud/ocs/v2.php/core/whatsnew'],
179+
];
180+
}
181+
182+
165183
}

0 commit comments

Comments
 (0)