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