]> git.mxchange.org Git - friendica.git/blob - mod/search.php
Merge remote-tracking branch 'upstream/develop' into 1706-ostatus-attach
[friendica.git] / mod / search.php
1 <?php
2
3 use Friendica\App;
4
5 require_once("include/bbcode.php");
6 require_once('include/security.php');
7 require_once('include/conversation.php');
8 require_once('mod/dirfind.php');
9
10 function search_saved_searches() {
11
12         $o = '';
13
14         if(! feature_enabled(local_user(),'savedsearch'))
15                 return $o;
16
17         $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
18                 intval(local_user())
19         );
20
21         if (dbm::is_result($r)) {
22                 $saved = array();
23                 foreach ($r as $rr) {
24                         $saved[] = array(
25                                 'id'            => $rr['id'],
26                                 'term'          => $rr['term'],
27                                 'encodedterm'   => urlencode($rr['term']),
28                                 'delete'        => t('Remove term'),
29                                 'selected'      => ($search==$rr['term']),
30                         );
31                 }
32
33
34                 $tpl = get_markup_template("saved_searches_aside.tpl");
35
36                 $o .= replace_macros($tpl, array(
37                         '$title'        => t('Saved Searches'),
38                         '$add'          => '',
39                         '$searchbox'    => '',
40                         '$saved'        => $saved,
41                 ));
42         }
43
44         return $o;
45
46 }
47
48
49 function search_init(App $a) {
50
51         $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
52
53         if(local_user()) {
54                 if(x($_GET,'save') && $search) {
55                         $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
56                                 intval(local_user()),
57                                 dbesc($search)
58                         );
59                         if (! dbm::is_result($r)) {
60                                 q("INSERT INTO `search` (`uid`,`term`) VALUES ( %d, '%s')",
61                                         intval(local_user()),
62                                         dbesc($search)
63                                 );
64                         }
65                 }
66                 if(x($_GET,'remove') && $search) {
67                         q("DELETE FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
68                                 intval(local_user()),
69                                 dbesc($search)
70                         );
71                 }
72
73                 $a->page['aside'] .= search_saved_searches();
74
75         }
76         else {
77                 unset($_SESSION['theme']);
78                 unset($_SESSION['mobile-theme']);
79         }
80
81
82
83 }
84
85
86
87 function search_post(App $a) {
88         if(x($_POST,'search'))
89                 $a->data['search'] = $_POST['search'];
90 }
91
92
93 function search_content(App $a) {
94
95         if (get_config('system','block_public') && !local_user() && !remote_user()) {
96                 notice(t('Public access denied.') . EOL);
97                 return;
98         }
99
100         if (get_config('system','local_search') && !local_user() && !remote_user()) {
101                 http_status_exit(403,
102                                 array("title" => t("Public access denied."),
103                                         "description" => t("Only logged in users are permitted to perform a search.")));
104                 killme();
105                 //notice(t('Public access denied.').EOL);
106                 //return;
107         }
108
109         if (get_config('system','permit_crawling') && !local_user() && !remote_user()) {
110                 // Default values:
111                 // 10 requests are "free", after the 11th only a call per minute is allowed
112
113                 $free_crawls = intval(get_config('system','free_crawls'));
114                 if ($free_crawls == 0)
115                         $free_crawls = 10;
116
117                 $crawl_permit_period = intval(get_config('system','crawl_permit_period'));
118                 if ($crawl_permit_period == 0)
119                         $crawl_permit_period = 10;
120
121                 $remote = $_SERVER["REMOTE_ADDR"];
122                 $result = Cache::get("remote_search:".$remote);
123                 if (!is_null($result)) {
124                         $resultdata = json_decode($result);
125                         if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
126                                 http_status_exit(429,
127                                                 array("title" => t("Too Many Requests"),
128                                                         "description" => t("Only one search per minute is permitted for not logged in users.")));
129                                 killme();
130                         }
131                         Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
132                 } else
133                         Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
134         }
135
136         nav_set_selected('search');
137
138         if(x($a->data,'search'))
139                 $search = notags(trim($a->data['search']));
140         else
141                 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
142
143         $tag = false;
144         if(x($_GET,'tag')) {
145                 $tag = true;
146                 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
147         }
148
149         // contruct a wrapper for the search header
150         $o .= replace_macros(get_markup_template("content_wrapper.tpl"),array(
151                 'name' => "search-header",
152                 '$title' => t("Search"),
153                 '$title_size' => 3,
154                 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
155         ));
156
157         if(strpos($search,'#') === 0) {
158                 $tag = true;
159                 $search = substr($search,1);
160         }
161         if(strpos($search,'@') === 0) {
162                 return dirfind_content($a);
163         }
164         if(strpos($search,'!') === 0) {
165                 return dirfind_content($a);
166         }
167
168         if(x($_GET,'search-option'))
169                 switch($_GET['search-option']) {
170                         case 'fulltext':
171                                 break;
172                         case 'tags':
173                                 $tag = true;
174                                 break;
175                         case 'contacts':
176                                 return dirfind_content($a, "@");
177                                 break;
178                         case 'forums':
179                                 return dirfind_content($a, "!");
180                                 break;
181                 }
182
183         if(! $search)
184                 return $o;
185
186         if (get_config('system','only_tag_search'))
187                 $tag = true;
188
189         // Here is the way permissions work in the search module...
190         // Only public posts can be shown
191         // OR your own posts if you are a logged in member
192         // No items will be shown if the member has a blocked profile wall.
193
194         if($tag) {
195                 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
196
197                 $r = q("SELECT %s
198                         FROM `term`
199                                 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
200                         WHERE %s AND (`term`.`uid` = 0 OR (`term`.`uid` = %d AND NOT `term`.`global`)) AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`term` = '%s'
201                         ORDER BY term.created DESC LIMIT %d , %d ",
202                                 item_fieldlists(), item_joins(), item_condition(),
203                                 intval(local_user()),
204                                 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
205                                 intval($a->pager['start']), intval($a->pager['itemspage']));
206         } else {
207                 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
208
209                 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
210
211                 $r = q("SELECT %s
212                         FROM `item` %s
213                         WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
214                                 $sql_extra
215                         GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
216                                 item_fieldlists(), item_joins(), item_condition(),
217                                 intval(local_user()),
218                                 intval($a->pager['start']), intval($a->pager['itemspage']));
219         }
220
221         if (! dbm::is_result($r)) {
222                 info( t('No results.') . EOL);
223                 return $o;
224         }
225
226
227         if($tag)
228                 $title = sprintf( t('Items tagged with: %s'), $search);
229         else
230                 $title = sprintf( t('Results for: %s'), $search);
231
232         $o .= replace_macros(get_markup_template("section_title.tpl"),array(
233                 '$title' => $title
234         ));
235
236         logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
237         $o .= conversation($a,$r,'search',false);
238
239         $o .= alt_pager($a,count($r));
240
241         logger("Done '".$search."'", LOGGER_DEBUG);
242
243         return $o;
244 }
245