NavigationController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. * @author Kate Döen <kate.doeen@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Controller;
  25. use OCA\Core\ResponseDefinitions;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\Attribute\ApiRoute;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\OCSController;
  30. use OCP\INavigationManager;
  31. use OCP\IRequest;
  32. use OCP\IURLGenerator;
  33. /**
  34. * @psalm-import-type CoreNavigationEntry from ResponseDefinitions
  35. */
  36. class NavigationController extends OCSController {
  37. public function __construct(
  38. string $appName,
  39. IRequest $request,
  40. private INavigationManager $navigationManager,
  41. private IURLGenerator $urlGenerator,
  42. ) {
  43. parent::__construct($appName, $request);
  44. }
  45. /**
  46. * @NoAdminRequired
  47. * @NoCSRFRequired
  48. *
  49. * Get the apps navigation
  50. *
  51. * @param bool $absolute Rewrite URLs to absolute ones
  52. * @return DataResponse<Http::STATUS_OK, CoreNavigationEntry[], array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
  53. *
  54. * 200: Apps navigation returned
  55. * 304: No apps navigation changed
  56. */
  57. #[ApiRoute(verb: 'GET', url: '/navigation/apps', root: '/core')]
  58. public function getAppsNavigation(bool $absolute = false): DataResponse {
  59. $navigation = $this->navigationManager->getAll();
  60. if ($absolute) {
  61. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  62. }
  63. $navigation = array_values($navigation);
  64. $etag = $this->generateETag($navigation);
  65. if ($this->request->getHeader('If-None-Match') === $etag) {
  66. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  67. }
  68. $response = new DataResponse($navigation);
  69. $response->setETag($etag);
  70. return $response;
  71. }
  72. /**
  73. * @NoAdminRequired
  74. * @NoCSRFRequired
  75. *
  76. * Get the settings navigation
  77. *
  78. * @param bool $absolute Rewrite URLs to absolute ones
  79. * @return DataResponse<Http::STATUS_OK, CoreNavigationEntry[], array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
  80. *
  81. * 200: Apps navigation returned
  82. * 304: No apps navigation changed
  83. */
  84. #[ApiRoute(verb: 'GET', url: '/navigation/settings', root: '/core')]
  85. public function getSettingsNavigation(bool $absolute = false): DataResponse {
  86. $navigation = $this->navigationManager->getAll('settings');
  87. if ($absolute) {
  88. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  89. }
  90. $navigation = array_values($navigation);
  91. $etag = $this->generateETag($navigation);
  92. if ($this->request->getHeader('If-None-Match') === $etag) {
  93. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  94. }
  95. $response = new DataResponse($navigation);
  96. $response->setETag($etag);
  97. return $response;
  98. }
  99. /**
  100. * Generate an ETag for a list of navigation entries
  101. */
  102. private function generateETag(array $navigation): string {
  103. foreach ($navigation as &$nav) {
  104. if ($nav['id'] === 'logout') {
  105. $nav['href'] = 'logout';
  106. }
  107. }
  108. return md5(json_encode($navigation));
  109. }
  110. /**
  111. * Rewrite href attribute of navigation entries to an absolute URL
  112. */
  113. private function rewriteToAbsoluteUrls(array $navigation): array {
  114. foreach ($navigation as &$entry) {
  115. /* If parse_url finds no host it means the URL is not absolute */
  116. if (!isset(\parse_url($entry['href'])['host'])) {
  117. $entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
  118. }
  119. if (!str_starts_with($entry['icon'], $this->urlGenerator->getBaseUrl())) {
  120. $entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
  121. }
  122. }
  123. return $navigation;
  124. }
  125. }