namespace Friendica\Protocol;
+use Friendica\Content\Text\HTML;
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Model\GContact;
use Friendica\Model\Profile;
use Friendica\Network\Probe;
+use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
-use Friendica\Util\Temporal;
use dba;
use DOMDocument;
use DOMXPath;
use Exception;
require_once 'include/dba.php';
-require_once 'include/datetime.php';
-require_once 'include/html2bbcode.php';
class PortableContact
{
}
if (isset($entry->updated)) {
- $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
+ $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
}
if (isset($entry->network)) {
}
if (isset($entry->aboutMe)) {
- $about = html2bbcode($entry->aboutMe);
+ $about = HTML::toBBCode($entry->aboutMe);
}
if (isset($entry->gender)) {
$contact = ["url" => $profile];
if ($gcontacts[0]["created"] <= NULL_DATE) {
- $contact['created'] = Temporal::convert();
+ $contact['created'] = DateTimeFormat::utcNow();
}
+ $server_url = '';
if ($force) {
$server_url = normalise_link(self::detectServer($profile));
}
if ($server_url != "") {
if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
if ($force) {
- $fields = ['last_failure' => Temporal::convert()];
+ $fields = ['last_failure' => DateTimeFormat::utcNow()];
dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
}
// Set the date of the last contact
/// @todo By now the function "update_gcontact" doesn't work with this field
- //$contact["last_contact"] = Temporal::convert();
+ //$contact["last_contact"] = DateTimeFormat::utcNow();
$contact = array_merge($contact, $noscrape);
GContact::update($contact);
if (trim($noscrape["updated"]) != "") {
- $fields = ['last_contact' => Temporal::convert()];
+ $fields = ['last_contact' => DateTimeFormat::utcNow()];
dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
}
if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) {
- $fields = ['last_failure' => Temporal::convert()];
+ $fields = ['last_failure' => DateTimeFormat::utcNow()];
dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
$feedret = Network::curl($data["poll"]);
if (!$feedret["success"]) {
- $fields = ['last_failure' => Temporal::convert()];
+ $fields = ['last_failure' => DateTimeFormat::utcNow()];
dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
$last_updated = "";
foreach ($entries as $entry) {
- $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
- $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
+ $published = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue);
+ $updated = DateTimeFormat::utc($xpath->query('atom:updated/text()' , $entry)->item(0)->nodeValue);
- if ($last_updated < $published)
+ if ($last_updated < $published) {
$last_updated = $published;
+ }
- if ($last_updated < $updated)
+ if ($last_updated < $updated) {
$last_updated = $updated;
+ }
}
// Maybe there aren't any entries. Then check if it is a valid feed
$last_updated = NULL_DATE;
}
}
- $fields = ['updated' => DBM::date($last_updated), 'last_contact' => DBM::date()];
+
+ $fields = ['updated' => $last_updated, 'last_contact' => DateTimeFormat::utcNow()];
dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
if (($gcontacts[0]["generation"] == 0)) {
logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
- return($last_updated);
+ return $last_updated;
}
public static function updateNeeded($created, $updated, $last_failure, $last_contact)
{
- $now = strtotime(Temporal::convert());
+ $now = strtotime(DateTimeFormat::utcNow());
if ($updated > $last_contact) {
$contact_time = strtotime($updated);
$gserver = dba::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
if (DBM::is_result($gserver)) {
if ($gserver["created"] <= NULL_DATE) {
- $fields = ['created' => Temporal::convert()];
+ $fields = ['created' => DateTimeFormat::utcNow()];
$condition = ['nurl' => normalise_link($server_url)];
dba::update('gserver', $fields, $condition);
}
$register_policy = $gserver["register_policy"];
$registered_users = $gserver["registered-users"];
+ // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
+ // It can happen that a zero date is in the database, but storing it again is forbidden.
+ if ($last_contact < NULL_DATE) {
+ $last_contact = NULL_DATE;
+ }
+ if ($last_failure < NULL_DATE) {
+ $last_failure = NULL_DATE;
+ }
+
if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
return ($last_contact >= $last_failure);
// Mastodon uses the "@" for user profiles.
// But this can be misunderstood.
if (parse_url($server_url, PHP_URL_USER) != '') {
- dba::update('gserver', ['last_failure' => Temporal::convert()], ['nurl' => normalise_link($server_url)]);
+ dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
return false;
}
if (DBM::is_result($gserver) && ($orig_server_url == $server_url) &&
($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
- dba::update('gserver', ['last_failure' => Temporal::convert()], ['nurl' => normalise_link($server_url)]);
+ dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
return false;
}
// Quit if there is a timeout
if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
- dba::update('gserver', ['last_failure' => Temporal::convert()], ['nurl' => normalise_link($server_url)]);
+ dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
return false;
}
if (isset($data->version)) {
$network = NETWORK_DFRN;
- $noscrape = $data->no_scrape_url;
+ $noscrape = defaults($data->no_scrape_url, '');
$version = $data->version;
$site_name = $data->site_name;
$info = $data->info;
if ($failure) {
$last_contact = $orig_last_contact;
- $last_failure = Temporal::convert();
+ $last_failure = DateTimeFormat::utcNow();
} else {
- $last_contact = Temporal::convert();
+ $last_contact = DateTimeFormat::utcNow();
$last_failure = $orig_last_failure;
}
dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
} elseif (!$failure) {
$fields['nurl'] = normalise_link($server_url);
- $fields['created'] = Temporal::convert();
+ $fields['created'] = DateTimeFormat::utcNow();
dba::insert('gserver', $fields);
}
+
+ if (!$failure && in_array($fields['network'], [NETWORK_DFRN, NETWORK_DIASPORA])) {
+ self::discoverRelay($server_url);
+ }
+
logger("End discovery for server " . $server_url, LOGGER_DEBUG);
return !$failure;
}
+ /**
+ * @brief Fetch relay data from a given server url
+ *
+ * @param string $server_url address of the server
+ */
+ private static function discoverRelay($server_url)
+ {
+ logger("Discover relay data for server " . $server_url, LOGGER_DEBUG);
+
+ $serverret = Network::curl($server_url."/.well-known/x-social-relay");
+ if (!$serverret["success"]) {
+ return;
+ }
+
+ $data = json_decode($serverret['body']);
+ if (!is_object($data)) {
+ return;
+ }
+
+ $gserver = dba::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => normalise_link($server_url)]);
+ if (!DBM::is_result($gserver)) {
+ return;
+ }
+
+ if (($gserver['relay-subscribe'] != $data->subscribe) || ($gserver['relay-scope'] != $data->scope)) {
+ $fields = ['relay-subscribe' => $data->subscribe, 'relay-scope' => $data->scope];
+ dba::update('gserver', $fields, ['id' => $gserver['id']]);
+ }
+
+ dba::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
+ if ($data->scope == 'tags') {
+ // Avoid duplicates
+ $tags = [];
+ foreach ($data->tags as $tag) {
+ $tag = strtolower($tag);
+ $tags[$tag] = $tag;
+ }
+
+ foreach ($tags as $tag) {
+ dba::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag]);
+ }
+ }
+ }
+
/**
* @brief Returns a list of all known servers
* @return array List of server urls
$header = ['Authorization: Bearer '.$accesstoken];
$serverdata = Network::curl($api, false, $redirects, ['headers' => $header]);
if ($serverdata['success']) {
- $servers = json_decode($serverdata['body']);
- foreach ($servers->instances as $server) {
+ $servers = json_decode($serverdata['body']);
+ foreach ($servers->instances as $server) {
$url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
}
$timeframe = 30;
}
- $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
+ $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
// 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";
}
}
- $fields = ['last_poco_query' => Temporal::convert()];
+ $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
return true;
self::checkServer($server["url"], $server["network"], true);
// If we couldn't reach the server, we will try it some time later
- $fields = ['last_poco_query' => Temporal::convert()];
+ $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
return false;
foreach ($r as $server) {
if (!self::checkServer($server["url"], $server["network"])) {
// The server is not reachable? Okay, then we will try it later
- $fields = ['last_poco_query' => Temporal::convert()];
+ $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
continue;
}
}
if (isset($entry->updated)) {
- $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
+ $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
}
if (isset($entry->network)) {
}
if (isset($entry->aboutMe)) {
- $about = html2bbcode($entry->aboutMe);
+ $about = HTML::toBBCode($entry->aboutMe);
}
if (isset($entry->gender)) {