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