1
0

UnifiedSearchController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /** @var SearchComposer */
  40. private $composer;
  41. /** @var IUserSession */
  42. private $userSession;
  43. /** @var IRouter */
  44. private $router;
  45. /** @var IURLGenerator */
  46. private $urlGenerator;
  47. public function __construct(IRequest $request,
  48. IUserSession $userSession,
  49. SearchComposer $composer,
  50. IRouter $router,
  51. IURLGenerator $urlGenerator) {
  52. parent::__construct('core', $request);
  53. $this->composer = $composer;
  54. $this->userSession = $userSession;
  55. $this->router = $router;
  56. $this->urlGenerator = $urlGenerator;
  57. }
  58. /**
  59. * @NoAdminRequired
  60. * @NoCSRFRequired
  61. *
  62. * @param string $from the url the user is currently at
  63. *
  64. * @return DataResponse
  65. */
  66. public function getProviders(string $from = ''): DataResponse {
  67. [$route, $parameters] = $this->getRouteInformation($from);
  68. $result = $this->composer->getProviders($route, $parameters);
  69. $response = new DataResponse($result);
  70. $response->setETag(md5(json_encode($result)));
  71. return $response;
  72. }
  73. /**
  74. * @NoAdminRequired
  75. * @NoCSRFRequired
  76. *
  77. * @param string $providerId
  78. * @param string $term
  79. * @param int|null $sortOrder
  80. * @param int|null $limit
  81. * @param int|string|null $cursor
  82. * @param string $from
  83. *
  84. * @return DataResponse
  85. */
  86. public function search(string $providerId,
  87. string $term = '',
  88. ?int $sortOrder = null,
  89. ?int $limit = null,
  90. $cursor = null,
  91. string $from = ''): DataResponse {
  92. if (empty(trim($term))) {
  93. return new DataResponse(null, Http::STATUS_BAD_REQUEST);
  94. }
  95. [$route, $routeParameters] = $this->getRouteInformation($from);
  96. return new DataResponse(
  97. $this->composer->search(
  98. $this->userSession->getUser(),
  99. $providerId,
  100. new SearchQuery(
  101. $term,
  102. $sortOrder ?? ISearchQuery::SORT_DATE_DESC,
  103. $limit ?? SearchQuery::LIMIT_DEFAULT,
  104. $cursor,
  105. $route,
  106. $routeParameters
  107. )
  108. )
  109. );
  110. }
  111. protected function getRouteInformation(string $url): array {
  112. $routeStr = '';
  113. $parameters = [];
  114. if ($url !== '') {
  115. $urlParts = parse_url($url);
  116. $urlPath = $urlParts['path'];
  117. // Optionally strip webroot from URL. Required for route matching on setups
  118. // with Nextcloud in a webserver subfolder (webroot).
  119. $webroot = $this->urlGenerator->getWebroot();
  120. if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) {
  121. $urlPath = substr($urlPath, strlen($webroot));
  122. }
  123. try {
  124. $parameters = $this->router->findMatchingRoute($urlPath);
  125. // contacts.PageController.index => contacts.Page.index
  126. $route = $parameters['caller'];
  127. if (substr($route[1], -10) === 'Controller') {
  128. $route[1] = substr($route[1], 0, -10);
  129. }
  130. $routeStr = implode('.', $route);
  131. // cleanup
  132. unset($parameters['_route'], $parameters['action'], $parameters['caller']);
  133. } catch (ResourceNotFoundException $exception) {
  134. }
  135. if (isset($urlParts['query'])) {
  136. parse_str($urlParts['query'], $queryParameters);
  137. $parameters = array_merge($parameters, $queryParameters);
  138. }
  139. }
  140. return [
  141. $routeStr,
  142. $parameters,
  143. ];
  144. }
  145. }