2017-11-15 15:47:28 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Protocol/PortableContact.php
|
|
|
|
*
|
|
|
|
* @todo Move GNU Social URL schemata (http://server.tld/user/number) to http://server.tld/username
|
|
|
|
* @todo Fetch profile data from profile page for Redmatrix users
|
|
|
|
* @todo Detect if it is a forum
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Protocol;
|
|
|
|
|
2018-07-20 04:15:21 +02:00
|
|
|
use DOMDocument;
|
|
|
|
use DOMXPath;
|
|
|
|
use Exception;
|
2018-03-07 22:24:13 +01:00
|
|
|
use Friendica\Content\Text\HTML;
|
2017-11-15 15:47:28 +01:00
|
|
|
use Friendica\Core\Config;
|
2018-10-29 22:20:46 +01:00
|
|
|
use Friendica\Core\Logger;
|
2018-08-11 22:40:44 +02:00
|
|
|
use Friendica\Core\Protocol;
|
2017-11-15 15:47:28 +01:00
|
|
|
use Friendica\Core\Worker;
|
2018-07-20 14:19:26 +02:00
|
|
|
use Friendica\Database\DBA;
|
2017-12-07 15:09:28 +01:00
|
|
|
use Friendica\Model\GContact;
|
2017-12-07 14:57:35 +01:00
|
|
|
use Friendica\Model\Profile;
|
2018-12-28 02:56:15 +01:00
|
|
|
use Friendica\Module\Register;
|
2017-11-15 15:47:28 +01:00
|
|
|
use Friendica\Network\Probe;
|
2018-01-27 03:38:34 +01:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2018-01-27 05:09:48 +01:00
|
|
|
use Friendica\Util\Network;
|
2018-11-08 17:28:29 +01:00
|
|
|
use Friendica\Util\Strings;
|
2018-07-15 20:36:20 +02:00
|
|
|
use Friendica\Util\XML;
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
class PortableContact
|
|
|
|
{
|
2019-02-08 14:38:13 +01:00
|
|
|
const DISABLED = 0;
|
|
|
|
const USERS = 1;
|
|
|
|
const USERS_GCONTACTS = 2;
|
|
|
|
const USERS_GCONTACTS_FALLBACK = 3;
|
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
/**
|
|
|
|
* @brief Fetch POCO data
|
|
|
|
*
|
|
|
|
* @param integer $cid Contact ID
|
|
|
|
* @param integer $uid User ID
|
|
|
|
* @param integer $zcid Global Contact ID
|
|
|
|
* @param integer $url POCO address that should be polled
|
|
|
|
*
|
|
|
|
* Given a contact-id (minimum), load the PortableContacts friend list for that contact,
|
|
|
|
* and add the entries to the gcontact (Global Contact) table, or update existing entries
|
|
|
|
* if anything (name or photo) has changed.
|
|
|
|
* We use normalised urls for comparison which ignore http vs https and www.domain vs domain
|
|
|
|
*
|
|
|
|
* 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.
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2017-11-15 15:47:28 +01:00
|
|
|
*/
|
|
|
|
public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null)
|
|
|
|
{
|
|
|
|
// Call the function "load" via the worker
|
2017-11-18 12:02:46 +01:00
|
|
|
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "load", (int)$cid, (int)$uid, (int)$zcid, $url);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Fetch POCO data from the worker
|
|
|
|
*
|
|
|
|
* @param integer $cid Contact ID
|
|
|
|
* @param integer $uid User ID
|
|
|
|
* @param integer $zcid Global Contact ID
|
|
|
|
* @param integer $url POCO address that should be polled
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2017-11-15 15:47:28 +01:00
|
|
|
*/
|
|
|
|
public static function load($cid, $uid, $zcid, $url)
|
|
|
|
{
|
|
|
|
if ($cid) {
|
2017-12-19 07:47:39 +01:00
|
|
|
if (!$url || !$uid) {
|
2018-07-20 14:19:26 +02:00
|
|
|
$contact = DBA::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
|
2018-07-21 14:46:04 +02:00
|
|
|
if (DBA::isResult($contact)) {
|
2018-01-11 09:26:30 +01:00
|
|
|
$url = $contact['poco'];
|
|
|
|
$uid = $contact['uid'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-19 07:47:39 +01:00
|
|
|
if (!$uid) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 07:47:39 +01:00
|
|
|
if (!$url) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-30 15:06:22 +01:00
|
|
|
$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');
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('load: ' . $url, Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:20:30 +02:00
|
|
|
$fetchresult = Network::fetchUrlFull($url);
|
|
|
|
$s = $fetchresult->getBody();
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('load: returns ' . $s, Logger::DATA);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('load: return code: ' . $fetchresult->getReturnCode(), Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:20:30 +02:00
|
|
|
if (($fetchresult->getReturnCode() > 299) || (! $s)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
$j = json_decode($s, true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('load: json: ' . print_r($j, true), Logger::DATA);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!isset($j['entry'])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$total = 0;
|
2018-07-28 07:35:27 +02:00
|
|
|
foreach ($j['entry'] as $entry) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$total ++;
|
|
|
|
$profile_url = '';
|
|
|
|
$profile_photo = '';
|
|
|
|
$connect_url = '';
|
|
|
|
$name = '';
|
|
|
|
$network = '';
|
2018-10-21 07:53:47 +02:00
|
|
|
$updated = DBA::NULL_DATETIME;
|
2017-11-15 15:47:28 +01:00
|
|
|
$location = '';
|
|
|
|
$about = '';
|
|
|
|
$keywords = '';
|
|
|
|
$gender = '';
|
|
|
|
$contact_type = -1;
|
|
|
|
$generation = 0;
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($entry['displayName'])) {
|
|
|
|
$name = $entry['displayName'];
|
2018-07-10 14:27:56 +02:00
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['urls'])) {
|
|
|
|
foreach ($entry['urls'] as $url) {
|
|
|
|
if ($url['type'] == 'profile') {
|
|
|
|
$profile_url = $url['value'];
|
2017-11-15 15:47:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
if ($url['type'] == 'webfinger') {
|
|
|
|
$connect_url = str_replace('acct:', '', $url['value']);
|
2017-11-15 15:47:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['photos'])) {
|
|
|
|
foreach ($entry['photos'] as $photo) {
|
|
|
|
if ($photo['type'] == 'profile') {
|
|
|
|
$profile_photo = $photo['value'];
|
2017-11-15 15:47:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['updated'])) {
|
|
|
|
$updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['network'])) {
|
|
|
|
$network = $entry['network'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['currentLocation'])) {
|
|
|
|
$location = $entry['currentLocation'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['aboutMe'])) {
|
|
|
|
$about = HTML::toBBCode($entry['aboutMe']);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['gender'])) {
|
|
|
|
$gender = $entry['gender'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['generation']) && ($entry['generation'] > 0)) {
|
|
|
|
$generation = ++$entry['generation'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['tags'])) {
|
|
|
|
foreach ($entry['tags'] as $tag) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$keywords = implode(", ", $tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
|
|
|
|
$contact_type = $entry['contactType'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
$gcontact = ["url" => $profile_url,
|
2017-11-15 15:47:28 +01:00
|
|
|
"name" => $name,
|
|
|
|
"network" => $network,
|
|
|
|
"photo" => $profile_photo,
|
|
|
|
"about" => $about,
|
|
|
|
"location" => $location,
|
|
|
|
"gender" => $gender,
|
|
|
|
"keywords" => $keywords,
|
|
|
|
"connect" => $connect_url,
|
|
|
|
"updated" => $updated,
|
|
|
|
"contact-type" => $contact_type,
|
2018-01-15 14:05:12 +01:00
|
|
|
"generation" => $generation];
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
try {
|
2017-12-07 15:09:28 +01:00
|
|
|
$gcontact = GContact::sanitize($gcontact);
|
|
|
|
$gcid = GContact::update($gcontact);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2017-12-07 15:09:28 +01:00
|
|
|
GContact::link($gcid, $uid, $cid, $zcid);
|
2017-11-15 15:47:28 +01:00
|
|
|
} catch (Exception $e) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log($e->getMessage(), Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("load: loaded $total entries", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2017-12-19 07:47:39 +01:00
|
|
|
$condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::delete('glink', $condition);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function reachable($profile, $server = "", $network = "", $force = false)
|
|
|
|
{
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($server == "") {
|
2017-11-15 16:53:16 +01:00
|
|
|
$server = self::detectServer($profile);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($server == "") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
return self::checkServer($server, $network, $force);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function detectServer($profile)
|
|
|
|
{
|
2017-11-15 15:47:28 +01:00
|
|
|
// Try to detect the server path based upon some known standard paths
|
|
|
|
$server_url = "";
|
|
|
|
|
|
|
|
if ($server_url == "") {
|
|
|
|
$friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
|
|
|
|
if ($friendica != $profile) {
|
|
|
|
$server_url = $friendica;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($server_url == "") {
|
|
|
|
$diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
|
|
|
|
if ($diaspora != $profile) {
|
|
|
|
$server_url = $diaspora;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($server_url == "") {
|
|
|
|
$red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
|
|
|
|
if ($red != $profile) {
|
|
|
|
$server_url = $red;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mastodon
|
|
|
|
if ($server_url == "") {
|
|
|
|
$mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
|
|
|
|
if ($mastodon != $profile) {
|
|
|
|
$server_url = $mastodon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Numeric OStatus variant
|
|
|
|
if ($server_url == "") {
|
|
|
|
$ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
|
|
|
|
if ($ostatus != $profile) {
|
|
|
|
$server_url = $ostatus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wild guess
|
|
|
|
if ($server_url == "") {
|
|
|
|
$base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
|
|
|
|
if ($base != $profile) {
|
|
|
|
$server_url = $base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($server_url == "") {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
$r = q(
|
|
|
|
"SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::escape(Strings::normaliseLink($server_url))
|
2017-11-15 16:53:16 +01:00
|
|
|
);
|
|
|
|
|
2018-07-21 14:46:04 +02:00
|
|
|
if (DBA::isResult($r)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return $server_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the host-meta to check if this really is a server
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url."/.well-known/host-meta");
|
|
|
|
if (!$curlResult->isSuccess()) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $server_url;
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function alternateOStatusUrl($url)
|
|
|
|
{
|
2017-11-15 15:47:28 +01:00
|
|
|
return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function lastUpdated($profile, $force = false)
|
|
|
|
{
|
|
|
|
$gcontacts = q(
|
|
|
|
"SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::escape(Strings::normaliseLink($profile))
|
2017-11-15 16:53:16 +01:00
|
|
|
);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-21 14:46:04 +02:00
|
|
|
if (!DBA::isResult($gcontacts)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
$contact = ["url" => $profile];
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-21 07:53:47 +02:00
|
|
|
if ($gcontacts[0]["created"] <= DBA::NULL_DATETIME) {
|
2018-01-27 03:38:34 +01:00
|
|
|
$contact['created'] = DateTimeFormat::utcNow();
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 05:58:46 +01:00
|
|
|
$server_url = '';
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($force) {
|
2018-11-08 17:28:29 +01:00
|
|
|
$server_url = Strings::normaliseLink(self::detectServer($profile));
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
|
|
|
|
$server_url = $gcontacts[0]["server_url"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
|
2018-11-08 17:28:29 +01:00
|
|
|
$server_url = Strings::normaliseLink(self::detectServer($profile));
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-16 22:39:56 +01:00
|
|
|
if (!in_array($gcontacts[0]["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::FEED, Protocol::OSTATUS, ""])) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($server_url != "") {
|
2017-11-15 16:53:16 +01:00
|
|
|
if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($force) {
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields = ['last_failure' => DateTimeFormat::utcNow()];
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Profile ".$profile.": Server ".$server_url." wasn't reachable.", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$contact['server_url'] = $server_url;
|
|
|
|
}
|
|
|
|
|
2018-08-11 22:40:44 +02:00
|
|
|
if (in_array($gcontacts[0]["network"], ["", Protocol::FEED])) {
|
2017-11-15 16:53:16 +01:00
|
|
|
$server = q(
|
|
|
|
"SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::escape(Strings::normaliseLink($server_url))
|
2017-11-15 16:53:16 +01:00
|
|
|
);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if ($server) {
|
|
|
|
$contact['network'] = $server[0]["network"];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// noscrape is really fast so we don't cache the call.
|
|
|
|
if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
|
|
|
|
// Use noscrape if possible
|
2018-11-08 17:28:29 +01:00
|
|
|
$server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", DBA::escape(Strings::normaliseLink($server_url)));
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if ($server) {
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess() && ($curlResult->getBody() != "")) {
|
|
|
|
$noscrape = json_decode($curlResult->getBody(), true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if (is_array($noscrape)) {
|
|
|
|
$contact["network"] = $server[0]["network"];
|
|
|
|
|
|
|
|
if (isset($noscrape["fn"])) {
|
|
|
|
$contact["name"] = $noscrape["fn"];
|
|
|
|
}
|
|
|
|
if (isset($noscrape["comm"])) {
|
|
|
|
$contact["community"] = $noscrape["comm"];
|
|
|
|
}
|
|
|
|
if (isset($noscrape["tags"])) {
|
|
|
|
$keywords = implode(" ", $noscrape["tags"]);
|
|
|
|
if ($keywords != "") {
|
|
|
|
$contact["keywords"] = $keywords;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-19 23:03:39 +01:00
|
|
|
$location = Profile::formatLocation($noscrape);
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($location) {
|
|
|
|
$contact["location"] = $location;
|
|
|
|
}
|
|
|
|
if (isset($noscrape["dfrn-notify"])) {
|
|
|
|
$contact["notify"] = $noscrape["dfrn-notify"];
|
|
|
|
}
|
|
|
|
// Remove all fields that are not present in the gcontact table
|
|
|
|
unset($noscrape["fn"]);
|
|
|
|
unset($noscrape["key"]);
|
|
|
|
unset($noscrape["homepage"]);
|
|
|
|
unset($noscrape["comm"]);
|
|
|
|
unset($noscrape["tags"]);
|
|
|
|
unset($noscrape["locality"]);
|
|
|
|
unset($noscrape["region"]);
|
|
|
|
unset($noscrape["country-name"]);
|
|
|
|
unset($noscrape["contacts"]);
|
|
|
|
unset($noscrape["dfrn-request"]);
|
|
|
|
unset($noscrape["dfrn-confirm"]);
|
|
|
|
unset($noscrape["dfrn-notify"]);
|
|
|
|
unset($noscrape["dfrn-poll"]);
|
|
|
|
|
|
|
|
// Set the date of the last contact
|
|
|
|
/// @todo By now the function "update_gcontact" doesn't work with this field
|
2018-01-27 03:38:34 +01:00
|
|
|
//$contact["last_contact"] = DateTimeFormat::utcNow();
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
$contact = array_merge($contact, $noscrape);
|
|
|
|
|
2017-12-07 15:09:28 +01:00
|
|
|
GContact::update($contact);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-10 14:27:56 +02:00
|
|
|
if (!empty($noscrape["updated"])) {
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields = ['last_contact' => DateTimeFormat::utcNow()];
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
return $noscrape["updated"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we only can poll the feed, then we only do this once a while
|
2017-11-15 16:53:16 +01:00
|
|
|
if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2017-12-07 15:09:28 +01:00
|
|
|
GContact::update($contact);
|
2017-11-15 15:47:28 +01:00
|
|
|
return $gcontacts[0]["updated"];
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = Probe::uri($profile);
|
|
|
|
|
|
|
|
// Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
|
|
|
|
// Then check the other link and delete this one
|
2018-08-11 22:40:44 +02:00
|
|
|
if (($data["network"] == Protocol::OSTATUS) && self::alternateOStatusUrl($profile)
|
2018-11-08 17:28:29 +01:00
|
|
|
&& (Strings::normaliseLink($profile) == Strings::normaliseLink($data["alias"]))
|
|
|
|
&& (Strings::normaliseLink($profile) != Strings::normaliseLink($data["url"]))
|
2017-11-15 15:47:28 +01:00
|
|
|
) {
|
|
|
|
// Delete the old entry
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::delete('gcontact', ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
$gcontact = array_merge($gcontacts[0], $data);
|
|
|
|
|
|
|
|
$gcontact["server_url"] = $data["baseurl"];
|
|
|
|
|
|
|
|
try {
|
2017-12-07 15:09:28 +01:00
|
|
|
$gcontact = GContact::sanitize($gcontact);
|
|
|
|
GContact::update($gcontact);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
self::lastUpdated($data["url"], $force);
|
2017-11-15 15:47:28 +01:00
|
|
|
} catch (Exception $e) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log($e->getMessage(), Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Profile ".$profile." was deleted", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-11 22:40:44 +02:00
|
|
|
if (($data["poll"] == "") || (in_array($data["network"], [Protocol::FEED, Protocol::PHANTOM]))) {
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields = ['last_failure' => DateTimeFormat::utcNow()];
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Profile ".$profile." wasn't reachable (profile)", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contact = array_merge($contact, $data);
|
|
|
|
|
|
|
|
$contact["server_url"] = $data["baseurl"];
|
|
|
|
|
2017-12-07 15:09:28 +01:00
|
|
|
GContact::update($contact);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($data["poll"]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!$curlResult->isSuccess()) {
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields = ['last_failure' => DateTimeFormat::utcNow()];
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Profile ".$profile." wasn't reachable (no feed)", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
2018-07-28 07:35:27 +02:00
|
|
|
/// @TODO Avoid error supression here
|
2018-10-10 21:08:43 +02:00
|
|
|
@$doc->loadXML($curlResult->getBody());
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2017-12-17 21:24:57 +01:00
|
|
|
$xpath = new DOMXPath($doc);
|
2017-11-15 15:47:28 +01:00
|
|
|
$xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
|
|
|
|
|
|
|
|
$entries = $xpath->query('/atom:feed/atom:entry');
|
|
|
|
|
|
|
|
$last_updated = "";
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
2019-02-24 15:22:14 +01:00
|
|
|
$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;
|
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-02-04 03:39:35 +01:00
|
|
|
if ($last_updated < $published) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$last_updated = $published;
|
2018-02-04 03:39:35 +01:00
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-02-04 03:39:35 +01:00
|
|
|
if ($last_updated < $updated) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$last_updated = $updated;
|
2018-02-04 03:39:35 +01:00
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe there aren't any entries. Then check if it is a valid feed
|
|
|
|
if ($last_updated == "") {
|
|
|
|
if ($xpath->query('/atom:feed')->length > 0) {
|
2018-10-21 07:53:47 +02:00
|
|
|
$last_updated = DBA::NULL_DATETIME;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 03:39:35 +01:00
|
|
|
|
2018-05-11 12:44:28 +02:00
|
|
|
$fields = ['last_contact' => DateTimeFormat::utcNow()];
|
|
|
|
|
|
|
|
if (!empty($last_updated)) {
|
|
|
|
$fields['updated'] = $last_updated;
|
|
|
|
}
|
|
|
|
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if (($gcontacts[0]["generation"] == 0)) {
|
2017-12-19 07:47:39 +01:00
|
|
|
$fields = ['generation' => 9];
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Profile ".$profile." was last updated at ".$last_updated, Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-02-04 03:39:35 +01:00
|
|
|
return $last_updated;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function updateNeeded($created, $updated, $last_failure, $last_contact)
|
|
|
|
{
|
2018-01-27 03:38:34 +01:00
|
|
|
$now = strtotime(DateTimeFormat::utcNow());
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if ($updated > $last_contact) {
|
|
|
|
$contact_time = strtotime($updated);
|
|
|
|
} else {
|
|
|
|
$contact_time = strtotime($last_contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
$failure_time = strtotime($last_failure);
|
|
|
|
$created_time = strtotime($created);
|
|
|
|
|
|
|
|
// If there is no "created" time then use the current time
|
|
|
|
if ($created_time <= 0) {
|
|
|
|
$created_time = $now;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the last contact was less than 24 hours then don't update
|
|
|
|
if (($now - $contact_time) < (60 * 60 * 24)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the last failure was less than 24 hours then don't update
|
|
|
|
if (($now - $failure_time) < (60 * 60 * 24)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the last contact was less than a week ago and the last failure is older than a week then don't update
|
|
|
|
//if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
|
|
|
|
if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
|
|
|
|
if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
/// @TODO Maybe move this out to an utilities class?
|
2017-12-21 08:27:20 +01:00
|
|
|
private static function toBoolean($val)
|
2017-11-15 16:53:16 +01:00
|
|
|
{
|
2017-11-15 15:47:28 +01:00
|
|
|
if (($val == "true") || ($val == 1)) {
|
|
|
|
return true;
|
|
|
|
} elseif (($val == "false") || ($val == 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Detect server type (Hubzilla or Friendica) via the poco data
|
|
|
|
*
|
2018-07-28 07:35:27 +02:00
|
|
|
* @param array $data POCO data
|
2017-11-15 15:47:28 +01:00
|
|
|
* @return array Server data
|
|
|
|
*/
|
2018-07-28 07:35:27 +02:00
|
|
|
private static function detectPocoData(array $data)
|
2017-11-15 16:53:16 +01:00
|
|
|
{
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!isset($data['entry'])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (count($data['entry']) == 0) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!isset($data['entry'][0]['urls'])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (count($data['entry'][0]['urls']) == 0) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
foreach ($data['entry'][0]['urls'] as $url) {
|
|
|
|
if ($url['type'] == 'zot') {
|
2018-01-15 14:05:12 +01:00
|
|
|
$server = [];
|
2017-11-15 15:47:28 +01:00
|
|
|
$server["platform"] = 'Hubzilla';
|
2018-08-11 22:40:44 +02:00
|
|
|
$server["network"] = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Detect server type by using the nodeinfo data
|
|
|
|
*
|
|
|
|
* @param string $server_url address of the server
|
|
|
|
* @return array Server data
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2017-11-15 15:47:28 +01:00
|
|
|
*/
|
2017-12-21 08:27:20 +01:00
|
|
|
private static function fetchNodeinfo($server_url)
|
2017-11-15 16:53:16 +01:00
|
|
|
{
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url."/.well-known/nodeinfo");
|
|
|
|
if (!$curlResult->isSuccess()) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$nodeinfo = json_decode($curlResult->getBody(), true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!is_array($nodeinfo) || !isset($nodeinfo['links'])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-02 14:53:40 +01:00
|
|
|
$nodeinfo1_url = '';
|
|
|
|
$nodeinfo2_url = '';
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
foreach ($nodeinfo['links'] as $link) {
|
2019-05-29 03:27:13 +02:00
|
|
|
if (!is_array($link) || empty($link['rel']) || empty($link['href'])) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('Invalid nodeinfo format for ' . $server_url, Logger::DEBUG);
|
2018-08-01 08:47:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
|
|
|
|
$nodeinfo1_url = $link['href'];
|
|
|
|
} elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
|
|
|
|
$nodeinfo2_url = $link['href'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 14:53:40 +01:00
|
|
|
if ($nodeinfo1_url . $nodeinfo2_url == '') {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-02 14:53:40 +01:00
|
|
|
$server = [];
|
|
|
|
|
2017-12-19 10:57:24 +01:00
|
|
|
// When the nodeinfo url isn't on the same host, then there is obviously something wrong
|
2018-02-02 14:53:40 +01:00
|
|
|
if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
|
|
|
|
$server = self::parseNodeinfo2($nodeinfo2_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the nodeinfo url isn't on the same host, then there is obviously something wrong
|
|
|
|
if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
|
|
|
|
$server = self::parseNodeinfo1($nodeinfo1_url);
|
2017-12-19 10:57:24 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 14:53:40 +01:00
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parses Nodeinfo 1
|
|
|
|
*
|
|
|
|
* @param string $nodeinfo_url address of the nodeinfo path
|
|
|
|
* @return array Server data
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-02-02 14:53:40 +01:00
|
|
|
*/
|
|
|
|
private static function parseNodeinfo1($nodeinfo_url)
|
|
|
|
{
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($nodeinfo_url);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!$curlResult->isSuccess()) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$nodeinfo = json_decode($curlResult->getBody(), true);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!is_array($nodeinfo)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
$server = [];
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-12-28 02:56:15 +01:00
|
|
|
$server['register_policy'] = Register::CLOSED;
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) {
|
2018-12-28 02:56:15 +01:00
|
|
|
$server['register_policy'] = Register::OPEN;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (is_array($nodeinfo['software'])) {
|
|
|
|
if (isset($nodeinfo['software']['name'])) {
|
|
|
|
$server['platform'] = $nodeinfo['software']['name'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($nodeinfo['software']['version'])) {
|
|
|
|
$server['version'] = $nodeinfo['software']['version'];
|
2017-11-15 15:47:28 +01:00
|
|
|
// Version numbers on Nodeinfo are presented with additional info, e.g.:
|
|
|
|
// 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
|
|
|
|
$server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 20:29:06 +01:00
|
|
|
if (isset($nodeinfo['metadata']['nodeName'])) {
|
2018-07-28 07:35:27 +02:00
|
|
|
$server['site_name'] = $nodeinfo['metadata']['nodeName'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($nodeinfo['usage']['users']['total'])) {
|
|
|
|
$server['registered-users'] = $nodeinfo['usage']['users']['total'];
|
2017-12-19 00:58:18 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
$diaspora = false;
|
|
|
|
$friendica = false;
|
|
|
|
$gnusocial = false;
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (is_array($nodeinfo['protocols']['inbound'])) {
|
|
|
|
foreach ($nodeinfo['protocols']['inbound'] as $inbound) {
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($inbound == 'diaspora') {
|
|
|
|
$diaspora = true;
|
|
|
|
}
|
|
|
|
if ($inbound == 'friendica') {
|
|
|
|
$friendica = true;
|
|
|
|
}
|
|
|
|
if ($inbound == 'gnusocial') {
|
|
|
|
$gnusocial = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($gnusocial) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$server['network'] = Protocol::OSTATUS;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
if ($diaspora) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$server['network'] = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
if ($friendica) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$server['network'] = Protocol::DFRN;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$server) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
|
2018-02-02 14:53:40 +01:00
|
|
|
/**
|
|
|
|
* @brief Parses Nodeinfo 2
|
|
|
|
*
|
|
|
|
* @param string $nodeinfo_url address of the nodeinfo path
|
|
|
|
* @return array Server data
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-02-02 14:53:40 +01:00
|
|
|
*/
|
|
|
|
private static function parseNodeinfo2($nodeinfo_url)
|
|
|
|
{
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($nodeinfo_url);
|
|
|
|
if (!$curlResult->isSuccess()) {
|
2018-02-02 14:53:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$nodeinfo = json_decode($curlResult->getBody(), true);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!is_array($nodeinfo)) {
|
2018-02-02 14:53:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$server = [];
|
|
|
|
|
2018-12-28 02:56:15 +01:00
|
|
|
$server['register_policy'] = Register::CLOSED;
|
2018-02-02 14:53:40 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) {
|
2018-12-28 02:56:15 +01:00
|
|
|
$server['register_policy'] = Register::OPEN;
|
2018-02-02 14:53:40 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (is_array($nodeinfo['software'])) {
|
|
|
|
if (isset($nodeinfo['software']['name'])) {
|
|
|
|
$server['platform'] = $nodeinfo['software']['name'];
|
2018-02-02 14:53:40 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($nodeinfo['software']['version'])) {
|
|
|
|
$server['version'] = $nodeinfo['software']['version'];
|
2018-02-02 14:53:40 +01:00
|
|
|
// Version numbers on Nodeinfo are presented with additional info, e.g.:
|
|
|
|
// 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
|
|
|
|
$server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 20:29:06 +01:00
|
|
|
if (isset($nodeinfo['metadata']['nodeName'])) {
|
2018-07-28 07:35:27 +02:00
|
|
|
$server['site_name'] = $nodeinfo['metadata']['nodeName'];
|
2018-02-02 14:53:40 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($nodeinfo['usage']['users']['total'])) {
|
|
|
|
$server['registered-users'] = $nodeinfo['usage']['users']['total'];
|
2018-02-02 14:53:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$diaspora = false;
|
|
|
|
$friendica = false;
|
|
|
|
$gnusocial = false;
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($nodeinfo['protocols'])) {
|
|
|
|
foreach ($nodeinfo['protocols'] as $protocol) {
|
2018-02-02 14:53:40 +01:00
|
|
|
if ($protocol == 'diaspora') {
|
|
|
|
$diaspora = true;
|
2018-07-28 07:35:27 +02:00
|
|
|
} elseif ($protocol == 'friendica') {
|
2018-02-02 14:53:40 +01:00
|
|
|
$friendica = true;
|
2018-07-28 07:35:27 +02:00
|
|
|
} elseif ($protocol == 'gnusocial') {
|
2018-02-02 14:53:40 +01:00
|
|
|
$gnusocial = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($gnusocial) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$server['network'] = Protocol::OSTATUS;
|
2018-07-28 07:35:27 +02:00
|
|
|
} elseif ($diaspora) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$server['network'] = Protocol::DIASPORA;
|
2018-07-28 07:35:27 +02:00
|
|
|
} elseif ($friendica) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$server['network'] = Protocol::DFRN;
|
2018-02-02 14:53:40 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (empty($server)) {
|
2018-02-02 14:53:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
/**
|
|
|
|
* @brief Detect server type (Hubzilla or Friendica) via the front page body
|
|
|
|
*
|
|
|
|
* @param string $body Front page of the server
|
|
|
|
* @return array Server data
|
|
|
|
*/
|
2017-12-21 08:27:20 +01:00
|
|
|
private static function detectServerType($body)
|
2017-11-15 16:53:16 +01:00
|
|
|
{
|
2017-11-15 15:47:28 +01:00
|
|
|
$server = false;
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
2018-07-28 07:35:27 +02:00
|
|
|
/// @TODO Acoid supressing error
|
2017-11-15 15:47:28 +01:00
|
|
|
@$doc->loadHTML($body);
|
2017-12-17 21:24:57 +01:00
|
|
|
$xpath = new DOMXPath($doc);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
$list = $xpath->query("//meta[@name]");
|
|
|
|
|
|
|
|
foreach ($list as $node) {
|
2018-01-15 14:05:12 +01:00
|
|
|
$attr = [];
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($node->attributes->length) {
|
|
|
|
foreach ($node->attributes as $attribute) {
|
|
|
|
$attr[$attribute->name] = $attribute->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($attr['name'] == 'generator') {
|
|
|
|
$version_part = explode(" ", $attr['content']);
|
|
|
|
if (count($version_part) == 2) {
|
2018-01-15 14:05:12 +01:00
|
|
|
if (in_array($version_part[0], ["Friendika", "Friendica"])) {
|
|
|
|
$server = [];
|
2017-11-15 15:47:28 +01:00
|
|
|
$server["platform"] = $version_part[0];
|
|
|
|
$server["version"] = $version_part[1];
|
2018-08-11 22:40:44 +02:00
|
|
|
$server["network"] = Protocol::DFRN;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$server) {
|
|
|
|
$list = $xpath->query("//meta[@property]");
|
|
|
|
|
|
|
|
foreach ($list as $node) {
|
2018-01-15 14:05:12 +01:00
|
|
|
$attr = [];
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($node->attributes->length) {
|
|
|
|
foreach ($node->attributes as $attribute) {
|
|
|
|
$attr[$attribute->name] = $attribute->value;
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 14:05:12 +01:00
|
|
|
if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) {
|
|
|
|
$server = [];
|
2017-11-15 15:47:28 +01:00
|
|
|
$server["platform"] = $attr['content'];
|
|
|
|
$server["version"] = "";
|
2018-08-11 22:40:44 +02:00
|
|
|
$server["network"] = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$server) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-15 20:36:20 +02:00
|
|
|
$server["site_name"] = XML::getFirstNodeValue($xpath, '//head/title/text()');
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
return $server;
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function checkServer($server_url, $network = "", $force = false)
|
|
|
|
{
|
2017-11-15 15:47:28 +01:00
|
|
|
// Unify the server address
|
|
|
|
$server_url = trim($server_url, "/");
|
|
|
|
$server_url = str_replace("/index.php", "", $server_url);
|
|
|
|
|
|
|
|
if ($server_url == "") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-08 17:28:29 +01:00
|
|
|
$gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]);
|
2018-07-21 14:46:04 +02:00
|
|
|
if (DBA::isResult($gserver)) {
|
2018-10-21 07:53:47 +02:00
|
|
|
if ($gserver["created"] <= DBA::NULL_DATETIME) {
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields = ['created' => DateTimeFormat::utcNow()];
|
2018-11-08 17:28:29 +01:00
|
|
|
$condition = ['nurl' => Strings::normaliseLink($server_url)];
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::update('gserver', $fields, $condition);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-01-11 09:26:30 +01:00
|
|
|
$poco = $gserver["poco"];
|
|
|
|
$noscrape = $gserver["noscrape"];
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if ($network == "") {
|
2018-01-11 09:26:30 +01:00
|
|
|
$network = $gserver["network"];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:26:30 +01:00
|
|
|
$last_contact = $gserver["last_contact"];
|
|
|
|
$last_failure = $gserver["last_failure"];
|
|
|
|
$version = $gserver["version"];
|
|
|
|
$platform = $gserver["platform"];
|
|
|
|
$site_name = $gserver["site_name"];
|
|
|
|
$info = $gserver["info"];
|
|
|
|
$register_policy = $gserver["register_policy"];
|
|
|
|
$registered_users = $gserver["registered-users"];
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-03-21 06:20:55 +01:00
|
|
|
// 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.
|
2018-10-21 07:53:47 +02:00
|
|
|
if ($last_contact < DBA::NULL_DATETIME) {
|
|
|
|
$last_contact = DBA::NULL_DATETIME;
|
2018-03-21 06:20:55 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-21 07:53:47 +02:00
|
|
|
if ($last_failure < DBA::NULL_DATETIME) {
|
|
|
|
$last_failure = DBA::NULL_DATETIME;
|
2018-03-21 06:20:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:26:30 +01:00
|
|
|
if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Use cached data for server ".$server_url, Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
return ($last_contact >= $last_failure);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$poco = "";
|
|
|
|
$noscrape = "";
|
|
|
|
$version = "";
|
|
|
|
$platform = "";
|
|
|
|
$site_name = "";
|
|
|
|
$info = "";
|
|
|
|
$register_policy = -1;
|
2017-12-19 00:58:18 +01:00
|
|
|
$registered_users = 0;
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-21 07:53:47 +02:00
|
|
|
$last_contact = DBA::NULL_DATETIME;
|
|
|
|
$last_failure = DBA::NULL_DATETIME;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
$failure = false;
|
|
|
|
$possible_failure = false;
|
|
|
|
$orig_last_failure = $last_failure;
|
|
|
|
$orig_last_contact = $last_contact;
|
|
|
|
|
2017-12-19 12:54:02 +01:00
|
|
|
// Mastodon uses the "@" for user profiles.
|
|
|
|
// But this can be misunderstood.
|
|
|
|
if (parse_url($server_url, PHP_URL_USER) != '') {
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]);
|
2017-12-19 12:54:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
// Check if the page is accessible via SSL.
|
|
|
|
$orig_server_url = $server_url;
|
|
|
|
$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
|
2019-06-10 14:34:54 +02:00
|
|
|
$curlResult = Network::curl($server_url."/.well-known/host-meta", false, ['timeout' => 20]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
// 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.
|
2018-07-21 14:46:04 +02:00
|
|
|
if (DBA::isResult($gserver) && ($orig_server_url == $server_url) &&
|
2018-10-10 21:08:43 +02:00
|
|
|
($curlResult->isTimeout())) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Connection to server ".$server_url." timed out.", Logger::DEBUG);
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe the page is unencrypted only?
|
2018-10-10 21:08:43 +02:00
|
|
|
$xmlobj = @simplexml_load_string($curlResult->getBody(), 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
|
|
|
|
if (!$curlResult->isSuccess() || ($curlResult->getBody() == "") || empty($xmlobj) || !is_object($xmlobj)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$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
|
2019-06-10 14:34:54 +02:00
|
|
|
$curlResult = Network::curl($server_url."/.well-known/host-meta", false, ['timeout' => 20]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
// Quit if there is a timeout
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isTimeout()) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Connection to server " . $server_url . " timed out.", Logger::DEBUG);
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$xmlobj = @simplexml_load_string($curlResult->getBody(), 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!$curlResult->isSuccess() || ($curlResult->getBody() == "") || empty($xmlobj) || !is_object($xmlobj)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
// Workaround for bad configured servers (known nginx problem)
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!empty($curlResult->getInfo()) && !in_array($curlResult->getInfo()["http_code"], ["403", "404"])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$failure = true;
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
$possible_failure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the server has no possible failure we reset the cached data
|
|
|
|
if (!$possible_failure) {
|
|
|
|
$version = "";
|
|
|
|
$platform = "";
|
|
|
|
$site_name = "";
|
|
|
|
$info = "";
|
|
|
|
$register_policy = -1;
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:58:18 +01:00
|
|
|
if (!$failure) {
|
|
|
|
// This will be too low, but better than no value at all.
|
2018-11-08 17:28:29 +01:00
|
|
|
$registered_users = DBA::count('gcontact', ['server_url' => Strings::normaliseLink($server_url)]);
|
2017-12-19 00:58:18 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
// Look for poco
|
|
|
|
if (!$failure) {
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url."/poco");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['totalResults'])) {
|
|
|
|
$registered_users = $data['totalResults'];
|
|
|
|
$poco = $server_url . "/poco";
|
2017-11-15 16:53:16 +01:00
|
|
|
$server = self::detectPocoData($data);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!empty($server)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$platform = $server['platform'];
|
|
|
|
$network = $server['network'];
|
|
|
|
$version = '';
|
|
|
|
$site_name = '';
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There are servers out there who don't return 404 on a failure
|
|
|
|
* We have to be sure that don't misunderstand this
|
|
|
|
*/
|
2017-12-19 10:57:24 +01:00
|
|
|
if (is_null($data)) {
|
|
|
|
$poco = "";
|
|
|
|
$noscrape = "";
|
|
|
|
$network = "";
|
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$failure) {
|
|
|
|
// Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!$curlResult->isSuccess() || ($curlResult->getBody() == "")) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$failure = true;
|
|
|
|
} else {
|
2018-10-10 21:08:43 +02:00
|
|
|
$server = self::detectServerType($curlResult->getBody());
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!empty($server)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$platform = $server['platform'];
|
|
|
|
$network = $server['network'];
|
|
|
|
$version = $server['version'];
|
|
|
|
$site_name = $server['site_name'];
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$lines = explode("\n", $curlResult->getHeader());
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
if (count($lines)) {
|
2017-11-15 16:53:16 +01:00
|
|
|
foreach ($lines as $line) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$line = trim($line);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
if (stristr($line, 'X-Diaspora-Version:')) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$platform = "Diaspora";
|
|
|
|
$version = trim(str_replace("X-Diaspora-Version:", "", $line));
|
|
|
|
$version = trim(str_replace("x-diaspora-version:", "", $version));
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
$versionparts = explode("-", $version);
|
|
|
|
$version = $versionparts[0];
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
if (stristr($line, 'Server: Mastodon')) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$platform = "Mastodon";
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::OSTATUS;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$failure && ($poco == "")) {
|
|
|
|
// Test for Statusnet
|
|
|
|
// Will also return data for Friendica and GNU Social - but it will be overwritten later
|
|
|
|
// The "not implemented" is a special treatment for really, really old Friendica versions
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url."/api/statusnet/version.json");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
|
|
|
|
($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$platform = "StatusNet";
|
|
|
|
// Remove junk that some GNU Social servers return
|
2018-10-10 21:08:43 +02:00
|
|
|
$version = str_replace(chr(239).chr(187).chr(191), "", $curlResult->getBody());
|
2017-11-15 15:47:28 +01:00
|
|
|
$version = trim($version, '"');
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::OSTATUS;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test for GNU Social
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url."/api/gnusocial/version.json");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
|
|
|
|
($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$platform = "GNU Social";
|
|
|
|
// Remove junk that some GNU Social servers return
|
2018-10-10 21:08:43 +02:00
|
|
|
$version = str_replace(chr(239) . chr(187) . chr(191), "", $curlResult->getBody());
|
2017-11-15 15:47:28 +01:00
|
|
|
$version = trim($version, '"');
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::OSTATUS;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test for Mastodon
|
|
|
|
$orig_version = $version;
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url . "/api/v1/instance");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess() && ($curlResult->getBody() != '')) {
|
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2017-12-19 10:57:24 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($data['version'])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$platform = "Mastodon";
|
2018-09-11 10:04:14 +02:00
|
|
|
$version = defaults($data, 'version', '');
|
|
|
|
$site_name = defaults($data, 'title', '');
|
|
|
|
$info = defaults($data, 'description', '');
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::OSTATUS;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!empty($data['stats']['user_count'])) {
|
|
|
|
$registered_users = $data['stats']['user_count'];
|
2017-12-19 00:58:18 +01:00
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (strstr($orig_version . $version, 'Pleroma')) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$platform = 'Pleroma';
|
|
|
|
$version = trim(str_replace('Pleroma', '', $version));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$failure) {
|
|
|
|
// Test for Hubzilla and Red
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url . "/siteinfo.json");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['url'])) {
|
|
|
|
$platform = $data['platform'];
|
|
|
|
$version = $data['version'];
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!empty($data['site_name'])) {
|
|
|
|
$site_name = $data['site_name'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!empty($data['channels_total'])) {
|
|
|
|
$registered_users = $data['channels_total'];
|
2017-12-19 00:58:18 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!empty($data['register_policy'])) {
|
|
|
|
switch ($data['register_policy']) {
|
2018-07-10 14:27:56 +02:00
|
|
|
case "REGISTER_OPEN":
|
2018-12-28 02:56:15 +01:00
|
|
|
$register_policy = Register::OPEN;
|
2018-07-10 14:27:56 +02:00
|
|
|
break;
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-07-10 14:27:56 +02:00
|
|
|
case "REGISTER_APPROVE":
|
2018-12-28 02:56:15 +01:00
|
|
|
$register_policy = Register::APPROVE;
|
2018-07-10 14:27:56 +02:00
|
|
|
break;
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-07-10 14:27:56 +02:00
|
|
|
case "REGISTER_CLOSED":
|
|
|
|
default:
|
2018-12-28 02:56:15 +01:00
|
|
|
$register_policy = Register::CLOSED;
|
2018-07-10 14:27:56 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Test for Hubzilla, Redmatrix or Friendica
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url."/api/statusnet/config.json");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['site']['server'])) {
|
|
|
|
if (isset($data['site']['platform'])) {
|
|
|
|
$platform = $data['site']['platform']['PLATFORM_NAME'];
|
|
|
|
$version = $data['site']['platform']['STD_VERSION'];
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['site']['BlaBlaNet'])) {
|
|
|
|
$platform = $data['site']['BlaBlaNet']['PLATFORM_NAME'];
|
|
|
|
$version = $data['site']['BlaBlaNet']['STD_VERSION'];
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['site']['hubzilla'])) {
|
|
|
|
$platform = $data['site']['hubzilla']['PLATFORM_NAME'];
|
|
|
|
$version = $data['site']['hubzilla']['RED_VERSION'];
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['site']['redmatrix'])) {
|
|
|
|
if (isset($data['site']['redmatrix']['PLATFORM_NAME'])) {
|
|
|
|
$platform = $data['site']['redmatrix']['PLATFORM_NAME'];
|
|
|
|
} elseif (isset($data['site']['redmatrix']['RED_PLATFORM'])) {
|
|
|
|
$platform = $data['site']['redmatrix']['RED_PLATFORM'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
$version = $data['site']['redmatrix']['RED_VERSION'];
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['site']['friendica'])) {
|
|
|
|
$platform = $data['site']['friendica']['FRIENDICA_PLATFORM'];
|
|
|
|
$version = $data['site']['friendica']['FRIENDICA_VERSION'];
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DFRN;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
$site_name = $data['site']['name'];
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-23 13:43:18 +02:00
|
|
|
$private = false;
|
|
|
|
$inviteonly = false;
|
|
|
|
$closed = false;
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($data['site']['closed'])) {
|
|
|
|
$closed = self::toBoolean($data['site']['closed']);
|
2018-07-23 13:43:18 +02:00
|
|
|
}
|
2018-07-15 20:36:20 +02:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($data['site']['private'])) {
|
|
|
|
$private = self::toBoolean($data['site']['private']);
|
2018-07-15 20:36:20 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($data['site']['inviteonly'])) {
|
|
|
|
$inviteonly = self::toBoolean($data['site']['inviteonly']);
|
2018-07-15 20:36:20 +02:00
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-23 13:43:18 +02:00
|
|
|
if (!$closed && !$private and $inviteonly) {
|
2018-12-28 02:56:15 +01:00
|
|
|
$register_policy = Register::APPROVE;
|
2018-07-23 13:43:18 +02:00
|
|
|
} elseif (!$closed && !$private) {
|
2018-12-28 02:56:15 +01:00
|
|
|
$register_policy = Register::OPEN;
|
2017-11-15 15:47:28 +01:00
|
|
|
} else {
|
2018-12-28 02:56:15 +01:00
|
|
|
$register_policy = Register::CLOSED;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
|
|
|
|
if (!$failure) {
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url . "/statistics.json");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2017-12-19 10:57:24 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($data['version'])) {
|
|
|
|
$version = $data['version'];
|
2017-11-15 15:47:28 +01:00
|
|
|
// Version numbers on statistics.json are presented with additional info, e.g.:
|
|
|
|
// 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
|
|
|
|
$version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($data['name'])) {
|
|
|
|
$site_name = $data['name'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($data['network'])) {
|
|
|
|
$platform = $data['network'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($platform == "Diaspora") {
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DIASPORA;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($data['registrations_open']) && $data['registrations_open']) {
|
2018-12-28 02:56:15 +01:00
|
|
|
$register_policy = Register::OPEN;
|
2017-11-15 15:47:28 +01:00
|
|
|
} else {
|
2018-12-28 02:56:15 +01:00
|
|
|
$register_policy = Register::CLOSED;
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query nodeinfo. Working for (at least) Diaspora and Friendica.
|
|
|
|
if (!$failure) {
|
2017-11-15 16:53:16 +01:00
|
|
|
$server = self::fetchNodeinfo($server_url);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!empty($server)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$register_policy = $server['register_policy'];
|
|
|
|
|
|
|
|
if (isset($server['platform'])) {
|
|
|
|
$platform = $server['platform'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($server['network'])) {
|
|
|
|
$network = $server['network'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($server['version'])) {
|
|
|
|
$version = $server['version'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($server['site_name'])) {
|
|
|
|
$site_name = $server['site_name'];
|
|
|
|
}
|
2017-12-19 00:58:18 +01:00
|
|
|
|
|
|
|
if (isset($server['registered-users'])) {
|
|
|
|
$registered_users = $server['registered-users'];
|
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for noscrape
|
|
|
|
// Friendica servers could be detected as OStatus servers
|
2018-08-11 22:40:44 +02:00
|
|
|
if (!$failure && in_array($network, [Protocol::DFRN, Protocol::OSTATUS])) {
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url . "/friendica/json");
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!$curlResult->isSuccess()) {
|
|
|
|
$curlResult = Network::curl($server_url . "/friendika/json");
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($data['version'])) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$network = Protocol::DFRN;
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($data['no_scrape_url'])) {
|
|
|
|
$noscrape = $data['no_scrape_url'];
|
2018-07-10 14:27:56 +02:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
$version = $data['version'];
|
|
|
|
|
|
|
|
if (!empty($data['site_name'])) {
|
|
|
|
$site_name = $data['site_name'];
|
2018-07-15 20:36:20 +02:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-12-15 10:32:47 +01:00
|
|
|
$info = defaults($data, 'info', '');
|
2018-12-28 02:56:15 +01:00
|
|
|
|
|
|
|
$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;
|
2018-11-06 12:10:23 +01:00
|
|
|
}
|
2018-12-28 02:56:15 +01:00
|
|
|
|
2018-12-15 10:32:47 +01:00
|
|
|
$platform = defaults($data, 'platform', '');
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 12:54:02 +01:00
|
|
|
// Every server has got at least an admin account
|
|
|
|
if (!$failure && ($registered_users == 0)) {
|
|
|
|
$registered_users = 1;
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($possible_failure && !$failure) {
|
|
|
|
$failure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($failure) {
|
|
|
|
$last_contact = $orig_last_contact;
|
2018-01-27 03:38:34 +01:00
|
|
|
$last_failure = DateTimeFormat::utcNow();
|
2017-11-15 15:47:28 +01:00
|
|
|
} else {
|
2018-01-27 03:38:34 +01:00
|
|
|
$last_contact = DateTimeFormat::utcNow();
|
2017-11-15 15:47:28 +01:00
|
|
|
$last_failure = $orig_last_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($last_contact <= $last_failure) && !$failure) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
} elseif (($last_contact >= $last_failure) && $failure) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check again if the server exists
|
2018-11-08 17:28:29 +01:00
|
|
|
$found = DBA::exists('gserver', ['nurl' => Strings::normaliseLink($server_url)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
$version = strip_tags($version);
|
|
|
|
$site_name = strip_tags($site_name);
|
|
|
|
$info = strip_tags($info);
|
|
|
|
$platform = strip_tags($platform);
|
|
|
|
|
2017-12-19 07:47:39 +01:00
|
|
|
$fields = ['url' => $server_url, 'version' => $version,
|
|
|
|
'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
|
|
|
|
'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
|
|
|
|
'platform' => $platform, 'registered-users' => $registered_users,
|
2017-12-19 10:18:45 +01:00
|
|
|
'last_contact' => $last_contact, 'last_failure' => $last_failure];
|
2017-12-19 07:47:39 +01:00
|
|
|
|
|
|
|
if ($found) {
|
2018-11-08 17:28:29 +01:00
|
|
|
DBA::update('gserver', $fields, ['nurl' => Strings::normaliseLink($server_url)]);
|
2017-11-15 15:47:28 +01:00
|
|
|
} elseif (!$failure) {
|
2018-11-08 17:28:29 +01:00
|
|
|
$fields['nurl'] = Strings::normaliseLink($server_url);
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields['created'] = DateTimeFormat::utcNow();
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::insert('gserver', $fields);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
2018-03-26 07:44:53 +02:00
|
|
|
|
2018-08-11 22:40:44 +02:00
|
|
|
if (!$failure && in_array($fields['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
|
2018-03-26 08:17:50 +02:00
|
|
|
self::discoverRelay($server_url);
|
2018-03-26 07:44:53 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("End discovery for server " . $server_url, Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
return !$failure;
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:12:19 +02:00
|
|
|
/**
|
|
|
|
* @brief Fetch relay data from a given server url
|
|
|
|
*
|
|
|
|
* @param string $server_url address of the server
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-03-27 17:12:19 +02:00
|
|
|
*/
|
2018-03-26 07:44:53 +02:00
|
|
|
private static function discoverRelay($server_url)
|
|
|
|
{
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Discover relay data for server " . $server_url, Logger::DEBUG);
|
2018-03-26 07:44:53 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($server_url . "/.well-known/x-social-relay");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!$curlResult->isSuccess()) {
|
2018-03-26 07:44:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (!is_array($data)) {
|
2018-03-26 07:44:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-08 17:28:29 +01:00
|
|
|
$gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-07-21 14:46:04 +02:00
|
|
|
if (!DBA::isResult($gserver)) {
|
2018-03-26 07:44:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) {
|
|
|
|
$fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']];
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::update('gserver', $fields, ['id' => $gserver['id']]);
|
2018-03-26 08:17:50 +02:00
|
|
|
}
|
2018-03-26 07:44:53 +02:00
|
|
|
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if ($data['scope'] == 'tags') {
|
2018-04-11 20:56:22 +02:00
|
|
|
// Avoid duplicates
|
|
|
|
$tags = [];
|
2018-07-28 07:35:27 +02:00
|
|
|
foreach ($data['tags'] as $tag) {
|
2018-04-18 07:00:28 +02:00
|
|
|
$tag = mb_strtolower($tag);
|
2018-08-12 10:00:37 +02:00
|
|
|
if (strlen($tag) < 100) {
|
2018-08-06 19:11:40 +02:00
|
|
|
$tags[$tag] = $tag;
|
|
|
|
}
|
2018-04-11 20:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($tags as $tag) {
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true);
|
2018-03-26 08:11:55 +02:00
|
|
|
}
|
2018-03-26 07:44:53 +02:00
|
|
|
}
|
2018-04-30 07:33:47 +02:00
|
|
|
|
|
|
|
// Create or update the relay contact
|
|
|
|
$fields = [];
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($data['protocols'])) {
|
|
|
|
if (isset($data['protocols']['diaspora'])) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$fields['network'] = Protocol::DIASPORA;
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['protocols']['diaspora']['receive'])) {
|
|
|
|
$fields['batch'] = $data['protocols']['diaspora']['receive'];
|
|
|
|
} elseif (is_string($data['protocols']['diaspora'])) {
|
|
|
|
$fields['batch'] = $data['protocols']['diaspora'];
|
2018-05-10 13:04:18 +02:00
|
|
|
}
|
2018-04-30 07:33:47 +02:00
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['protocols']['dfrn'])) {
|
2018-08-11 22:40:44 +02:00
|
|
|
$fields['network'] = Protocol::DFRN;
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($data['protocols']['dfrn']['receive'])) {
|
|
|
|
$fields['batch'] = $data['protocols']['dfrn']['receive'];
|
|
|
|
} elseif (is_string($data['protocols']['dfrn'])) {
|
|
|
|
$fields['batch'] = $data['protocols']['dfrn'];
|
2018-05-10 13:04:18 +02:00
|
|
|
}
|
2018-04-30 07:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Diaspora::setRelayContact($server_url, $fields);
|
2018-03-26 07:44:53 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns a list of all known servers
|
|
|
|
* @return array List of server urls
|
2019-01-07 16:24:06 +01:00
|
|
|
* @throws Exception
|
2017-11-15 15:47:28 +01:00
|
|
|
*/
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function serverlist()
|
|
|
|
{
|
|
|
|
$r = q(
|
|
|
|
"SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
|
2017-11-15 15:47:28 +01:00
|
|
|
WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
|
|
|
|
ORDER BY `last_contact`
|
|
|
|
LIMIT 1000",
|
2018-08-11 22:40:44 +02:00
|
|
|
DBA::escape(Protocol::DFRN),
|
|
|
|
DBA::escape(Protocol::DIASPORA),
|
|
|
|
DBA::escape(Protocol::OSTATUS)
|
2017-11-15 16:53:16 +01:00
|
|
|
);
|
|
|
|
|
2018-07-21 14:46:04 +02:00
|
|
|
if (!DBA::isResult($r)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Fetch server list from remote servers and adds them when they are new.
|
|
|
|
*
|
|
|
|
* @param string $poco URL to the POCO endpoint
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2017-11-15 15:47:28 +01:00
|
|
|
*/
|
2017-12-21 08:27:20 +01:00
|
|
|
private static function fetchServerlist($poco)
|
2017-11-15 16:53:16 +01:00
|
|
|
{
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($poco . "/@server");
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!$curlResult->isSuccess()) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$serverlist = json_decode($curlResult->getBody(), true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if (!is_array($serverlist)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($serverlist as $server) {
|
2018-07-28 14:52:57 +02:00
|
|
|
$server_url = str_replace("/index.php", "", $server['url']);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-11-08 17:28:29 +01:00
|
|
|
$r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", DBA::escape(Strings::normaliseLink($server_url)));
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-07-21 14:46:04 +02:00
|
|
|
if (!DBA::isResult($r)) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Call server check for server ".$server_url, Logger::DEBUG);
|
2017-11-18 12:02:46 +01:00
|
|
|
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 08:27:20 +01:00
|
|
|
private static function discoverFederation()
|
2017-11-15 16:53:16 +01:00
|
|
|
{
|
|
|
|
$last = Config::get('poco', 'last_federation_discovery');
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if ($last) {
|
|
|
|
$next = $last + (24 * 60 * 60);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($next > time()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discover Friendica, Hubzilla and Diaspora servers
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::fetchUrl("http://the-federation.info/pods.json");
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!empty($curlResult)) {
|
|
|
|
$servers = json_decode($curlResult, true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($servers['pods'])) {
|
|
|
|
foreach ($servers['pods'] as $server) {
|
|
|
|
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://" . $server['host']);
|
2018-05-18 18:07:55 +02:00
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disvover Mastodon servers
|
2017-11-15 16:53:16 +01:00
|
|
|
if (!Config::get('system', 'ostatus_disabled')) {
|
2017-12-06 21:09:59 +01:00
|
|
|
$accesstoken = Config::get('system', 'instances_social_key');
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2017-12-06 21:09:59 +01:00
|
|
|
if (!empty($accesstoken)) {
|
|
|
|
$api = 'https://instances.social/api/1.0/instances/list?count=0';
|
2018-01-15 14:05:12 +01:00
|
|
|
$header = ['Authorization: Bearer '.$accesstoken];
|
2019-06-10 14:34:54 +02:00
|
|
|
$curlResult = Network::curl($api, false, ['headers' => $header]);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
$servers = json_decode($curlResult->getBody(), true);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
foreach ($servers['instances'] as $server) {
|
|
|
|
$url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
|
2017-12-06 21:09:59 +01:00
|
|
|
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
|
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently disabled, since the service isn't available anymore.
|
|
|
|
// It is not removed since I hope that there will be a successor.
|
|
|
|
// Discover GNU Social Servers.
|
|
|
|
//if (!Config::get('system','ostatus_disabled')) {
|
|
|
|
// $serverdata = "http://gstools.org/api/get_open_instances/";
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
// $curlResult = Network::curl($serverdata);
|
|
|
|
// if ($curlResult->isSuccess()) {
|
|
|
|
// $servers = json_decode($result->getBody(), true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
// foreach($servers['data'] as $server)
|
|
|
|
// self::checkServer($server['instance_address']);
|
2017-11-15 15:47:28 +01:00
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
Config::set('poco', 'last_federation_discovery', time());
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function discoverSingleServer($id)
|
|
|
|
{
|
2019-02-08 14:38:13 +01:00
|
|
|
$server = DBA::selectFirst('gserver', ['poco', 'nurl', 'url', 'network'], ['id' => $id]);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2019-02-08 14:38:13 +01:00
|
|
|
if (!DBA::isResult($server)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discover new servers out there (Works from Friendica version 3.5.2)
|
2017-11-15 16:53:16 +01:00
|
|
|
self::fetchServerlist($server["poco"]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
// Fetch all users from the other server
|
2018-07-28 07:35:27 +02:00
|
|
|
$url = $server["poco"] . "/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2019-02-08 14:38:13 +01:00
|
|
|
Logger::info("Fetch all users from the server " . $server["url"]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($url);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
|
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-08-02 19:07:20 +02:00
|
|
|
if (!empty($data)) {
|
|
|
|
self::discoverServer($data, 2);
|
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2019-02-08 14:38:13 +01:00
|
|
|
if (Config::get('system', 'poco_discovery') >= self::USERS_GCONTACTS) {
|
2017-11-15 16:53:16 +01:00
|
|
|
$timeframe = Config::get('system', 'poco_discovery_since');
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2017-11-15 15:47:28 +01:00
|
|
|
if ($timeframe == 0) {
|
|
|
|
$timeframe = 30;
|
|
|
|
}
|
|
|
|
|
2018-01-27 03:38:34 +01:00
|
|
|
$updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
// Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
|
|
|
|
$url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
|
|
|
|
|
|
|
|
$success = false;
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($url);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
|
2019-02-08 14:38:13 +01:00
|
|
|
Logger::info("Fetch all global contacts from the server " . $server["nurl"]);
|
2018-10-10 21:08:43 +02:00
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2018-08-02 19:07:20 +02:00
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
$success = self::discoverServer($data);
|
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 15:23:22 +01:00
|
|
|
if (!$success && !empty($data) && Config::get('system', 'poco_discovery') >= self::USERS_GCONTACTS_FALLBACK) {
|
|
|
|
Logger::info("Fetch contacts from users of the server " . $server["nurl"]);
|
2017-11-15 16:53:16 +01:00
|
|
|
self::discoverServerUsers($data, $server);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields = ['last_poco_query' => DateTimeFormat::utcNow()];
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// If the server hadn't replied correctly, then force a sanity check
|
2017-11-15 16:53:16 +01:00
|
|
|
self::checkServer($server["url"], $server["network"], true);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
// If we couldn't reach the server, we will try it some time later
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields = ['last_poco_query' => DateTimeFormat::utcNow()];
|
2018-07-20 14:19:26 +02:00
|
|
|
DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:53:16 +01:00
|
|
|
public static function discover($complete = false)
|
|
|
|
{
|
2017-11-15 15:47:28 +01:00
|
|
|
// Update the server list
|
2017-11-15 16:53:16 +01:00
|
|
|
self::discoverFederation();
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
$no_of_queries = 5;
|
|
|
|
|
2018-07-22 18:50:13 +02:00
|
|
|
$requery_days = intval(Config::get('system', 'poco_requery_days'));
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
if ($requery_days == 0) {
|
|
|
|
$requery_days = 7;
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-07-22 18:50:13 +02:00
|
|
|
$last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-22 18:50:13 +02:00
|
|
|
$gservers = q("SELECT `id`, `url`, `nurl`, `network`
|
|
|
|
FROM `gserver`
|
|
|
|
WHERE `last_contact` >= `last_failure`
|
|
|
|
AND `poco` != ''
|
|
|
|
AND `last_poco_query` < '%s'
|
|
|
|
ORDER BY RAND()", DBA::escape($last_update)
|
|
|
|
);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-07-22 18:50:13 +02:00
|
|
|
if (DBA::isResult($gservers)) {
|
|
|
|
foreach ($gservers as $gserver) {
|
|
|
|
if (!self::checkServer($gserver['url'], $gserver['network'])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
// The server is not reachable? Okay, then we will try it later
|
2018-01-27 03:38:34 +01:00
|
|
|
$fields = ['last_poco_query' => DateTimeFormat::utcNow()];
|
2018-07-22 18:50:13 +02:00
|
|
|
DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
|
2017-11-15 15:47:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('Update directory from server ' . $gserver['url'] . ' with ID ' . $gserver['id'], Logger::DEBUG);
|
2018-07-22 18:50:13 +02:00
|
|
|
Worker::add(PRIORITY_LOW, 'DiscoverPoCo', 'update_server_directory', (int) $gserver['id']);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-22 18:50:13 +02:00
|
|
|
if (!$complete && ( --$no_of_queries == 0)) {
|
2017-11-15 15:47:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
private static function discoverServerUsers(array $data, array $server)
|
2017-11-15 16:53:16 +01:00
|
|
|
{
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!isset($data['entry'])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
foreach ($data['entry'] as $entry) {
|
2018-07-22 18:45:24 +02:00
|
|
|
$username = '';
|
2018-07-28 07:35:27 +02:00
|
|
|
|
|
|
|
if (isset($entry['urls'])) {
|
|
|
|
foreach ($entry['urls'] as $url) {
|
|
|
|
if ($url['type'] == 'profile') {
|
|
|
|
$profile_url = $url['value'];
|
2018-07-22 18:45:24 +02:00
|
|
|
$path_array = explode('/', parse_url($profile_url, PHP_URL_PATH));
|
|
|
|
$username = end($path_array);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-07-22 18:45:24 +02:00
|
|
|
if ($username != '') {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('Fetch contacts for the user ' . $username . ' from the server ' . $server['nurl'], Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
// Fetch all contacts from a given user from the other server
|
2018-07-22 18:45:24 +02:00
|
|
|
$url = $server['poco'] . '/' . $username . '/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation';
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
$curlResult = Network::curl($url);
|
2018-07-28 07:35:27 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if ($curlResult->isSuccess()) {
|
2018-12-22 17:23:18 +01:00
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2018-08-02 19:07:20 +02:00
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
self::discoverServer($data, 3);
|
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
private static function discoverServer(array $data, $default_generation = 0)
|
2017-11-15 16:53:16 +01:00
|
|
|
{
|
2018-07-28 07:35:27 +02:00
|
|
|
if (empty($data['entry'])) {
|
2017-11-15 15:47:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = false;
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
foreach ($data['entry'] as $entry) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$profile_url = '';
|
|
|
|
$profile_photo = '';
|
|
|
|
$connect_url = '';
|
|
|
|
$name = '';
|
|
|
|
$network = '';
|
2018-10-21 07:53:47 +02:00
|
|
|
$updated = DBA::NULL_DATETIME;
|
2017-11-15 15:47:28 +01:00
|
|
|
$location = '';
|
|
|
|
$about = '';
|
|
|
|
$keywords = '';
|
|
|
|
$gender = '';
|
|
|
|
$contact_type = -1;
|
|
|
|
$generation = $default_generation;
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (!empty($entry['displayName'])) {
|
|
|
|
$name = $entry['displayName'];
|
2018-07-15 20:36:20 +02:00
|
|
|
}
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['urls'])) {
|
|
|
|
foreach ($entry['urls'] as $url) {
|
|
|
|
if ($url['type'] == 'profile') {
|
|
|
|
$profile_url = $url['value'];
|
2017-11-15 15:47:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-28 07:35:27 +02:00
|
|
|
if ($url['type'] == 'webfinger') {
|
|
|
|
$connect_url = str_replace('acct:' , '', $url['value']);
|
2017-11-15 15:47:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['photos'])) {
|
|
|
|
foreach ($entry['photos'] as $photo) {
|
|
|
|
if ($photo['type'] == 'profile') {
|
|
|
|
$profile_photo = $photo['value'];
|
2017-11-15 15:47:28 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['updated'])) {
|
|
|
|
$updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['network'])) {
|
|
|
|
$network = $entry['network'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['currentLocation'])) {
|
|
|
|
$location = $entry['currentLocation'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['aboutMe'])) {
|
|
|
|
$about = HTML::toBBCode($entry['aboutMe']);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['gender'])) {
|
|
|
|
$gender = $entry['gender'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['generation']) && ($entry['generation'] > 0)) {
|
|
|
|
$generation = ++$entry['generation'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
|
|
|
|
$contact_type = $entry['contactType'];
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 07:35:27 +02:00
|
|
|
if (isset($entry['tags'])) {
|
|
|
|
foreach ($entry['tags'] as $tag) {
|
2017-11-15 15:47:28 +01:00
|
|
|
$keywords = implode(", ", $tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($generation > 0) {
|
|
|
|
$success = true;
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Store profile ".$profile_url, Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
$gcontact = ["url" => $profile_url,
|
2017-11-15 15:47:28 +01:00
|
|
|
"name" => $name,
|
|
|
|
"network" => $network,
|
|
|
|
"photo" => $profile_photo,
|
|
|
|
"about" => $about,
|
|
|
|
"location" => $location,
|
|
|
|
"gender" => $gender,
|
|
|
|
"keywords" => $keywords,
|
|
|
|
"connect" => $connect_url,
|
|
|
|
"updated" => $updated,
|
|
|
|
"contact-type" => $contact_type,
|
2018-01-15 14:05:12 +01:00
|
|
|
"generation" => $generation];
|
2017-11-15 15:47:28 +01:00
|
|
|
|
|
|
|
try {
|
2017-12-07 15:09:28 +01:00
|
|
|
$gcontact = GContact::sanitize($gcontact);
|
|
|
|
GContact::update($gcontact);
|
2017-11-15 15:47:28 +01:00
|
|
|
} catch (Exception $e) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log($e->getMessage(), Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log("Done for profile ".$profile_url, Logger::DEBUG);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|