NavigationController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Controller;
  7. use OC\Core\ResponseDefinitions;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\Attribute\ApiRoute;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\AppFramework\OCSController;
  12. use OCP\INavigationManager;
  13. use OCP\IRequest;
  14. use OCP\IURLGenerator;
  15. /**
  16. * @psalm-import-type CoreNavigationEntry from ResponseDefinitions
  17. */
  18. class NavigationController extends OCSController {
  19. public function __construct(
  20. string $appName,
  21. IRequest $request,
  22. private INavigationManager $navigationManager,
  23. private IURLGenerator $urlGenerator,
  24. ) {
  25. parent::__construct($appName, $request);
  26. }
  27. /**
  28. * @NoAdminRequired
  29. * @NoCSRFRequired
  30. *
  31. * Get the apps navigation
  32. *
  33. * @param bool $absolute Rewrite URLs to absolute ones
  34. * @return DataResponse<Http::STATUS_OK, CoreNavigationEntry[], array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
  35. *
  36. * 200: Apps navigation returned
  37. * 304: No apps navigation changed
  38. */
  39. #[ApiRoute(verb: 'GET', url: '/navigation/apps', root: '/core')]
  40. public function getAppsNavigation(bool $absolute = false): DataResponse {
  41. $navigation = $this->navigationManager->getAll();
  42. if ($absolute) {
  43. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  44. }
  45. $navigation = array_values($navigation);
  46. $etag = $this->generateETag($navigation);
  47. if ($this->request->getHeader('If-None-Match') === $etag) {
  48. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  49. }
  50. $response = new DataResponse($navigation);
  51. $response->setETag($etag);
  52. return $response;
  53. }
  54. /**
  55. * @NoAdminRequired
  56. * @NoCSRFRequired
  57. *
  58. * Get the settings navigation
  59. *
  60. * @param bool $absolute Rewrite URLs to absolute ones
  61. * @return DataResponse<Http::STATUS_OK, CoreNavigationEntry[], array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
  62. *
  63. * 200: Apps navigation returned
  64. * 304: No apps navigation changed
  65. */
  66. #[ApiRoute(verb: 'GET', url: '/navigation/settings', root: '/core')]
  67. public function getSettingsNavigation(bool $absolute = false): DataResponse {
  68. $navigation = $this->navigationManager->getAll('settings');
  69. if ($absolute) {
  70. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  71. }
  72. $navigation = array_values($navigation);
  73. $etag = $this->generateETag($navigation);
  74. if ($this->request->getHeader('If-None-Match') === $etag) {
  75. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  76. }
  77. $response = new DataResponse($navigation);
  78. $response->setETag($etag);
  79. return $response;
  80. }
  81. /**
  82. * Generate an ETag for a list of navigation entries
  83. */
  84. private function generateETag(array $navigation): string {
  85. foreach ($navigation as &$nav) {
  86. if ($nav['id'] === 'logout') {
  87. $nav['href'] = 'logout';
  88. }
  89. }
  90. return md5(json_encode($navigation));
  91. }
  92. /**
  93. * Rewrite href attribute of navigation entries to an absolute URL
  94. */
  95. private function rewriteToAbsoluteUrls(array $navigation): array {
  96. foreach ($navigation as &$entry) {
  97. /* If parse_url finds no host it means the URL is not absolute */
  98. if (!isset(\parse_url($entry['href'])['host'])) {
  99. $entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
  100. }
  101. if (!str_starts_with($entry['icon'], $this->urlGenerator->getBaseUrl())) {
  102. $entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
  103. }
  104. }
  105. return $navigation;
  106. }
  107. }