]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Make the automatic title appending optional
[friendica.git] / src / Protocol / Diaspora.php
index 9e074e8a9a1c3a0b820938151fe7d18ac9ce6dae..6a1520521585913e7bc2371f9b8c2f5c02a88357 100644 (file)
@@ -32,6 +32,7 @@ use Friendica\Model\Mail;
 use Friendica\Model\Profile;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
+use Friendica\Protocol\ActivityNamespace;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Map;
@@ -175,7 +176,7 @@ class Diaspora
         * @return array with the contact
         * @throws \Exception
         */
-       private static function getRelayContact(string $server_url, array $fields = ['batch', 'id', 'name', 'network', 'protocol', 'archive', 'blocked'])
+       private static function getRelayContact(string $server_url, array $fields = ['batch', 'id', 'url', 'name', 'network', 'protocol', 'archive', 'blocked'])
        {
                // Fetch the relay contact
                $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
@@ -249,7 +250,7 @@ class Diaspora
         */
        public static function participantsForThread($thread, array $contacts)
        {
-               $r = DBA::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, `contact`.`protocol`,
+               $r = DBA::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`url`, `contact`.`name`, `contact`.`network`, `contact`.`protocol`,
                                `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation`
                                INNER JOIN `contact` ON `contact`.`id` = `participation`.`cid`
                                INNER JOIN `fcontact` ON `fcontact`.`id` = `participation`.`fid`
@@ -413,8 +414,8 @@ class Diaspora
        /**
         * @brief: Decodes incoming Diaspora message in the new format
         *
-        * @param array   $importer Array of the importer user
         * @param string  $raw      raw post message
+        * @param string  $privKey   The private key of the importer
         * @param boolean $no_exit  Don't do an http exit on error
         *
         * @return array
@@ -424,7 +425,7 @@ class Diaspora
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function decodeRaw(array $importer, $raw, $no_exit = false)
+       public static function decodeRaw(string $raw, string $privKey = '', bool $no_exit = false)
        {
                $data = json_decode($raw);
 
@@ -434,7 +435,7 @@ class Diaspora
                        $ciphertext = base64_decode($data->encrypted_magic_envelope);
 
                        $outer_key_bundle = '';
-                       @openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $importer['prvkey']);
+                       @openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
                        $j_outer_key_bundle = json_decode($outer_key_bundle);
 
                        if (!is_object($j_outer_key_bundle)) {
@@ -465,7 +466,7 @@ class Diaspora
                        }
                }
 
-               $base = $basedom->children(NAMESPACE_SALMON_ME);
+               $base = $basedom->children(ActivityNamespace::SALMON_ME);
 
                // Not sure if this cleaning is needed
                $data = str_replace([" ", "\t", "\r", "\n"], ["", "", "", ""], $base->data);
@@ -519,8 +520,8 @@ class Diaspora
        /**
         * @brief: Decodes incoming Diaspora message in the deprecated format
         *
-        * @param array  $importer Array of the importer user
         * @param string $xml      urldecoded Diaspora salmon
+        * @param string $privKey  The private key of the importer
         *
         * @return array
         * 'message' -> decoded Diaspora XML message
@@ -529,7 +530,7 @@ class Diaspora
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function decode(array $importer, $xml)
+       public static function decode(string $xml, string $privKey = '')
        {
                $public = false;
                $basedom = XML::parseString($xml);
@@ -548,7 +549,7 @@ class Diaspora
                        $author_link = str_replace('acct:', '', $children->header->author_id);
                } else {
                        // This happens with posts from a relais
-                       if (!$importer) {
+                       if (empty($privKey)) {
                                Logger::log("This is no private post in the old format", Logger::DEBUG);
                                return false;
                        }
@@ -559,7 +560,7 @@ class Diaspora
                        $ciphertext = base64_decode($encrypted_header->ciphertext);
 
                        $outer_key_bundle = '';
-                       openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $importer['prvkey']);
+                       openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
 
                        $j_outer_key_bundle = json_decode($outer_key_bundle);
 
@@ -577,7 +578,7 @@ class Diaspora
                        $author_link = str_replace('acct:', '', $idom->author_id);
                }
 
-               $dom = $basedom->children(NAMESPACE_SALMON_ME);
+               $dom = $basedom->children(ActivityNamespace::SALMON_ME);
 
                // figure out where in the DOM tree our data is hiding
 
@@ -1608,7 +1609,7 @@ class Diaspora
                }
 
                if (self::isRedmatrix($contact["url"])) {
-                       return $contact["url"] . "/?f=&mid=" . $guid;
+                       return $contact["url"] . "/?mid=" . $guid;
                }
 
                if ($parent_guid != '') {
@@ -1845,7 +1846,7 @@ class Diaspora
                $datarray["guid"] = $guid;
                $datarray["uri"] = self::getUriFromGuid($author, $guid);
 
-               $datarray["verb"] = ACTIVITY_POST;
+               $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_COMMENT;
 
                if ($thr_uri != "") {
@@ -1854,7 +1855,7 @@ class Diaspora
                        $datarray["parent-uri"] = $parent_item["uri"];
                }
 
-               $datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
+               $datarray["object-type"] = Activity\ObjectType::COMMENT;
 
                $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
                $datarray["source"] = $xml;
@@ -2062,9 +2063,9 @@ class Diaspora
                // "positive" = "false" would be a Dislike - wich isn't currently supported by Diaspora
                // We would accept this anyhow.
                if ($positive == "true") {
-                       $verb = ACTIVITY_LIKE;
+                       $verb = Activity::LIKE;
                } else {
-                       $verb = ACTIVITY_DISLIKE;
+                       $verb = Activity::DISLIKE;
                }
 
                $datarray = [];
@@ -2085,7 +2086,7 @@ class Diaspora
                $datarray["gravity"] = GRAVITY_ACTIVITY;
                $datarray["parent-uri"] = $parent_item["uri"];
 
-               $datarray["object-type"] = ACTIVITY_OBJ_NOTE;
+               $datarray["object-type"] = Activity\ObjectType::NOTE;
 
                $datarray["body"] = $verb;
 
@@ -2475,101 +2476,30 @@ class Diaspora
                        return false;
                }
 
-               $batch = (($ret["batch"]) ? $ret["batch"] : implode("/", array_slice(explode("/", $ret["url"]), 0, 3))."/receive/public");
-
-               q(
-                       "INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`nurl`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
-                       VALUES (%d, '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d)",
-                       intval($importer["uid"]),
-                       DBA::escape($ret["network"]),
-                       DBA::escape($ret["addr"]),
-                       DateTimeFormat::utcNow(),
-                       DBA::escape($ret["url"]),
-                       DBA::escape(Strings::normaliseLink($ret["url"])),
-                       DBA::escape($batch),
-                       DBA::escape($ret["name"]),
-                       DBA::escape($ret["nick"]),
-                       DBA::escape($ret["photo"]),
-                       DBA::escape($ret["pubkey"]),
-                       DBA::escape($ret["notify"]),
-                       DBA::escape($ret["poll"]),
-                       1,
-                       2
-               );
-
-               // find the contact record we just created
-
-               $contact_record = self::contactByHandle($importer["uid"], $author);
-
-               if (!$contact_record) {
-                       Logger::log("unable to locate newly created contact record.");
-                       return;
-               }
-
-               Logger::log("Author ".$author." was added as contact number ".$contact_record["id"].".", Logger::DEBUG);
-
-               Group::addMember(User::getDefaultGroup($importer['uid'], $ret["network"]), $contact_record['id']);
-
-               Contact::updateAvatar($ret["photo"], $importer['uid'], $contact_record["id"], true);
-
-               if (in_array($importer["page-flags"], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
-                       Logger::log("Sending intra message for author ".$author.".", Logger::DEBUG);
-
-                       $hash = Strings::getRandomHex().(string)time();   // Generate a confirm_key
-
-                       q(
-                               "INSERT INTO `intro` (`uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
-                               VALUES (%d, %d, %d, %d, '%s', '%s', '%s')",
-                               intval($importer["uid"]),
-                               intval($contact_record["id"]),
-                               0,
-                               0,
-                               DBA::escape(L10n::t("Sharing notification from Diaspora network")),
-                               DBA::escape($hash),
-                               DBA::escape(DateTimeFormat::utcNow())
-                       );
+               $cid = Contact::getIdForURL($ret['url'], $importer['uid']);
+               if (!empty($cid)) {
+                       $contact = DBA::selectFirst('contact', [], ['id' => $cid, 'network' => Protocol::NATIVE_SUPPORT]);
                } else {
-                       // automatic friend approval
-
-                       Logger::log("Does an automatic friend approval for author ".$author.".", Logger::DEBUG);
+                       $contact = [];
+               }
 
-                       Contact::updateAvatar($contact_record["photo"], $importer["uid"], $contact_record["id"]);
+               $item = ['author-id' => Contact::getIdForURL($ret['url']),
+                       'author-link' => $ret['url']];
 
-                       /*
-                        * technically they are sharing with us (Contact::SHARING),
-                        * but if our page-type is Profile::PAGE_COMMUNITY or Profile::PAGE_SOAPBOX
-                        * we are going to change the relationship and make them a follower.
-                        */
-                       if (($importer["page-flags"] == User::PAGE_FLAGS_FREELOVE) && $sharing && $following) {
-                               $new_relation = Contact::FRIEND;
-                       } elseif (($importer["page-flags"] == User::PAGE_FLAGS_FREELOVE) && $sharing) {
-                               $new_relation = Contact::SHARING;
-                       } else {
-                               $new_relation = Contact::FOLLOWER;
+               $result = Contact::addRelationship($importer, $contact, $item, false);
+               if ($result === true) {
+                       $contact_record = self::contactByHandle($importer['uid'], $author);
+                       if (!$contact_record) {
+                               Logger::info('unable to locate newly created contact record.');
+                               return;
                        }
 
-                       q(
-                               "UPDATE `contact` SET `rel` = %d,
-                               `name-date` = '%s',
-                               `uri-date` = '%s',
-                               `blocked` = 0,
-                               `pending` = 0,
-                               `writable` = 1
-                               WHERE `id` = %d
-                               ",
-                               intval($new_relation),
-                               DBA::escape(DateTimeFormat::utcNow()),
-                               DBA::escape(DateTimeFormat::utcNow()),
-                               intval($contact_record["id"])
-                       );
-
-                       $user = DBA::selectFirst('user', [], ['uid' => $importer["uid"]]);
+                       $user = DBA::selectFirst('user', [], ['uid' => $importer['uid']]);
                        if (DBA::isResult($user)) {
-                               Logger::log("Sending share message (Relation: ".$new_relation.") to author ".$author." - Contact: ".$contact_record["id"]." - User: ".$importer["uid"], Logger::DEBUG);
                                self::sendShare($user, $contact_record);
 
                                // Send the profile data, maybe it weren't transmitted before
-                               self::sendProfile($importer["uid"], [$contact_record]);
+                               self::sendProfile($importer['uid'], [$contact_record]);
                        }
                }
 
@@ -2684,9 +2614,9 @@ class Diaspora
                $datarray['uri'] = self::getUriFromGuid($author, $datarray['guid']);
                $datarray['parent-uri'] = $parent['uri'];
 
-               $datarray['verb'] = $datarray['body'] = ACTIVITY2_ANNOUNCE;
+               $datarray['verb'] = $datarray['body'] = Activity::ANNOUNCE;
                $datarray['gravity'] = GRAVITY_ACTIVITY;
-               $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
+               $datarray['object-type'] = Activity\ObjectType::NOTE;
 
                $datarray['protocol'] = $item['protocol'];
 
@@ -2757,7 +2687,7 @@ class Diaspora
                $datarray["guid"] = $guid;
                $datarray["uri"] = $datarray["parent-uri"] = self::getUriFromGuid($author, $guid);
 
-               $datarray["verb"] = ACTIVITY_POST;
+               $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_PARENT;
 
                $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
@@ -2962,9 +2892,9 @@ class Diaspora
                                        XML::unescape($photo->remote_photo_name)."[/img]\n".$body;
                        }
 
-                       $datarray["object-type"] = ACTIVITY_OBJ_IMAGE;
+                       $datarray["object-type"] = Activity\ObjectType::IMAGE;
                } else {
-                       $datarray["object-type"] = ACTIVITY_OBJ_NOTE;
+                       $datarray["object-type"] = Activity\ObjectType::NOTE;
 
                        // Add OEmbed and other information to the body
                        if (!self::isRedmatrix($contact["url"])) {
@@ -2994,7 +2924,7 @@ class Diaspora
                $datarray["guid"] = $guid;
                $datarray["uri"] = $datarray["parent-uri"] = self::getUriFromGuid($author, $guid);
 
-               $datarray["verb"] = ACTIVITY_POST;
+               $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_PARENT;
 
                $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
@@ -3654,6 +3584,14 @@ class Diaspora
                        $title = $item["title"];
                        $body = $item["body"];
 
+                       // Fetch the title from an attached link - if there is one
+                       if (empty($item["title"]) && PConfig::get($owner['uid'], 'system', 'append_title')) {
+                               $page_data = BBCode::getAttachmentData($item['body']);
+                               if (!empty($page_data['type']) && !empty($page_data['title']) && ($page_data['type'] == 'link')) {
+                                       $title = $page_data['title'];
+                               }
+                       }
+
                        if ($item['author-link'] != $item['owner-link']) {
                                require_once 'mod/share.php';
                                $body = share_header($item['author-name'], $item['author-link'], $item['author-avatar'],
@@ -3665,7 +3603,7 @@ class Diaspora
 
                        // Adding the title
                        if (strlen($title)) {
-                               $body = "## ".html_entity_decode($title)."\n\n".$body;
+                               $body = "### ".html_entity_decode($title)."\n\n".$body;
                        }
 
                        if ($item["attach"]) {
@@ -3780,9 +3718,9 @@ class Diaspora
 
                $target_type = ($parent["uri"] === $parent["parent-uri"] ? "Post" : "Comment");
                $positive = null;
-               if ($item['verb'] === ACTIVITY_LIKE) {
+               if ($item['verb'] === Activity::LIKE) {
                        $positive = "true";
-               } elseif ($item['verb'] === ACTIVITY_DISLIKE) {
+               } elseif ($item['verb'] === Activity::DISLIKE) {
                        $positive = "false";
                }
 
@@ -3811,13 +3749,13 @@ class Diaspora
                }
 
                switch ($item['verb']) {
-                       case ACTIVITY_ATTEND:
+                       case Activity::ATTEND:
                                $attend_answer = 'accepted';
                                break;
-                       case ACTIVITY_ATTENDNO:
+                       case Activity::ATTENDNO:
                                $attend_answer = 'declined';
                                break;
-                       case ACTIVITY_ATTENDMAYBE:
+                       case Activity::ATTENDMAYBE:
                                $attend_answer = 'tentative';
                                break;
                        default:
@@ -3913,13 +3851,13 @@ class Diaspora
         */
        public static function sendFollowup(array $item, array $owner, array $contact, $public_batch = false)
        {
-               if (in_array($item['verb'], [ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE])) {
+               if (in_array($item['verb'], [Activity::ATTEND, Activity::ATTENDNO, Activity::ATTENDMAYBE])) {
                        $message = self::constructAttend($item, $owner);
                        $type = "event_participation";
-               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               } elseif (in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        $message = self::constructLike($item, $owner);
                        $type = "like";
-               } elseif (!in_array($item["verb"], [ACTIVITY_FOLLOW, ACTIVITY_TAG])) {
+               } elseif (!in_array($item["verb"], [Activity::FOLLOW, Activity::TAG])) {
                        $message = self::constructComment($item, $owner);
                        $type = "comment";
                }
@@ -3948,7 +3886,7 @@ class Diaspora
                        $message = ["author" => $item['signer'],
                                        "target_guid" => $signed_parts[0],
                                        "target_type" => $signed_parts[1]];
-               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               } elseif (in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        $message = ["author" => $signed_parts[4],
                                        "guid" => $signed_parts[1],
                                        "parent_guid" => $signed_parts[3],
@@ -3993,7 +3931,7 @@ class Diaspora
        {
                if ($item["deleted"]) {
                        return self::sendRetraction($item, $owner, $contact, $public_batch, true);
-               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               } elseif (in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        $type = "like";
                } else {
                        $type = "comment";
@@ -4054,7 +3992,7 @@ class Diaspora
 
                if ($item['id'] == $item['parent']) {
                        $target_type = "Post";
-               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               } elseif (in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        $target_type = "Like";
                } else {
                        $target_type = "Comment";
@@ -4320,7 +4258,7 @@ class Diaspora
                        return false;
                }
 
-               if (!in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+               if (!in_array($item["verb"], [Activity::LIKE, Activity::DISLIKE])) {
                        return false;
                }