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