]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/OStatus.php
"FetchPoCo" added
[friendica.git] / src / Protocol / OStatus.php
index 1e287b2ce441fd8f18205db7d05b7bee9a22892e..a63b12731dd3556cb56dcf3650b3cd76d7aca244 100644 (file)
@@ -10,23 +10,24 @@ use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
-use Friendica\Core\PConfig;
 use Friendica\Core\L10n;
-use Friendica\Core\Logger;
 use Friendica\Core\Lock;
+use Friendica\Core\Logger;
+use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
+use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\GContact;
-use Friendica\Model\APContact;
 use Friendica\Model\Item;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Object\Image;
-use Friendica\Protocol\Activity\Namespaces;
+use Friendica\Protocol\ActivityNamespace;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Images;
 use Friendica\Util\Network;
 use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
@@ -262,14 +263,14 @@ class OStatus
                @$doc->loadXML($xml);
 
                $xpath = new DOMXPath($doc);
-               $xpath->registerNamespace('atom', Namespaces::ATOM1);
-               $xpath->registerNamespace('thr', Namespaces::THREAD);
-               $xpath->registerNamespace('georss', Namespaces::GEORSS);
-               $xpath->registerNamespace('activity', Namespaces::ACTIVITY);
-               $xpath->registerNamespace('media', Namespaces::MEDIA);
-               $xpath->registerNamespace('poco', Namespaces::POCO);
-               $xpath->registerNamespace('ostatus', Namespaces::OSTATUS);
-               $xpath->registerNamespace('statusnet', Namespaces::STATUSNET);
+               $xpath->registerNamespace('atom', ActivityNamespace::ATOM1);
+               $xpath->registerNamespace('thr', ActivityNamespace::THREAD);
+               $xpath->registerNamespace('georss', ActivityNamespace::GEORSS);
+               $xpath->registerNamespace('activity', ActivityNamespace::ACTIVITY);
+               $xpath->registerNamespace('media', ActivityNamespace::MEDIA);
+               $xpath->registerNamespace('poco', ActivityNamespace::POCO);
+               $xpath->registerNamespace('ostatus', ActivityNamespace::OSTATUS);
+               $xpath->registerNamespace('statusnet', ActivityNamespace::STATUSNET);
 
                $contact = ["id" => 0];
 
@@ -343,14 +344,14 @@ class OStatus
                @$doc->loadXML($xml);
 
                $xpath = new DOMXPath($doc);
-               $xpath->registerNamespace('atom', Namespaces::ATOM1);
-               $xpath->registerNamespace('thr', Namespaces::THREAD);
-               $xpath->registerNamespace('georss', Namespaces::GEORSS);
-               $xpath->registerNamespace('activity', Namespaces::ACTIVITY);
-               $xpath->registerNamespace('media', Namespaces::MEDIA);
-               $xpath->registerNamespace('poco', Namespaces::POCO);
-               $xpath->registerNamespace('ostatus', Namespaces::OSTATUS);
-               $xpath->registerNamespace('statusnet', Namespaces::STATUSNET);
+               $xpath->registerNamespace('atom', ActivityNamespace::ATOM1);
+               $xpath->registerNamespace('thr', ActivityNamespace::THREAD);
+               $xpath->registerNamespace('georss', ActivityNamespace::GEORSS);
+               $xpath->registerNamespace('activity', ActivityNamespace::ACTIVITY);
+               $xpath->registerNamespace('media', ActivityNamespace::MEDIA);
+               $xpath->registerNamespace('poco', ActivityNamespace::POCO);
+               $xpath->registerNamespace('ostatus', ActivityNamespace::OSTATUS);
+               $xpath->registerNamespace('statusnet', ActivityNamespace::STATUSNET);
 
                $hub = "";
                $hub_items = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0);
@@ -434,7 +435,7 @@ class OStatus
                                continue;
                        }
 
-                       if (in_array($item["verb"], [Namespaces::OSTATUS . "/unfavorite", Activity::UNFAVORITE])) {
+                       if (in_array($item["verb"], [Activity::O_UNFAVOURITE, Activity::UNFAVORITE])) {
                                // Ignore "Unfavorite" message
                                Logger::log("Ignore unfavorite message ".print_r($item, true), Logger::DEBUG);
                                continue;
@@ -465,7 +466,7 @@ class OStatus
                                continue;
                        }
 
-                       if ($item["verb"] == Namespaces::OSTATUS."/unfollow") {
+                       if ($item["verb"] == Activity::O_UNFOLLOW) {
                                $dummy = null;
                                Contact::removeFollower($importer, $contact, $item, $dummy);
                                continue;
@@ -478,7 +479,7 @@ class OStatus
                                $item["verb"] = Activity::LIKE;
                                $item["parent-uri"] = $orig_uri;
                                $item["gravity"] = GRAVITY_ACTIVITY;
-                               $item["object-type"] = Activity::OBJ_NOTE;
+                               $item["object-type"] = Activity\ObjectType::NOTE;
                        }
 
                        // http://activitystrea.ms/schema/1.0/rsvp-yes
@@ -593,10 +594,10 @@ class OStatus
        {
                $item["body"] = HTML::toBBCode(XML::getFirstNodeValue($xpath, 'atom:content/text()', $entry));
                $item["object-type"] = XML::getFirstNodeValue($xpath, 'activity:object-type/text()', $entry);
-               if (($item["object-type"] == Activity::OBJ_BOOKMARK) || ($item["object-type"] == Activity::OBJ_EVENT)) {
+               if (($item["object-type"] == Activity\ObjectType::BOOKMARK) || ($item["object-type"] == Activity\ObjectType::EVENT)) {
                        $item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
                        $item["body"] = XML::getFirstNodeValue($xpath, 'atom:summary/text()', $entry);
-               } elseif ($item["object-type"] == Activity::OBJ_QUESTION) {
+               } elseif ($item["object-type"] == Activity\ObjectType::QUESTION) {
                        $item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
                }
 
@@ -804,9 +805,9 @@ class OStatus
                @$doc->loadXML($xml);
 
                $xpath = new DOMXPath($doc);
-               $xpath->registerNamespace('atom', Namespaces::ATOM1);
-               $xpath->registerNamespace('thr', Namespaces::THREAD);
-               $xpath->registerNamespace('ostatus', Namespaces::OSTATUS);
+               $xpath->registerNamespace('atom', ActivityNamespace::ATOM1);
+               $xpath->registerNamespace('thr', ActivityNamespace::THREAD);
+               $xpath->registerNamespace('ostatus', ActivityNamespace::OSTATUS);
 
                $entries = $xpath->query('/atom:feed/atom:entry');
 
@@ -1106,8 +1107,8 @@ class OStatus
                                switch ($attribute['rel']) {
                                        case "alternate":
                                                $item["plink"] = $attribute['href'];
-                                               if (($item["object-type"] == Activity::OBJ_QUESTION)
-                                                       || ($item["object-type"] == Activity::OBJ_EVENT)
+                                               if (($item["object-type"] == Activity\ObjectType::QUESTION)
+                                                       || ($item["object-type"] == Activity\ObjectType::EVENT)
                                                ) {
                                                        $item["body"] .= add_page_info($attribute['href']);
                                                }
@@ -1136,7 +1137,7 @@ class OStatus
                                                }
                                                break;
                                        case "related":
-                                               if ($item["object-type"] != Activity::OBJ_BOOKMARK) {
+                                               if ($item["object-type"] != Activity\ObjectType::BOOKMARK) {
                                                        if (!isset($item["parent-uri"])) {
                                                                $item["parent-uri"] = $attribute['href'];
                                                        }
@@ -1198,37 +1199,12 @@ class OStatus
         */
        private static function getResharedGuid(array $item)
        {
-               $body = trim($item["body"]);
-
-               // Skip if it isn't a pure repeated messages
-               // Does it start with a share?
-               if (strpos($body, "[share") > 0) {
-                       return "";
-               }
-
-               // Does it end with a share?
-               if (strlen($body) > (strrpos($body, "[/share]") + 8)) {
-                       return "";
-               }
-
-               $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
-               // Skip if there is no shared message in there
-               if ($body == $attributes) {
-                       return false;
-               }
-
-               $guid = "";
-               preg_match("/guid='(.*?)'/ism", $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $guid = $matches[1];
-               }
-
-               preg_match('/guid="(.*?)"/ism', $attributes, $matches);
-               if (!empty($matches[1])) {
-                       $guid = $matches[1];
+               $reshared = Item::getShareArray($item);
+               if (empty($reshared['guid']) || !empty($reshared['comment'])) {
+                       return '';
                }
 
-               return $guid;
+               return $reshared['guid'];
        }
 
        /**
@@ -1282,17 +1258,17 @@ class OStatus
         */
        private static function addHeader(DOMDocument $doc, array $owner, $filter, $feed_mode = false)
        {
-               $root = $doc->createElementNS(Namespaces::ATOM1, 'feed');
+               $root = $doc->createElementNS(ActivityNamespace::ATOM1, 'feed');
                $doc->appendChild($root);
 
-               $root->setAttribute("xmlns:thr", Namespaces::THREAD);
-               $root->setAttribute("xmlns:georss", Namespaces::GEORSS);
-               $root->setAttribute("xmlns:activity", Namespaces::ACTIVITY);
-               $root->setAttribute("xmlns:media", Namespaces::MEDIA);
-               $root->setAttribute("xmlns:poco", Namespaces::POCO);
-               $root->setAttribute("xmlns:ostatus", Namespaces::OSTATUS);
-               $root->setAttribute("xmlns:statusnet", Namespaces::STATUSNET);
-               $root->setAttribute("xmlns:mastodon", Namespaces::MASTODON);
+               $root->setAttribute("xmlns:thr", ActivityNamespace::THREAD);
+               $root->setAttribute("xmlns:georss", ActivityNamespace::GEORSS);
+               $root->setAttribute("xmlns:activity", ActivityNamespace::ACTIVITY);
+               $root->setAttribute("xmlns:media", ActivityNamespace::MEDIA);
+               $root->setAttribute("xmlns:poco", ActivityNamespace::POCO);
+               $root->setAttribute("xmlns:ostatus", ActivityNamespace::OSTATUS);
+               $root->setAttribute("xmlns:statusnet", ActivityNamespace::STATUSNET);
+               $root->setAttribute("xmlns:mastodon", ActivityNamespace::MASTODON);
 
                $title = '';
                $selfUri = '/feed/' . $owner["nick"] . '/';
@@ -1389,7 +1365,7 @@ class OStatus
                switch ($siteinfo["type"]) {
                        case 'photo':
                                if (!empty($siteinfo["image"])) {
-                                       $imgdata = Image::getInfoFromURL($siteinfo["image"]);
+                                       $imgdata = Images::getInfoFromURLCached($siteinfo["image"]);
                                        if ($imgdata) {
                                                $attributes = ["rel" => "enclosure",
                                                                "href" => $siteinfo["image"],
@@ -1413,7 +1389,7 @@ class OStatus
                }
 
                if (!Config::get('system', 'ostatus_not_attach_preview') && ($siteinfo["type"] != "photo") && isset($siteinfo["image"])) {
-                       $imgdata = Image::getInfoFromURL($siteinfo["image"]);
+                       $imgdata = Images::getInfoFromURLCached($siteinfo["image"]);
                        if ($imgdata) {
                                $attributes = ["rel" => "enclosure",
                                                "href" => $siteinfo["image"],
@@ -1462,9 +1438,9 @@ class OStatus
                $author = $doc->createElement("author");
                XML::addElement($doc, $author, "id", $owner["url"]);
                if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
-                       XML::addElement($doc, $author, "activity:object-type", Activity::OBJ_GROUP);
+                       XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::GROUP);
                } else {
-                       XML::addElement($doc, $author, "activity:object-type", Activity::OBJ_PERSON);
+                       XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::PERSON);
                }
                XML::addElement($doc, $author, "uri", $owner["url"]);
                XML::addElement($doc, $author, "name", $owner["nick"]);
@@ -1557,11 +1533,11 @@ class OStatus
         */
        private static function constructObjecttype(array $item)
        {
-               if (!empty($item['object-type']) && in_array($item['object-type'], [Activity::OBJ_NOTE, Activity::OBJ_COMMENT])) {
+               if (!empty($item['object-type']) && in_array($item['object-type'], [Activity\ObjectType::NOTE, Activity\ObjectType::COMMENT])) {
                        return $item['object-type'];
                }
 
-               return Activity::OBJ_NOTE;
+               return Activity\ObjectType::NOTE;
        }
 
        /**
@@ -1592,7 +1568,7 @@ class OStatus
 
                if ($item["verb"] == Activity::LIKE) {
                        return self::likeEntry($doc, $item, $owner, $toplevel);
-               } elseif (in_array($item["verb"], [Activity::FOLLOW, Namespaces::OSTATUS . "/unfollow"])) {
+               } elseif (in_array($item["verb"], [Activity::FOLLOW, Activity::O_UNFOLLOW])) {
                        return self::followEntry($doc, $item, $owner, $toplevel);
                } else {
                        return self::noteEntry($doc, $item, $owner, $toplevel, $feed_mode);
@@ -1710,7 +1686,7 @@ class OStatus
 
                $as_object = $doc->createElement("activity:object");
 
-               XML::addElement($doc, $as_object, "activity:object-type", Namespaces::ACTIVITY_SCHEMA . "activity");
+               XML::addElement($doc, $as_object, "activity:object-type", ActivityNamespace::ACTIVITY_SCHEMA . "activity");
 
                self::entryContent($doc, $as_object, $repeated_item, $owner, "", "", false);
 
@@ -1760,7 +1736,7 @@ class OStatus
 
                $entry = self::entryHeader($doc, $owner, $item, $toplevel);
 
-               $verb = Activity\Namespaces::ACTIVITY_SCHEMA."favorite";
+               $verb = ActivityNamespace::ACTIVITY_SCHEMA . "favorite";
                self::entryContent($doc, $entry, $item, $owner, "Favorite", $verb, false);
 
                $parent = Item::selectFirst([], ['uri' => $item["thr-parent"], 'uid' => $item["uid"]]);
@@ -1791,7 +1767,7 @@ class OStatus
        private static function addPersonObject(DOMDocument $doc, array $owner, array $contact)
        {
                $object = $doc->createElement("activity:object");
-               XML::addElement($doc, $object, "activity:object-type", Activity::OBJ_PERSON);
+               XML::addElement($doc, $object, "activity:object-type", Activity\ObjectType::PERSON);
 
                if ($contact['network'] == Protocol::PHANTOM) {
                        XML::addElement($doc, $object, "id", $contact['url']);
@@ -1919,7 +1895,7 @@ class OStatus
 
                $entry = self::entryHeader($doc, $owner, $item, $toplevel);
 
-               XML::addElement($doc, $entry, "activity:object-type", Activity::OBJ_NOTE);
+               XML::addElement($doc, $entry, "activity:object-type", Activity\ObjectType::NOTE);
 
                self::entryContent($doc, $entry, $item, $owner, $title, '', true, $feed_mode);
 
@@ -1951,16 +1927,16 @@ class OStatus
                                $entry->appendChild($author);
                        }
                } else {
-                       $entry = $doc->createElementNS(Namespaces::ATOM1, "entry");
+                       $entry = $doc->createElementNS(ActivityNamespace::ATOM1, "entry");
 
-                       $entry->setAttribute("xmlns:thr", Namespaces::THREAD);
-                       $entry->setAttribute("xmlns:georss", Namespaces::GEORSS);
-                       $entry->setAttribute("xmlns:activity", Namespaces::ACTIVITY);
-                       $entry->setAttribute("xmlns:media", Namespaces::MEDIA);
-                       $entry->setAttribute("xmlns:poco", Namespaces::POCO);
-                       $entry->setAttribute("xmlns:ostatus", Namespaces::OSTATUS);
-                       $entry->setAttribute("xmlns:statusnet", Namespaces::STATUSNET);
-                       $entry->setAttribute("xmlns:mastodon", Namespaces::MASTODON);
+                       $entry->setAttribute("xmlns:thr", ActivityNamespace::THREAD);
+                       $entry->setAttribute("xmlns:georss", ActivityNamespace::GEORSS);
+                       $entry->setAttribute("xmlns:activity", ActivityNamespace::ACTIVITY);
+                       $entry->setAttribute("xmlns:media", ActivityNamespace::MEDIA);
+                       $entry->setAttribute("xmlns:poco", ActivityNamespace::POCO);
+                       $entry->setAttribute("xmlns:ostatus", ActivityNamespace::OSTATUS);
+                       $entry->setAttribute("xmlns:statusnet", ActivityNamespace::STATUSNET);
+                       $entry->setAttribute("xmlns:mastodon", ActivityNamespace::MASTODON);
 
                        $author = self::addAuthor($doc, $owner);
                        $entry->appendChild($author);
@@ -2112,14 +2088,14 @@ class OStatus
                                XML::addElement($doc, $entry, "link", "",
                                        [
                                                "rel" => "mentioned",
-                                               "ostatus:object-type" => Activity::OBJ_GROUP,
+                                               "ostatus:object-type" => Activity\ObjectType::GROUP,
                                                "href" => $mention]
                                );
                        } else {
                                XML::addElement($doc, $entry, "link", "",
                                        [
                                                "rel" => "mentioned",
-                                               "ostatus:object-type" => Activity::OBJ_PERSON,
+                                               "ostatus:object-type" => Activity\ObjectType::PERSON,
                                                "href" => $mention]
                                );
                        }
@@ -2232,7 +2208,7 @@ class OStatus
 
                if ($filter === 'comments') {
                        $condition[0] .= " AND `object-type` = ? ";
-                       $condition[] = Activity::OBJ_COMMENT;
+                       $condition[] = Activity\ObjectType::COMMENT;
                }
 
                if ($owner['account-type'] != User::ACCOUNT_TYPE_COMMUNITY) {