UnifiedSearchController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Core\Controller;
  27. use OC\Search\SearchComposer;
  28. use OC\Search\SearchQuery;
  29. use OCP\AppFramework\OCSController;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\IRequest;
  33. use OCP\IURLGenerator;
  34. use OCP\IUserSession;
  35. use OCP\Route\IRouter;
  36. use OCP\Search\ISearchQuery;
  37. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  38. class UnifiedSearchController extends OCSController {
  39. private SearchComposer $composer;
  40. private IUserSession $userSession;
  41. private IRouter $router;
  42. private IURLGenerator $urlGenerator;
  43. public function __construct(IRequest $request,
  44. IUserSession $userSession,
  45. SearchComposer $composer,
  46. IRouter $router,
  47. IURLGenerator $urlGenerator) {
  48. parent::__construct('core', $request);
  49. $this->composer = $composer;
  50. $this->userSession = $userSession;
  51. $this->router = $router;
  52. $this->urlGenerator = $urlGenerator;
  53. }
  54. /**
  55. * @NoAdminRequired
  56. * @NoCSRFRequired
  57. *
  58. * @param string $from the url the user is currently at
  59. */
  60. public function getProviders(string $from = ''): DataResponse {
  61. [$route, $parameters] = $this->getRouteInformation($from);
  62. $result = $this->composer->getProviders($route, $parameters);
  63. $response = new DataResponse($result);
  64. $response->setETag(md5(json_encode($result)));
  65. return $response;
  66. }
  67. /**
  68. * @NoAdminRequired
  69. * @NoCSRFRequired
  70. *
  71. * @param string $providerId
  72. * @param string $term
  73. * @param int|null $sortOrder
  74. * @param int|null $limit
  75. * @param int|string|null $cursor
  76. * @param string $from
  77. *
  78. * @return DataResponse
  79. */
  80. public function search(string $providerId,
  81. string $term = '',
  82. ?int $sortOrder = null,
  83. ?int $limit = null,
  84. $cursor = null,
  85. string $from = ''): DataResponse {
  86. if (empty(trim($term))) {
  87. return new DataResponse(null, Http::STATUS_BAD_REQUEST);
  88. }
  89. [$route, $routeParameters] = $this->getRouteInformation($from);
  90. return new DataResponse(
  91. $this->composer->search(
  92. $this->userSession->getUser(),
  93. $providerId,
  94. new SearchQuery(
  95. $term,
  96. $sortOrder ?? ISearchQuery::SORT_DATE_DESC,
  97. $limit ?? SearchQuery::LIMIT_DEFAULT,
  98. $cursor,
  99. $route,
  100. $routeParameters
  101. )
  102. )
  103. );
  104. }
  105. protected function getRouteInformation(string $url): array {
  106. $routeStr = '';
  107. $parameters = [];
  108. if ($url !== '') {
  109. $urlParts = parse_url($url);
  110. $urlPath = $urlParts['path'];
  111. // Optionally strip webroot from URL. Required for route matching on setups
  112. // with Nextcloud in a webserver subfolder (webroot).
  113. $webroot = $this->urlGenerator->getWebroot();
  114. if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) {
  115. $urlPath = substr($urlPath, strlen($webroot));
  116. }
  117. try {
  118. $parameters = $this->router->findMatchingRoute($urlPath);
  119. // contacts.PageController.index => contacts.Page.index
  120. $route = $parameters['caller'];
  121. if (substr($route[1], -10) === 'Controller') {
  122. $route[1] = substr($route[1], 0, -10);
  123. }
  124. $routeStr = implode('.', $route);
  125. // cleanup
  126. unset($parameters['_route'], $parameters['action'], $parameters['caller']);
  127. } catch (ResourceNotFoundException $exception) {
  128. }
  129. if (isset($urlParts['query'])) {
  130. parse_str($urlParts['query'], $queryParameters);
  131. $parameters = array_merge($parameters, $queryParameters);
  132. }
  133. }
  134. return [
  135. $routeStr,
  136. $parameters,
  137. ];
  138. }
  139. }