X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FDiaspora.php;h=a2a4f36031583d731143b8f2600e1f03a1bc73e3;hb=26ea6f69d726a0a9ff87e5db2f42126ec56708f3;hp=077c93c292e615496e6a690e6193e84f66e4cda0;hpb=35d06bd9ebee5bad76d81a1d434090f3e255e20f;p=friendica.git diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 077c93c292..a2a4f36031 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -7,14 +7,16 @@ * This implementation here interprets the old and the new protocol and sends the new one. * In the future we will remove most stuff from "validPosting" and interpret only the new protocol. */ + namespace Friendica\Protocol; -use Friendica\App; -use Friendica\Core\System; +use Friendica\Content\Text\BBCode; +use Friendica\Content\Text\Markdown; use Friendica\Core\Cache; use Friendica\Core\Config; use Friendica\Core\L10n; use Friendica\Core\PConfig; +use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; @@ -26,8 +28,8 @@ use Friendica\Model\Queue; use Friendica\Model\User; use Friendica\Network\Probe; use Friendica\Util\Crypto; +use Friendica\Util\DateTimeFormat; use Friendica\Util\Network; -use Friendica\Util\Temporal; use Friendica\Util\XML; use dba; use SimpleXMLElement; @@ -35,7 +37,6 @@ use SimpleXMLElement; require_once 'include/dba.php'; require_once 'include/items.php'; require_once 'include/bb2diaspora.php'; -require_once 'include/datetime.php'; /** * @brief This class contain functions to create and send Diaspora XML files @@ -77,7 +78,7 @@ class Diaspora $r = q( "INSERT INTO `contact` (`uid`, `created`, `name`, `nick`, `addr`, `url`, `nurl`, `batch`, `network`, `rel`, `blocked`, `pending`, `writable`, `name-date`, `uri-date`, `avatar-date`) VALUES (0, '%s', '%s', 'relay', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, '%s', '%s', '%s')", - Temporal::utcNow(), + DateTimeFormat::utcNow(), dbesc($addr), dbesc($addr), dbesc($server), @@ -85,9 +86,9 @@ class Diaspora dbesc($batch), dbesc(NETWORK_DIASPORA), intval(CONTACT_IS_FOLLOWER), - dbesc(Temporal::utcNow()), - dbesc(Temporal::utcNow()), - dbesc(Temporal::utcNow()) + dbesc(DateTimeFormat::utcNow()), + dbesc(DateTimeFormat::utcNow()), + dbesc(DateTimeFormat::utcNow()) ); $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch)); @@ -357,6 +358,9 @@ class Diaspora } $children = $basedom->children('https://joindiaspora.com/protocol'); + $inner_aes_key = null; + $inner_iv = null; + if ($children->header) { $public = true; $author_link = str_replace('acct:', '', $children->header->author_id); @@ -395,6 +399,7 @@ class Diaspora // figure out where in the DOM tree our data is hiding + $base = null; if ($dom->provenance->data) { $base = $dom->provenance; } elseif ($dom->env->data) { @@ -640,8 +645,6 @@ class Diaspora return false; } - $first_child = $data->getName(); - // Is this the new or the old version? if ($data->getName() == "XML") { $oldXML = true; @@ -670,6 +673,8 @@ class Diaspora $fields = new SimpleXMLElement("<".$type."/>"); $signed_data = ""; + $author_signature = null; + $parent_author_signature = null; foreach ($element->children() as $fieldname => $entry) { if ($oldXML) { @@ -798,14 +803,11 @@ class Diaspora */ public static function personByHandle($handle) { - $r = q( - "SELECT * FROM `fcontact` WHERE `network` = '%s' AND `addr` = '%s' LIMIT 1", - dbesc(NETWORK_DIASPORA), - dbesc($handle) - ); - if ($r) { - $person = $r[0]; - logger("In cache " . print_r($r, true), LOGGER_DEBUG); + $update = false; + + $person = dba::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]); + if (DBM::is_result($person)) { + logger("In cache " . print_r($person, true), LOGGER_DEBUG); // update record occasionally so it doesn't get stale $d = strtotime($person["updated"]." +00:00"); @@ -818,7 +820,7 @@ class Diaspora } } - if (!$person || $update) { + if (!DBM::is_result($person) || $update) { logger("create or refresh", LOGGER_DEBUG); $r = Probe::uri($handle, NETWORK_DIASPORA); @@ -826,9 +828,15 @@ class Diaspora // if Diaspora connectivity is enabled on their server if ($r && ($r["network"] === NETWORK_DIASPORA)) { self::addFContact($r, $update); - $person = $r; + + // Fetch the updated or added contact + $person = dba::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]); + if (!DBM::is_result($person)) { + $person = $r; + } } } + return $person; } @@ -871,7 +879,7 @@ class Diaspora dbesc($arr["confirm"]), dbesc($arr["alias"]), dbesc($arr["pubkey"]), - dbesc(Temporal::utcNow()), + dbesc(DateTimeFormat::utcNow()), dbesc($arr["url"]), dbesc($arr["network"]) ); @@ -894,7 +902,7 @@ class Diaspora dbesc($arr["network"]), dbesc($arr["alias"]), dbesc($arr["pubkey"]), - dbesc(Temporal::utcNow()) + dbesc(DateTimeFormat::utcNow()) ); } @@ -902,23 +910,23 @@ class Diaspora } /** - * @brief get a handle (user@domain.tld) from a given contact id or gcontact id + * @brief get a handle (user@domain.tld) from a given contact id * * @param int $contact_id The id in the contact table - * @param int $gcontact_id The id in the gcontact table + * @param int $pcontact_id The id in the contact table (Used for the public contact) * * @return string the handle */ - public static function handleFromContact($contact_id, $gcontact_id = 0) + private static function handleFromContact($contact_id, $pcontact_id = 0) { $handle = false; - logger("contact id is ".$contact_id." - gcontact id is ".$gcontact_id, LOGGER_DEBUG); + logger("contact id is ".$contact_id." - pcontact id is ".$pcontact_id, LOGGER_DEBUG); - if ($gcontact_id != 0) { + if ($pcontact_id != 0) { $r = q( - "SELECT `addr` FROM `gcontact` WHERE `id` = %d AND `addr` != ''", - intval($gcontact_id) + "SELECT `addr` FROM `contact` WHERE `id` = %d AND `addr` != ''", + intval($pcontact_id) ); if (DBM::is_result($r)) { @@ -1059,7 +1067,7 @@ class Diaspora //} // We don't seem to like that person - if ($contact["blocked"] || $contact["readonly"] || $contact["archive"]) { + if ($contact["blocked"] || $contact["readonly"]) { // Maybe blocked, don't accept. return false; // We are following this person? @@ -1376,26 +1384,23 @@ class Diaspora /** * @brief returns contact details * - * @param array $contact The default contact if the person isn't found - * @param array $person The record of the person - * @param int $uid The user id + * @param array $def_contact The default contact if the person isn't found + * @param array $person The record of the person + * @param int $uid The user id * * @return array * 'cid' => contact id * 'network' => network type */ - private static function authorContactByUrl($contact, $person, $uid) + private static function authorContactByUrl($def_contact, $person, $uid) { - $r = q( - "SELECT `id`, `network`, `url` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1", - dbesc(normalise_link($person["url"])), - intval($uid) - ); - if ($r) { - $cid = $r[0]["id"]; - $network = $r[0]["network"]; - } else { + $condition = ['nurl' => normalise_link($person["url"]), 'uid' => $uid]; + $contact = dba::selectFirst('contact', ['id', 'network'], $condition); + if (DBM::is_result($contact)) { $cid = $contact["id"]; + $network = $contact["network"]; + } else { + $cid = $def_contact["id"]; $network = NETWORK_DIASPORA; } @@ -1654,9 +1659,9 @@ class Diaspora $text = unxmlify($data->text); if (isset($data->created_at)) { - $created_at = Temporal::utc(notags(unxmlify($data->created_at))); + $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at))); } else { - $created_at = Temporal::utcNow(); + $created_at = DateTimeFormat::utcNow(); } if (isset($data->thread_parent_guid)) { @@ -1726,7 +1731,7 @@ class Diaspora $datarray["plink"] = self::plink($author, $guid, $parent_item['guid']); - $body = diaspora2bb($text); + $body = Markdown::toBBCode($text); $datarray["body"] = self::replacePeopleGuid($body, $person["url"]); @@ -1786,14 +1791,14 @@ class Diaspora $msg_guid = notags(unxmlify($mesg->guid)); $msg_conversation_guid = notags(unxmlify($mesg->conversation_guid)); $msg_text = unxmlify($mesg->text); - $msg_created_at = Temporal::utc(notags(unxmlify($mesg->created_at))); + $msg_created_at = DateTimeFormat::utc(notags(unxmlify($mesg->created_at))); if ($msg_conversation_guid != $guid) { logger("message conversation guid does not belong to the current conversation."); return false; } - $body = diaspora2bb($msg_text); + $body = Markdown::toBBCode($msg_text); $message_uri = $msg_author.":".$msg_guid; $person = self::personByHandle($msg_author); @@ -1831,7 +1836,7 @@ class Diaspora dba::unlock(); - dba::update('conv', ['updated' => Temporal::utcNow()], ['id' => $conversation["id"]]); + dba::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]); notification( [ @@ -1865,7 +1870,7 @@ class Diaspora $author = notags(unxmlify($data->author)); $guid = notags(unxmlify($data->guid)); $subject = notags(unxmlify($data->subject)); - $created_at = Temporal::utc(notags(unxmlify($data->created_at))); + $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at))); $participants = notags(unxmlify($data->participants)); $messages = $data->message; @@ -1897,7 +1902,7 @@ class Diaspora dbesc($guid), dbesc($author), dbesc($created_at), - dbesc(Temporal::utcNow()), + dbesc(DateTimeFormat::utcNow()), dbesc($subject), dbesc($participants) ); @@ -2098,7 +2103,7 @@ class Diaspora $guid = notags(unxmlify($data->guid)); $conversation_guid = notags(unxmlify($data->conversation_guid)); $text = unxmlify($data->text); - $created_at = Temporal::utc(notags(unxmlify($data->created_at))); + $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at))); $contact = self::allowedContactByHandle($importer, $author, true); if (!$contact) { @@ -2127,7 +2132,7 @@ class Diaspora return false; } - $body = diaspora2bb($text); + $body = Markdown::toBBCode($text); $body = self::replacePeopleGuid($body, $person["url"]); @@ -2164,7 +2169,7 @@ class Diaspora dba::unlock(); - dba::update('conv', ['updated' => Temporal::utcNow()], ['id' => $conversation["id"]]); + dba::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]); return true; } @@ -2279,8 +2284,8 @@ class Diaspora $image_url = unxmlify($data->image_url); $birthday = unxmlify($data->birthday); $gender = unxmlify($data->gender); - $about = diaspora2bb(unxmlify($data->bio)); - $location = diaspora2bb(unxmlify($data->location)); + $about = Markdown::toBBCode(unxmlify($data->bio)); + $location = Markdown::toBBCode(unxmlify($data->location)); $searchable = (unxmlify($data->searchable) == "true"); $nsfw = (unxmlify($data->nsfw) == "true"); $tags = unxmlify($data->tag_string); @@ -2315,7 +2320,7 @@ class Diaspora $birthday = str_replace("1000", "1901", $birthday); if ($birthday != "") { - $birthday = Temporal::utc($birthday, "Y-m-d"); + $birthday = DateTimeFormat::utc($birthday, "Y-m-d"); } // this is to prevent multiple birthday notifications in a single year @@ -2331,7 +2336,7 @@ class Diaspora dbesc($name), dbesc($nick), dbesc($author), - dbesc(Temporal::utcNow()), + dbesc(DateTimeFormat::utcNow()), dbesc($birthday), dbesc($location), dbesc($about), @@ -2537,7 +2542,7 @@ class Diaspora intval($importer["uid"]), dbesc($ret["network"]), dbesc($ret["addr"]), - Temporal::utcNow(), + DateTimeFormat::utcNow(), dbesc($ret["url"]), dbesc(normalise_link($ret["url"])), dbesc($batch), @@ -2580,7 +2585,7 @@ class Diaspora 0, dbesc(L10n::t("Sharing notification from Diaspora network")), dbesc($hash), - dbesc(Temporal::utcNow()) + dbesc(DateTimeFormat::utcNow()) ); } else { // automatic friend approval @@ -2611,8 +2616,8 @@ class Diaspora WHERE `id` = %d ", intval($new_relation), - dbesc(Temporal::utcNow()), - dbesc(Temporal::utcNow()), + dbesc(DateTimeFormat::utcNow()), + dbesc(DateTimeFormat::utcNow()), intval($contact_record["id"]) ); @@ -2657,7 +2662,7 @@ class Diaspora if (self::isReshare($r[0]["body"], true)) { $r = []; } elseif (self::isReshare($r[0]["body"], false) || strstr($r[0]["body"], "[share")) { - $r[0]["body"] = diaspora2bb(bb2diaspora($r[0]["body"])); + $r[0]["body"] = Markdown::toBBCode(BBCode::toMarkdown($r[0]["body"])); $r[0]["body"] = self::replacePeopleGuid($r[0]["body"], $r[0]["author-link"]); @@ -2692,7 +2697,7 @@ class Diaspora if (DBM::is_result($r)) { // If it is a reshared post from another network then reformat to avoid display problems with two share elements if (self::isReshare($r[0]["body"], false)) { - $r[0]["body"] = diaspora2bb(bb2diaspora($r[0]["body"])); + $r[0]["body"] = Markdown::toBBCode(BBCode::toMarkdown($r[0]["body"])); $r[0]["body"] = self::replacePeopleGuid($r[0]["body"], $r[0]["author-link"]); } @@ -2716,7 +2721,7 @@ class Diaspora { $author = notags(unxmlify($data->author)); $guid = notags(unxmlify($data->guid)); - $created_at = Temporal::utc(notags(unxmlify($data->created_at))); + $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at))); $root_author = notags(unxmlify($data->root_author)); $root_guid = notags(unxmlify($data->root_guid)); /// @todo handle unprocessed property "provider_display_name" @@ -2844,23 +2849,7 @@ class Diaspora continue; } - // Currently we don't have a central deletion function that we could use in this case. - // The function "item_drop" doesn't work for that case - dba::update( - 'item', - [ - 'deleted' => true, - 'title' => '', - 'body' => '', - 'edited' => Temporal::utcNow(), - 'changed' => Temporal::utcNow()], - ['id' => $item["id"]] - ); - - // Delete the thread - if it is a starting post and not a comment - if ($target_type != 'Comment') { - delete_thread($item["id"], $item["parent-uri"]); - } + Item::deleteById($item["id"]); logger("Deleted target ".$target_guid." (".$item["id"].") from user ".$item["uid"]." parent: ".$item["parent"], LOGGER_DEBUG); @@ -2930,7 +2919,7 @@ class Diaspora { $author = notags(unxmlify($data->author)); $guid = notags(unxmlify($data->guid)); - $created_at = Temporal::utc(notags(unxmlify($data->created_at))); + $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at))); $public = notags(unxmlify($data->public)); $text = unxmlify($data->text); $provider_display_name = notags(unxmlify($data->provider_display_name)); @@ -2952,7 +2941,7 @@ class Diaspora } } - $body = diaspora2bb($text); + $body = Markdown::toBBCode($text); $datarray = []; @@ -3199,7 +3188,7 @@ class Diaspora * * @return int Result of the transmission */ - public static function transmit($owner, $contact, $envelope, $public_batch, $queue_run = false, $guid = "") + public static function transmit($owner, $contact, $envelope, $public_batch, $queue_run = false, $guid = "", $no_queue = false) { $a = get_app(); @@ -3242,30 +3231,20 @@ class Diaspora logger("transmit: ".$logid."-".$guid." returns: ".$return_code); if (!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) { - logger("queue message"); - - $r = q( - "SELECT `id` FROM `queue` WHERE `cid` = %d AND `network` = '%s' AND `content` = '%s' AND `batch` = %d LIMIT 1", - intval($contact["id"]), - dbesc(NETWORK_DIASPORA), - dbesc($envelope), - intval($public_batch) - ); - if ($r) { - logger("add_to_queue ignored - identical item already in queue"); - } else { + if (!$no_queue) { + logger("queue message"); // queue message for redelivery - Queue::add($contact["id"], NETWORK_DIASPORA, $envelope, $public_batch); - - // The message could not be delivered. We mark the contact as "dead" - Contact::markForArchival($contact); + Queue::add($contact["id"], NETWORK_DIASPORA, $envelope, $public_batch, $guid); } + + // The message could not be delivered. We mark the contact as "dead" + Contact::markForArchival($contact); } elseif (($return_code >= 200) && ($return_code <= 299)) { // We successfully delivered a message, the contact is alive Contact::unmarkForArchival($contact); } - return(($return_code) ? $return_code : (-1)); + return $return_code ? $return_code : -1; } @@ -3312,7 +3291,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); + Queue::add($contact['id'], NETWORK_DIASPORA, $envelope, $public_batch, $guid); return true; } else { $return_code = self::transmit($owner, $contact, $envelope, $public_batch, false, $guid); @@ -3593,7 +3572,7 @@ class Diaspora $eventdata['guid'] = $event['guid']; } - $mask = Temporal::ATOM; + $mask = DateTimeFormat::ATOM; /// @todo - establish "all day" events in Friendica $eventdata["all_day"] = "false"; @@ -3607,20 +3586,20 @@ class Diaspora } if ($event['start']) { - $eventdata['start'] = Temporal::convert($event['start'], "UTC", $eventdata['timezone'], $mask); + $eventdata['start'] = DateTimeFormat::convert($event['start'], "UTC", $eventdata['timezone'], $mask); } if ($event['finish'] && !$event['nofinish']) { - $eventdata['end'] = Temporal::convert($event['finish'], "UTC", $eventdata['timezone'], $mask); + $eventdata['end'] = DateTimeFormat::convert($event['finish'], "UTC", $eventdata['timezone'], $mask); } if ($event['summary']) { - $eventdata['summary'] = html_entity_decode(bb2diaspora($event['summary'])); + $eventdata['summary'] = html_entity_decode(BBCode::toMarkdown($event['summary'])); } if ($event['desc']) { - $eventdata['description'] = html_entity_decode(bb2diaspora($event['desc'])); + $eventdata['description'] = html_entity_decode(BBCode::toMarkdown($event['desc'])); } if ($event['location']) { $location = []; - $location["address"] = html_entity_decode(bb2diaspora($event['location'])); + $location["address"] = html_entity_decode(BBCode::toMarkdown($event['location'])); $location["lat"] = 0; $location["lng"] = 0; $eventdata['location'] = $location; @@ -3652,7 +3631,7 @@ class Diaspora $public = (($item["private"]) ? "false" : "true"); - $created = Temporal::utc($item["created"], Temporal::ATOM); + $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM); // Detect a share element and do a reshare if (!$item['private'] && ($ret = self::isReshare($item["body"]))) { @@ -3670,7 +3649,7 @@ class Diaspora $body = $item["body"]; // convert to markdown - $body = html_entity_decode(bb2diaspora($body)); + $body = html_entity_decode(BBCode::toMarkdown($body)); // Adding the title if (strlen($title)) { @@ -3769,6 +3748,7 @@ class Diaspora $parent = $p[0]; $target_type = ($parent["uri"] === $parent["parent-uri"] ? "Post" : "Comment"); + $positive = null; if ($item['verb'] === ACTIVITY_LIKE) { $positive = "true"; } elseif ($item['verb'] === ACTIVITY_DISLIKE) { @@ -3854,8 +3834,8 @@ class Diaspora $parent = $p[0]; - $text = html_entity_decode(bb2diaspora($item["body"])); - $created = Temporal::utc($item["created"], Temporal::ATOM); + $text = html_entity_decode(BBCode::toMarkdown($item["body"])); + $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM); $comment = ["author" => self::myHandle($owner), "guid" => $item["guid"], @@ -4036,7 +4016,7 @@ class Diaspora */ public static function sendRetraction($item, $owner, $contact, $public_batch = false, $relay = false) { - $itemaddr = self::handleFromContact($item["contact-id"], $item["gcontact-id"]); + $itemaddr = self::handleFromContact($item["contact-id"], $item["author-id"]); $msg_type = "retraction"; @@ -4086,12 +4066,12 @@ class Diaspora "author" => $cnv["creator"], "guid" => $cnv["guid"], "subject" => $cnv["subject"], - "created_at" => Temporal::utc($cnv['created'], Temporal::ATOM), + "created_at" => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM), "participants" => $cnv["recips"] ]; - $body = bb2diaspora($item["body"]); - $created = Temporal::utc($item["created"], Temporal::ATOM); + $body = BBCode::toMarkdown($item["body"]); + $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM); $msg = [ "author" => $myaddr, @@ -4109,7 +4089,7 @@ class Diaspora "author" => $cnv["creator"], "guid" => $cnv["guid"], "subject" => $cnv["subject"], - "created_at" => Temporal::utc($cnv['created'], Temporal::ATOM), + "created_at" => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM), "participants" => $cnv["recips"], "message" => $msg]; @@ -4209,6 +4189,10 @@ class Diaspora $small = System::baseUrl().'/photo/custom/50/' .$profile['uid'].'.jpg'; $searchable = (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false'); + $dob = null; + $about = null; + $location = null; + $tags = null; if ($searchable === 'true') { $dob = ''; @@ -4217,11 +4201,11 @@ class Diaspora if ($year < 1004) { $year = 1004; } - $dob = Temporal::utc($year . '-' . $month . '-'. $day, 'Y-m-d'); + $dob = DateTimeFormat::utc($year . '-' . $month . '-'. $day, 'Y-m-d'); } $about = $profile['about']; - $about = strip_tags(bbcode($about)); + $about = strip_tags(BBCode::convert($about)); $location = Profile::formatLocation($profile); $tags = '';