X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FDiaspora.php;h=7704d3f07b4e7aae14ce30626fbd0a13277a8526;hb=51ee24f65220f4d5e3b0717767fa399e1a5a58e5;hp=587b34c3fe3b4839927e52a536fb09042f39ef40;hpb=185f1fc6877ba4913db09509d36fedea3a6327b2;p=friendica.git diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 587b34c3fe..7704d3f07b 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -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 @@ -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]); } } @@ -2593,7 +2523,7 @@ class Diaspora } // Do we already have this item? - $fields = ['body', 'tag', 'app', 'created', 'object-type', 'uri', 'guid', + $fields = ['body', 'title', 'attach', 'tag', 'app', 'created', 'object-type', 'uri', 'guid', 'author-name', 'author-link', 'author-avatar']; $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => false]; $item = Item::selectFirst($fields, $condition); @@ -2637,7 +2567,7 @@ class Diaspora } if ($stored) { - $fields = ['body', 'tag', 'app', 'created', 'object-type', 'uri', 'guid', + $fields = ['body', 'title', 'attach', 'tag', 'app', 'created', 'object-type', 'uri', 'guid', 'author-name', 'author-link', 'author-avatar']; $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => false]; $item = Item::selectFirst($fields, $condition); @@ -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; @@ -2771,9 +2701,15 @@ class Diaspora $original_item["created"], $orig_url ); + + if (!empty($original_item['title'])) { + $prefix .= '[h3]' . $original_item['title'] . "[/h3]\n"; + } + $datarray["body"] = $prefix.$original_item["body"]."[/share]"; $datarray["tag"] = $original_item["tag"]; + $datarray["attach"] = $original_item["attach"]; $datarray["app"] = $original_item["app"]; $datarray["plink"] = self::plink($author, $guid); @@ -2962,9 +2898,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 +2930,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; @@ -3455,72 +3391,40 @@ class Diaspora { $body = trim($body); - // Skip if it isn't a pure repeated messages - // Does it start with a share? - if ((strpos($body, "[share") > 0) && $complete) { - return false; - } - - // Does it end with a share? - if (strlen($body) > (strrpos($body, "[/share]") + 8)) { + $reshared = Item::getShareArray(['body' => $body]); + if (empty($reshared)) { return false; } - $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body); - // Skip if there is no shared message in there - if ($body == $attributes) { + // Skip if it isn't a pure repeated messages + // Does it start with a share? + if (!empty($reshared['comment']) && $complete) { return false; } - // If we don't do the complete check we quit here - - $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]; - } - - if (($guid != "") && $complete) { - $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]]; + if (!empty($reshared['guid']) && $complete) { + $condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]]; $item = Item::selectFirst(['contact-id'], $condition); if (DBA::isResult($item)) { - $ret= []; + $ret = []; $ret["root_handle"] = self::handleFromContact($item["contact-id"]); - $ret["root_guid"] = $guid; + $ret["root_guid"] = $reshared['guid']; return $ret; } elseif ($complete) { // We are resharing something that isn't a DFRN or Diaspora post. // So we have to return "false" on "$complete" to not trigger a reshare. return false; } - } elseif (($guid == "") && $complete) { + } elseif (empty($reshared['guid']) && $complete) { return false; } - $ret["root_guid"] = $guid; - - $profile = ""; - preg_match("/profile='(.*?)'/ism", $attributes, $matches); - if (!empty($matches[1])) { - $profile = $matches[1]; - } - - preg_match('/profile="(.*?)"/ism', $attributes, $matches); - if (!empty($matches[1])) { - $profile = $matches[1]; - } - - $ret= []; + $ret = []; - if ($profile != "") { - if (Contact::getIdForURL($profile)) { - $author = Contact::getDetailsByURL($profile); - $ret["root_handle"] = $author['addr']; + if (!empty($reshared['profile']) && ($cid = Contact::getIdForURL($reshared['profile']))) { + $contact = DBA::selectFirst('contact', ['addr'], ['id' => $cid]); + if (!empty($contact['addr'])) { + $ret['root_handle'] = $contact['addr']; } } @@ -3635,8 +3539,7 @@ class Diaspora $myaddr = self::myHandle($owner); $public = ($item["private"] ? "false" : "true"); - - $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM); + $created = DateTimeFormat::utc($item['received'], DateTimeFormat::ATOM); $edited = DateTimeFormat::utc($item["edited"] ?? $item["created"], DateTimeFormat::ATOM); // Detect a share element and do a reshare @@ -3654,6 +3557,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', 'attach_link_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 +3576,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 +3691,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 +3722,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 +3824,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 +3859,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 +3904,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 +3965,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 +4231,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; }