X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2FProtocol%2FDiaspora.php;h=5792d56b6eeef6cc0e28e6315cbb08f8ed939d6f;hb=0b6f43113ff09241d4fcd125f5950ded831000c1;hp=5036aa357b7fc999d02ada9fb2ff26b972d3bf00;hpb=ef1c73fb6b6c20429ecb9f617252babc6954016c;p=friendica.git diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 5036aa357b..5792d56b6e 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -16,6 +16,7 @@ use Friendica\Core\Cache; use Friendica\Core\Config; use Friendica\Core\L10n; use Friendica\Core\PConfig; +use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBA; @@ -176,7 +177,7 @@ class Diaspora { $fields = ['created' => DateTimeFormat::utcNow(), 'name' => 'relay', 'nick' => 'relay', - 'url' => $server_url, 'network' => NETWORK_DIASPORA, + 'url' => $server_url, 'network' => Protocol::DIASPORA, 'batch' => $server_url . '/receive/public', 'rel' => Contact::FOLLOWER, 'blocked' => false, 'pending' => false, 'writable' => true]; @@ -364,15 +365,16 @@ 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 array $importer Array of the importer user + * @param string $raw raw post message + * @param boolean $no_exit Don't do an http exit on error * * @return array * 'message' -> decoded Diaspora XML message * 'author' -> author diaspora handle * 'key' -> author public key (converted to pkcs#8) */ - public static function decodeRaw(array $importer, $raw) + public static function decodeRaw(array $importer, $raw, $no_exit = false) { $data = json_decode($raw); @@ -387,7 +389,11 @@ class Diaspora if (!is_object($j_outer_key_bundle)) { logger('Outer Salmon did not verify. Discarding.'); - System::httpExit(400); + if ($no_exit) { + return false; + } else { + System::httpExit(400); + } } $outer_iv = base64_decode($j_outer_key_bundle->iv); @@ -402,7 +408,11 @@ class Diaspora if (!is_object($basedom)) { logger('Received data does not seem to be an XML. Discarding. '.$xml); - System::httpExit(400); + if ($no_exit) { + return false; + } else { + System::httpExit(400); + } } $base = $basedom->children(NAMESPACE_SALMON_ME); @@ -424,19 +434,31 @@ class Diaspora $author_addr = base64_decode($key_id); if ($author_addr == '') { logger('No author could be decoded. Discarding. Message: ' . $xml); - System::httpExit(400); + if ($no_exit) { + return false; + } else { + System::httpExit(400); + } } $key = self::key($author_addr); if ($key == '') { logger("Couldn't get a key for handle " . $author_addr . ". Discarding."); - System::httpExit(400); + if ($no_exit) { + return false; + } else { + System::httpExit(400); + } } $verify = Crypto::rsaVerify($signed_data, $signature, $key); if (!$verify) { logger('Message did not verify. Discarding.'); - System::httpExit(400); + if ($no_exit) { + return false; + } else { + System::httpExit(400); + } } return ['message' => (string)base64url_decode($base->data), @@ -899,7 +921,7 @@ class Diaspora { $update = false; - $person = DBA::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]); + $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); if (DBA::isResult($person)) { logger("In cache " . print_r($person, true), LOGGER_DEBUG); @@ -916,17 +938,18 @@ class Diaspora if (!DBA::isResult($person) || $update) { logger("create or refresh", LOGGER_DEBUG); - $r = Probe::uri($handle, NETWORK_DIASPORA); + $r = Probe::uri($handle, Protocol::DIASPORA); // Note that Friendica contacts will return a "Diaspora person" // if Diaspora connectivity is enabled on their server - if ($r && ($r["network"] === NETWORK_DIASPORA)) { + if ($r && ($r["network"] === Protocol::DIASPORA)) { self::updateFContact($r); // Fetch the updated or added contact - $person = DBA::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]); + $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); if (!DBA::isResult($person)) { $person = $r; + $person['id'] = 0; } } } @@ -969,13 +992,10 @@ class Diaspora logger("contact id is ".$contact_id." - pcontact id is ".$pcontact_id, LOGGER_DEBUG); if ($pcontact_id != 0) { - $r = q( - "SELECT `addr` FROM `contact` WHERE `id` = %d AND `addr` != ''", - intval($pcontact_id) - ); + $contact = DBA::selectFirst('contact', ['addr'], ['id' => $pcontact_id]); - if (DBA::isResult($r)) { - return strtolower($r[0]["addr"]); + if (DBA::isResult($contact) && !empty($contact["addr"])) { + return strtolower($contact["addr"]); } } @@ -1017,7 +1037,7 @@ class Diaspora $r = q( "SELECT `url` FROM `fcontact` WHERE `url` != '' AND `network` = '%s' AND `guid` = '%s'", - DBA::escape(NETWORK_DIASPORA), + DBA::escape(Protocol::DIASPORA), DBA::escape($fcontact_guid) ); @@ -1052,7 +1072,7 @@ class Diaspora return false; } - $contact = dba::selectFirst('contact', [], ['id' => $cid]); + $contact = DBA::selectFirst('contact', [], ['id' => $cid]); if (!DBA::isResult($contact)) { // This here shouldn't happen at all logger("Haven't found a contact for user " . $uid . " and handle " . $handle, LOGGER_DEBUG); @@ -1081,7 +1101,7 @@ class Diaspora // It is deactivated by now, due to side effects. See issue https://github.com/friendica/friendica/pull/4033 // It is not removed by now. Possibly the code is needed? //if (!$is_comment && $contact["rel"] == Contact::FOLLOWER && in_array($importer["page-flags"], array(Contact::PAGE_FREELOVE))) { - // dba::update( + // DBA::update( // 'contact', // array('rel' => Contact::FRIEND, 'writable' => true), // array('id' => $contact["id"], 'uid' => $contact["uid"]) @@ -1180,7 +1200,7 @@ class Diaspora ); preg_replace_callback( - "&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi", + "&\[url=/?posts/([^\[\]]*)\](.*)\[\/url\]&Usi", function ($match) use ($item) { self::fetchGuidSub($match, $item); }, @@ -1419,7 +1439,7 @@ class Diaspora $network = $contact["network"]; } else { $cid = $def_contact["id"]; - $network = NETWORK_DIASPORA; + $network = Protocol::DIASPORA; } return ["cid" => $cid, "network" => $network]; @@ -1459,7 +1479,7 @@ class Diaspora } } - if ($contact["network"] == NETWORK_DFRN) { + if ($contact["network"] == Protocol::DFRN) { return str_replace("/profile/" . $contact["nick"] . "/", "/display/" . $guid, $contact["url"] . "/"); } @@ -1509,7 +1529,7 @@ class Diaspora // change the technical stuff in contact and gcontact $data = Probe::uri($new_handle); - if ($data['network'] == NETWORK_PHANTOM) { + if ($data['network'] == Protocol::PHANTOM) { logger('Account for '.$new_handle." couldn't be probed."); return false; } @@ -1573,17 +1593,13 @@ class Diaspora if (DBA::isResult($item)) { return $item["uri"]; } elseif (!$onlyfound) { - $contact = Contact::getDetailsByAddr($author, 0); - if (!empty($contact['network'])) { - $prefix = 'urn:X-' . $contact['network'] . ':'; - } else { - // This fallback should happen most unlikely - $prefix = 'urn:X-dspr:'; - } + $person = self::personByHandle($author); - $author_parts = explode('@', $author); + $parts = parse_url($person['url']); + unset($parts['path']); + $host_url = Network::unparseURL($parts); - return $prefix . $author_parts[1] . ':' . $author_parts[0] . ':'. $guid; + return $host_url . '/objects/' . $guid; } return ""; @@ -1787,12 +1803,7 @@ class Diaspora DBA::lock('mail'); - $r = q( - "SELECT `id` FROM `mail` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1", - DBA::escape($msg_guid), - intval($importer["uid"]) - ); - if (DBA::isResult($r)) { + if (DBA::exists('mail', ['guid' => $msg_guid, 'uid' => $importer["uid"]])) { logger("duplicate message already delivered.", LOGGER_DEBUG); return false; } @@ -1828,10 +1839,10 @@ class Diaspora "to_name" => $importer["username"], "to_email" => $importer["email"], "uid" =>$importer["uid"], - "item" => ["subject" => $subject, "body" => $body], + "item" => ["id" => $conversation["id"], "title" => $subject, "subject" => $subject, "body" => $body], "source_name" => $person["name"], "source_link" => $person["url"], - "source_photo" => $person["thumb"], + "source_photo" => $person["photo"], "verb" => ACTIVITY_POST, "otype" => "mail"] ); @@ -1867,16 +1878,8 @@ class Diaspora return false; } - $conversation = null; - - $c = q( - "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1", - intval($importer["uid"]), - DBA::escape($guid) - ); - if ($c) - $conversation = $c[0]; - else { + $conversation = DBA::selectFirst('conv', [], ['uid' => $importer["uid"], 'guid' => $guid]); + if (!DBA::isResult($conversation)) { $r = q( "INSERT INTO `conv` (`uid`, `guid`, `creator`, `created`, `updated`, `subject`, `recips`) VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s')", @@ -1889,15 +1892,7 @@ class Diaspora DBA::escape($participants) ); if ($r) { - $c = q( - "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1", - intval($importer["uid"]), - DBA::escape($guid) - ); - } - - if ($c) { - $conversation = $c[0]; + $conversation = DBA::selectFirst('conv', [], ['uid' => $importer["uid"], 'guid' => $guid]); } } if (!$conversation) { @@ -1975,11 +1970,8 @@ class Diaspora $datarray["contact-id"] = $author_contact["cid"]; $datarray["network"] = $author_contact["network"]; - $datarray["author-link"] = $person["url"]; - $datarray["author-id"] = Contact::getIdForURL($person["url"], 0); - - $datarray["owner-link"] = $contact["url"]; - $datarray["owner-id"] = Contact::getIdForURL($contact["url"], 0); + $datarray["owner-link"] = $datarray["author-link"] = $person["url"]; + $datarray["owner-id"] = $datarray["author-id"] = Contact::getIdForURL($person["url"], 0); $datarray["guid"] = $guid; $datarray["uri"] = self::getUriFromGuid($author, $guid); @@ -2048,14 +2040,10 @@ class Diaspora $conversation = null; - $c = q( - "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1", - intval($importer["uid"]), - DBA::escape($conversation_guid) - ); - if ($c) { - $conversation = $c[0]; - } else { + $condition = ['uid' => $importer["uid"], 'guid' => $conversation_guid]; + $conversation = DBA::selectFirst('conv', [], $condition); + + if (!DBA::isResult($conversation)) { logger("conversation not available."); return false; } @@ -2074,12 +2062,7 @@ class Diaspora DBA::lock('mail'); - $r = q( - "SELECT `id` FROM `mail` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1", - DBA::escape($guid), - intval($importer["uid"]) - ); - if (DBA::isResult($r)) { + if (DBA::exists('mail', ['guid' => $guid, 'uid' => $importer["uid"]])) { logger("duplicate message already delivered.", LOGGER_DEBUG); return false; } @@ -2281,7 +2264,7 @@ class Diaspora DBA::update('contact', $fields, ['id' => $contact['id']]); - $gcontact = ["url" => $contact["url"], "network" => NETWORK_DIASPORA, "generation" => 2, + $gcontact = ["url" => $contact["url"], "network" => Protocol::DIASPORA, "generation" => 2, "photo" => $image_url, "name" => $name, "location" => $location, "about" => $about, "birthday" => $birthday, "gender" => $gender, "addr" => $author, "nick" => $nick, "keywords" => $keywords, @@ -2362,10 +2345,10 @@ class Diaspora // If we are now friends, we are sending a share message. // Normally we needn't to do so, but the first message could have been vanished. if (in_array($contact["rel"], [Contact::FRIEND])) { - $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"])); - if ($u) { + $user = DBA::selectFirst('user', [], ['uid' => $importer["uid"]]); + if (DBA::isResult($user)) { logger("Sending share message to author ".$author." - Contact: ".$contact["id"]." - User: ".$importer["uid"], LOGGER_DEBUG); - $ret = self::sendShare($u[0], $contact); + $ret = self::sendShare($user, $contact); } } return true; @@ -2392,7 +2375,7 @@ class Diaspora $ret = self::personByHandle($author); - if (!$ret || ($ret["network"] != NETWORK_DIASPORA)) { + if (!$ret || ($ret["network"] != Protocol::DIASPORA)) { logger("Cannot resolve diaspora handle ".$author." for ".$recipient); return false; } @@ -2457,10 +2440,11 @@ class Diaspora Contact::updateAvatar($contact_record["photo"], $importer["uid"], $contact_record["id"]); - // technically they are sharing with us (Contact::SHARING), - // but if our page-type is PAGE_COMMUNITY or PAGE_SOAPBOX - // we are going to change the relationship and make them a follower. - + /* + * 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"] == Contact::PAGE_FREELOVE) && $sharing && $following) { $new_relation = Contact::FRIEND; } elseif (($importer["page-flags"] == Contact::PAGE_FREELOVE) && $sharing) { @@ -2484,10 +2468,10 @@ class Diaspora intval($contact_record["id"]) ); - $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"])); - if ($u) { + $user = DBA::selectFirst('user', [], ['uid' => $importer["uid"]]); + if (DBA::isResult($user)) { logger("Sending share message (Relation: ".$new_relation.") to author ".$author." - Contact: ".$contact_record["id"]." - User: ".$importer["uid"], LOGGER_DEBUG); - $ret = self::sendShare($u[0], $contact_record); + $ret = self::sendShare($user, $contact_record); // Send the profile data, maybe it weren't transmitted before self::sendProfile($importer["uid"], [$contact_record]); @@ -2617,7 +2601,7 @@ class Diaspora $datarray["uid"] = $importer["uid"]; $datarray["contact-id"] = $contact["id"]; - $datarray["network"] = NETWORK_DIASPORA; + $datarray["network"] = Protocol::DIASPORA; $datarray["author-link"] = $contact["url"]; $datarray["author-id"] = Contact::getIdForURL($contact["url"], 0); @@ -2787,7 +2771,7 @@ class Diaspora * * @return int The message id of the newly created item */ - private static function receiveStatusMessage(array $importer, $data, $xml) + private static function receiveStatusMessage(array $importer, SimpleXMLElement $data, $xml) { $author = notags(unxmlify($data->author)); $guid = notags(unxmlify($data->guid)); @@ -2845,7 +2829,7 @@ class Diaspora $datarray["uid"] = $importer["uid"]; $datarray["contact-id"] = $contact["id"]; - $datarray["network"] = NETWORK_DIASPORA; + $datarray["network"] = Protocol::DIASPORA; $datarray["author-link"] = $contact["url"]; $datarray["author-id"] = Contact::getIdForURL($contact["url"], 0); @@ -3096,8 +3080,8 @@ class Diaspora if (!intval(Config::get("system", "diaspora_test"))) { $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json"); - Network::post($dest_url."/", $envelope, ["Content-Type: ".$content_type]); - $return_code = $a->get_curl_code(); + $postResult = Network::post($dest_url."/", $envelope, ["Content-Type: ".$content_type]); + $return_code = $postResult->getReturnCode(); } else { logger("test_mode"); return 200; @@ -3106,11 +3090,11 @@ class Diaspora logger("transmit: ".$logid."-".$guid." to ".$dest_url." returns: ".$return_code); - if (!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) { - if (!$no_queue && ($contact['contact-type'] != Contact::ACCOUNT_TYPE_RELAY)) { + if (!$return_code || (($return_code == 503) && (stristr($postResult->getHeader(), "retry-after")))) { + if (!$no_queue && !empty($contact['contact-type']) && ($contact['contact-type'] != Contact::ACCOUNT_TYPE_RELAY)) { logger("queue message"); // queue message for redelivery - Queue::add($contact["id"], NETWORK_DIASPORA, $envelope, $public_batch, $guid); + Queue::add($contact["id"], Protocol::DIASPORA, $envelope, $public_batch, $guid); } // The message could not be delivered. We mark the contact as "dead" @@ -3167,7 +3151,7 @@ class Diaspora $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $contact['pubkey'], $public_batch); if ($spool) { - Queue::add($contact['id'], NETWORK_DIASPORA, $envelope, $public_batch, $guid); + Queue::add($contact['id'], Protocol::DIASPORA, $envelope, $public_batch, $guid); return true; } else { $return_code = self::transmit($owner, $contact, $envelope, $public_batch, false, $guid); @@ -3214,7 +3198,7 @@ class Diaspora $author = self::myHandle($owner); $message = ["author" => $author, - "guid" => System::createGUID(32), + "guid" => System::createUUID(), "parent_type" => "Post", "parent_guid" => $item["guid"]]; @@ -3358,7 +3342,7 @@ class Diaspora } if (($guid != "") && $complete) { - $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]]; + $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]]; $item = Item::selectFirst(['contact-id'], $condition); if (DBA::isResult($item)) { $ret= []; @@ -3446,12 +3430,9 @@ class Diaspora /// @todo - establish "all day" events in Friendica $eventdata["all_day"] = "false"; - if (!$event['adjust']) { + $eventdata['timezone'] = 'UTC'; + if (!$event['adjust'] && $user['timezone']) { $eventdata['timezone'] = $user['timezone']; - - if ($eventdata['timezone'] == "") { - $eventdata['timezone'] = 'UTC'; - } } if ($event['start']) { @@ -3506,7 +3487,7 @@ class Diaspora $myaddr = self::myHandle($owner); - $public = (($item["private"]) ? "false" : "true"); + $public = ($item["private"] ? "false" : "true"); $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM); @@ -3766,13 +3747,13 @@ class Diaspora * * @return string The message */ - private static function messageFromSignature(array $item, array $signature) + private static function messageFromSignature(array $item) { // Split the signed text - $signed_parts = explode(";", $signature['signed_text']); + $signed_parts = explode(";", $item['signed_text']); if ($item["deleted"]) { - $message = ["author" => $signature['signer'], + $message = ["author" => $item['signer'], "target_guid" => $signed_parts[0], "target_type" => $signed_parts[1]]; } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) { @@ -3781,7 +3762,7 @@ class Diaspora "parent_guid" => $signed_parts[3], "parent_type" => $signed_parts[2], "positive" => $signed_parts[0], - "author_signature" => $signature['signature'], + "author_signature" => $item['signature'], "parent_author_signature" => ""]; } else { // Remove the comment guid @@ -3800,7 +3781,7 @@ class Diaspora "guid" => $guid, "parent_guid" => $parent_guid, "text" => implode(";", $signed_parts), - "author_signature" => $signature['signature'], + "author_signature" => $item['signature'], "parent_author_signature" => ""]; } return $message; @@ -3828,26 +3809,12 @@ class Diaspora logger("Got relayable data ".$type." for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG); - // fetch the original signature - - $r = q( - "SELECT `signed_text`, `signature`, `signer` FROM `sign` WHERE `iid` = %d LIMIT 1", - intval($item["id"]) - ); - - if (!$r) { - logger("Couldn't fetch signatur for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG); - return false; - } - - $signature = $r[0]; - // Old way - is used by the internal Friendica functions /// @todo Change all signatur storing functions to the new format - if ($signature['signed_text'] && $signature['signature'] && $signature['signer']) { - $message = self::messageFromSignature($item, $signature); + if ($item['signed_text'] && $item['signature'] && $item['signer']) { + $message = self::messageFromSignature($item); } else {// New way - $msg = json_decode($signature['signed_text'], true); + $msg = json_decode($item['signed_text'], true); $message = []; if (is_array($msg)) { @@ -3864,7 +3831,7 @@ class Diaspora $message[$field] = $data; } } else { - logger("Signature text for item ".$item["guid"]." (".$item["id"].") couldn't be extracted: ".$signature['signed_text'], LOGGER_DEBUG); + logger("Signature text for item ".$item["guid"]." (".$item["id"].") couldn't be extracted: ".$item['signed_text'], LOGGER_DEBUG); } } @@ -3922,17 +3889,11 @@ class Diaspora { $myaddr = self::myHandle($owner); - $r = q( - "SELECT * FROM `conv` WHERE `id` = %d AND `uid` = %d LIMIT 1", - intval($item["convid"]), - intval($item["uid"]) - ); - - if (!DBA::isResult($r)) { + $cnv = DBA::selectFirst('conv', [], ['id' => $item["convid"], 'uid' => $item["uid"]]); + if (!DBA::isResult($cnv)) { logger("conversation not found."); return; } - $cnv = $r[0]; $conv = [ "author" => $cnv["creator"], @@ -4133,7 +4094,7 @@ class Diaspora $recips = q( "SELECT `id`,`name`,`network`,`pubkey`,`notify` FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `rel` != %d", - DBA::escape(NETWORK_DIASPORA), + DBA::escape(Protocol::DIASPORA), intval($uid), intval(Contact::SHARING) ); @@ -4167,12 +4128,12 @@ class Diaspora return false; } - $r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($contact['uid'])); - if (!DBA::isResult($r)) { + $user = DBA::selectFirst('user', ['prvkey'], ['uid' => $contact["uid"]]); + if (!DBA::isResult($user)) { return false; } - $contact["uprvkey"] = $r[0]['prvkey']; + $contact["uprvkey"] = $user['prvkey']; $item = Item::selectFirst([], ['id' => $post_id]); if (!DBA::isResult($item)) {