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