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