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;
use Friendica\Model\Contact;
+use Friendica\Model\Conversation;
use Friendica\Model\GContact;
use Friendica\Model\Group;
use Friendica\Model\Item;
// Fetch the relay contact
$condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
- 'contact-type' => ACCOUNT_TYPE_RELAY];
+ 'contact-type' => Contact::ACCOUNT_TYPE_RELAY];
$contact = DBA::selectFirst('contact', $fields, $condition);
if (DBA::isResult($contact)) {
{
$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];
$fields = array_merge($fields, $network_fields);
$condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
- 'contact-type' => ACCOUNT_TYPE_RELAY];
+ 'contact-type' => Contact::ACCOUNT_TYPE_RELAY];
if (DBA::exists('contact', $condition)) {
unset($fields['created']);
/**
* @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);
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);
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);
$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),
return false;
}
- $importer = ["uid" => 0, "page-flags" => PAGE_FREELOVE];
+ $importer = ["uid" => 0, "page-flags" => Contact::PAGE_FREELOVE];
$success = self::dispatch($importer, $msg, $fields);
return $success;
{
$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);
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;
}
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"]);
}
}
$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)
);
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);
*/
// 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(PAGE_FREELOVE))) {
- // dba::update(
+ //if (!$is_comment && $contact["rel"] == Contact::FOLLOWER && in_array($importer["page-flags"], array(Contact::PAGE_FREELOVE))) {
+ // DBA::update(
// 'contact',
// array('rel' => Contact::FRIEND, 'writable' => true),
// array('id' => $contact["id"], 'uid' => $contact["uid"])
// Yes, then it is fine.
return true;
// Is it a post to a community?
- } elseif (($contact["rel"] == Contact::FOLLOWER) && in_array($importer["page-flags"], [PAGE_COMMUNITY, PAGE_PRVGROUP])) {
+ } elseif (($contact["rel"] == Contact::FOLLOWER) && in_array($importer["page-flags"], [Contact::PAGE_COMMUNITY, Contact::PAGE_PRVGROUP])) {
// That's good
return true;
// Is the message a global user or a comment?
$item = Item::selectFirst($fields, $condition);
if (!DBA::isResult($item)) {
- $result = self::storeByGuid($guid, $contact["url"], $uid);
+ $person = self::personByHandle($author);
+ $result = self::storeByGuid($guid, $person["url"], $uid);
- if (!$result) {
- $person = self::personByHandle($author);
- $result = self::storeByGuid($guid, $person["url"], $uid);
+ // We don't have an url for items that arrived at the public dispatcher
+ if (!$result && !empty($contact["url"])) {
+ $result = self::storeByGuid($guid, $contact["url"], $uid);
}
if ($result) {
$network = $contact["network"];
} else {
$cid = $def_contact["id"];
- $network = NETWORK_DIASPORA;
+ $network = Protocol::DIASPORA;
}
return ["cid" => $cid, "network" => $network];
}
}
- if ($contact["network"] == NETWORK_DFRN) {
+ if ($contact["network"] == Protocol::DFRN) {
return str_replace("/profile/" . $contact["nick"] . "/", "/display/" . $guid, $contact["url"] . "/");
}
// 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;
}
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 . '/object/' . $guid;
}
return "";
$datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
- $datarray["protocol"] = PROTOCOL_DIASPORA;
+ $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
$datarray["source"] = $xml;
$datarray["changed"] = $datarray["created"] = $datarray["edited"] = $created_at;
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;
}
"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"]
);
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')",
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) {
$datarray = [];
- $datarray["protocol"] = PROTOCOL_DIASPORA;
+ $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
$datarray["uid"] = $importer["uid"];
$datarray["contact-id"] = $author_contact["cid"];
$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;
}
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;
}
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,
// 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;
}
}
- if (!$following && $sharing && in_array($importer["page-flags"], [PAGE_SOAPBOX, PAGE_NORMAL])) {
+ if (!$following && $sharing && in_array($importer["page-flags"], [Contact::PAGE_SOAPBOX, Contact::PAGE_NORMAL])) {
logger("Author ".$author." wants to share with us - but doesn't want to listen. Request is ignored.", LOGGER_DEBUG);
return false;
} elseif (!$following && !$sharing) {
$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;
}
Contact::updateAvatar($ret["photo"], $importer['uid'], $contact_record["id"], true);
- if (in_array($importer["page-flags"], [PAGE_NORMAL, PAGE_PRVGROUP])) {
+ if (in_array($importer["page-flags"], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP])) {
logger("Sending intra message for author ".$author.".", LOGGER_DEBUG);
$hash = random_string().(string)time(); // Generate a confirm_key
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.
-
- if (($importer["page-flags"] == PAGE_FREELOVE) && $sharing && $following) {
+ /*
+ * 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"] == PAGE_FREELOVE) && $sharing) {
+ } elseif (($importer["page-flags"] == Contact::PAGE_FREELOVE) && $sharing) {
$new_relation = Contact::SHARING;
} else {
$new_relation = Contact::FOLLOWER;
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]);
$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);
$datarray["verb"] = ACTIVITY_POST;
$datarray["gravity"] = GRAVITY_PARENT;
- $datarray["protocol"] = PROTOCOL_DIASPORA;
+ $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
$datarray["source"] = $xml;
$prefix = share_header(
*
* @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));
$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);
$datarray["verb"] = ACTIVITY_POST;
$datarray["gravity"] = GRAVITY_PARENT;
- $datarray["protocol"] = PROTOCOL_DIASPORA;
+ $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
$datarray["source"] = $xml;
$datarray["body"] = self::replacePeopleGuid($body, $contact["url"]);
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'] != ACCOUNT_TYPE_RELAY)) {
+ 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"
$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);
}
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= [];
$myaddr = self::myHandle($owner);
- $public = (($item["private"]) ? "false" : "true");
+ $public = ($item["private"] ? "false" : "true");
$created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
if ($item["attach"]) {
$cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism', $item["attach"], $matches, PREG_SET_ORDER);
- if (cnt) {
+ if ($cnt) {
$body .= "\n".L10n::t("Attachments:")."\n";
foreach ($matches as $mtch) {
$body .= "[".$mtch[3]."](".$mtch[1].")\n";
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) {
+ $fields = ['signed_text', 'signature', 'signer'];
+ $signature = DBA::selectFirst('sign', $fields, ['iid' => $item["id"]]);
+ if (!DBA::isResult($signature)) {
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']) {
{
$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"],
$arr = explode(' ', $profile['pub_keywords']);
if (count($arr)) {
for ($x = 0; $x < 5; $x ++) {
- if (trim($arr[$x])) {
+ if (!empty($arr[$x])) {
$tags .= '#'. trim($arr[$x]) .' ';
}
}
$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)
);
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)) {