6 use Friendica\Content\Feature;
7 use Friendica\Core\Cache;
8 use Friendica\Core\Config;
9 use Friendica\Database\DBM;
11 require_once "include/bbcode.php";
12 require_once 'include/security.php';
13 require_once 'include/conversation.php';
14 require_once 'mod/dirfind.php';
16 function search_saved_searches() {
20 if (! Feature::isEnabled(local_user(),'savedsearch'))
23 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
27 if (DBM::is_result($r)) {
32 'term' => $rr['term'],
33 'encodedterm' => urlencode($rr['term']),
34 'delete' => t('Remove term'),
35 'selected' => ($search==$rr['term']),
40 $tpl = get_markup_template("saved_searches_aside.tpl");
42 $o .= replace_macros($tpl, array(
43 '$title' => t('Saved Searches'),
55 function search_init(App $a) {
57 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
60 if (x($_GET,'save') && $search) {
61 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
65 if (!DBM::is_result($r)) {
66 dba::insert('search', array('uid' => local_user(), 'term' => $search));
69 if (x($_GET,'remove') && $search) {
70 dba::delete('search', array('uid' => local_user(), 'term' => $search));
73 $a->page['aside'] .= search_saved_searches();
76 unset($_SESSION['theme']);
77 unset($_SESSION['mobile-theme']);
86 function search_post(App $a) {
87 if (x($_POST,'search'))
88 $a->data['search'] = $_POST['search'];
92 function search_content(App $a) {
94 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
95 notice(t('Public access denied.') . EOL);
99 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
100 http_status_exit(403,
101 array("title" => t("Public access denied."),
102 "description" => t("Only logged in users are permitted to perform a search.")));
104 //notice(t('Public access denied.').EOL);
108 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
110 // 10 requests are "free", after the 11th only a call per minute is allowed
112 $free_crawls = intval(Config::get('system','free_crawls'));
113 if ($free_crawls == 0)
116 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
117 if ($crawl_permit_period == 0)
118 $crawl_permit_period = 10;
120 $remote = $_SERVER["REMOTE_ADDR"];
121 $result = Cache::get("remote_search:".$remote);
122 if (!is_null($result)) {
123 $resultdata = json_decode($result);
124 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
125 http_status_exit(429,
126 array("title" => t("Too Many Requests"),
127 "description" => t("Only one search per minute is permitted for not logged in users.")));
130 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
132 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
135 nav_set_selected('search');
137 if (x($a->data,'search'))
138 $search = notags(trim($a->data['search']));
140 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
143 if (x($_GET,'tag')) {
145 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
148 // contruct a wrapper for the search header
149 $o .= replace_macros(get_markup_template("content_wrapper.tpl"),array(
150 'name' => "search-header",
151 '$title' => t("Search"),
153 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
156 if (strpos($search,'#') === 0) {
158 $search = substr($search,1);
160 if (strpos($search,'@') === 0) {
161 return dirfind_content($a);
163 if (strpos($search,'!') === 0) {
164 return dirfind_content($a);
167 if (x($_GET,'search-option'))
168 switch($_GET['search-option']) {
175 return dirfind_content($a, "@");
178 return dirfind_content($a, "!");
185 if (Config::get('system','only_tag_search'))
188 // Here is the way permissions work in the search module...
189 // Only public posts can be shown
190 // OR your own posts if you are a logged in member
191 // No items will be shown if the member has a blocked profile wall.
194 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
198 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
199 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'
200 ORDER BY term.created DESC LIMIT %d , %d ",
201 item_fieldlists(), item_joins(), item_condition(),
202 intval(local_user()),
203 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
204 intval($a->pager['start']), intval($a->pager['itemspage']));
206 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
208 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
212 WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
214 GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
215 item_fieldlists(), item_joins(), item_condition(),
216 intval(local_user()),
217 intval($a->pager['start']), intval($a->pager['itemspage']));
220 if (! DBM::is_result($r)) {
221 info( t('No results.') . EOL);
227 $title = sprintf( t('Items tagged with: %s'), $search);
229 $title = sprintf( t('Results for: %s'), $search);
231 $o .= replace_macros(get_markup_template("section_title.tpl"),array(
235 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
236 $o .= conversation($a,$r,'search',false);
238 $o .= alt_pager($a,count($r));
240 logger("Done '".$search."'", LOGGER_DEBUG);