4 use Friendica\Core\Cache;
5 use Friendica\Core\Config;
6 use Friendica\Database\DBM;
8 require_once("include/bbcode.php");
9 require_once('include/security.php');
10 require_once('include/conversation.php');
11 require_once('mod/dirfind.php');
13 function search_saved_searches() {
17 if (! feature_enabled(local_user(),'savedsearch'))
20 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
24 if (DBM::is_result($r)) {
29 'term' => $rr['term'],
30 'encodedterm' => urlencode($rr['term']),
31 'delete' => t('Remove term'),
32 'selected' => ($search==$rr['term']),
37 $tpl = get_markup_template("saved_searches_aside.tpl");
39 $o .= replace_macros($tpl, array(
40 '$title' => t('Saved Searches'),
52 function search_init(App $a) {
54 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
57 if (x($_GET,'save') && $search) {
58 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
62 if (!DBM::is_result($r)) {
63 dba::insert('search', array('uid' => local_user(), 'term' => $search));
66 if (x($_GET,'remove') && $search) {
67 dba::delete('search', array('uid' => local_user(), 'term' => $search));
70 $a->page['aside'] .= search_saved_searches();
73 unset($_SESSION['theme']);
74 unset($_SESSION['mobile-theme']);
83 function search_post(App $a) {
84 if (x($_POST,'search'))
85 $a->data['search'] = $_POST['search'];
89 function search_content(App $a) {
91 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
92 notice(t('Public access denied.') . EOL);
96 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
98 array("title" => t("Public access denied."),
99 "description" => t("Only logged in users are permitted to perform a search.")));
101 //notice(t('Public access denied.').EOL);
105 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
107 // 10 requests are "free", after the 11th only a call per minute is allowed
109 $free_crawls = intval(Config::get('system','free_crawls'));
110 if ($free_crawls == 0)
113 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
114 if ($crawl_permit_period == 0)
115 $crawl_permit_period = 10;
117 $remote = $_SERVER["REMOTE_ADDR"];
118 $result = Cache::get("remote_search:".$remote);
119 if (!is_null($result)) {
120 $resultdata = json_decode($result);
121 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
122 http_status_exit(429,
123 array("title" => t("Too Many Requests"),
124 "description" => t("Only one search per minute is permitted for not logged in users.")));
127 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
129 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
132 nav_set_selected('search');
134 if (x($a->data,'search'))
135 $search = notags(trim($a->data['search']));
137 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
140 if (x($_GET,'tag')) {
142 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
145 // contruct a wrapper for the search header
146 $o .= replace_macros(get_markup_template("content_wrapper.tpl"),array(
147 'name' => "search-header",
148 '$title' => t("Search"),
150 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
153 if (strpos($search,'#') === 0) {
155 $search = substr($search,1);
157 if (strpos($search,'@') === 0) {
158 return dirfind_content($a);
160 if (strpos($search,'!') === 0) {
161 return dirfind_content($a);
164 if (x($_GET,'search-option'))
165 switch($_GET['search-option']) {
172 return dirfind_content($a, "@");
175 return dirfind_content($a, "!");
182 if (Config::get('system','only_tag_search'))
185 // Here is the way permissions work in the search module...
186 // Only public posts can be shown
187 // OR your own posts if you are a logged in member
188 // No items will be shown if the member has a blocked profile wall.
191 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
195 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
196 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'
197 ORDER BY term.created DESC LIMIT %d , %d ",
198 item_fieldlists(), item_joins(), item_condition(),
199 intval(local_user()),
200 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
201 intval($a->pager['start']), intval($a->pager['itemspage']));
203 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
205 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
209 WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
211 GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
212 item_fieldlists(), item_joins(), item_condition(),
213 intval(local_user()),
214 intval($a->pager['start']), intval($a->pager['itemspage']));
217 if (! DBM::is_result($r)) {
218 info( t('No results.') . EOL);
224 $title = sprintf( t('Items tagged with: %s'), $search);
226 $title = sprintf( t('Results for: %s'), $search);
228 $o .= replace_macros(get_markup_template("section_title.tpl"),array(
232 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
233 $o .= conversation($a,$r,'search',false);
235 $o .= alt_pager($a,count($r));
237 logger("Done '".$search."'", LOGGER_DEBUG);