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));
81 $encrypted_outer_key_bundle = '';
82 openssl_public_encrypt($outer_json,$encrypted_outer_key_bundle,$pubkey);
84 logger('outer_bundle_encrypt: ' . openssl_error_string());
85 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
87 logger('outer_bundle: ' . $b64_encrypted_outer_key_bundle . ' key: ' . $pubkey);
89 $encrypted_header_json_object = json_encode(array('aes_key' => base64_encode($encrypted_outer_key_bundle),
90 'ciphertext' => base64_encode($ciphertext)));
91 $cipher_json = base64_encode($encrypted_header_json_object);
93 $encrypted_header = '<encrypted_header>' . $cipher_json . '</encrypted_header>';
96 <?xml version='1.0' encoding='UTF-8'?>
97 <entry xmlns='http://www.w3.org/2005/Atom'>
99 <me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
100 <me:encoding>base64url</me:encoding>
101 <me:alg>RSA-SHA256</me:alg>
102 <me:data type="application/atom+xml">$data</me:data>
103 <me:sig>$sig</me:sig>
108 logger('diaspora_msg_build: magic_env: ' . $magic_env, LOGGER_DATA);
115 * diaspora_decode($importer,$xml)
116 * array $importer -> from user table
117 * string $xml -> urldecoded Diaspora salmon
120 * 'message' -> decoded Diaspora XML message
121 * 'author' -> author diaspora handle
122 * 'key' -> author public key (converted to pkcs#8)
124 * Author and key are used elsewhere to save a lookup for verifying replies and likes
128 function diaspora_decode($importer,$xml) {
130 $basedom = parse_xml_string($xml);
132 $atom = $basedom->children(NAMESPACE_ATOM1);
134 // Diaspora devs: This is kind of sucky - 'encrypted_header' does not belong in the atom namespace
136 $encrypted_header = json_decode(base64_decode($atom->encrypted_header));
138 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
139 $ciphertext = base64_decode($encrypted_header->ciphertext);
141 $outer_key_bundle = '';
142 openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
144 $j_outer_key_bundle = json_decode($outer_key_bundle);
146 $outer_iv = base64_decode($j_outer_key_bundle->iv);
147 $outer_key = base64_decode($j_outer_key_bundle->key);
149 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
152 $decrypted = pkcs5_unpad($decrypted);
155 * $decrypted now contains something like
158 * <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
159 * <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
161 * <name>Ryan Hughes</name>
162 * <uri>acct:galaxor@diaspora.pirateship.org</uri>
164 * </decrypted_header>
167 logger('decrypted: ' . $decrypted);
168 $idom = parse_xml_string($decrypted,false);
170 $inner_iv = base64_decode($idom->iv);
171 $inner_aes_key = base64_decode($idom->aes_key);
173 $author_link = str_replace('acct:','',$idom->author->uri);
175 $dom = $basedom->children(NAMESPACE_SALMON_ME);
177 // figure out where in the DOM tree our data is hiding
179 if($dom->provenance->data)
180 $base = $dom->provenance;
181 elseif($dom->env->data)
187 logger('mod-diaspora: unable to locate salmon data in xml ');
188 http_status_exit(400);
192 // Stash the signature away for now. We have to find their key or it won't be good for anything.
193 $signature = base64url_decode($base->sig);
197 // strip whitespace so our data element will return to one big base64 blob
198 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
200 // Add back the 60 char linefeeds
202 // This completely violates the entire principle of salmon magic signatures,
203 // which was to have a message signing format that was completely ambivalent to linefeeds
204 // and transport whitespace mangling, and base64 wrapping rules. Guess what? PHP and Ruby
205 // use different linelengths for base64 output.
207 $lines = str_split($data,60);
208 $data = implode("\n",$lines);
211 // stash away some other stuff for later
213 $type = $base->data[0]->attributes()->type[0];
214 $keyhash = $base->sig[0]->attributes()->keyhash[0];
215 $encoding = $base->encoding;
218 // I can't even begin to tell you how sucky this is. Please read the spec.
220 $signed_data = $data . (($data[-1] != "\n") ? "\n" : '') . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
224 $data = base64url_decode($data);
226 // Now pull out the inner encrypted blob
228 $inner_encrypted = base64_decode($data);
231 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
233 $inner_decrypted = pkcs5_unpad($inner_decrypted);
236 logger('mod-diaspora: Could not retrieve author URI.');
237 http_status_exit(400);
240 // Once we have the author URI, go to the web and try to find their public key
241 // (first this will look it up locally if it is in the fcontact cache)
242 // This will also convert diaspora public key from pkcs#1 to pkcs#8
244 logger('mod-diaspora: Fetching key for ' . $author_link );
245 $key = get_diaspora_key($author_link);
248 logger('mod-diaspora: Could not retrieve author key.');
249 http_status_exit(400);
252 $verify = rsa_verify($signed_data,$signature,$key);
255 logger('mod-diaspora: Message did not verify. Discarding.');
256 http_status_exit(400);
259 logger('mod-diaspora: Message verified.');
261 return array('message' => $inner_decrypted, 'author' => $author_link, 'key' => $key);
265 function diaspora_get_contact_by_handle($uid,$handle) {
266 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `addr` = '%s' LIMIT 1",
267 dbesc(NETWORK_DIASPORA),
276 function find_diaspora_person_by_handle($handle) {
277 $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
278 dbesc(NETWORK_DIASPORA),
282 // update record occasionally so it doesn't get stale
283 $d = strtotime($r[0]['updated'] . ' +00:00');
284 if($d < strtotime('now - 14 days')) {
285 q("delete from fcontact where id = %d limit 1",
292 require_once('include/Scrape.php');
293 $r = probe_url($handle, PROBE_DIASPORA);
294 if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
303 function diaspora_request($importer,$xml) {
305 $sender_handle = unxmlify($xml->sender_handle);
306 $recipient_handle = unxmlify($xml->recipient_handle);
308 if(! $sender_handle || ! $recipient_handle)
311 $contact = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
316 // perhaps we were already sharing with this person. Now they're sharing with us.
317 // That makes us friends.
319 if($contact['rel'] == CONTACT_IS_FOLLOWER) {
320 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
321 intval(CONTACT_IS_FRIEND),
322 intval($contact['id']),
323 intval($importer['uid'])
326 // send notification?
330 $ret = find_diaspora_person_by_handle($sender_handle);
333 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
334 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
338 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
339 VALUES ( %d, '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s',%d,%d) ",
340 intval($importer['uid']),
341 dbesc($ret['network']),
347 dbesc($ret['photo']),
348 dbesc($ret['pubkey']),
349 dbesc($ret['notify']),
355 // find the contact record we just created
357 $contact_record = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
359 $hash = random_string() . (string) time(); // Generate a confirm_key
361 if($contact_record) {
362 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime` )
363 VALUES ( %d, %d, %d, %d, '%s', '%s', '%s' )",
364 intval($importer['uid']),
365 intval($contact_record['id']),
368 dbesc( t('Sharing notification from Diaspora network')),
370 dbesc(datetime_convert())
377 function diaspora_post($importer,$xml) {
379 $guid = notags(unxmlify($xml->guid));
380 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
382 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
386 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
387 logger('diaspora_post: Ignoring this author.');
388 http_status_exit(202);
392 $message_id = $diaspora_handle . ':' . $guid;
393 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
394 intval($importer['uid']),
401 // allocate a guid on our system - we aren't fixing any collisions.
402 // we're ignoring them
404 $g = q("select * from guid where guid = '%s' limit 1",
408 q("insert into guid ( guid ) values ( '%s' )",
413 $created = unxmlify($xml->created_at);
414 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
416 $body = unxmlify($xml->raw_message);
418 require_once('library/HTMLPurifier.auto.php');
419 require_once('include/html2bbcode.php');
421 $maxlen = get_max_import_size();
422 if($maxlen && (strlen($body) > $maxlen))
423 $body = substr($body,0, $maxlen);
425 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
427 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
428 '[youtube]$1[/youtube]', $body);
430 $body = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
431 '[youtube]$1[/youtube]', $body);
433 $body = oembed_html2bbcode($body);
435 $config = HTMLPurifier_Config::createDefault();
436 $config->set('Cache.DefinitionImpl', null);
437 $purifier = new HTMLPurifier($config);
438 $body = $purifier->purify($body);
440 $body = html2bbcode($body);
444 $datarray['uid'] = $importer['uid'];
445 $datarray['contact-id'] = $contact['id'];
446 $datarray['wall'] = 0;
447 $datarray['guid'] = $guid;
448 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
449 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
450 $datarray['private'] = $private;
451 $datarray['parent'] = 0;
452 $datarray['owner-name'] = $contact['name'];
453 $datarray['owner-link'] = $contact['url'];
454 $datarray['owner-avatar'] = $contact['thumb'];
455 $datarray['author-name'] = $contact['name'];
456 $datarray['author-link'] = $contact['url'];
457 $datarray['author-avatar'] = $contact['thumb'];
458 $datarray['body'] = $body;
460 item_store($datarray);
466 function diaspora_comment($importer,$xml,$msg) {
468 $guid = notags(unxmlify($xml->guid));
469 $parent_guid = notags(unxmlify($xml->parent_guid));
470 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
471 $target_type = notags(unxmlify($xml->target_type));
472 $text = unxmlify($xml->text);
473 $author_signature = notags(unxmlify($xml->author_signature));
475 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
479 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
483 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
484 logger('diaspora_comment: Ignoring this author.');
485 http_status_exit(202);
489 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
490 intval($importer['uid']),
494 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
497 $parent_item = $r[0];
499 $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
501 $author_signature = base64_decode($author_signature);
503 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
508 $person = find_diaspora_person_by_handle($diaspora_handle);
510 if(is_array($person) && x($person,'pubkey'))
511 $key = $person['pubkey'];
513 logger('diaspora_comment: unable to find author details');
518 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha')) {
519 logger('diaspora_comment: verification failed.');
524 if($parent_author_signature) {
525 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $msg['author'];
527 $parent_author_signature = base64_decode($parent_author_signature);
531 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha')) {
532 logger('diaspora_comment: owner verification failed.');
537 // Phew! Everything checks out. Now create an item.
539 require_once('library/HTMLPurifier.auto.php');
540 require_once('include/html2bbcode.php');
544 $maxlen = get_max_import_size();
545 if($maxlen && (strlen($body) > $maxlen))
546 $body = substr($body,0, $maxlen);
548 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
550 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
551 '[youtube]$1[/youtube]', $body);
553 $body = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
554 '[youtube]$1[/youtube]', $body);
556 $body = oembed_html2bbcode($body);
558 $config = HTMLPurifier_Config::createDefault();
559 $config->set('Cache.DefinitionImpl', null);
560 $purifier = new HTMLPurifier($config);
561 $body = $purifier->purify($body);
563 $body = html2bbcode($body);
566 $message_id = $diaspora_handle . ':' . $guid;
569 $datarray['uid'] = $importer['uid'];
570 $datarray['contact-id'] = $contact['id'];
571 $datarray['wall'] = $parent_item['wall'];
572 $datarray['gravity'] = GRAVITY_COMMENT;
573 $datarray['guid'] = $guid;
574 $datarray['uri'] = $message_id;
575 $datarray['parent-uri'] = $parent_item['uri'];
577 // No timestamps for comments? OK, we'll the use current time.
578 $datarray['created'] = $datarray['edited'] = datetime_convert();
579 $datarray['private'] = $parent_item['private'];
581 $datarray['owner-name'] = $contact['name'];
582 $datarray['owner-link'] = $contact['url'];
583 $datarray['owner-avatar'] = $contact['thumb'];
585 $datarray['author-name'] = $person['name'];
586 $datarray['author-link'] = $person['url'];
587 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
588 $datarray['body'] = $body;
590 $message_id = item_store($datarray);
592 if(! $parent_author_signature) {
593 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
595 dbesc($author_signed_data),
596 dbesc(base64_encode($author_signature)),
597 dbesc($diaspora_handle)
606 function diaspora_like($importer,$xml,$msg) {
609 $guid = notags(unxmlify($xml->guid));
610 $parent_guid = notags(unxmlify($xml->parent_guid));
611 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
612 $target_type = notags(unxmlify($xml->target_type));
613 $positive = notags(unxmlify($xml->positive));
614 $author_signature = notags(unxmlify($xml->author_signature));
616 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
618 // likes on comments not supported here and likes on photos not supported by Diaspora
620 if($target_type !== 'Post')
623 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
627 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
628 logger('diaspora_like: Ignoring this author.');
629 http_status_exit(202);
633 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
634 intval($importer['uid']),
638 logger('diaspora_like: parent item not found: ' . $guid);
642 $parent_item = $r[0];
644 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
645 intval($importer['uid']),
649 if($positive === 'true') {
650 logger('diaspora_like: duplicate like: ' . $guid);
653 if($positive === 'false') {
654 q("UPDATE `item` SET `deleted` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
656 intval($importer['uid'])
659 // send notification via proc_run()
663 if($positive === 'false') {
664 logger('diaspora_like: unlike received with no corresponding like');
668 $author_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
670 $author_signature = base64_decode($author_signature);
672 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
677 $person = find_diaspora_person_by_handle($diaspora_handle);
678 if(is_array($person) && x($person,'pubkey'))
679 $key = $person['pubkey'];
681 logger('diaspora_like: unable to find author details');
686 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha')) {
687 logger('diaspora_like: verification failed.');
691 if($parent_author_signature) {
692 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $target_type . ';' . $positive . ';' . $msg['author'];
694 $parent_author_signature = base64_decode($parent_author_signature);
698 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha')) {
699 logger('diaspora_like: owner verification failed.');
704 // Phew! Everything checks out. Now create an item.
706 $uri = $diaspora_handle . ':' . $guid;
708 $activity = ACTIVITY_LIKE;
709 $post_type = (($parent_item['resource-id']) ? t('photo') : t('status'));
710 $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
711 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . '" />' . "\n") ;
712 $body = $parent_item['body'];
717 <type>$objtype</type>
719 <id>{$parent_item['uri']}</id>
722 <content>$body</content>
725 $bodyverb = t('%1$s likes %2$s\'s %3$s');
730 $arr['uid'] = $importer['uid'];
731 $arr['guid'] = $guid;
732 $arr['contact-id'] = $contact['id'];
733 $arr['type'] = 'activity';
734 $arr['wall'] = $parent_item['wall'];
735 $arr['gravity'] = GRAVITY_LIKE;
736 $arr['parent'] = $parent_item['id'];
737 $arr['parent-uri'] = $parent_item['uri'];
739 $arr['owner-name'] = $contact['name'];
740 $arr['owner-link'] = $contact['url'];
741 $arr['owner-avatar'] = $contact['thumb'];
743 $arr['author-name'] = $person['name'];
744 $arr['author-link'] = $person['url'];
745 $arr['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
747 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
748 $alink = '[url=' . $parent_item['author-link'] . ']' . $parent_item['author-name'] . '[/url]';
749 $plink = '[url=' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . ']' . $post_type . '[/url]';
750 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
752 $arr['private'] = $parent_item['private'];
753 $arr['verb'] = $activity;
754 $arr['object-type'] = $objtype;
755 $arr['object'] = $obj;
758 $arr['last-child'] = 0;
760 $message_id = item_store($arr);
762 if(! $parent_author_signature) {
763 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
765 dbesc($author_signed_data),
766 dbesc(base64_encode($author_signature)),
767 dbesc($diaspora_handle)
771 // FIXME send notification
776 function diaspora_retraction($importer,$xml) {
778 $guid = notags(unxmlify($xml->guid));
779 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
781 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
785 // if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
786 // logger('diaspora_retraction: Ignoring this author.');
787 // http_status_exit(202);
795 function diaspora_share($me,$contact) {
797 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
798 $theiraddr = $contact['addr'];
800 $tpl = get_markup_template('diaspora_share.tpl');
801 $msg = replace_macros($tpl, array(
802 '$sender' => $myaddr,
803 '$recipient' => $theiraddr
806 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
808 post_url($contact['notify'] . '/',$slap);
809 $return_code = $a->get_curl_code();
810 logger('diaspora_send_share: returns: ' . $return_code);
814 function diaspora_send_status($item,$owner,$contact) {
817 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
818 $theiraddr = $contact['addr'];
819 require_once('include/bbcode.php');
821 $body = xmlify(bbcode($item['body']));
822 $public = (($item['private']) ? 'false' : 'true');
824 require_once('include/datetime.php');
825 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d h:i:s \U\T\C');
827 $tpl = get_markup_template('diaspora_post.tpl');
828 $msg = replace_macros($tpl, array(
830 '$guid' => $item['guid'],
831 '$handle' => xmlify($myaddr),
832 '$public' => $public,
833 '$created' => $created
836 logger('diaspora_send_status: base message: ' . $msg, LOGGER_DATA);
838 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
840 post_url($contact['notify'] . '/',$slap);
841 $return_code = $a->get_curl_code();
842 logger('diaspora_send_status: returns: ' . $return_code);
847 function diaspora_send_followup($item,$owner,$contact) {
850 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
851 $theiraddr = $contact['addr'];
853 $p = q("select guid from item where parent = %d limit 1",
857 $parent_guid = $p[0]['guid'];
861 if($item['verb'] === ACTIVITY_LIKE) {
862 $tpl = get_markup_template('diaspora_like.tpl');
864 $target_type = 'Post';
865 $positive = (($item['deleted']) ? 'false' : 'true');
868 $tpl = get_markup_template('diaspora_comment.tpl');
872 $text = bbcode($item['body']);
877 $signed_text = $item['guid'] . ';' . $target_type . ';' . $positive . ';' . $myaddr;
879 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
881 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey']),'sha');
883 $msg = replace_macros($tpl,array(
884 '$guid' => xmlify($item['guid']),
885 '$parent_guid' => xmlify($parent_guid),
886 '$target_type' =>xmlify($target_type),
887 '$authorsig' => xmlify($authorsig),
888 '$body' => xmlify($text),
889 '$positive' => xmlify($positive),
890 '$handle' => xmlify($myaddr)
893 logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
895 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
897 post_url($contact['notify'] . '/',$slap);
898 $return_code = $a->get_curl_code();
899 logger('diaspora_send_followup: returns: ' . $return_code);
905 function diaspora_send_relay($item,$owner,$contact) {
909 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
910 $theiraddr = $contact['addr'];
913 $p = q("select guid from item where parent = %d limit 1",
917 $parent_guid = $p[0]['guid'];
921 // fetch the original signature
922 $r = q("select * from sign where iid = %d limit 1",
929 if($item['verb'] === ACTIVITY_LIKE) {
930 $tpl = get_markup_template('diaspora_like_relay.tpl');
932 $target_type = 'Post';
933 $positive = (($item['deleted']) ? 'false' : 'true');
936 $tpl = get_markup_template('diaspora_comment_relay.tpl');
940 $text = bbcode($item['body']);
945 $parent_signed_text = $orig_sign['signed_text'];
947 $parent_signed_text = $orig_sign['signed_text'];
949 $parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha'));
951 $msg = replace_macros($tpl,array(
952 '$guid' => xmlify($item['guid']),
953 '$parent_guid' => xmlify($parent_guid),
954 '$target_type' =>xmlify($target_type),
955 '$authorsig' => xmlify($orig_sign['signature']),
956 '$parentsig' => xmlify($parentauthorsig),
957 '$text' => xmlify($text),
958 '$positive' => xmlify($positive),
959 '$diaspora_handle' => xmlify($myaddr)
962 // fetch the original signature
963 $r = q("select * from sign where iid = %d limit 1",
969 logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
971 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
973 post_url($contact['notify'] . '/',$slap);
974 $return_code = $a->get_curl_code();
975 logger('diaspora_send_relay: returns: ' . $return_code);
982 function diaspora_send_retraction($item,$owner,$contact) {