3 require_once('include/crypto.php');
4 require_once('include/items.php');
5 require_once('include/bb2diaspora.php');
7 function diaspora_get_contact_by_handle($uid,$handle) {
8 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `addr` = '%s' LIMIT 1",
9 dbesc(NETWORK_DIASPORA),
18 function find_diaspora_person_by_handle($handle) {
19 $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
20 dbesc(NETWORK_DIASPORA),
24 // update record occasionally so it doesn't get stale
25 $d = strtotime($r[0]['updated'] . ' +00:00');
26 if($d < strtotime('now - 14 days')) {
27 q("delete from fcontact where id = %d limit 1",
34 require_once('include/Scrape.php');
35 $r = probe_url($handle, PROBE_DIASPORA);
36 if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
44 function get_diaspora_key($uri) {
45 logger('Fetching diaspora key for: ' . $uri);
47 $r = find_diaspora_person_by_handle($uri);
54 function diaspora_msg_build($msg,$user,$contact,$prvkey,$pubkey) {
57 logger('diaspora_msg_build: ' . $msg, LOGGER_DATA);
59 $inner_aes_key = random_string(32);
60 $b_inner_aes_key = base64_encode($inner_aes_key);
61 $inner_iv = random_string(16);
62 $b_inner_iv = base64_encode($inner_iv);
64 $outer_aes_key = random_string(32);
65 $b_outer_aes_key = base64_encode($outer_aes_key);
66 $outer_iv = random_string(16);
67 $b_outer_iv = base64_encode($outer_iv);
69 $handle = 'acct:' . $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
71 $padded_data = pkcs5_pad($msg,16);
72 $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
74 $b64_data = base64_encode($inner_encrypted);
77 $b64url_data = base64url_encode($b64_data);
78 $b64url_stripped = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
79 $lines = str_split($b64url_stripped,60);
80 $data = implode("\n",$lines);
81 $data = $data . (($data[-1] != "\n") ? "\n" : '') ;
82 $type = 'application/atom+xml';
83 $encoding = 'base64url';
86 $signable_data = $data . '.' . base64url_encode($type) . "\n" . '.'
87 . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
89 $signature = rsa_sign($signable_data,$prvkey);
90 $sig = base64url_encode($signature);
92 $decrypted_header = <<< EOT
95 <aes_key>$b_inner_aes_key</aes_key>
97 <name>{$user['username']}</name>
103 $decrypted_header = pkcs5_pad($decrypted_header,16);
105 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
107 $outer_json = json_encode(array('iv' => $b_outer_iv,'key' => $b_outer_aes_key));
109 $encrypted_outer_key_bundle = '';
110 openssl_public_encrypt($outer_json,$encrypted_outer_key_bundle,$pubkey);
112 logger('outer_bundle_encrypt: ' . openssl_error_string());
113 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
115 logger('outer_bundle: ' . $b64_encrypted_outer_key_bundle . ' key: ' . $pubkey);
117 $encrypted_header_json_object = json_encode(array('aes_key' => base64_encode($encrypted_outer_key_bundle),
118 'ciphertext' => base64_encode($ciphertext)));
119 $cipher_json = base64_encode($encrypted_header_json_object);
121 $encrypted_header = '<encrypted_header>' . $cipher_json . '</encrypted_header>';
124 <?xml version='1.0' encoding='UTF-8'?>
125 <entry xmlns='http://www.w3.org/2005/Atom'>
127 <me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
128 <me:encoding>base64url</me:encoding>
129 <me:alg>RSA-SHA256</me:alg>
130 <me:data type="application/atom+xml">$data</me:data>
131 <me:sig>$sig</me:sig>
136 logger('diaspora_msg_build: magic_env: ' . $magic_env, LOGGER_DATA);
143 * diaspora_decode($importer,$xml)
144 * array $importer -> from user table
145 * string $xml -> urldecoded Diaspora salmon
148 * 'message' -> decoded Diaspora XML message
149 * 'author' -> author diaspora handle
150 * 'key' -> author public key (converted to pkcs#8)
152 * Author and key are used elsewhere to save a lookup for verifying replies and likes
156 function diaspora_decode($importer,$xml) {
158 $basedom = parse_xml_string($xml);
160 $atom = $basedom->children(NAMESPACE_ATOM1);
162 // Diaspora devs: This is kind of sucky - 'encrypted_header' does not belong in the atom namespace
164 $encrypted_header = json_decode(base64_decode($atom->encrypted_header));
166 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
167 $ciphertext = base64_decode($encrypted_header->ciphertext);
169 $outer_key_bundle = '';
170 openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
172 $j_outer_key_bundle = json_decode($outer_key_bundle);
174 $outer_iv = base64_decode($j_outer_key_bundle->iv);
175 $outer_key = base64_decode($j_outer_key_bundle->key);
177 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
180 $decrypted = pkcs5_unpad($decrypted);
183 * $decrypted now contains something like
186 * <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
187 * <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
189 * <name>Ryan Hughes</name>
190 * <uri>acct:galaxor@diaspora.pirateship.org</uri>
192 * </decrypted_header>
195 logger('decrypted: ' . $decrypted);
196 $idom = parse_xml_string($decrypted,false);
198 $inner_iv = base64_decode($idom->iv);
199 $inner_aes_key = base64_decode($idom->aes_key);
201 $author_link = str_replace('acct:','',$idom->author->uri);
203 $dom = $basedom->children(NAMESPACE_SALMON_ME);
205 // figure out where in the DOM tree our data is hiding
207 if($dom->provenance->data)
208 $base = $dom->provenance;
209 elseif($dom->env->data)
215 logger('mod-diaspora: unable to locate salmon data in xml ');
216 http_status_exit(400);
220 // Stash the signature away for now. We have to find their key or it won't be good for anything.
221 $signature = base64url_decode($base->sig);
225 // strip whitespace so our data element will return to one big base64 blob
226 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
228 // Add back the 60 char linefeeds
230 // This completely violates the entire principle of salmon magic signatures,
231 // which was to have a message signing format that was completely ambivalent to linefeeds
232 // and transport whitespace mangling, and base64 wrapping rules. Guess what? PHP and Ruby
233 // use different linelengths for base64 output.
235 $lines = str_split($data,60);
236 $data = implode("\n",$lines);
239 // stash away some other stuff for later
241 $type = $base->data[0]->attributes()->type[0];
242 $keyhash = $base->sig[0]->attributes()->keyhash[0];
243 $encoding = $base->encoding;
246 // I can't even begin to tell you how sucky this is. Please read the spec.
248 $signed_data = $data . (($data[-1] != "\n") ? "\n" : '') . '.' . base64url_encode($type) . "\n" . '.' . base64url_encode($encoding) . "\n" . '.' . base64url_encode($alg) . "\n";
252 $data = base64url_decode($data);
254 // Now pull out the inner encrypted blob
256 $inner_encrypted = base64_decode($data);
259 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
261 $inner_decrypted = pkcs5_unpad($inner_decrypted);
264 logger('mod-diaspora: Could not retrieve author URI.');
265 http_status_exit(400);
268 // Once we have the author URI, go to the web and try to find their public key
269 // (first this will look it up locally if it is in the fcontact cache)
270 // This will also convert diaspora public key from pkcs#1 to pkcs#8
272 logger('mod-diaspora: Fetching key for ' . $author_link );
273 $key = get_diaspora_key($author_link);
276 logger('mod-diaspora: Could not retrieve author key.');
277 http_status_exit(400);
280 $verify = rsa_verify($signed_data,$signature,$key);
283 logger('mod-diaspora: Message did not verify. Discarding.');
284 http_status_exit(400);
287 logger('mod-diaspora: Message verified.');
289 return array('message' => $inner_decrypted, 'author' => $author_link, 'key' => $key);
295 function diaspora_request($importer,$xml) {
297 $sender_handle = unxmlify($xml->sender_handle);
298 $recipient_handle = unxmlify($xml->recipient_handle);
300 if(! $sender_handle || ! $recipient_handle)
303 $contact = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
308 // perhaps we were already sharing with this person. Now they're sharing with us.
309 // That makes us friends.
311 if($contact['rel'] == CONTACT_IS_FOLLOWER) {
312 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
313 intval(CONTACT_IS_FRIEND),
314 intval($contact['id']),
315 intval($importer['uid'])
318 // send notification?
322 $ret = find_diaspora_person_by_handle($sender_handle);
325 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
326 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
330 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
331 VALUES ( %d, '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s',%d,%d) ",
332 intval($importer['uid']),
333 dbesc($ret['network']),
339 dbesc($ret['photo']),
340 dbesc($ret['pubkey']),
341 dbesc($ret['notify']),
347 // find the contact record we just created
349 $contact_record = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
351 $hash = random_string() . (string) time(); // Generate a confirm_key
353 if($contact_record) {
354 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime` )
355 VALUES ( %d, %d, %d, %d, '%s', '%s', '%s' )",
356 intval($importer['uid']),
357 intval($contact_record['id']),
360 dbesc( t('Sharing notification from Diaspora network')),
362 dbesc(datetime_convert())
369 function diaspora_post($importer,$xml) {
371 $guid = notags(unxmlify($xml->guid));
372 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
374 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
378 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
379 logger('diaspora_post: Ignoring this author.');
380 http_status_exit(202);
384 $message_id = $diaspora_handle . ':' . $guid;
385 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
386 intval($importer['uid']),
393 // allocate a guid on our system - we aren't fixing any collisions.
394 // we're ignoring them
396 $g = q("select * from guid where guid = '%s' limit 1",
400 q("insert into guid ( guid ) values ( '%s' )",
405 $created = unxmlify($xml->created_at);
406 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
408 $body = unxmlify($xml->raw_message);
410 require_once('library/HTMLPurifier.auto.php');
411 require_once('include/html2bbcode.php');
413 $maxlen = get_max_import_size();
414 if($maxlen && (strlen($body) > $maxlen))
415 $body = substr($body,0, $maxlen);
417 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
419 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
420 '[youtube]$1[/youtube]', $body);
422 $body = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
423 '[youtube]$1[/youtube]', $body);
425 $body = oembed_html2bbcode($body);
427 $config = HTMLPurifier_Config::createDefault();
428 $config->set('Cache.DefinitionImpl', null);
429 $purifier = new HTMLPurifier($config);
430 $body = $purifier->purify($body);
432 $body = html2bbcode($body);
436 $datarray['uid'] = $importer['uid'];
437 $datarray['contact-id'] = $contact['id'];
438 $datarray['wall'] = 0;
439 $datarray['guid'] = $guid;
440 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
441 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
442 $datarray['private'] = $private;
443 $datarray['parent'] = 0;
444 $datarray['owner-name'] = $contact['name'];
445 $datarray['owner-link'] = $contact['url'];
446 $datarray['owner-avatar'] = $contact['thumb'];
447 $datarray['author-name'] = $contact['name'];
448 $datarray['author-link'] = $contact['url'];
449 $datarray['author-avatar'] = $contact['thumb'];
450 $datarray['body'] = $body;
452 item_store($datarray);
458 function diaspora_comment($importer,$xml,$msg) {
460 $guid = notags(unxmlify($xml->guid));
461 $parent_guid = notags(unxmlify($xml->parent_guid));
462 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
463 $target_type = notags(unxmlify($xml->target_type));
464 $text = unxmlify($xml->text);
465 $author_signature = notags(unxmlify($xml->author_signature));
467 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
471 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
475 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
476 logger('diaspora_comment: Ignoring this author.');
477 http_status_exit(202);
481 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
482 intval($importer['uid']),
486 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
489 $parent_item = $r[0];
491 $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
493 $author_signature = base64_decode($author_signature);
495 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
500 $person = find_diaspora_person_by_handle($diaspora_handle);
502 if(is_array($person) && x($person,'pubkey'))
503 $key = $person['pubkey'];
505 logger('diaspora_comment: unable to find author details');
510 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha')) {
511 logger('diaspora_comment: verification failed.');
516 if($parent_author_signature) {
517 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $msg['author'];
519 $parent_author_signature = base64_decode($parent_author_signature);
523 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha')) {
524 logger('diaspora_comment: owner verification failed.');
529 // Phew! Everything checks out. Now create an item.
531 require_once('library/HTMLPurifier.auto.php');
532 require_once('include/html2bbcode.php');
536 $maxlen = get_max_import_size();
537 if($maxlen && (strlen($body) > $maxlen))
538 $body = substr($body,0, $maxlen);
540 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
542 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
543 '[youtube]$1[/youtube]', $body);
545 $body = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
546 '[youtube]$1[/youtube]', $body);
548 $body = oembed_html2bbcode($body);
550 $config = HTMLPurifier_Config::createDefault();
551 $config->set('Cache.DefinitionImpl', null);
552 $purifier = new HTMLPurifier($config);
553 $body = $purifier->purify($body);
555 $body = html2bbcode($body);
558 $message_id = $diaspora_handle . ':' . $guid;
561 $datarray['uid'] = $importer['uid'];
562 $datarray['contact-id'] = $contact['id'];
563 $datarray['wall'] = $parent_item['wall'];
564 $datarray['gravity'] = GRAVITY_COMMENT;
565 $datarray['guid'] = $guid;
566 $datarray['uri'] = $message_id;
567 $datarray['parent-uri'] = $parent_item['uri'];
569 // No timestamps for comments? OK, we'll the use current time.
570 $datarray['created'] = $datarray['edited'] = datetime_convert();
571 $datarray['private'] = $parent_item['private'];
573 $datarray['owner-name'] = $contact['name'];
574 $datarray['owner-link'] = $contact['url'];
575 $datarray['owner-avatar'] = $contact['thumb'];
577 $datarray['author-name'] = $person['name'];
578 $datarray['author-link'] = $person['url'];
579 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
580 $datarray['body'] = $body;
582 $message_id = item_store($datarray);
584 if(! $parent_author_signature) {
585 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
587 dbesc($author_signed_data),
588 dbesc(base64_encode($author_signature)),
589 dbesc($diaspora_handle)
594 proc_run('php','include/notifier.php','comment',$message_id);
600 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 . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
664 $author_signature = base64_decode($author_signature);
666 if(strcasecmp($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_like: unable to find author details');
680 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha')) {
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,'sha')) {
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['guid'] = $guid;
726 $arr['contact-id'] = $contact['id'];
727 $arr['type'] = 'activity';
728 $arr['wall'] = $parent_item['wall'];
729 $arr['gravity'] = GRAVITY_LIKE;
730 $arr['parent'] = $parent_item['id'];
731 $arr['parent-uri'] = $parent_item['uri'];
733 $arr['owner-name'] = $contact['name'];
734 $arr['owner-link'] = $contact['url'];
735 $arr['owner-avatar'] = $contact['thumb'];
737 $arr['author-name'] = $person['name'];
738 $arr['author-link'] = $person['url'];
739 $arr['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
741 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
742 $alink = '[url=' . $parent_item['author-link'] . ']' . $parent_item['author-name'] . '[/url]';
743 $plink = '[url=' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . ']' . $post_type . '[/url]';
744 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
746 $arr['private'] = $parent_item['private'];
747 $arr['verb'] = $activity;
748 $arr['object-type'] = $objtype;
749 $arr['object'] = $obj;
752 $arr['last-child'] = 0;
754 $message_id = item_store($arr);
756 if(! $parent_author_signature) {
757 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
759 dbesc($author_signed_data),
760 dbesc(base64_encode($author_signature)),
761 dbesc($diaspora_handle)
766 proc_run('php','include/notifier.php','comment',$message_id);
771 function diaspora_retraction($importer,$xml) {
773 $guid = notags(unxmlify($xml->guid));
774 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
775 $type = notags(unxmlify($xml->type));
777 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
781 if($type === 'Person') {
782 contact_remove($contact['id']);
784 elseif($type === 'Post') {
785 $r = q("select * from item where guid = '%s' and uid = %d limit 1",
787 intval($importer['uid'])
790 if(link_compare($r[0]['author-link'],$contact['url'])) {
791 q("update item set `deleted` = 1, `changed` = '%s' where `id` = %d limit 1",
792 dbesc(datetime_convert()),
799 http_exit_status(202);
803 function diaspora_share($me,$contact) {
805 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
806 $theiraddr = $contact['addr'];
808 $tpl = get_markup_template('diaspora_share.tpl');
809 $msg = replace_macros($tpl, array(
810 '$sender' => $myaddr,
811 '$recipient' => $theiraddr
814 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
816 post_url($contact['notify'] . '/',$slap);
817 $return_code = $a->get_curl_code();
818 logger('diaspora_send_share: returns: ' . $return_code);
822 function diaspora_unshare($me,$contact) {
825 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
827 $tpl = get_markup_template('diaspora_retract.tpl');
828 $msg = replace_macros($tpl, array(
829 '$guid' => $me['guid'],
834 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
836 post_url($contact['notify'] . '/',$slap);
837 $return_code = $a->get_curl_code();
838 logger('diaspora_send_unshare: returns: ' . $return_code);
844 function diaspora_send_status($item,$owner,$contact) {
847 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
848 $theiraddr = $contact['addr'];
850 $body = xmlify(bb2diaspora($item['body']));
851 $public = (($item['private']) ? 'false' : 'true');
853 require_once('include/datetime.php');
854 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d h:i:s \U\T\C');
856 $tpl = get_markup_template('diaspora_post.tpl');
857 $msg = replace_macros($tpl, array(
859 '$guid' => $item['guid'],
860 '$handle' => xmlify($myaddr),
861 '$public' => $public,
862 '$created' => $created
865 logger('diaspora_send_status: base message: ' . $msg, LOGGER_DATA);
867 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
869 post_url($contact['notify'] . '/',$slap);
870 $return_code = $a->get_curl_code();
871 logger('diaspora_send_status: returns: ' . $return_code);
876 function diaspora_send_followup($item,$owner,$contact) {
879 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
880 $theiraddr = $contact['addr'];
882 $p = q("select guid from item where parent = %d limit 1",
886 $parent_guid = $p[0]['guid'];
890 if($item['verb'] === ACTIVITY_LIKE) {
891 $tpl = get_markup_template('diaspora_like.tpl');
893 $target_type = 'Post';
894 $positive = (($item['deleted']) ? 'false' : 'true');
897 $tpl = get_markup_template('diaspora_comment.tpl');
901 $text = bb2diaspora($item['body']);
906 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
908 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
910 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha'));
912 $msg = replace_macros($tpl,array(
913 '$guid' => xmlify($item['guid']),
914 '$parent_guid' => xmlify($parent_guid),
915 '$target_type' =>xmlify($target_type),
916 '$authorsig' => xmlify($authorsig),
917 '$body' => xmlify($text),
918 '$positive' => xmlify($positive),
919 '$handle' => xmlify($myaddr)
922 logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
924 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
926 post_url($contact['notify'] . '/',$slap);
927 $return_code = $a->get_curl_code();
928 logger('diaspora_send_followup: returns: ' . $return_code);
934 function diaspora_send_relay($item,$owner,$contact) {
938 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
939 $theiraddr = $contact['addr'];
942 $p = q("select guid from item where parent = %d limit 1",
946 $parent_guid = $p[0]['guid'];
950 // fetch the original signature
951 $r = q("select * from sign where iid = %d limit 1",
958 if($item['verb'] === ACTIVITY_LIKE) {
959 $tpl = get_markup_template('diaspora_like_relay.tpl');
961 $target_type = 'Post';
962 $positive = (($item['deleted']) ? 'false' : 'true');
965 $tpl = get_markup_template('diaspora_comment_relay.tpl');
969 $text = bb2diaspora($item['body']);
974 $parent_signed_text = $orig_sign['signed_text'];
976 $parent_signed_text = $orig_sign['signed_text'];
978 $parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha'));
980 $msg = replace_macros($tpl,array(
981 '$guid' => xmlify($item['guid']),
982 '$parent_guid' => xmlify($parent_guid),
983 '$target_type' =>xmlify($target_type),
984 '$authorsig' => xmlify($orig_sign['signature']),
985 '$parentsig' => xmlify($parentauthorsig),
986 '$text' => xmlify($text),
987 '$positive' => xmlify($positive),
988 '$diaspora_handle' => xmlify($myaddr)
991 // fetch the original signature
992 $r = q("select * from sign where iid = %d limit 1",
998 logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
1000 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
1002 post_url($contact['notify'] . '/',$slap);
1003 $return_code = $a->get_curl_code();
1004 logger('diaspora_send_relay: returns: ' . $return_code);
1005 return $return_code;
1011 function diaspora_send_retraction($item,$owner,$contact) {
1014 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1016 $tpl = get_markup_template('diaspora_retract.tpl');
1017 $msg = replace_macros($tpl, array(
1018 '$guid' => $item['guid'],
1020 '$handle' => $myaddr
1023 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'])));
1025 post_url($contact['notify'] . '/',$slap);
1026 $return_code = $a->get_curl_code();
1027 logger('diaspora_send_retraction: returns: ' . $return_code);
1028 return $return_code;