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.

104 lines
3.2 KiB

  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. public function __construct(string $appName,
  32. IRequest $request,
  33. private INavigationManager $navigationManager,
  34. private IURLGenerator $urlGenerator) {
  35. parent::__construct($appName, $request);
  36. }
  37. /**
  38. * @NoAdminRequired
  39. * @NoCSRFRequired
  40. */
  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. * @NoAdminRequired
  57. * @NoCSRFRequired
  58. */
  59. public function getSettingsNavigation(bool $absolute = false): DataResponse {
  60. $navigation = $this->navigationManager->getAll('settings');
  61. if ($absolute) {
  62. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  63. }
  64. $navigation = array_values($navigation);
  65. $etag = $this->generateETag($navigation);
  66. if ($this->request->getHeader('If-None-Match') === $etag) {
  67. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  68. }
  69. $response = new DataResponse($navigation);
  70. $response->setETag($etag);
  71. return $response;
  72. }
  73. /**
  74. * Generate an ETag for a list of navigation entries
  75. */
  76. private function generateETag(array $navigation): string {
  77. foreach ($navigation as &$nav) {
  78. if ($nav['id'] === 'logout') {
  79. $nav['href'] = 'logout';
  80. }
  81. }
  82. return md5(json_encode($navigation));
  83. }
  84. /**
  85. * Rewrite href attribute of navigation entries to an absolute URL
  86. */
  87. private function rewriteToAbsoluteUrls(array $navigation): array {
  88. foreach ($navigation as &$entry) {
  89. if (!str_starts_with($entry['href'], $this->urlGenerator->getBaseUrl())) {
  90. $entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
  91. }
  92. if (!str_starts_with($entry['icon'], $this->urlGenerator->getBaseUrl())) {
  93. $entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
  94. }
  95. }
  96. return $navigation;
  97. }
  98. }