4 use Friendica\Core\Config;
5 use Friendica\Database\DBM;
7 require_once("include/bbcode.php");
8 require_once('include/security.php');
9 require_once('include/conversation.php');
10 require_once('mod/dirfind.php');
12 function search_saved_searches() {
16 if (! feature_enabled(local_user(),'savedsearch'))
19 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
23 if (DBM::is_result($r)) {
28 'term' => $rr['term'],
29 'encodedterm' => urlencode($rr['term']),
30 'delete' => t('Remove term'),
31 'selected' => ($search==$rr['term']),
36 $tpl = get_markup_template("saved_searches_aside.tpl");
38 $o .= replace_macros($tpl, array(
39 '$title' => t('Saved Searches'),
51 function search_init(App $a) {
53 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
56 if (x($_GET,'save') && $search) {
57 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
61 if (!DBM::is_result($r)) {
62 dba::insert('search', array('uid' => local_user(), 'term' => $search));
65 if (x($_GET,'remove') && $search) {
66 dba::delete('search', array('uid' => local_user(), 'term' => $search));
69 $a->page['aside'] .= search_saved_searches();
72 unset($_SESSION['theme']);
73 unset($_SESSION['mobile-theme']);
82 function search_post(App $a) {
83 if (x($_POST,'search'))
84 $a->data['search'] = $_POST['search'];
88 function search_content(App $a) {
90 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
91 notice(t('Public access denied.') . EOL);
95 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
97 array("title" => t("Public access denied."),
98 "description" => t("Only logged in users are permitted to perform a search.")));
100 //notice(t('Public access denied.').EOL);
104 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
106 // 10 requests are "free", after the 11th only a call per minute is allowed
108 $free_crawls = intval(Config::get('system','free_crawls'));
109 if ($free_crawls == 0)
112 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
113 if ($crawl_permit_period == 0)
114 $crawl_permit_period = 10;
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.")));
126 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
128 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
131 nav_set_selected('search');
133 if (x($a->data,'search'))
134 $search = notags(trim($a->data['search']));
136 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
139 if (x($_GET,'tag')) {
141 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
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"),
149 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
152 if (strpos($search,'#') === 0) {
154 $search = substr($search,1);
156 if (strpos($search,'@') === 0) {
157 return dirfind_content($a);
159 if (strpos($search,'!') === 0) {
160 return dirfind_content($a);
163 if (x($_GET,'search-option'))
164 switch($_GET['search-option']) {
171 return dirfind_content($a, "@");
174 return dirfind_content($a, "!");
181 if (Config::get('system','only_tag_search'))
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.
190 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
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']));
202 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
204 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
208 WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
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']));
216 if (! DBM::is_result($r)) {
217 info( t('No results.') . EOL);
223 $title = sprintf( t('Items tagged with: %s'), $search);
225 $title = sprintf( t('Results for: %s'), $search);
227 $o .= replace_macros(get_markup_template("section_title.tpl"),array(
231 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
232 $o .= conversation($a,$r,'search',false);
234 $o .= alt_pager($a,count($r));
236 logger("Done '".$search."'", LOGGER_DEBUG);