3 require_once('include/crypto.php');
4 require_once('include/items.php');
6 function get_diaspora_key($uri) {
7 logger('Fetching diaspora key for: ' . $uri);
9 $r = find_diaspora_person_by_handle($uri);
16 function diaspora_base_message($type,$data) {
18 $tpl = get_markup_template('diaspora_' . $type . '.tpl');
21 return replace_macros($tpl,$data);
26 function diaspora_msg_build($msg,$user,$contact,$prvkey,$pubkey) {
29 logger('diaspora_msg_build: ' . $msg, LOGGER_DATA);
31 $inner_aes_key = random_string(32);
32 $b_inner_aes_key = base64_encode($inner_aes_key);
33 $inner_iv = random_string(16);
34 $b_inner_iv = base64_encode($inner_iv);
36 $outer_aes_key = random_string(32);
37 $b_outer_aes_key = base64_encode($outer_aes_key);
38 $outer_iv = random_string(16);
39 $b_outer_iv = base64_encode($outer_iv);
41 $handle = 'acct:' . $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
43 $padded_data = pkcs5_pad($msg,16);
44 $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
46 $b64_data = base64_encode($inner_encrypted);
49 $b64url_data = base64url_encode($b64_data);
50 $b64url_stripped = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
51 $lines = str_split($b64url_stripped,60);
52 $data = implode("\n",$lines);
53 $data = $data . (($data[-1] != "\n") ? "\n" : '') ;
54 $type = 'application/atom+xml';
55 $encoding = 'base64url';
58 $signable_data = $data . '.' . base64url_encode($type) . "\n" . '.'
59 . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
61 $signature = rsa_sign($signable_data,$prvkey);
62 $sig = base64url_encode($signature);
64 $decrypted_header = <<< EOT
67 <aes_key>$b_inner_aes_key</aes_key>
69 <name>{$user['username']}</name>
75 $decrypted_header = pkcs5_pad($decrypted_header,16);
77 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
79 $outer_json = json_encode(array('iv' => $b_outer_iv,'key' => $b_outer_aes_key));
80 $encrypted_outer_key_bundle = '';
81 openssl_public_encrypt($outer_json,$encrypted_outer_key_bundle,$pubkey);
83 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
84 $encrypted_header_json_object = json_encode(array('aes_key' => base64_encode($encrypted_outer_key_bundle),
85 'ciphertext' => base64_encode($ciphertext)));
86 $encrypted_header = '<encrypted_header>' . base64_encode($encrypted_header_json_object) . '</encrypted_header>';
89 <?xml version='1.0' encoding='UTF-8'?>
90 <entry xmlns='http://www.w3.org/2005/Atom'>
92 <me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
93 <me:encoding>base64url</me:encoding>
94 <me:alg>RSA-SHA256</me:alg>
95 <me:data type="application/atom+xml">$data</me:data>
101 logger('diaspora_msg_build: magic_env: ' . $magic_env, LOGGER_DATA);
108 * diaspora_decode($importer,$xml)
109 * array $importer -> from user table
110 * string $xml -> urldecoded Diaspora salmon
113 * 'message' -> decoded Diaspora XML message
114 * 'author' -> author diaspora handle
115 * 'key' -> author public key (converted to pkcs#8)
117 * Author and key are used elsewhere to save a lookup for verifying replies and likes
121 function diaspora_decode($importer,$xml) {
123 $basedom = parse_xml_string($xml);
125 $atom = $basedom->children(NAMESPACE_ATOM1);
127 // Diaspora devs: This is kind of sucky - 'encrypted_header' does not belong in the atom namespace
129 $encrypted_header = json_decode(base64_decode($atom->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);
148 * $decrypted now contains something like
151 * <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
152 * <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
154 * <name>Ryan Hughes</name>
155 * <uri>acct:galaxor@diaspora.pirateship.org</uri>
157 * </decrypted_header>
160 logger('decrypted: ' . $decrypted);
161 $idom = parse_xml_string($decrypted,false);
163 $inner_iv = base64_decode($idom->iv);
164 $inner_aes_key = base64_decode($idom->aes_key);
166 $author_link = str_replace('acct:','',$idom->author->uri);
168 $dom = $basedom->children(NAMESPACE_SALMON_ME);
170 // figure out where in the DOM tree our data is hiding
172 if($dom->provenance->data)
173 $base = $dom->provenance;
174 elseif($dom->env->data)
180 logger('mod-diaspora: unable to locate salmon data in xml ');
181 http_status_exit(400);
185 // Stash the signature away for now. We have to find their key or it won't be good for anything.
186 $signature = base64url_decode($base->sig);
190 // strip whitespace so our data element will return to one big base64 blob
191 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
193 // Add back the 60 char linefeeds
195 // This completely violates the entire principle of salmon magic signatures,
196 // which was to have a message signing format that was completely ambivalent to linefeeds
197 // and transport whitespace mangling, and base64 wrapping rules. Guess what? PHP and Ruby
198 // use different linelengths for base64 output.
200 $lines = str_split($data,60);
201 $data = implode("\n",$lines);
204 // stash away some other stuff for later
206 $type = $base->data[0]->attributes()->type[0];
207 $keyhash = $base->sig[0]->attributes()->keyhash[0];
208 $encoding = $base->encoding;
211 // I can't even begin to tell you how sucky this is. Please read the spec.
213 $signed_data = $data . (($data[-1] != "\n") ? "\n" : '') . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
217 $data = base64url_decode($data);
219 // Now pull out the inner encrypted blob
221 $inner_encrypted = base64_decode($data);
224 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
226 $inner_decrypted = pkcs5_unpad($inner_decrypted);
229 logger('mod-diaspora: Could not retrieve author URI.');
230 http_status_exit(400);
233 // Once we have the author URI, go to the web and try to find their public key
234 // (first this will look it up locally if it is in the fcontact cache)
235 // This will also convert diaspora public key from pkcs#1 to pkcs#8
237 logger('mod-diaspora: Fetching key for ' . $author_link );
238 $key = get_diaspora_key($author_link);
241 logger('mod-diaspora: Could not retrieve author key.');
242 http_status_exit(400);
245 $verify = rsa_verify($signed_data,$signature,$key);
248 logger('mod-diaspora: Message did not verify. Discarding.');
249 http_status_exit(400);
252 logger('mod-diaspora: Message verified.');
254 return array('message' => $inner_decrypted, 'author' => $author_link, 'key' => $key);
258 function diaspora_get_contact_by_handle($uid,$handle) {
259 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `addr` = '%s' LIMIT 1",
260 dbesc(NETWORK_DIASPORA),
269 function find_diaspora_person_by_handle($handle) {
270 $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
271 dbesc(NETWORK_DIASPORA),
275 // update record occasionally so it doesn't get stale
276 $d = strtotime($r[0]['updated'] . ' +00:00');
277 if($d < strtotime('now - 14 days')) {
278 q("delete from fcontact where id = %d limit 1",
285 require_once('include/Scrape.php');
286 $r = probe_url($handle, PROBE_DIASPORA);
287 if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
296 function diaspora_request($importer,$xml) {
298 $sender_handle = unxmlify($xml->sender_handle);
299 $recipient_handle = unxmlify($xml->recipient_handle);
301 if(! $sender_handle || ! $recipient_handle)
304 $contact = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
309 // perhaps we were already sharing with this person. Now they're sharing with us.
310 // That makes us friends.
312 if($contact['rel'] == CONTACT_IS_FOLLOWER) {
313 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
314 intval(CONTACT_IS_FRIEND),
315 intval($contact['id']),
316 intval($importer['uid'])
319 // send notification?
323 $ret = find_diaspora_person_by_handle($sender_handle);
326 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
327 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
331 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
332 VALUES ( %d, '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s',%d,%d) ",
333 intval($importer['uid']),
334 dbesc($ret['network']),
340 dbesc($ret['photo']),
341 dbesc($ret['pubkey']),
342 dbesc($ret['notify']),
348 // find the contact record we just created
350 $contact_record = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
352 $hash = random_string() . (string) time(); // Generate a confirm_key
354 if($contact_record) {
355 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime` )
356 VALUES ( %d, %d, %d, %d, '%s', '%s', '%s' )",
357 intval($importer['uid']),
358 intval($contact_record['id']),
361 dbesc( t('Sharing notification from Diaspora network')),
363 dbesc(datetime_convert())
370 function diaspora_post($importer,$xml) {
372 $guid = notags(unxmlify($xml->guid));
373 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
375 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
379 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
380 logger('diaspora_post: Ignoring this author.');
381 http_status_exit(202);
385 $message_id = $diaspora_handle . ':' . $guid;
386 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
387 intval($importer['uid']),
394 // allocate a guid on our system - we aren't fixing any collisions.
395 // we're ignoring them
397 $g = q("select * from guid where guid = '%s' limit 1",
401 q("insert into guid ( guid ) values ( '%s' )",
406 $created = unxmlify($xml->created_at);
407 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
409 $body = unxmlify($xml->raw_message);
411 require_once('library/HTMLPurifier.auto.php');
412 require_once('include/html2bbcode.php');
414 $maxlen = get_max_import_size();
415 if($maxlen && (strlen($body) > $maxlen))
416 $body = substr($body,0, $maxlen);
418 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
420 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
421 '[youtube]$1[/youtube]', $body);
423 $body = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
424 '[youtube]$1[/youtube]', $body);
426 $body = oembed_html2bbcode($body);
428 $config = HTMLPurifier_Config::createDefault();
429 $config->set('Cache.DefinitionImpl', null);
430 $purifier = new HTMLPurifier($config);
431 $body = $purifier->purify($body);
433 $body = html2bbcode($body);
437 $datarray['uid'] = $importer['uid'];
438 $datarray['contact-id'] = $contact['id'];
439 $datarray['wall'] = 0;
440 $datarray['guid'] = $guid;
441 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
442 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
443 $datarray['private'] = $private;
444 $datarray['parent'] = 0;
445 $datarray['owner-name'] = $contact['name'];
446 $datarray['owner-link'] = $contact['url'];
447 $datarray['owner-avatar'] = $contact['thumb'];
448 $datarray['author-name'] = $contact['name'];
449 $datarray['author-link'] = $contact['url'];
450 $datarray['author-avatar'] = $contact['thumb'];
451 $datarray['body'] = $body;
453 item_store($datarray);
459 function diaspora_comment($importer,$xml,$msg) {
461 $guid = notags(unxmlify($xml->guid));
462 $parent_guid = notags(unxmlify($xml->parent_guid));
463 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
464 $target_type = notags(unxmlify($xml->target_type));
465 $text = unxmlify($xml->text);
466 $author_signature = notags(unxmlify($xml->author_signature));
468 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
472 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
476 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
477 logger('diaspora_comment: Ignoring this author.');
478 http_status_exit(202);
482 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
483 intval($importer['uid']),
487 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
490 $parent_item = $r[0];
492 $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
494 $author_signature = base64_decode($author_signature);
496 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
501 $person = find_diaspora_person_by_handle($diaspora_handle);
503 if(is_array($person) && x($person,'pubkey'))
504 $key = $person['pubkey'];
506 logger('diaspora_comment: unable to find author details');
511 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha1')) {
512 logger('diaspora_comment: verification failed.');
513 // until we figure out what is different about their signing algorithm, accept it
518 if($parent_author_signature) {
519 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $msg['author'];
521 $parent_author_signature = base64_decode($parent_author_signature);
525 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha1')) {
526 logger('diaspora_comment: owner verification failed.');
531 // Phew! Everything checks out. Now create an item.
533 require_once('library/HTMLPurifier.auto.php');
534 require_once('include/html2bbcode.php');
538 $maxlen = get_max_import_size();
539 if($maxlen && (strlen($body) > $maxlen))
540 $body = substr($body,0, $maxlen);
542 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
544 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
545 '[youtube]$1[/youtube]', $body);
547 $body = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
548 '[youtube]$1[/youtube]', $body);
550 $body = oembed_html2bbcode($body);
552 $config = HTMLPurifier_Config::createDefault();
553 $config->set('Cache.DefinitionImpl', null);
554 $purifier = new HTMLPurifier($config);
555 $body = $purifier->purify($body);
557 $body = html2bbcode($body);
560 $message_id = $diaspora_handle . ':' . $guid;
563 $datarray['uid'] = $importer['uid'];
564 $datarray['contact-id'] = $contact['id'];
565 $datarray['wall'] = $parent_item['wall'];
566 $datarray['gravity'] = GRAVITY_COMMENT;
567 $datarray['guid'] = $guid;
568 $datarray['uri'] = $message_id;
569 $datarray['parent-uri'] = $parent_item['uri'];
571 // No timestamps for comments? OK, we'll the use current time.
572 $datarray['created'] = $datarray['edited'] = datetime_convert();
573 $datarray['private'] = $parent_item['private'];
575 $datarray['owner-name'] = $contact['name'];
576 $datarray['owner-link'] = $contact['url'];
577 $datarray['owner-avatar'] = $contact['thumb'];
579 $datarray['author-name'] = $person['name'];
580 $datarray['author-link'] = $person['url'];
581 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
582 $datarray['body'] = $body;
584 $message_id = item_store($datarray);
586 if(! $parent_author_signature) {
587 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
589 dbesc($author_signed_data),
590 dbesc(base64_encode($author_signature)),
591 dbesc($diaspora_handle)
600 function diaspora_like($importer,$xml,$msg) {
602 $guid = notags(unxmlify($xml->guid));
603 $parent_guid = notags(unxmlify($xml->parent_guid));
604 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
605 $target_type = notags(unxmlify($xml->target_type));
606 $positive = notags(unxmlify($xml->positive));
607 $author_signature = notags(unxmlify($xml->author_signature));
609 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
611 // likes on comments not supported here and likes on photos not supported by Diaspora
613 if($target_type !== 'Post')
616 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
620 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
621 logger('diaspora_like: Ignoring this author.');
622 http_status_exit(202);
626 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
627 intval($importer['uid']),
631 logger('diaspora_like: parent item not found: ' . $guid);
635 $parent_item = $r[0];
637 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '$s' LIMIT 1",
638 intval($importer['uid']),
642 if($positive === 'true') {
643 logger('diaspora_like: duplicate like: ' . $guid);
646 if($positive === 'false') {
647 q("UPDATE `item` SET `deleted` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
649 intval($importer['uid'])
652 // send notification via proc_run()
656 if($positive === 'false') {
657 logger('diaspora_like: unlike received with no corresponding like');
661 $author_signed_data = $guid . ';' . $parent_guid . ';' . $target_type . ';' . $positive . ';' . $diaspora_handle;
663 $author_signature = base64_decode($author_signature);
665 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
670 $person = find_diaspora_person_by_handle($diaspora_handle);
671 if(is_array($person) && x($person,'pubkey'))
672 $key = $person['pubkey'];
674 logger('diaspora_comment: unable to find author details');
679 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha1')) {
680 logger('diaspora_like: verification failed.');
684 if($parent_author_signature) {
685 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $target_type . ';' . $positive . ';' . $msg['author'];
687 $parent_author_signature = base64_decode($parent_author_signature);
691 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha1')) {
692 logger('diaspora_like: owner verification failed.');
697 // Phew! Everything checks out. Now create an item.
699 $uri = $diaspora_handle . ':' . $guid;
701 $activity = ACTIVITY_LIKE;
702 $post_type = (($parent_item['resource-id']) ? t('photo') : t('status'));
703 $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
704 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . '" />' . "\n") ;
705 $body = $parent_item['body'];
710 <type>$objtype</type>
712 <id>{$parent_item['uri']}</id>
715 <content>$body</content>
718 $bodyverb = t('%1$s likes %2$s\'s %3$s');
723 $arr['uid'] = $importer['uid'];
724 $arr['contact-id'] = $contact['id'];
725 $arr['type'] = 'activity';
726 $arr['wall'] = $parent_item['wall'];
727 $arr['gravity'] = GRAVITY_LIKE;
728 $arr['parent'] = $parent_item['id'];
729 $arr['parent-uri'] = $parent_item['uri'];
731 $datarray['owner-name'] = $contact['name'];
732 $datarray['owner-link'] = $contact['url'];
733 $datarray['owner-avatar'] = $contact['thumb'];
735 $datarray['author-name'] = $person['name'];
736 $datarray['author-link'] = $person['url'];
737 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
739 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
740 $alink = '[url=' . $parent_item['author-link'] . ']' . $parent_item['author-name'] . '[/url]';
741 $plink = '[url=' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . ']' . $post_type . '[/url]';
742 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
744 $arr['private'] = $parent_item['private'];
745 $arr['verb'] = $activity;
746 $arr['object-type'] = $objtype;
747 $arr['object'] = $obj;
750 $arr['last-child'] = 0;
752 $message_id = item_store($arr);
754 if(! $parent_author_signature) {
755 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
757 dbesc($author_signed_data),
758 dbesc(base64_encode($author_signature)),
759 dbesc($diaspora_handle)
763 // FIXME send notification
768 function diaspora_retraction($importer,$xml) {
770 $guid = notags(unxmlify($xml->guid));
771 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
773 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
777 // if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
778 // logger('diaspora_retraction: Ignoring this author.');
779 // http_status_exit(202);
787 function diaspora_share($me,$contact) {
789 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
790 $theiraddr = $contact['addr'];
792 $tpl = get_markup_template('diaspora_share.tpl');
793 $msg = replace_macros($tpl, array(
794 '$sender' => $myaddr,
795 '$recipient' => $theiraddr
798 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
800 post_url($contact['notify'],$slap);
801 $return_code = $a->get_curl_code();
805 function diaspora_send_status($item,$owner,$contact) {
808 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
809 $theiraddr = $contact['addr'];
810 require_once('include/bbcode.php');
812 $body = xmlify(bbcode($item['body']));
813 $public = (($item['private']) ? 'false' : 'true');
815 require_once('include/datetime.php');
816 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d h:i:s \U\T\C');
818 $tpl = get_markup_template('diaspora_post.tpl');
819 $msg = replace_macros($tpl, array(
821 '$guid' => $item['guid'],
822 '$handle' => xmlify($myaddr),
823 '$public' => $public,
824 '$created' => $created
827 logger('diaspora_send_status: base message: ' . $msg, LOGGER_DATA);
829 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
831 post_url($contact['notify'],$slap);
832 $return_code = $a->get_curl_code();
833 logger('diaspora_send_status: returns: ' . $return_code);
838 function diaspora_send_followup($item,$owner,$contact) {
841 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
842 $theiraddr = $contact['addr'];
844 $p = q("select guid from item where parent = %d limit 1",
848 $parent_guid = $p[0]['guid'];
852 if($item['verb'] === ACTIVITY_LIKE) {
853 $tpl = get_markup_template('diaspora_like.tpl');
855 $target_type = 'Post';
856 $positive = (($item['deleted']) ? 'false' : 'true');
859 $tpl = get_markup_template('diaspora_comment.tpl');
863 $text = bbcode($item['body']);
868 $signed_text = $item['guid'] . ';' . $target_type . ';' . $positive . ';' . $myaddr;
870 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
872 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey']),'sha1');
874 $msg = replace_macros($tpl,array(
875 '$guid' => xmlify($item['guid']),
876 '$parent_guid' => xmlify($parent_guid),
877 '$target_type' =>xmlify($target_type),
878 '$authorsig' => xmlify($authorsig),
879 '$body' => xmlify($text),
880 '$positive' => xmlify($positive),
881 '$handle' => xmlify($myaddr)
884 logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
886 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
888 post_url($contact['notify'],$slap);
889 $return_code = $a->get_curl_code();
890 logger('diaspora_send_status: returns: ' . $return_code);
896 function diaspora_send_relay($item,$owner,$contact) {
900 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
901 $theiraddr = $contact['addr'];
904 $p = q("select guid from item where parent = %d limit 1",
908 $parent_guid = $p[0]['guid'];
912 // fetch the original signature
913 $r = q("select * from sign where iid = %d limit 1",
920 if($item['verb'] === ACTIVITY_LIKE) {
921 $tpl = get_markup_template('diaspora_like_relay.tpl');
923 $target_type = 'Post';
924 $positive = (($item['deleted']) ? 'false' : 'true');
927 $tpl = get_markup_template('diaspora_comment_relay.tpl');
931 $text = bbcode($item['body']);
936 $parent_signed_text = $orig_sign['signed_text'];
938 $parent_signed_text = $orig_sign['signed_text'];
940 $parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha1'));
942 $msg = replace_macros($tpl,array(
943 '$guid' => xmlify($item['guid']),
944 '$parent_guid' => xmlify($parent_guid),
945 '$target_type' =>xmlify($target_type),
946 '$authorsig' => xmlify($orig_sign['signature']),
947 '$parentsig' => xmlify($parentauthorsig),
948 '$text' => xmlify($text),
949 '$positive' => xmlify($positive),
950 '$diaspora_handle' => xmlify($myaddr)
953 // fetch the original signature
954 $r = q("select * from sign where iid = %d limit 1",
960 logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
962 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
964 post_url($contact['notify'],$slap);
965 $return_code = $a->get_curl_code();
966 logger('diaspora_send_status: returns: ' . $return_code);
973 function diaspora_send_retraction($item,$owner,$contact) {