]> git.mxchange.org Git - friendica.git/blob - mod/search.php
d22fea6ffc8df6245b0446d01449fbe5be2aa6c9
[friendica.git] / mod / search.php
1 <?php
2
3 function search_saved_searches() {
4
5         $o = '';
6
7         $r = q("select `term` from `search` WHERE `uid` = %d",
8                 intval(local_user())
9         );
10
11         if(count($r)) {
12                 $o .= '<h3>' . t('Saved Searches') . '</h3>' . "\r\n";
13                 $o .= '<div id="saved-search-list"><ul id="saved-search-ul">' . "\r\n";
14                 foreach($r as $rr) {
15                         $o .= '<li class="saved-search-li clear"><a href="search/?f=&remove=1&search=' . $rr['term'] . '" class="icon drophide savedsearchdrop" title="' . t('Remove term') . '" onclick="return confirmDelete();" onmouseover="imgbright(this);" onmouseout="imgdull(this);" ></a> <a href="search/?f&search=' . $rr['term'] . '" class="savedsearchterm" >' . $rr['term'] . '</a></li>' . "\r\n";
16                 }
17                 $o .= '</ul></div>' . "\r\n";
18         }               
19
20         return $o;
21
22 }
23
24
25 function search_init(&$a) {
26
27         $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
28
29         if(local_user()) {
30                 if(x($_GET,'save') && $search) {
31                         $r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
32                                 intval(local_user()),
33                                 dbesc($search)
34                         );
35                         if(! count($r)) {
36                                 q("insert into `search` ( `uid`,`term` ) values ( %d, '%s') ",
37                                         intval(local_user()),
38                                         dbesc($search)
39                                 );
40                         }
41                 }
42                 if(x($_GET,'remove') && $search) {
43                         q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
44                                 intval(local_user()),
45                                 dbesc($search)
46                         );
47                 }
48
49                 $a->page['aside'] .= search_saved_searches();
50
51         }
52
53
54 }
55
56
57
58 function search_post(&$a) {
59         if(x($_POST,'search'))
60                 $a->data['search'] = $_POST['search'];
61 }
62
63
64 function search_content(&$a) {
65
66         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
67                 notice( t('Public access denied.') . EOL);
68                 return;
69         }
70
71         require_once("include/bbcode.php");
72         require_once('include/security.php');
73         require_once('include/conversation.php');
74
75         if(x($_SESSION,'theme'))
76                 unset($_SESSION['theme']);
77
78         $o = '<div id="live-search"></div>' . "\r\n";
79
80         $o .= '<h3>' . t('Search') . '</h3>';
81
82         if(x($a->data,'search'))
83                 $search = notags(trim($a->data['search']));
84         else
85                 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
86
87         $o .= search($search,'search-box','/search',((local_user()) ? true : false));
88
89         if(! $search)
90                 return $o;
91
92         // Here is the way permissions work in the search module...
93         // Only public wall posts can be shown
94         // OR your own posts if you are a logged in member
95
96         $s_bool  = "AND MATCH (`item`.`body`) AGAINST ( '%s' IN BOOLEAN MODE )";
97         $s_regx  = "AND `item`.`body` REGEXP '%s' ";
98
99         if(mb_strlen($search) >= 3)
100                 $search_alg = $s_bool;
101         else
102                 $search_alg = $s_regx;
103
104         $r = q("SELECT COUNT(*) AS `total`
105                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
106                 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0
107                 AND (( `wall` = 1 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' AND `user`.`hidewall` = 0) 
108                         OR `item`.`uid` = %d )
109                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
110                 $search_alg ",
111                 intval(local_user()),
112                 dbesc($search)
113         );
114
115         if(count($r))
116                 $a->set_pager_total($r[0]['total']);
117
118         if(! $r[0]['total']) {
119                 info( t('No results.') . EOL);
120                 return $o;
121         }
122
123         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
124                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
125                 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`, 
126                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`,
127                 `user`.`nickname`
128                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
129                 LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
130                 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0
131                 AND (( `wall` = 1 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' AND `user`.`hidewall` = 0 ) 
132                         OR `item`.`uid` = %d )
133                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
134                 $search_alg
135                 ORDER BY `received` DESC LIMIT %d , %d ",
136                 intval(local_user()),
137                 dbesc($search),
138                 intval($a->pager['start']),
139                 intval($a->pager['itemspage'])
140
141         );
142
143
144
145         $o .= conversation($a,$r,'search',false);
146
147         $o .= paginate($a);
148         $o .= cc_license();
149
150         return $o;
151 }
152