]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/DFRN.php
Merge pull request #10472 from annando/proxy
[friendica.git] / src / Protocol / DFRN.php
index 23a287a9a1b2c3fc8e077c1f8932f87aec8acc39..4d2744498787125673215aafccc542f3ca58f7ab 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -34,11 +34,11 @@ 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;
 use Friendica\Model\Mail;
-use Friendica\Model\Notify;
-use Friendica\Model\Notify\Type;
+use Friendica\Model\Notification;
 use Friendica\Model\PermissionSet;
 use Friendica\Model\Post;
 use Friendica\Model\Post\Category;
@@ -51,6 +51,7 @@ use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Images;
 use Friendica\Util\Network;
+use Friendica\Util\Proxy;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
 
@@ -311,23 +312,26 @@ class DFRN
        }
 
        /**
-        * Generate an atom entry for a given item id
+        * Generate an atom entry for a given uri id and user
         *
-        * @param int     $item_id      The item id
+        * @param int     $uri_id       The uri id
+        * @param int     $uid          The user id
         * @param boolean $conversation Show the conversation. If false show the single post.
         *
         * @return string DFRN feed entry
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function itemFeed($item_id, $conversation = false)
+       public static function itemFeed(int $uri_id, int $uid, bool $conversation = false)
        {
                if ($conversation) {
-                       $condition = ['parent' => $item_id];
+                       $condition = ['parent-uri-id' => $uri_id];
                } else {
-                       $condition = ['id' => $item_id];
+                       $condition = ['uri-id' => $uri_id];
                }
 
+               $condition['uid'] = $uid;
+
                $items = Post::selectToArray(Item::DELIVER_FIELDLIST, $condition);
                if (!DBA::isResult($items)) {
                        return '';
@@ -615,7 +619,8 @@ class DFRN
                XML::addElement($doc, $author, "dfrn:handle", $owner["addr"], $attributes);
 
                $attributes = ["rel" => "photo", "type" => "image/jpeg",
-                                       "media:width" => 300, "media:height" => 300, "href" => $owner['photo']];
+                                       "media:width" => Proxy::PIXEL_SMALL, "media:height" => Proxy::PIXEL_SMALL,
+                                       "href" => Contact::getAvatarUrlForId($owner['id'], Proxy::SIZE_SMALL, $owner['updated'])];
 
                if (!$public || !$hide) {
                        $attributes["dfrn:updated"] = $picdate;
@@ -896,10 +901,10 @@ class DFRN
                        $entry->setAttribute("xmlns:statusnet", ActivityNamespace::STATUSNET);
                }
 
+               $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body'] ?? '');
+
                if ($item['private'] == Item::PRIVATE) {
-                       $body = Item::fixPrivatePhotos($item['body'], $owner['uid'], $item, $cid);
-               } else {
-                       $body = $item['body'];
+                       $body = Item::fixPrivatePhotos($body, $owner['uid'], $item, $cid);
                }
 
                // Remove the abstract element. It is only locally important.
@@ -913,7 +918,7 @@ class DFRN
                                $htmlbody = "[b]" . $item['title'] . "[/b]\n\n" . $htmlbody;
                        }
 
-                       $htmlbody = BBCode::convert($htmlbody, false, BBCode::OSTATUS);
+                       $htmlbody = BBCode::convertForUriId($item['uri-id'], $htmlbody, BBCode::OSTATUS);
                }
 
                $author = self::addEntryAuthor($doc, "author", $item["author-link"], $item);
@@ -1269,7 +1274,7 @@ class DFRN
 
                        switch ($rino_remote_version) {
                                case 1:
-                                       $key = openssl_random_pseudo_bytes(16);
+                                       $key = random_bytes(16);
                                        $data = self::aesEncrypt($postvars['data'], $key);
                                        break;
 
@@ -1438,19 +1443,19 @@ class DFRN
        /**
         * Fetch the author data from head or entry items
         *
-        * @param object $xpath     XPath object
-        * @param object $context   In which context should the data be searched
-        * @param array  $importer  Record of the importer user mixed with contact of the content
-        * @param string $element   Element name from which the data is fetched
-        * @param bool   $onlyfetch Should the data only be fetched or should it update the contact record as well
-        * @param string $xml       optional, default empty
+        * @param \DOMXPath $xpath     XPath object
+        * @param \DOMNode  $context   In which context should the data be searched
+        * @param array     $importer  Record of the importer user mixed with contact of the content
+        * @param string    $element   Element name from which the data is fetched
+        * @param bool      $onlyfetch Should the data only be fetched or should it update the contact record as well
+        * @param string    $xml       optional, default empty
         *
         * @return array Relevant data of the author
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         * @todo  Find good type-hints for all parameter
         */
-       private static function fetchauthor($xpath, $context, $importer, $element, $onlyfetch, $xml = "")
+       private static function fetchauthor(\DOMXPath $xpath, \DOMNode $context, $importer, $element, $onlyfetch, $xml = "")
        {
                $author = [];
                $author["name"] = XML::getFirstNodeValue($xpath, $element."/atom:name/text()", $context);
@@ -1606,12 +1611,14 @@ class DFRN
                        }
 
                        // "dfrn:birthday" contains the birthday converted to UTC
-                       $birthday = XML::getFirstNodeValue($xpath, $element . "/poco:birthday/text()", $context);
-
-                       if (strtotime($birthday) > time()) {
-                               $bd_timestamp = strtotime($birthday);
-
-                               $poco["bdyear"] = date("Y", $bd_timestamp);
+                       $birthday = XML::getFirstNodeValue($xpath, $element . "/dfrn:birthday/text()", $context);
+                       try {
+                               $birthday_date = new \DateTime($birthday);
+                               if ($birthday_date > new \DateTime()) {
+                                       $poco["bdyear"] = $birthday_date->format("Y");
+                               }
+                       } catch (\Exception $e) {
+                               // Invalid birthday
                        }
 
                        // "poco:birthday" is the birthday in the format "yyyy-mm-dd"
@@ -1963,8 +1970,8 @@ class DFRN
                                // send a notification
                                notification(
                                        [
-                                       "type"     => Type::POKE,
-                                       "otype"    => Notify\ObjectType::PERSON,
+                                       "type"     => Notification\Type::POKE,
+                                       "otype"    => Notification\ObjectType::PERSON,
                                        "activity" => $verb,
                                        "verb"     => $item["verb"],
                                        "uid"      => $importer["importer_uid"],
@@ -2192,6 +2199,12 @@ class DFRN
 
                $item["title"] = XML::getFirstNodeValue($xpath, "atom:title/text()", $entry);
 
+               if (!empty($item["title"])) {
+                       $item["post-type"] = Item::PT_ARTICLE;
+               } else {
+                       $item["post-type"] = Item::PT_NOTE;
+               }
+
                $item["created"] = XML::getFirstNodeValue($xpath, "atom:published/text()", $entry);
 
                $item["body"] = XML::getFirstNodeValue($xpath, "dfrn:env/text()", $entry);
@@ -2236,6 +2249,8 @@ class DFRN
 
                $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
 
+               $item["body"] = Item::improveSharedDataInBody($item);
+
                Tag::storeFromBody($item['uri-id'], $item["body"]);
 
                // We store the data from "dfrn:diaspora_signature" in a different table, this is done in "Item::insert"
@@ -2331,7 +2346,7 @@ class DFRN
                                return 403;
                        }
                }
-                               
+
                // Get the type of the item (Top level post, reply or remote reply)
                $entrytype = self::getEntryType($importer, $item);
 
@@ -2423,8 +2438,8 @@ class DFRN
 
                if (in_array($entrytype, [DFRN::REPLY, DFRN::REPLY_RC])) {
                        // Will be overwritten for sharing accounts in Item::insert
-                       if (empty($item['post-type']) && ($entrytype == DFRN::REPLY)) {
-                               $item['post-type'] = Item::PT_COMMENT;
+                       if (empty($item['post-reason']) && ($entrytype == DFRN::REPLY)) {
+                               $item['post-reason'] = Item::PR_COMMENT;
                        }
 
                        $posted_id = Item::insert($item);
@@ -2508,14 +2523,14 @@ class DFRN
                }
 
                $condition = ['uri' => $uri, 'uid' => $importer["importer_uid"]];
-               $item = Post::selectFirst(['id', 'parent', 'contact-id', 'file', 'deleted', 'gravity'], $condition);
+               $item = Post::selectFirst(['id', 'parent', 'contact-id', 'uri-id', 'deleted', 'gravity'], $condition);
                if (!DBA::isResult($item)) {
                        Logger::log("Item with uri " . $uri . " for user " . $importer["importer_uid"] . " wasn't found.", Logger::DEBUG);
                        return;
                }
 
-               if (strstr($item['file'], '[')) {
-                       Logger::log("Item with uri " . $uri . " for user " . $importer["importer_uid"] . " is filed. So it won't be deleted.", Logger::DEBUG);
+               if (DBA::exists('post-category', ['uri-id' => $item['uri-id'], 'uid' => $importer['importer_uid'], 'type' => Post\Category::FILE])) {
+                       Logger::notice("Item is filed. It won't be deleted.", ['uri' => $uri, 'uri-id' => $item['uri_id'], 'uid' => $importer["importer_uid"]]);
                        return;
                }
 
@@ -2585,7 +2600,7 @@ class DFRN
                $header["direction"] = $direction;
 
                if ($direction === Conversation::RELAY) {
-                       $header['post-type'] = Item::PT_RELAY;
+                       $header['post-reason'] = Item::PR_RELAY;
                }
 
                // Update the contact table if the data has changed
@@ -2602,6 +2617,14 @@ class DFRN
 
                Logger::log("Import DFRN message for user " . $importer["importer_uid"] . " from contact " . $importer["id"], Logger::DEBUG);
 
+               if (!empty($importer['gsid'])) {
+                       if ($protocol == Conversation::PARCEL_DIASPORA_DFRN) {
+                               GServer::setProtocol($importer['gsid'], Post\DeliveryData::DFRN);
+                       } elseif ($protocol == Conversation::PARCEL_LEGACY_DFRN) {
+                               GServer::setProtocol($importer['gsid'], Post\DeliveryData::LEGACY_DFRN);
+                       }
+               }
+
                // is it a public forum? Private forums aren't exposed with this method
                $forum = intval(XML::getFirstNodeValue($xpath, "/atom:feed/dfrn:community/text()"));