]> git.mxchange.org Git - friendica.git/blob - mod/search.php
Fix undefined variables PHP Notice
[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\Database\DBM;
11
12 require_once "include/bbcode.php";
13 require_once 'include/security.php';
14 require_once 'include/conversation.php';
15 require_once 'mod/dirfind.php';
16
17 function search_saved_searches() {
18
19         $o = '';
20
21         if (! Feature::isEnabled(local_user(),'savedsearch'))
22                 return $o;
23
24         $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
25                 intval(local_user())
26         );
27
28         if (DBM::is_result($r)) {
29                 $saved = [];
30                 foreach ($r as $rr) {
31                         $saved[] = [
32                                 'id'            => $rr['id'],
33                                 'term'          => $rr['term'],
34                                 'encodedterm'   => urlencode($rr['term']),
35                                 'delete'        => t('Remove term'),
36                                 'selected'      => ($search==$rr['term']),
37                         ];
38                 }
39
40
41                 $tpl = get_markup_template("saved_searches_aside.tpl");
42
43                 $o .= replace_macros($tpl, [
44                         '$title'        => t('Saved Searches'),
45                         '$add'          => '',
46                         '$searchbox'    => '',
47                         '$saved'        => $saved,
48                 ]);
49         }
50
51         return $o;
52
53 }
54
55
56 function search_init(App $a) {
57
58         $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
59
60         if (local_user()) {
61                 if (x($_GET,'save') && $search) {
62                         $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
63                                 intval(local_user()),
64                                 dbesc($search)
65                         );
66                         if (!DBM::is_result($r)) {
67                                 dba::insert('search', ['uid' => local_user(), 'term' => $search]);
68                         }
69                 }
70                 if (x($_GET,'remove') && $search) {
71                         dba::delete('search', ['uid' => local_user(), 'term' => $search]);
72                 }
73
74                 $a->page['aside'] .= search_saved_searches();
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 (Config::get('system','block_public') && !local_user() && !remote_user()) {
96                 notice(t('Public access denied.') . EOL);
97                 return;
98         }
99
100         if (Config::get('system','local_search') && !local_user() && !remote_user()) {
101                 http_status_exit(403,
102                                 ["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 (Config::get('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(Config::get('system','free_crawls'));
114                 if ($free_crawls == 0)
115                         $free_crawls = 10;
116
117                 $crawl_permit_period = intval(Config::get('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                                                 ["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(["time" => time(), "accesses" => $resultdata->accesses + 1]), CACHE_HOUR);
132                 } else
133                         Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), CACHE_HOUR);
134         }
135
136         Nav::setSelected('search');
137
138         $search = '';
139         if (x($a->data,'search'))
140                 $search = notags(trim($a->data['search']));
141         else
142                 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
143
144         $tag = false;
145         if (x($_GET,'tag')) {
146                 $tag = true;
147                 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
148         }
149
150         // contruct a wrapper for the search header
151         $o .= replace_macros(get_markup_template("content_wrapper.tpl"),[
152                 'name' => "search-header",
153                 '$title' => t("Search"),
154                 '$title_size' => 3,
155                 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
156         ]);
157
158         if (strpos($search,'#') === 0) {
159                 $tag = true;
160                 $search = substr($search,1);
161         }
162         if (strpos($search,'@') === 0) {
163                 return dirfind_content($a);
164         }
165         if (strpos($search,'!') === 0) {
166                 return dirfind_content($a);
167         }
168
169         if (x($_GET,'search-option'))
170                 switch($_GET['search-option']) {
171                         case 'fulltext':
172                                 break;
173                         case 'tags':
174                                 $tag = true;
175                                 break;
176                         case 'contacts':
177                                 return dirfind_content($a, "@");
178                                 break;
179                         case 'forums':
180                                 return dirfind_content($a, "!");
181                                 break;
182                 }
183
184         if (! $search)
185                 return $o;
186
187         if (Config::get('system','only_tag_search'))
188                 $tag = true;
189
190         // Here is the way permissions work in the search module...
191         // Only public posts can be shown
192         // OR your own posts if you are a logged in member
193         // No items will be shown if the member has a blocked profile wall.
194
195         if ($tag) {
196                 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
197
198                 $r = q("SELECT %s
199                         FROM `term`
200                                 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
201                         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'
202                         ORDER BY term.created DESC LIMIT %d , %d ",
203                                 item_fieldlists(), item_joins(), item_condition(),
204                                 intval(local_user()),
205                                 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
206                                 intval($a->pager['start']), intval($a->pager['itemspage']));
207         } else {
208                 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
209
210                 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
211
212                 $r = q("SELECT %s
213                         FROM `item` %s
214                         WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
215                                 $sql_extra
216                         GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
217                                 item_fieldlists(), item_joins(), item_condition(),
218                                 intval(local_user()),
219                                 intval($a->pager['start']), intval($a->pager['itemspage']));
220         }
221
222         if (! DBM::is_result($r)) {
223                 info( t('No results.') . EOL);
224                 return $o;
225         }
226
227
228         if ($tag)
229                 $title = sprintf( t('Items tagged with: %s'), $search);
230         else
231                 $title = sprintf( t('Results for: %s'), $search);
232
233         $o .= replace_macros(get_markup_template("section_title.tpl"),[
234                 '$title' => $title
235         ]);
236
237         logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
238         $o .= conversation($a,$r,'search',false);
239
240         $o .= alt_pager($a,count($r));
241
242         logger("Done '".$search."'", LOGGER_DEBUG);
243
244         return $o;
245 }
246