]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/PortableContact.php
Merge pull request #5072 from Alkarex/ru-plural-intval-bug
[friendica.git] / src / Protocol / PortableContact.php
index aea68eb998e9bd9043e528f0bc516c1fefe8d45c..f824be40f0e8065c796035d3976f7fbec041c982 100644 (file)
@@ -9,22 +9,22 @@
 
 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 Friendica\Protocol\Diaspora;
 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
 {
@@ -144,7 +144,7 @@ class PortableContact
                        }
 
                        if (isset($entry->updated)) {
-                               $updated = date(Temporal::MYSQL, strtotime($entry->updated));
+                               $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
                        }
 
                        if (isset($entry->network)) {
@@ -156,7 +156,7 @@ class PortableContact
                        }
 
                        if (isset($entry->aboutMe)) {
-                               $about = html2bbcode($entry->aboutMe);
+                               $about = HTML::toBBCode($entry->aboutMe);
                        }
 
                        if (isset($entry->gender)) {
@@ -315,9 +315,10 @@ class PortableContact
                $contact = ["url" => $profile];
 
                if ($gcontacts[0]["created"] <= NULL_DATE) {
-                       $contact['created'] = Temporal::utcNow();
+                       $contact['created'] = DateTimeFormat::utcNow();
                }
 
+               $server_url = '';
                if ($force) {
                        $server_url = normalise_link(self::detectServer($profile));
                }
@@ -338,7 +339,7 @@ class PortableContact
                if ($server_url != "") {
                        if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
                                if ($force) {
-                                       $fields = ['last_failure' => Temporal::utcNow()];
+                                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
                                        dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
                                }
 
@@ -412,14 +413,14 @@ class PortableContact
 
                                                // Set the date of the last contact
                                                /// @todo By now the function "update_gcontact" doesn't work with this field
-                                               //$contact["last_contact"] = Temporal::utcNow();
+                                               //$contact["last_contact"] = DateTimeFormat::utcNow();
 
                                                $contact = array_merge($contact, $noscrape);
 
                                                GContact::update($contact);
 
                                                if (trim($noscrape["updated"]) != "") {
-                                                       $fields = ['last_contact' => Temporal::utcNow()];
+                                                       $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);
@@ -468,7 +469,7 @@ class PortableContact
                }
 
                if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) {
-                       $fields = ['last_failure' => Temporal::utcNow()];
+                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
                        dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                        logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
@@ -484,7 +485,7 @@ class PortableContact
                $feedret = Network::curl($data["poll"]);
 
                if (!$feedret["success"]) {
-                       $fields = ['last_failure' => Temporal::utcNow()];
+                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
                        dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                        logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
@@ -502,14 +503,16 @@ class PortableContact
                $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
@@ -518,7 +521,13 @@ class PortableContact
                                $last_updated = NULL_DATE;
                        }
                }
-               $fields = ['updated' => DBM::date($last_updated), 'last_contact' => DBM::date()];
+
+               $fields = ['last_contact' => DateTimeFormat::utcNow()];
+
+               if (!empty($last_updated)) {
+                       $fields['updated'] = $last_updated;
+               }
+
                dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                if (($gcontacts[0]["generation"] == 0)) {
@@ -528,12 +537,12 @@ class PortableContact
 
                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::utcNow());
+               $now = strtotime(DateTimeFormat::utcNow());
 
                if ($updated > $last_contact) {
                        $contact_time = strtotime($updated);
@@ -922,7 +931,7 @@ class PortableContact
                $gserver = dba::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
                if (DBM::is_result($gserver)) {
                        if ($gserver["created"] <= NULL_DATE) {
-                               $fields = ['created' => Temporal::utcNow()];
+                               $fields = ['created' => DateTimeFormat::utcNow()];
                                $condition = ['nurl' => normalise_link($server_url)];
                                dba::update('gserver', $fields, $condition);
                        }
@@ -942,6 +951,15 @@ class PortableContact
                        $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);
@@ -969,7 +987,7 @@ class PortableContact
                // Mastodon uses the "@" for user profiles.
                // But this can be misunderstood.
                if (parse_url($server_url, PHP_URL_USER) != '') {
-                       dba::update('gserver', ['last_failure' => Temporal::utcNow()], ['nurl' => normalise_link($server_url)]);
+                       dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
                        return false;
                }
 
@@ -985,7 +1003,7 @@ class PortableContact
                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::utcNow()], ['nurl' => normalise_link($server_url)]);
+                       dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
                        return false;
                }
 
@@ -1000,7 +1018,7 @@ class PortableContact
                        // 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::utcNow()], ['nurl' => normalise_link($server_url)]);
+                               dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
                                return false;
                        }
 
@@ -1299,7 +1317,7 @@ class PortableContact
                                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;
@@ -1332,9 +1350,9 @@ class PortableContact
 
                if ($failure) {
                        $last_contact = $orig_last_contact;
-                       $last_failure = Temporal::utcNow();
+                       $last_failure = DateTimeFormat::utcNow();
                } else {
-                       $last_contact = Temporal::utcNow();
+                       $last_contact = DateTimeFormat::utcNow();
                        $last_failure = $orig_last_failure;
                }
 
@@ -1362,14 +1380,85 @@ class PortableContact
                        dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
                } elseif (!$failure) {
                        $fields['nurl'] = normalise_link($server_url);
-                       $fields['created'] = Temporal::utcNow();
+                       $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 = mb_strtolower($tag);
+                               $tags[$tag] = $tag;
+                       }
+
+                       foreach ($tags as $tag) {
+                               dba::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true);
+                       }
+               }
+
+               // Create or update the relay contact
+               $fields = [];
+               if (isset($data->protocols)) {
+                       if (isset($data->protocols->diaspora)) {
+                               $fields['network'] = NETWORK_DIASPORA;
+                               if (isset($data->protocols->diaspora->receive)) {
+                                       $fields['batch'] = $data->protocols->diaspora->receive;
+                               } elseif (is_string($data->protocols->diaspora)) {
+                                       $fields['batch'] = $data->protocols->diaspora;
+                               }
+                       }
+                       if (isset($data->protocols->dfrn)) {
+                               $fields['network'] = NETWORK_DFRN;
+                               if (isset($data->protocols->dfrn->receive)) {
+                                       $fields['batch'] = $data->protocols->dfrn->receive;
+                               } elseif (is_string($data->protocols->dfrn)) {
+                                       $fields['batch'] = $data->protocols->dfrn;
+                               }
+                       }
+               }
+               Diaspora::setRelayContact($server_url, $fields);
+       }
+
        /**
         * @brief Returns a list of all known servers
         * @return array List of server urls
@@ -1451,8 +1540,8 @@ class PortableContact
                                $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);
                                        }
@@ -1507,7 +1596,7 @@ class PortableContact
                                        $timeframe = 30;
                                }
 
-                               $updatedSince = date(Temporal::MYSQL, 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";
@@ -1526,7 +1615,7 @@ class PortableContact
                                }
                        }
 
-                       $fields = ['last_poco_query' => Temporal::utcNow()];
+                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
                        dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 
                        return true;
@@ -1535,7 +1624,7 @@ class PortableContact
                        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::utcNow()];
+                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
                        dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 
                        return false;
@@ -1561,7 +1650,7 @@ class PortableContact
                        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::utcNow()];
+                                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
                                        dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
                                        continue;
                                }
@@ -1654,7 +1743,7 @@ class PortableContact
                        }
 
                        if (isset($entry->updated)) {
-                               $updated = date(Temporal::MYSQL, strtotime($entry->updated));
+                               $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
                        }
 
                        if (isset($entry->network)) {
@@ -1666,7 +1755,7 @@ class PortableContact
                        }
 
                        if (isset($entry->aboutMe)) {
-                               $about = html2bbcode($entry->aboutMe);
+                               $about = HTML::toBBCode($entry->aboutMe);
                        }
 
                        if (isset($entry->gender)) {