]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Ignore obvious garbage results
[friendica.git] / mod / dirfind.php
1 <?php
2 /**
3  * @file mod/dirfind.php
4  */
5 use Friendica\App;
6 use Friendica\Content\ContactSelector;
7 use Friendica\Content\Widget;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Core\Worker;
12 use Friendica\Model\Contact;
13 use Friendica\Model\GContact;
14 use Friendica\Model\Profile;
15 use Friendica\Network\Probe;
16 use Friendica\Protocol\PortableContact;
17 use Friendica\Util\Network;
18 use Friendica\Database\DBM;
19
20 require_once 'mod/contacts.php';
21
22 function dirfind_init(App $a) {
23
24         if (! local_user()) {
25                 notice(L10n::t('Permission denied.') . EOL );
26                 return;
27         }
28
29         if (! x($a->page,'aside')) {
30                 $a->page['aside'] = '';
31         }
32
33         $a->page['aside'] .= Widget::findPeople();
34
35         $a->page['aside'] .= Widget::follow();
36 }
37
38 function dirfind_content(App $a, $prefix = "") {
39
40         $community = false;
41         $discover_user = false;
42
43         $local = Config::get('system','poco_local_search');
44
45         $search = $prefix.notags(trim($_REQUEST['search']));
46
47         if (strpos($search,'@') === 0) {
48                 $search = substr($search,1);
49                 $header = L10n::t('People Search - %s', $search);
50                 if ((valid_email($search) && Network::isEmailDomainValid($search)) ||
51                         (substr(normalise_link($search), 0, 7) == "http://")) {
52                         $user_data = Probe::uri($search);
53                         $discover_user = (in_array($user_data["network"], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA]));
54                 }
55         }
56
57         if (strpos($search,'!') === 0) {
58                 $search = substr($search,1);
59                 $community = true;
60                 $header = L10n::t('Forum Search - %s', $search);
61         }
62
63         $o = '';
64
65         if ($search) {
66
67                 if ($discover_user) {
68                         $j = new stdClass();
69                         $j->total = 1;
70                         $j->items_page = 1;
71                         $j->page = $a->pager['page'];
72
73                         $objresult = new stdClass();
74                         $objresult->cid = 0;
75                         $objresult->name = $user_data["name"];
76                         $objresult->addr = $user_data["addr"];
77                         $objresult->url = $user_data["url"];
78                         $objresult->photo = $user_data["photo"];
79                         $objresult->tags = "";
80                         $objresult->network = $user_data["network"];
81
82                         $contact = Contact::getDetailsByURL($user_data["url"], local_user());
83                         $objresult->cid = $contact["cid"];
84
85                         $j->results[] = $objresult;
86
87                         // Add the contact to the global contacts if it isn't already in our system
88                         if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
89                                 GContact::update($user_data);
90                         }
91                 } elseif ($local) {
92
93                         if ($community)
94                                 $extra_sql = " AND `community`";
95                         else
96                                 $extra_sql = "";
97
98                         $perpage = 80;
99                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
100
101                         if (Config::get('system','diaspora_enabled')) {
102                                 $diaspora = NETWORK_DIASPORA;
103                         } else {
104                                 $diaspora = NETWORK_DFRN;
105                         }
106
107                         if (!Config::get('system','ostatus_disabled')) {
108                                 $ostatus = NETWORK_OSTATUS;
109                         } else {
110                                 $ostatus = NETWORK_DFRN;
111                         }
112
113                         $search2 = "%".$search."%";
114
115                         /// @TODO These 2 SELECTs are not checked on validity with DBM::is_result()
116                         $count = q("SELECT count(*) AS `total` FROM `gcontact`
117                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = 0
118                                         WHERE NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
119                                                 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR
120                                                 (`gcontact`.`updated` >= `gcontact`.`last_failure`)) AND
121                                                 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR
122                                                 `gcontact`.`location` LIKE '%s' OR `gcontact`.`addr` LIKE '%s' OR
123                                                 `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
124                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
125                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
126                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
127
128                         $results = q("SELECT `gcontact`.`nurl`
129                                         FROM `gcontact`
130                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = 0
131                                         WHERE NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
132                                                 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR
133                                                 (`gcontact`.`updated` >= `gcontact`.`last_failure`)) AND
134                                                 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR
135                                                 `gcontact`.`location` LIKE '%s' OR `gcontact`.`addr` LIKE '%s' OR
136                                                 `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
137                                                 GROUP BY `gcontact`.`nurl`
138                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
139                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
140                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
141                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
142                                         intval($startrec), intval($perpage));
143                         $j = new stdClass();
144                         $j->total = $count[0]["total"];
145                         $j->items_page = $perpage;
146                         $j->page = $a->pager['page'];
147                         foreach ($results AS $result) {
148                                 if (PortableContact::alternateOStatusUrl($result["nurl"])) {
149                                         continue;
150                                 }
151
152                                 $urlparts = parse_url($result["nurl"]);
153
154                                 // Ignore results that look strange.
155                                 // For historic reasons the gcontact table does contain some garbage.
156                                 if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
157                                         continue;
158                                 }
159
160                                 $result = Contact::getDetailsByURL($result["nurl"], local_user());
161
162                                 if ($result["name"] == "") {
163                                         $result["name"] = end(explode("/", $urlparts["path"]));
164                                 }
165
166                                 $objresult = new stdClass();
167                                 $objresult->cid = $result["cid"];
168                                 $objresult->name = $result["name"];
169                                 $objresult->addr = $result["addr"];
170                                 $objresult->url = $result["url"];
171                                 $objresult->photo = $result["photo"];
172                                 $objresult->tags = $result["keywords"];
173                                 $objresult->network = $result["network"];
174
175                                 $j->results[] = $objresult;
176                         }
177
178                         // Add found profiles from the global directory to the local directory
179                         Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
180                 } else {
181
182                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
183
184                         if(strlen(Config::get('system','directory')))
185                                 $x = Network::fetchUrl(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
186
187                         $j = json_decode($x);
188                 }
189
190                 if ($j->total) {
191                         $a->set_pager_total($j->total);
192                         $a->set_pager_itemspage($j->items_page);
193                 }
194
195                 if (count($j->results)) {
196
197                         $id = 0;
198
199                         foreach ($j->results as $jj) {
200
201                                 $alt_text = "";
202
203                                 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
204
205                                 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
206
207                                 // If We already know this contact then don't show the "connect" button
208                                 if ($jj->cid > 0) {
209                                         $connlnk = "";
210                                         $conntxt = "";
211                                         $contact = dba::selectFirst('contact', [], ['id' => $jj->cid]);
212                                         if (DBM::is_result($contact)) {
213                                                 $photo_menu = Contact::photoMenu($contact);
214                                                 $details = _contact_detail_for_template($contact);
215                                                 $alt_text = $details['alt_text'];
216                                         } else {
217                                                 $photo_menu = [];
218                                         }
219                                 } else {
220                                         $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
221                                         $conntxt = L10n::t('Connect');
222                                         $photo_menu = [
223                                                 'profile' => [L10n::t("View Profile"), Profile::zrl($jj->url)],
224                                                 'follow' => [L10n::t("Connect/Follow"), $connlnk]
225                                         ];
226                                 }
227
228                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
229
230                                 $entry = [
231                                         'alt_text' => $alt_text,
232                                         'url' => Profile::zrl($jj->url),
233                                         'itemurl' => $itemurl,
234                                         'name' => htmlentities($jj->name),
235                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
236                                         'img_hover' => $jj->tags,
237                                         'conntxt' => $conntxt,
238                                         'connlnk' => $connlnk,
239                                         'photo_menu' => $photo_menu,
240                                         'details'       => $contact_details['location'],
241                                         'tags'          => $contact_details['keywords'],
242                                         'about'         => $contact_details['about'],
243                                         'account_type'  => Contact::getAccountType($contact_details),
244                                         'network' => ContactSelector::networkToName($jj->network, $jj->url),
245                                         'id' => ++$id,
246                                 ];
247                                 $entries[] = $entry;
248                         }
249
250                 $tpl = get_markup_template('viewcontact_template.tpl');
251
252                 $o .= replace_macros($tpl,[
253                         'title' => $header,
254                         '$contacts' => $entries,
255                         '$paginate' => paginate($a),
256                 ]);
257
258                 } else {
259                         info(L10n::t('No matches') . EOL);
260                 }
261
262         }
263
264         return $o;
265 }