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 logger('find_diaspora_person_by handle: in cache ' . print_r($r,true), LOGGER_DEBUG);
96 // update record occasionally so it doesn't get stale
97 $d = strtotime($r[0]['updated'] . ' +00:00');
98 if($d > strtotime('now - 14 days'))
102 logger('find_diaspora_person_by_handle: refresh',LOGGER_DEBUG);
103 require_once('include/Scrape.php');
104 $r = probe_url($handle, PROBE_DIASPORA);
105 if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
106 add_fcontact($r,$update);
113 function get_diaspora_key($uri) {
114 logger('Fetching diaspora key for: ' . $uri);
116 $r = find_diaspora_person_by_handle($uri);
123 function diaspora_pubmsg_build($msg,$user,$contact,$prvkey,$pubkey) {
126 logger('diaspora_pubmsg_build: ' . $msg, LOGGER_DATA);
129 $handle = $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
131 // $b64_data = base64_encode($msg);
132 // $b64url_data = base64url_encode($b64_data);
134 $b64url_data = base64url_encode($msg);
136 $data = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
138 $type = 'application/xml';
139 $encoding = 'base64url';
142 $signable_data = $data . '.' . base64url_encode($type) . '.'
143 . base64url_encode($encoding) . '.' . base64url_encode($alg) ;
145 $signature = rsa_sign($signable_data,$prvkey);
146 $sig = base64url_encode($signature);
149 <?xml version='1.0' encoding='UTF-8'?>
150 <diaspora xmlns="https://joindiaspora.com/protocol" xmlns:me="http://salmon-protocol.org/ns/magic-env" >
152 <author_id>$handle</author_id>
155 <me:encoding>base64url</me:encoding>
156 <me:alg>RSA-SHA256</me:alg>
157 <me:data type="application/xml">$data</me:data>
158 <me:sig>$sig</me:sig>
163 logger('diaspora_pubmsg_build: magic_env: ' . $magic_env, LOGGER_DATA);
171 function diaspora_msg_build($msg,$user,$contact,$prvkey,$pubkey,$public = false) {
175 return diaspora_pubmsg_build($msg,$user,$contact,$prvkey,$pubkey);
177 logger('diaspora_msg_build: ' . $msg, LOGGER_DATA);
179 $inner_aes_key = random_string(32);
180 $b_inner_aes_key = base64_encode($inner_aes_key);
181 $inner_iv = random_string(16);
182 $b_inner_iv = base64_encode($inner_iv);
184 $outer_aes_key = random_string(32);
185 $b_outer_aes_key = base64_encode($outer_aes_key);
186 $outer_iv = random_string(16);
187 $b_outer_iv = base64_encode($outer_iv);
189 $handle = $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
191 $padded_data = pkcs5_pad($msg,16);
192 $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
194 $b64_data = base64_encode($inner_encrypted);
197 $b64url_data = base64url_encode($b64_data);
198 $data = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
200 $type = 'application/xml';
201 $encoding = 'base64url';
204 $signable_data = $data . '.' . base64url_encode($type) . '.'
205 . base64url_encode($encoding) . '.' . base64url_encode($alg) ;
207 $signature = rsa_sign($signable_data,$prvkey);
208 $sig = base64url_encode($signature);
210 $decrypted_header = <<< EOT
213 <aes_key>$b_inner_aes_key</aes_key>
214 <author_id>$handle</author_id>
218 $decrypted_header = pkcs5_pad($decrypted_header,16);
220 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
222 $outer_json = json_encode(array('iv' => $b_outer_iv,'key' => $b_outer_aes_key));
224 $encrypted_outer_key_bundle = '';
225 openssl_public_encrypt($outer_json,$encrypted_outer_key_bundle,$pubkey);
227 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
229 logger('outer_bundle: ' . $b64_encrypted_outer_key_bundle . ' key: ' . $pubkey, LOGGER_DATA);
231 $encrypted_header_json_object = json_encode(array('aes_key' => base64_encode($encrypted_outer_key_bundle),
232 'ciphertext' => base64_encode($ciphertext)));
233 $cipher_json = base64_encode($encrypted_header_json_object);
235 $encrypted_header = '<encrypted_header>' . $cipher_json . '</encrypted_header>';
238 <?xml version='1.0' encoding='UTF-8'?>
239 <diaspora xmlns="https://joindiaspora.com/protocol" xmlns:me="http://salmon-protocol.org/ns/magic-env" >
242 <me:encoding>base64url</me:encoding>
243 <me:alg>RSA-SHA256</me:alg>
244 <me:data type="application/xml">$data</me:data>
245 <me:sig>$sig</me:sig>
250 logger('diaspora_msg_build: magic_env: ' . $magic_env, LOGGER_DATA);
257 * diaspora_decode($importer,$xml)
258 * array $importer -> from user table
259 * string $xml -> urldecoded Diaspora salmon
262 * 'message' -> decoded Diaspora XML message
263 * 'author' -> author diaspora handle
264 * 'key' -> author public key (converted to pkcs#8)
266 * Author and key are used elsewhere to save a lookup for verifying replies and likes
270 function diaspora_decode($importer,$xml) {
273 $basedom = parse_xml_string($xml);
275 $children = $basedom->children('https://joindiaspora.com/protocol');
277 if($children->header) {
279 $author_link = str_replace('acct:','',$children->header->author_id);
283 $encrypted_header = json_decode(base64_decode($children->encrypted_header));
285 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
286 $ciphertext = base64_decode($encrypted_header->ciphertext);
288 $outer_key_bundle = '';
289 openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
291 $j_outer_key_bundle = json_decode($outer_key_bundle);
293 $outer_iv = base64_decode($j_outer_key_bundle->iv);
294 $outer_key = base64_decode($j_outer_key_bundle->key);
296 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
299 $decrypted = pkcs5_unpad($decrypted);
302 * $decrypted now contains something like
305 * <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
306 * <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
311 * <name>Ryan Hughes</name>
312 * <uri>acct:galaxor@diaspora.pirateship.org</uri>
317 * <author_id>galaxor@diaspora.priateship.org</author_id>
321 * </decrypted_header>
324 logger('decrypted: ' . $decrypted, LOGGER_DEBUG);
325 $idom = parse_xml_string($decrypted,false);
327 $inner_iv = base64_decode($idom->iv);
328 $inner_aes_key = base64_decode($idom->aes_key);
330 $author_link = str_replace('acct:','',$idom->author_id);
334 $dom = $basedom->children(NAMESPACE_SALMON_ME);
336 // figure out where in the DOM tree our data is hiding
338 if($dom->provenance->data)
339 $base = $dom->provenance;
340 elseif($dom->env->data)
346 logger('mod-diaspora: unable to locate salmon data in xml ');
347 http_status_exit(400);
351 // Stash the signature away for now. We have to find their key or it won't be good for anything.
352 $signature = base64url_decode($base->sig);
356 // strip whitespace so our data element will return to one big base64 blob
357 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
360 // stash away some other stuff for later
362 $type = $base->data[0]->attributes()->type[0];
363 $keyhash = $base->sig[0]->attributes()->keyhash[0];
364 $encoding = $base->encoding;
368 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
372 $data = base64url_decode($data);
376 $inner_decrypted = $data;
380 // Decode the encrypted blob
382 $inner_encrypted = base64_decode($data);
383 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
384 $inner_decrypted = pkcs5_unpad($inner_decrypted);
388 logger('mod-diaspora: Could not retrieve author URI.');
389 http_status_exit(400);
392 // Once we have the author URI, go to the web and try to find their public key
393 // (first this will look it up locally if it is in the fcontact cache)
394 // This will also convert diaspora public key from pkcs#1 to pkcs#8
396 logger('mod-diaspora: Fetching key for ' . $author_link );
397 $key = get_diaspora_key($author_link);
400 logger('mod-diaspora: Could not retrieve author key.');
401 http_status_exit(400);
404 $verify = rsa_verify($signed_data,$signature,$key);
407 logger('mod-diaspora: Message did not verify. Discarding.');
408 http_status_exit(400);
411 logger('mod-diaspora: Message verified.');
413 return array('message' => $inner_decrypted, 'author' => $author_link, 'key' => $key);
418 function diaspora_request($importer,$xml) {
420 $sender_handle = unxmlify($xml->sender_handle);
421 $recipient_handle = unxmlify($xml->recipient_handle);
423 if(! $sender_handle || ! $recipient_handle)
426 $contact = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
430 // perhaps we were already sharing with this person. Now they're sharing with us.
431 // That makes us friends.
433 if($contact['rel'] == CONTACT_IS_FOLLOWER) {
434 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
435 intval(CONTACT_IS_FRIEND),
436 intval($contact['id']),
437 intval($importer['uid'])
440 // send notification?
444 $ret = find_diaspora_person_by_handle($sender_handle);
447 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
448 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
452 $batch = (($ret['batch']) ? $ret['batch'] : implode('/', array_slice(explode('/',$ret['url']),0,3)) . '/receive/public');
454 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
455 VALUES ( %d, '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d) ",
456 intval($importer['uid']),
457 dbesc($ret['network']),
464 dbesc($ret['photo']),
465 dbesc($ret['pubkey']),
466 dbesc($ret['notify']),
472 // find the contact record we just created
474 $contact_record = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
476 $hash = random_string() . (string) time(); // Generate a confirm_key
478 if($contact_record) {
479 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime` )
480 VALUES ( %d, %d, %d, %d, '%s', '%s', '%s' )",
481 intval($importer['uid']),
482 intval($contact_record['id']),
485 dbesc( t('Sharing notification from Diaspora network')),
487 dbesc(datetime_convert())
494 function diaspora_post($importer,$xml) {
497 $guid = notags(unxmlify($xml->guid));
498 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
500 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
504 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
505 logger('diaspora_post: Ignoring this author.');
509 $message_id = $diaspora_handle . ':' . $guid;
510 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
511 intval($importer['uid']),
516 logger('diaspora_post: message exists: ' . $guid);
520 // allocate a guid on our system - we aren't fixing any collisions.
521 // we're ignoring them
523 $g = q("select * from guid where guid = '%s' limit 1",
527 q("insert into guid ( guid ) values ( '%s' )",
532 $created = unxmlify($xml->created_at);
533 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
535 $body = diaspora2bb($xml->raw_message);
541 $tags = get_tags($body);
544 foreach($tags as $tag) {
545 if(strpos($tag,'#') === 0) {
546 if(strpos($tag,'[url='))
548 $basetag = str_replace('_',' ',substr($tag,1));
549 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
550 if(strlen($str_tags))
552 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
558 $datarray['uid'] = $importer['uid'];
559 $datarray['contact-id'] = $contact['id'];
560 $datarray['wall'] = 0;
561 $datarray['guid'] = $guid;
562 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
563 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
564 $datarray['private'] = $private;
565 $datarray['parent'] = 0;
566 $datarray['owner-name'] = $contact['name'];
567 $datarray['owner-link'] = $contact['url'];
568 $datarray['owner-avatar'] = $contact['thumb'];
569 $datarray['author-name'] = $contact['name'];
570 $datarray['author-link'] = $contact['url'];
571 $datarray['author-avatar'] = $contact['thumb'];
572 $datarray['body'] = $body;
573 $datarray['tag'] = $str_tags;
574 $datarray['app'] = 'Diaspora';
576 // if empty content it might be a photo that hasn't arrived yet. If a photo arrives, we'll make it visible.
578 $datarray['visible'] = ((strlen($body)) ? 1 : 0);
580 $message_id = item_store($datarray);
583 q("update item set plink = '%s' where id = %d limit 1",
584 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
593 function diaspora_reshare($importer,$xml) {
595 logger('diaspora_reshare: init: ' . print_r($xml,true));
598 $guid = notags(unxmlify($xml->guid));
599 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
602 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
606 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
607 logger('diaspora_reshare: Ignoring this author: ' . $diaspora_handle . ' ' . print_r($xml,true));
611 $message_id = $diaspora_handle . ':' . $guid;
612 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
613 intval($importer['uid']),
618 logger('diaspora_reshare: message exists: ' . $guid);
622 $orig_author = notags(unxmlify($xml->root_diaspora_id));
623 $orig_guid = notags(unxmlify($xml->root_guid));
625 $source_url = 'https://' . substr($orig_author,strpos($orig_author,'@')+1) . '/p/' . $orig_guid . '.xml';
626 $x = fetch_url($source_url);
628 $x = fetch_url(str_replace('https://','http://',$source_url));
630 logger('diaspora_reshare: unable to fetch source url ' . $source_url);
633 logger('diaspora_reshare: source: ' . $x);
635 $x = str_replace(array('<activity_streams-photo>','</activity_streams-photo>'),array('<asphoto>','</asphoto>'),$x);
636 $source_xml = parse_xml_string($x,false);
638 if(strlen($source_xml->post->asphoto->objectId) && ($source_xml->post->asphoto->objectId != 0) && ($source_xml->post->asphoto->image_url)) {
639 $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
640 $body = scale_diaspora_images($body,false);
642 elseif($source_xml->post->asphoto->image_url) {
643 $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
644 $body = scale_diaspora_images($body);
646 elseif($source_xml->post->status_message) {
647 $body = diaspora2bb($source_xml->post->status_message->raw_message);
648 $body = scale_diaspora_images($body);
652 logger('diaspora_reshare: no reshare content found: ' . print_r($source_xml,true));
656 logger('diaspora_reshare: empty body: source= ' . $x);
660 $person = find_diaspora_person_by_handle($orig_author);
662 if(is_array($person) && x($person,'name') && x($person,'url'))
663 $details = '[url=' . $person['url'] . ']' . $person['name'] . '[/url]';
665 $details = $orig_author;
667 $prefix = '♲ ' . $details . "\n";
670 // allocate a guid on our system - we aren't fixing any collisions.
671 // we're ignoring them
673 $g = q("select * from guid where guid = '%s' limit 1",
677 q("insert into guid ( guid ) values ( '%s' )",
682 $created = unxmlify($xml->created_at);
683 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
689 $tags = get_tags($body);
692 foreach($tags as $tag) {
693 if(strpos($tag,'#') === 0) {
694 if(strpos($tag,'[url='))
696 $basetag = str_replace('_',' ',substr($tag,1));
697 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
698 if(strlen($str_tags))
700 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
706 $datarray['uid'] = $importer['uid'];
707 $datarray['contact-id'] = $contact['id'];
708 $datarray['wall'] = 0;
709 $datarray['guid'] = $guid;
710 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
711 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
712 $datarray['private'] = $private;
713 $datarray['parent'] = 0;
714 $datarray['owner-name'] = $contact['name'];
715 $datarray['owner-link'] = $contact['url'];
716 $datarray['owner-avatar'] = $contact['thumb'];
717 $datarray['author-name'] = $contact['name'];
718 $datarray['author-link'] = $contact['url'];
719 $datarray['author-avatar'] = $contact['thumb'];
720 $datarray['body'] = $prefix . $body;
721 $datarray['tag'] = $str_tags;
722 $datarray['app'] = 'Diaspora';
724 $message_id = item_store($datarray);
727 q("update item set plink = '%s' where id = %d limit 1",
728 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
738 function diaspora_asphoto($importer,$xml) {
739 logger('diaspora_asphoto called');
742 $guid = notags(unxmlify($xml->guid));
743 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
745 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
749 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
750 logger('diaspora_asphoto: Ignoring this author.');
754 $message_id = $diaspora_handle . ':' . $guid;
755 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
756 intval($importer['uid']),
761 logger('diaspora_asphoto: message exists: ' . $guid);
765 // allocate a guid on our system - we aren't fixing any collisions.
766 // we're ignoring them
768 $g = q("select * from guid where guid = '%s' limit 1",
772 q("insert into guid ( guid ) values ( '%s' )",
777 $created = unxmlify($xml->created_at);
778 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
780 if(strlen($xml->objectId) && ($xml->objectId != 0) && ($xml->image_url))
781 $body = '[url=' . notags(unxmlify($xml->image_url)) . '][img]' . notags(unxmlify($xml->objectId)) . '[/img][/url]' . "\n";
782 elseif($xml->image_url)
783 $body = '[img]' . notags(unxmlify($xml->image_url)) . '[/img]' . "\n";
785 logger('diaspora_asphoto: no photo url found.');
793 $datarray['uid'] = $importer['uid'];
794 $datarray['contact-id'] = $contact['id'];
795 $datarray['wall'] = 0;
796 $datarray['guid'] = $guid;
797 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
798 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
799 $datarray['private'] = $private;
800 $datarray['parent'] = 0;
801 $datarray['owner-name'] = $contact['name'];
802 $datarray['owner-link'] = $contact['url'];
803 $datarray['owner-avatar'] = $contact['thumb'];
804 $datarray['author-name'] = $contact['name'];
805 $datarray['author-link'] = $contact['url'];
806 $datarray['author-avatar'] = $contact['thumb'];
807 $datarray['body'] = $body;
809 $datarray['app'] = 'Diaspora/Cubbi.es';
811 $message_id = item_store($datarray);
814 q("update item set plink = '%s' where id = %d limit 1",
815 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
829 function diaspora_comment($importer,$xml,$msg) {
832 $guid = notags(unxmlify($xml->guid));
833 $parent_guid = notags(unxmlify($xml->parent_guid));
834 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
835 $target_type = notags(unxmlify($xml->target_type));
836 $text = unxmlify($xml->text);
837 $author_signature = notags(unxmlify($xml->author_signature));
839 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
843 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
845 logger('diaspora_comment: cannot find contact: ' . $msg['author']);
849 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
850 logger('diaspora_comment: Ignoring this author.');
854 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
855 intval($importer['uid']),
859 logger('diaspora_comment: our comment just got relayed back to us (or there was a guid collision) : ' . $guid);
863 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
864 intval($importer['uid']),
868 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
871 $parent_item = $r[0];
873 $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
875 $author_signature = base64_decode($author_signature);
877 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
882 $person = find_diaspora_person_by_handle($diaspora_handle);
884 if(is_array($person) && x($person,'pubkey'))
885 $key = $person['pubkey'];
887 logger('diaspora_comment: unable to find author details');
892 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
893 logger('diaspora_comment: verification failed.');
897 if($parent_author_signature) {
898 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
900 $parent_author_signature = base64_decode($parent_author_signature);
904 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
905 logger('diaspora_comment: owner verification failed.');
910 // Phew! Everything checks out. Now create an item.
912 $body = diaspora2bb($text);
914 $message_id = $diaspora_handle . ':' . $guid;
920 $tags = get_tags($body);
923 foreach($tags as $tag) {
924 if(strpos($tag,'#') === 0) {
925 if(strpos($tag,'[url='))
927 $basetag = str_replace('_',' ',substr($tag,1));
928 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
929 if(strlen($str_tags))
931 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
937 $datarray['uid'] = $importer['uid'];
938 $datarray['contact-id'] = $contact['id'];
939 $datarray['wall'] = $parent_item['wall'];
940 $datarray['gravity'] = GRAVITY_COMMENT;
941 $datarray['guid'] = $guid;
942 $datarray['uri'] = $message_id;
943 $datarray['parent-uri'] = $parent_item['uri'];
945 // No timestamps for comments? OK, we'll the use current time.
946 $datarray['created'] = $datarray['edited'] = datetime_convert();
947 $datarray['private'] = $parent_item['private'];
949 $datarray['owner-name'] = $parent_item['owner-name'];
950 $datarray['owner-link'] = $parent_item['owner-link'];
951 $datarray['owner-avatar'] = $parent_item['owner-avatar'];
953 $datarray['author-name'] = $person['name'];
954 $datarray['author-link'] = $person['url'];
955 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
956 $datarray['body'] = $body;
957 $datarray['tag'] = $str_tags;
959 // We can't be certain what the original app is if the message is relayed.
960 if(($parent_item['origin']) && (! $parent_author_signature))
961 $datarray['app'] = 'Diaspora';
963 $message_id = item_store($datarray);
966 q("update item set plink = '%s' where id = %d limit 1",
967 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
972 if(($parent_item['origin']) && (! $parent_author_signature)) {
973 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
975 dbesc($author_signed_data),
976 dbesc(base64_encode($author_signature)),
977 dbesc($diaspora_handle)
980 // if the message isn't already being relayed, notify others
981 // the existence of parent_author_signature means the parent_author or owner
982 // is already relaying.
984 proc_run('php','include/notifier.php','comment',$message_id);
989 function diaspora_photo($importer,$xml,$msg) {
992 $remote_photo_path = notags(unxmlify($xml->remote_photo_path));
994 $remote_photo_name = notags(unxmlify($xml->remote_photo_name));
996 $status_message_guid = notags(unxmlify($xml->status_message_guid));
998 $guid = notags(unxmlify($xml->guid));
1000 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1002 $public = notags(unxmlify($xml->public));
1004 $created_at = notags(unxmlify($xml_created_at));
1007 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1011 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
1012 logger('diaspora_photo: Ignoring this author.');
1016 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1017 intval($importer['uid']),
1018 dbesc($status_message_guid)
1021 logger('diaspora_photo: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
1024 $parent_item = $r[0];
1026 $link_text = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n";
1028 if(strpos($parent_item['body'],$link_text) === false) {
1029 $r = q("update item set `body` = '%s', `visible` = 1 where `id` = %d and `uid` = %d limit 1",
1030 dbesc($link_text . $parent_item['body']),
1031 intval($parent_item['id']),
1032 intval($parent_item['uid'])
1042 function diaspora_like($importer,$xml,$msg) {
1045 $guid = notags(unxmlify($xml->guid));
1046 $parent_guid = notags(unxmlify($xml->parent_guid));
1047 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1048 $target_type = notags(unxmlify($xml->target_type));
1049 $positive = notags(unxmlify($xml->positive));
1050 $author_signature = notags(unxmlify($xml->author_signature));
1052 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
1054 // likes on comments not supported here and likes on photos not supported by Diaspora
1056 if($target_type !== 'Post')
1059 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1061 logger('diaspora_like: cannot find contact: ' . $msg['author']);
1065 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
1066 logger('diaspora_like: Ignoring this author.');
1070 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1071 intval($importer['uid']),
1075 logger('diaspora_like: parent item not found: ' . $guid);
1079 $parent_item = $r[0];
1081 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1082 intval($importer['uid']),
1086 if($positive === 'true') {
1087 logger('diaspora_like: duplicate like: ' . $guid);
1090 if($positive === 'false') {
1091 q("UPDATE `item` SET `deleted` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
1092 intval($r[0]['id']),
1093 intval($importer['uid'])
1096 // send notification via proc_run()
1100 if($positive === 'false') {
1101 logger('diaspora_like: unlike received with no corresponding like');
1105 $author_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
1107 $author_signature = base64_decode($author_signature);
1109 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
1114 $person = find_diaspora_person_by_handle($diaspora_handle);
1115 if(is_array($person) && x($person,'pubkey'))
1116 $key = $person['pubkey'];
1118 logger('diaspora_like: unable to find author details');
1123 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
1124 logger('diaspora_like: verification failed.');
1128 if($parent_author_signature) {
1130 $owner_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
1132 $parent_author_signature = base64_decode($parent_author_signature);
1136 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
1137 logger('diaspora_like: owner verification failed.');
1142 // Phew! Everything checks out. Now create an item.
1144 $uri = $diaspora_handle . ':' . $guid;
1146 $activity = ACTIVITY_LIKE;
1147 $post_type = (($parent_item['resource-id']) ? t('photo') : t('status'));
1148 $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
1149 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . '" />' . "\n") ;
1150 $body = $parent_item['body'];
1155 <type>$objtype</type>
1157 <id>{$parent_item['uri']}</id>
1160 <content>$body</content>
1163 $bodyverb = t('%1$s likes %2$s\'s %3$s');
1168 $arr['uid'] = $importer['uid'];
1169 $arr['guid'] = $guid;
1170 $arr['contact-id'] = $contact['id'];
1171 $arr['type'] = 'activity';
1172 $arr['wall'] = $parent_item['wall'];
1173 $arr['gravity'] = GRAVITY_LIKE;
1174 $arr['parent'] = $parent_item['id'];
1175 $arr['parent-uri'] = $parent_item['uri'];
1177 $arr['owner-name'] = $parent_item['name'];
1178 $arr['owner-link'] = $parent_item['url'];
1179 $arr['owner-avatar'] = $parent_item['thumb'];
1181 $arr['author-name'] = $person['name'];
1182 $arr['author-link'] = $person['url'];
1183 $arr['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
1185 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
1186 $alink = '[url=' . $parent_item['author-link'] . ']' . $parent_item['author-name'] . '[/url]';
1187 $plink = '[url=' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . ']' . $post_type . '[/url]';
1188 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
1190 $arr['app'] = 'Diaspora';
1192 $arr['private'] = $parent_item['private'];
1193 $arr['verb'] = $activity;
1194 $arr['object-type'] = $objtype;
1195 $arr['object'] = $obj;
1196 $arr['visible'] = 1;
1198 $arr['last-child'] = 0;
1200 $message_id = item_store($arr);
1204 q("update item set plink = '%s' where id = %d limit 1",
1205 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
1210 if(! $parent_author_signature) {
1211 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1212 intval($message_id),
1213 dbesc($author_signed_data),
1214 dbesc(base64_encode($author_signature)),
1215 dbesc($diaspora_handle)
1219 // if the message isn't already being relayed, notify others
1220 // the existence of parent_author_signature means the parent_author or owner
1221 // is already relaying. The parent_item['origin'] indicates the message was created on our system
1223 if(($parent_item['origin']) && (! $parent_author_signature))
1224 proc_run('php','include/notifier.php','comment',$message_id);
1229 function diaspora_retraction($importer,$xml) {
1231 $guid = notags(unxmlify($xml->guid));
1232 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1233 $type = notags(unxmlify($xml->type));
1235 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1239 if($type === 'Person') {
1240 contact_remove($contact['id']);
1242 elseif($type === 'Post') {
1243 $r = q("select * from item where guid = '%s' and uid = %d limit 1",
1245 intval($importer['uid'])
1248 if(link_compare($r[0]['author-link'],$contact['url'])) {
1249 q("update item set `deleted` = 1, `changed` = '%s' where `id` = %d limit 1",
1250 dbesc(datetime_convert()),
1261 function diaspora_profile($importer,$xml) {
1264 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1266 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1270 if($contact['blocked']) {
1271 logger('diaspora_post: Ignoring this author.');
1275 $name = unxmlify($xml->first_name) . ((strlen($xml->last_name)) ? ' ' . unxmlify($xml->last_name) : '');
1276 $image_url = unxmlify($xml->image_url);
1277 $birthday = unxmlify($xml->birthday);
1279 $r = q("SELECT DISTINCT ( `resource-id` ) FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `album` = 'Contact Photos' ",
1280 intval($importer['uid']),
1281 intval($contact['id'])
1283 $oldphotos = ((count($r)) ? $r : null);
1285 require_once('include/Photo.php');
1287 $images = import_profile_photo($image_url,$importer['uid'],$contact['id']);
1289 // Generic birthday. We don't know the timezone. The year is irrelevant.
1291 $birthday = str_replace('1000','1901',$birthday);
1293 $birthday = datetime_convert('UTC','UTC',$birthday,'Y-m-d');
1295 $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",
1297 dbesc(datetime_convert()),
1301 dbesc(datetime_convert()),
1303 intval($contact['id']),
1304 intval($importer['uid'])
1309 foreach($oldphotos as $ph) {
1310 q("DELETE FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `album` = 'Contact Photos' AND `resource-id` = '%s' ",
1311 intval($importer['uid']),
1312 intval($contact['id']),
1313 dbesc($ph['resource-id'])
1344 function diaspora_share($me,$contact) {
1346 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1347 $theiraddr = $contact['addr'];
1349 $tpl = get_markup_template('diaspora_share.tpl');
1350 $msg = replace_macros($tpl, array(
1351 '$sender' => $myaddr,
1352 '$recipient' => $theiraddr
1355 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
1357 return(diaspora_transmit($owner,$contact,$slap, false));
1360 function diaspora_unshare($me,$contact) {
1363 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1365 $tpl = get_markup_template('diaspora_retract.tpl');
1366 $msg = replace_macros($tpl, array(
1367 '$guid' => $me['guid'],
1368 '$type' => 'Person',
1369 '$handle' => $myaddr
1372 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
1374 return(diaspora_transmit($owner,$contact,$slap, false));
1380 function diaspora_send_status($item,$owner,$contact,$public_batch = false) {
1383 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1384 $theiraddr = $contact['addr'];
1388 $body = $item['body'];
1391 // We're trying to match Diaspora's split message/photo protocol but
1392 // all the photos are displayed on D* as links and not img's - even
1393 // though we're sending pretty much precisely what they send us when
1394 // doing the same operation.
1395 // Commented out for now, we'll use bb2diaspora to convert photos to markdown
1396 // which seems to get through intact.
1398 $cnt = preg_match_all('|\[img\](.*?)\[\/img\]|',$body,$matches,PREG_SET_ORDER);
1400 foreach($matches as $mtch) {
1402 $detail['str'] = $mtch[0];
1403 $detail['path'] = dirname($mtch[1]) . '/';
1404 $detail['file'] = basename($mtch[1]);
1405 $detail['guid'] = $item['guid'];
1406 $detail['handle'] = $myaddr;
1407 $images[] = $detail;
1408 $body = str_replace($detail['str'],$mtch[1],$body);
1413 $body = xmlify(html_entity_decode(bb2diaspora($body)));
1415 $public = (($item['private']) ? 'false' : 'true');
1417 require_once('include/datetime.php');
1418 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d H:i:s \U\T\C');
1420 $tpl = get_markup_template('diaspora_post.tpl');
1421 $msg = replace_macros($tpl, array(
1423 '$guid' => $item['guid'],
1424 '$handle' => xmlify($myaddr),
1425 '$public' => $public,
1426 '$created' => $created
1429 logger('diaspora_send_status: ' . $owner['username'] . ' -> ' . $contact['name'] . ' base message: ' . $msg, LOGGER_DATA);
1431 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1433 $return_code = diaspora_transmit($owner,$contact,$slap,$public_batch);
1435 if(count($images)) {
1436 diaspora_send_images($item,$owner,$contact,$images,$public_batch);
1439 return $return_code;
1443 function diaspora_send_images($item,$owner,$contact,$images,$public_batch = false) {
1445 if(! count($images))
1447 $mysite = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://') + 3) . '/photo';
1449 $tpl = get_markup_template('diaspora_photo.tpl');
1450 foreach($images as $image) {
1451 if(! stristr($image['path'],$mysite))
1453 $resource = str_replace('.jpg','',$image['file']);
1454 $resource = substr($resource,0,strpos($resource,'-'));
1456 $r = q("select * from photo where `resource-id` = '%s' and `uid` = %d limit 1",
1458 intval($owner['uid'])
1462 $public = (($r[0]['allow_cid'] || $r[0]['allow_gid'] || $r[0]['deny_cid'] || $r[0]['deny_gid']) ? 'false' : 'true' );
1463 $msg = replace_macros($tpl,array(
1464 '$path' => xmlify($image['path']),
1465 '$filename' => xmlify($image['file']),
1466 '$msg_guid' => xmlify($image['guid']),
1467 '$guid' => xmlify($r[0]['guid']),
1468 '$handle' => xmlify($image['handle']),
1469 '$public' => xmlify($public),
1470 '$created_at' => xmlify(datetime_convert('UTC','UTC',$r[0]['created'],'Y-m-d H:i:s \U\T\C'))
1474 logger('diaspora_send_photo: base message: ' . $msg, LOGGER_DATA);
1475 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1477 diaspora_transmit($owner,$contact,$slap,$public_batch);
1482 function diaspora_send_followup($item,$owner,$contact,$public_batch = false) {
1485 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1486 $theiraddr = $contact['addr'];
1488 $p = q("select guid from item where parent = %d limit 1",
1492 $parent_guid = $p[0]['guid'];
1496 if($item['verb'] === ACTIVITY_LIKE) {
1497 $tpl = get_markup_template('diaspora_like.tpl');
1499 $target_type = 'Post';
1500 $positive = (($item['deleted']) ? 'false' : 'true');
1503 $tpl = get_markup_template('diaspora_comment.tpl');
1507 $text = html_entity_decode(bb2diaspora($item['body']));
1512 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
1514 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
1516 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1518 $msg = replace_macros($tpl,array(
1519 '$guid' => xmlify($item['guid']),
1520 '$parent_guid' => xmlify($parent_guid),
1521 '$target_type' =>xmlify($target_type),
1522 '$authorsig' => xmlify($authorsig),
1523 '$body' => xmlify($text),
1524 '$positive' => xmlify($positive),
1525 '$handle' => xmlify($myaddr)
1528 logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
1530 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1532 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1536 function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
1540 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1541 $theiraddr = $contact['addr'];
1544 $p = q("select guid from item where parent = %d limit 1",
1548 $parent_guid = $p[0]['guid'];
1552 if($item['verb'] === ACTIVITY_LIKE) {
1553 $tpl = get_markup_template('diaspora_like_relay.tpl');
1555 $target_type = 'Post';
1556 $positive = (($item['deleted']) ? 'false' : 'true');
1559 $tpl = get_markup_template('diaspora_comment_relay.tpl');
1563 $body = $item['body'];
1565 $text = html_entity_decode(bb2diaspora($body));
1567 // fetch the original signature if somebody sent the post to us to relay
1568 // If we are relaying for a reply originating on our own account, there wasn't a 'send to relay'
1569 // action. It wasn't needed. In that case create the original signature and the
1570 // owner (parent author) signature
1571 // comments from other networks will be relayed under our name, with a brief
1572 // preamble to describe what's happening and noting the real author
1574 $r = q("select * from sign where iid = %d limit 1",
1579 $signed_text = $orig_sign['signed_text'];
1580 $authorsig = $orig_sign['signature'];
1581 $handle = $orig_sign['signer'];
1585 $itemcontact = q("select * from contact where `id` = %d limit 1",
1586 intval($item['contact-id'])
1588 if(count($itemcontact)) {
1589 if(! $itemcontact[0]['self']) {
1590 $prefix = sprintf( t('[Relayed] Comment authored by %s from network %s'),
1591 '['. $item['author-name'] . ']' . '(' . $item['author-link'] . ')',
1592 network_to_name($itemcontact['network'])) . "\n";
1593 $body = $prefix . $body;
1599 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
1601 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
1603 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1605 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1606 intval($item['id']),
1607 dbesc($signed_text),
1608 dbesc(base64_encode($authorsig)),
1617 $parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
1619 $msg = replace_macros($tpl,array(
1620 '$guid' => xmlify($item['guid']),
1621 '$parent_guid' => xmlify($parent_guid),
1622 '$target_type' =>xmlify($target_type),
1623 '$authorsig' => xmlify($orig_sign['signature']),
1624 '$parentsig' => xmlify($parentauthorsig),
1625 '$body' => xmlify($text),
1626 '$positive' => xmlify($positive),
1627 '$handle' => xmlify($handle)
1630 logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
1632 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1634 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1640 function diaspora_send_retraction($item,$owner,$contact,$public_batch = false) {
1643 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1645 $tpl = get_markup_template('diaspora_retract.tpl');
1646 $msg = replace_macros($tpl, array(
1647 '$guid' => $item['guid'],
1649 '$handle' => $myaddr
1652 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
1654 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
1659 function diaspora_transmit($owner,$contact,$slap,$public_batch) {
1662 $logid = random_string(4);
1663 logger('diaspora_transmit: ' . $logid . ' ' . (($public_batch) ? $contact['batch'] : $contact['notify']));
1664 post_url((($public_batch) ? $contact['batch'] : $contact['notify']) . '/',$slap);
1665 $return_code = $a->get_curl_code();
1666 logger('diaspora_transmit: ' . $logid . ' returns: ' . $return_code);
1668 if((! $return_code) || (($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))) {
1669 logger('diaspora_transmit: queue message');
1670 // queue message for redelivery
1671 add_to_queue($contact['id'],NETWORK_DIASPORA,$slap,$public_batch);
1675 return(($return_code) ? $return_code : (-1));