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