]> git.mxchange.org Git - friendica.git/blob - mod/search.php
c17b11c5d9a46c3025d143232da52a6c68e2a30c
[friendica.git] / mod / search.php
1 <?php
2 /**
3  * @file mod/search.php
4  */
5 use Friendica\App;
6 use Friendica\Content\Feature;
7 use Friendica\Core\Cache;
8 use Friendica\Core\Config;
9 use Friendica\Database\DBM;
10
11 require_once "include/bbcode.php";
12 require_once 'include/security.php';
13 require_once 'include/conversation.php';
14 require_once 'mod/dirfind.php';
15
16 function search_saved_searches() {
17
18         $o = '';
19
20         if (! Feature::isEnabled(local_user(),'savedsearch'))
21                 return $o;
22
23         $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
24                 intval(local_user())
25         );
26
27         if (DBM::is_result($r)) {
28                 $saved = array();
29                 foreach ($r as $rr) {
30                         $saved[] = array(
31                                 'id'            => $rr['id'],
32                                 'term'          => $rr['term'],
33                                 'encodedterm'   => urlencode($rr['term']),
34                                 'delete'        => t('Remove term'),
35                                 'selected'      => ($search==$rr['term']),
36                         );
37                 }
38
39
40                 $tpl = get_markup_template("saved_searches_aside.tpl");
41
42                 $o .= replace_macros($tpl, array(
43                         '$title'        => t('Saved Searches'),
44                         '$add'          => '',
45                         '$searchbox'    => '',
46                         '$saved'        => $saved,
47                 ));
48         }
49
50         return $o;
51
52 }
53
54
55 function search_init(App $a) {
56
57         $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
58
59         if (local_user()) {
60                 if (x($_GET,'save') && $search) {
61                         $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
62                                 intval(local_user()),
63                                 dbesc($search)
64                         );
65                         if (!DBM::is_result($r)) {
66                                 dba::insert('search', array('uid' => local_user(), 'term' => $search));
67                         }
68                 }
69                 if (x($_GET,'remove') && $search) {
70                         dba::delete('search', array('uid' => local_user(), 'term' => $search));
71                 }
72
73                 $a->page['aside'] .= search_saved_searches();
74
75         } else {
76                 unset($_SESSION['theme']);
77                 unset($_SESSION['mobile-theme']);
78         }
79
80
81
82 }
83
84
85
86 function search_post(App $a) {
87         if (x($_POST,'search'))
88                 $a->data['search'] = $_POST['search'];
89 }
90
91
92 function search_content(App $a) {
93
94         if (Config::get('system','block_public') && !local_user() && !remote_user()) {
95                 notice(t('Public access denied.') . EOL);
96                 return;
97         }
98
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.")));
103                 killme();
104                 //notice(t('Public access denied.').EOL);
105                 //return;
106         }
107
108         if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
109                 // Default values:
110                 // 10 requests are "free", after the 11th only a call per minute is allowed
111
112                 $free_crawls = intval(Config::get('system','free_crawls'));
113                 if ($free_crawls == 0)
114                         $free_crawls = 10;
115
116                 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
117                 if ($crawl_permit_period == 0)
118                         $crawl_permit_period = 10;
119
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.")));
128                                 killme();
129                         }
130                         Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
131                 } else
132                         Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
133         }
134
135         nav_set_selected('search');
136
137         if (x($a->data,'search'))
138                 $search = notags(trim($a->data['search']));
139         else
140                 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
141
142         $tag = false;
143         if (x($_GET,'tag')) {
144                 $tag = true;
145                 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
146         }
147
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"),
152                 '$title_size' => 3,
153                 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
154         ));
155
156         if (strpos($search,'#') === 0) {
157                 $tag = true;
158                 $search = substr($search,1);
159         }
160         if (strpos($search,'@') === 0) {
161                 return dirfind_content($a);
162         }
163         if (strpos($search,'!') === 0) {
164                 return dirfind_content($a);
165         }
166
167         if (x($_GET,'search-option'))
168                 switch($_GET['search-option']) {
169                         case 'fulltext':
170                                 break;
171                         case 'tags':
172                                 $tag = true;
173                                 break;
174                         case 'contacts':
175                                 return dirfind_content($a, "@");
176                                 break;
177                         case 'forums':
178                                 return dirfind_content($a, "!");
179                                 break;
180                 }
181
182         if (! $search)
183                 return $o;
184
185         if (Config::get('system','only_tag_search'))
186                 $tag = true;
187
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.
192
193         if ($tag) {
194                 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
195
196                 $r = q("SELECT %s
197                         FROM `term`
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']));
205         } else {
206                 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
207
208                 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
209
210                 $r = q("SELECT %s
211                         FROM `item` %s
212                         WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
213                                 $sql_extra
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']));
218         }
219
220         if (! DBM::is_result($r)) {
221                 info( t('No results.') . EOL);
222                 return $o;
223         }
224
225
226         if ($tag)
227                 $title = sprintf( t('Items tagged with: %s'), $search);
228         else
229                 $title = sprintf( t('Results for: %s'), $search);
230
231         $o .= replace_macros(get_markup_template("section_title.tpl"),array(
232                 '$title' => $title
233         ));
234
235         logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
236         $o .= conversation($a,$r,'search',false);
237
238         $o .= alt_pager($a,count($r));
239
240         logger("Done '".$search."'", LOGGER_DEBUG);
241
242         return $o;
243 }
244