3 require_once('include/crypto.php');
4 require_once('include/items.php');
5 require_once('include/bb2diaspora.php');
6 require_once('include/contact_selectors.php');
7 require_once('include/queue_fn.php');
10 function diaspora_dispatch_public($msg) {
12 $r = q("SELECT `user`.* FROM `user` WHERE `user`.`uid` IN ( SELECT `contact`.`uid` FROM `contact` WHERE `contact`.`network` = '%s' AND `contact`.`addr` = '%s' ) AND `account_expired` = 0 ",
13 dbesc(NETWORK_DIASPORA),
18 logger('diaspora_public: delivering to: ' . $rr['username']);
19 diaspora_dispatch($rr,$msg);
23 logger('diaspora_public: no subscribers');
28 function diaspora_dispatch($importer,$msg) {
32 // php doesn't like dashes in variable names
34 $msg['message'] = str_replace(
35 array('<activity_streams-photo>','</activity_streams-photo>'),
36 array('<asphoto>','</asphoto>'),
40 $parsed_xml = parse_xml_string($msg['message'],false);
42 $xmlbase = $parsed_xml->post;
44 if($xmlbase->request) {
45 $ret = diaspora_request($importer,$xmlbase->request);
47 elseif($xmlbase->status_message) {
48 $ret = diaspora_post($importer,$xmlbase->status_message);
50 elseif($xmlbase->profile) {
51 $ret = diaspora_profile($importer,$xmlbase->profile);
53 elseif($xmlbase->comment) {
54 $ret = diaspora_comment($importer,$xmlbase->comment,$msg);
56 elseif($xmlbase->like) {
57 $ret = diaspora_like($importer,$xmlbase->like,$msg);
59 elseif($xmlbase->asphoto) {
60 $ret = diaspora_asphoto($importer,$xmlbase->asphoto);
62 elseif($xmlbase->reshare) {
63 $ret = diaspora_reshare($importer,$xmlbase->reshare);
65 elseif($xmlbase->retraction) {
66 $ret = diaspora_retraction($importer,$xmlbase->retraction,$msg);
68 elseif($xmlbase->photo) {
69 $ret = diaspora_photo($importer,$xmlbase->photo,$msg);
72 logger('diaspora_dispatch: unknown message type: ' . print_r($xmlbase,true));
77 function diaspora_get_contact_by_handle($uid,$handle) {
78 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `addr` = '%s' LIMIT 1",
79 dbesc(NETWORK_DIASPORA),
88 function find_diaspora_person_by_handle($handle) {
90 $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
91 dbesc(NETWORK_DIASPORA),
95 // update record occasionally so it doesn't get stale
96 $d = strtotime($r[0]['updated'] . ' +00:00');
97 if($d > strtotime('now - 14 days'))
101 require_once('include/Scrape.php');
102 $r = probe_url($handle, PROBE_DIASPORA);
103 if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
104 add_fcontact($r,$update);
111 function get_diaspora_key($uri) {
112 logger('Fetching diaspora key for: ' . $uri);
114 $r = find_diaspora_person_by_handle($uri);
121 function diaspora_pubmsg_build($msg,$user,$contact,$prvkey,$pubkey) {
124 logger('diaspora_pubmsg_build: ' . $msg, LOGGER_DATA);
127 $handle = $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
129 // $b64_data = base64_encode($msg);
130 // $b64url_data = base64url_encode($b64_data);
132 $b64url_data = base64url_encode($msg);
134 $data = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
136 $type = 'application/xml';
137 $encoding = 'base64url';
140 $signable_data = $data . '.' . base64url_encode($type) . '.'
141 . base64url_encode($encoding) . '.' . base64url_encode($alg) ;
143 $signature = rsa_sign($signable_data,$prvkey);
144 $sig = base64url_encode($signature);
147 <?xml version='1.0' encoding='UTF-8'?>
148 <diaspora xmlns="https://joindiaspora.com/protocol" xmlns:me="http://salmon-protocol.org/ns/magic-env" >
150 <author_id>$handle</author_id>
153 <me:encoding>base64url</me:encoding>
154 <me:alg>RSA-SHA256</me:alg>
155 <me:data type="application/xml">$data</me:data>
156 <me:sig>$sig</me:sig>
161 logger('diaspora_pubmsg_build: magic_env: ' . $magic_env, LOGGER_DATA);
169 function diaspora_msg_build($msg,$user,$contact,$prvkey,$pubkey,$public = false) {
173 return diaspora_pubmsg_build($msg,$user,$contact,$prvkey,$pubkey);
175 logger('diaspora_msg_build: ' . $msg, LOGGER_DATA);
177 $inner_aes_key = random_string(32);
178 $b_inner_aes_key = base64_encode($inner_aes_key);
179 $inner_iv = random_string(16);
180 $b_inner_iv = base64_encode($inner_iv);
182 $outer_aes_key = random_string(32);
183 $b_outer_aes_key = base64_encode($outer_aes_key);
184 $outer_iv = random_string(16);
185 $b_outer_iv = base64_encode($outer_iv);
187 $handle = $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
189 $padded_data = pkcs5_pad($msg,16);
190 $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
192 $b64_data = base64_encode($inner_encrypted);
195 $b64url_data = base64url_encode($b64_data);
196 $data = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
198 $type = 'application/xml';
199 $encoding = 'base64url';
202 $signable_data = $data . '.' . base64url_encode($type) . '.'
203 . base64url_encode($encoding) . '.' . base64url_encode($alg) ;
205 $signature = rsa_sign($signable_data,$prvkey);
206 $sig = base64url_encode($signature);
208 $decrypted_header = <<< EOT
211 <aes_key>$b_inner_aes_key</aes_key>
212 <author_id>$handle</author_id>
216 $decrypted_header = pkcs5_pad($decrypted_header,16);
218 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
220 $outer_json = json_encode(array('iv' => $b_outer_iv,'key' => $b_outer_aes_key));
222 $encrypted_outer_key_bundle = '';
223 openssl_public_encrypt($outer_json,$encrypted_outer_key_bundle,$pubkey);
225 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
227 logger('outer_bundle: ' . $b64_encrypted_outer_key_bundle . ' key: ' . $pubkey, LOGGER_DATA);
229 $encrypted_header_json_object = json_encode(array('aes_key' => base64_encode($encrypted_outer_key_bundle),
230 'ciphertext' => base64_encode($ciphertext)));
231 $cipher_json = base64_encode($encrypted_header_json_object);
233 $encrypted_header = '<encrypted_header>' . $cipher_json . '</encrypted_header>';
236 <?xml version='1.0' encoding='UTF-8'?>
237 <diaspora xmlns="https://joindiaspora.com/protocol" xmlns:me="http://salmon-protocol.org/ns/magic-env" >
240 <me:encoding>base64url</me:encoding>
241 <me:alg>RSA-SHA256</me:alg>
242 <me:data type="application/xml">$data</me:data>
243 <me:sig>$sig</me:sig>
248 logger('diaspora_msg_build: magic_env: ' . $magic_env, LOGGER_DATA);
255 * diaspora_decode($importer,$xml)
256 * array $importer -> from user table
257 * string $xml -> urldecoded Diaspora salmon
260 * 'message' -> decoded Diaspora XML message
261 * 'author' -> author diaspora handle
262 * 'key' -> author public key (converted to pkcs#8)
264 * Author and key are used elsewhere to save a lookup for verifying replies and likes
268 function diaspora_decode($importer,$xml) {
271 $basedom = parse_xml_string($xml);
273 $children = $basedom->children('https://joindiaspora.com/protocol');
275 if($children->header) {
277 $author_link = str_replace('acct:','',$children->header->author_id);
281 $encrypted_header = json_decode(base64_decode($children->encrypted_header));
283 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
284 $ciphertext = base64_decode($encrypted_header->ciphertext);
286 $outer_key_bundle = '';
287 openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
289 $j_outer_key_bundle = json_decode($outer_key_bundle);
291 $outer_iv = base64_decode($j_outer_key_bundle->iv);
292 $outer_key = base64_decode($j_outer_key_bundle->key);
294 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
297 $decrypted = pkcs5_unpad($decrypted);
300 * $decrypted now contains something like
303 * <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
304 * <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
309 * <name>Ryan Hughes</name>
310 * <uri>acct:galaxor@diaspora.pirateship.org</uri>
315 * <author_id>galaxor@diaspora.priateship.org</author_id>
319 * </decrypted_header>
322 logger('decrypted: ' . $decrypted, LOGGER_DEBUG);
323 $idom = parse_xml_string($decrypted,false);
325 $inner_iv = base64_decode($idom->iv);
326 $inner_aes_key = base64_decode($idom->aes_key);
328 $author_link = str_replace('acct:','',$idom->author_id);
332 $dom = $basedom->children(NAMESPACE_SALMON_ME);
334 // figure out where in the DOM tree our data is hiding
336 if($dom->provenance->data)
337 $base = $dom->provenance;
338 elseif($dom->env->data)
344 logger('mod-diaspora: unable to locate salmon data in xml ');
345 http_status_exit(400);
349 // Stash the signature away for now. We have to find their key or it won't be good for anything.
350 $signature = base64url_decode($base->sig);
354 // strip whitespace so our data element will return to one big base64 blob
355 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
358 // stash away some other stuff for later
360 $type = $base->data[0]->attributes()->type[0];
361 $keyhash = $base->sig[0]->attributes()->keyhash[0];
362 $encoding = $base->encoding;
366 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
370 $data = base64url_decode($data);
374 $inner_decrypted = $data;
378 // Decode the encrypted blob
380 $inner_encrypted = base64_decode($data);
381 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
382 $inner_decrypted = pkcs5_unpad($inner_decrypted);
386 logger('mod-diaspora: Could not retrieve author URI.');
387 http_status_exit(400);
390 // Once we have the author URI, go to the web and try to find their public key
391 // (first this will look it up locally if it is in the fcontact cache)
392 // This will also convert diaspora public key from pkcs#1 to pkcs#8
394 logger('mod-diaspora: Fetching key for ' . $author_link );
395 $key = get_diaspora_key($author_link);
398 logger('mod-diaspora: Could not retrieve author key.');
399 http_status_exit(400);
402 $verify = rsa_verify($signed_data,$signature,$key);
405 logger('mod-diaspora: Message did not verify. Discarding.');
406 http_status_exit(400);
409 logger('mod-diaspora: Message verified.');
411 return array('message' => $inner_decrypted, 'author' => $author_link, 'key' => $key);
416 function diaspora_request($importer,$xml) {
418 $sender_handle = unxmlify($xml->sender_handle);
419 $recipient_handle = unxmlify($xml->recipient_handle);
421 if(! $sender_handle || ! $recipient_handle)
424 $contact = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
428 // perhaps we were already sharing with this person. Now they're sharing with us.
429 // That makes us friends.
431 if($contact['rel'] == CONTACT_IS_FOLLOWER) {
432 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
433 intval(CONTACT_IS_FRIEND),
434 intval($contact['id']),
435 intval($importer['uid'])
438 // send notification?
442 $ret = find_diaspora_person_by_handle($sender_handle);
445 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
446 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
450 $batch = (($ret['batch']) ? $ret['batch'] : implode('/', array_slice(explode('/',$ret['url']),0,3)) . '/receive/public');
452 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
453 VALUES ( %d, '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d) ",
454 intval($importer['uid']),
455 dbesc($ret['network']),
462 dbesc($ret['photo']),
463 dbesc($ret['pubkey']),
464 dbesc($ret['notify']),
470 // find the contact record we just created
472 $contact_record = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
474 $hash = random_string() . (string) time(); // Generate a confirm_key
476 if($contact_record) {
477 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime` )
478 VALUES ( %d, %d, %d, %d, '%s', '%s', '%s' )",
479 intval($importer['uid']),
480 intval($contact_record['id']),
483 dbesc( t('Sharing notification from Diaspora network')),
485 dbesc(datetime_convert())
492 function diaspora_post($importer,$xml) {
495 $guid = notags(unxmlify($xml->guid));
496 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
498 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
502 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
503 logger('diaspora_post: Ignoring this author.');
507 $message_id = $diaspora_handle . ':' . $guid;
508 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
509 intval($importer['uid']),
514 logger('diaspora_post: message exists: ' . $guid);
518 // allocate a guid on our system - we aren't fixing any collisions.
519 // we're ignoring them
521 $g = q("select * from guid where guid = '%s' limit 1",
525 q("insert into guid ( guid ) values ( '%s' )",
530 $created = unxmlify($xml->created_at);
531 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
533 $body = diaspora2bb($xml->raw_message);
539 $tags = get_tags($body);
542 foreach($tags as $tag) {
543 if(strpos($tag,'#') === 0) {
544 if(strpos($tag,'[url='))
546 $basetag = str_replace('_',' ',substr($tag,1));
547 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
548 if(strlen($str_tags))
550 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
556 $datarray['uid'] = $importer['uid'];
557 $datarray['contact-id'] = $contact['id'];
558 $datarray['wall'] = 0;
559 $datarray['guid'] = $guid;
560 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
561 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
562 $datarray['private'] = $private;
563 $datarray['parent'] = 0;
564 $datarray['owner-name'] = $contact['name'];
565 $datarray['owner-link'] = $contact['url'];
566 $datarray['owner-avatar'] = $contact['thumb'];
567 $datarray['author-name'] = $contact['name'];
568 $datarray['author-link'] = $contact['url'];
569 $datarray['author-avatar'] = $contact['thumb'];
570 $datarray['body'] = $body;
571 $datarray['tag'] = $str_tags;
572 $datarray['app'] = 'Diaspora';
574 // if empty content it might be a photo that hasn't arrived yet. If a photo arrives, we'll make it visible.
576 $datarray['visible'] = ((strlen($body)) ? 1 : 0);
578 $message_id = item_store($datarray);
581 q("update item set plink = '%s' where id = %d limit 1",
582 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
591 function diaspora_reshare($importer,$xml) {
593 logger('diaspora_reshare: init: ' . print_r($xml,true));
596 $guid = notags(unxmlify($xml->guid));
597 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
600 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
604 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
605 logger('diaspora_reshare: Ignoring this author: ' . $diaspora_handle . ' ' . print_r($xml,true));
609 $message_id = $diaspora_handle . ':' . $guid;
610 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
611 intval($importer['uid']),
616 logger('diaspora_reshare: message exists: ' . $guid);
620 $orig_author = notags(unxmlify($xml->root_diaspora_id));
621 $orig_guid = notags(unxmlify($xml->root_guid));
623 $source_url = 'https://' . substr($orig_author,strpos($orig_author,'@')+1) . '/p/' . $orig_guid . '.xml';
624 $x = fetch_url($source_url);
626 $x = fetch_url(str_replace('https://','http://',$source_url));
628 logger('diaspora_reshare: unable to fetch source url ' . $source_url);
631 logger('diaspora_reshare: source: ' . $x);
633 $x = str_replace(array('<activity_streams-photo>','</activity_streams-photo>'),array('<asphoto>','</asphoto>'),$x);
634 $source_xml = parse_xml_string($x,false);
636 if(strlen($source_xml->post->asphoto->objectId) && ($source_xml->post->asphoto->objectId != 0) && ($source_xml->post->asphoto->image_url))
637 $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
638 elseif($source_xml->post->asphoto->image_url)
639 $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
640 elseif($source_xml->post->status_message) {
641 $body = diaspora2bb($source_xml->post->status_message->raw_message);
644 logger('diaspora_reshare: no reshare content found: ' . print_r($source_xml,true));
648 logger('diaspora_reshare: empty body: source= ' . $x);
652 $person = find_diaspora_person_by_handle($orig_author);
654 if(is_array($person) && x($person,'name') && x($person,'url'))
655 $details = '[url=' . $person['url'] . ']' . $person['name'] . '[/url]';
657 $details = $orig_author;
659 $prefix = '♲ ' . $details . "\n";
662 // allocate a guid on our system - we aren't fixing any collisions.
663 // we're ignoring them
665 $g = q("select * from guid where guid = '%s' limit 1",
669 q("insert into guid ( guid ) values ( '%s' )",
674 $created = unxmlify($xml->created_at);
675 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
681 $tags = get_tags($body);
684 foreach($tags as $tag) {
685 if(strpos($tag,'#') === 0) {
686 if(strpos($tag,'[url='))
688 $basetag = str_replace('_',' ',substr($tag,1));
689 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
690 if(strlen($str_tags))
692 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
698 $datarray['uid'] = $importer['uid'];
699 $datarray['contact-id'] = $contact['id'];
700 $datarray['wall'] = 0;
701 $datarray['guid'] = $guid;
702 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
703 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
704 $datarray['private'] = $private;
705 $datarray['parent'] = 0;
706 $datarray['owner-name'] = $contact['name'];
707 $datarray['owner-link'] = $contact['url'];
708 $datarray['owner-avatar'] = $contact['thumb'];
709 $datarray['author-name'] = $contact['name'];
710 $datarray['author-link'] = $contact['url'];
711 $datarray['author-avatar'] = $contact['thumb'];
712 $datarray['body'] = $prefix . $body;
713 $datarray['tag'] = $str_tags;
714 $datarray['app'] = 'Diaspora';
716 $message_id = item_store($datarray);
719 q("update item set plink = '%s' where id = %d limit 1",
720 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
730 function diaspora_asphoto($importer,$xml) {
731 logger('diaspora_asphoto called');
734 $guid = notags(unxmlify($xml->guid));
735 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
737 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
741 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
742 logger('diaspora_asphoto: Ignoring this author.');
746 $message_id = $diaspora_handle . ':' . $guid;
747 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
748 intval($importer['uid']),
753 logger('diaspora_asphoto: message exists: ' . $guid);
757 // allocate a guid on our system - we aren't fixing any collisions.
758 // we're ignoring them
760 $g = q("select * from guid where guid = '%s' limit 1",
764 q("insert into guid ( guid ) values ( '%s' )",
769 $created = unxmlify($xml->created_at);
770 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
772 if(strlen($xml->objectId) && ($xml->objectId != 0) && ($xml->image_url))
773 $body = '[url=' . notags(unxmlify($xml->image_url)) . '][img]' . notags(unxmlify($xml->objectId)) . '[/img][/url]' . "\n";
774 elseif($xml->image_url)
775 $body = '[img]' . notags(unxmlify($xml->image_url)) . '[/img]' . "\n";
777 logger('diaspora_asphoto: no photo url found.');
785 $datarray['uid'] = $importer['uid'];
786 $datarray['contact-id'] = $contact['id'];
787 $datarray['wall'] = 0;
788 $datarray['guid'] = $guid;
789 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
790 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
791 $datarray['private'] = $private;
792 $datarray['parent'] = 0;
793 $datarray['owner-name'] = $contact['name'];
794 $datarray['owner-link'] = $contact['url'];
795 $datarray['owner-avatar'] = $contact['thumb'];
796 $datarray['author-name'] = $contact['name'];
797 $datarray['author-link'] = $contact['url'];
798 $datarray['author-avatar'] = $contact['thumb'];
799 $datarray['body'] = $body;
801 $datarray['app'] = 'Diaspora/Cubbi.es';
803 $message_id = item_store($datarray);
806 q("update item set plink = '%s' where id = %d limit 1",
807 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
821 function diaspora_comment($importer,$xml,$msg) {
824 $guid = notags(unxmlify($xml->guid));
825 $parent_guid = notags(unxmlify($xml->parent_guid));
826 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
827 $target_type = notags(unxmlify($xml->target_type));
828 $text = unxmlify($xml->text);
829 $author_signature = notags(unxmlify($xml->author_signature));
831 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
835 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
837 logger('diaspora_comment: cannot find contact: ' . $msg['author']);
841 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
842 logger('diaspora_comment: Ignoring this author.');
846 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
847 intval($importer['uid']),
851 logger('diaspora_comment: our comment just got relayed back to us (or there was a guid collision) : ' . $guid);
855 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
856 intval($importer['uid']),
860 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
863 $parent_item = $r[0];
865 $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
867 $author_signature = base64_decode($author_signature);
869 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
874 $person = find_diaspora_person_by_handle($diaspora_handle);
876 if(is_array($person) && x($person,'pubkey'))
877 $key = $person['pubkey'];
879 logger('diaspora_comment: unable to find author details');
884 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
885 logger('diaspora_comment: verification failed.');
889 if($parent_author_signature) {
890 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
892 $parent_author_signature = base64_decode($parent_author_signature);
896 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
897 logger('diaspora_comment: owner verification failed.');
902 // Phew! Everything checks out. Now create an item.
904 $body = diaspora2bb($text);
906 $message_id = $diaspora_handle . ':' . $guid;
912 $tags = get_tags($body);
915 foreach($tags as $tag) {
916 if(strpos($tag,'#') === 0) {
917 if(strpos($tag,'[url='))
919 $basetag = str_replace('_',' ',substr($tag,1));
920 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
921 if(strlen($str_tags))
923 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
929 $datarray['uid'] = $importer['uid'];
930 $datarray['contact-id'] = $contact['id'];
931 $datarray['wall'] = $parent_item['wall'];
932 $datarray['gravity'] = GRAVITY_COMMENT;
933 $datarray['guid'] = $guid;
934 $datarray['uri'] = $message_id;
935 $datarray['parent-uri'] = $parent_item['uri'];
937 // No timestamps for comments? OK, we'll the use current time.
938 $datarray['created'] = $datarray['edited'] = datetime_convert();
939 $datarray['private'] = $parent_item['private'];
941 $datarray['owner-name'] = $parent_item['owner-name'];
942 $datarray['owner-link'] = $parent_item['owner-link'];
943 $datarray['owner-avatar'] = $parent_item['owner-avatar'];
945 $datarray['author-name'] = $person['name'];
946 $datarray['author-link'] = $person['url'];
947 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
948 $datarray['body'] = $body;
949 $datarray['tag'] = $str_tags;
950 $datarray['app'] = 'Diaspora';
952 $message_id = item_store($datarray);
955 q("update item set plink = '%s' where id = %d limit 1",
956 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
961 if(($parent_item['origin']) && (! $parent_author_signature)) {
962 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
964 dbesc($author_signed_data),
965 dbesc(base64_encode($author_signature)),
966 dbesc($diaspora_handle)
969 // if the message isn't already being relayed, notify others
970 // the existence of parent_author_signature means the parent_author or owner
971 // is already relaying.
973 proc_run('php','include/notifier.php','comment',$message_id);
978 function diaspora_photo($importer,$xml,$msg) {
981 $remote_photo_path = notags(unxmlify($xml->remote_photo_path));
983 $remote_photo_name = notags(unxmlify($xml->remote_photo_name));
985 $status_message_guid = notags(unxmlify($xml->status_message_guid));
987 $guid = notags(unxmlify($xml->guid));
989 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
991 $public = notags(unxmlify($xml->public));
993 $created_at = notags(unxmlify($xml_created_at));
996 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1000 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
1001 logger('diaspora_photo: Ignoring this author.');
1005 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1006 intval($importer['uid']),
1007 dbesc($status_message_guid)
1010 logger('diaspora_photo: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
1013 $parent_item = $r[0];
1015 $link_text = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n";
1017 if(strpos($parent_item['body'],$link_text) === false) {
1018 $r = q("update item set `body` = '%s', `visible` = 1 where `id` = %d and `uid` = %d limit 1",
1019 dbesc($link_text . $parent_item['body']),
1020 intval($parent_item['id']),
1021 intval($parent_item['uid'])
1031 function diaspora_like($importer,$xml,$msg) {
1034 $guid = notags(unxmlify($xml->guid));
1035 $parent_guid = notags(unxmlify($xml->parent_guid));
1036 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1037 $target_type = notags(unxmlify($xml->target_type));
1038 $positive = notags(unxmlify($xml->positive));
1039 $author_signature = notags(unxmlify($xml->author_signature));
1041 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
1043 // likes on comments not supported here and likes on photos not supported by Diaspora
1045 if($target_type !== 'Post')
1048 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1050 logger('diaspora_like: cannot find contact: ' . $msg['author']);
1054 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
1055 logger('diaspora_like: Ignoring this author.');
1059 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1060 intval($importer['uid']),
1064 logger('diaspora_like: parent item not found: ' . $guid);
1068 $parent_item = $r[0];
1070 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1071 intval($importer['uid']),
1075 if($positive === 'true') {
1076 logger('diaspora_like: duplicate like: ' . $guid);
1079 if($positive === 'false') {
1080 q("UPDATE `item` SET `deleted` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
1081 intval($r[0]['id']),
1082 intval($importer['uid'])
1085 // send notification via proc_run()
1089 if($positive === 'false') {
1090 logger('diaspora_like: unlike received with no corresponding like');
1094 $author_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
1096 $author_signature = base64_decode($author_signature);
1098 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
1103 $person = find_diaspora_person_by_handle($diaspora_handle);
1104 if(is_array($person) && x($person,'pubkey'))
1105 $key = $person['pubkey'];
1107 logger('diaspora_like: unable to find author details');
1112 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
1113 logger('diaspora_like: verification failed.');
1117 if($parent_author_signature) {
1119 $owner_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
1121 $parent_author_signature = base64_decode($parent_author_signature);
1125 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
1126 logger('diaspora_like: owner verification failed.');
1131 // Phew! Everything checks out. Now create an item.
1133 $uri = $diaspora_handle . ':' . $guid;
1135 $activity = ACTIVITY_LIKE;
1136 $post_type = (($parent_item['resource-id']) ? t('photo') : t('status'));
1137 $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
1138 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . '" />' . "\n") ;
1139 $body = $parent_item['body'];
1144 <type>$objtype</type>
1146 <id>{$parent_item['uri']}</id>
1149 <content>$body</content>
1152 $bodyverb = t('%1$s likes %2$s\'s %3$s');
1157 $arr['uid'] = $importer['uid'];
1158 $arr['guid'] = $guid;
1159 $arr['contact-id'] = $contact['id'];
1160 $arr['type'] = 'activity';
1161 $arr['wall'] = $parent_item['wall'];
1162 $arr['gravity'] = GRAVITY_LIKE;
1163 $arr['parent'] = $parent_item['id'];
1164 $arr['parent-uri'] = $parent_item['uri'];
1166 $arr['owner-name'] = $parent_item['name'];
1167 $arr['owner-link'] = $parent_item['url'];
1168 $arr['owner-avatar'] = $parent_item['thumb'];
1170 $arr['author-name'] = $person['name'];
1171 $arr['author-link'] = $person['url'];
1172 $arr['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
1174 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
1175 $alink = '[url=' . $parent_item['author-link'] . ']' . $parent_item['author-name'] . '[/url]';
1176 $plink = '[url=' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . ']' . $post_type . '[/url]';
1177 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
1179 $arr['app'] = 'Diaspora';
1181 $arr['private'] = $parent_item['private'];
1182 $arr['verb'] = $activity;
1183 $arr['object-type'] = $objtype;
1184 $arr['object'] = $obj;
1185 $arr['visible'] = 1;
1187 $arr['last-child'] = 0;
1189 $message_id = item_store($arr);
1193 q("update item set plink = '%s' where id = %d limit 1",
1194 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
1199 if(! $parent_author_signature) {
1200 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1201 intval($message_id),
1202 dbesc($author_signed_data),
1203 dbesc(base64_encode($author_signature)),
1204 dbesc($diaspora_handle)
1208 // if the message isn't already being relayed, notify others
1209 // the existence of parent_author_signature means the parent_author or owner
1210 // is already relaying. The parent_item['origin'] indicates the message was created on our system
1212 if(($parent_item['origin']) && (! $parent_author_signature))
1213 proc_run('php','include/notifier.php','comment',$message_id);
1218 function diaspora_retraction($importer,$xml) {
1220 $guid = notags(unxmlify($xml->guid));
1221 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1222 $type = notags(unxmlify($xml->type));
1224 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1228 if($type === 'Person') {
1229 contact_remove($contact['id']);
1231 elseif($type === 'Post') {
1232 $r = q("select * from item where guid = '%s' and uid = %d limit 1",
1234 intval($importer['uid'])
1237 if(link_compare($r[0]['author-link'],$contact['url'])) {
1238 q("update item set `deleted` = 1, `changed` = '%s' where `id` = %d limit 1",
1239 dbesc(datetime_convert()),
1250 function diaspora_profile($importer,$xml) {
1253 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1255 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1259 if($contact['blocked']) {
1260 logger('diaspora_post: Ignoring this author.');
1264 $name = unxmlify($xml->first_name) . ((strlen($xml->last_name)) ? ' ' . unxmlify($xml->last_name) : '');
1265 $image_url = unxmlify($xml->image_url);
1266 $birthday = unxmlify($xml->birthday);
1268 $r = q("SELECT DISTINCT ( `resource-id` ) FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `album` = 'Contact Photos' ",
1269 intval($importer['uid']),
1270 intval($contact['id'])
1272 $oldphotos = ((count($r)) ? $r : null);
1274 require_once('include/Photo.php');
1276 $images = import_profile_photo($image_url,$importer['uid'],$contact['id']);
1278 // Generic birthday. We don't know the timezone. The year is irrelevant.
1280 $birthday = str_replace('1000','1901',$birthday);
1282 $birthday = datetime_convert('UTC','UTC',$birthday,'Y-m-d');
1284 $r = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s', `avatar-date` = '%s' , `bd` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
1286 dbesc(datetime_convert()),
1290 dbesc(datetime_convert()),
1292 intval($contact['id']),
1293 intval($importer['uid'])
1298 foreach($oldphotos as $ph) {
1299 q("DELETE FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `album` = 'Contact Photos' AND `resource-id` = '%s' ",
1300 intval($importer['uid']),
1301 intval($contact['id']),
1302 dbesc($ph['resource-id'])
1333 function diaspora_share($me,$contact) {
1335 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1336 $theiraddr = $contact['addr'];
1338 $tpl = get_markup_template('diaspora_share.tpl');
1339 $msg = replace_macros($tpl, array(
1340 '$sender' => $myaddr,
1341 '$recipient' => $theiraddr
1344 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
1346 return(diaspora_transmit($owner,$contact,$slap, false));
1349 function diaspora_unshare($me,$contact) {
1352 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1354 $tpl = get_markup_template('diaspora_retract.tpl');
1355 $msg = replace_macros($tpl, array(
1356 '$guid' => $me['guid'],
1357 '$type' => 'Person',
1358 '$handle' => $myaddr
1361 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
1363 return(diaspora_transmit($owner,$contact,$slap, false));
1369 function diaspora_send_status($item,$owner,$contact,$public_batch = false) {
1372 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1373 $theiraddr = $contact['addr'];
1377 $body = $item['body'];
1380 $cnt = preg_match_all('|\[img\](.*?)\[\/img\]|',$body,$matches,PREG_SET_ORDER);
1382 foreach($matches as $mtch) {
1384 $detail['str'] = $mtch[0];
1385 $detail['path'] = dirname($mtch[1]) . '/';
1386 $detail['file'] = basename($mtch[1]);
1387 $detail['guid'] = $item['guid'];
1388 $detail['handle'] = $myaddr;
1389 $images[] = $detail;
1390 $body = str_replace($detail['str'],$mtch[1],$body);
1395 $body = xmlify(html_entity_decode(bb2diaspora($body)));
1397 $public = (($item['private']) ? 'false' : 'true');
1399 require_once('include/datetime.php');
1400 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d H:i:s \U\T\C');
1402 $tpl = get_markup_template('diaspora_post.tpl');
1403 $msg = replace_macros($tpl, array(
1405 '$guid' => $item['guid'],
1406 '$handle' => xmlify($myaddr),
1407 '$public' => $public,
1408 '$created' => $created
1411 logger('diaspora_send_status: ' . $owner['username'] . ' -> ' . $contact['name'] . ' base message: ' . $msg, LOGGER_DATA);
1413 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1415 $return_code = diaspora_transmit($owner,$contact,$slap,$public_batch);
1417 if(count($images)) {
1418 diaspora_send_images($item,$owner,$contact,$images,$public_batch);
1421 return $return_code;
1425 function diaspora_send_images($item,$owner,$contact,$images,$public_batch = false) {
1427 if(! count($images))
1429 $mysite = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://') + 3) . '/photo';
1431 $tpl = get_markup_template('diaspora_photo.tpl');
1432 foreach($images as $image) {
1433 if(! stristr($image['path'],$mysite))
1435 $resource = str_replace('.jpg','',$image['file']);
1436 $resource = substr($resource,0,strpos($resource,'-'));
1438 $r = q("select * from photo where `resource-id` = '%s' and `uid` = %d limit 1",
1440 intval($owner['uid'])
1444 $public = (($r[0]['allow_cid'] || $r[0]['allow_gid'] || $r[0]['deny_cid'] || $r[0]['deny_gid']) ? 'false' : 'true' );
1445 $msg = replace_macros($tpl,array(
1446 '$path' => xmlify($image['path']),
1447 '$filename' => xmlify($image['file']),
1448 '$msg_guid' => xmlify($image['guid']),
1449 '$guid' => xmlify($r[0]['guid']),
1450 '$handle' => xmlify($image['handle']),
1451 '$public' => xmlify($public),
1452 '$created_at' => xmlify(datetime_convert('UTC','UTC',$r[0]['created'],'Y-m-d H:i:s \U\T\C'))
1456 logger('diaspora_send_photo: base message: ' . $msg, LOGGER_DATA);
1457 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1459 diaspora_transmit($owner,$contact,$slap,$public_batch);
1464 function diaspora_send_followup($item,$owner,$contact,$public_batch = false) {
1467 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1468 $theiraddr = $contact['addr'];
1470 $p = q("select guid from item where parent = %d limit 1",
1474 $parent_guid = $p[0]['guid'];
1478 if($item['verb'] === ACTIVITY_LIKE) {
1479 $tpl = get_markup_template('diaspora_like.tpl');
1481 $target_type = 'Post';
1482 $positive = (($item['deleted']) ? 'false' : 'true');
1485 $tpl = get_markup_template('diaspora_comment.tpl');
1489 $text = html_entity_decode(bb2diaspora($item['body']));
1494 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
1496 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
1498 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1500 $msg = replace_macros($tpl,array(
1501 '$guid' => xmlify($item['guid']),
1502 '$parent_guid' => xmlify($parent_guid),
1503 '$target_type' =>xmlify($target_type),
1504 '$authorsig' => xmlify($authorsig),
1505 '$body' => xmlify($text),
1506 '$positive' => xmlify($positive),
1507 '$handle' => xmlify($myaddr)
1510 logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
1512 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1514 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1518 function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
1522 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1523 $theiraddr = $contact['addr'];
1526 $p = q("select guid from item where parent = %d limit 1",
1530 $parent_guid = $p[0]['guid'];
1534 if($item['verb'] === ACTIVITY_LIKE) {
1535 $tpl = get_markup_template('diaspora_like_relay.tpl');
1537 $target_type = 'Post';
1538 $positive = (($item['deleted']) ? 'false' : 'true');
1541 $tpl = get_markup_template('diaspora_comment_relay.tpl');
1545 $body = $item['body'];
1547 $text = html_entity_decode(bb2diaspora($body));
1549 // fetch the original signature if somebody sent the post to us to relay
1550 // If we are relaying for a reply originating on our own account, there wasn't a 'send to relay'
1551 // action. It wasn't needed. In that case create the original signature and the
1552 // owner (parent author) signature
1553 // comments from other networks will be relayed under our name, with a brief
1554 // preamble to describe what's happening and noting the real author
1556 $r = q("select * from sign where iid = %d limit 1",
1561 $signed_text = $orig_sign['signed_text'];
1562 $authorsig = $orig_sign['signature'];
1563 $handle = $orig_sign['signer'];
1567 $itemcontact = q("select * from contact where `id` = %d limit 1",
1568 intval($item['contact-id'])
1570 if(count($itemcontact)) {
1571 if(! $itemcontact[0]['self']) {
1572 $prefix = sprintf( t('[Relayed] Comment authored by %s from network %s'),
1573 '['. $item['author-name'] . ']' . '(' . $item['author-link'] . ')',
1574 network_to_name($itemcontact['network'])) . "\n";
1575 $body = $prefix . $body;
1581 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
1583 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
1585 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1587 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1588 intval($item['id']),
1589 dbesc($signed_text),
1590 dbesc(base64_encode($authorsig)),
1599 $parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1601 $msg = replace_macros($tpl,array(
1602 '$guid' => xmlify($item['guid']),
1603 '$parent_guid' => xmlify($parent_guid),
1604 '$target_type' =>xmlify($target_type),
1605 '$authorsig' => xmlify($orig_sign['signature']),
1606 '$parentsig' => xmlify($parentauthorsig),
1607 '$body' => xmlify($text),
1608 '$positive' => xmlify($positive),
1609 '$handle' => xmlify($handle)
1612 logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
1614 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1616 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1622 function diaspora_send_retraction($item,$owner,$contact,$public_batch = false) {
1625 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1627 $tpl = get_markup_template('diaspora_retract.tpl');
1628 $msg = replace_macros($tpl, array(
1629 '$guid' => $item['guid'],
1631 '$handle' => $myaddr
1634 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1636 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1641 function diaspora_transmit($owner,$contact,$slap,$public_batch) {
1644 $logid = random_string(4);
1645 logger('diaspora_transmit: ' . $logid . ' ' . (($public_batch) ? $contact['batch'] : $contact['notify']));
1646 post_url((($public_batch) ? $contact['batch'] : $contact['notify']) . '/',$slap);
1647 $return_code = $a->get_curl_code();
1648 logger('diaspora_transmit: ' . $logid . ' returns: ' . $return_code);
1650 if((! $return_code) || (($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))) {
1651 logger('diaspora_transmit: queue message');
1652 // queue message for redelivery
1653 add_to_queue($contact['id'],NETWORK_DIASPORA,$slap,$public_batch);
1657 return(($return_code) ? $return_code : (-1));