]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/DFRN.php
Configuration for the number of languages
[friendica.git] / src / Protocol / DFRN.php
index a86cf2094dc2343fc353426da5bbd52810dfb3df..1ab12089de0c0ff588262be44388ddda9b68e425 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -34,7 +34,6 @@ use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\Event;
-use Friendica\Model\FContact;
 use Friendica\Model\GServer;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
@@ -45,6 +44,7 @@ use Friendica\Model\Post;
 use Friendica\Model\Profile;
 use Friendica\Model\Tag;
 use Friendica\Model\User;
+use Friendica\Network\HTTPException;
 use Friendica\Network\Probe;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
@@ -88,7 +88,7 @@ class DFRN
                $contact['senderName'] = $contact['name'];
 
                if ($uid != 0) {
-                       $condition = ['uid' => $uid, 'account_expired' => false, 'account_removed' => false];
+                       $condition = ['uid' => $uid, 'verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false];
                        $user = DBA::selectFirst('user', [], $condition);
                        if (!DBA::isResult($user)) {
                                return [];
@@ -301,16 +301,15 @@ class DFRN
                        DI::config()->set('system', 'site_pubkey', $res['pubkey']);
                }
 
-               $profilephotos = Photo::selectToArray(['resource-id' , 'scale'], ['profile' => true, 'uid' => $uid], ['order' => ['scale']]);
+               $profilephotos = Photo::selectToArray(['resource-id', 'scale', 'type'], ['profile' => true, 'uid' => $uid], ['order' => ['scale']]);
 
                $photos = [];
                $ext = Images::supportedTypes();
 
                foreach ($profilephotos as $p) {
-                       $photos[$p['scale']] = DI::baseUrl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
+                       $photos[$p['scale']] = DI::baseUrl() . '/photo/' . $p['resource-id'] . '-' . $p['scale'] . '.' . $ext[$p['type']];
                }
 
-
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
 
@@ -609,7 +608,7 @@ class DFRN
 
                        /// @Todo
                        /// - Check real image type and image size
-                       /// - Check which of these boths elements we should use
+                       /// - Check which of these elements we should use
                        $attributes = [
                                'rel' => 'photo',
                                'type' => 'image/jpeg',
@@ -711,7 +710,7 @@ class DFRN
         */
        private static function getAttachment($doc, $root, array $item)
        {
-               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
+               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
                        $attributes = ['rel' => 'enclosure',
                                'href' => $attachment['url'],
                                'type' => $attachment['mimetype']];
@@ -775,6 +774,7 @@ class DFRN
                }
 
                $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
+               $body = Post\Media::addHTMLAttachmentToBody($item['uri-id'], $body);
 
                if ($item['private'] == Item::PRIVATE) {
                        $body = Item::fixPrivatePhotos($body, $owner['uid'], $item, $cid);
@@ -878,7 +878,7 @@ class DFRN
 
                XML::addElement($doc, $entry, 'dfrn:diaspora_guid', $item['guid']);
 
-               // The signed text contains the content in Markdown, the sender handle and the signatur for the content
+               // The signed text contains the content in Markdown, the sender handle and the signature for the content
                // It is needed for relayed comments to Diaspora.
                if ($item['signed_text']) {
                        $sign = base64_encode(json_encode(['signed_text' => $item['signed_text'],'signature' => '','signer' => '']));
@@ -981,12 +981,12 @@ class DFRN
                                }
                        }
 
-                       $fcontact = FContact::getByURL($contact['addr']);
-                       if (empty($fcontact)) {
+                       try {
+                               $pubkey = DI::dsprContact()->getByAddr(WebFingerUri::fromString($contact['addr']))->pubKey;
+                       } catch (HTTPException\NotFoundException|\InvalidArgumentException $e) {
                                Logger::notice('Unable to find contact details for ' . $contact['id'] . ' - ' . $contact['addr']);
                                return -22;
                        }
-                       $pubkey = $fcontact['pubkey'] ?? '';
                } else {
                        $pubkey = '';
                }
@@ -1015,6 +1015,10 @@ class DFRN
                $xml = $postResult->getBody();
 
                $curl_stat = $postResult->getReturnCode();
+               if (!empty($contact['gsid']) && ($postResult->isTimeout() || empty($curl_stat))) {
+                       GServer::setFailureById($contact['gsid']);
+               }
+
                if (empty($curl_stat) || empty($xml)) {
                        Logger::notice('Empty answer from ' . $contact['id'] . ' - ' . $dest_url);
                        return -9; // timed out
@@ -1036,6 +1040,10 @@ class DFRN
                        return -23;
                }
 
+               if (!empty($contact['gsid'])) {
+                       GServer::setReachableById($contact['gsid'], Protocol::DFRN);
+               }
+
                if (!empty($res->message)) {
                        Logger::info('Transmit to ' . $dest_url . ' returned status '.$res->status.' - '.$res->message);
                }
@@ -1066,8 +1074,8 @@ class DFRN
 
                $fields = ['id', 'uid', 'url', 'network', 'avatar-date', 'avatar', 'name-date', 'uri-date', 'addr',
                        'name', 'nick', 'about', 'location', 'keywords', 'xmpp', 'bdyear', 'bd', 'hidden', 'contact-type'];
-               $condition = ["`uid` = ? AND `nurl` = ? AND `network` != ? AND NOT `pending` AND NOT `blocked`",
-                       $importer["importer_uid"], Strings::normaliseLink($author["link"]), Protocol::STATUSNET];
+               $condition = ["`uid` = ? AND `nurl` = ? AND NOT `pending` AND NOT `blocked`",
+                       $importer["importer_uid"], Strings::normaliseLink($author["link"])];
 
                if ($importer['account-type'] != User::ACCOUNT_TYPE_COMMUNITY) {
                        $condition = DBA::mergeConditions($condition, ['rel' => [Contact::SHARING, Contact::FRIEND]]);
@@ -1334,7 +1342,7 @@ class DFRN
         */
        private static function processMail(DOMXPath $xpath, DOMNode $mail, array $importer)
        {
-               Logger::notice("Processing mails");
+               Logger::info("Processing mails");
 
                $msg = [];
                $msg['uid'] = $importer['importer_uid'];
@@ -1362,7 +1370,7 @@ class DFRN
         */
        private static function processSuggestion(DOMXPath $xpath, DOMNode $suggestion, array $importer)
        {
-               Logger::notice('Processing suggestions');
+               Logger::info('Processing suggestions');
 
                $url = $xpath->evaluate('string(dfrn:url[1]/text())', $suggestion);
                $cid = Contact::getIdForURL($url);
@@ -1438,7 +1446,7 @@ class DFRN
         */
        private static function processRelocation(DOMXPath $xpath, DOMNode $relocation, array $importer): bool
        {
-               Logger::notice("Processing relocations");
+               Logger::info("Processing relocations");
 
                /// @TODO Rewrite this to one statement
                $relocate = [];
@@ -1491,7 +1499,7 @@ class DFRN
 
                Contact::updateAvatar($importer['id'], $relocate['avatar'], true);
 
-               Logger::notice('Contacts are updated.');
+               Logger::info('Contacts are updated.');
 
                /// @TODO
                /// merge with current record, current contents have priority
@@ -1577,7 +1585,7 @@ class DFRN
                Logger::info('Process verb ' . $item['verb'] . ' and object-type ' . $item['object-type'] . ' for entrytype ' . $entrytype);
 
                if (($entrytype == self::TOP_LEVEL) && !empty($importer['id'])) {
-                       // The filling of the the "contact" variable is done for legcy reasons
+                       // The filling of the "contact" variable is done for legacy reasons
                        // The functions below are partly used by ostatus.php as well - where we have this variable
                        $contact = Contact::selectFirst([], ['id' => $importer['id']]);
 
@@ -1586,22 +1594,22 @@ class DFRN
                        // Big question: Do we need these functions? They were part of the "consume_feed" function.
                        // This function once was responsible for DFRN and OStatus.
                        if ($activity->match($item['verb'], Activity::FOLLOW)) {
-                               Logger::notice("New follower");
+                               Logger::info("New follower");
                                Contact::addRelationship($importer, $contact, $item);
                                return false;
                        }
                        if ($activity->match($item['verb'], Activity::UNFOLLOW)) {
-                               Logger::notice("Lost follower");
+                               Logger::info("Lost follower");
                                Contact::removeFollower($contact);
                                return false;
                        }
                        if ($activity->match($item['verb'], Activity::REQ_FRIEND)) {
-                               Logger::notice("New friend request");
+                               Logger::info("New friend request");
                                Contact::addRelationship($importer, $contact, $item, true);
                                return false;
                        }
                        if ($activity->match($item['verb'], Activity::UNFRIEND)) {
-                               Logger::notice("Lost sharer");
+                               Logger::info("Lost sharer");
                                Contact::removeSharer($contact);
                                return false;
                        }
@@ -1705,7 +1713,7 @@ class DFRN
         * Checks if an incoming message is wanted
         *
         * @param array $item
-        * @param array $imporer
+        * @param array $importer
         * @return boolean Is the message wanted?
         */
        private static function isSolicitedMessage(array $item, array $importer): bool
@@ -1751,7 +1759,7 @@ class DFRN
         */
        private static function processEntry(array $header, DOMXPath $xpath, DOMNode $entry, array $importer, string $xml, int $protocol)
        {
-               Logger::notice("Processing entries");
+               Logger::info("Processing entries");
 
                $item = $header;
 
@@ -2072,6 +2080,7 @@ class DFRN
                        // This is my contact on another system, but it's really me.
                        // Turn this into a wall post.
                        $notify = Item::isRemoteSelf($importer, $item);
+                       $item['wall'] = (bool)$notify;
 
                        $posted_id = Item::insert($item, $notify);
 
@@ -2098,7 +2107,7 @@ class DFRN
         */
        private static function processDeletion(DOMXPath $xpath, DOMNode $deletion, array $importer)
        {
-               Logger::notice("Processing deletions");
+               Logger::info("Processing deletions");
                $uri = null;
 
                foreach ($deletion->attributes as $attributes) {
@@ -2211,8 +2220,8 @@ class DFRN
                        GServer::setProtocol($importer['gsid'], Post\DeliveryData::DFRN);
                }
 
-               // is it a public forum? Private forums aren't exposed with this method
-               $forum = intval(XML::getFirstNodeValue($xpath, '/atom:feed/dfrn:community/text()'));
+               // is it a public group? Private groups aren't exposed with this method
+               $group = intval(XML::getFirstNodeValue($xpath, '/atom:feed/dfrn:community/text()'));
 
                // The account type is new since 3.5.1
                if ($xpath->query('/atom:feed/dfrn:account_type')->length > 0) {
@@ -2226,17 +2235,17 @@ class DFRN
                                // Updating the public contact as well
                                Contact::update(['contact-type' => $accounttype], ['uid' => 0, 'nurl' => $importer['nurl']]);
                        }
-                       // A forum contact can either have set "forum" or "prv" - but not both
+                       // A group contact can either have set "forum" or "prv" - but not both
                        if ($accounttype == User::ACCOUNT_TYPE_COMMUNITY) {
-                               // It's a forum, so either set the public or private forum flag
-                               $condition = ['(`forum` != ? OR `prv` != ?) AND `id` = ?', $forum, !$forum, $importer['id']];
-                               Contact::update(['forum' => $forum, 'prv' => !$forum], $condition);
+                               // It's a group, so either set the public or private forum flag
+                               $condition = ['(`forum` != ? OR `prv` != ?) AND `id` = ?', $group, !$group, $importer['id']];
+                               Contact::update(['forum' => $group, 'prv' => !$group], $condition);
 
                                // Updating the public contact as well
-                               $condition = ['(`forum` != ? OR `prv` != ?) AND `uid` = 0 AND `nurl` = ?', $forum, !$forum, $importer['nurl']];
-                               Contact::update(['forum' => $forum, 'prv' => !$forum], $condition);
+                               $condition = ['(`forum` != ? OR `prv` != ?) AND `uid` = 0 AND `nurl` = ?', $group, !$group, $importer['nurl']];
+                               Contact::update(['forum' => $group, 'prv' => !$group], $condition);
                        } else {
-                               // It's not a forum, so remove the flags
+                               // It's not a group, so remove the flags
                                $condition = ['(`forum` OR `prv`) AND `id` = ?', $importer['id']];
                                Contact::update(['forum' => false, 'prv' => false], $condition);
 
@@ -2244,13 +2253,13 @@ class DFRN
                                $condition = ['(`forum` OR `prv`) AND `uid` = 0 AND `nurl` = ?', $importer['nurl']];
                                Contact::update(['forum' => false, 'prv' => false], $condition);
                        }
-               } elseif ($forum != $importer['forum']) { // Deprecated since 3.5.1
-                       $condition = ['`forum` != ? AND `id` = ?', $forum, $importer['id']];
-                       Contact::update(['forum' => $forum], $condition);
+               } elseif ($group != $importer['forum']) { // Deprecated since 3.5.1
+                       $condition = ['`forum` != ? AND `id` = ?', $group, $importer['id']];
+                       Contact::update(['forum' => $group], $condition);
 
                        // Updating the public contact as well
-                       $condition = ['`forum` != ? AND `uid` = 0 AND `nurl` = ?', $forum, $importer['nurl']];
-                       Contact::update(['forum' => $forum], $condition);
+                       $condition = ['`forum` != ? AND `uid` = 0 AND `nurl` = ?', $group, $importer['nurl']];
+                       Contact::update(['forum' => $group], $condition);
                }
 
 
@@ -2278,7 +2287,7 @@ class DFRN
                                self::processDeletion($xpath, $deletion, $importer);
                        }
                        if (count($deletions) > 0) {
-                               Logger::notice(count($deletions) . ' deletions had been processed');
+                               Logger::info(count($deletions) . ' deletions had been processed');
                                return 200;
                        }
                }