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