You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NavigationController.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Controller;
  24. use OCP\AppFramework\Http;
  25. use OCP\AppFramework\Http\DataResponse;
  26. use OCP\AppFramework\OCSController;
  27. use OCP\INavigationManager;
  28. use OCP\IRequest;
  29. use OCP\IURLGenerator;
  30. class NavigationController extends OCSController {
  31. private INavigationManager $navigationManager;
  32. private IURLGenerator $urlGenerator;
  33. public function __construct(string $appName, IRequest $request, INavigationManager $navigationManager, IURLGenerator $urlGenerator) {
  34. parent::__construct($appName, $request);
  35. $this->navigationManager = $navigationManager;
  36. $this->urlGenerator = $urlGenerator;
  37. }
  38. /**
  39. * @NoAdminRequired
  40. * @NoCSRFRequired
  41. */
  42. public function getAppsNavigation(bool $absolute = false): DataResponse {
  43. $navigation = $this->navigationManager->getAll();
  44. if ($absolute) {
  45. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  46. }
  47. $navigation = array_values($navigation);
  48. $etag = $this->generateETag($navigation);
  49. if ($this->request->getHeader('If-None-Match') === $etag) {
  50. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  51. }
  52. $response = new DataResponse($navigation);
  53. $response->setETag($etag);
  54. return $response;
  55. }
  56. /**
  57. * @NoAdminRequired
  58. * @NoCSRFRequired
  59. */
  60. public function getSettingsNavigation(bool $absolute = false): DataResponse {
  61. $navigation = $this->navigationManager->getAll('settings');
  62. if ($absolute) {
  63. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  64. }
  65. $navigation = array_values($navigation);
  66. $etag = $this->generateETag($navigation);
  67. if ($this->request->getHeader('If-None-Match') === $etag) {
  68. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  69. }
  70. $response = new DataResponse($navigation);
  71. $response->setETag($etag);
  72. return $response;
  73. }
  74. /**
  75. * Generate an ETag for a list of navigation entries
  76. */
  77. private function generateETag(array $navigation): string {
  78. foreach ($navigation as &$nav) {
  79. if ($nav['id'] === 'logout') {
  80. $nav['href'] = 'logout';
  81. }
  82. }
  83. return md5(json_encode($navigation));
  84. }
  85. /**
  86. * Rewrite href attribute of navigation entries to an absolute URL
  87. */
  88. private function rewriteToAbsoluteUrls(array $navigation): array {
  89. foreach ($navigation as &$entry) {
  90. if (0 !== strpos($entry['href'], $this->urlGenerator->getBaseUrl())) {
  91. $entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
  92. }
  93. if (0 !== strpos($entry['icon'], $this->urlGenerator->getBaseUrl())) {
  94. $entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
  95. }
  96. }
  97. return $navigation;
  98. }
  99. }