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.
 
 
 
 
 
 

207 lines
5.4 KiB

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