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.

247 lines
6.5 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. require_once("include/bbcode.php");
  3. require_once('include/security.php');
  4. require_once('include/conversation.php');
  5. require_once('mod/dirfind.php');
  6. function search_saved_searches() {
  7. $o = '';
  8. if(! feature_enabled(local_user(),'savedsearch'))
  9. return $o;
  10. $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
  11. intval(local_user())
  12. );
  13. if (dbm::is_result($r)) {
  14. $saved = array();
  15. foreach ($r as $rr) {
  16. $saved[] = array(
  17. 'id' => $rr['id'],
  18. 'term' => $rr['term'],
  19. 'encodedterm' => urlencode($rr['term']),
  20. 'delete' => t('Remove term'),
  21. 'selected' => ($search==$rr['term']),
  22. );
  23. }
  24. $tpl = get_markup_template("saved_searches_aside.tpl");
  25. $o .= replace_macros($tpl, array(
  26. '$title' => t('Saved Searches'),
  27. '$add' => '',
  28. '$searchbox' => '',
  29. '$saved' => $saved,
  30. ));
  31. }
  32. return $o;
  33. }
  34. function search_init(App $a) {
  35. $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
  36. if(local_user()) {
  37. if(x($_GET,'save') && $search) {
  38. $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
  39. intval(local_user()),
  40. dbesc($search)
  41. );
  42. if (! dbm::is_result($r)) {
  43. q("INSERT INTO `search` (`uid`,`term`) VALUES ( %d, '%s')",
  44. intval(local_user()),
  45. dbesc($search)
  46. );
  47. }
  48. }
  49. if(x($_GET,'remove') && $search) {
  50. q("DELETE FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
  51. intval(local_user()),
  52. dbesc($search)
  53. );
  54. }
  55. $a->page['aside'] .= search_saved_searches();
  56. }
  57. else {
  58. unset($_SESSION['theme']);
  59. unset($_SESSION['mobile-theme']);
  60. }
  61. }
  62. function search_post(App $a) {
  63. if(x($_POST,'search'))
  64. $a->data['search'] = $_POST['search'];
  65. }
  66. function search_content(App $a) {
  67. if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
  68. notice( t('Public access denied.') . EOL);
  69. return;
  70. }
  71. if(get_config('system','local_search') AND !local_user()) {
  72. http_status_exit(403,
  73. array("title" => t("Public access denied."),
  74. "description" => t("Only logged in users are permitted to perform a search.")));
  75. killme();
  76. //notice(t('Public access denied.').EOL);
  77. //return;
  78. }
  79. if (get_config('system','permit_crawling') AND !local_user()) {
  80. // Default values:
  81. // 10 requests are "free", after the 11th only a call per minute is allowed
  82. $free_crawls = intval(get_config('system','free_crawls'));
  83. if ($free_crawls == 0)
  84. $free_crawls = 10;
  85. $crawl_permit_period = intval(get_config('system','crawl_permit_period'));
  86. if ($crawl_permit_period == 0)
  87. $crawl_permit_period = 10;
  88. $remote = $_SERVER["REMOTE_ADDR"];
  89. $result = Cache::get("remote_search:".$remote);
  90. if (!is_null($result)) {
  91. $resultdata = json_decode($result);
  92. if (($resultdata->time > (time() - $crawl_permit_period)) AND ($resultdata->accesses > $free_crawls)) {
  93. http_status_exit(429,
  94. array("title" => t("Too Many Requests"),
  95. "description" => t("Only one search per minute is permitted for not logged in users.")));
  96. killme();
  97. }
  98. Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
  99. } else
  100. Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
  101. }
  102. nav_set_selected('search');
  103. if(x($a->data,'search'))
  104. $search = notags(trim($a->data['search']));
  105. else
  106. $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
  107. $tag = false;
  108. if(x($_GET,'tag')) {
  109. $tag = true;
  110. $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
  111. }
  112. // contruct a wrapper for the search header
  113. $o .= replace_macros(get_markup_template("content_wrapper.tpl"),array(
  114. 'name' => "search-header",
  115. '$title' => t("Search"),
  116. '$title_size' => 3,
  117. '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
  118. ));
  119. if(strpos($search,'#') === 0) {
  120. $tag = true;
  121. $search = substr($search,1);
  122. }
  123. if(strpos($search,'@') === 0) {
  124. return dirfind_content($a);
  125. }
  126. if(strpos($search,'!') === 0) {
  127. return dirfind_content($a);
  128. }
  129. if(x($_GET,'search-option'))
  130. switch($_GET['search-option']) {
  131. case 'fulltext':
  132. break;
  133. case 'tags':
  134. $tag = true;
  135. break;
  136. case 'contacts':
  137. return dirfind_content($a, "@");
  138. break;
  139. case 'forums':
  140. return dirfind_content($a, "!");
  141. break;
  142. }
  143. if(! $search)
  144. return $o;
  145. if (get_config('system','only_tag_search'))
  146. $tag = true;
  147. // Here is the way permissions work in the search module...
  148. // Only public posts can be shown
  149. // OR your own posts if you are a logged in member
  150. // No items will be shown if the member has a blocked profile wall.
  151. if($tag) {
  152. logger("Start tag search for '".$search."'", LOGGER_DEBUG);
  153. $r = q("SELECT %s
  154. FROM `term`
  155. STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
  156. WHERE %s AND (`term`.`uid` = 0 OR (`term`.`uid` = %d AND NOT `term`.`global`)) AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`term` = '%s'
  157. ORDER BY term.created DESC LIMIT %d , %d ",
  158. item_fieldlists(), item_joins(), item_condition(),
  159. intval(local_user()),
  160. intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
  161. intval($a->pager['start']), intval($a->pager['itemspage']));
  162. } else {
  163. logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
  164. // Disabled until finally is decided how to proceed with this
  165. //if (get_config('system','use_fulltext_engine')) {
  166. // $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
  167. //} else {
  168. $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
  169. //}
  170. $r = q("SELECT %s
  171. FROM `item` %s
  172. WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
  173. $sql_extra
  174. GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
  175. item_fieldlists(), item_joins(), item_condition(),
  176. intval(local_user()),
  177. intval($a->pager['start']), intval($a->pager['itemspage']));
  178. }
  179. if (! dbm::is_result($r)) {
  180. info( t('No results.') . EOL);
  181. return $o;
  182. }
  183. if($tag)
  184. $title = sprintf( t('Items tagged with: %s'), $search);
  185. else
  186. $title = sprintf( t('Results for: %s'), $search);
  187. $o .= replace_macros(get_markup_template("section_title.tpl"),array(
  188. '$title' => $title
  189. ));
  190. logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
  191. $o .= conversation($a,$r,'search',false);
  192. $o .= alt_pager($a,count($r));
  193. logger("Done '".$search."'", LOGGER_DEBUG);
  194. return $o;
  195. }