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