Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

195 lines
5.7KB

  1. <?php
  2. /**
  3. * @file mod/search.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Content\Nav;
  7. use Friendica\Content\Pager;
  8. use Friendica\Content\Text\HTML;
  9. use Friendica\Core\Cache;
  10. use Friendica\Core\Config;
  11. use Friendica\Core\L10n;
  12. use Friendica\Core\Logger;
  13. use Friendica\Core\Session;
  14. use Friendica\Core\Renderer;
  15. use Friendica\Database\DBA;
  16. use Friendica\Model\Item;
  17. use Friendica\Module\BaseSearchModule;
  18. use Friendica\Util\Strings;
  19. function search_init(App $a) {
  20. $search = (!empty($_GET['q']) ? Strings::escapeTags(trim(rawurldecode($_GET['q']))) : '');
  21. if (local_user()) {
  22. /// @todo Check if there is a case at all that "aside" is prefilled here
  23. if (!isset($a->page['aside'])) {
  24. $a->page['aside'] = '';
  25. }
  26. $a->page['aside'] .= \Friendica\Content\Widget\SavedSearches::getHTML('search?q=' . $search, $search);
  27. }
  28. }
  29. function search_content(App $a) {
  30. if (Config::get('system','block_public') && !Session::isAuthenticated()) {
  31. notice(L10n::t('Public access denied.') . EOL);
  32. return;
  33. }
  34. if (Config::get('system','local_search') && !Session::isAuthenticated()) {
  35. $e = new \Friendica\Network\HTTPException\ForbiddenException(L10n::t("Only logged in users are permitted to perform a search."));
  36. $e->httpdesc = L10n::t("Public access denied.");
  37. throw $e;
  38. }
  39. if (Config::get('system','permit_crawling') && !Session::isAuthenticated()) {
  40. // Default values:
  41. // 10 requests are "free", after the 11th only a call per minute is allowed
  42. $free_crawls = intval(Config::get('system','free_crawls'));
  43. if ($free_crawls == 0)
  44. $free_crawls = 10;
  45. $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
  46. if ($crawl_permit_period == 0)
  47. $crawl_permit_period = 10;
  48. $remote = $_SERVER["REMOTE_ADDR"];
  49. $result = Cache::get("remote_search:".$remote);
  50. if (!is_null($result)) {
  51. $resultdata = json_decode($result);
  52. if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
  53. throw new \Friendica\Network\HTTPException\TooManyRequestsException(L10n::t("Only one search per minute is permitted for not logged in users."));
  54. }
  55. Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR);
  56. } else
  57. Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR);
  58. }
  59. Nav::setSelected('search');
  60. $search = (!empty($_REQUEST['q']) ? Strings::escapeTags(trim(rawurldecode($_REQUEST['q']))) : '');
  61. $tag = false;
  62. if (!empty($_GET['tag'])) {
  63. $tag = true;
  64. $search = (!empty($_GET['tag']) ? '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))) : '');
  65. }
  66. // contruct a wrapper for the search header
  67. $o = Renderer::replaceMacros(Renderer::getMarkupTemplate("content_wrapper.tpl"),[
  68. 'name' => "search-header",
  69. '$title' => L10n::t("Search"),
  70. '$title_size' => 3,
  71. '$content' => HTML::search($search,'search-box',false)
  72. ]);
  73. if (strpos($search,'#') === 0) {
  74. $tag = true;
  75. $search = substr($search,1);
  76. }
  77. if (strpos($search,'@') === 0) {
  78. return BaseSearchModule::performSearch();
  79. }
  80. if (strpos($search,'!') === 0) {
  81. return BaseSearchModule::performSearch();
  82. }
  83. if (parse_url($search, PHP_URL_SCHEME) != '') {
  84. $id = Item::fetchByLink($search);
  85. if (!empty($id)) {
  86. $item = Item::selectFirst(['guid'], ['id' => $id]);
  87. if (DBA::isResult($item)) {
  88. $a->internalRedirect('display/' . $item['guid']);
  89. }
  90. }
  91. }
  92. if (!empty($_GET['search-option']))
  93. switch($_GET['search-option']) {
  94. case 'fulltext':
  95. break;
  96. case 'tags':
  97. $tag = true;
  98. break;
  99. case 'contacts':
  100. return BaseSearchModule::performSearch('@');
  101. case 'forums':
  102. return BaseSearchModule::performSearch('!');
  103. }
  104. if (!$search)
  105. return $o;
  106. if (Config::get('system','only_tag_search'))
  107. $tag = true;
  108. // Here is the way permissions work in the search module...
  109. // Only public posts can be shown
  110. // OR your own posts if you are a logged in member
  111. // No items will be shown if the member has a blocked profile wall.
  112. $pager = new Pager($a->query_string);
  113. if ($tag) {
  114. Logger::log("Start tag search for '".$search."'", Logger::DEBUG);
  115. $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
  116. AND `otype` = ? AND `type` = ? AND `term` = ?",
  117. local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
  118. $params = ['order' => ['received' => true],
  119. 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
  120. $terms = DBA::select('term', ['oid'], $condition, $params);
  121. $itemids = [];
  122. while ($term = DBA::fetch($terms)) {
  123. $itemids[] = $term['oid'];
  124. }
  125. DBA::close($terms);
  126. if (!empty($itemids)) {
  127. $params = ['order' => ['id' => true]];
  128. $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
  129. $r = Item::inArray($items);
  130. } else {
  131. $r = [];
  132. }
  133. } else {
  134. Logger::log("Start fulltext search for '".$search."'", Logger::DEBUG);
  135. $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
  136. AND `body` LIKE CONCAT('%',?,'%')",
  137. local_user(), $search];
  138. $params = ['order' => ['id' => true],
  139. 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
  140. $items = Item::selectForUser(local_user(), [], $condition, $params);
  141. $r = Item::inArray($items);
  142. }
  143. if (!DBA::isResult($r)) {
  144. info(L10n::t('No results.') . EOL);
  145. return $o;
  146. }
  147. if ($tag) {
  148. $title = L10n::t('Items tagged with: %s', $search);
  149. } else {
  150. $title = L10n::t('Results for: %s', $search);
  151. }
  152. $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate("section_title.tpl"),[
  153. '$title' => $title
  154. ]);
  155. Logger::log("Start Conversation for '".$search."'", Logger::DEBUG);
  156. $o .= conversation($a, $r, $pager, 'search', false, false, 'commented', local_user());
  157. $o .= $pager->renderMinimal(count($r));
  158. Logger::log("Done '".$search."'", Logger::DEBUG);
  159. return $o;
  160. }