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