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