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