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 // Diaspora devs: 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 // Diaspora devs: 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 // *** or look it up locally ***
236 logger('mod-diaspora: Fetching key for ' . $author_link );
238 // Get diaspora public key (pkcs#1) and convert to pkcs#8
240 $key = get_diaspora_key($author_link);
243 logger('mod-diaspora: Could not retrieve author key.');
244 http_status_exit(400);
247 $verify = rsa_verify($signed_data,$signature,$key);
250 logger('mod-diaspora: Message did not verify. Discarding.');
251 http_status_exit(400);
254 logger('mod-diaspora: Message verified.');
256 return array('message' => $inner_decrypted, 'author' => $author_link, 'key' => $key);
260 function diaspora_get_contact_by_handle($uid,$handle) {
261 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `addr` = '%s' LIMIT 1",
262 dbesc(NETWORK_DIASPORA),
271 function find_diaspora_person_by_handle($handle) {
272 $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
273 dbesc(NETWORK_DIASPORA),
277 // update record occasionally so it doesn't get stale
278 $d = strtotime($r[0]['updated'] . ' +00:00');
279 if($d < strtotime('now - 14 days')) {
280 q("delete from fcontact where id = %d limit 1",
287 require_once('include/Scrape.php');
288 $r = probe_url($handle, PROBE_DIASPORA);
289 if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
298 function diaspora_request($importer,$xml) {
300 $sender_handle = unxmlify($xml->sender_handle);
301 $recipient_handle = unxmlify($xml->recipient_handle);
303 if(! $sender_handle || ! $recipient_handle)
306 $contact = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
311 // perhaps we were already sharing with this person. Now they're sharing with us.
312 // That makes us friends.
314 if($contact['rel'] == CONTACT_IS_FOLLOWER) {
315 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
316 intval(CONTACT_IS_FRIEND),
317 intval($contact['id']),
318 intval($importer['uid'])
321 // send notification?
325 $ret = find_diaspora_person_by_handle($sender_handle);
328 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
329 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
333 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
334 VALUES ( %d, '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s',%d,%d) ",
335 intval($importer['uid']),
336 dbesc($ret['network']),
342 dbesc($ret['photo']),
343 dbesc($ret['pubkey']),
344 dbesc($ret['notify']),
350 // find the contact record we just created
352 $contact_record = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
354 $hash = random_string() . (string) time(); // Generate a confirm_key
356 if($contact_record) {
357 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime` )
358 VALUES ( %d, %d, %d, %d, '%s', '%s', '%s' )",
359 intval($importer['uid']),
360 intval($contact_record['id']),
363 dbesc( t('Sharing notification from Diaspora network')),
365 dbesc(datetime_convert())
372 function diaspora_post($importer,$xml) {
374 $guid = notags(unxmlify($xml->guid));
375 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
377 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
381 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
382 logger('diaspora_post: Ignoring this author.');
383 http_status_exit(202);
387 $message_id = $diaspora_handle . ':' . $guid;
388 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
389 intval($importer['uid']),
396 // allocate a guid on our system - we aren't fixing any collisions.
397 // we're ignoring them
399 $g = q("select * from guid where guid = '%s' limit 1",
403 q("insert into guid ( guid ) values ( '%s' )",
408 $created = unxmlify($xml->created_at);
409 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
411 $body = unxmlify($xml->raw_message);
413 require_once('library/HTMLPurifier.auto.php');
414 require_once('include/html2bbcode.php');
416 $maxlen = get_max_import_size();
417 if($maxlen && (strlen($body) > $maxlen))
418 $body = substr($body,0, $maxlen);
420 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
422 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
423 '[youtube]$1[/youtube]', $body);
425 $body = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
426 '[youtube]$1[/youtube]', $body);
428 $body = oembed_html2bbcode($body);
430 $config = HTMLPurifier_Config::createDefault();
431 $config->set('Cache.DefinitionImpl', null);
432 $purifier = new HTMLPurifier($config);
433 $body = $purifier->purify($body);
435 $body = html2bbcode($body);
439 $datarray['uid'] = $importer['uid'];
440 $datarray['contact-id'] = $contact['id'];
441 $datarray['wall'] = 0;
442 $datarray['guid'] = $guid;
443 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
444 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
445 $datarray['private'] = $private;
446 $datarray['parent'] = 0;
447 $datarray['owner-name'] = $contact['name'];
448 $datarray['owner-link'] = $contact['url'];
449 $datarray['owner-avatar'] = $contact['thumb'];
450 $datarray['author-name'] = $contact['name'];
451 $datarray['author-link'] = $contact['url'];
452 $datarray['author-avatar'] = $contact['thumb'];
453 $datarray['body'] = $body;
455 item_store($datarray);
461 function diaspora_comment($importer,$xml,$msg) {
463 $guid = notags(unxmlify($xml->guid));
464 $parent_guid = notags(unxmlify($xml->parent_guid));
465 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
466 $target_type = notags(unxmlify($xml->target_type));
467 $text = unxmlify($xml->text);
468 $author_signature = notags(unxmlify($xml->author_signature));
470 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
474 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
478 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
479 logger('diaspora_comment: Ignoring this author.');
480 http_status_exit(202);
484 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
485 intval($importer['uid']),
489 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
492 $parent_item = $r[0];
494 $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
496 $author_signature = base64_decode($author_signature);
498 if(stricmp($diaspora_handle,$msg['author']) == 0) {
503 $person = find_diaspora_person_by_handle($diaspora_handle);
505 if(is_array($person) && x($person,'pubkey'))
506 $key = $person['pubkey'];
508 logger('diaspora_comment: unable to find author details');
513 if(! rsa_verify($author_signed_data,$author_signature,$key)) {
514 logger('diaspora_comment: verification failed.');
519 if($parent_author_signature) {
520 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $msg['author'];
522 $parent_author_signature = base64_decode($parent_author_signature);
526 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key)) {
527 logger('diaspora_comment: owner verification failed.');
532 // Phew! Everything checks out. Now create an item.
534 require_once('library/HTMLPurifier.auto.php');
535 require_once('include/html2bbcode.php');
539 $maxlen = get_max_import_size();
540 if($maxlen && (strlen($body) > $maxlen))
541 $body = substr($body,0, $maxlen);
543 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
545 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
546 '[youtube]$1[/youtube]', $body);
548 $body = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
549 '[youtube]$1[/youtube]', $body);
551 $body = oembed_html2bbcode($body);
553 $config = HTMLPurifier_Config::createDefault();
554 $config->set('Cache.DefinitionImpl', null);
555 $purifier = new HTMLPurifier($config);
556 $body = $purifier->purify($body);
558 $body = html2bbcode($body);
561 $message_id = $diaspora_handle . ':' . $guid;
564 $datarray['uid'] = $importer['uid'];
565 $datarray['contact-id'] = $contact['id'];
566 $datarray['wall'] = $parent_item['wall'];
567 $datarray['gravity'] = GRAVITY_COMMENT;
568 $datarray['guid'] = $guid;
569 $datarray['uri'] = $message_id;
570 $datarray['parent-uri'] = $parent_item['uri'];
572 // No timestamps for comments? OK, we'll the use current time.
573 $datarray['created'] = $datarray['edited'] = datetime_convert();
574 $datarray['private'] = $parent_item['private'];
576 $datarray['owner-name'] = $contact['name'];
577 $datarray['owner-link'] = $contact['url'];
578 $datarray['owner-avatar'] = $contact['thumb'];
580 $datarray['author-name'] = $person['name'];
581 $datarray['author-link'] = $person['url'];
582 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
583 $datarray['body'] = $body;
585 $message_id = item_store($datarray);
587 if(! $parent_author_signature) {
588 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
590 dbesc($author_signed_data),
591 dbesc(base64_encode($author_signature)),
592 dbesc($diaspora_handle)
601 function diaspora_like($importer,$xml,$msg) {
603 $guid = notags(unxmlify($xml->guid));
604 $parent_guid = notags(unxmlify($xml->parent_guid));
605 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
606 $target_type = notags(unxmlify($xml->target_type));
607 $positive = notags(unxmlify($xml->positive));
608 $author_signature = notags(unxmlify($xml->author_signature));
610 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
612 // likes on comments not supported here and likes on photos not supported by Diaspora
614 if($target_type !== 'Post')
617 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
621 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
622 logger('diaspora_like: Ignoring this author.');
623 http_status_exit(202);
627 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
628 intval($importer['uid']),
632 logger('diaspora_like: parent item not found: ' . $guid);
636 $parent_item = $r[0];
638 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '$s' LIMIT 1",
639 intval($importer['uid']),
643 if($positive === 'true') {
644 logger('diaspora_like: duplicate like: ' . $guid);
647 if($positive === 'false') {
648 q("UPDATE `item` SET `deleted` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
650 intval($importer['uid'])
653 // send notification via proc_run()
657 if($positive === 'false') {
658 logger('diaspora_like: unlike received with no corresponding like');
662 $author_signed_data = $guid . ';' . $parent_guid . ';' . $target_type . ';' . $positive . ';' . $diaspora_handle;
664 $author_signature = base64_decode($author_signature);
666 if(stricmp($diaspora_handle,$msg['author']) == 0) {
671 $person = find_diaspora_person_by_handle($diaspora_handle);
672 if(is_array($person) && x($person,'pubkey'))
673 $key = $person['pubkey'];
675 logger('diaspora_comment: unable to find author details');
680 if(! rsa_verify($author_signed_data,$author_signature,$key)) {
681 logger('diaspora_like: verification failed.');
685 if($parent_author_signature) {
686 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $target_type . ';' . $positive . ';' . $msg['author'];
688 $parent_author_signature = base64_decode($parent_author_signature);
692 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key)) {
693 logger('diaspora_like: owner verification failed.');
698 // Phew! Everything checks out. Now create an item.
700 $uri = $diaspora_handle . ':' . $guid;
702 $activity = ACTIVITY_LIKE;
703 $post_type = (($parent_item['resource-id']) ? t('photo') : t('status'));
704 $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
705 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . '" />' . "\n") ;
706 $body = $parent_item['body'];
711 <type>$objtype</type>
713 <id>{$parent_item['uri']}</id>
716 <content>$body</content>
719 $bodyverb = t('%1$s likes %2$s\'s %3$s');
724 $arr['uid'] = $importer['uid'];
725 $arr['contact-id'] = $contact['id'];
726 $arr['type'] = 'activity';
727 $arr['wall'] = $parent_item['wall'];
728 $arr['gravity'] = GRAVITY_LIKE;
729 $arr['parent'] = $parent_item['id'];
730 $arr['parent-uri'] = $parent_item['uri'];
732 $datarray['owner-name'] = $contact['name'];
733 $datarray['owner-link'] = $contact['url'];
734 $datarray['owner-avatar'] = $contact['thumb'];
736 $datarray['author-name'] = $person['name'];
737 $datarray['author-link'] = $person['url'];
738 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
740 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
741 $alink = '[url=' . $parent_item['author-link'] . ']' . $parent_item['author-name'] . '[/url]';
742 $plink = '[url=' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . ']' . $post_type . '[/url]';
743 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
745 $arr['private'] = $parent_item['private'];
746 $arr['verb'] = $activity;
747 $arr['object-type'] = $objtype;
748 $arr['object'] = $obj;
751 $arr['last-child'] = 0;
753 $message_id = item_store($arr);
755 if(! $parent_author_signature) {
756 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
758 dbesc($author_signed_data),
759 dbesc(base64_encode($author_signature)),
760 dbesc($diaspora_handle)
764 // FIXME send notification
769 function diaspora_retraction($importer,$xml) {
771 $guid = notags(unxmlify($xml->guid));
772 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
774 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
778 // if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
779 // logger('diaspora_retraction: Ignoring this author.');
780 // http_status_exit(202);
788 function diaspora_share($me,$contact) {
790 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
791 $theiraddr = $contact['addr'];
793 logger('diaspora_share: contact: ' . print_r($contact,true), LOGGER_DATA);
794 $tpl = get_markup_template('diaspora_share.tpl');
795 $msg = replace_macros($tpl, array(
796 '$sender' => $myaddr,
797 '$recipient' => $theiraddr
800 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
802 post_url($contact['notify'],$slap);
803 $return_code = $a->get_curl_code();
807 function diaspora_send_status($item,$owner,$contact) {
810 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
811 $theiraddr = $contact['addr'];
812 require_once('include/bbcode.php');
814 $body = xmlify(bbcode($item['body']));
815 $public = (($item['private']) ? 'false' : 'true');
817 require_once('include/datetime.php');
818 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d h:i:s \U\T\C');
820 $tpl = get_markup_template('diaspora_post.tpl');
821 $msg = replace_macros($tpl, array(
823 '$guid' => $item['guid'],
824 '$handle' => xmlify($myaddr),
825 '$public' => $public,
826 '$created' => $created
829 logger('diaspora_send_status: base message: ' . $msg, LOGGER_DATA);
831 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
833 post_url($contact['notify'],$slap);
834 $return_code = $a->get_curl_code();
835 logger('diaspora_send_status: returns: ' . $return_code);
840 function diaspora_send_followup($item,$owner,$contact) {
843 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
844 $theiraddr = $contact['addr'];
846 $p = q("select guid from item where parent = %d limit 1",
850 $parent_guid = $p[0]['guid'];
854 if($item['verb'] === ACTIVITY_LIKE) {
855 $tpl = get_markup_template('diaspora_like.tpl');
857 $target_type = 'Post';
858 $positive = (($item['deleted']) ? 'false' : 'true');
861 $tpl = get_markup_template('diaspora_comment.tpl');
865 $text = bbcode($item['body']);
870 $signed_text = $item['guid'] . ';' . $target_type . ';' . $positive . ';' . $myaddr;
872 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
874 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey']));
876 $msg = replace_macros($tpl,array(
877 '$guid' => xmlify($item['guid']),
878 '$parent_guid' => xmlify($parent_guid),
879 '$target_type' =>xmlify($target_type),
880 '$authorsig' => xmlify($authorsig),
881 '$body' => xmlify($text),
882 '$positive' => xmlify($positive),
883 '$handle' => xmlify($myaddr)
886 logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
888 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
890 post_url($contact['notify'],$slap);
891 $return_code = $a->get_curl_code();
892 logger('diaspora_send_status: returns: ' . $return_code);
898 function diaspora_send_relay($item,$owner,$contact) {
902 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
903 $theiraddr = $contact['addr'];
906 $p = q("select guid from item where parent = %d limit 1",
910 $parent_guid = $p[0]['guid'];
914 // fetch the original signature
915 $r = q("select * from sign where iid = %d limit 1",
922 if($item['verb'] === ACTIVITY_LIKE) {
923 $tpl = get_markup_template('diaspora_like_relay.tpl');
925 $target_type = 'Post';
926 $positive = (($item['deleted']) ? 'false' : 'true');
929 $tpl = get_markup_template('diaspora_comment_relay.tpl');
933 $text = bbcode($item['body']);
938 $parent_signed_text = $orig_sign['signed_text'];
940 $parent_signed_text = $orig_sign['signed_text'];
942 $parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey']));
944 $msg = replace_macros($tpl,array(
945 '$guid' => xmlify($item['guid']),
946 '$parent_guid' => xmlify($parent_guid),
947 '$target_type' =>xmlify($target_type),
948 '$authorsig' => xmlify($orig_sign['signature']),
949 '$parentsig' => xmlify($parentauthorsig),
950 '$text' => xmlify($text),
951 '$positive' => xmlify($positive),
952 '$diaspora_handle' => xmlify($myaddr)
955 // fetch the original signature
956 $r = q("select * from sign where iid = %d limit 1",
962 logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
964 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
966 post_url($contact['notify'],$slap);
967 $return_code = $a->get_curl_code();
968 logger('diaspora_send_status: returns: ' . $return_code);
975 function diaspora_send_retraction($item,$owner,$contact) {