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;
|
2019-10-02 17:10:42 +02:00
|
|
|
use Friendica\Model\Contact;
|
2017-12-07 15:09:28 +01:00
|
|
|
use Friendica\Model\GContact;
|
2019-10-04 01:33:41 +02:00
|
|
|
use Friendica\Model\GServer;
|
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
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Fetch POCO data
|
2017-11-15 15:47:28 +01:00
|
|
|
*
|
|
|
|
* @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
|
2019-12-20 22:21:25 +01:00
|
|
|
Worker::add(PRIORITY_LOW, 'FetchPoCo', (int)$cid, (int)$uid, (int)$zcid, $url);
|
2017-11-15 15:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Fetch POCO data from the worker
|
2017-11-15 15:47:28 +01:00
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Returns a list of all known servers
|
2017-11-15 15:47:28 +01:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Fetch server list from remote servers and adds them when they are new.
|
2017-11-15 15:47:28 +01:00
|
|
|
*
|
|
|
|
* @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);
|
2019-12-21 19:11:51 +01:00
|
|
|
Worker::add(PRIORITY_LOW, 'UpdateGServer', $server_url);
|
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
|
2019-10-04 07:42:54 +02:00
|
|
|
GServer::check($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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|