Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NavigationController.php 3.6KB

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