2017-11-15 14:47:28 +00: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 02:15:21 +00:00
|
|
|
use DOMDocument;
|
|
|
|
use DOMXPath;
|
|
|
|
use Exception;
|
2018-03-07 21:24:13 +00:00
|
|
|
use Friendica\Content\Text\HTML;
|
2017-11-15 14:47:28 +00:00
|
|
|
use Friendica\Core\Config;
|
2018-10-29 21:20:46 +00:00
|
|
|
use Friendica\Core\Logger;
|
2018-08-11 20:40:44 +00:00
|
|
|
use Friendica\Core\Protocol;
|
2017-11-15 14:47:28 +00:00
|
|
|
use Friendica\Core\Worker;
|
2018-07-20 12:19:26 +00:00
|
|
|
use Friendica\Database\DBA;
|
2019-10-02 15:10:42 +00:00
|
|
|
use Friendica\Model\Contact;
|
2017-12-07 14:09:28 +00:00
|
|
|
use Friendica\Model\GContact;
|
2019-10-03 23:33:41 +00:00
|
|
|
use Friendica\Model\GServer;
|
2017-12-07 13:57:35 +00:00
|
|
|
use Friendica\Model\Profile;
|
2018-12-28 01:56:15 +00:00
|
|
|
use Friendica\Module\Register;
|
2017-11-15 14:47:28 +00:00
|
|
|
use Friendica\Network\Probe;
|
2018-01-27 02:38:34 +00:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2018-01-27 04:09:48 +00:00
|
|
|
use Friendica\Util\Network;
|
2018-11-08 16:28:29 +00:00
|
|
|
use Friendica\Util\Strings;
|
2018-07-15 18:36:20 +00:00
|
|
|
use Friendica\Util\XML;
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
class PortableContact
|
|
|
|
{
|
2019-02-08 13:38:13 +00:00
|
|
|
const DISABLED = 0;
|
|
|
|
const USERS = 1;
|
|
|
|
const USERS_GCONTACTS = 2;
|
|
|
|
const USERS_GCONTACTS_FALLBACK = 3;
|
|
|
|
|
2017-11-15 14:47:28 +00: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 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2017-11-15 14:47:28 +00:00
|
|
|
*/
|
|
|
|
public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null)
|
|
|
|
{
|
|
|
|
// Call the function "load" via the worker
|
2017-11-18 11:02:46 +00:00
|
|
|
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "load", (int)$cid, (int)$uid, (int)$zcid, $url);
|
2017-11-15 14:47:28 +00: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 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2017-11-15 14:47:28 +00:00
|
|
|
*/
|
|
|
|
public static function load($cid, $uid, $zcid, $url)
|
|
|
|
{
|
|
|
|
if ($cid) {
|
2017-12-19 06:47:39 +00:00
|
|
|
if (!$url || !$uid) {
|
2018-07-20 12:19:26 +00:00
|
|
|
$contact = DBA::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
|
2018-07-21 12:46:04 +00:00
|
|
|
if (DBA::isResult($contact)) {
|
2018-01-11 08:26:30 +00:00
|
|
|
$url = $contact['poco'];
|
|
|
|
$uid = $contact['uid'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-19 06:47:39 +00:00
|
|
|
if (!$uid) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 06:47:39 +00:00
|
|
|
if (!$url) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-30 14:06:22 +00: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 14:47:28 +00:00
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log('load: ' . $url, Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-10 19:20:30 +00:00
|
|
|
$fetchresult = Network::fetchUrlFull($url);
|
|
|
|
$s = $fetchresult->getBody();
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log('load: returns ' . $s, Logger::DATA);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log('load: return code: ' . $fetchresult->getReturnCode(), Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-10 19:20:30 +00:00
|
|
|
if (($fetchresult->getReturnCode() > 299) || (! $s)) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
$j = json_decode($s, true);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log('load: json: ' . print_r($j, true), Logger::DATA);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (!isset($j['entry'])) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$total = 0;
|
2018-07-28 05:35:27 +00:00
|
|
|
foreach ($j['entry'] as $entry) {
|
2017-11-15 14:47:28 +00:00
|
|
|
$total ++;
|
|
|
|
$profile_url = '';
|
|
|
|
$profile_photo = '';
|
|
|
|
$connect_url = '';
|
|
|
|
$name = '';
|
|
|
|
$network = '';
|
2018-10-21 05:53:47 +00:00
|
|
|
$updated = DBA::NULL_DATETIME;
|
2017-11-15 14:47:28 +00:00
|
|
|
$location = '';
|
|
|
|
$about = '';
|
|
|
|
$keywords = '';
|
|
|
|
$gender = '';
|
|
|
|
$contact_type = -1;
|
|
|
|
$generation = 0;
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (!empty($entry['displayName'])) {
|
|
|
|
$name = $entry['displayName'];
|
2018-07-10 12:27:56 +00:00
|
|
|
}
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['urls'])) {
|
|
|
|
foreach ($entry['urls'] as $url) {
|
|
|
|
if ($url['type'] == 'profile') {
|
|
|
|
$profile_url = $url['value'];
|
2017-11-15 14:47:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-28 05:35:27 +00:00
|
|
|
if ($url['type'] == 'webfinger') {
|
|
|
|
$connect_url = str_replace('acct:', '', $url['value']);
|
2017-11-15 14:47:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['photos'])) {
|
|
|
|
foreach ($entry['photos'] as $photo) {
|
|
|
|
if ($photo['type'] == 'profile') {
|
|
|
|
$profile_photo = $photo['value'];
|
2017-11-15 14:47:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['updated'])) {
|
|
|
|
$updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['network'])) {
|
|
|
|
$network = $entry['network'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['currentLocation'])) {
|
|
|
|
$location = $entry['currentLocation'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['aboutMe'])) {
|
|
|
|
$about = HTML::toBBCode($entry['aboutMe']);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['gender'])) {
|
|
|
|
$gender = $entry['gender'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['generation']) && ($entry['generation'] > 0)) {
|
|
|
|
$generation = ++$entry['generation'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['tags'])) {
|
|
|
|
foreach ($entry['tags'] as $tag) {
|
2017-11-15 14:47:28 +00:00
|
|
|
$keywords = implode(", ", $tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
|
|
|
|
$contact_type = $entry['contactType'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 13:05:12 +00:00
|
|
|
$gcontact = ["url" => $profile_url,
|
2017-11-15 14:47:28 +00: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 13:05:12 +00:00
|
|
|
"generation" => $generation];
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
try {
|
2017-12-07 14:09:28 +00:00
|
|
|
$gcontact = GContact::sanitize($gcontact);
|
|
|
|
$gcid = GContact::update($gcontact);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2017-12-07 14:09:28 +00:00
|
|
|
GContact::link($gcid, $uid, $cid, $zcid);
|
2017-11-15 14:47:28 +00:00
|
|
|
} catch (Exception $e) {
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log($e->getMessage(), Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("load: loaded $total entries", Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2017-12-19 06:47:39 +00:00
|
|
|
$condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
|
2018-07-20 12:19:26 +00:00
|
|
|
DBA::delete('glink', $condition);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:53:16 +00:00
|
|
|
public static function reachable($profile, $server = "", $network = "", $force = false)
|
|
|
|
{
|
2017-11-15 14:47:28 +00:00
|
|
|
if ($server == "") {
|
2019-10-02 15:10:42 +00:00
|
|
|
$server = Contact::getBasepath($profile);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($server == "") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-03 23:33:41 +00:00
|
|
|
return GServer::check($server, $force);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:53:16 +00:00
|
|
|
public static function alternateOStatusUrl($url)
|
|
|
|
{
|
2017-11-15 14:47:28 +00:00
|
|
|
return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:53:16 +00:00
|
|
|
public static function lastUpdated($profile, $force = false)
|
|
|
|
{
|
|
|
|
$gcontacts = q(
|
|
|
|
"SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::escape(Strings::normaliseLink($profile))
|
2017-11-15 15:53:16 +00:00
|
|
|
);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
if (!DBA::isResult($gcontacts)) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-15 13:05:12 +00:00
|
|
|
$contact = ["url" => $profile];
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-21 05:53:47 +00:00
|
|
|
if ($gcontacts[0]["created"] <= DBA::NULL_DATETIME) {
|
2018-01-27 02:38:34 +00:00
|
|
|
$contact['created'] = DateTimeFormat::utcNow();
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 04:58:46 +00:00
|
|
|
$server_url = '';
|
2017-11-15 14:47:28 +00:00
|
|
|
if ($force) {
|
2019-10-02 15:10:42 +00:00
|
|
|
$server_url = Strings::normaliseLink(Contact::getBasepath($profile));
|
2017-11-15 14:47:28 +00: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"]))) {
|
2019-10-02 15:10:42 +00:00
|
|
|
$server_url = Strings::normaliseLink(Contact::getBasepath($profile));
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 21:39:56 +00:00
|
|
|
if (!in_array($gcontacts[0]["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::FEED, Protocol::OSTATUS, ""])) {
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($server_url != "") {
|
2019-10-03 23:33:41 +00:00
|
|
|
if (!GServer::check($server_url, $force)) {
|
2017-11-15 14:47:28 +00:00
|
|
|
if ($force) {
|
2018-01-27 02:38:34 +00:00
|
|
|
$fields = ['last_failure' => DateTimeFormat::utcNow()];
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Profile ".$profile.": Server ".$server_url." wasn't reachable.", Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$contact['server_url'] = $server_url;
|
|
|
|
}
|
|
|
|
|
2018-08-11 20:40:44 +00:00
|
|
|
if (in_array($gcontacts[0]["network"], ["", Protocol::FEED])) {
|
2017-11-15 15:53:16 +00:00
|
|
|
$server = q(
|
|
|
|
"SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::escape(Strings::normaliseLink($server_url))
|
2017-11-15 15:53:16 +00:00
|
|
|
);
|
2017-11-15 14:47:28 +00: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 16:28:29 +00:00
|
|
|
$server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", DBA::escape(Strings::normaliseLink($server_url)));
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
if ($server) {
|
2018-10-10 19:08:43 +00:00
|
|
|
$curlResult = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if ($curlResult->isSuccess() && ($curlResult->getBody() != "")) {
|
|
|
|
$noscrape = json_decode($curlResult->getBody(), true);
|
2017-11-15 14:47:28 +00: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 22:03:39 +00:00
|
|
|
$location = Profile::formatLocation($noscrape);
|
2017-11-15 14:47:28 +00: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 02:38:34 +00:00
|
|
|
//$contact["last_contact"] = DateTimeFormat::utcNow();
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
$contact = array_merge($contact, $noscrape);
|
|
|
|
|
2017-12-07 14:09:28 +00:00
|
|
|
GContact::update($contact);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-10 12:27:56 +00:00
|
|
|
if (!empty($noscrape["updated"])) {
|
2018-01-27 02:38:34 +00:00
|
|
|
$fields = ['last_contact' => DateTimeFormat::utcNow()];
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
return $noscrape["updated"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we only can poll the feed, then we only do this once a while
|
2017-11-15 15:53:16 +00:00
|
|
|
if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2017-12-07 14:09:28 +00:00
|
|
|
GContact::update($contact);
|
2017-11-15 14:47:28 +00: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 20:40:44 +00:00
|
|
|
if (($data["network"] == Protocol::OSTATUS) && self::alternateOStatusUrl($profile)
|
2018-11-08 16:28:29 +00:00
|
|
|
&& (Strings::normaliseLink($profile) == Strings::normaliseLink($data["alias"]))
|
|
|
|
&& (Strings::normaliseLink($profile) != Strings::normaliseLink($data["url"]))
|
2017-11-15 14:47:28 +00:00
|
|
|
) {
|
|
|
|
// Delete the old entry
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::delete('gcontact', ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
$gcontact = array_merge($gcontacts[0], $data);
|
|
|
|
|
|
|
|
$gcontact["server_url"] = $data["baseurl"];
|
|
|
|
|
|
|
|
try {
|
2017-12-07 14:09:28 +00:00
|
|
|
$gcontact = GContact::sanitize($gcontact);
|
|
|
|
GContact::update($gcontact);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2017-11-15 15:53:16 +00:00
|
|
|
self::lastUpdated($data["url"], $force);
|
2017-11-15 14:47:28 +00:00
|
|
|
} catch (Exception $e) {
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log($e->getMessage(), Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Profile ".$profile." was deleted", Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-11 20:40:44 +00:00
|
|
|
if (($data["poll"] == "") || (in_array($data["network"], [Protocol::FEED, Protocol::PHANTOM]))) {
|
2018-01-27 02:38:34 +00:00
|
|
|
$fields = ['last_failure' => DateTimeFormat::utcNow()];
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Profile ".$profile." wasn't reachable (profile)", Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contact = array_merge($contact, $data);
|
|
|
|
|
|
|
|
$contact["server_url"] = $data["baseurl"];
|
|
|
|
|
2017-12-07 14:09:28 +00:00
|
|
|
GContact::update($contact);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
$curlResult = Network::curl($data["poll"]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if (!$curlResult->isSuccess()) {
|
2018-01-27 02:38:34 +00:00
|
|
|
$fields = ['last_failure' => DateTimeFormat::utcNow()];
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Profile ".$profile." wasn't reachable (no feed)", Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
2018-07-28 05:35:27 +00:00
|
|
|
/// @TODO Avoid error supression here
|
2018-10-10 19:08:43 +00:00
|
|
|
@$doc->loadXML($curlResult->getBody());
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2017-12-17 20:24:57 +00:00
|
|
|
$xpath = new DOMXPath($doc);
|
2017-11-15 14:47:28 +00: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 14:22:14 +00: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 14:47:28 +00:00
|
|
|
|
2018-02-04 02:39:35 +00:00
|
|
|
if ($last_updated < $published) {
|
2017-11-15 14:47:28 +00:00
|
|
|
$last_updated = $published;
|
2018-02-04 02:39:35 +00:00
|
|
|
}
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-02-04 02:39:35 +00:00
|
|
|
if ($last_updated < $updated) {
|
2017-11-15 14:47:28 +00:00
|
|
|
$last_updated = $updated;
|
2018-02-04 02:39:35 +00:00
|
|
|
}
|
2017-11-15 14:47:28 +00: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 05:53:47 +00:00
|
|
|
$last_updated = DBA::NULL_DATETIME;
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 02:39:35 +00:00
|
|
|
|
2018-05-11 10:44:28 +00:00
|
|
|
$fields = ['last_contact' => DateTimeFormat::utcNow()];
|
|
|
|
|
|
|
|
if (!empty($last_updated)) {
|
|
|
|
$fields['updated'] = $last_updated;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
if (($gcontacts[0]["generation"] == 0)) {
|
2017-12-19 06:47:39 +00:00
|
|
|
$fields = ['generation' => 9];
|
2018-11-08 16:28:29 +00:00
|
|
|
DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Profile ".$profile." was last updated at ".$last_updated, Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-02-04 02:39:35 +00:00
|
|
|
return $last_updated;
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:53:16 +00:00
|
|
|
public static function updateNeeded($created, $updated, $last_failure, $last_contact)
|
|
|
|
{
|
2018-01-27 02:38:34 +00:00
|
|
|
$now = strtotime(DateTimeFormat::utcNow());
|
2017-11-15 14:47:28 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a list of all known servers
|
|
|
|
* @return array List of server urls
|
2019-01-07 15:24:06 +00:00
|
|
|
* @throws Exception
|
2017-11-15 14:47:28 +00:00
|
|
|
*/
|
2017-11-15 15:53:16 +00:00
|
|
|
public static function serverlist()
|
|
|
|
{
|
|
|
|
$r = q(
|
|
|
|
"SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
|
2017-11-15 14:47:28 +00:00
|
|
|
WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
|
|
|
|
ORDER BY `last_contact`
|
|
|
|
LIMIT 1000",
|
2018-08-11 20:40:44 +00:00
|
|
|
DBA::escape(Protocol::DFRN),
|
|
|
|
DBA::escape(Protocol::DIASPORA),
|
|
|
|
DBA::escape(Protocol::OSTATUS)
|
2017-11-15 15:53:16 +00:00
|
|
|
);
|
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
if (!DBA::isResult($r)) {
|
2017-11-15 14:47:28 +00: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 21:06:53 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2017-11-15 14:47:28 +00:00
|
|
|
*/
|
2017-12-21 07:27:20 +00:00
|
|
|
private static function fetchServerlist($poco)
|
2017-11-15 15:53:16 +00:00
|
|
|
{
|
2018-10-10 19:08:43 +00:00
|
|
|
$curlResult = Network::curl($poco . "/@server");
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if (!$curlResult->isSuccess()) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
$serverlist = json_decode($curlResult->getBody(), true);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
if (!is_array($serverlist)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($serverlist as $server) {
|
2018-07-28 12:52:57 +00:00
|
|
|
$server_url = str_replace("/index.php", "", $server['url']);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-11-08 16:28:29 +00:00
|
|
|
$r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", DBA::escape(Strings::normaliseLink($server_url)));
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
if (!DBA::isResult($r)) {
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Call server check for server ".$server_url, Logger::DEBUG);
|
2017-11-18 11:02:46 +00:00
|
|
|
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:27:20 +00:00
|
|
|
private static function discoverFederation()
|
2017-11-15 15:53:16 +00:00
|
|
|
{
|
|
|
|
$last = Config::get('poco', 'last_federation_discovery');
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
if ($last) {
|
|
|
|
$next = $last + (24 * 60 * 60);
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2017-11-15 14:47:28 +00:00
|
|
|
if ($next > time()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discover Friendica, Hubzilla and Diaspora servers
|
2018-10-10 19:08:43 +00:00
|
|
|
$curlResult = Network::fetchUrl("http://the-federation.info/pods.json");
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if (!empty($curlResult)) {
|
|
|
|
$servers = json_decode($curlResult, true);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (!empty($servers['pods'])) {
|
|
|
|
foreach ($servers['pods'] as $server) {
|
|
|
|
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://" . $server['host']);
|
2018-05-18 16:07:55 +00:00
|
|
|
}
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disvover Mastodon servers
|
2017-11-15 15:53:16 +00:00
|
|
|
if (!Config::get('system', 'ostatus_disabled')) {
|
2017-12-06 20:09:59 +00:00
|
|
|
$accesstoken = Config::get('system', 'instances_social_key');
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2017-12-06 20:09:59 +00:00
|
|
|
if (!empty($accesstoken)) {
|
|
|
|
$api = 'https://instances.social/api/1.0/instances/list?count=0';
|
2018-01-15 13:05:12 +00:00
|
|
|
$header = ['Authorization: Bearer '.$accesstoken];
|
2019-06-10 12:34:54 +00:00
|
|
|
$curlResult = Network::curl($api, false, ['headers' => $header]);
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
$servers = json_decode($curlResult->getBody(), true);
|
2018-07-28 05:35:27 +00:00
|
|
|
|
|
|
|
foreach ($servers['instances'] as $server) {
|
|
|
|
$url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
|
2017-12-06 20:09:59 +00:00
|
|
|
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
|
|
|
|
}
|
2017-11-15 14:47:28 +00: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 19:08:43 +00:00
|
|
|
// $curlResult = Network::curl($serverdata);
|
|
|
|
// if ($curlResult->isSuccess()) {
|
|
|
|
// $servers = json_decode($result->getBody(), true);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
// foreach($servers['data'] as $server)
|
2019-10-03 23:33:41 +00:00
|
|
|
// GServer::check($server['instance_address']);
|
2017-11-15 14:47:28 +00:00
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2017-11-15 15:53:16 +00:00
|
|
|
Config::set('poco', 'last_federation_discovery', time());
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 15:53:16 +00:00
|
|
|
public static function discoverSingleServer($id)
|
|
|
|
{
|
2019-02-08 13:38:13 +00:00
|
|
|
$server = DBA::selectFirst('gserver', ['poco', 'nurl', 'url', 'network'], ['id' => $id]);
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2019-02-08 13:38:13 +00:00
|
|
|
if (!DBA::isResult($server)) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discover new servers out there (Works from Friendica version 3.5.2)
|
2017-11-15 15:53:16 +00:00
|
|
|
self::fetchServerlist($server["poco"]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
// Fetch all users from the other server
|
2018-07-28 05:35:27 +00:00
|
|
|
$url = $server["poco"] . "/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2019-02-08 13:38:13 +00:00
|
|
|
Logger::info("Fetch all users from the server " . $server["url"]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
$curlResult = Network::curl($url);
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
|
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-08-02 17:07:20 +00:00
|
|
|
if (!empty($data)) {
|
|
|
|
self::discoverServer($data, 2);
|
|
|
|
}
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2019-02-08 13:38:13 +00:00
|
|
|
if (Config::get('system', 'poco_discovery') >= self::USERS_GCONTACTS) {
|
2017-11-15 15:53:16 +00:00
|
|
|
$timeframe = Config::get('system', 'poco_discovery_since');
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2017-11-15 14:47:28 +00:00
|
|
|
if ($timeframe == 0) {
|
|
|
|
$timeframe = 30;
|
|
|
|
}
|
|
|
|
|
2018-01-27 02:38:34 +00:00
|
|
|
$updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
|
2017-11-15 14:47:28 +00: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 19:08:43 +00:00
|
|
|
$curlResult = Network::curl($url);
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
|
2019-02-08 13:38:13 +00:00
|
|
|
Logger::info("Fetch all global contacts from the server " . $server["nurl"]);
|
2018-10-10 19:08:43 +00:00
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2018-08-02 17:07:20 +00:00
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
$success = self::discoverServer($data);
|
|
|
|
}
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:23:22 +00: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 15:53:16 +00:00
|
|
|
self::discoverServerUsers($data, $server);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 02:38:34 +00:00
|
|
|
$fields = ['last_poco_query' => DateTimeFormat::utcNow()];
|
2018-07-20 12:19:26 +00:00
|
|
|
DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// If the server hadn't replied correctly, then force a sanity check
|
2019-10-03 23:33:41 +00:00
|
|
|
GServer::check($server["url"], true);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
// If we couldn't reach the server, we will try it some time later
|
2018-01-27 02:38:34 +00:00
|
|
|
$fields = ['last_poco_query' => DateTimeFormat::utcNow()];
|
2018-07-20 12:19:26 +00:00
|
|
|
DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 15:53:16 +00:00
|
|
|
public static function discover($complete = false)
|
|
|
|
{
|
2017-11-15 14:47:28 +00:00
|
|
|
// Update the server list
|
2017-11-15 15:53:16 +00:00
|
|
|
self::discoverFederation();
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
$no_of_queries = 5;
|
|
|
|
|
2018-07-22 16:50:13 +00:00
|
|
|
$requery_days = intval(Config::get('system', 'poco_requery_days'));
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
if ($requery_days == 0) {
|
|
|
|
$requery_days = 7;
|
|
|
|
}
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-07-22 16:50:13 +00:00
|
|
|
$last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-22 16:50:13 +00: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 05:35:27 +00:00
|
|
|
|
2018-07-22 16:50:13 +00:00
|
|
|
if (DBA::isResult($gservers)) {
|
|
|
|
foreach ($gservers as $gserver) {
|
2019-10-03 23:33:41 +00:00
|
|
|
if (!GServer::check($gserver['url'])) {
|
2017-11-15 14:47:28 +00:00
|
|
|
// The server is not reachable? Okay, then we will try it later
|
2018-01-27 02:38:34 +00:00
|
|
|
$fields = ['last_poco_query' => DateTimeFormat::utcNow()];
|
2018-07-22 16:50:13 +00:00
|
|
|
DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
|
2017-11-15 14:47:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log('Update directory from server ' . $gserver['url'] . ' with ID ' . $gserver['id'], Logger::DEBUG);
|
2018-07-22 16:50:13 +00:00
|
|
|
Worker::add(PRIORITY_LOW, 'DiscoverPoCo', 'update_server_directory', (int) $gserver['id']);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-22 16:50:13 +00:00
|
|
|
if (!$complete && ( --$no_of_queries == 0)) {
|
2017-11-15 14:47:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
private static function discoverServerUsers(array $data, array $server)
|
2017-11-15 15:53:16 +00:00
|
|
|
{
|
2018-07-28 05:35:27 +00:00
|
|
|
if (!isset($data['entry'])) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
foreach ($data['entry'] as $entry) {
|
2018-07-22 16:45:24 +00:00
|
|
|
$username = '';
|
2018-07-28 05:35:27 +00:00
|
|
|
|
|
|
|
if (isset($entry['urls'])) {
|
|
|
|
foreach ($entry['urls'] as $url) {
|
|
|
|
if ($url['type'] == 'profile') {
|
|
|
|
$profile_url = $url['value'];
|
2018-07-22 16:45:24 +00:00
|
|
|
$path_array = explode('/', parse_url($profile_url, PHP_URL_PATH));
|
|
|
|
$username = end($path_array);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-07-22 16:45:24 +00:00
|
|
|
if ($username != '') {
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log('Fetch contacts for the user ' . $username . ' from the server ' . $server['nurl'], Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
// Fetch all contacts from a given user from the other server
|
2018-07-22 16:45:24 +00:00
|
|
|
$url = $server['poco'] . '/' . $username . '/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation';
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
$curlResult = Network::curl($url);
|
2018-07-28 05:35:27 +00:00
|
|
|
|
2018-10-10 19:08:43 +00:00
|
|
|
if ($curlResult->isSuccess()) {
|
2018-12-22 16:23:18 +00:00
|
|
|
$data = json_decode($curlResult->getBody(), true);
|
2018-08-02 17:07:20 +00:00
|
|
|
|
|
|
|
if (!empty($data)) {
|
|
|
|
self::discoverServer($data, 3);
|
|
|
|
}
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
private static function discoverServer(array $data, $default_generation = 0)
|
2017-11-15 15:53:16 +00:00
|
|
|
{
|
2018-07-28 05:35:27 +00:00
|
|
|
if (empty($data['entry'])) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = false;
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
foreach ($data['entry'] as $entry) {
|
2017-11-15 14:47:28 +00:00
|
|
|
$profile_url = '';
|
|
|
|
$profile_photo = '';
|
|
|
|
$connect_url = '';
|
|
|
|
$name = '';
|
|
|
|
$network = '';
|
2018-10-21 05:53:47 +00:00
|
|
|
$updated = DBA::NULL_DATETIME;
|
2017-11-15 14:47:28 +00:00
|
|
|
$location = '';
|
|
|
|
$about = '';
|
|
|
|
$keywords = '';
|
|
|
|
$gender = '';
|
|
|
|
$contact_type = -1;
|
|
|
|
$generation = $default_generation;
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (!empty($entry['displayName'])) {
|
|
|
|
$name = $entry['displayName'];
|
2018-07-15 18:36:20 +00:00
|
|
|
}
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['urls'])) {
|
|
|
|
foreach ($entry['urls'] as $url) {
|
|
|
|
if ($url['type'] == 'profile') {
|
|
|
|
$profile_url = $url['value'];
|
2017-11-15 14:47:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-28 05:35:27 +00:00
|
|
|
if ($url['type'] == 'webfinger') {
|
|
|
|
$connect_url = str_replace('acct:' , '', $url['value']);
|
2017-11-15 14:47:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['photos'])) {
|
|
|
|
foreach ($entry['photos'] as $photo) {
|
|
|
|
if ($photo['type'] == 'profile') {
|
|
|
|
$profile_photo = $photo['value'];
|
2017-11-15 14:47:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['updated'])) {
|
|
|
|
$updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['network'])) {
|
|
|
|
$network = $entry['network'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['currentLocation'])) {
|
|
|
|
$location = $entry['currentLocation'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['aboutMe'])) {
|
|
|
|
$about = HTML::toBBCode($entry['aboutMe']);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['gender'])) {
|
|
|
|
$gender = $entry['gender'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['generation']) && ($entry['generation'] > 0)) {
|
|
|
|
$generation = ++$entry['generation'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
|
|
|
|
$contact_type = $entry['contactType'];
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 05:35:27 +00:00
|
|
|
if (isset($entry['tags'])) {
|
|
|
|
foreach ($entry['tags'] as $tag) {
|
2017-11-15 14:47:28 +00:00
|
|
|
$keywords = implode(", ", $tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($generation > 0) {
|
|
|
|
$success = true;
|
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Store profile ".$profile_url, Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
|
2018-01-15 13:05:12 +00:00
|
|
|
$gcontact = ["url" => $profile_url,
|
2017-11-15 14:47:28 +00: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 13:05:12 +00:00
|
|
|
"generation" => $generation];
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
try {
|
2017-12-07 14:09:28 +00:00
|
|
|
$gcontact = GContact::sanitize($gcontact);
|
|
|
|
GContact::update($gcontact);
|
2017-11-15 14:47:28 +00:00
|
|
|
} catch (Exception $e) {
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log($e->getMessage(), Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 13:58:45 +00:00
|
|
|
Logger::log("Done for profile ".$profile_url, Logger::DEBUG);
|
2017-11-15 14:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|