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.

199 lines
5.7KB

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