1
0

NavigationController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\DataResponse;
  28. use OCP\AppFramework\OCSController;
  29. use OCP\INavigationManager;
  30. use OCP\IRequest;
  31. use OCP\IURLGenerator;
  32. /**
  33. * @psalm-import-type CoreNavigationEntry from ResponseDefinitions
  34. */
  35. class NavigationController extends OCSController {
  36. public function __construct(
  37. string $appName,
  38. IRequest $request,
  39. private INavigationManager $navigationManager,
  40. private IURLGenerator $urlGenerator,
  41. ) {
  42. parent::__construct($appName, $request);
  43. }
  44. /**
  45. * @NoAdminRequired
  46. * @NoCSRFRequired
  47. *
  48. * Get the apps navigation
  49. *
  50. * @param bool $absolute Rewrite URLs to absolute ones
  51. * @return DataResponse<Http::STATUS_OK, CoreNavigationEntry[], array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
  52. *
  53. * 200: Apps navigation returned
  54. * 304: No apps navigation changed
  55. */
  56. public function getAppsNavigation(bool $absolute = false): DataResponse {
  57. $navigation = $this->navigationManager->getAll();
  58. if ($absolute) {
  59. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  60. }
  61. $navigation = array_values($navigation);
  62. $etag = $this->generateETag($navigation);
  63. if ($this->request->getHeader('If-None-Match') === $etag) {
  64. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  65. }
  66. $response = new DataResponse($navigation);
  67. $response->setETag($etag);
  68. return $response;
  69. }
  70. /**
  71. * @NoAdminRequired
  72. * @NoCSRFRequired
  73. *
  74. * Get the settings navigation
  75. *
  76. * @param bool $absolute Rewrite URLs to absolute ones
  77. * @return DataResponse<Http::STATUS_OK, CoreNavigationEntry[], array{}>|DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
  78. *
  79. * 200: Apps navigation returned
  80. * 304: No apps navigation changed
  81. */
  82. public function getSettingsNavigation(bool $absolute = false): DataResponse {
  83. $navigation = $this->navigationManager->getAll('settings');
  84. if ($absolute) {
  85. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  86. }
  87. $navigation = array_values($navigation);
  88. $etag = $this->generateETag($navigation);
  89. if ($this->request->getHeader('If-None-Match') === $etag) {
  90. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  91. }
  92. $response = new DataResponse($navigation);
  93. $response->setETag($etag);
  94. return $response;
  95. }
  96. /**
  97. * Generate an ETag for a list of navigation entries
  98. */
  99. private function generateETag(array $navigation): string {
  100. foreach ($navigation as &$nav) {
  101. if ($nav['id'] === 'logout') {
  102. $nav['href'] = 'logout';
  103. }
  104. }
  105. return md5(json_encode($navigation));
  106. }
  107. /**
  108. * Rewrite href attribute of navigation entries to an absolute URL
  109. */
  110. private function rewriteToAbsoluteUrls(array $navigation): array {
  111. foreach ($navigation as &$entry) {
  112. /* If parse_url finds no host it means the URL is not absolute */
  113. if (!isset(\parse_url($entry['href'])['host'])) {
  114. $entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
  115. }
  116. if (!str_starts_with($entry['icon'], $this->urlGenerator->getBaseUrl())) {
  117. $entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
  118. }
  119. }
  120. return $navigation;
  121. }
  122. }