diff options
Diffstat (limited to 'lib/private/URLGenerator.php')
-rw-r--r-- | lib/private/URLGenerator.php | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/lib/private/URLGenerator.php b/lib/private/URLGenerator.php index ab568a3296d..1a2978b84d7 100644 --- a/lib/private/URLGenerator.php +++ b/lib/private/URLGenerator.php @@ -14,6 +14,7 @@ use OCP\App\AppPathNotFoundException; use OCP\App\IAppManager; use OCP\ICacheFactory; use OCP\IConfig; +use OCP\INavigationManager; use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserSession; @@ -36,12 +37,13 @@ class URLGenerator implements IURLGenerator { /** @var null|string */ private $baseUrl = null; private ?IAppManager $appManager = null; + private ?INavigationManager $navigationManager = null; public function __construct(IConfig $config, IUserSession $userSession, ICacheFactory $cacheFactory, IRequest $request, - Router $router + Router $router, ) { $this->config = $config; $this->userSession = $userSession; @@ -58,6 +60,14 @@ class URLGenerator implements IURLGenerator { return $this->appManager; } + private function getNavigationManager(): INavigationManager { + if ($this->navigationManager !== null) { + return $this->navigationManager; + } + $this->navigationManager = \OCP\Server::get(INavigationManager::class); + return $this->navigationManager; + } + /** * Creates an url using a defined route * @@ -86,7 +96,7 @@ 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); + $route = $this->router->generate('ocs.' . $routeName, $arguments, false); // Cut off `/subfolder` if (\OC::$WEBROOT !== '' && str_starts_with($route, \OC::$WEBROOT)) { @@ -112,7 +122,7 @@ class URLGenerator implements IURLGenerator { * @param string $appName app * @param string $file file * @param array $args array with param=>value, will be appended to the returned url - * The value of $args will be urlencoded + * The value of $args will be urlencoded * @return string the url * * Returns a url to the given app and file. @@ -166,8 +176,8 @@ class URLGenerator implements IURLGenerator { * Returns the path to the image. */ public function imagePath(string $appName, string $file): string { - $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-'); - $cacheKey = $appName.'-'.$file; + $cache = $this->cacheFactory->createDistributed('imagePath-' . md5($this->getBaseUrl()) . '-'); + $cacheKey = $appName . '-' . $file; if ($key = $cache->get($cacheKey)) { return $key; } @@ -179,14 +189,14 @@ class URLGenerator implements IURLGenerator { $basename = substr(basename($file), 0, -4); try { - $appPath = $this->getAppManager()->getAppPath($appName); - } catch (AppPathNotFoundException $e) { if ($appName === 'core' || $appName === '') { $appName = 'core'; $appPath = false; } else { - throw new RuntimeException('image not found: image: ' . $file . ' webroot: ' . \OC::$WEBROOT . ' serverroot: ' . \OC::$SERVERROOT); + $appPath = $this->getAppManager()->getAppPath($appName); } + } catch (AppPathNotFoundException $e) { + throw new RuntimeException('image not found: image: ' . $file . ' webroot: ' . \OC::$WEBROOT . ' serverroot: ' . \OC::$SERVERROOT); } // Check if the app is in the app folder @@ -245,7 +255,7 @@ class URLGenerator implements IURLGenerator { /** * Makes an URL absolute - * @param string $url the url in the ownCloud host + * @param string $url the url in the Nextcloud host * @return string the absolute version of the url */ public function getAbsoluteURL(string $url): string { @@ -254,7 +264,7 @@ class URLGenerator implements IURLGenerator { if (\OC::$CLI && !\defined('PHPUNIT_RUN')) { return rtrim($this->config->getSystemValueString('overwrite.cli.url'), '/') . '/' . ltrim($url, '/'); } - // The ownCloud web root can already be prepended. + // The Nextcloud web root could already be prepended. if (\OC::$WEBROOT !== '' && str_starts_with($url, \OC::$WEBROOT)) { $url = substr($url, \strlen(\OC::$WEBROOT)); } @@ -288,14 +298,22 @@ class URLGenerator implements IURLGenerator { return $this->getAbsoluteURL($defaultPage); } - $appId = $this->getAppManager()->getDefaultAppForUser(); + $entryId = $this->getNavigationManager()->getDefaultEntryIdForUser(); + $entry = $this->getNavigationManager()->get($entryId); + $href = (string)$entry['href']; + if ($href === '') { + throw new \InvalidArgumentException('Default navigation entry is missing href: ' . $entryId); + } + + if (str_starts_with($href, $this->getBaseUrl())) { + return $href; + } - if ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false) - || getenv('front_controller_active') === 'true') { - return $this->getAbsoluteURL('/apps/' . $appId . '/'); + if (str_starts_with($href, '/index.php/') && ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false) || getenv('front_controller_active') === 'true')) { + $href = substr($href, 10); } - return $this->getAbsoluteURL('/index.php/apps/' . $appId . '/'); + return $this->getAbsoluteURL($href); } /** @@ -303,7 +321,7 @@ class URLGenerator implements IURLGenerator { */ public function getBaseUrl(): string { // BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup. - if ($this->baseUrl === null || $this->baseUrl === "http://" || $this->baseUrl === "https://") { + if ($this->baseUrl === null || $this->baseUrl === 'http://' || $this->baseUrl === 'https://') { $this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT; } return $this->baseUrl; |