]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/OStatus.php
use "random_bytes" instead of "openssl_random_pseudo_bytes"
[friendica.git] / src / Protocol / OStatus.php
index a6e0df4c33e355644cc6e99344f77a39a13fa43e..9de0138ad6f64782dd19f5ca3e48f84a3d177d2f 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
  *
@@ -23,7 +23,6 @@ namespace Friendica\Protocol;
 
 use DOMDocument;
 use DOMXPath;
-use Friendica\Content\PageInfo;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Cache\Duration;
@@ -46,9 +45,6 @@ use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
 
-require_once 'mod/share.php';
-require_once 'include/api.php';
-
 /**
  * This class contain functions for the OStatus protocol
  */
@@ -315,7 +311,7 @@ class OStatus
         */
        public static function import($xml, array $importer, array &$contact, &$hub)
        {
-               self::process($xml, $importer, $contact, $hub);
+               self::process($xml, $importer, $contact, $hub, false, true, Conversation::PUSH);
        }
 
        /**
@@ -332,7 +328,7 @@ class OStatus
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function process($xml, array $importer, array &$contact = null, &$hub, $stored = false, $initialize = true)
+       private static function process($xml, array $importer, array &$contact = null, &$hub, $stored = false, $initialize = true, $direction = Conversation::UNKNOWN)
        {
                if ($initialize) {
                        self::$itemlist = [];
@@ -400,6 +396,7 @@ class OStatus
 
                        $header["protocol"] = Conversation::PARCEL_SALMON;
                        $header["source"] = $xml2;
+                       $header["direction"] = $direction;
                } elseif (!$initialize) {
                        return false;
                }
@@ -447,7 +444,7 @@ class OStatus
                        }
 
                        // Deletions come with the same uri, so we check for duplicates after processing deletions
-                       if (Item::exists(['uid' => $importer["uid"], 'uri' => $item["uri"]])) {
+                       if (Post::exists(['uid' => $importer["uid"], 'uri' => $item["uri"]])) {
                                Logger::log('Post with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.', Logger::DEBUG);
                                continue;
                        } else {
@@ -535,7 +532,7 @@ class OStatus
                                                }
                                        }
                                        foreach (self::$itemlist as $item) {
-                                               $found = Item::exists(['uid' => $importer["uid"], 'uri' => $item["uri"]]);
+                                               $found = Post::exists(['uid' => $importer["uid"], 'uri' => $item["uri"]]);
                                                if ($found) {
                                                        Logger::log("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already exists.", Logger::DEBUG);
                                                } elseif ($item['contact-id'] < 0) {
@@ -563,7 +560,7 @@ class OStatus
        private static function deleteNotice(array $item)
        {
                $condition = ['uid' => $item['uid'], 'author-id' => $item['author-id'], 'uri' => $item['uri']];
-               if (!Item::exists($condition)) {
+               if (!Post::exists($condition)) {
                        Logger::log('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it.");
                        return;
                }
@@ -675,11 +672,6 @@ class OStatus
 
                $item["body"] .= $add_body;
 
-               // Only add additional data when there is no picture in the post
-               if (!strstr($item["body"], '[/img]')) {
-                       $item["body"] = PageInfo::searchAndAppendToBody($item["body"]);
-               }
-
                Tag::storeFromBody($item['uri-id'], $item['body']);
 
                // Mastodon Content Warning
@@ -699,7 +691,7 @@ class OStatus
                }
 
                if (isset($item["thr-parent"])) {
-                       if (!Item::exists(['uid' => $importer["uid"], 'uri' => $item['thr-parent']])) {
+                       if (!Post::exists(['uid' => $importer["uid"], 'uri' => $item['thr-parent']])) {
                                if ($related != '') {
                                        self::fetchRelated($related, $item["thr-parent"], $importer);
                                }
@@ -737,7 +729,7 @@ class OStatus
 
                $curlResult = DI::httpRequest()->get($conversation, ['accept_content' => 'application/atom+xml, text/html']);
 
-               if (!$curlResult->isSuccess()) {
+               if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
                        return;
                }
 
@@ -810,6 +802,7 @@ class OStatus
                        $conv_data = [];
 
                        $conv_data['protocol'] = Conversation::PARCEL_SPLIT_CONVERSATION;
+                       $conv_data['direction'] = Conversation::PULL;
                        $conv_data['network'] = Protocol::OSTATUS;
                        $conv_data['uri'] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
 
@@ -850,12 +843,6 @@ class OStatus
 
                        $conv_data['source'] = $doc2->saveXML();
 
-                       $condition = ['item-uri' => $conv_data['uri'],'protocol' => Conversation::PARCEL_FEED];
-                       if (DBA::exists('conversation', $condition)) {
-                               Logger::log('Delete deprecated entry for URI '.$conv_data['uri'], Logger::DEBUG);
-                               DBA::delete('conversation', ['item-uri' => $conv_data['uri']]);
-                       }
-
                        Logger::log('Store conversation data for uri '.$conv_data['uri'], Logger::DEBUG);
                        Conversation::insert($conv_data);
                }
@@ -898,6 +885,7 @@ class OStatus
 
                $item["protocol"] = Conversation::PARCEL_SALMON;
                $item["source"] = $xml;
+               $item["direction"] = Conversation::PULL;
 
                Logger::log('Conversation '.$item['uri'].' is now fetched.', Logger::DEBUG);
        }
@@ -921,7 +909,7 @@ class OStatus
                if (DBA::isResult($conversation)) {
                        $stored = true;
                        $xml = $conversation['source'];
-                       if (self::process($xml, $importer, $contact, $hub, $stored, false)) {
+                       if (self::process($xml, $importer, $contact, $hub, $stored, false, Conversation::PULL)) {
                                Logger::log('Got valid cached XML for URI '.$related_uri, Logger::DEBUG);
                                return;
                        }
@@ -934,7 +922,7 @@ class OStatus
                $stored = false;
                $curlResult = DI::httpRequest()->get($related, ['accept_content' => 'application/atom+xml, text/html']);
 
-               if (!$curlResult->isSuccess()) {
+               if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
                        return;
                }
 
@@ -1006,7 +994,7 @@ class OStatus
                }
 
                if ($xml != '') {
-                       self::process($xml, $importer, $contact, $hub, $stored, false);
+                       self::process($xml, $importer, $contact, $hub, $stored, false, Conversation::PULL);
                } else {
                        Logger::log("XML couldn't be fetched for URI: ".$related_uri." - href: ".$related, Logger::DEBUG);
                }
@@ -1104,7 +1092,9 @@ class OStatus
                                                if (($item["object-type"] == Activity\ObjectType::QUESTION)
                                                        || ($item["object-type"] == Activity\ObjectType::EVENT)
                                                ) {
-                                                       $item["body"] .= "\n" . PageInfo::getFooterFromUrl($attribute['href']);
+                                                       Post\Media::insert(['uri-id' => $item['uri-id'], 'type' => Post\Media::UNKNOWN,
+                                                               'url' => $attribute['href'], 'mimetype' => $attribute['type'] ?? null,
+                                                               'size' => $attribute['length'] ?? null, 'description' => $attribute['title'] ?? null]);
                                                }
                                                break;
                                        case "ostatus:conversation":
@@ -1131,7 +1121,9 @@ class OStatus
                                                        }
                                                        $link_data['related'] = $attribute['href'];
                                                } else {
-                                                       $item["body"] .= "\n" . PageInfo::getFooterFromUrl($attribute['href']);
+                                                       Post\Media::insert(['uri-id' => $item['uri-id'], 'type' => Post\Media::UNKNOWN,
+                                                               'url' => $attribute['href'], 'mimetype' => $attribute['type'] ?? null,
+                                                               'size' => $attribute['length'] ?? null, 'description' => $attribute['title'] ?? null]);
                                                }
                                                break;
                                        case "self":
@@ -1599,7 +1591,7 @@ class OStatus
 
                $condition = ['uid' => $owner["uid"], 'guid' => $repeated_guid, 'private' => [Item::PUBLIC, Item::UNLISTED],
                        'network' => [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]];
-               $repeated_item = Item::selectFirst([], $condition);
+               $repeated_item = Post::selectFirst([], $condition);
                if (!DBA::isResult($repeated_item)) {
                        return false;
                }
@@ -1665,7 +1657,7 @@ class OStatus
                $verb = ActivityNamespace::ACTIVITY_SCHEMA . "favorite";
                self::entryContent($doc, $entry, $item, $owner, "Favorite", $verb, false);
 
-               $parent = Item::selectFirst([], ['uri' => $item["thr-parent"], 'uid' => $item["uid"]]);
+               $parent = Post::selectFirst([], ['uri' => $item["thr-parent"], 'uid' => $item["uid"]]);
                if (DBA::isResult($parent)) {
                        $as_object = $doc->createElement("activity:object");
 
@@ -1893,7 +1885,8 @@ class OStatus
                XML::addElement($doc, $entry, "id", $item["uri"]);
                XML::addElement($doc, $entry, "title", html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
 
-               $body = self::formatPicturePost($item['body']);
+               $body = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
+               $body = self::formatPicturePost($body);
 
                if (!empty($item['title'])) {
                        $body = "[b]".$item['title']."[/b]\n\n".$body;
@@ -1933,9 +1926,9 @@ class OStatus
                $mentioned = [];
 
                if ($item['gravity'] != GRAVITY_PARENT) {
-                       $parent = Item::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item['parent']]);
+                       $parent = Post::selectFirst(['guid', 'author-link', 'owner-link'], ['id' => $item['parent']]);
 
-                       $thrparent = Item::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner["uid"], 'uri' => $item['thr-parent']]);
+                       $thrparent = Post::selectFirst(['guid', 'author-link', 'owner-link', 'plink'], ['uid' => $owner["uid"], 'uri' => $item['thr-parent']]);
 
                        if (DBA::isResult($thrparent)) {
                                $mentioned[$thrparent["author-link"]] = $thrparent["author-link"];
@@ -2130,12 +2123,12 @@ class OStatus
                $params = ['order' => ['received' => true], 'limit' => $max_items];
 
                if ($filter === 'posts') {
-                       $ret = Item::selectThread([], $condition, $params);
+                       $ret = Post::selectThread([], $condition, $params);
                } else {
-                       $ret = Item::select([], $condition, $params);
+                       $ret = Post::select([], $condition, $params);
                }
 
-               $items = Item::inArray($ret);
+               $items = Post::toArray($ret);
 
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;