X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FPortableContact.php;h=def8f1b628bc3b667636843c9d701f2bf331c5fb;hb=95827140891f45646ab53c6a850a3ea2389ce6e9;hp=8ce732bfcc6d53bb78deab410ae8032f3177e3b0;hpb=2dec8895a99070281c2a13991b484c96c6e0cbed;p=friendica.git diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index 8ce732bfcc..def8f1b628 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -14,20 +14,26 @@ use DOMXPath; use Exception; use Friendica\Content\Text\HTML; use Friendica\Core\Config; +use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\Model\GContact; use Friendica\Model\Profile; +use Friendica\Module\Register; use Friendica\Network\Probe; use Friendica\Util\DateTimeFormat; use Friendica\Util\Network; +use Friendica\Util\Strings; use Friendica\Util\XML; -require_once 'include/dba.php'; - class PortableContact { + const DISABLED = 0; + const USERS = 1; + const USERS_GCONTACTS = 2; + const USERS_GCONTACTS_FALLBACK = 3; + /** * @brief Fetch POCO data * @@ -44,7 +50,7 @@ class PortableContact * Once the global contact is stored add (if necessary) the contact linkage which associates * the given uid, cid to the global contact entry. There can be many uid/cid combinations * pointing to the same global contact id. - * + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null) { @@ -59,12 +65,10 @@ class PortableContact * @param integer $uid User ID * @param integer $zcid Global Contact ID * @param integer $url POCO address that should be polled - * + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function load($cid, $uid, $zcid, $url) { - $a = get_app(); - if ($cid) { if (!$url || !$uid) { $contact = DBA::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]); @@ -82,23 +86,24 @@ class PortableContact return; } - $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation') ; + $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation'); - logger('load: ' . $url, LOGGER_DEBUG); + Logger::log('load: ' . $url, Logger::DEBUG); - $s = Network::fetchUrl($url); + $fetchresult = Network::fetchUrlFull($url); + $s = $fetchresult->getBody(); - logger('load: returns ' . $s, LOGGER_DATA); + Logger::log('load: returns ' . $s, Logger::DATA); - logger('load: return code: ' . Network::getCurl()->getCode(), LOGGER_DEBUG); + Logger::log('load: return code: ' . $fetchresult->getReturnCode(), Logger::DEBUG); - if ((Network::getCurl()->getCode() > 299) || (! $s)) { + if (($fetchresult->getReturnCode() > 299) || (! $s)) { return; } $j = json_decode($s, true); - logger('load: json: ' . print_r($j, true), LOGGER_DATA); + Logger::log('load: json: ' . print_r($j, true), Logger::DATA); if (!isset($j['entry'])) { return; @@ -112,7 +117,7 @@ class PortableContact $connect_url = ''; $name = ''; $network = ''; - $updated = NULL_DATE; + $updated = DBA::NULL_DATETIME; $location = ''; $about = ''; $keywords = ''; @@ -198,10 +203,10 @@ class PortableContact GContact::link($gcid, $uid, $cid, $zcid); } catch (Exception $e) { - logger($e->getMessage(), LOGGER_DEBUG); + Logger::log($e->getMessage(), Logger::DEBUG); } } - logger("load: loaded $total entries", LOGGER_DEBUG); + Logger::log("load: loaded $total entries", Logger::DEBUG); $condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid]; DBA::delete('glink', $condition); @@ -229,7 +234,6 @@ class PortableContact $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile); if ($friendica != $profile) { $server_url = $friendica; - $network = Protocol::DFRN; } } @@ -237,7 +241,6 @@ class PortableContact $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile); if ($diaspora != $profile) { $server_url = $diaspora; - $network = Protocol::DIASPORA; } } @@ -245,7 +248,6 @@ class PortableContact $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile); if ($red != $profile) { $server_url = $red; - $network = Protocol::DIASPORA; } } @@ -254,7 +256,6 @@ class PortableContact $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile); if ($mastodon != $profile) { $server_url = $mastodon; - $network = Protocol::OSTATUS; } } @@ -263,7 +264,6 @@ class PortableContact $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile); if ($ostatus != $profile) { $server_url = $ostatus; - $network = Protocol::OSTATUS; } } @@ -272,7 +272,6 @@ class PortableContact $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile); if ($base != $profile) { $server_url = $base; - $network = Protocol::PHANTOM; } } @@ -282,7 +281,7 @@ class PortableContact $r = q( "SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`", - DBA::escape(normalise_link($server_url)) + DBA::escape(Strings::normaliseLink($server_url)) ); if (DBA::isResult($r)) { @@ -307,7 +306,7 @@ class PortableContact { $gcontacts = q( "SELECT * FROM `gcontact` WHERE `nurl` = '%s'", - DBA::escape(normalise_link($profile)) + DBA::escape(Strings::normaliseLink($profile)) ); if (!DBA::isResult($gcontacts)) { @@ -316,13 +315,13 @@ class PortableContact $contact = ["url" => $profile]; - if ($gcontacts[0]["created"] <= NULL_DATE) { + if ($gcontacts[0]["created"] <= DBA::NULL_DATETIME) { $contact['created'] = DateTimeFormat::utcNow(); } $server_url = ''; if ($force) { - $server_url = normalise_link(self::detectServer($profile)); + $server_url = Strings::normaliseLink(self::detectServer($profile)); } if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) { @@ -330,11 +329,11 @@ class PortableContact } if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) { - $server_url = normalise_link(self::detectServer($profile)); + $server_url = Strings::normaliseLink(self::detectServer($profile)); } - if (!in_array($gcontacts[0]["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::FEED, Protocol::OSTATUS, ""])) { - logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG); + if (!in_array($gcontacts[0]["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::FEED, Protocol::OSTATUS, ""])) { + Logger::log("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", Logger::DEBUG); return false; } @@ -342,10 +341,10 @@ class PortableContact if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) { if ($force) { $fields = ['last_failure' => DateTimeFormat::utcNow()]; - DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); + DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]); } - logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG); + Logger::log("Profile ".$profile.": Server ".$server_url." wasn't reachable.", Logger::DEBUG); return false; } $contact['server_url'] = $server_url; @@ -354,7 +353,7 @@ class PortableContact if (in_array($gcontacts[0]["network"], ["", Protocol::FEED])) { $server = q( "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''", - DBA::escape(normalise_link($server_url)) + DBA::escape(Strings::normaliseLink($server_url)) ); if ($server) { @@ -367,7 +366,7 @@ class PortableContact // noscrape is really fast so we don't cache the call. if (($server_url != "") && ($gcontacts[0]["nick"] != "")) { // Use noscrape if possible - $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", DBA::escape(normalise_link($server_url))); + $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", DBA::escape(Strings::normaliseLink($server_url))); if ($server) { $curlResult = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]); @@ -423,9 +422,9 @@ class PortableContact if (!empty($noscrape["updated"])) { $fields = ['last_contact' => DateTimeFormat::utcNow()]; - DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); + DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]); - logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG); + Logger::log("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", Logger::DEBUG); return $noscrape["updated"]; } @@ -436,7 +435,7 @@ class PortableContact // If we only can poll the feed, then we only do this once a while if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) { - logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG); + Logger::log("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", Logger::DEBUG); GContact::update($contact); return $gcontacts[0]["updated"]; @@ -447,11 +446,11 @@ class PortableContact // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711) // Then check the other link and delete this one if (($data["network"] == Protocol::OSTATUS) && self::alternateOStatusUrl($profile) - && (normalise_link($profile) == normalise_link($data["alias"])) - && (normalise_link($profile) != normalise_link($data["url"])) + && (Strings::normaliseLink($profile) == Strings::normaliseLink($data["alias"])) + && (Strings::normaliseLink($profile) != Strings::normaliseLink($data["url"])) ) { // Delete the old entry - DBA::delete('gcontact', ['nurl' => normalise_link($profile)]); + DBA::delete('gcontact', ['nurl' => Strings::normaliseLink($profile)]); $gcontact = array_merge($gcontacts[0], $data); @@ -463,18 +462,18 @@ class PortableContact self::lastUpdated($data["url"], $force); } catch (Exception $e) { - logger($e->getMessage(), LOGGER_DEBUG); + Logger::log($e->getMessage(), Logger::DEBUG); } - logger("Profile ".$profile." was deleted", LOGGER_DEBUG); + Logger::log("Profile ".$profile." was deleted", Logger::DEBUG); return false; } if (($data["poll"] == "") || (in_array($data["network"], [Protocol::FEED, Protocol::PHANTOM]))) { $fields = ['last_failure' => DateTimeFormat::utcNow()]; - DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); + DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]); - logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG); + Logger::log("Profile ".$profile." wasn't reachable (profile)", Logger::DEBUG); return false; } @@ -488,9 +487,9 @@ class PortableContact if (!$curlResult->isSuccess()) { $fields = ['last_failure' => DateTimeFormat::utcNow()]; - DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); + DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]); - logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG); + Logger::log("Profile ".$profile." wasn't reachable (no feed)", Logger::DEBUG); return false; } @@ -506,8 +505,15 @@ class PortableContact $last_updated = ""; foreach ($entries as $entry) { - $published = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue); - $updated = DateTimeFormat::utc($xpath->query('atom:updated/text()' , $entry)->item(0)->nodeValue); + $published_item = $xpath->query('atom:published/text()', $entry)->item(0); + $updated_item = $xpath->query('atom:updated/text()' , $entry)->item(0); + $published = isset($published_item->nodeValue) ? DateTimeFormat::utc($published_item->nodeValue) : null; + $updated = isset($updated_item->nodeValue) ? DateTimeFormat::utc($updated_item->nodeValue) : null; + + if (!isset($published) || !isset($updated)) { + Logger::notice('Invalid entry for XPath.', ['entry' => $entry, 'profile' => $profile]); + continue; + } if ($last_updated < $published) { $last_updated = $published; @@ -521,7 +527,7 @@ class PortableContact // Maybe there aren't any entries. Then check if it is a valid feed if ($last_updated == "") { if ($xpath->query('/atom:feed')->length > 0) { - $last_updated = NULL_DATE; + $last_updated = DBA::NULL_DATETIME; } } @@ -531,14 +537,14 @@ class PortableContact $fields['updated'] = $last_updated; } - DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); + DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]); if (($gcontacts[0]["generation"] == 0)) { $fields = ['generation' => 9]; - DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); + DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]); } - logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG); + Logger::log("Profile ".$profile." was last updated at ".$last_updated, Logger::DEBUG); return $last_updated; } @@ -608,8 +614,6 @@ class PortableContact */ private static function detectPocoData(array $data) { - $server = false; - if (!isset($data['entry'])) { return false; } @@ -642,6 +646,7 @@ class PortableContact * * @param string $server_url address of the server * @return array Server data + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function fetchNodeinfo($server_url) { @@ -660,8 +665,8 @@ class PortableContact $nodeinfo2_url = ''; foreach ($nodeinfo['links'] as $link) { - if (!is_array($link) || empty($link['rel'])) { - logger('Invalid nodeinfo format for ' . $server_url, LOGGER_DEBUG); + if (!is_array($link) || empty($link['rel']) || empty($link['href'])) { + Logger::log('Invalid nodeinfo format for ' . $server_url, Logger::DEBUG); continue; } if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') { @@ -695,6 +700,7 @@ class PortableContact * * @param string $nodeinfo_url address of the nodeinfo path * @return array Server data + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function parseNodeinfo1($nodeinfo_url) { @@ -712,10 +718,10 @@ class PortableContact $server = []; - $server['register_policy'] = REGISTER_CLOSED; + $server['register_policy'] = Register::CLOSED; if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) { - $server['register_policy'] = REGISTER_OPEN; + $server['register_policy'] = Register::OPEN; } if (is_array($nodeinfo['software'])) { @@ -731,7 +737,7 @@ class PortableContact } } - if (is_array($nodeinfo['metadata']) && isset($nodeinfo['metadata']['nodeName'])) { + if (isset($nodeinfo['metadata']['nodeName'])) { $server['site_name'] = $nodeinfo['metadata']['nodeName']; } @@ -743,7 +749,7 @@ class PortableContact $friendica = false; $gnusocial = false; - if (is_array($nodeinfo['protocols']['inbound'])) { + if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) { foreach ($nodeinfo['protocols']['inbound'] as $inbound) { if ($inbound == 'diaspora') { $diaspora = true; @@ -779,6 +785,7 @@ class PortableContact * * @param string $nodeinfo_url address of the nodeinfo path * @return array Server data + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function parseNodeinfo2($nodeinfo_url) { @@ -795,10 +802,10 @@ class PortableContact $server = []; - $server['register_policy'] = REGISTER_CLOSED; + $server['register_policy'] = Register::CLOSED; if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) { - $server['register_policy'] = REGISTER_OPEN; + $server['register_policy'] = Register::OPEN; } if (is_array($nodeinfo['software'])) { @@ -814,7 +821,7 @@ class PortableContact } } - if (is_array($nodeinfo['metadata']) && isset($nodeinfo['metadata']['nodeName'])) { + if (isset($nodeinfo['metadata']['nodeName'])) { $server['site_name'] = $nodeinfo['metadata']['nodeName']; } @@ -928,11 +935,11 @@ class PortableContact return false; } - $gserver = DBA::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]); + $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]); if (DBA::isResult($gserver)) { - if ($gserver["created"] <= NULL_DATE) { + if ($gserver["created"] <= DBA::NULL_DATETIME) { $fields = ['created' => DateTimeFormat::utcNow()]; - $condition = ['nurl' => normalise_link($server_url)]; + $condition = ['nurl' => Strings::normaliseLink($server_url)]; DBA::update('gserver', $fields, $condition); } $poco = $gserver["poco"]; @@ -953,16 +960,16 @@ class PortableContact // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633 // It can happen that a zero date is in the database, but storing it again is forbidden. - if ($last_contact < NULL_DATE) { - $last_contact = NULL_DATE; + if ($last_contact < DBA::NULL_DATETIME) { + $last_contact = DBA::NULL_DATETIME; } - if ($last_failure < NULL_DATE) { - $last_failure = NULL_DATE; + if ($last_failure < DBA::NULL_DATETIME) { + $last_failure = DBA::NULL_DATETIME; } if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) { - logger("Use cached data for server ".$server_url, LOGGER_DEBUG); + Logger::log("Use cached data for server ".$server_url, Logger::DEBUG); return ($last_contact >= $last_failure); } } else { @@ -975,10 +982,10 @@ class PortableContact $register_policy = -1; $registered_users = 0; - $last_contact = NULL_DATE; - $last_failure = NULL_DATE; + $last_contact = DBA::NULL_DATETIME; + $last_failure = DBA::NULL_DATETIME; } - logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG); + Logger::log("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, Logger::DEBUG); $failure = false; $possible_failure = false; @@ -988,7 +995,7 @@ class PortableContact // Mastodon uses the "@" for user profiles. // But this can be misunderstood. if (parse_url($server_url, PHP_URL_USER) != '') { - DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]); + DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]); return false; } @@ -997,14 +1004,14 @@ class PortableContact $server_url = str_replace("http://", "https://", $server_url); // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital - $curlResult = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]); + $curlResult = Network::curl($server_url."/.well-known/host-meta", false, ['timeout' => 20]); // Quit if there is a timeout. // But we want to make sure to only quit if we are mostly sure that this server url fits. if (DBA::isResult($gserver) && ($orig_server_url == $server_url) && ($curlResult->isTimeout())) { - logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG); - DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]); + Logger::log("Connection to server ".$server_url." timed out.", Logger::DEBUG); + DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]); return false; } @@ -1014,12 +1021,12 @@ class PortableContact $server_url = str_replace("https://", "http://", $server_url); // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital - $curlResult = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]); + $curlResult = Network::curl($server_url."/.well-known/host-meta", false, ['timeout' => 20]); // Quit if there is a timeout if ($curlResult->isTimeout()) { - logger("Connection to server " . $server_url . " timed out.", LOGGER_DEBUG); - DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]); + Logger::log("Connection to server " . $server_url . " timed out.", Logger::DEBUG); + DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]); return false; } @@ -1046,7 +1053,7 @@ class PortableContact if (!$failure) { // This will be too low, but better than no value at all. - $registered_users = DBA::count('gcontact', ['server_url' => normalise_link($server_url)]); + $registered_users = DBA::count('gcontact', ['server_url' => Strings::normaliseLink($server_url)]); } // Look for poco @@ -1198,16 +1205,16 @@ class PortableContact if (!empty($data['register_policy'])) { switch ($data['register_policy']) { case "REGISTER_OPEN": - $register_policy = REGISTER_OPEN; + $register_policy = Register::OPEN; break; case "REGISTER_APPROVE": - $register_policy = REGISTER_APPROVE; + $register_policy = Register::APPROVE; break; case "REGISTER_CLOSED": default: - $register_policy = REGISTER_CLOSED; + $register_policy = Register::CLOSED; break; } } @@ -1273,11 +1280,11 @@ class PortableContact } if (!$closed && !$private and $inviteonly) { - $register_policy = REGISTER_APPROVE; + $register_policy = Register::APPROVE; } elseif (!$closed && !$private) { - $register_policy = REGISTER_OPEN; + $register_policy = Register::OPEN; } else { - $register_policy = REGISTER_CLOSED; + $register_policy = Register::CLOSED; } } } @@ -1311,9 +1318,9 @@ class PortableContact } if (!empty($data['registrations_open']) && $data['registrations_open']) { - $register_policy = REGISTER_OPEN; + $register_policy = Register::OPEN; } else { - $register_policy = REGISTER_CLOSED; + $register_policy = Register::CLOSED; } } } @@ -1372,9 +1379,29 @@ class PortableContact $site_name = $data['site_name']; } - $info = $data['info']; - $register_policy = constant($data['register_policy']); - $platform = $data['platform']; + $info = defaults($data, 'info', ''); + + $register_policy = defaults($data, 'register_policy', 'REGISTER_CLOSED'); + switch ($register_policy) { + case 'REGISTER_OPEN': + $register_policy = Register::OPEN; + break; + + case 'REGISTER_APPROVE': + $register_policy = Register::APPROVE; + break; + + default: + Logger::log("Register policy '$register_policy' from $server_url is invalid."); + // Defaulting to closed + + case 'REGISTER_CLOSED': + case 'REGISTER_INVITATION': + $register_policy = Register::CLOSED; + break; + } + + $platform = defaults($data, 'platform', ''); } } } @@ -1397,13 +1424,13 @@ class PortableContact } if (($last_contact <= $last_failure) && !$failure) { - logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG); + Logger::log("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", Logger::DEBUG); } elseif (($last_contact >= $last_failure) && $failure) { - logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG); + Logger::log("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", Logger::DEBUG); } // Check again if the server exists - $found = DBA::exists('gserver', ['nurl' => normalise_link($server_url)]); + $found = DBA::exists('gserver', ['nurl' => Strings::normaliseLink($server_url)]); $version = strip_tags($version); $site_name = strip_tags($site_name); @@ -1417,9 +1444,9 @@ class PortableContact 'last_contact' => $last_contact, 'last_failure' => $last_failure]; if ($found) { - DBA::update('gserver', $fields, ['nurl' => normalise_link($server_url)]); + DBA::update('gserver', $fields, ['nurl' => Strings::normaliseLink($server_url)]); } elseif (!$failure) { - $fields['nurl'] = normalise_link($server_url); + $fields['nurl'] = Strings::normaliseLink($server_url); $fields['created'] = DateTimeFormat::utcNow(); DBA::insert('gserver', $fields); } @@ -1428,7 +1455,7 @@ class PortableContact self::discoverRelay($server_url); } - logger("End discovery for server " . $server_url, LOGGER_DEBUG); + Logger::log("End discovery for server " . $server_url, Logger::DEBUG); return !$failure; } @@ -1437,10 +1464,11 @@ class PortableContact * @brief Fetch relay data from a given server url * * @param string $server_url address of the server + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function discoverRelay($server_url) { - logger("Discover relay data for server " . $server_url, LOGGER_DEBUG); + Logger::log("Discover relay data for server " . $server_url, Logger::DEBUG); $curlResult = Network::curl($server_url . "/.well-known/x-social-relay"); @@ -1454,7 +1482,7 @@ class PortableContact return; } - $gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => normalise_link($server_url)]); + $gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]); if (!DBA::isResult($gserver)) { return; @@ -1511,6 +1539,7 @@ class PortableContact /** * @brief Returns a list of all known servers * @return array List of server urls + * @throws Exception */ public static function serverlist() { @@ -1535,6 +1564,7 @@ class PortableContact * @brief Fetch server list from remote servers and adds them when they are new. * * @param string $poco URL to the POCO endpoint + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function fetchServerlist($poco) { @@ -1553,10 +1583,10 @@ class PortableContact foreach ($serverlist as $server) { $server_url = str_replace("/index.php", "", $server['url']); - $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", DBA::escape(normalise_link($server_url))); + $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", DBA::escape(Strings::normaliseLink($server_url))); if (!DBA::isResult($r)) { - logger("Call server check for server ".$server_url, LOGGER_DEBUG); + Logger::log("Call server check for server ".$server_url, Logger::DEBUG); Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url); } } @@ -1594,7 +1624,7 @@ class PortableContact if (!empty($accesstoken)) { $api = 'https://instances.social/api/1.0/instances/list?count=0'; $header = ['Authorization: Bearer '.$accesstoken]; - $curlResult = Network::curl($api, false, $redirects, ['headers' => $header]); + $curlResult = Network::curl($api, false, ['headers' => $header]); if ($curlResult->isSuccess()) { $servers = json_decode($curlResult->getBody(), true); @@ -1627,21 +1657,19 @@ class PortableContact public static function discoverSingleServer($id) { - $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id)); + $server = DBA::selectFirst('gserver', ['poco', 'nurl', 'url', 'network'], ['id' => $id]); - if (!DBA::isResult($r)) { + if (!DBA::isResult($server)) { return false; } - $server = $r[0]; - // Discover new servers out there (Works from Friendica version 3.5.2) self::fetchServerlist($server["poco"]); // Fetch all users from the other server $url = $server["poco"] . "/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation"; - logger("Fetch all users from the server " . $server["url"], LOGGER_DEBUG); + Logger::info("Fetch all users from the server " . $server["url"]); $curlResult = Network::curl($url); @@ -1652,7 +1680,7 @@ class PortableContact self::discoverServer($data, 2); } - if (Config::get('system', 'poco_discovery') > 1) { + if (Config::get('system', 'poco_discovery') >= self::USERS_GCONTACTS) { $timeframe = Config::get('system', 'poco_discovery_since'); if ($timeframe == 0) { @@ -1669,7 +1697,7 @@ class PortableContact $curlResult = Network::curl($url); if ($curlResult->isSuccess() && !empty($curlResult->getBody())) { - logger("Fetch all global contacts from the server " . $server["nurl"], LOGGER_DEBUG); + Logger::info("Fetch all global contacts from the server " . $server["nurl"]); $data = json_decode($curlResult->getBody(), true); if (!empty($data)) { @@ -1677,8 +1705,8 @@ class PortableContact } } - if (!$success && (Config::get('system', 'poco_discovery') > 2)) { - logger("Fetch contacts from users of the server " . $server["nurl"], LOGGER_DEBUG); + if (!$success && !empty($data) && Config::get('system', 'poco_discovery') >= self::USERS_GCONTACTS_FALLBACK) { + Logger::info("Fetch contacts from users of the server " . $server["nurl"]); self::discoverServerUsers($data, $server); } } @@ -1731,7 +1759,7 @@ class PortableContact continue; } - logger('Update directory from server ' . $gserver['url'] . ' with ID ' . $gserver['id'], LOGGER_DEBUG); + Logger::log('Update directory from server ' . $gserver['url'] . ' with ID ' . $gserver['id'], Logger::DEBUG); Worker::add(PRIORITY_LOW, 'DiscoverPoCo', 'update_server_directory', (int) $gserver['id']); if (!$complete && ( --$no_of_queries == 0)) { @@ -1761,7 +1789,7 @@ class PortableContact } if ($username != '') { - logger('Fetch contacts for the user ' . $username . ' from the server ' . $server['nurl'], LOGGER_DEBUG); + Logger::log('Fetch contacts for the user ' . $username . ' from the server ' . $server['nurl'], Logger::DEBUG); // Fetch all contacts from a given user from the other server $url = $server['poco'] . '/' . $username . '/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation'; @@ -1769,7 +1797,7 @@ class PortableContact $curlResult = Network::curl($url); if ($curlResult->isSuccess()) { - $data = json_decode($curlResult["body"], true); + $data = json_decode($curlResult->getBody(), true); if (!empty($data)) { self::discoverServer($data, 3); @@ -1793,7 +1821,7 @@ class PortableContact $connect_url = ''; $name = ''; $network = ''; - $updated = NULL_DATE; + $updated = DBA::NULL_DATETIME; $location = ''; $about = ''; $keywords = ''; @@ -1864,7 +1892,7 @@ class PortableContact if ($generation > 0) { $success = true; - logger("Store profile ".$profile_url, LOGGER_DEBUG); + Logger::log("Store profile ".$profile_url, Logger::DEBUG); $gcontact = ["url" => $profile_url, "name" => $name, @@ -1883,10 +1911,10 @@ class PortableContact $gcontact = GContact::sanitize($gcontact); GContact::update($gcontact); } catch (Exception $e) { - logger($e->getMessage(), LOGGER_DEBUG); + Logger::log($e->getMessage(), Logger::DEBUG); } - logger("Done for profile ".$profile_url, LOGGER_DEBUG); + Logger::log("Done for profile ".$profile_url, Logger::DEBUG); } } return $success;