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