2018-01-29 12:04:45 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2024-05-27 10:08:53 +02:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-01-29 12:04:45 +01:00
|
|
|
*/
|
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
2024-05-26 12:51:24 +02:00
|
|
|
use OC\Core\ResponseDefinitions;
|
2018-03-05 12:19:20 +01:00
|
|
|
use OCP\AppFramework\Http;
|
2024-01-10 12:35:44 +01:00
|
|
|
use OCP\AppFramework\Http\Attribute\ApiRoute;
|
2024-07-25 13:24:59 +02:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
|
2018-02-01 12:18:24 +01:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\OCSController;
|
2018-01-29 12:04:45 +01:00
|
|
|
use OCP\INavigationManager;
|
|
|
|
use OCP\IRequest;
|
2018-01-29 12:52:40 +01:00
|
|
|
use OCP\IURLGenerator;
|
2018-01-29 12:04:45 +01:00
|
|
|
|
2023-03-15 17:29:32 +01:00
|
|
|
/**
|
|
|
|
* @psalm-import-type CoreNavigationEntry from ResponseDefinitions
|
|
|
|
*/
|
2018-02-01 12:18:24 +01:00
|
|
|
class NavigationController extends OCSController {
|
2023-06-05 18:42:42 +03:30
|
|
|
public function __construct(
|
|
|
|
string $appName,
|
|
|
|
IRequest $request,
|
|
|
|
private INavigationManager $navigationManager,
|
|
|
|
private IURLGenerator $urlGenerator,
|
|
|
|
) {
|
2018-01-29 12:18:11 +01:00
|
|
|
parent::__construct($appName, $request);
|
2018-01-29 12:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Get the apps navigation
|
|
|
|
*
|
|
|
|
* @param bool $absolute Rewrite URLs to absolute ones
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, list<CoreNavigationEntry>, array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Apps navigation returned
|
|
|
|
* 304: No apps navigation changed
|
2018-01-29 12:04:45 +01:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
|
|
|
#[NoCSRFRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/navigation/apps', root: '/core')]
|
2018-02-01 12:18:24 +01:00
|
|
|
public function getAppsNavigation(bool $absolute = false): DataResponse {
|
|
|
|
$navigation = $this->navigationManager->getAll();
|
2018-01-29 12:52:40 +01:00
|
|
|
if ($absolute) {
|
2018-02-01 12:18:24 +01:00
|
|
|
$navigation = $this->rewriteToAbsoluteUrls($navigation);
|
2018-01-29 12:52:40 +01:00
|
|
|
}
|
2018-07-25 13:50:46 +02:00
|
|
|
$navigation = array_values($navigation);
|
2018-03-05 16:22:18 +01:00
|
|
|
$etag = $this->generateETag($navigation);
|
2018-03-05 12:19:20 +01:00
|
|
|
if ($this->request->getHeader('If-None-Match') === $etag) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
|
|
|
|
}
|
|
|
|
$response = new DataResponse($navigation);
|
2018-03-05 16:22:18 +01:00
|
|
|
$response->setETag($etag);
|
2018-03-05 12:19:20 +01:00
|
|
|
return $response;
|
2018-01-29 12:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-15 17:29:32 +01:00
|
|
|
* Get the settings navigation
|
|
|
|
*
|
|
|
|
* @param bool $absolute Rewrite URLs to absolute ones
|
2024-09-24 15:53:13 +02:00
|
|
|
* @return DataResponse<Http::STATUS_OK, list<CoreNavigationEntry>, array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, list<empty>, array{}>
|
2023-03-15 17:29:32 +01:00
|
|
|
*
|
|
|
|
* 200: Apps navigation returned
|
|
|
|
* 304: No apps navigation changed
|
2018-01-29 12:04:45 +01:00
|
|
|
*/
|
2024-07-25 13:24:59 +02:00
|
|
|
#[NoAdminRequired]
|
|
|
|
#[NoCSRFRequired]
|
2024-01-10 12:35:44 +01:00
|
|
|
#[ApiRoute(verb: 'GET', url: '/navigation/settings', root: '/core')]
|
2018-02-01 12:18:24 +01:00
|
|
|
public function getSettingsNavigation(bool $absolute = false): DataResponse {
|
2018-01-29 12:52:40 +01:00
|
|
|
$navigation = $this->navigationManager->getAll('settings');
|
|
|
|
if ($absolute) {
|
2018-02-01 12:18:24 +01:00
|
|
|
$navigation = $this->rewriteToAbsoluteUrls($navigation);
|
2018-01-29 12:52:40 +01:00
|
|
|
}
|
2018-07-25 13:50:46 +02:00
|
|
|
$navigation = array_values($navigation);
|
2018-03-05 16:22:18 +01:00
|
|
|
$etag = $this->generateETag($navigation);
|
2018-03-05 12:19:20 +01:00
|
|
|
if ($this->request->getHeader('If-None-Match') === $etag) {
|
|
|
|
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
|
|
|
|
}
|
|
|
|
$response = new DataResponse($navigation);
|
2018-03-05 16:22:18 +01:00
|
|
|
$response->setETag($etag);
|
2018-03-05 12:19:20 +01:00
|
|
|
return $response;
|
2018-01-29 12:52:40 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 16:22:18 +01:00
|
|
|
/**
|
|
|
|
* Generate an ETag for a list of navigation entries
|
|
|
|
*/
|
|
|
|
private function generateETag(array $navigation): string {
|
|
|
|
foreach ($navigation as &$nav) {
|
|
|
|
if ($nav['id'] === 'logout') {
|
|
|
|
$nav['href'] = 'logout';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return md5(json_encode($navigation));
|
|
|
|
}
|
|
|
|
|
2018-01-29 12:52:40 +01:00
|
|
|
/**
|
|
|
|
* Rewrite href attribute of navigation entries to an absolute URL
|
|
|
|
*/
|
2018-02-01 12:18:24 +01:00
|
|
|
private function rewriteToAbsoluteUrls(array $navigation): array {
|
2018-01-29 12:52:40 +01:00
|
|
|
foreach ($navigation as &$entry) {
|
2024-02-01 14:29:51 +01:00
|
|
|
/* If parse_url finds no host it means the URL is not absolute */
|
|
|
|
if (!isset(\parse_url($entry['href'])['host'])) {
|
2018-01-29 12:52:40 +01:00
|
|
|
$entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
|
|
|
|
}
|
2024-02-01 10:15:45 +01:00
|
|
|
if (!str_starts_with($entry['icon'], $this->urlGenerator->getBaseUrl())) {
|
2018-02-12 17:30:08 +01:00
|
|
|
$entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
|
|
|
|
}
|
2018-01-29 12:52:40 +01:00
|
|
|
}
|
2018-02-01 12:18:24 +01:00
|
|
|
return $navigation;
|
2018-01-29 12:04:45 +01:00
|
|
|
}
|
|
|
|
}
|