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.

226 lines
6.6 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 = sprintf(" AND `term`.`term` = '%s' AND `term`.`otype` = %d AND `term`.`type` = %d",
  96. //$sql_extra = sprintf(" AND `term`.`term` = '%s' AND `term`.`otype` = %d AND `term`.`type` = %d group by `item`.`uri` ",
  97. // dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG));
  98. //$sql_table = "`term` LEFT JOIN `item` ON `item`.`id` = `term`.`oid` AND `item`.`uid` = `term`.`uid` ";
  99. //$sql_order = "`term`.`tid`";
  100. //$sql_order = "`item`.`received`";
  101. //$sql_extra = sprintf(" AND EXISTS (SELECT * FROM `term` WHERE `item`.`id` = `term`.`oid` AND `item`.`uid` = `term`.`uid` AND `term`.`term` = '%s' AND `term`.`otype` = %d AND `term`.`type` = %d) GROUP BY `item`.`uri` ",
  102. // dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG));
  103. //$sql_table = "`item` FORCE INDEX (`uri`) ";
  104. $sql_extra = "";
  105. $sql_table = sprintf("`item` INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
  106. dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval(local_user()));
  107. $sql_order = "`item`.`received`";
  108. } else {
  109. if (get_config('system','use_fulltext_engine')) {
  110. $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
  111. } else {
  112. $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
  113. }
  114. $sql_table = "`item`";
  115. $sql_order = "`item`.`received`";
  116. }
  117. // Here is the way permissions work in the search module...
  118. // Only public posts can be shown
  119. // OR your own posts if you are a logged in member
  120. // No items will be shown if the member has a blocked profile wall.
  121. if( (! get_config('alt_pager', 'global')) && (! get_pconfig(local_user(),'system','alt_pager')) ) {
  122. $r = q("SELECT distinct(`item`.`uri`) as `total`
  123. FROM $sql_table LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
  124. WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
  125. AND (( `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND `item`.`private` = 0 AND `user`.`hidewall` = 0)
  126. OR ( `item`.`uid` = %d ))
  127. AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  128. $sql_extra ",
  129. intval(local_user())
  130. );
  131. // $sql_extra group by `item`.`uri` ",
  132. if(count($r))
  133. $a->set_pager_total(count($r));
  134. if(! count($r)) {
  135. info( t('No results.') . EOL);
  136. return $o;
  137. }
  138. }
  139. $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
  140. `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`,
  141. `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
  142. `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`,
  143. `user`.`nickname`, `user`.`uid`, `user`.`hidewall`
  144. FROM $sql_table LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
  145. LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
  146. WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
  147. AND (( `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND `item`.`private` = 0 AND `user`.`hidewall` = 0 )
  148. OR ( `item`.`uid` = %d ))
  149. AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  150. $sql_extra
  151. ORDER BY $sql_order DESC LIMIT %d , %d ",
  152. intval(local_user()),
  153. intval($a->pager['start']),
  154. intval($a->pager['itemspage'])
  155. );
  156. // group by `item`.`uri`
  157. if(! count($r)) {
  158. info( t('No results.') . EOL);
  159. return $o;
  160. }
  161. if($tag)
  162. $o .= '<h2>Items tagged with: ' . $search . '</h2>';
  163. else
  164. $o .= '<h2>Search results for: ' . $search . '</h2>';
  165. $o .= conversation($a,$r,'search',false);
  166. if( get_config('alt_pager', 'global') || get_pconfig(local_user(),'system','alt_pager') ) {
  167. $o .= alt_pager($a,count($r));
  168. }
  169. else {
  170. $o .= paginate($a);
  171. }
  172. return $o;
  173. }