3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Protocol;
25 use Friendica\Content\Text\HTML;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Worker;
29 use Friendica\Database\DBA;
31 use Friendica\Model\GContact;
32 use Friendica\Model\GServer;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Strings;
38 * @todo Move GNU Social URL schemata (http://server.tld/user/number) to http://server.tld/username
39 * @todo Fetch profile data from profile page for Redmatrix users
40 * @todo Detect if it is a forum
46 const USERS_GCONTACTS = 2;
47 const USERS_GCONTACTS_FALLBACK = 3;
52 * @param integer $cid Contact ID
53 * @param integer $uid User ID
54 * @param integer $zcid Global Contact ID
55 * @param integer $url POCO address that should be polled
57 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
58 * and add the entries to the gcontact (Global Contact) table, or update existing entries
59 * if anything (name or photo) has changed.
60 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
62 * Once the global contact is stored add (if necessary) the contact linkage which associates
63 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
64 * pointing to the same global contact id.
65 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
67 public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null)
69 // Call the function "load" via the worker
70 Worker::add(PRIORITY_LOW, 'FetchPoCo', (int)$cid, (int)$uid, (int)$zcid, $url);
74 * Fetch POCO data from the worker
76 * @param integer $cid Contact ID
77 * @param integer $uid User ID
78 * @param integer $zcid Global Contact ID
79 * @param integer $url POCO address that should be polled
80 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
82 public static function load($cid, $uid, $zcid, $url)
86 $contact = DBA::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
87 if (DBA::isResult($contact)) {
88 $url = $contact['poco'];
89 $uid = $contact['uid'];
101 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,contactType,generation');
103 Logger::log('load: ' . $url, Logger::DEBUG);
105 $fetchresult = DI::httpRequest()->fetchFull($url);
106 $s = $fetchresult->getBody();
108 Logger::log('load: returns ' . $s, Logger::DATA);
110 Logger::log('load: return code: ' . $fetchresult->getReturnCode(), Logger::DEBUG);
112 if (($fetchresult->getReturnCode() > 299) || (! $s)) {
116 $j = json_decode($s, true);
118 Logger::debug('load', ['json' => $j]);
120 if (!isset($j['entry'])) {
125 foreach ($j['entry'] as $entry) {
132 $updated = DBA::NULL_DATETIME;
139 if (!empty($entry['displayName'])) {
140 $name = $entry['displayName'];
143 if (isset($entry['urls'])) {
144 foreach ($entry['urls'] as $url) {
145 if ($url['type'] == 'profile') {
146 $profile_url = $url['value'];
149 if ($url['type'] == 'webfinger') {
150 $connect_url = str_replace('acct:', '', $url['value']);
155 if (isset($entry['photos'])) {
156 foreach ($entry['photos'] as $photo) {
157 if ($photo['type'] == 'profile') {
158 $profile_photo = $photo['value'];
164 if (isset($entry['updated'])) {
165 $updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
168 if (isset($entry['network'])) {
169 $network = $entry['network'];
172 if (isset($entry['currentLocation'])) {
173 $location = $entry['currentLocation'];
176 if (isset($entry['aboutMe'])) {
177 $about = HTML::toBBCode($entry['aboutMe']);
180 if (isset($entry['generation']) && ($entry['generation'] > 0)) {
181 $generation = ++$entry['generation'];
184 if (isset($entry['tags'])) {
185 foreach ($entry['tags'] as $tag) {
186 $keywords = implode(", ", $tag);
190 if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
191 $contact_type = $entry['contactType'];
194 $gcontact = ["url" => $profile_url,
196 "network" => $network,
197 "photo" => $profile_photo,
199 "location" => $location,
200 "keywords" => $keywords,
201 "connect" => $connect_url,
202 "updated" => $updated,
203 "contact-type" => $contact_type,
204 "generation" => $generation];
207 $gcontact = GContact::sanitize($gcontact);
208 $gcid = GContact::update($gcontact);
210 GContact::link($gcid, $uid, $cid, $zcid);
211 } catch (Exception $e) {
212 Logger::log($e->getMessage(), Logger::DEBUG);
215 Logger::log("load: loaded $total entries", Logger::DEBUG);
217 $condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
218 DBA::delete('glink', $condition);
222 * Returns a list of all known servers
223 * @return array List of server urls
226 public static function serverlist()
229 "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
230 WHERE `network` IN ('%s', '%s', '%s') AND NOT `failed`
231 ORDER BY `last_contact`
233 DBA::escape(Protocol::DFRN),
234 DBA::escape(Protocol::DIASPORA),
235 DBA::escape(Protocol::OSTATUS)
238 if (!DBA::isResult($r)) {
246 * Fetch server list from remote servers and adds them when they are new.
248 * @param string $poco URL to the POCO endpoint
249 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
251 private static function fetchServerlist($poco)
253 $curlResult = DI::httpRequest()->get($poco . "/@server");
255 if (!$curlResult->isSuccess()) {
259 $serverlist = json_decode($curlResult->getBody(), true);
261 if (!is_array($serverlist)) {
265 foreach ($serverlist as $server) {
266 $server_url = str_replace("/index.php", "", $server['url']);
268 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", DBA::escape(Strings::normaliseLink($server_url)));
270 if (!DBA::isResult($r)) {
271 Logger::log("Call server check for server ".$server_url, Logger::DEBUG);
272 Worker::add(PRIORITY_LOW, 'UpdateGServer', $server_url);
277 public static function discoverSingleServer($id)
279 $server = DBA::selectFirst('gserver', ['poco', 'nurl', 'url', 'network'], ['id' => $id]);
281 if (!DBA::isResult($server)) {
285 // Discover new servers out there (Works from Friendica version 3.5.2)
286 self::fetchServerlist($server["poco"]);
288 // Fetch all users from the other server
289 $url = $server["poco"] . "/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,contactType,generation";
291 Logger::info("Fetch all users from the server " . $server["url"]);
293 $curlResult = DI::httpRequest()->get($url);
295 if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
296 $data = json_decode($curlResult->getBody(), true);
299 self::discoverServer($data, 2);
302 if (DI::config()->get('system', 'poco_discovery') >= self::USERS_GCONTACTS) {
303 $timeframe = DI::config()->get('system', 'poco_discovery_since');
305 if ($timeframe == 0) {
309 $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
311 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
312 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,contactType,generation";
316 $curlResult = DI::httpRequest()->get($url);
318 if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
319 Logger::info("Fetch all global contacts from the server " . $server["nurl"]);
320 $data = json_decode($curlResult->getBody(), true);
323 $success = self::discoverServer($data);
327 if (!$success && !empty($data) && DI::config()->get('system', 'poco_discovery') >= self::USERS_GCONTACTS_FALLBACK) {
328 Logger::info("Fetch contacts from users of the server " . $server["nurl"]);
329 self::discoverServerUsers($data, $server);
333 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
334 DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
338 // If the server hadn't replied correctly, then force a sanity check
339 GServer::check($server["url"], $server["network"], true);
341 // If we couldn't reach the server, we will try it some time later
342 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
343 DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
349 private static function discoverServerUsers(array $data, array $server)
351 if (!isset($data['entry'])) {
355 foreach ($data['entry'] as $entry) {
358 if (isset($entry['urls'])) {
359 foreach ($entry['urls'] as $url) {
360 if ($url['type'] == 'profile') {
361 $profile_url = $url['value'];
362 $path_array = explode('/', parse_url($profile_url, PHP_URL_PATH));
363 $username = end($path_array);
368 if ($username != '') {
369 Logger::log('Fetch contacts for the user ' . $username . ' from the server ' . $server['nurl'], Logger::DEBUG);
371 // Fetch all contacts from a given user from the other server
372 $url = $server['poco'] . '/' . $username . '/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,contactType,generation';
374 $curlResult = DI::httpRequest()->get($url);
376 if ($curlResult->isSuccess()) {
377 $data = json_decode($curlResult->getBody(), true);
380 self::discoverServer($data, 3);
387 private static function discoverServer(array $data, $default_generation = 0)
389 if (empty($data['entry'])) {
395 foreach ($data['entry'] as $entry) {
401 $updated = DBA::NULL_DATETIME;
406 $generation = $default_generation;
408 if (!empty($entry['displayName'])) {
409 $name = $entry['displayName'];
412 if (isset($entry['urls'])) {
413 foreach ($entry['urls'] as $url) {
414 if ($url['type'] == 'profile') {
415 $profile_url = $url['value'];
418 if ($url['type'] == 'webfinger') {
419 $connect_url = str_replace('acct:' , '', $url['value']);
425 if (isset($entry['photos'])) {
426 foreach ($entry['photos'] as $photo) {
427 if ($photo['type'] == 'profile') {
428 $profile_photo = $photo['value'];
434 if (isset($entry['updated'])) {
435 $updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
438 if (isset($entry['network'])) {
439 $network = $entry['network'];
442 if (isset($entry['currentLocation'])) {
443 $location = $entry['currentLocation'];
446 if (isset($entry['aboutMe'])) {
447 $about = HTML::toBBCode($entry['aboutMe']);
450 if (isset($entry['generation']) && ($entry['generation'] > 0)) {
451 $generation = ++$entry['generation'];
454 if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
455 $contact_type = $entry['contactType'];
458 if (isset($entry['tags'])) {
459 foreach ($entry['tags'] as $tag) {
460 $keywords = implode(", ", $tag);
464 if ($generation > 0) {
467 Logger::log("Store profile ".$profile_url, Logger::DEBUG);
469 $gcontact = ["url" => $profile_url,
471 "network" => $network,
472 "photo" => $profile_photo,
474 "location" => $location,
475 "keywords" => $keywords,
476 "connect" => $connect_url,
477 "updated" => $updated,
478 "contact-type" => $contact_type,
479 "generation" => $generation];
482 $gcontact = GContact::sanitize($gcontact);
483 GContact::update($gcontact);
484 } catch (Exception $e) {
485 Logger::log($e->getMessage(), Logger::DEBUG);
488 Logger::log("Done for profile ".$profile_url, Logger::DEBUG);