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.

216 lines
5.7 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
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. function search_saved_searches() {
  3. $o = '';
  4. if(! feature_enabled(local_user(),'savedsearch'))
  5. return $o;
  6. $r = q("select `id`,`term` from `search` WHERE `uid` = %d",
  7. intval(local_user())
  8. );
  9. if(count($r)) {
  10. $saved = array();
  11. foreach($r as $rr) {
  12. $saved[] = array(
  13. 'id' => $rr['id'],
  14. 'term' => $rr['term'],
  15. 'encodedterm' => urlencode($rr['term']),
  16. 'delete' => t('Remove term'),
  17. 'selected' => ($search==$rr['term']),
  18. );
  19. }
  20. $tpl = get_markup_template("saved_searches_aside.tpl");
  21. $o .= replace_macros($tpl, array(
  22. '$title' => t('Saved Searches'),
  23. '$add' => '',
  24. '$searchbox' => '',
  25. '$saved' => $saved,
  26. ));
  27. }
  28. return $o;
  29. }
  30. function search_init(&$a) {
  31. $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
  32. if(local_user()) {
  33. if(x($_GET,'save') && $search) {
  34. $r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
  35. intval(local_user()),
  36. dbesc($search)
  37. );
  38. if(! count($r)) {
  39. q("insert into `search` ( `uid`,`term` ) values ( %d, '%s') ",
  40. intval(local_user()),
  41. dbesc($search)
  42. );
  43. }
  44. }
  45. if(x($_GET,'remove') && $search) {
  46. q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
  47. intval(local_user()),
  48. dbesc($search)
  49. );
  50. }
  51. $a->page['aside'] .= search_saved_searches();
  52. }
  53. else {
  54. unset($_SESSION['theme']);
  55. unset($_SESSION['mobile-theme']);
  56. }
  57. }
  58. function search_post(&$a) {
  59. if(x($_POST,'search'))
  60. $a->data['search'] = $_POST['search'];
  61. }
  62. function search_content(&$a) {
  63. if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
  64. notice( t('Public access denied.') . EOL);
  65. return;
  66. }
  67. nav_set_selected('search');
  68. require_once("include/bbcode.php");
  69. require_once('include/security.php');
  70. require_once('include/conversation.php');
  71. $o = '<h3>' . t('Search') . '</h3>';
  72. if(x($a->data,'search'))
  73. $search = notags(trim($a->data['search']));
  74. else
  75. $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
  76. $tag = false;
  77. if(x($_GET,'tag')) {
  78. $tag = true;
  79. $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
  80. }
  81. $o .= search($search,'search-box','/search',((local_user()) ? true : false));
  82. if(strpos($search,'#') === 0) {
  83. $tag = true;
  84. $search = substr($search,1);
  85. }
  86. if(strpos($search,'@') === 0) {
  87. require_once('mod/dirfind.php');
  88. return dirfind_content($a);
  89. }
  90. if(! $search)
  91. return $o;
  92. if (get_config('system','only_tag_search'))
  93. $tag = true;
  94. if($tag) {
  95. $sql_extra = "";
  96. $sql_table = sprintf("`item` INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d) AS `term` ON `item`.`id` = `term`.`oid` ",
  97. dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG));
  98. $sql_order = "`item`.`id`";
  99. } else {
  100. if (get_config('system','use_fulltext_engine')) {
  101. $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
  102. } else {
  103. $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
  104. }
  105. $sql_table = "`item`";
  106. $sql_order = "`item`.`id`";
  107. //$sql_order = "`item`.`received`";
  108. }
  109. // Here is the way permissions work in the search module...
  110. // Only public posts can be shown
  111. // OR your own posts if you are a logged in member
  112. // No items will be shown if the member has a blocked profile wall.
  113. if(get_config('system', 'old_pager')) {
  114. $r = q("SELECT distinct(`item`.`uri`) as `total`
  115. FROM $sql_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
  116. AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  117. INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
  118. WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
  119. AND (( `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND `item`.`private` = 0 AND `user`.`hidewall` = 0)
  120. OR ( `item`.`uid` = %d ))
  121. $sql_extra ",
  122. intval(local_user())
  123. );
  124. // $sql_extra group by `item`.`uri` ",
  125. if(count($r))
  126. $a->set_pager_total(count($r));
  127. if(! count($r)) {
  128. info( t('No results.') . EOL);
  129. return $o;
  130. }
  131. }
  132. $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
  133. `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`,
  134. `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
  135. `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`,
  136. `user`.`nickname`, `user`.`uid`, `user`.`hidewall`
  137. FROM $sql_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
  138. AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  139. INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
  140. WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
  141. AND (( `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND `item`.`private` = 0 AND `user`.`hidewall` = 0 )
  142. OR ( `item`.`uid` = %d ))
  143. $sql_extra GROUP BY `item`.`uri`
  144. ORDER BY $sql_order DESC LIMIT %d , %d ",
  145. intval(local_user()),
  146. intval($a->pager['start']),
  147. intval($a->pager['itemspage'])
  148. );
  149. // group by `item`.`uri`
  150. if(! count($r)) {
  151. info( t('No results.') . EOL);
  152. return $o;
  153. }
  154. if($tag)
  155. $o .= '<h2>Items tagged with: ' . $search . '</h2>';
  156. else
  157. $o .= '<h2>Search results for: ' . $search . '</h2>';
  158. $o .= conversation($a,$r,'search',false);
  159. if(!get_config('system', 'old_pager')) {
  160. $o .= alt_pager($a,count($r));
  161. } else {
  162. $o .= paginate($a);
  163. }
  164. return $o;
  165. }