3 * @file include/diaspora.php
4 * @brief The implementation of the diaspora protocol
7 require_once("include/items.php");
8 require_once("include/bb2diaspora.php");
9 require_once("include/Scrape.php");
10 require_once("include/Contact.php");
11 require_once("include/Photo.php");
12 require_once("include/socgraph.php");
13 require_once("include/group.php");
14 require_once("include/xml.php");
15 require_once("include/datetime.php");
16 require_once("include/queue_fn.php");
19 * @brief This class contain functions to create and send Diaspora XML files
25 * @brief Return a list of relay servers
27 * This is an experimental Diaspora feature.
29 * @return array of relay servers
31 public static function relay_list() {
33 $serverdata = get_config("system", "relay_server");
34 if ($serverdata == "")
39 $servers = explode(",", $serverdata);
41 foreach($servers AS $server) {
42 $server = trim($server);
43 $batch = $server."/receive/public";
45 $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch));
48 $addr = "relay@".str_replace("http://", "", normalise_link($server));
50 $r = q("INSERT INTO `contact` (`uid`, `created`, `name`, `nick`, `addr`, `url`, `nurl`, `batch`, `network`, `rel`, `blocked`, `pending`, `writable`, `name-date`, `uri-date`, `avatar-date`)
51 VALUES (0, '%s', '%s', 'relay', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, '%s', '%s', '%s')",
56 dbesc(normalise_link($server)),
58 dbesc(NETWORK_DIASPORA),
59 intval(CONTACT_IS_FOLLOWER),
60 dbesc(datetime_convert()),
61 dbesc(datetime_convert()),
62 dbesc(datetime_convert())
65 $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch));
67 $relay[] = $relais[0];
69 $relay[] = $relais[0];
76 * @brief repairs a signature that was double encoded
78 * The function is unused at the moment. It was copied from the old implementation.
80 * @param string $signature The signature
81 * @param string $handle The handle of the signature owner
82 * @param integer $level This value is only set inside this function to avoid endless loops
84 * @return string the repaired signature
86 private function repair_signature($signature, $handle = "", $level = 1) {
91 if (base64_encode(base64_decode(base64_decode($signature))) == base64_decode($signature)) {
92 $signature = base64_decode($signature);
93 logger("Repaired double encoded signature from Diaspora/Hubzilla handle ".$handle." - level ".$level, LOGGER_DEBUG);
95 // Do a recursive call to be able to fix even multiple levels
97 $signature = self::repair_signature($signature, $handle, ++$level);
104 * @brief: Decodes incoming Diaspora message
106 * @param array $importer Array of the importer user
107 * @param string $xml urldecoded Diaspora salmon
110 * 'message' -> decoded Diaspora XML message
111 * 'author' -> author diaspora handle
112 * 'key' -> author public key (converted to pkcs#8)
114 public static function decode($importer, $xml) {
117 $basedom = parse_xml_string($xml);
119 if (!is_object($basedom))
122 $children = $basedom->children('https://joindiaspora.com/protocol');
124 if($children->header) {
126 $author_link = str_replace('acct:','',$children->header->author_id);
129 $encrypted_header = json_decode(base64_decode($children->encrypted_header));
131 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
132 $ciphertext = base64_decode($encrypted_header->ciphertext);
134 $outer_key_bundle = '';
135 openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
137 $j_outer_key_bundle = json_decode($outer_key_bundle);
139 $outer_iv = base64_decode($j_outer_key_bundle->iv);
140 $outer_key = base64_decode($j_outer_key_bundle->key);
142 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
145 $decrypted = pkcs5_unpad($decrypted);
147 logger('decrypted: '.$decrypted, LOGGER_DEBUG);
148 $idom = parse_xml_string($decrypted,false);
150 $inner_iv = base64_decode($idom->iv);
151 $inner_aes_key = base64_decode($idom->aes_key);
153 $author_link = str_replace('acct:','',$idom->author_id);
156 $dom = $basedom->children(NAMESPACE_SALMON_ME);
158 // figure out where in the DOM tree our data is hiding
160 if($dom->provenance->data)
161 $base = $dom->provenance;
162 elseif($dom->env->data)
168 logger('unable to locate salmon data in xml');
169 http_status_exit(400);
173 // Stash the signature away for now. We have to find their key or it won't be good for anything.
174 $signature = base64url_decode($base->sig);
178 // strip whitespace so our data element will return to one big base64 blob
179 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
182 // stash away some other stuff for later
184 $type = $base->data[0]->attributes()->type[0];
185 $keyhash = $base->sig[0]->attributes()->keyhash[0];
186 $encoding = $base->encoding;
190 $signed_data = $data.'.'.base64url_encode($type).'.'.base64url_encode($encoding).'.'.base64url_encode($alg);
194 $data = base64url_decode($data);
198 $inner_decrypted = $data;
201 // Decode the encrypted blob
203 $inner_encrypted = base64_decode($data);
204 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
205 $inner_decrypted = pkcs5_unpad($inner_decrypted);
209 logger('Could not retrieve author URI.');
210 http_status_exit(400);
212 // Once we have the author URI, go to the web and try to find their public key
213 // (first this will look it up locally if it is in the fcontact cache)
214 // This will also convert diaspora public key from pkcs#1 to pkcs#8
216 logger('Fetching key for '.$author_link);
217 $key = self::key($author_link);
220 logger('Could not retrieve author key.');
221 http_status_exit(400);
224 $verify = rsa_verify($signed_data,$signature,$key);
227 logger('Message did not verify. Discarding.');
228 http_status_exit(400);
231 logger('Message verified.');
233 return array('message' => (string)$inner_decrypted,
234 'author' => unxmlify($author_link),
235 'key' => (string)$key);
241 * @brief Dispatches public messages and find the fitting receivers
243 * @param array $msg The post that will be dispatched
245 * @return int The message id of the generated message, "true" or "false" if there was an error
247 public static function dispatch_public($msg) {
249 $enabled = intval(get_config("system", "diaspora_enabled"));
251 logger("diaspora is disabled");
255 // Use a dummy importer to import the data for the public copy
256 $importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE);
257 $message_id = self::dispatch($importer,$msg);
259 // Now distribute it to the followers
260 $r = q("SELECT `user`.* FROM `user` WHERE `user`.`uid` IN
261 (SELECT `contact`.`uid` FROM `contact` WHERE `contact`.`network` = '%s' AND `contact`.`addr` = '%s')
262 AND NOT `account_expired` AND NOT `account_removed`",
263 dbesc(NETWORK_DIASPORA),
264 dbesc($msg["author"])
268 logger("delivering to: ".$rr["username"]);
269 self::dispatch($rr,$msg);
272 logger("No subscribers for ".$msg["author"]." ".print_r($msg, true));
278 * @brief Dispatches the different message types to the different functions
280 * @param array $importer Array of the importer user
281 * @param array $msg The post that will be dispatched
283 * @return int The message id of the generated message, "true" or "false" if there was an error
285 public static function dispatch($importer, $msg) {
287 // The sender is the handle of the contact that sent the message.
288 // This will often be different with relayed messages (for example "like" and "comment")
289 $sender = $msg["author"];
291 if (!diaspora::valid_posting($msg, $fields)) {
292 logger("Invalid posting");
296 $type = $fields->getName();
298 logger("Received message type ".$type." from ".$sender." for user ".$importer["uid"], LOGGER_DEBUG);
301 case "account_deletion":
302 return self::receive_account_deletion($importer, $fields);
305 return self::receive_comment($importer, $sender, $fields, $msg["message"]);
308 return self::receive_contact_request($importer, $fields);
311 return self::receive_conversation($importer, $msg, $fields);
314 return self::receive_like($importer, $sender, $fields);
317 return self::receive_message($importer, $fields);
319 case "participation": // Not implemented
320 return self::receive_participation($importer, $fields);
322 case "photo": // Not implemented
323 return self::receive_photo($importer, $fields);
325 case "poll_participation": // Not implemented
326 return self::receive_poll_participation($importer, $fields);
329 return self::receive_profile($importer, $fields);
332 return self::receive_reshare($importer, $fields, $msg["message"]);
335 return self::receive_retraction($importer, $sender, $fields);
337 case "status_message":
338 return self::receive_status_message($importer, $fields, $msg["message"]);
341 logger("Unknown message type ".$type);
349 * @brief Checks if a posting is valid and fetches the data fields.
351 * This function does not only check the signature.
352 * It also does the conversion between the old and the new diaspora format.
354 * @param array $msg Array with the XML, the sender handle and the sender signature
355 * @param object $fields SimpleXML object that contains the posting when it is valid
357 * @return bool Is the posting valid?
359 private function valid_posting($msg, &$fields) {
361 $data = parse_xml_string($msg["message"], false);
363 if (!is_object($data))
366 $first_child = $data->getName();
368 // Is this the new or the old version?
369 if ($data->getName() == "XML") {
371 foreach ($data->post->children() as $child)
378 $type = $element->getName();
381 // All retractions are handled identically from now on.
382 // In the new version there will only be "retraction".
383 if (in_array($type, array("signed_retraction", "relayable_retraction")))
384 $type = "retraction";
386 if ($type == "request")
389 $fields = new SimpleXMLElement("<".$type."/>");
393 foreach ($element->children() AS $fieldname => $entry) {
395 // Translation for the old XML structure
396 if ($fieldname == "diaspora_handle")
397 $fieldname = "author";
399 if ($fieldname == "participant_handles")
400 $fieldname = "participants";
402 if (in_array($type, array("like", "participation"))) {
403 if ($fieldname == "target_type")
404 $fieldname = "parent_type";
407 if ($fieldname == "sender_handle")
408 $fieldname = "author";
410 if ($fieldname == "recipient_handle")
411 $fieldname = "recipient";
413 if ($fieldname == "root_diaspora_id")
414 $fieldname = "root_author";
416 if ($type == "retraction") {
417 if ($fieldname == "post_guid")
418 $fieldname = "target_guid";
420 if ($fieldname == "type")
421 $fieldname = "target_type";
425 if ($fieldname == "author_signature")
426 $author_signature = base64_decode($entry);
427 elseif ($fieldname == "parent_author_signature")
428 $parent_author_signature = base64_decode($entry);
429 elseif ($fieldname != "target_author_signature") {
430 if ($signed_data != "") {
432 $signed_data_parent .= ";";
435 $signed_data .= $entry;
437 if (!in_array($fieldname, array("parent_author_signature", "target_author_signature")) OR
438 ($orig_type == "relayable_retraction"))
439 xml::copy($entry, $fields, $fieldname);
442 // This is something that shouldn't happen at all.
443 if (in_array($type, array("status_message", "reshare", "profile")))
444 if ($msg["author"] != $fields->author) {
445 logger("Message handle is not the same as envelope sender. Quitting this message.");
449 // Only some message types have signatures. So we quit here for the other types.
450 if (!in_array($type, array("comment", "message", "like")))
453 // No author_signature? This is a must, so we quit.
454 if (!isset($author_signature))
457 if (isset($parent_author_signature)) {
458 $key = self::key($msg["author"]);
460 if (!rsa_verify($signed_data, $parent_author_signature, $key, "sha256"))
464 $key = self::key($fields->author);
466 return rsa_verify($signed_data, $author_signature, $key, "sha256");
470 * @brief Fetches the public key for a given handle
472 * @param string $handle The handle
474 * @return string The public key
476 private function key($handle) {
477 $handle = strval($handle);
479 logger("Fetching diaspora key for: ".$handle);
481 $r = self::person_by_handle($handle);
489 * @brief Fetches data for a given handle
491 * @param string $handle The handle
493 * @return array the queried data
495 private function person_by_handle($handle) {
497 $r = q("SELECT * FROM `fcontact` WHERE `network` = '%s' AND `addr` = '%s' LIMIT 1",
498 dbesc(NETWORK_DIASPORA),
503 logger("In cache ".print_r($r,true), LOGGER_DEBUG);
505 // update record occasionally so it doesn't get stale
506 $d = strtotime($person["updated"]." +00:00");
507 if ($d < strtotime("now - 14 days"))
511 if (!$person OR $update) {
512 logger("create or refresh", LOGGER_DEBUG);
513 $r = probe_url($handle, PROBE_DIASPORA);
515 // Note that Friendica contacts will return a "Diaspora person"
516 // if Diaspora connectivity is enabled on their server
517 if ($r AND ($r["network"] === NETWORK_DIASPORA)) {
518 self::add_fcontact($r, $update);
526 * @brief Updates the fcontact table
528 * @param array $arr The fcontact data
529 * @param bool $update Update or insert?
531 * @return string The id of the fcontact entry
533 private function add_fcontact($arr, $update = false) {
536 $r = q("UPDATE `fcontact` SET
549 WHERE `url` = '%s' AND `network` = '%s'",
551 dbesc($arr["photo"]),
552 dbesc($arr["request"]),
555 dbesc($arr["batch"]),
556 dbesc($arr["notify"]),
558 dbesc($arr["confirm"]),
559 dbesc($arr["alias"]),
560 dbesc($arr["pubkey"]),
561 dbesc(datetime_convert()),
563 dbesc($arr["network"])
566 $r = q("INSERT INTO `fcontact` (`url`,`name`,`photo`,`request`,`nick`,`addr`,
567 `batch`, `notify`,`poll`,`confirm`,`network`,`alias`,`pubkey`,`updated`)
568 VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
571 dbesc($arr["photo"]),
572 dbesc($arr["request"]),
575 dbesc($arr["batch"]),
576 dbesc($arr["notify"]),
578 dbesc($arr["confirm"]),
579 dbesc($arr["network"]),
580 dbesc($arr["alias"]),
581 dbesc($arr["pubkey"]),
582 dbesc(datetime_convert())
590 * @brief get a handle (user@domain.tld) from a given contact id or gcontact id
592 * @param int $contact_id The id in the contact table
593 * @param int $gcontact_id The id in the gcontact table
595 * @return string the handle
597 public static function handle_from_contact($contact_id, $gcontact_id = 0) {
600 logger("contact id is ".$contact_id." - gcontact id is ".$gcontact_id, LOGGER_DEBUG);
602 if ($gcontact_id != 0) {
603 $r = q("SELECT `addr` FROM `gcontact` WHERE `id` = %d AND `addr` != ''",
604 intval($gcontact_id));
606 return $r[0]["addr"];
609 $r = q("SELECT `network`, `addr`, `self`, `url`, `nick` FROM `contact` WHERE `id` = %d",
610 intval($contact_id));
614 logger("contact 'self' = ".$contact['self']." 'url' = ".$contact['url'], LOGGER_DEBUG);
616 if($contact['addr'] != "")
617 $handle = $contact['addr'];
619 $baseurl_start = strpos($contact['url'],'://') + 3;
620 $baseurl_length = strpos($contact['url'],'/profile') - $baseurl_start; // allows installations in a subdirectory--not sure how Diaspora will handle
621 $baseurl = substr($contact['url'], $baseurl_start, $baseurl_length);
622 $handle = $contact['nick'].'@'.$baseurl;
630 * @brief Get a contact id for a given handle
632 * @param int $uid The user id
633 * @param string $handle The handle in the format user@domain.tld
635 * @return The contact id
637 private function contact_by_handle($uid, $handle) {
638 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
646 $handle_parts = explode("@", $handle);
647 $nurl_sql = "%%://".$handle_parts[1]."%%/profile/".$handle_parts[0];
648 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` LIKE '%s' LIMIT 1",
660 * @brief Check if posting is allowed for this contact
662 * @param array $importer Array of the importer user
663 * @param array $contact The contact that is checked
664 * @param bool $is_comment Is the check for a comment?
666 * @return bool is the contact allowed to post?
668 private function post_allow($importer, $contact, $is_comment = false) {
670 // perhaps we were already sharing with this person. Now they're sharing with us.
671 // That makes us friends.
672 // Normally this should have handled by getting a request - but this could get lost
673 if($contact["rel"] == CONTACT_IS_FOLLOWER && in_array($importer["page-flags"], array(PAGE_FREELOVE))) {
674 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d",
675 intval(CONTACT_IS_FRIEND),
676 intval($contact["id"]),
677 intval($importer["uid"])
679 $contact["rel"] = CONTACT_IS_FRIEND;
680 logger("defining user ".$contact["nick"]." as friend");
683 if(($contact["blocked"]) || ($contact["readonly"]) || ($contact["archive"]))
685 if($contact["rel"] == CONTACT_IS_SHARING || $contact["rel"] == CONTACT_IS_FRIEND)
687 if($contact["rel"] == CONTACT_IS_FOLLOWER)
688 if(($importer["page-flags"] == PAGE_COMMUNITY) OR $is_comment)
691 // Messages for the global users are always accepted
692 if ($importer["uid"] == 0)
699 * @brief Fetches the contact id for a handle and checks if posting is allowed
701 * @param array $importer Array of the importer user
702 * @param string $handle The checked handle in the format user@domain.tld
703 * @param bool $is_comment Is the check for a comment?
705 * @return array The contact data
707 private function allowed_contact_by_handle($importer, $handle, $is_comment = false) {
708 $contact = self::contact_by_handle($importer["uid"], $handle);
710 logger("A Contact for handle ".$handle." and user ".$importer["uid"]." was not found");
714 if (!self::post_allow($importer, $contact, $is_comment)) {
715 logger("The handle: ".$handle." is not allowed to post to user ".$importer["uid"]);
722 * @brief Does the message already exists on the system?
724 * @param int $uid The user id
725 * @param string $guid The guid of the message
727 * @return int|bool message id if the message already was stored into the system - or false.
729 private function message_exists($uid, $guid) {
730 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
736 logger("message ".$guid." already exists for user ".$uid);
744 * @brief Checks for links to posts in a message
746 * @param array $item The item array
748 private function fetch_guid($item) {
749 preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
750 function ($match) use ($item){
751 return(self::fetch_guid_sub($match, $item));
756 * @brief sub function of "fetch_guid" which checks for links in messages
758 * @param array $match array containing a link that has to be checked for a message link
759 * @param array $item The item array
761 private function fetch_guid_sub($match, $item) {
762 if (!self::store_by_guid($match[1], $item["author-link"]))
763 self::store_by_guid($match[1], $item["owner-link"]);
767 * @brief Fetches an item with a given guid from a given server
769 * @param string $guid the message guid
770 * @param string $server The server address
771 * @param int $uid The user id of the user
773 * @return int the message id of the stored message or false
775 private function store_by_guid($guid, $server, $uid = 0) {
776 $serverparts = parse_url($server);
777 $server = $serverparts["scheme"]."://".$serverparts["host"];
779 logger("Trying to fetch item ".$guid." from ".$server, LOGGER_DEBUG);
781 $msg = self::message($guid, $server);
786 logger("Successfully fetched item ".$guid." from ".$server, LOGGER_DEBUG);
788 // Now call the dispatcher
789 return self::dispatch_public($msg);
793 * @brief Fetches a message from a server
795 * @param string $guid message guid
796 * @param string $server The url of the server
797 * @param int $level Endless loop prevention
800 * 'message' => The message XML
801 * 'author' => The author handle
802 * 'key' => The public key of the author
804 private function message($guid, $server, $level = 0) {
809 // This will work for Diaspora and newer Friendica servers
810 $source_url = $server."/p/".$guid.".xml";
811 $x = fetch_url($source_url);
815 $source_xml = parse_xml_string($x, false);
817 if (!is_object($source_xml))
820 if ($source_xml->post->reshare) {
821 // Reshare of a reshare - old Diaspora version
822 return self::message($source_xml->post->reshare->root_guid, $server, ++$level);
823 } elseif ($source_xml->getName() == "reshare") {
824 // Reshare of a reshare - new Diaspora version
825 return self::message($source_xml->root_guid, $server, ++$level);
830 // Fetch the author - for the old and the new Diaspora version
831 if ($source_xml->post->status_message->diaspora_handle)
832 $author = (string)$source_xml->post->status_message->diaspora_handle;
833 elseif ($source_xml->author AND ($source_xml->getName() == "status_message"))
834 $author = (string)$source_xml->author;
836 // If this isn't a "status_message" then quit
840 $msg = array("message" => $x, "author" => $author);
842 $msg["key"] = self::key($msg["author"]);
848 * @brief Fetches the item record of a given guid
850 * @param int $uid The user id
851 * @param string $guid message guid
852 * @param string $author The handle of the item
853 * @param array $contact The contact of the item owner
855 * @return array the item record
857 private function parent_item($uid, $guid, $author, $contact) {
858 $r = q("SELECT `id`, `body`, `wall`, `uri`, `private`, `origin`,
859 `author-name`, `author-link`, `author-avatar`,
860 `owner-name`, `owner-link`, `owner-avatar`
861 FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
862 intval($uid), dbesc($guid));
865 $result = self::store_by_guid($guid, $contact["url"], $uid);
868 $person = self::person_by_handle($author);
869 $result = self::store_by_guid($guid, $person["url"], $uid);
873 logger("Fetched missing item ".$guid." - result: ".$result, LOGGER_DEBUG);
875 $r = q("SELECT `id`, `body`, `wall`, `uri`, `private`, `origin`,
876 `author-name`, `author-link`, `author-avatar`,
877 `owner-name`, `owner-link`, `owner-avatar`
878 FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
879 intval($uid), dbesc($guid));
884 logger("parent item not found: parent: ".$guid." - user: ".$uid);
887 logger("parent item found: parent: ".$guid." - user: ".$uid);
893 * @brief returns contact details
895 * @param array $contact The default contact if the person isn't found
896 * @param array $person The record of the person
897 * @param int $uid The user id
900 * 'cid' => contact id
901 * 'network' => network type
903 private function author_contact_by_url($contact, $person, $uid) {
905 $r = q("SELECT `id`, `network` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1",
906 dbesc(normalise_link($person["url"])), intval($uid));
909 $network = $r[0]["network"];
911 $cid = $contact["id"];
912 $network = NETWORK_DIASPORA;
915 return (array("cid" => $cid, "network" => $network));
919 * @brief Is the profile a hubzilla profile?
921 * @param string $url The profile link
923 * @return bool is it a hubzilla server?
925 public static function is_redmatrix($url) {
926 return(strstr($url, "/channel/"));
930 * @brief Generate a post link with a given handle and message guid
932 * @param string $addr The user handle
933 * @param string $guid message guid
935 * @return string the post link
937 private function plink($addr, $guid) {
938 $r = q("SELECT `url`, `nick`, `network` FROM `fcontact` WHERE `addr`='%s' LIMIT 1", dbesc($addr));
942 return "https://".substr($addr,strpos($addr,"@")+1)."/posts/".$guid;
944 // Friendica contacts are often detected as Diaspora contacts in the "fcontact" table
945 // So we try another way as well.
946 $s = q("SELECT `network` FROM `gcontact` WHERE `nurl`='%s' LIMIT 1", dbesc(normalise_link($r[0]["url"])));
948 $r[0]["network"] = $s[0]["network"];
950 if ($r[0]["network"] == NETWORK_DFRN)
951 return(str_replace("/profile/".$r[0]["nick"]."/", "/display/".$guid, $r[0]["url"]."/"));
953 if (self::is_redmatrix($r[0]["url"]))
954 return $r[0]["url"]."/?f=&mid=".$guid;
956 return "https://".substr($addr,strpos($addr,"@")+1)."/posts/".$guid;
960 * @brief Processes an account deletion
962 * @param array $importer Array of the importer user
963 * @param object $data The message object
965 * @return bool Success
967 private function receive_account_deletion($importer, $data) {
969 /// @todo Account deletion should remove the contact from the global contacts as well
971 $author = notags(unxmlify($data->author));
973 $contact = self::contact_by_handle($importer["uid"], $author);
975 logger("cannot find contact for author: ".$author);
979 // We now remove the contact
980 contact_remove($contact["id"]);
985 * @brief Processes an incoming comment
987 * @param array $importer Array of the importer user
988 * @param string $sender The sender of the message
989 * @param object $data The message object
990 * @param string $xml The original XML of the message
992 * @return int The message id of the generated comment or "false" if there was an error
994 private function receive_comment($importer, $sender, $data, $xml) {
995 $guid = notags(unxmlify($data->guid));
996 $parent_guid = notags(unxmlify($data->parent_guid));
997 $text = unxmlify($data->text);
998 $author = notags(unxmlify($data->author));
1000 $contact = self::allowed_contact_by_handle($importer, $sender, true);
1004 $message_id = self::message_exists($importer["uid"], $guid);
1008 $parent_item = self::parent_item($importer["uid"], $parent_guid, $author, $contact);
1012 $person = self::person_by_handle($author);
1013 if (!is_array($person)) {
1014 logger("unable to find author details");
1018 // Fetch the contact id - if we know this contact
1019 $author_contact = self::author_contact_by_url($contact, $person, $importer["uid"]);
1021 $datarray = array();
1023 $datarray["uid"] = $importer["uid"];
1024 $datarray["contact-id"] = $author_contact["cid"];
1025 $datarray["network"] = $author_contact["network"];
1027 $datarray["author-name"] = $person["name"];
1028 $datarray["author-link"] = $person["url"];
1029 $datarray["author-avatar"] = ((x($person,"thumb")) ? $person["thumb"] : $person["photo"]);
1031 $datarray["owner-name"] = $contact["name"];
1032 $datarray["owner-link"] = $contact["url"];
1033 $datarray["owner-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
1035 $datarray["guid"] = $guid;
1036 $datarray["uri"] = $author.":".$guid;
1038 $datarray["type"] = "remote-comment";
1039 $datarray["verb"] = ACTIVITY_POST;
1040 $datarray["gravity"] = GRAVITY_COMMENT;
1041 $datarray["parent-uri"] = $parent_item["uri"];
1043 $datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
1044 $datarray["object"] = $xml;
1046 $datarray["body"] = diaspora2bb($text);
1048 self::fetch_guid($datarray);
1050 $message_id = item_store($datarray);
1053 logger("Stored comment ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
1055 // If we are the origin of the parent we store the original data and notify our followers
1056 if($message_id AND $parent_item["origin"]) {
1058 // Formerly we stored the signed text, the signature and the author in different fields.
1059 // We now store the raw data so that we are more flexible.
1060 q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
1061 intval($message_id),
1062 dbesc(json_encode($data))
1066 proc_run("php", "include/notifier.php", "comment-import", $message_id);
1073 * @brief processes and stores private messages
1075 * @param array $importer Array of the importer user
1076 * @param array $contact The contact of the message
1077 * @param object $data The message object
1078 * @param array $msg Array of the processed message, author handle and key
1079 * @param object $mesg The private message
1080 * @param array $conversation The conversation record to which this message belongs
1082 * @return bool "true" if it was successful
1084 private function receive_conversation_message($importer, $contact, $data, $msg, $mesg, $conversation) {
1085 $guid = notags(unxmlify($data->guid));
1086 $subject = notags(unxmlify($data->subject));
1087 $author = notags(unxmlify($data->author));
1091 $msg_guid = notags(unxmlify($mesg->guid));
1092 $msg_parent_guid = notags(unxmlify($mesg->parent_guid));
1093 $msg_parent_author_signature = notags(unxmlify($mesg->parent_author_signature));
1094 $msg_author_signature = notags(unxmlify($mesg->author_signature));
1095 $msg_text = unxmlify($mesg->text);
1096 $msg_created_at = datetime_convert("UTC", "UTC", notags(unxmlify($mesg->created_at)));
1098 // "diaspora_handle" is the element name from the old version
1099 // "author" is the element name from the new version
1101 $msg_author = notags(unxmlify($mesg->author));
1102 elseif ($mesg->diaspora_handle)
1103 $msg_author = notags(unxmlify($mesg->diaspora_handle));
1107 $msg_conversation_guid = notags(unxmlify($mesg->conversation_guid));
1109 if($msg_conversation_guid != $guid) {
1110 logger("message conversation guid does not belong to the current conversation.");
1114 $body = diaspora2bb($msg_text);
1115 $message_uri = $msg_author.":".$msg_guid;
1117 $author_signed_data = $msg_guid.";".$msg_parent_guid.";".$msg_text.";".unxmlify($mesg->created_at).";".$msg_author.";".$msg_conversation_guid;
1119 $author_signature = base64_decode($msg_author_signature);
1121 if(strcasecmp($msg_author,$msg["author"]) == 0) {
1125 $person = self::person_by_handle($msg_author);
1127 if (is_array($person) && x($person, "pubkey"))
1128 $key = $person["pubkey"];
1130 logger("unable to find author details");
1135 if (!rsa_verify($author_signed_data, $author_signature, $key, "sha256")) {
1136 logger("verification failed.");
1140 if($msg_parent_author_signature) {
1141 $owner_signed_data = $msg_guid.";".$msg_parent_guid.";".$msg_text.";".unxmlify($mesg->created_at).";".$msg_author.";".$msg_conversation_guid;
1143 $parent_author_signature = base64_decode($msg_parent_author_signature);
1147 if (!rsa_verify($owner_signed_data, $parent_author_signature, $key, "sha256")) {
1148 logger("owner verification failed.");
1153 $r = q("SELECT `id` FROM `mail` WHERE `uri` = '%s' LIMIT 1",
1157 logger("duplicate message already delivered.", LOGGER_DEBUG);
1161 q("INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
1162 VALUES (%d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
1163 intval($importer["uid"]),
1165 intval($conversation["id"]),
1166 dbesc($person["name"]),
1167 dbesc($person["photo"]),
1168 dbesc($person["url"]),
1169 intval($contact["id"]),
1174 dbesc($message_uri),
1175 dbesc($author.":".$guid),
1176 dbesc($msg_created_at)
1179 q("UPDATE `conv` SET `updated` = '%s' WHERE `id` = %d",
1180 dbesc(datetime_convert()),
1181 intval($conversation["id"])
1185 "type" => NOTIFY_MAIL,
1186 "notify_flags" => $importer["notify-flags"],
1187 "language" => $importer["language"],
1188 "to_name" => $importer["username"],
1189 "to_email" => $importer["email"],
1190 "uid" =>$importer["uid"],
1191 "item" => array("subject" => $subject, "body" => $body),
1192 "source_name" => $person["name"],
1193 "source_link" => $person["url"],
1194 "source_photo" => $person["thumb"],
1195 "verb" => ACTIVITY_POST,
1202 * @brief Processes new private messages (answers to private messages are processed elsewhere)
1204 * @param array $importer Array of the importer user
1205 * @param array $msg Array of the processed message, author handle and key
1206 * @param object $data The message object
1208 * @return bool Success
1210 private function receive_conversation($importer, $msg, $data) {
1211 $guid = notags(unxmlify($data->guid));
1212 $subject = notags(unxmlify($data->subject));
1213 $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
1214 $author = notags(unxmlify($data->author));
1215 $participants = notags(unxmlify($data->participants));
1217 $messages = $data->message;
1219 if (!count($messages)) {
1220 logger("empty conversation");
1224 $contact = self::allowed_contact_by_handle($importer, $msg["author"], true);
1228 $conversation = null;
1230 $c = q("SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1231 intval($importer["uid"]),
1235 $conversation = $c[0];
1237 $r = q("INSERT INTO `conv` (`uid`, `guid`, `creator`, `created`, `updated`, `subject`, `recips`)
1238 VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s')",
1239 intval($importer["uid"]),
1242 dbesc(datetime_convert("UTC", "UTC", $created_at)),
1243 dbesc(datetime_convert()),
1245 dbesc($participants)
1248 $c = q("SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1249 intval($importer["uid"]),
1254 $conversation = $c[0];
1256 if (!$conversation) {
1257 logger("unable to create conversation.");
1261 foreach($messages as $mesg)
1262 self::receive_conversation_message($importer, $contact, $data, $msg, $mesg, $conversation);
1268 * @brief Creates the body for a "like" message
1270 * @param array $contact The contact that send us the "like"
1271 * @param array $parent_item The item array of the parent item
1272 * @param string $guid message guid
1274 * @return string the body
1276 private function construct_like_body($contact, $parent_item, $guid) {
1277 $bodyverb = t('%1$s likes %2$s\'s %3$s');
1279 $ulink = "[url=".$contact["url"]."]".$contact["name"]."[/url]";
1280 $alink = "[url=".$parent_item["author-link"]."]".$parent_item["author-name"]."[/url]";
1281 $plink = "[url=".App::get_baseurl()."/display/".urlencode($guid)."]".t("status")."[/url]";
1283 return sprintf($bodyverb, $ulink, $alink, $plink);
1287 * @brief Creates a XML object for a "like"
1289 * @param array $importer Array of the importer user
1290 * @param array $parent_item The item array of the parent item
1292 * @return string The XML
1294 private function construct_like_object($importer, $parent_item) {
1295 $objtype = ACTIVITY_OBJ_NOTE;
1296 $link = '<link rel="alternate" type="text/html" href="'.App::get_baseurl()."/display/".$importer["nickname"]."/".$parent_item["id"].'" />';
1297 $parent_body = $parent_item["body"];
1299 $xmldata = array("object" => array("type" => $objtype,
1301 "id" => $parent_item["uri"],
1304 "content" => $parent_body));
1306 return xml::from_array($xmldata, $xml, true);
1310 * @brief Processes "like" messages
1312 * @param array $importer Array of the importer user
1313 * @param string $sender The sender of the message
1314 * @param object $data The message object
1316 * @return int The message id of the generated like or "false" if there was an error
1318 private function receive_like($importer, $sender, $data) {
1319 $positive = notags(unxmlify($data->positive));
1320 $guid = notags(unxmlify($data->guid));
1321 $parent_type = notags(unxmlify($data->parent_type));
1322 $parent_guid = notags(unxmlify($data->parent_guid));
1323 $author = notags(unxmlify($data->author));
1325 // likes on comments aren't supported by Diaspora - only on posts
1326 // But maybe this will be supported in the future, so we will accept it.
1327 if (!in_array($parent_type, array("Post", "Comment")))
1330 $contact = self::allowed_contact_by_handle($importer, $sender, true);
1334 $message_id = self::message_exists($importer["uid"], $guid);
1338 $parent_item = self::parent_item($importer["uid"], $parent_guid, $author, $contact);
1342 $person = self::person_by_handle($author);
1343 if (!is_array($person)) {
1344 logger("unable to find author details");
1348 // Fetch the contact id - if we know this contact
1349 $author_contact = self::author_contact_by_url($contact, $person, $importer["uid"]);
1351 // "positive" = "false" would be a Dislike - wich isn't currently supported by Diaspora
1352 // We would accept this anyhow.
1353 if ($positive == "true")
1354 $verb = ACTIVITY_LIKE;
1356 $verb = ACTIVITY_DISLIKE;
1358 $datarray = array();
1360 $datarray["uid"] = $importer["uid"];
1361 $datarray["contact-id"] = $author_contact["cid"];
1362 $datarray["network"] = $author_contact["network"];
1364 $datarray["author-name"] = $person["name"];
1365 $datarray["author-link"] = $person["url"];
1366 $datarray["author-avatar"] = ((x($person,"thumb")) ? $person["thumb"] : $person["photo"]);
1368 $datarray["owner-name"] = $contact["name"];
1369 $datarray["owner-link"] = $contact["url"];
1370 $datarray["owner-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
1372 $datarray["guid"] = $guid;
1373 $datarray["uri"] = $author.":".$guid;
1375 $datarray["type"] = "activity";
1376 $datarray["verb"] = $verb;
1377 $datarray["gravity"] = GRAVITY_LIKE;
1378 $datarray["parent-uri"] = $parent_item["uri"];
1380 $datarray["object-type"] = ACTIVITY_OBJ_NOTE;
1381 $datarray["object"] = self::construct_like_object($importer, $parent_item);
1383 $datarray["body"] = self::construct_like_body($contact, $parent_item, $guid);
1385 $message_id = item_store($datarray);
1388 logger("Stored like ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
1390 // If we are the origin of the parent we store the original data and notify our followers
1391 if($message_id AND $parent_item["origin"]) {
1393 // Formerly we stored the signed text, the signature and the author in different fields.
1394 // We now store the raw data so that we are more flexible.
1395 q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
1396 intval($message_id),
1397 dbesc(json_encode($data))
1401 proc_run("php", "include/notifier.php", "comment-import", $message_id);
1408 * @brief Processes private messages
1410 * @param array $importer Array of the importer user
1411 * @param object $data The message object
1413 * @return bool Success?
1415 private function receive_message($importer, $data) {
1416 $guid = notags(unxmlify($data->guid));
1417 $parent_guid = notags(unxmlify($data->parent_guid));
1418 $text = unxmlify($data->text);
1419 $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
1420 $author = notags(unxmlify($data->author));
1421 $conversation_guid = notags(unxmlify($data->conversation_guid));
1423 $contact = self::allowed_contact_by_handle($importer, $author, true);
1427 $conversation = null;
1429 $c = q("SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1430 intval($importer["uid"]),
1431 dbesc($conversation_guid)
1434 $conversation = $c[0];
1436 logger("conversation not available.");
1442 $body = diaspora2bb($text);
1443 $message_uri = $author.":".$guid;
1445 $person = self::person_by_handle($author);
1447 logger("unable to find author details");
1451 $r = q("SELECT `id` FROM `mail` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1452 dbesc($message_uri),
1453 intval($importer["uid"])
1456 logger("duplicate message already delivered.", LOGGER_DEBUG);
1460 q("INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
1461 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
1462 intval($importer["uid"]),
1464 intval($conversation["id"]),
1465 dbesc($person["name"]),
1466 dbesc($person["photo"]),
1467 dbesc($person["url"]),
1468 intval($contact["id"]),
1469 dbesc($conversation["subject"]),
1473 dbesc($message_uri),
1474 dbesc($author.":".$parent_guid),
1478 q("UPDATE `conv` SET `updated` = '%s' WHERE `id` = %d",
1479 dbesc(datetime_convert()),
1480 intval($conversation["id"])
1487 * @brief Processes participations - unsupported by now
1489 * @param array $importer Array of the importer user
1490 * @param object $data The message object
1492 * @return bool always true
1494 private function receive_participation($importer, $data) {
1495 // I'm not sure if we can fully support this message type
1500 * @brief Processes photos - unneeded
1502 * @param array $importer Array of the importer user
1503 * @param object $data The message object
1505 * @return bool always true
1507 private function receive_photo($importer, $data) {
1508 // There doesn't seem to be a reason for this function, since the photo data is transmitted in the status message as well
1513 * @brief Processes poll participations - unssupported
1515 * @param array $importer Array of the importer user
1516 * @param object $data The message object
1518 * @return bool always true
1520 private function receive_poll_participation($importer, $data) {
1521 // We don't support polls by now
1526 * @brief Processes incoming profile updates
1528 * @param array $importer Array of the importer user
1529 * @param object $data The message object
1531 * @return bool Success
1533 private function receive_profile($importer, $data) {
1534 $author = notags(unxmlify($data->author));
1536 $contact = self::contact_by_handle($importer["uid"], $author);
1540 $name = unxmlify($data->first_name).((strlen($data->last_name)) ? " ".unxmlify($data->last_name) : "");
1541 $image_url = unxmlify($data->image_url);
1542 $birthday = unxmlify($data->birthday);
1543 $location = diaspora2bb(unxmlify($data->location));
1544 $about = diaspora2bb(unxmlify($data->bio));
1545 $gender = unxmlify($data->gender);
1546 $searchable = (unxmlify($data->searchable) == "true");
1547 $nsfw = (unxmlify($data->nsfw) == "true");
1548 $tags = unxmlify($data->tag_string);
1550 $tags = explode("#", $tags);
1552 $keywords = array();
1553 foreach ($tags as $tag) {
1554 $tag = trim(strtolower($tag));
1559 $keywords = implode(", ", $keywords);
1561 $handle_parts = explode("@", $author);
1562 $nick = $handle_parts[0];
1565 $name = $handle_parts[0];
1567 if( preg_match("|^https?://|", $image_url) === 0)
1568 $image_url = "http://".$handle_parts[1].$image_url;
1570 update_contact_avatar($image_url, $importer["uid"], $contact["id"]);
1572 // Generic birthday. We don't know the timezone. The year is irrelevant.
1574 $birthday = str_replace("1000", "1901", $birthday);
1576 if ($birthday != "")
1577 $birthday = datetime_convert("UTC", "UTC", $birthday, "Y-m-d");
1579 // this is to prevent multiple birthday notifications in a single year
1580 // if we already have a stored birthday and the 'm-d' part hasn't changed, preserve the entry, which will preserve the notify year
1582 if(substr($birthday,5) === substr($contact["bd"],5))
1583 $birthday = $contact["bd"];
1585 $r = q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `name-date` = '%s', `bd` = '%s',
1586 `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d AND `uid` = %d",
1590 dbesc(datetime_convert()),
1596 intval($contact["id"]),
1597 intval($importer["uid"])
1601 poco_check($contact["url"], $name, NETWORK_DIASPORA, $image_url, $about, $location, $gender, $keywords, "",
1602 datetime_convert(), 2, $contact["id"], $importer["uid"]);
1605 $gcontact = array("url" => $contact["url"], "network" => NETWORK_DIASPORA, "generation" => 2,
1606 "photo" => $image_url, "name" => $name, "location" => $location,
1607 "about" => $about, "birthday" => $birthday, "gender" => $gender,
1608 "addr" => $author, "nick" => $nick, "keywords" => $keywords,
1609 "hide" => !$searchable, "nsfw" => $nsfw);
1611 update_gcontact($gcontact);
1613 logger("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], LOGGER_DEBUG);
1619 * @brief Processes incoming friend requests
1621 * @param array $importer Array of the importer user
1622 * @param array $contact The contact that send the request
1624 private function receive_request_make_friend($importer, $contact) {
1628 if($contact["rel"] == CONTACT_IS_FOLLOWER && in_array($importer["page-flags"], array(PAGE_FREELOVE))) {
1629 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d",
1630 intval(CONTACT_IS_FRIEND),
1631 intval($contact["id"]),
1632 intval($importer["uid"])
1635 // send notification
1637 $r = q("SELECT `hide-friends` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
1638 intval($importer["uid"])
1641 if($r && !$r[0]["hide-friends"] && !$contact["hidden"] && intval(get_pconfig($importer["uid"], "system", "post_newfriend"))) {
1643 $self = q("SELECT * FROM `contact` WHERE `self` AND `uid` = %d LIMIT 1",
1644 intval($importer["uid"])
1647 // they are not CONTACT_IS_FOLLOWER anymore but that's what we have in the array
1649 if($self && $contact["rel"] == CONTACT_IS_FOLLOWER) {
1652 $arr["uri"] = $arr["parent-uri"] = item_new_uri($a->get_hostname(), $importer["uid"]);
1653 $arr["uid"] = $importer["uid"];
1654 $arr["contact-id"] = $self[0]["id"];
1656 $arr["type"] = 'wall';
1657 $arr["gravity"] = 0;
1659 $arr["author-name"] = $arr["owner-name"] = $self[0]["name"];
1660 $arr["author-link"] = $arr["owner-link"] = $self[0]["url"];
1661 $arr["author-avatar"] = $arr["owner-avatar"] = $self[0]["thumb"];
1662 $arr["verb"] = ACTIVITY_FRIEND;
1663 $arr["object-type"] = ACTIVITY_OBJ_PERSON;
1665 $A = "[url=".$self[0]["url"]."]".$self[0]["name"]."[/url]";
1666 $B = "[url=".$contact["url"]."]".$contact["name"]."[/url]";
1667 $BPhoto = "[url=".$contact["url"]."][img]".$contact["thumb"]."[/img][/url]";
1668 $arr["body"] = sprintf(t("%1$s is now friends with %2$s"), $A, $B)."\n\n\n".$Bphoto;
1670 $arr["object"] = self::construct_new_friend_object($contact);
1672 $arr["last-child"] = 1;
1674 $arr["allow_cid"] = $user[0]["allow_cid"];
1675 $arr["allow_gid"] = $user[0]["allow_gid"];
1676 $arr["deny_cid"] = $user[0]["deny_cid"];
1677 $arr["deny_gid"] = $user[0]["deny_gid"];
1679 $i = item_store($arr);
1681 proc_run("php", "include/notifier.php", "activity", $i);
1687 * @brief Creates a XML object for a "new friend" message
1689 * @param array $contact Array of the contact
1691 * @return string The XML
1693 private function construct_new_friend_object($contact) {
1694 $objtype = ACTIVITY_OBJ_PERSON;
1695 $link = '<link rel="alternate" type="text/html" href="'.$contact["url"].'" />'."\n".
1696 '<link rel="photo" type="image/jpeg" href="'.$contact["thumb"].'" />'."\n";
1698 $xmldata = array("object" => array("type" => $objtype,
1699 "title" => $contact["name"],
1700 "id" => $contact["url"]."/".$contact["name"],
1703 return xml::from_array($xmldata, $xml, true);
1707 * @brief Processes incoming sharing notification
1709 * @param array $importer Array of the importer user
1710 * @param object $data The message object
1712 * @return bool Success
1714 private function receive_contact_request($importer, $data) {
1715 $author = unxmlify($data->author);
1716 $recipient = unxmlify($data->recipient);
1718 if (!$author || !$recipient)
1721 // the current protocol version doesn't know these fields
1722 // That means that we will assume their existance
1723 if (isset($data->following))
1724 $following = (unxmlify($data->following) == "true");
1728 if (isset($data->sharing))
1729 $sharing = (unxmlify($data->sharing) == "true");
1733 $contact = self::contact_by_handle($importer["uid"],$author);
1735 // perhaps we were already sharing with this person. Now they're sharing with us.
1736 // That makes us friends.
1738 if ($following AND $sharing) {
1739 self::receive_request_make_friend($importer, $contact);
1741 } else /// @todo Handle all possible variations of adding and retracting of permissions
1745 if (!$following AND $sharing AND in_array($importer["page-flags"], array(PAGE_SOAPBOX, PAGE_NORMAL))) {
1746 logger("Author ".$author." wants to share with us - but doesn't want to listen. Request is ignored.", LOGGER_DEBUG);
1748 } elseif (!$following AND !$sharing) {
1749 logger("Author ".$author." doesn't want anything - and we don't know the author. Request is ignored.", LOGGER_DEBUG);
1753 $ret = self::person_by_handle($author);
1755 if (!$ret || ($ret["network"] != NETWORK_DIASPORA)) {
1756 logger("Cannot resolve diaspora handle ".$author." for ".$recipient);
1760 $batch = (($ret["batch"]) ? $ret["batch"] : implode("/", array_slice(explode("/", $ret["url"]), 0, 3))."/receive/public");
1762 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`nurl`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
1763 VALUES (%d, '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d)",
1764 intval($importer["uid"]),
1765 dbesc($ret["network"]),
1766 dbesc($ret["addr"]),
1769 dbesc(normalise_link($ret["url"])),
1771 dbesc($ret["name"]),
1772 dbesc($ret["nick"]),
1773 dbesc($ret["photo"]),
1774 dbesc($ret["pubkey"]),
1775 dbesc($ret["notify"]),
1776 dbesc($ret["poll"]),
1781 // find the contact record we just created
1783 $contact_record = self::contact_by_handle($importer["uid"],$author);
1785 if (!$contact_record) {
1786 logger("unable to locate newly created contact record.");
1790 $def_gid = get_default_group($importer['uid'], $ret["network"]);
1792 if(intval($def_gid))
1793 group_add_member($importer["uid"], "", $contact_record["id"], $def_gid);
1795 if($importer["page-flags"] == PAGE_NORMAL) {
1797 $hash = random_string().(string)time(); // Generate a confirm_key
1799 $ret = q("INSERT INTO `intro` (`uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
1800 VALUES (%d, %d, %d, %d, '%s', '%s', '%s')",
1801 intval($importer["uid"]),
1802 intval($contact_record["id"]),
1805 dbesc(t("Sharing notification from Diaspora network")),
1807 dbesc(datetime_convert())
1811 // automatic friend approval
1813 update_contact_avatar($contact_record["photo"],$importer["uid"],$contact_record["id"]);
1815 // technically they are sharing with us (CONTACT_IS_SHARING),
1816 // but if our page-type is PAGE_COMMUNITY or PAGE_SOAPBOX
1817 // we are going to change the relationship and make them a follower.
1819 if (($importer["page-flags"] == PAGE_FREELOVE) AND $sharing AND $following)
1820 $new_relation = CONTACT_IS_FRIEND;
1821 elseif (($importer["page-flags"] == PAGE_FREELOVE) AND $sharing)
1822 $new_relation = CONTACT_IS_SHARING;
1824 $new_relation = CONTACT_IS_FOLLOWER;
1826 $r = q("UPDATE `contact` SET `rel` = %d,
1834 intval($new_relation),
1835 dbesc(datetime_convert()),
1836 dbesc(datetime_convert()),
1837 intval($contact_record["id"])
1840 $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"]));
1842 $ret = self::send_share($u[0], $contact_record);
1849 * @brief Fetches a message with a given guid
1851 * @param string $guid message guid
1852 * @param string $orig_author handle of the original post
1853 * @param string $author handle of the sharer
1855 * @return array The fetched item
1857 private function original_item($guid, $orig_author, $author) {
1859 // Do we already have this item?
1860 $r = q("SELECT `body`, `tag`, `app`, `created`, `object-type`, `uri`, `guid`,
1861 `author-name`, `author-link`, `author-avatar`
1862 FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1",
1866 logger("reshared message ".$guid." already exists on system.");
1868 // Maybe it is already a reshared item?
1869 // Then refetch the content, if it is a reshare from a reshare.
1870 // If it is a reshared post from another network then reformat to avoid display problems with two share elements
1871 if (self::is_reshare($r[0]["body"], true))
1873 elseif (self::is_reshare($r[0]["body"], false)) {
1874 $r[0]["body"] = diaspora2bb(bb2diaspora($r[0]["body"]));
1876 // Add OEmbed and other information to the body
1877 $r[0]["body"] = add_page_info_to_body($r[0]["body"], false, true);
1885 $server = "https://".substr($orig_author, strpos($orig_author, "@") + 1);
1886 logger("1st try: reshared message ".$guid." will be fetched from original server: ".$server);
1887 $item_id = self::store_by_guid($guid, $server);
1890 $server = "http://".substr($orig_author, strpos($orig_author, "@") + 1);
1891 logger("2nd try: reshared message ".$guid." will be fetched from original server: ".$server);
1892 $item_id = self::store_by_guid($guid, $server);
1895 // Deactivated by now since there is a risk that someone could manipulate postings through this method
1897 $server = "https://".substr($author, strpos($author, "@") + 1);
1898 logger("3rd try: reshared message ".$guid." will be fetched from sharer's server: ".$server);
1899 $item_id = self::store_by_guid($guid, $server);
1902 $server = "http://".substr($author, strpos($author, "@") + 1);
1903 logger("4th try: reshared message ".$guid." will be fetched from sharer's server: ".$server);
1904 $item_id = self::store_by_guid($guid, $server);
1908 $r = q("SELECT `body`, `tag`, `app`, `created`, `object-type`, `uri`, `guid`,
1909 `author-name`, `author-link`, `author-avatar`
1910 FROM `item` WHERE `id` = %d AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1",
1914 // If it is a reshared post from another network then reformat to avoid display problems with two share elements
1915 if (self::is_reshare($r[0]["body"], false))
1916 $r[0]["body"] = diaspora2bb(bb2diaspora($r[0]["body"]));
1927 * @brief Processes a reshare message
1929 * @param array $importer Array of the importer user
1930 * @param object $data The message object
1931 * @param string $xml The original XML of the message
1933 * @return int the message id
1935 private function receive_reshare($importer, $data, $xml) {
1936 $root_author = notags(unxmlify($data->root_author));
1937 $root_guid = notags(unxmlify($data->root_guid));
1938 $guid = notags(unxmlify($data->guid));
1939 $author = notags(unxmlify($data->author));
1940 $public = notags(unxmlify($data->public));
1941 $created_at = notags(unxmlify($data->created_at));
1943 $contact = self::allowed_contact_by_handle($importer, $author, false);
1947 $message_id = self::message_exists($importer["uid"], $guid);
1951 $original_item = self::original_item($root_guid, $root_author, $author);
1952 if (!$original_item)
1955 $orig_url = App::get_baseurl()."/display/".$original_item["guid"];
1957 $datarray = array();
1959 $datarray["uid"] = $importer["uid"];
1960 $datarray["contact-id"] = $contact["id"];
1961 $datarray["network"] = NETWORK_DIASPORA;
1963 $datarray["author-name"] = $contact["name"];
1964 $datarray["author-link"] = $contact["url"];
1965 $datarray["author-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
1967 $datarray["owner-name"] = $datarray["author-name"];
1968 $datarray["owner-link"] = $datarray["author-link"];
1969 $datarray["owner-avatar"] = $datarray["author-avatar"];
1971 $datarray["guid"] = $guid;
1972 $datarray["uri"] = $datarray["parent-uri"] = $author.":".$guid;
1974 $datarray["verb"] = ACTIVITY_POST;
1975 $datarray["gravity"] = GRAVITY_PARENT;
1977 $datarray["object"] = $xml;
1979 $prefix = share_header($original_item["author-name"], $original_item["author-link"], $original_item["author-avatar"],
1980 $original_item["guid"], $original_item["created"], $orig_url);
1981 $datarray["body"] = $prefix.$original_item["body"]."[/share]";
1983 $datarray["tag"] = $original_item["tag"];
1984 $datarray["app"] = $original_item["app"];
1986 $datarray["plink"] = self::plink($author, $guid);
1987 $datarray["private"] = (($public == "false") ? 1 : 0);
1988 $datarray["changed"] = $datarray["created"] = $datarray["edited"] = datetime_convert("UTC", "UTC", $created_at);
1990 $datarray["object-type"] = $original_item["object-type"];
1992 self::fetch_guid($datarray);
1993 $message_id = item_store($datarray);
1996 logger("Stored reshare ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
2002 * @brief Processes retractions
2004 * @param array $importer Array of the importer user
2005 * @param array $contact The contact of the item owner
2006 * @param object $data The message object
2008 * @return bool success
2010 private function item_retraction($importer, $contact, $data) {
2011 $target_type = notags(unxmlify($data->target_type));
2012 $target_guid = notags(unxmlify($data->target_guid));
2013 $author = notags(unxmlify($data->author));
2015 $person = self::person_by_handle($author);
2016 if (!is_array($person)) {
2017 logger("unable to find author detail for ".$author);
2021 $r = q("SELECT `id`, `parent`, `parent-uri`, `author-link` FROM `item` WHERE `guid` = '%s' AND `uid` = %d AND NOT `file` LIKE '%%[%%' LIMIT 1",
2022 dbesc($target_guid),
2023 intval($importer["uid"])
2028 // Only delete it if the author really fits
2029 if (!link_compare($r[0]["author-link"], $person["url"])) {
2030 logger("Item author ".$r[0]["author-link"]." doesn't fit to expected contact ".$person["url"], LOGGER_DEBUG);
2034 // Check if the sender is the thread owner
2035 $p = q("SELECT `id`, `author-link`, `origin` FROM `item` WHERE `id` = %d",
2036 intval($r[0]["parent"]));
2038 // Only delete it if the parent author really fits
2039 if (!link_compare($p[0]["author-link"], $contact["url"]) AND !link_compare($r[0]["author-link"], $contact["url"])) {
2040 logger("Thread author ".$p[0]["author-link"]." and item author ".$r[0]["author-link"]." don't fit to expected contact ".$contact["url"], LOGGER_DEBUG);
2044 // 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
2045 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = '' WHERE `id` = %d",
2046 dbesc(datetime_convert()),
2047 dbesc(datetime_convert()),
2050 delete_thread($r[0]["id"], $r[0]["parent-uri"]);
2052 logger("Deleted target ".$target_guid." (".$r[0]["id"].") from user ".$importer["uid"]." parent: ".$p[0]["id"], LOGGER_DEBUG);
2054 // Now check if the retraction needs to be relayed by us
2055 if($p[0]["origin"]) {
2057 proc_run("php", "include/notifier.php", "drop", $r[0]["id"]);
2064 * @brief Receives retraction messages
2066 * @param array $importer Array of the importer user
2067 * @param string $sender The sender of the message
2068 * @param object $data The message object
2070 * @return bool Success
2072 private function receive_retraction($importer, $sender, $data) {
2073 $target_type = notags(unxmlify($data->target_type));
2075 $contact = self::contact_by_handle($importer["uid"], $sender);
2077 logger("cannot find contact for sender: ".$sender." and user ".$importer["uid"]);
2081 logger("Got retraction for ".$target_type.", sender ".$sender." and user ".$importer["uid"], LOGGER_DEBUG);
2083 switch ($target_type) {
2086 case "Post": // "Post" will be supported in a future version
2088 case "StatusMessage":
2089 return self::item_retraction($importer, $contact, $data);;
2093 /// @todo What should we do with an "unshare"?
2094 // Removing the contact isn't correct since we still can read the public items
2095 contact_remove($contact["id"]);
2099 logger("Unknown target type ".$target_type);
2106 * @brief Receives status messages
2108 * @param array $importer Array of the importer user
2109 * @param object $data The message object
2110 * @param string $xml The original XML of the message
2112 * @return int The message id of the newly created item
2114 private function receive_status_message($importer, $data, $xml) {
2116 $raw_message = unxmlify($data->raw_message);
2117 $guid = notags(unxmlify($data->guid));
2118 $author = notags(unxmlify($data->author));
2119 $public = notags(unxmlify($data->public));
2120 $created_at = notags(unxmlify($data->created_at));
2121 $provider_display_name = notags(unxmlify($data->provider_display_name));
2123 /// @todo enable support for polls
2124 //if ($data->poll) {
2125 // foreach ($data->poll AS $poll)
2129 $contact = self::allowed_contact_by_handle($importer, $author, false);
2133 $message_id = self::message_exists($importer["uid"], $guid);
2138 if ($data->location)
2139 foreach ($data->location->children() AS $fieldname => $data)
2140 $address[$fieldname] = notags(unxmlify($data));
2142 $body = diaspora2bb($raw_message);
2144 $datarray = array();
2146 // Attach embedded pictures to the body
2148 foreach ($data->photo AS $photo)
2149 $body = "[img]".unxmlify($photo->remote_photo_path).
2150 unxmlify($photo->remote_photo_name)."[/img]\n".$body;
2152 $datarray["object-type"] = ACTIVITY_OBJ_PHOTO;
2154 $datarray["object-type"] = ACTIVITY_OBJ_NOTE;
2156 // Add OEmbed and other information to the body
2157 if (!self::is_redmatrix($contact["url"]))
2158 $body = add_page_info_to_body($body, false, true);
2161 $datarray["uid"] = $importer["uid"];
2162 $datarray["contact-id"] = $contact["id"];
2163 $datarray["network"] = NETWORK_DIASPORA;
2165 $datarray["author-name"] = $contact["name"];
2166 $datarray["author-link"] = $contact["url"];
2167 $datarray["author-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
2169 $datarray["owner-name"] = $datarray["author-name"];
2170 $datarray["owner-link"] = $datarray["author-link"];
2171 $datarray["owner-avatar"] = $datarray["author-avatar"];
2173 $datarray["guid"] = $guid;
2174 $datarray["uri"] = $datarray["parent-uri"] = $author.":".$guid;
2176 $datarray["verb"] = ACTIVITY_POST;
2177 $datarray["gravity"] = GRAVITY_PARENT;
2179 $datarray["object"] = $xml;
2181 $datarray["body"] = $body;
2183 if ($provider_display_name != "")
2184 $datarray["app"] = $provider_display_name;
2186 $datarray["plink"] = self::plink($author, $guid);
2187 $datarray["private"] = (($public == "false") ? 1 : 0);
2188 $datarray["changed"] = $datarray["created"] = $datarray["edited"] = datetime_convert("UTC", "UTC", $created_at);
2190 if (isset($address["address"]))
2191 $datarray["location"] = $address["address"];
2193 if (isset($address["lat"]) AND isset($address["lng"]))
2194 $datarray["coord"] = $address["lat"]." ".$address["lng"];
2196 self::fetch_guid($datarray);
2197 $message_id = item_store($datarray);
2200 logger("Stored item ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
2205 /* ************************************************************************************** *
2206 * Here are all the functions that are needed to transmit data with the Diaspora protocol *
2207 * ************************************************************************************** */
2210 * @brief returnes the handle of a contact
2212 * @param array $me contact array
2214 * @return string the handle in the format user@domain.tld
2216 private function my_handle($contact) {
2217 if ($contact["addr"] != "")
2218 return $contact["addr"];
2220 // Normally we should have a filled "addr" field - but in the past this wasn't the case
2221 // So - just in case - we build the the address here.
2222 if ($contact["nickname"] != "")
2223 $nick = $contact["nickname"];
2225 $nick = $contact["nick"];
2227 return $nick."@".substr(App::get_baseurl(), strpos(App::get_baseurl(),"://") + 3);
2231 * @brief Creates the envelope for a public message
2233 * @param string $msg The message that is to be transmitted
2234 * @param array $user The record of the sender
2235 * @param array $contact Target of the communication
2236 * @param string $prvkey The private key of the sender
2237 * @param string $pubkey The public key of the receiver
2239 * @return string The envelope
2241 private function build_public_message($msg, $user, $contact, $prvkey, $pubkey) {
2243 logger("Message: ".$msg, LOGGER_DATA);
2245 $handle = self::my_handle($user);
2247 $b64url_data = base64url_encode($msg);
2249 $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
2251 $type = "application/xml";
2252 $encoding = "base64url";
2253 $alg = "RSA-SHA256";
2255 $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
2257 $signature = rsa_sign($signable_data,$prvkey);
2258 $sig = base64url_encode($signature);
2260 $xmldata = array("diaspora" => array("header" => array("author_id" => $handle),
2261 "me:env" => array("me:encoding" => "base64url",
2262 "me:alg" => "RSA-SHA256",
2264 "@attributes" => array("type" => "application/xml"),
2265 "me:sig" => $sig)));
2267 $namespaces = array("" => "https://joindiaspora.com/protocol",
2268 "me" => "http://salmon-protocol.org/ns/magic-env");
2270 $magic_env = xml::from_array($xmldata, $xml, false, $namespaces);
2272 logger("magic_env: ".$magic_env, LOGGER_DATA);
2277 * @brief Creates the envelope for a private message
2279 * @param string $msg The message that is to be transmitted
2280 * @param array $user The record of the sender
2281 * @param array $contact Target of the communication
2282 * @param string $prvkey The private key of the sender
2283 * @param string $pubkey The public key of the receiver
2285 * @return string The envelope
2287 private function build_private_message($msg, $user, $contact, $prvkey, $pubkey) {
2289 logger("Message: ".$msg, LOGGER_DATA);
2291 // without a public key nothing will work
2294 logger("pubkey missing: contact id: ".$contact["id"]);
2298 $inner_aes_key = random_string(32);
2299 $b_inner_aes_key = base64_encode($inner_aes_key);
2300 $inner_iv = random_string(16);
2301 $b_inner_iv = base64_encode($inner_iv);
2303 $outer_aes_key = random_string(32);
2304 $b_outer_aes_key = base64_encode($outer_aes_key);
2305 $outer_iv = random_string(16);
2306 $b_outer_iv = base64_encode($outer_iv);
2308 $handle = self::my_handle($user);
2310 $padded_data = pkcs5_pad($msg,16);
2311 $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
2313 $b64_data = base64_encode($inner_encrypted);
2316 $b64url_data = base64url_encode($b64_data);
2317 $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
2319 $type = "application/xml";
2320 $encoding = "base64url";
2321 $alg = "RSA-SHA256";
2323 $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
2325 $signature = rsa_sign($signable_data,$prvkey);
2326 $sig = base64url_encode($signature);
2328 $xmldata = array("decrypted_header" => array("iv" => $b_inner_iv,
2329 "aes_key" => $b_inner_aes_key,
2330 "author_id" => $handle));
2332 $decrypted_header = xml::from_array($xmldata, $xml, true);
2333 $decrypted_header = pkcs5_pad($decrypted_header,16);
2335 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
2337 $outer_json = json_encode(array("iv" => $b_outer_iv, "key" => $b_outer_aes_key));
2339 $encrypted_outer_key_bundle = "";
2340 openssl_public_encrypt($outer_json, $encrypted_outer_key_bundle, $pubkey);
2342 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
2344 logger("outer_bundle: ".$b64_encrypted_outer_key_bundle." key: ".$pubkey, LOGGER_DATA);
2346 $encrypted_header_json_object = json_encode(array("aes_key" => base64_encode($encrypted_outer_key_bundle),
2347 "ciphertext" => base64_encode($ciphertext)));
2348 $cipher_json = base64_encode($encrypted_header_json_object);
2350 $xmldata = array("diaspora" => array("encrypted_header" => $cipher_json,
2351 "me:env" => array("me:encoding" => "base64url",
2352 "me:alg" => "RSA-SHA256",
2354 "@attributes" => array("type" => "application/xml"),
2355 "me:sig" => $sig)));
2357 $namespaces = array("" => "https://joindiaspora.com/protocol",
2358 "me" => "http://salmon-protocol.org/ns/magic-env");
2360 $magic_env = xml::from_array($xmldata, $xml, false, $namespaces);
2362 logger("magic_env: ".$magic_env, LOGGER_DATA);
2367 * @brief Create the envelope for a message
2369 * @param string $msg The message that is to be transmitted
2370 * @param array $user The record of the sender
2371 * @param array $contact Target of the communication
2372 * @param string $prvkey The private key of the sender
2373 * @param string $pubkey The public key of the receiver
2374 * @param bool $public Is the message public?
2376 * @return string The message that will be transmitted to other servers
2378 private function build_message($msg, $user, $contact, $prvkey, $pubkey, $public = false) {
2381 $magic_env = self::build_public_message($msg,$user,$contact,$prvkey,$pubkey);
2383 $magic_env = self::build_private_message($msg,$user,$contact,$prvkey,$pubkey);
2385 // The data that will be transmitted is double encoded via "urlencode", strange ...
2386 $slap = "xml=".urlencode(urlencode($magic_env));
2391 * @brief Creates a signature for a message
2393 * @param array $owner the array of the owner of the message
2394 * @param array $message The message that is to be signed
2396 * @return string The signature
2398 private function signature($owner, $message) {
2400 unset($sigmsg["author_signature"]);
2401 unset($sigmsg["parent_author_signature"]);
2403 $signed_text = implode(";", $sigmsg);
2405 return base64_encode(rsa_sign($signed_text, $owner["uprvkey"], "sha256"));
2409 * @brief Transmit a message to a target server
2411 * @param array $owner the array of the item owner
2412 * @param array $contact Target of the communication
2413 * @param string $slap The message that is to be transmitted
2414 * @param bool $public_batch Is it a public post?
2415 * @param bool $queue_run Is the transmission called from the queue?
2416 * @param string $guid message guid
2418 * @return int Result of the transmission
2420 public static function transmit($owner, $contact, $slap, $public_batch, $queue_run=false, $guid = "") {
2424 $enabled = intval(get_config("system", "diaspora_enabled"));
2428 $logid = random_string(4);
2429 $dest_url = (($public_batch) ? $contact["batch"] : $contact["notify"]);
2431 logger("no url for contact: ".$contact["id"]." batch mode =".$public_batch);
2435 logger("transmit: ".$logid."-".$guid." ".$dest_url);
2437 if (!$queue_run && was_recently_delayed($contact["id"])) {
2440 if (!intval(get_config("system", "diaspora_test"))) {
2441 post_url($dest_url."/", $slap);
2442 $return_code = $a->get_curl_code();
2444 logger("test_mode");
2449 logger("transmit: ".$logid."-".$guid." returns: ".$return_code);
2451 if(!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) {
2452 logger("queue message");
2454 $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d AND `network` = '%s' AND `content` = '%s' AND `batch` = %d LIMIT 1",
2455 intval($contact["id"]),
2456 dbesc(NETWORK_DIASPORA),
2458 intval($public_batch)
2461 logger("add_to_queue ignored - identical item already in queue");
2463 // queue message for redelivery
2464 add_to_queue($contact["id"], NETWORK_DIASPORA, $slap, $public_batch);
2468 return(($return_code) ? $return_code : (-1));
2473 * @brief Builds and transmit messages
2475 * @param array $owner the array of the item owner
2476 * @param array $contact Target of the communication
2477 * @param string $type The message type
2478 * @param array $message The message data
2479 * @param bool $public_batch Is it a public post?
2480 * @param string $guid message guid
2481 * @param bool $spool Should the transmission be spooled or transmitted?
2483 * @return int Result of the transmission
2485 private function build_and_transmit($owner, $contact, $type, $message, $public_batch = false, $guid = "", $spool = false) {
2487 $data = array("XML" => array("post" => array($type => $message)));
2489 $msg = xml::from_array($data, $xml);
2491 logger('message: '.$msg, LOGGER_DATA);
2492 logger('send guid '.$guid, LOGGER_DEBUG);
2494 $slap = self::build_message($msg, $owner, $contact, $owner['uprvkey'], $contact['pubkey'], $public_batch);
2497 add_to_queue($contact['id'], NETWORK_DIASPORA, $slap, $public_batch);
2500 $return_code = self::transmit($owner, $contact, $slap, $public_batch, false, $guid);
2502 logger("guid: ".$item["guid"]." result ".$return_code, LOGGER_DEBUG);
2504 return $return_code;
2508 * @brief Sends a "share" message
2510 * @param array $owner the array of the item owner
2511 * @param array $contact Target of the communication
2513 * @return int The result of the transmission
2515 public static function send_share($owner,$contact) {
2517 $message = array("sender_handle" => self::my_handle($owner),
2518 "recipient_handle" => $contact["addr"]);
2520 return self::build_and_transmit($owner, $contact, "request", $message);
2524 * @brief sends an "unshare"
2526 * @param array $owner the array of the item owner
2527 * @param array $contact Target of the communication
2529 * @return int The result of the transmission
2531 public static function send_unshare($owner,$contact) {
2533 $message = array("post_guid" => $owner["guid"],
2534 "diaspora_handle" => self::my_handle($owner),
2535 "type" => "Person");
2537 return self::build_and_transmit($owner, $contact, "retraction", $message);
2541 * @brief Checks a message body if it is a reshare
2543 * @param string $body The message body that is to be check
2544 * @param bool $complete Should it be a complete check or a simple check?
2546 * @return array|bool Reshare details or "false" if no reshare
2548 public static function is_reshare($body, $complete = true) {
2549 $body = trim($body);
2551 // Skip if it isn't a pure repeated messages
2552 // Does it start with a share?
2553 if ((strpos($body, "[share") > 0) AND $complete)
2556 // Does it end with a share?
2557 if (strlen($body) > (strrpos($body, "[/share]") + 8))
2560 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
2561 // Skip if there is no shared message in there
2562 if ($body == $attributes)
2565 // If we don't do the complete check we quit here
2570 preg_match("/guid='(.*?)'/ism", $attributes, $matches);
2571 if ($matches[1] != "")
2572 $guid = $matches[1];
2574 preg_match('/guid="(.*?)"/ism', $attributes, $matches);
2575 if ($matches[1] != "")
2576 $guid = $matches[1];
2579 $r = q("SELECT `contact-id` FROM `item` WHERE `guid` = '%s' AND `network` IN ('%s', '%s') LIMIT 1",
2580 dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA);
2583 $ret["root_handle"] = self::handle_from_contact($r[0]["contact-id"]);
2584 $ret["root_guid"] = $guid;
2590 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
2591 if ($matches[1] != "")
2592 $profile = $matches[1];
2594 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
2595 if ($matches[1] != "")
2596 $profile = $matches[1];
2600 $ret["root_handle"] = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
2601 if (($ret["root_handle"] == $profile) OR ($ret["root_handle"] == ""))
2605 preg_match("/link='(.*?)'/ism", $attributes, $matches);
2606 if ($matches[1] != "")
2607 $link = $matches[1];
2609 preg_match('/link="(.*?)"/ism', $attributes, $matches);
2610 if ($matches[1] != "")
2611 $link = $matches[1];
2613 $ret["root_guid"] = preg_replace("=https?://(.*)/posts/(.*)=ism", "$2", $link);
2614 if (($ret["root_guid"] == $link) OR (trim($ret["root_guid"]) == ""))
2621 * @brief Sends a post
2623 * @param array $item The item that will be exported
2624 * @param array $owner the array of the item owner
2625 * @param array $contact Target of the communication
2626 * @param bool $public_batch Is it a public post?
2628 * @return int The result of the transmission
2630 public static function send_status($item, $owner, $contact, $public_batch = false) {
2632 $myaddr = self::my_handle($owner);
2634 $public = (($item["private"]) ? "false" : "true");
2636 $created = datetime_convert("UTC", "UTC", $item["created"], 'Y-m-d H:i:s \U\T\C');
2638 // Detect a share element and do a reshare
2639 if (!$item['private'] AND ($ret = self::is_reshare($item["body"]))) {
2640 $message = array("root_diaspora_id" => $ret["root_handle"],
2641 "root_guid" => $ret["root_guid"],
2642 "guid" => $item["guid"],
2643 "diaspora_handle" => $myaddr,
2644 "public" => $public,
2645 "created_at" => $created,
2646 "provider_display_name" => $item["app"]);
2650 $title = $item["title"];
2651 $body = $item["body"];
2653 // convert to markdown
2654 $body = html_entity_decode(bb2diaspora($body));
2658 $body = "## ".html_entity_decode($title)."\n\n".$body;
2660 if ($item["attach"]) {
2661 $cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism', $item["attach"], $matches, PREG_SET_ORDER);
2663 $body .= "\n".t("Attachments:")."\n";
2664 foreach($matches as $mtch)
2665 $body .= "[".$mtch[3]."](".$mtch[1].")\n";
2669 $location = array();
2671 if ($item["location"] != "")
2672 $location["address"] = $item["location"];
2674 if ($item["coord"] != "") {
2675 $coord = explode(" ", $item["coord"]);
2676 $location["lat"] = $coord[0];
2677 $location["lng"] = $coord[1];
2680 $message = array("raw_message" => $body,
2681 "location" => $location,
2682 "guid" => $item["guid"],
2683 "diaspora_handle" => $myaddr,
2684 "public" => $public,
2685 "created_at" => $created,
2686 "provider_display_name" => $item["app"]);
2688 if (count($location) == 0)
2689 unset($message["location"]);
2691 $type = "status_message";
2694 return self::build_and_transmit($owner, $contact, $type, $message, $public_batch, $item["guid"]);
2698 * @brief Creates a "like" object
2700 * @param array $item The item that will be exported
2701 * @param array $owner the array of the item owner
2703 * @return array The data for a "like"
2705 private function construct_like($item, $owner) {
2707 $p = q("SELECT `guid`, `uri`, `parent-uri` FROM `item` WHERE `uri` = '%s' LIMIT 1",
2708 dbesc($item["thr-parent"]));
2714 $target_type = ($parent["uri"] === $parent["parent-uri"] ? "Post" : "Comment");
2717 return(array("positive" => $positive,
2718 "guid" => $item["guid"],
2719 "target_type" => $target_type,
2720 "parent_guid" => $parent["guid"],
2721 "author_signature" => "",
2722 "diaspora_handle" => self::my_handle($owner)));
2726 * @brief Creates the object for a comment
2728 * @param array $item The item that will be exported
2729 * @param array $owner the array of the item owner
2731 * @return array The data for a comment
2733 private function construct_comment($item, $owner) {
2735 $p = q("SELECT `guid` FROM `item` WHERE `parent` = %d AND `id` = %d LIMIT 1",
2736 intval($item["parent"]),
2737 intval($item["parent"])
2745 $text = html_entity_decode(bb2diaspora($item["body"]));
2747 return(array("guid" => $item["guid"],
2748 "parent_guid" => $parent["guid"],
2749 "author_signature" => "",
2751 "diaspora_handle" => self::my_handle($owner)));
2755 * @brief Send a like or a comment
2757 * @param array $item The item that will be exported
2758 * @param array $owner the array of the item owner
2759 * @param array $contact Target of the communication
2760 * @param bool $public_batch Is it a public post?
2762 * @return int The result of the transmission
2764 public static function send_followup($item,$owner,$contact,$public_batch = false) {
2766 if($item['verb'] === ACTIVITY_LIKE) {
2767 $message = self::construct_like($item, $owner);
2770 $message = self::construct_comment($item, $owner);
2777 $message["author_signature"] = self::signature($owner, $message);
2779 return self::build_and_transmit($owner, $contact, $type, $message, $public_batch, $item["guid"]);
2783 * @brief Creates a message from a signature record entry
2785 * @param array $item The item that will be exported
2786 * @param array $signature The entry of the "sign" record
2788 * @return string The message
2790 private function message_from_signature($item, $signature) {
2792 // Split the signed text
2793 $signed_parts = explode(";", $signature['signed_text']);
2795 if ($item["deleted"])
2796 $message = array("parent_author_signature" => "",
2797 "target_guid" => $signed_parts[0],
2798 "target_type" => $signed_parts[1],
2799 "sender_handle" => $signature['signer'],
2800 "target_author_signature" => $signature['signature']);
2801 elseif ($item['verb'] === ACTIVITY_LIKE)
2802 $message = array("positive" => $signed_parts[0],
2803 "guid" => $signed_parts[1],
2804 "target_type" => $signed_parts[2],
2805 "parent_guid" => $signed_parts[3],
2806 "parent_author_signature" => "",
2807 "author_signature" => $signature['signature'],
2808 "diaspora_handle" => $signed_parts[4]);
2810 // Remove the comment guid
2811 $guid = array_shift($signed_parts);
2813 // Remove the parent guid
2814 $parent_guid = array_shift($signed_parts);
2816 // Remove the handle
2817 $handle = array_pop($signed_parts);
2819 // Glue the parts together
2820 $text = implode(";", $signed_parts);
2822 $message = array("guid" => $guid,
2823 "parent_guid" => $parent_guid,
2824 "parent_author_signature" => "",
2825 "author_signature" => $signature['signature'],
2826 "text" => implode(";", $signed_parts),
2827 "diaspora_handle" => $handle);
2833 * @brief Relays messages (like, comment, retraction) to other servers if we are the thread owner
2835 * @param array $item The item that will be exported
2836 * @param array $owner the array of the item owner
2837 * @param array $contact Target of the communication
2838 * @param bool $public_batch Is it a public post?
2840 * @return int The result of the transmission
2842 public static function send_relay($item, $owner, $contact, $public_batch = false) {
2844 if ($item["deleted"])
2845 return self::send_retraction($item, $owner, $contact, $public_batch, true);
2846 elseif ($item['verb'] === ACTIVITY_LIKE)
2851 logger("Got relayable data ".$type." for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG);
2853 // fetch the original signature
2855 $r = q("SELECT `signed_text`, `signature`, `signer` FROM `sign` WHERE `iid` = %d LIMIT 1",
2856 intval($item["id"]));
2859 logger("Couldn't fetch signatur for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG);
2865 // Old way - is used by the internal Friendica functions
2866 /// @todo Change all signatur storing functions to the new format
2867 if ($signature['signed_text'] AND $signature['signature'] AND $signature['signer'])
2868 $message = self::message_from_signature($item, $signature);
2870 $msg = json_decode($signature['signed_text'], true);
2873 if (is_array($msg)) {
2874 foreach ($msg AS $field => $data) {
2875 if (!$item["deleted"]) {
2876 if ($field == "author")
2877 $field = "diaspora_handle";
2878 if ($field == "parent_type")
2879 $field = "target_type";
2882 $message[$field] = $data;
2885 logger("Signature text for item ".$item["guid"]." (".$item["id"].") couldn't be extracted: ".$signature['signed_text'], LOGGER_DEBUG);
2888 $message["parent_author_signature"] = self::signature($owner, $message);
2890 logger("Relayed data ".print_r($message, true), LOGGER_DEBUG);
2892 return self::build_and_transmit($owner, $contact, $type, $message, $public_batch, $item["guid"]);
2896 * @brief Sends a retraction (deletion) of a message, like or comment
2898 * @param array $item The item that will be exported
2899 * @param array $owner the array of the item owner
2900 * @param array $contact Target of the communication
2901 * @param bool $public_batch Is it a public post?
2902 * @param bool $relay Is the retraction transmitted from a relay?
2904 * @return int The result of the transmission
2906 public static function send_retraction($item, $owner, $contact, $public_batch = false, $relay = false) {
2908 $itemaddr = self::handle_from_contact($item["contact-id"], $item["gcontact-id"]);
2910 // Check whether the retraction is for a top-level post or whether it's a relayable
2911 if ($item["uri"] !== $item["parent-uri"]) {
2912 $msg_type = "relayable_retraction";
2913 $target_type = (($item["verb"] === ACTIVITY_LIKE) ? "Like" : "Comment");
2915 $msg_type = "signed_retraction";
2916 $target_type = "StatusMessage";
2919 if ($relay AND ($item["uri"] !== $item["parent-uri"]))
2920 $signature = "parent_author_signature";
2922 $signature = "target_author_signature";
2924 $signed_text = $item["guid"].";".$target_type;
2926 $message = array("target_guid" => $item['guid'],
2927 "target_type" => $target_type,
2928 "sender_handle" => $itemaddr,
2929 $signature => base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256')));
2931 logger("Got message ".print_r($message, true), LOGGER_DEBUG);
2933 return self::build_and_transmit($owner, $contact, $msg_type, $message, $public_batch, $item["guid"]);
2937 * @brief Sends a mail
2939 * @param array $item The item that will be exported
2940 * @param array $owner The owner
2941 * @param array $contact Target of the communication
2943 * @return int The result of the transmission
2945 public static function send_mail($item, $owner, $contact) {
2947 $myaddr = self::my_handle($owner);
2949 $r = q("SELECT * FROM `conv` WHERE `id` = %d AND `uid` = %d LIMIT 1",
2950 intval($item["convid"]),
2951 intval($item["uid"])
2955 logger("conversation not found.");
2961 "guid" => $cnv["guid"],
2962 "subject" => $cnv["subject"],
2963 "created_at" => datetime_convert("UTC", "UTC", $cnv['created'], 'Y-m-d H:i:s \U\T\C'),
2964 "diaspora_handle" => $cnv["creator"],
2965 "participant_handles" => $cnv["recips"]
2968 $body = bb2diaspora($item["body"]);
2969 $created = datetime_convert("UTC", "UTC", $item["created"], 'Y-m-d H:i:s \U\T\C');
2971 $signed_text = $item["guid"].";".$cnv["guid"].";".$body.";".$created.";".$myaddr.";".$cnv['guid'];
2972 $sig = base64_encode(rsa_sign($signed_text, $owner["uprvkey"], "sha256"));
2975 "guid" => $item["guid"],
2976 "parent_guid" => $cnv["guid"],
2977 "parent_author_signature" => $sig,
2978 "author_signature" => $sig,
2980 "created_at" => $created,
2981 "diaspora_handle" => $myaddr,
2982 "conversation_guid" => $cnv["guid"]
2985 if ($item["reply"]) {
2989 $message = array("guid" => $cnv["guid"],
2990 "subject" => $cnv["subject"],
2991 "created_at" => datetime_convert("UTC", "UTC", $cnv['created'], 'Y-m-d H:i:s \U\T\C'),
2993 "diaspora_handle" => $cnv["creator"],
2994 "participant_handles" => $cnv["recips"]);
2996 $type = "conversation";
2999 return self::build_and_transmit($owner, $contact, $type, $message, false, $item["guid"]);
3003 * @brief Sends profile data
3005 * @param int $uid The user id
3007 public static function send_profile($uid) {
3012 $recips = q("SELECT `id`,`name`,`network`,`pubkey`,`notify` FROM `contact` WHERE `network` = '%s'
3013 AND `uid` = %d AND `rel` != %d",
3014 dbesc(NETWORK_DIASPORA),
3016 intval(CONTACT_IS_SHARING)
3021 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.*, `user`.`prvkey` AS `uprvkey`, `contact`.`addr`
3023 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
3024 INNER JOIN `contact` ON `profile`.`uid` = `contact`.`uid`
3025 WHERE `user`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
3034 $handle = $profile["addr"];
3035 $first = ((strpos($profile['name'],' ')
3036 ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']));
3037 $last = (($first === $profile['name']) ? '' : trim(substr($profile['name'], strlen($first))));
3038 $large = App::get_baseurl().'/photo/custom/300/'.$profile['uid'].'.jpg';
3039 $medium = App::get_baseurl().'/photo/custom/100/'.$profile['uid'].'.jpg';
3040 $small = App::get_baseurl().'/photo/custom/50/' .$profile['uid'].'.jpg';
3041 $searchable = (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false');
3043 if ($searchable === 'true') {
3044 $dob = '1000-00-00';
3046 if (($profile['dob']) && ($profile['dob'] != '0000-00-00'))
3047 $dob = ((intval($profile['dob'])) ? intval($profile['dob']) : '1000') .'-'. datetime_convert('UTC','UTC',$profile['dob'],'m-d');
3049 $about = $profile['about'];
3050 $about = strip_tags(bbcode($about));
3052 $location = formatted_location($profile);
3054 if ($profile['pub_keywords']) {
3055 $kw = str_replace(',',' ',$profile['pub_keywords']);
3056 $kw = str_replace(' ',' ',$kw);
3057 $arr = explode(' ',$profile['pub_keywords']);
3059 for($x = 0; $x < 5; $x ++) {
3061 $tags .= '#'. trim($arr[$x]) .' ';
3065 $tags = trim($tags);
3068 $message = array("diaspora_handle" => $handle,
3069 "first_name" => $first,
3070 "last_name" => $last,
3071 "image_url" => $large,
3072 "image_url_medium" => $medium,
3073 "image_url_small" => $small,
3075 "gender" => $profile['gender'],
3077 "location" => $location,
3078 "searchable" => $searchable,
3079 "tag_string" => $tags);
3081 foreach($recips as $recip)
3082 self::build_and_transmit($profile, $recip, "profile", $message, false, "", true);
3086 * @brief Stores the signature for likes that are created on our system
3088 * @param array $contact The contact array of the "like"
3089 * @param int $post_id The post id of the "like"
3091 * @return bool Success
3093 public static function store_like_signature($contact, $post_id) {
3095 // Is the contact the owner? Then fetch the private key
3096 if (!$contact['self'] OR ($contact['uid'] == 0)) {
3097 logger("No owner post, so not storing signature", LOGGER_DEBUG);
3101 $r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($contact['uid']));
3105 $contact["uprvkey"] = $r[0]['prvkey'];
3107 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1", intval($post_id));
3111 if (!in_array($r[0]["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE)))
3114 $message = self::construct_like($r[0], $contact);
3115 $message["author_signature"] = self::signature($contact, $message);
3117 // In the future we will store the signature more flexible to support new fields.
3118 // Right now we cannot change this since old Friendica versions (prior to 3.5) can only handle this format.
3119 // (We are transmitting this data here via DFRN)
3121 $signed_text = $message["positive"].";".$message["guid"].";".$message["target_type"].";".
3122 $message["parent_guid"].";".$message["diaspora_handle"];
3124 q("INSERT INTO `sign` (`iid`,`signed_text`,`signature`,`signer`) VALUES (%d,'%s','%s','%s')",
3126 dbesc($signed_text),
3127 dbesc($message["author_signature"]),
3128 dbesc($message["diaspora_handle"])
3131 // This here will replace the lines above, once Diaspora changed its protocol
3132 //q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
3133 // intval($message_id),
3134 // dbesc(json_encode($message))
3137 logger('Stored diaspora like signature');
3142 * @brief Stores the signature for comments that are created on our system
3144 * @param array $item The item array of the comment
3145 * @param array $contact The contact array of the item owner
3146 * @param string $uprvkey The private key of the sender
3147 * @param int $message_id The message id of the comment
3149 * @return bool Success
3151 public static function store_comment_signature($item, $contact, $uprvkey, $message_id) {
3153 if ($uprvkey == "") {
3154 logger('No private key, so not storing comment signature', LOGGER_DEBUG);
3158 $contact["uprvkey"] = $uprvkey;
3160 $message = self::construct_comment($item, $contact);
3161 $message["author_signature"] = self::signature($contact, $message);
3163 // In the future we will store the signature more flexible to support new fields.
3164 // Right now we cannot change this since old Friendica versions (prior to 3.5) can only handle this format.
3165 // (We are transmitting this data here via DFRN)
3166 $signed_text = $message["guid"].";".$message["parent_guid"].";".
3167 $message["text"].";".$message["diaspora_handle"];
3169 q("INSERT INTO `sign` (`iid`,`signed_text`,`signature`,`signer`) VALUES (%d,'%s','%s','%s')",
3170 intval($message_id),
3171 dbesc($signed_text),
3172 dbesc($message["author_signature"]),
3173 dbesc($message["diaspora_handle"])
3176 // This here will replace the lines above, once Diaspora changed its protocol
3177 //q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
3178 // intval($message_id),
3179 // dbesc(json_encode($message))
3182 logger('Stored diaspora comment signature');