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');
9 function diaspora_dispatch_public($msg) {
11 $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 ",
12 dbesc(NETWORK_DIASPORA),
17 logger('diaspora_public: delivering to: ' . $rr['username']);
18 diaspora_dispatch($rr,$msg);
22 logger('diaspora_public: no subscribers');
27 function diaspora_dispatch($importer,$msg) {
31 // php doesn't like dashes in variable names
33 $msg['message'] = str_replace(
34 array('<activity_streams-photo>','</activity_streams-photo>'),
35 array('<asphoto>','</asphoto>'),
39 $parsed_xml = parse_xml_string($msg['message'],false);
41 $xmlbase = $parsed_xml->post;
43 if($xmlbase->request) {
44 $ret = diaspora_request($importer,$xmlbase->request);
46 elseif($xmlbase->status_message) {
47 $ret = diaspora_post($importer,$xmlbase->status_message);
49 elseif($xmlbase->profile) {
50 $ret = diaspora_profile($importer,$xmlbase->profile);
52 elseif($xmlbase->comment) {
53 $ret = diaspora_comment($importer,$xmlbase->comment,$msg);
55 elseif($xmlbase->like) {
56 $ret = diaspora_like($importer,$xmlbase->like,$msg);
58 elseif($xmlbase->asphoto) {
59 $ret = diaspora_asphoto($importer,$xmlbase->asphoto);
61 elseif($xmlbase->reshare) {
62 $ret = diaspora_reshare($importer,$xmlbase->reshare);
64 elseif($xmlbase->retraction) {
65 $ret = diaspora_retraction($importer,$xmlbase->retraction,$msg);
67 elseif($xmlbase->photo) {
68 $ret = diaspora_photo($importer,$xmlbase->photo,$msg);
71 logger('diaspora_dispatch: unknown message type: ' . print_r($xmlbase,true));
76 function diaspora_get_contact_by_handle($uid,$handle) {
77 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `addr` = '%s' LIMIT 1",
78 dbesc(NETWORK_DIASPORA),
87 function find_diaspora_person_by_handle($handle) {
89 $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
90 dbesc(NETWORK_DIASPORA),
94 // update record occasionally so it doesn't get stale
95 $d = strtotime($r[0]['updated'] . ' +00:00');
96 if($d > strtotime('now - 14 days'))
100 require_once('include/Scrape.php');
101 $r = probe_url($handle, PROBE_DIASPORA);
102 if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
103 add_fcontact($r,$update);
110 function get_diaspora_key($uri) {
111 logger('Fetching diaspora key for: ' . $uri);
113 $r = find_diaspora_person_by_handle($uri);
120 function diaspora_pubmsg_build($msg,$user,$contact,$prvkey,$pubkey) {
123 logger('diaspora_pubmsg_build: ' . $msg, LOGGER_DATA);
126 $handle = $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
128 // $b64_data = base64_encode($msg);
129 // $b64url_data = base64url_encode($b64_data);
131 $b64url_data = base64url_encode($msg);
133 $data = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
135 $type = 'application/xml';
136 $encoding = 'base64url';
139 $signable_data = $data . '.' . base64url_encode($type) . '.'
140 . base64url_encode($encoding) . '.' . base64url_encode($alg) ;
142 $signature = rsa_sign($signable_data,$prvkey);
143 $sig = base64url_encode($signature);
146 <?xml version='1.0' encoding='UTF-8'?>
147 <diaspora xmlns="https://joindiaspora.com/protocol" xmlns:me="http://salmon-protocol.org/ns/magic-env" >
149 <author_id>$handle</author_id>
152 <me:encoding>base64url</me:encoding>
153 <me:alg>RSA-SHA256</me:alg>
154 <me:data type="application/xml">$data</me:data>
155 <me:sig>$sig</me:sig>
160 logger('diaspora_pubmsg_build: magic_env: ' . $magic_env, LOGGER_DATA);
168 function diaspora_msg_build($msg,$user,$contact,$prvkey,$pubkey,$public = false) {
172 return diaspora_pubmsg_build($msg,$user,$contact,$prvkey,$pubkey);
174 logger('diaspora_msg_build: ' . $msg, LOGGER_DATA);
176 $inner_aes_key = random_string(32);
177 $b_inner_aes_key = base64_encode($inner_aes_key);
178 $inner_iv = random_string(16);
179 $b_inner_iv = base64_encode($inner_iv);
181 $outer_aes_key = random_string(32);
182 $b_outer_aes_key = base64_encode($outer_aes_key);
183 $outer_iv = random_string(16);
184 $b_outer_iv = base64_encode($outer_iv);
186 $handle = $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
188 $padded_data = pkcs5_pad($msg,16);
189 $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
191 $b64_data = base64_encode($inner_encrypted);
194 $b64url_data = base64url_encode($b64_data);
195 $data = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
197 $type = 'application/xml';
198 $encoding = 'base64url';
201 $signable_data = $data . '.' . base64url_encode($type) . '.'
202 . base64url_encode($encoding) . '.' . base64url_encode($alg) ;
204 $signature = rsa_sign($signable_data,$prvkey);
205 $sig = base64url_encode($signature);
207 $decrypted_header = <<< EOT
210 <aes_key>$b_inner_aes_key</aes_key>
211 <author_id>$handle</author_id>
215 $decrypted_header = pkcs5_pad($decrypted_header,16);
217 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
219 $outer_json = json_encode(array('iv' => $b_outer_iv,'key' => $b_outer_aes_key));
221 $encrypted_outer_key_bundle = '';
222 openssl_public_encrypt($outer_json,$encrypted_outer_key_bundle,$pubkey);
224 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
226 logger('outer_bundle: ' . $b64_encrypted_outer_key_bundle . ' key: ' . $pubkey, LOGGER_DATA);
228 $encrypted_header_json_object = json_encode(array('aes_key' => base64_encode($encrypted_outer_key_bundle),
229 'ciphertext' => base64_encode($ciphertext)));
230 $cipher_json = base64_encode($encrypted_header_json_object);
232 $encrypted_header = '<encrypted_header>' . $cipher_json . '</encrypted_header>';
235 <?xml version='1.0' encoding='UTF-8'?>
236 <diaspora xmlns="https://joindiaspora.com/protocol" xmlns:me="http://salmon-protocol.org/ns/magic-env" >
239 <me:encoding>base64url</me:encoding>
240 <me:alg>RSA-SHA256</me:alg>
241 <me:data type="application/xml">$data</me:data>
242 <me:sig>$sig</me:sig>
247 logger('diaspora_msg_build: magic_env: ' . $magic_env, LOGGER_DATA);
254 * diaspora_decode($importer,$xml)
255 * array $importer -> from user table
256 * string $xml -> urldecoded Diaspora salmon
259 * 'message' -> decoded Diaspora XML message
260 * 'author' -> author diaspora handle
261 * 'key' -> author public key (converted to pkcs#8)
263 * Author and key are used elsewhere to save a lookup for verifying replies and likes
267 function diaspora_decode($importer,$xml) {
270 $basedom = parse_xml_string($xml);
272 $children = $basedom->children('https://joindiaspora.com/protocol');
274 if($children->header) {
276 $author_link = str_replace('acct:','',$children->header->author_id);
280 $encrypted_header = json_decode(base64_decode($children->encrypted_header));
282 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
283 $ciphertext = base64_decode($encrypted_header->ciphertext);
285 $outer_key_bundle = '';
286 openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
288 $j_outer_key_bundle = json_decode($outer_key_bundle);
290 $outer_iv = base64_decode($j_outer_key_bundle->iv);
291 $outer_key = base64_decode($j_outer_key_bundle->key);
293 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
296 $decrypted = pkcs5_unpad($decrypted);
299 * $decrypted now contains something like
302 * <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
303 * <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
308 * <name>Ryan Hughes</name>
309 * <uri>acct:galaxor@diaspora.pirateship.org</uri>
314 * <author_id>galaxor@diaspora.priateship.org</author_id>
318 * </decrypted_header>
321 logger('decrypted: ' . $decrypted, LOGGER_DEBUG);
322 $idom = parse_xml_string($decrypted,false);
324 $inner_iv = base64_decode($idom->iv);
325 $inner_aes_key = base64_decode($idom->aes_key);
327 $author_link = str_replace('acct:','',$idom->author_id);
331 $dom = $basedom->children(NAMESPACE_SALMON_ME);
333 // figure out where in the DOM tree our data is hiding
335 if($dom->provenance->data)
336 $base = $dom->provenance;
337 elseif($dom->env->data)
343 logger('mod-diaspora: unable to locate salmon data in xml ');
344 http_status_exit(400);
348 // Stash the signature away for now. We have to find their key or it won't be good for anything.
349 $signature = base64url_decode($base->sig);
353 // strip whitespace so our data element will return to one big base64 blob
354 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
357 // stash away some other stuff for later
359 $type = $base->data[0]->attributes()->type[0];
360 $keyhash = $base->sig[0]->attributes()->keyhash[0];
361 $encoding = $base->encoding;
365 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
369 $data = base64url_decode($data);
373 $inner_decrypted = $data;
377 // Decode the encrypted blob
379 $inner_encrypted = base64_decode($data);
380 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
381 $inner_decrypted = pkcs5_unpad($inner_decrypted);
385 logger('mod-diaspora: Could not retrieve author URI.');
386 http_status_exit(400);
389 // Once we have the author URI, go to the web and try to find their public key
390 // (first this will look it up locally if it is in the fcontact cache)
391 // This will also convert diaspora public key from pkcs#1 to pkcs#8
393 logger('mod-diaspora: Fetching key for ' . $author_link );
394 $key = get_diaspora_key($author_link);
397 logger('mod-diaspora: Could not retrieve author key.');
398 http_status_exit(400);
401 $verify = rsa_verify($signed_data,$signature,$key);
404 logger('mod-diaspora: Message did not verify. Discarding.');
405 http_status_exit(400);
408 logger('mod-diaspora: Message verified.');
410 return array('message' => $inner_decrypted, 'author' => $author_link, 'key' => $key);
415 function diaspora_request($importer,$xml) {
417 $sender_handle = unxmlify($xml->sender_handle);
418 $recipient_handle = unxmlify($xml->recipient_handle);
420 if(! $sender_handle || ! $recipient_handle)
423 $contact = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
427 // perhaps we were already sharing with this person. Now they're sharing with us.
428 // That makes us friends.
430 if($contact['rel'] == CONTACT_IS_FOLLOWER) {
431 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
432 intval(CONTACT_IS_FRIEND),
433 intval($contact['id']),
434 intval($importer['uid'])
437 // send notification?
441 $ret = find_diaspora_person_by_handle($sender_handle);
444 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
445 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
449 $batch = (($ret['batch']) ? $ret['batch'] : implode('/', array_slice(explode('/',$ret['url']),0,3)) . '/receive/public');
451 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
452 VALUES ( %d, '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d) ",
453 intval($importer['uid']),
454 dbesc($ret['network']),
461 dbesc($ret['photo']),
462 dbesc($ret['pubkey']),
463 dbesc($ret['notify']),
469 // find the contact record we just created
471 $contact_record = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
473 $hash = random_string() . (string) time(); // Generate a confirm_key
475 if($contact_record) {
476 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime` )
477 VALUES ( %d, %d, %d, %d, '%s', '%s', '%s' )",
478 intval($importer['uid']),
479 intval($contact_record['id']),
482 dbesc( t('Sharing notification from Diaspora network')),
484 dbesc(datetime_convert())
491 function diaspora_post($importer,$xml) {
494 $guid = notags(unxmlify($xml->guid));
495 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
497 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
501 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
502 logger('diaspora_post: Ignoring this author.');
506 $message_id = $diaspora_handle . ':' . $guid;
507 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
508 intval($importer['uid']),
513 logger('diaspora_post: message exists: ' . $guid);
517 // allocate a guid on our system - we aren't fixing any collisions.
518 // we're ignoring them
520 $g = q("select * from guid where guid = '%s' limit 1",
524 q("insert into guid ( guid ) values ( '%s' )",
529 $created = unxmlify($xml->created_at);
530 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
532 $body = diaspora2bb($xml->raw_message);
538 $tags = get_tags($body);
541 foreach($tags as $tag) {
542 if(strpos($tag,'#') === 0) {
543 if(strpos($tag,'[url='))
545 $basetag = str_replace('_',' ',substr($tag,1));
546 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
547 if(strlen($str_tags))
549 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
555 $datarray['uid'] = $importer['uid'];
556 $datarray['contact-id'] = $contact['id'];
557 $datarray['wall'] = 0;
558 $datarray['guid'] = $guid;
559 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
560 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
561 $datarray['private'] = $private;
562 $datarray['parent'] = 0;
563 $datarray['owner-name'] = $contact['name'];
564 $datarray['owner-link'] = $contact['url'];
565 $datarray['owner-avatar'] = $contact['thumb'];
566 $datarray['author-name'] = $contact['name'];
567 $datarray['author-link'] = $contact['url'];
568 $datarray['author-avatar'] = $contact['thumb'];
569 $datarray['body'] = $body;
570 $datarray['tag'] = $str_tags;
571 $datarray['app'] = 'Diaspora';
573 // if empty content it might be a photo that hasn't arrived yet. If a photo arrives, we'll make it visible.
575 $datarray['visible'] = ((strlen($body)) ? 1 : 0);
577 $message_id = item_store($datarray);
580 q("update item set plink = '%s' where id = %d limit 1",
581 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
590 function diaspora_reshare($importer,$xml) {
593 $guid = notags(unxmlify($xml->guid));
594 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
596 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
600 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
601 logger('diaspora_reshare: Ignoring this author: ' . $diaspora_handle . ' ' . print_r($xml,true));
605 $message_id = $diaspora_handle . ':' . $guid;
606 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
607 intval($importer['uid']),
612 logger('diaspora_reshare: message exists: ' . $guid);
616 $orig_author = notags(unxmlify($xml->root_diaspora_id));
617 $orig_guid = notags(unxmlify($xml->root_guid));
619 $source_url = 'https://' . substr($orig_author,strpos($orig_author,'@')+1) . '/p/' . $orig_guid . '.xml';
620 $x = fetch_url($source_url);
622 $x = fetch_url(str_replace('https://','http://',$source_url));
624 logger('diaspora_reshare: unable to fetch source url ' . $source_url);
627 $x = str_replace(array('<activity_streams-photo>','</activity_streams-photo>'),array('<asphoto>','</asphoto>'),$x);
628 $source_xml = parse_xml_string($x,false);
630 if(strlen($source_xml->asphoto->objectId) && ($source_xml->asphoto->objectId != 0) && ($source_xml->asphoto->image_url))
631 $body = '[url=' . notags(unxmlify($source_xml->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->asphoto->objectId)) . '[/img][/url]' . "\n";
632 elseif($source_xml->asphoto->image_url)
633 $body = '[img]' . notags(unxmlify($source_xml->asphoto->image_url)) . '[/img]' . "\n";
634 elseif($source_xml->status_message) {
635 $body = diaspora2bb($source_xml->status_message->raw_message);
638 logger('diaspora_reshare: no reshare content found: ' . print_r($source_xml,true));
642 logger('diaspora_reshare: empty body: source= ' . $x);
646 $person = find_diaspora_person_by_handle($orig_author);
648 if(is_array($person) && x($person,'name') && x($person,'url'))
649 $details = '[url=' . $person['url'] . ']' . $person['name'] . '[/url]';
651 $details = $orig_author;
653 $prefix = '♲ ' . $details . "\n";
656 // allocate a guid on our system - we aren't fixing any collisions.
657 // we're ignoring them
659 $g = q("select * from guid where guid = '%s' limit 1",
663 q("insert into guid ( guid ) values ( '%s' )",
668 $created = unxmlify($xml->created_at);
669 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
671 $body = diaspora2bb($xml->raw_message);
677 $tags = get_tags($body);
680 foreach($tags as $tag) {
681 if(strpos($tag,'#') === 0) {
682 if(strpos($tag,'[url='))
684 $basetag = str_replace('_',' ',substr($tag,1));
685 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
686 if(strlen($str_tags))
688 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
694 $datarray['uid'] = $importer['uid'];
695 $datarray['contact-id'] = $contact['id'];
696 $datarray['wall'] = 0;
697 $datarray['guid'] = $guid;
698 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
699 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
700 $datarray['private'] = $private;
701 $datarray['parent'] = 0;
702 $datarray['owner-name'] = $contact['name'];
703 $datarray['owner-link'] = $contact['url'];
704 $datarray['owner-avatar'] = $contact['thumb'];
705 $datarray['author-name'] = $contact['name'];
706 $datarray['author-link'] = $contact['url'];
707 $datarray['author-avatar'] = $contact['thumb'];
708 $datarray['body'] = $prefix . $body;
709 $datarray['tag'] = $str_tags;
710 $datarray['app'] = 'Diaspora';
712 $message_id = item_store($datarray);
715 q("update item set plink = '%s' where id = %d limit 1",
716 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
726 function diaspora_asphoto($importer,$xml) {
727 logger('diaspora_asphoto called');
730 $guid = notags(unxmlify($xml->guid));
731 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
733 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
737 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
738 logger('diaspora_asphoto: Ignoring this author.');
742 $message_id = $diaspora_handle . ':' . $guid;
743 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
744 intval($importer['uid']),
749 logger('diaspora_asphoto: message exists: ' . $guid);
753 // allocate a guid on our system - we aren't fixing any collisions.
754 // we're ignoring them
756 $g = q("select * from guid where guid = '%s' limit 1",
760 q("insert into guid ( guid ) values ( '%s' )",
765 $created = unxmlify($xml->created_at);
766 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
768 if(strlen($xml->objectId) && ($xml->objectId != 0) && ($xml->image_url))
769 $body = '[url=' . notags(unxmlify($xml->image_url)) . '][img]' . notags(unxmlify($xml->objectId)) . '[/img][/url]' . "\n";
770 elseif($xml->image_url)
771 $body = '[img]' . notags(unxmlify($xml->image_url)) . '[/img]' . "\n";
773 logger('diaspora_asphoto: no photo url found.');
781 $datarray['uid'] = $importer['uid'];
782 $datarray['contact-id'] = $contact['id'];
783 $datarray['wall'] = 0;
784 $datarray['guid'] = $guid;
785 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
786 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
787 $datarray['private'] = $private;
788 $datarray['parent'] = 0;
789 $datarray['owner-name'] = $contact['name'];
790 $datarray['owner-link'] = $contact['url'];
791 $datarray['owner-avatar'] = $contact['thumb'];
792 $datarray['author-name'] = $contact['name'];
793 $datarray['author-link'] = $contact['url'];
794 $datarray['author-avatar'] = $contact['thumb'];
795 $datarray['body'] = $body;
797 $datarray['app'] = 'Diaspora/Cubbi.es';
799 $message_id = item_store($datarray);
802 q("update item set plink = '%s' where id = %d limit 1",
803 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
817 function diaspora_comment($importer,$xml,$msg) {
820 $guid = notags(unxmlify($xml->guid));
821 $parent_guid = notags(unxmlify($xml->parent_guid));
822 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
823 $target_type = notags(unxmlify($xml->target_type));
824 $text = unxmlify($xml->text);
825 $author_signature = notags(unxmlify($xml->author_signature));
827 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
831 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
833 logger('diaspora_comment: cannot find contact: ' . $msg['author']);
837 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
838 logger('diaspora_comment: Ignoring this author.');
842 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
843 intval($importer['uid']),
847 logger('diaspora_comment: our comment just got relayed back to us (or there was a guid collision) : ' . $guid);
851 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
852 intval($importer['uid']),
856 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
859 $parent_item = $r[0];
861 $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
863 $author_signature = base64_decode($author_signature);
865 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
870 $person = find_diaspora_person_by_handle($diaspora_handle);
872 if(is_array($person) && x($person,'pubkey'))
873 $key = $person['pubkey'];
875 logger('diaspora_comment: unable to find author details');
880 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
881 logger('diaspora_comment: verification failed.');
885 if($parent_author_signature) {
886 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
888 $parent_author_signature = base64_decode($parent_author_signature);
892 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
893 logger('diaspora_comment: owner verification failed.');
898 // Phew! Everything checks out. Now create an item.
900 $body = diaspora2bb($text);
902 $message_id = $diaspora_handle . ':' . $guid;
908 $tags = get_tags($body);
911 foreach($tags as $tag) {
912 if(strpos($tag,'#') === 0) {
913 if(strpos($tag,'[url='))
915 $basetag = str_replace('_',' ',substr($tag,1));
916 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
917 if(strlen($str_tags))
919 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
925 $datarray['uid'] = $importer['uid'];
926 $datarray['contact-id'] = $contact['id'];
927 $datarray['wall'] = $parent_item['wall'];
928 $datarray['gravity'] = GRAVITY_COMMENT;
929 $datarray['guid'] = $guid;
930 $datarray['uri'] = $message_id;
931 $datarray['parent-uri'] = $parent_item['uri'];
933 // No timestamps for comments? OK, we'll the use current time.
934 $datarray['created'] = $datarray['edited'] = datetime_convert();
935 $datarray['private'] = $parent_item['private'];
937 $datarray['owner-name'] = $parent_item['owner-name'];
938 $datarray['owner-link'] = $parent_item['owner-link'];
939 $datarray['owner-avatar'] = $parent_item['owner-avatar'];
941 $datarray['author-name'] = $person['name'];
942 $datarray['author-link'] = $person['url'];
943 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
944 $datarray['body'] = $body;
945 $datarray['tag'] = $str_tags;
946 $datarray['app'] = 'Diaspora';
948 $message_id = item_store($datarray);
951 q("update item set plink = '%s' where id = %d limit 1",
952 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
957 if(! $parent_author_signature) {
958 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
960 dbesc($author_signed_data),
961 dbesc(base64_encode($author_signature)),
962 dbesc($diaspora_handle)
965 // if the message isn't already being relayed, notify others
966 // the existence of parent_author_signature means the parent_author or owner
967 // is already relaying.
969 proc_run('php','include/notifier.php','comment',$message_id);
974 function diaspora_photo($importer,$xml,$msg) {
977 $remote_photo_path = notags(unxmlify($xml->remote_photo_path));
979 $remote_photo_name = notags(unxmlify($xml->remote_photo_name));
981 $status_message_guid = notags(unxmlify($xml->status_message_guid));
983 $guid = notags(unxmlify($xml->guid));
985 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
987 $public = notags(unxmlify($xml->public));
989 $created_at = notags(unxmlify($xml_created_at));
992 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
996 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
997 logger('diaspora_photo: Ignoring this author.');
1001 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1002 intval($importer['uid']),
1003 dbesc($status_message_guid)
1006 logger('diaspora_photo: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
1009 $parent_item = $r[0];
1011 $link_text = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n";
1013 if(strpos($parent_item['body'],$link_text) === false) {
1014 $r = q("update item set `body` = '%s', `visible` = 1 where `id` = %d and `uid` = %d limit 1",
1015 dbesc($link_text . $parent_item['body']),
1016 intval($parent_item['id']),
1017 intval($parent_item['uid'])
1027 function diaspora_like($importer,$xml,$msg) {
1030 $guid = notags(unxmlify($xml->guid));
1031 $parent_guid = notags(unxmlify($xml->parent_guid));
1032 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1033 $target_type = notags(unxmlify($xml->target_type));
1034 $positive = notags(unxmlify($xml->positive));
1035 $author_signature = notags(unxmlify($xml->author_signature));
1037 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
1039 // likes on comments not supported here and likes on photos not supported by Diaspora
1041 if($target_type !== 'Post')
1044 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1046 logger('diaspora_like: cannot find contact: ' . $msg['author']);
1050 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
1051 logger('diaspora_like: Ignoring this author.');
1055 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1056 intval($importer['uid']),
1060 logger('diaspora_like: parent item not found: ' . $guid);
1064 $parent_item = $r[0];
1066 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1067 intval($importer['uid']),
1071 if($positive === 'true') {
1072 logger('diaspora_like: duplicate like: ' . $guid);
1075 if($positive === 'false') {
1076 q("UPDATE `item` SET `deleted` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
1077 intval($r[0]['id']),
1078 intval($importer['uid'])
1081 // send notification via proc_run()
1085 if($positive === 'false') {
1086 logger('diaspora_like: unlike received with no corresponding like');
1090 $author_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
1092 $author_signature = base64_decode($author_signature);
1094 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
1099 $person = find_diaspora_person_by_handle($diaspora_handle);
1100 if(is_array($person) && x($person,'pubkey'))
1101 $key = $person['pubkey'];
1103 logger('diaspora_like: unable to find author details');
1108 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
1109 logger('diaspora_like: verification failed.');
1113 if($parent_author_signature) {
1115 $owner_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
1117 $parent_author_signature = base64_decode($parent_author_signature);
1121 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
1122 logger('diaspora_like: owner verification failed.');
1127 // Phew! Everything checks out. Now create an item.
1129 $uri = $diaspora_handle . ':' . $guid;
1131 $activity = ACTIVITY_LIKE;
1132 $post_type = (($parent_item['resource-id']) ? t('photo') : t('status'));
1133 $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
1134 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . '" />' . "\n") ;
1135 $body = $parent_item['body'];
1140 <type>$objtype</type>
1142 <id>{$parent_item['uri']}</id>
1145 <content>$body</content>
1148 $bodyverb = t('%1$s likes %2$s\'s %3$s');
1153 $arr['uid'] = $importer['uid'];
1154 $arr['guid'] = $guid;
1155 $arr['contact-id'] = $contact['id'];
1156 $arr['type'] = 'activity';
1157 $arr['wall'] = $parent_item['wall'];
1158 $arr['gravity'] = GRAVITY_LIKE;
1159 $arr['parent'] = $parent_item['id'];
1160 $arr['parent-uri'] = $parent_item['uri'];
1162 $arr['owner-name'] = $contact['name'];
1163 $arr['owner-link'] = $contact['url'];
1164 $arr['owner-avatar'] = $contact['thumb'];
1166 $arr['author-name'] = $person['name'];
1167 $arr['author-link'] = $person['url'];
1168 $arr['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
1170 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
1171 $alink = '[url=' . $parent_item['author-link'] . ']' . $parent_item['author-name'] . '[/url]';
1172 $plink = '[url=' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . ']' . $post_type . '[/url]';
1173 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
1175 $arr['app'] = 'Diaspora';
1177 $arr['private'] = $parent_item['private'];
1178 $arr['verb'] = $activity;
1179 $arr['object-type'] = $objtype;
1180 $arr['object'] = $obj;
1181 $arr['visible'] = 1;
1183 $arr['last-child'] = 0;
1185 $message_id = item_store($arr);
1189 q("update item set plink = '%s' where id = %d limit 1",
1190 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
1195 if(! $parent_author_signature) {
1196 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1197 intval($message_id),
1198 dbesc($author_signed_data),
1199 dbesc(base64_encode($author_signature)),
1200 dbesc($diaspora_handle)
1204 // if the message isn't already being relayed, notify others
1205 // the existence of parent_author_signature means the parent_author or owner
1206 // is already relaying.
1208 if(! $parent_author_signature)
1209 proc_run('php','include/notifier.php','comment',$message_id);
1214 function diaspora_retraction($importer,$xml) {
1216 $guid = notags(unxmlify($xml->guid));
1217 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1218 $type = notags(unxmlify($xml->type));
1220 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1224 if($type === 'Person') {
1225 contact_remove($contact['id']);
1227 elseif($type === 'Post') {
1228 $r = q("select * from item where guid = '%s' and uid = %d limit 1",
1230 intval($importer['uid'])
1233 if(link_compare($r[0]['author-link'],$contact['url'])) {
1234 q("update item set `deleted` = 1, `changed` = '%s' where `id` = %d limit 1",
1235 dbesc(datetime_convert()),
1246 function diaspora_profile($importer,$xml) {
1249 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1251 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1255 if($contact['blocked']) {
1256 logger('diaspora_post: Ignoring this author.');
1260 $name = unxmlify($xml->first_name) . ((strlen($xml->last_name)) ? ' ' . unxmlify($xml->last_name) : '');
1261 $image_url = unxmlify($xml->image_url);
1262 $birthday = unxmlify($xml->birthday);
1264 $r = q("SELECT DISTINCT ( `resource-id` ) FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `album` = 'Contact Photos' ",
1265 intval($importer['uid']),
1266 intval($contact['id'])
1268 $oldphotos = ((count($r)) ? $r : null);
1270 require_once('include/Photo.php');
1272 $images = import_profile_photo($image_url,$importer['uid'],$contact['id']);
1274 // Generic birthday. We don't know the timezone. The year is irrelevant.
1276 $birthday = str_replace('1000','1901',$birthday);
1278 $birthday = datetime_convert('UTC','UTC',$birthday,'Y-m-d');
1280 $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",
1282 dbesc(datetime_convert()),
1286 dbesc(datetime_convert()),
1288 intval($contact['id']),
1289 intval($importer['uid'])
1294 foreach($oldphotos as $ph) {
1295 q("DELETE FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `album` = 'Contact Photos' AND `resource-id` = '%s' ",
1296 intval($importer['uid']),
1297 intval($contact['id']),
1298 dbesc($ph['resource-id'])
1329 function diaspora_share($me,$contact) {
1331 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1332 $theiraddr = $contact['addr'];
1334 $tpl = get_markup_template('diaspora_share.tpl');
1335 $msg = replace_macros($tpl, array(
1336 '$sender' => $myaddr,
1337 '$recipient' => $theiraddr
1340 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
1342 return(diaspora_transmit($owner,$contact,$slap, false));
1345 function diaspora_unshare($me,$contact) {
1348 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1350 $tpl = get_markup_template('diaspora_retract.tpl');
1351 $msg = replace_macros($tpl, array(
1352 '$guid' => $me['guid'],
1353 '$type' => 'Person',
1354 '$handle' => $myaddr
1357 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
1359 return(diaspora_transmit($owner,$contact,$slap, false));
1365 function diaspora_send_status($item,$owner,$contact,$public_batch = false) {
1368 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1369 $theiraddr = $contact['addr'];
1373 $body = $item['body'];
1375 $cnt = preg_match_all('|\[img\](.*?)\[\/img\]|',$body,$matches,PREG_SET_ORDER);
1377 foreach($matches as $mtch) {
1379 $detail['str'] = $mtch[0];
1380 $detail['path'] = dirname($mtch[1]) . '/';
1381 $detail['file'] = basename($mtch[1]);
1382 $detail['guid'] = $item['guid'];
1383 $detail['handle'] = $myaddr;
1384 $images[] = $detail;
1385 $body = str_replace($detail['str'],t('link'),$body);
1389 $body = xmlify(html_entity_decode(bb2diaspora($body)));
1391 $public = (($item['private']) ? 'false' : 'true');
1393 require_once('include/datetime.php');
1394 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d H:i:s \U\T\C');
1396 $tpl = get_markup_template('diaspora_post.tpl');
1397 $msg = replace_macros($tpl, array(
1399 '$guid' => $item['guid'],
1400 '$handle' => xmlify($myaddr),
1401 '$public' => $public,
1402 '$created' => $created
1405 logger('diaspora_send_status: ' . $owner['username'] . ' -> ' . $contact['name'] . ' base message: ' . $msg, LOGGER_DATA);
1407 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1409 $return_code = diaspora_transmit($owner,$contact,$slap,$public_batch);
1411 if(count($images)) {
1412 diaspora_send_images($item,$owner,$contact,$images,$public_batch);
1415 return $return_code;
1419 function diaspora_send_images($item,$owner,$contact,$images,$public_batch = false) {
1421 if(! count($images))
1423 $mysite = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://') + 3) . '/photo';
1425 $tpl = get_markup_template('diaspora_photo.tpl');
1426 foreach($images as $image) {
1427 if(! stristr($image['path'],$mysite))
1429 $resource = str_replace('.jpg','',$image['file']);
1430 $resource = substr($resource,0,strpos($resource,'-'));
1432 $r = q("select * from photo where `resource-id` = '%s' and `uid` = %d limit 1",
1434 intval($owner['uid'])
1438 $public = (($r[0]['allow_cid'] || $r[0]['allow_gid'] || $r[0]['deny_cid'] || $r[0]['deny_gid']) ? 'false' : 'true' );
1439 $msg = replace_macros($tpl,array(
1440 '$path' => xmlify($image['path']),
1441 '$filename' => xmlify($image['file']),
1442 '$msg_guid' => xmlify($image['guid']),
1443 '$guid' => xmlify($r[0]['guid']),
1444 '$handle' => xmlify($image['handle']),
1445 '$public' => xmlify($public),
1446 '$created_at' => xmlify(datetime_convert('UTC','UTC',$r[0]['created'],'Y-m-d H:i:s \U\T\C'))
1450 logger('diaspora_send_photo: base message: ' . $msg, LOGGER_DATA);
1451 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1453 diaspora_transmit($owner,$contact,$slap,$public_batch);
1458 function diaspora_send_followup($item,$owner,$contact,$public_batch = false) {
1461 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1462 $theiraddr = $contact['addr'];
1464 $p = q("select guid from item where parent = %d limit 1",
1468 $parent_guid = $p[0]['guid'];
1472 if($item['verb'] === ACTIVITY_LIKE) {
1473 $tpl = get_markup_template('diaspora_like.tpl');
1475 $target_type = 'Post';
1476 $positive = (($item['deleted']) ? 'false' : 'true');
1479 $tpl = get_markup_template('diaspora_comment.tpl');
1483 $text = html_entity_decode(bb2diaspora($item['body']));
1488 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
1490 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
1492 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1494 $msg = replace_macros($tpl,array(
1495 '$guid' => xmlify($item['guid']),
1496 '$parent_guid' => xmlify($parent_guid),
1497 '$target_type' =>xmlify($target_type),
1498 '$authorsig' => xmlify($authorsig),
1499 '$body' => xmlify($text),
1500 '$positive' => xmlify($positive),
1501 '$handle' => xmlify($myaddr)
1504 logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
1506 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1508 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1512 function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
1516 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1517 $theiraddr = $contact['addr'];
1520 $p = q("select guid from item where parent = %d limit 1",
1524 $parent_guid = $p[0]['guid'];
1528 if($item['verb'] === ACTIVITY_LIKE) {
1529 $tpl = get_markup_template('diaspora_like_relay.tpl');
1531 $target_type = 'Post';
1532 $positive = (($item['deleted']) ? 'false' : 'true');
1535 $tpl = get_markup_template('diaspora_comment_relay.tpl');
1539 $body = $item['body'];
1541 $text = html_entity_decode(bb2diaspora($body));
1543 // fetch the original signature if somebody sent the post to us to relay
1544 // If we are relaying for a reply originating on our own account, there wasn't a 'send to relay'
1545 // action. It wasn't needed. In that case create the original signature and the
1546 // owner (parent author) signature
1547 // comments from other networks will be relayed under our name, with a brief
1548 // preamble to describe what's happening and noting the real author
1550 $r = q("select * from sign where iid = %d limit 1",
1555 $signed_text = $orig_sign['signed_text'];
1556 $authorsig = $orig_sign['signature'];
1557 $handle = $orig_sign['signer'];
1561 $itemcontact = q("select * from contact where `id` = %d limit 1",
1562 intval($item['contact-id'])
1564 if(count($itemcontact)) {
1565 if(! $itemcontact[0]['self']) {
1566 $prefix = sprintf( t('[Relayed] Comment authored by %s from network %s'),
1567 '['. $item['author-name'] . ']' . '(' . $item['author-link'] . ')',
1568 network_to_name($itemcontact['network'])) . "\n";
1569 $body = $prefix . $body;
1575 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
1577 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
1579 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1581 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1582 intval($item['id']),
1583 dbesc($signed_text),
1584 dbesc(base64_encode($authorsig)),
1593 $parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1595 $msg = replace_macros($tpl,array(
1596 '$guid' => xmlify($item['guid']),
1597 '$parent_guid' => xmlify($parent_guid),
1598 '$target_type' =>xmlify($target_type),
1599 '$authorsig' => xmlify($orig_sign['signature']),
1600 '$parentsig' => xmlify($parentauthorsig),
1601 '$body' => xmlify($text),
1602 '$positive' => xmlify($positive),
1603 '$handle' => xmlify($handle)
1606 logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
1608 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1610 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1616 function diaspora_send_retraction($item,$owner,$contact,$public_batch = false) {
1619 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1621 $tpl = get_markup_template('diaspora_retract.tpl');
1622 $msg = replace_macros($tpl, array(
1623 '$guid' => $item['guid'],
1625 '$handle' => $myaddr
1628 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1630 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1635 function diaspora_transmit($owner,$contact,$slap,$public_batch) {
1638 $logid = random_string(4);
1639 logger('diaspora_transmit: ' . $logid . ' ' . (($public_batch) ? $contact['batch'] : $contact['notify']));
1640 post_url((($public_batch) ? $contact['batch'] : $contact['notify']) . '/',$slap);
1641 $return_code = $a->get_curl_code();
1642 logger('diaspora_transmit: ' . $logid . ' returns: ' . $return_code);
1644 if((! $return_code) || (($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))) {
1645 logger('diaspora_transmit: queue message');
1646 // queue message for redelivery
1647 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`,`batch`)
1648 VALUES ( %d, '%s', '%s', '%s', %d) ",
1649 intval($contact['id']),
1650 dbesc(datetime_convert()),
1651 dbesc(datetime_convert()),
1653 intval($public_batch)
1658 return(($return_code) ? $return_code : (-1));