1
0

NavigationController.php 3.5 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\Attribute\NoAdminRequired;
  11. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCSController;
  14. use OCP\INavigationManager;
  15. use OCP\IRequest;
  16. use OCP\IURLGenerator;
  17. /**
  18. * @psalm-import-type CoreNavigationEntry from ResponseDefinitions
  19. */
  20. class NavigationController extends OCSController {
  21. public function __construct(
  22. string $appName,
  23. IRequest $request,
  24. private INavigationManager $navigationManager,
  25. private IURLGenerator $urlGenerator,
  26. ) {
  27. parent::__construct($appName, $request);
  28. }
  29. /**
  30. * Get the apps navigation
  31. *
  32. * @param bool $absolute Rewrite URLs to absolute ones
  33. * @return DataResponse<Http::STATUS_OK, CoreNavigationEntry[], array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
  34. *
  35. * 200: Apps navigation returned
  36. * 304: No apps navigation changed
  37. */
  38. #[NoAdminRequired]
  39. #[NoCSRFRequired]
  40. #[ApiRoute(verb: 'GET', url: '/navigation/apps', root: '/core')]
  41. public function getAppsNavigation(bool $absolute = false): DataResponse {
  42. $navigation = $this->navigationManager->getAll();
  43. if ($absolute) {
  44. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  45. }
  46. $navigation = array_values($navigation);
  47. $etag = $this->generateETag($navigation);
  48. if ($this->request->getHeader('If-None-Match') === $etag) {
  49. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  50. }
  51. $response = new DataResponse($navigation);
  52. $response->setETag($etag);
  53. return $response;
  54. }
  55. /**
  56. * Get the settings navigation
  57. *
  58. * @param bool $absolute Rewrite URLs to absolute ones
  59. * @return DataResponse<Http::STATUS_OK, CoreNavigationEntry[], array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
  60. *
  61. * 200: Apps navigation returned
  62. * 304: No apps navigation changed
  63. */
  64. #[NoAdminRequired]
  65. #[NoCSRFRequired]
  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. }