* 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\Content\Text\BBCode;
+use Friendica\Content\Text\Markdown;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
use Friendica\Util\XML;
+use Friendica\Util\Map;
use dba;
use SimpleXMLElement;
require_once 'include/dba.php';
require_once 'include/items.php';
-require_once 'include/bb2diaspora.php';
/**
* @brief This class contain functions to create and send Diaspora XML files
/**
* @brief Return a list of relay servers
*
- * This is an experimental Diaspora feature.
+ * The list contains not only the official relays but also servers that we serve directly
+ *
+ * @param integer $item_id The id of the item that is sent
+ * @param array $contacts The previously fetched contacts
*
* @return array of relay servers
*/
- public static function relayList()
+ public static function relayList($item_id, $contacts = [])
{
+ $serverlist = [];
+
+ // Fetching relay servers
$serverdata = Config::get("system", "relay_server");
- if ($serverdata == "") {
- return [];
+ if ($serverdata != "") {
+ $servers = explode(",", $serverdata);
+ foreach ($servers as $server) {
+ $serverlist[$server] = trim($server);
+ }
}
- $relay = [];
+ if (Config::get("system", "relay_directly", false)) {
+ // Servers that want to get all content
+ $servers = dba::select('gserver', ['url'], ['relay-subscribe' => true, 'relay-scope' => 'all']);
+ while ($server = dba::fetch($servers)) {
+ $serverlist[$server['url']] = $server['url'];
+ }
- $servers = explode(",", $serverdata);
+ // All tags of the current post
+ $condition = ['otype' => TERM_OBJ_POST, 'type' => TERM_HASHTAG, 'oid' => $item_id];
+ $tags = dba::select('term', ['term'], $condition);
+ $taglist = [];
+ while ($tag = dba::fetch($tags)) {
+ $taglist[] = $tag['term'];
+ }
- foreach ($servers as $server) {
- $server = trim($server);
- $addr = "relay@".str_replace("http://", "", normalise_link($server));
- $batch = $server."/receive/public";
+ // All servers who wants content with this tag
+ $tagserverlist = [];
+ if (!empty($taglist)) {
+ $tagserver = dba::select('gserver-tag', ['gserver-id'], ['tag' => $taglist]);
+ while ($server = dba::fetch($tagserver)) {
+ $tagserverlist[] = $server['gserver-id'];
+ }
+ }
- $relais = q(
- "SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' AND `addr` = '%s' AND `nurl` = '%s' LIMIT 1",
- dbesc($batch),
- dbesc($addr),
- dbesc(normalise_link($server))
- );
+ // All adresses with the given id
+ if (!empty($tagserverlist)) {
+ $servers = dba::select('gserver', ['url'], ['relay-subscribe' => true, 'relay-scope' => 'tags', 'id' => $tagserverlist]);
+ while ($server = dba::fetch($servers)) {
+ $serverlist[$server['url']] = $server['url'];
+ }
+ }
+ }
- if (!$relais) {
- $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')",
- DateTimeFormat::utcNow(),
- dbesc($addr),
- dbesc($addr),
- dbesc($server),
- dbesc(normalise_link($server)),
- dbesc($batch),
- dbesc(NETWORK_DIASPORA),
- intval(CONTACT_IS_FOLLOWER),
- dbesc(DateTimeFormat::utcNow()),
- dbesc(DateTimeFormat::utcNow()),
- dbesc(DateTimeFormat::utcNow())
- );
+ // Now we are collecting all relay contacts
+ foreach ($serverlist as $server_url) {
+ // We don't send messages to ourselves
+ if (link_compare($server_url, System::baseUrl())) {
+ continue;
+ }
+ $contact = self::getRelayContact($server_url);
+ if (is_bool($contact)) {
+ continue;
+ }
- $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch));
- if ($relais) {
- $relay[] = $relais[0];
+ $exists = false;
+ foreach ($contacts as $entry) {
+ if ($entry['batch'] == $contact['batch']) {
+ $exists = true;
}
- } else {
- $relay[] = $relais[0];
+ }
+
+ if (!$exists) {
+ $contacts[] = $contact;
}
}
- return $relay;
+ return $contacts;
+ }
+
+ /**
+ * @brief Return a contact for a given server address or creates a dummy entry
+ *
+ * @param string $server_url The url of the server
+ * @return array with the contact
+ */
+ private static function getRelayContact($server_url)
+ {
+ $batch = $server_url . '/receive/public';
+
+ $fields = ['batch', 'id', 'name', 'network', 'archive', 'blocked'];
+
+ // Fetch the relay contact
+ $condition = ['uid' => 0, 'network' => NETWORK_DIASPORA, 'batch' => $batch,
+ 'contact-type' => ACCOUNT_TYPE_RELAY];
+ $contact = dba::selectFirst('contact', $fields, $condition);
+
+ // If there is nothing found, we check if there is some unmarked relay
+ // This code segment can be removed before the release 2018-05
+ if (!DBM::is_result($contact)) {
+ $condition = ['uid' => 0, 'network' => NETWORK_DIASPORA, 'batch' => $batch,
+ 'name' => 'relay', 'nick' => 'relay', 'url' => $server_url];
+ $contact = dba::selectFirst('contact', $fields, $condition);
+
+ if (DBM::is_result($contact)) {
+ // Mark the relay account as a relay account
+ $fields = ['contact-type' => ACCOUNT_TYPE_RELAY];
+ dba::update('contact', $fields, ['id' => $contact['id']]);
+ }
+ }
+ if (DBM::is_result($contact)) {
+ if ($contact['archive'] || $contact['blocked']) {
+ return false;
+ }
+ return $contact;
+ } else {
+ $fields = ['uid' => 0, 'created' => DateTimeFormat::utcNow(),
+ 'name' => 'relay', 'nick' => 'relay',
+ 'url' => $server_url, 'nurl' => normalise_link($server_url),
+ 'batch' => $batch, 'network' => NETWORK_DIASPORA,
+ 'rel' => CONTACT_IS_FOLLOWER, 'blocked' => false,
+ 'contact-type' => ACCOUNT_TYPE_RELAY,
+ 'pending' => false, 'writable' => true];
+ dba::insert('contact', $fields);
+
+ $fields = ['batch', 'id', 'name', 'network'];
+ $contact = dba::selectFirst('contact', $fields, $condition);
+ if (DBM::is_result($contact)) {
+ return $contact;
+ }
+
+ }
+
+ // It should never happen that we arrive here
+ return [];
}
/**
$signable_data = $msg.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
+ if ($handle == '') {
+ logger('No author could be decoded. Discarding. Message: ' . $envelope);
+ return false;
+ }
+
$key = self::key($handle);
+ if ($key == '') {
+ logger("Couldn't get a key for handle " . $handle . ". Discarding.");
+ return false;
+ }
$verify = Crypto::rsaVerify($signable_data, $sig, $key);
if (!$verify) {
- logger('Message did not verify. Discarding.');
+ logger('Message from ' . $handle . ' did not verify. Discarding.');
return false;
}
// Get the senders' public key
$key_id = $base->sig[0]->attributes()->key_id[0];
$author_addr = base64_decode($key_id);
+ if ($author_addr == '') {
+ logger('No author could be decoded. Discarding. Message: ' . $xml);
+ System::httpExit(400);
+ }
+
$key = self::key($author_addr);
+ if ($key == '') {
+ logger("Couldn't get a key for handle " . $author_addr . ". Discarding.");
+ System::httpExit(400);
+ }
$verify = Crypto::rsaVerify($signed_data, $signature, $key);
if (!$verify) {
}
$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);
// figure out where in the DOM tree our data is hiding
+ $base = null;
if ($dom->provenance->data) {
$base = $dom->provenance;
} elseif ($dom->env->data) {
return false;
}
- $first_child = $data->getName();
-
// Is this the new or the old version?
if ($data->getName() == "XML") {
$oldXML = true;
$fields = new SimpleXMLElement("<".$type."/>");
$signed_data = "";
+ $author_signature = null;
+ $parent_author_signature = null;
foreach ($element->children() as $fieldname => $entry) {
if ($oldXML) {
*/
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");
}
}
- if (!$person || $update) {
+ if (!DBM::is_result($person) || $update) {
logger("create or refresh", LOGGER_DEBUG);
$r = Probe::uri($handle, NETWORK_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;
}
}
/**
- * @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)) {
//}
// 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?
/**
* @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;
}
$datarray["plink"] = self::plink($author, $guid, $parent_item['guid']);
- $body = diaspora2bb($text);
+ $body = Markdown::toBBCode($text);
$datarray["body"] = self::replacePeopleGuid($body, $person["url"]);
return false;
}
- $body = diaspora2bb($msg_text);
+ $body = Markdown::toBBCode($msg_text);
$message_uri = $msg_author.":".$msg_guid;
$person = self::personByHandle($msg_author);
return false;
}
- $body = diaspora2bb($text);
+ $body = Markdown::toBBCode($text);
$body = self::replacePeopleGuid($body, $person["url"]);
}
logger('Received participation for ID: '.$item['id'].' - Contact: '.$contact_id.' - Server: '.$server, LOGGER_DEBUG);
- dba::insert('participation', ['iid' => $item['id'], 'cid' => $contact_id, 'fid' => $person['id'], 'server' => $server]);
+
+ if (!dba::exists('participation', ['iid' => $item['id'], 'server' => $server])) {
+ dba::insert('participation', ['iid' => $item['id'], 'cid' => $contact_id, 'fid' => $person['id'], 'server' => $server]);
+ }
// Send all existing comments and likes to the requesting server
$comments = dba::p("SELECT `item`.`id`, `item`.`verb`, `contact`.`self`
$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);
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"]);
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"]);
}
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' => DateTimeFormat::utcNow(),
- 'changed' => DateTimeFormat::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);
}
}
- $body = diaspora2bb($text);
+ $body = Markdown::toBBCode($text);
$datarray = [];
*
* @return string The message that will be transmitted to other servers
*/
- private static function buildMessage($msg, $user, $contact, $prvkey, $pubkey, $public = false)
+ public static function buildMessage($msg, $user, $contact, $prvkey, $pubkey, $public = false)
{
// The message is put into an envelope with the sender's signature
$envelope = self::buildMagicEnvelope($msg, $user);
*
* @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();
}
$logid = random_string(4);
+
$dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
- // Fetch the fcontact entry when there is missing data
- // Will possibly happen when data is transmitted to a DFRN contact
- if (empty($dest_url) && !empty($contact['addr'])) {
+ // We always try to use the data from the fcontact table.
+ // This is important for transmitting data to Friendica servers.
+ if (!empty($contact['addr'])) {
$fcontact = self::personByHandle($contact['addr']);
- $dest_url = ($public_batch ? $fcontact["batch"] : $fcontact["notify"]);
+ if (!empty($fcontact)) {
+ $dest_url = ($public_batch ? $fcontact["batch"] : $fcontact["notify"]);
+ }
}
if (!$dest_url) {
}
}
- logger("transmit: ".$logid."-".$guid." returns: ".$return_code);
+ logger("transmit: ".$logid."-".$guid." to ".$dest_url." 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 && ($contact['contact-type'] != ACCOUNT_TYPE_RELAY)) {
+ 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;
}
$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);
$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']) {
+ $event['location'] = preg_replace("/\[map\](.*?)\[\/map\]/ism", '$1', $event['location']);
+ $coord = Map::getCoordinates($event['location']);
+
$location = [];
- $location["address"] = html_entity_decode(bb2diaspora($event['location']));
- $location["lat"] = 0;
- $location["lng"] = 0;
+ $location["address"] = html_entity_decode(BBCode::toMarkdown($event['location']));
+ if (!empty($coord['lat']) && !empty($coord['lon'])) {
+ $location["lat"] = $coord['lat'];
+ $location["lng"] = $coord['lon'];
+ } else {
+ $location["lat"] = 0;
+ $location["lng"] = 0;
+ }
$eventdata['location'] = $location;
}
$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)) {
if (count($event)) {
$message['event'] = $event;
- /// @todo Once Diaspora supports it, we will remove the body
+ if (!empty($event['location']['address']) &&
+ !empty($event['location']['lat']) &&
+ !empty($event['location']['lng'])) {
+ $message['location'] = $event['location'];
+ }
+
+ /// @todo Once Diaspora supports it, we will remove the body and the location hack above
// $message['text'] = '';
}
}
$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) {
$parent = $p[0];
- $text = html_entity_decode(bb2diaspora($item["body"]));
+ $text = html_entity_decode(BBCode::toMarkdown($item["body"]));
$created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
$comment = ["author" => self::myHandle($owner),
*/
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";
"participants" => $cnv["recips"]
];
- $body = bb2diaspora($item["body"]);
+ $body = BBCode::toMarkdown($item["body"]);
$created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
$msg = [
$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 = '';
}
$about = $profile['about'];
- $about = strip_tags(bbcode($about));
+ $about = strip_tags(BBCode::convert($about));
$location = Profile::formatLocation($profile);
$tags = '';