]> git.mxchange.org Git - friendica.git/blob - mod/search.php
Merge pull request #1848 from annando/1508-search-options
[friendica.git] / mod / search.php
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
7 function search_saved_searches() {
8
9         $o = '';
10
11         if(! feature_enabled(local_user(),'savedsearch'))
12                 return $o;
13
14         $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
15                 intval(local_user())
16         );
17
18         if(count($r)) {
19                 $saved = array();
20                 foreach($r as $rr) {
21                         $saved[] = array(
22                                 'id'            => $rr['id'],
23                                 'term'          => $rr['term'],
24                                 'encodedterm'   => urlencode($rr['term']),
25                                 'delete'        => t('Remove term'),
26                                 'selected'      => ($search==$rr['term']),
27                         );
28                 }
29
30
31                 $tpl = get_markup_template("saved_searches_aside.tpl");
32
33                 $o .= replace_macros($tpl, array(
34                         '$title'        => t('Saved Searches'),
35                         '$add'          => '',
36                         '$searchbox'    => '',
37                         '$saved'        => $saved,
38                 ));
39         }
40
41         return $o;
42
43 }
44
45
46 function search_init(&$a) {
47
48         $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
49
50         if(local_user()) {
51                 if(x($_GET,'save') && $search) {
52                         $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
53                                 intval(local_user()),
54                                 dbesc($search)
55                         );
56                         if(! count($r)) {
57                                 q("INSERT INTO `search` (`uid`,`term`) VALUES ( %d, '%s')",
58                                         intval(local_user()),
59                                         dbesc($search)
60                                 );
61                         }
62                 }
63                 if(x($_GET,'remove') && $search) {
64                         q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
65                                 intval(local_user()),
66                                 dbesc($search)
67                         );
68                 }
69
70                 $a->page['aside'] .= search_saved_searches();
71
72         }
73         else {
74                 unset($_SESSION['theme']);
75                 unset($_SESSION['mobile-theme']);
76         }
77
78
79
80 }
81
82
83
84 function search_post(&$a) {
85         if(x($_POST,'search'))
86                 $a->data['search'] = $_POST['search'];
87 }
88
89
90 function search_content(&$a) {
91
92         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
93                 notice( t('Public access denied.') . EOL);
94                 return;
95         }
96
97         nav_set_selected('search');
98
99
100         $o = '<h3>' . t('Search') . '</h3>';
101
102         if(x($a->data,'search'))
103                 $search = notags(trim($a->data['search']));
104         else
105                 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
106
107         $tag = false;
108         if(x($_GET,'tag')) {
109                 $tag = true;
110                 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
111         }
112
113
114         $o .= search($search,'search-box','/search',((local_user()) ? true : false), false);
115
116         if(strpos($search,'#') === 0) {
117                 $tag = true;
118                 $search = substr($search,1);
119         }
120         if(strpos($search,'@') === 0) {
121                 return dirfind_content($a);
122         }
123         if(strpos($search,'!') === 0) {
124                 return dirfind_content($a);
125         }
126
127         if(x($_GET,'search-option'))
128                 switch($_GET['search-option']) {
129                         case 'fulltext':
130                                 break;
131                         case 'tags':
132                                 $tag = true;
133                                 break;
134                         case 'contacts':
135                                 return dirfind_content($a, "@");
136                                 break;
137                         case 'forums':
138                                 return dirfind_content($a, "!");
139                                 break;
140                 }
141
142         if(! $search)
143                 return $o;
144
145         if (get_config('system','only_tag_search'))
146                 $tag = true;
147
148         // Here is the way permissions work in the search module...
149         // Only public posts can be shown
150         // OR your own posts if you are a logged in member
151         // No items will be shown if the member has a blocked profile wall.
152
153         if($tag) {
154                 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
155
156                 $r = q("SELECT STRAIGHT_JOIN `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
157                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`,
158                                 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
159                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
160                         FROM `term`
161                                 INNER JOIN `item` ON `item`.`id`=`term`.`oid`
162                                 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
163                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
164                                 AND (`term`.`uid` = 0 OR (`term`.`uid` = %d AND NOT `term`.`global`)) AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`term` = '%s'
165                         ORDER BY term.created DESC LIMIT %d , %d ",
166                                 intval(local_user()), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
167                                 intval($a->pager['start']), intval($a->pager['itemspage']));
168         } else {
169                 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
170
171                 if (get_config('system','use_fulltext_engine')) {
172                         $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
173                 } else {
174                         $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
175                 }
176
177                 $r = q("SELECT STRAIGHT_JOIN `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
178                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`,
179                                 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
180                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
181                         FROM `item`
182                                 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
183                         WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
184                                 AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND (`item`.`private` OR NOT `item`.`network` IN ('%s', '%s', '%s'))))
185                                 $sql_extra
186                         GROUP BY `item`.`uri` ORDER BY `item`.`id` DESC LIMIT %d , %d ",
187                                 intval(local_user()), dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA),
188                                 intval($a->pager['start']), intval($a->pager['itemspage']));
189         }
190
191         if(! count($r)) {
192                 info( t('No results.') . EOL);
193                 return $o;
194         }
195
196
197         if($tag)
198                 $title = sprintf( t('Items tagged with: %s'), $search);
199         else
200                 $title = sprintf( t('Search results for: %s'), $search);
201
202         $o .= replace_macros(get_markup_template("section_title.tpl"),array(
203                 '$title' => $title
204         ));
205
206         logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
207         $o .= conversation($a,$r,'search',false);
208
209         $o .= alt_pager($a,count($r));
210
211         logger("Done '".$search."'", LOGGER_DEBUG);
212
213         return $o;
214 }
215