1
0

NavigationController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /** @var INavigationManager */
  32. private $navigationManager;
  33. /** @var IURLGenerator */
  34. private $urlGenerator;
  35. public function __construct(string $appName, IRequest $request, INavigationManager $navigationManager, IURLGenerator $urlGenerator) {
  36. parent::__construct($appName, $request);
  37. $this->navigationManager = $navigationManager;
  38. $this->urlGenerator = $urlGenerator;
  39. }
  40. /**
  41. * @NoAdminRequired
  42. * @NoCSRFRequired
  43. *
  44. * @param bool $absolute
  45. * @return DataResponse
  46. */
  47. public function getAppsNavigation(bool $absolute = false): DataResponse {
  48. $navigation = $this->navigationManager->getAll();
  49. if ($absolute) {
  50. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  51. }
  52. $navigation = array_values($navigation);
  53. $etag = $this->generateETag($navigation);
  54. if ($this->request->getHeader('If-None-Match') === $etag) {
  55. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  56. }
  57. $response = new DataResponse($navigation);
  58. $response->setETag($etag);
  59. return $response;
  60. }
  61. /**
  62. * @NoAdminRequired
  63. * @NoCSRFRequired
  64. *
  65. * @param bool $absolute
  66. * @return DataResponse
  67. */
  68. public function getSettingsNavigation(bool $absolute = false): DataResponse {
  69. $navigation = $this->navigationManager->getAll('settings');
  70. if ($absolute) {
  71. $navigation = $this->rewriteToAbsoluteUrls($navigation);
  72. }
  73. $navigation = array_values($navigation);
  74. $etag = $this->generateETag($navigation);
  75. if ($this->request->getHeader('If-None-Match') === $etag) {
  76. return new DataResponse([], Http::STATUS_NOT_MODIFIED);
  77. }
  78. $response = new DataResponse($navigation);
  79. $response->setETag($etag);
  80. return $response;
  81. }
  82. /**
  83. * Generate an ETag for a list of navigation entries
  84. *
  85. * @param array $navigation
  86. * @return string
  87. */
  88. private function generateETag(array $navigation): string {
  89. foreach ($navigation as &$nav) {
  90. if ($nav['id'] === 'logout') {
  91. $nav['href'] = 'logout';
  92. }
  93. }
  94. return md5(json_encode($navigation));
  95. }
  96. /**
  97. * Rewrite href attribute of navigation entries to an absolute URL
  98. *
  99. * @param array $navigation
  100. * @return array
  101. */
  102. private function rewriteToAbsoluteUrls(array $navigation): array {
  103. foreach ($navigation as &$entry) {
  104. if (0 !== strpos($entry['href'], $this->urlGenerator->getBaseUrl())) {
  105. $entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
  106. }
  107. if (0 !== strpos($entry['icon'], $this->urlGenerator->getBaseUrl())) {
  108. $entry['icon'] = $this->urlGenerator->getAbsoluteURL($entry['icon']);
  109. }
  110. }
  111. return $navigation;
  112. }
  113. }