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 $enabled = intval(get_config('system','diaspora_enabled'));
14 logger('mod-diaspora: disabled');
18 $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 ",
19 dbesc(NETWORK_DIASPORA),
24 logger('diaspora_public: delivering to: ' . $rr['username']);
25 diaspora_dispatch($rr,$msg);
29 logger('diaspora_public: no subscribers');
34 function diaspora_dispatch($importer,$msg) {
38 $enabled = intval(get_config('system','diaspora_enabled'));
40 logger('mod-diaspora: disabled');
44 // php doesn't like dashes in variable names
46 $msg['message'] = str_replace(
47 array('<activity_streams-photo>','</activity_streams-photo>'),
48 array('<asphoto>','</asphoto>'),
52 $parsed_xml = parse_xml_string($msg['message'],false);
54 $xmlbase = $parsed_xml->post;
56 logger('diaspora_dispatch: ' . print_r($xmlbase,true), LOGGER_DEBUG);
59 if($xmlbase->request) {
60 $ret = diaspora_request($importer,$xmlbase->request);
62 elseif($xmlbase->status_message) {
63 $ret = diaspora_post($importer,$xmlbase->status_message);
65 elseif($xmlbase->profile) {
66 $ret = diaspora_profile($importer,$xmlbase->profile);
68 elseif($xmlbase->comment) {
69 $ret = diaspora_comment($importer,$xmlbase->comment,$msg);
71 elseif($xmlbase->like) {
72 $ret = diaspora_like($importer,$xmlbase->like,$msg);
74 elseif($xmlbase->asphoto) {
75 $ret = diaspora_asphoto($importer,$xmlbase->asphoto);
77 elseif($xmlbase->reshare) {
78 $ret = diaspora_reshare($importer,$xmlbase->reshare);
80 elseif($xmlbase->retraction) {
81 $ret = diaspora_retraction($importer,$xmlbase->retraction,$msg);
83 elseif($xmlbase->signed_retraction) {
84 $ret = diaspora_signed_retraction($importer,$xmlbase->signed_retraction,$msg);
86 elseif($xmlbase->photo) {
87 $ret = diaspora_photo($importer,$xmlbase->photo,$msg);
89 elseif($xmlbase->conversation) {
90 $ret = diaspora_conversation($importer,$xmlbase->conversation,$msg);
92 elseif($xmlbase->message) {
93 $ret = diaspora_message($importer,$xmlbase->message,$msg);
96 logger('diaspora_dispatch: unknown message type: ' . print_r($xmlbase,true));
101 function diaspora_get_contact_by_handle($uid,$handle) {
102 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `addr` = '%s' LIMIT 1",
103 dbesc(NETWORK_DIASPORA),
112 function find_diaspora_person_by_handle($handle) {
114 $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
115 dbesc(NETWORK_DIASPORA),
119 logger('find_diaspora_person_by handle: in cache ' . print_r($r,true), LOGGER_DEBUG);
120 // update record occasionally so it doesn't get stale
121 $d = strtotime($r[0]['updated'] . ' +00:00');
122 if($d > strtotime('now - 14 days'))
126 logger('find_diaspora_person_by_handle: refresh',LOGGER_DEBUG);
127 require_once('include/Scrape.php');
128 $r = probe_url($handle, PROBE_DIASPORA);
129 if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
130 add_fcontact($r,$update);
137 function get_diaspora_key($uri) {
138 logger('Fetching diaspora key for: ' . $uri);
140 $r = find_diaspora_person_by_handle($uri);
147 function diaspora_pubmsg_build($msg,$user,$contact,$prvkey,$pubkey) {
150 logger('diaspora_pubmsg_build: ' . $msg, LOGGER_DATA);
153 $handle = $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
155 // $b64_data = base64_encode($msg);
156 // $b64url_data = base64url_encode($b64_data);
158 $b64url_data = base64url_encode($msg);
160 $data = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
162 $type = 'application/xml';
163 $encoding = 'base64url';
166 $signable_data = $data . '.' . base64url_encode($type) . '.'
167 . base64url_encode($encoding) . '.' . base64url_encode($alg) ;
169 $signature = rsa_sign($signable_data,$prvkey);
170 $sig = base64url_encode($signature);
173 <?xml version='1.0' encoding='UTF-8'?>
174 <diaspora xmlns="https://joindiaspora.com/protocol" xmlns:me="http://salmon-protocol.org/ns/magic-env" >
176 <author_id>$handle</author_id>
179 <me:encoding>base64url</me:encoding>
180 <me:alg>RSA-SHA256</me:alg>
181 <me:data type="application/xml">$data</me:data>
182 <me:sig>$sig</me:sig>
187 logger('diaspora_pubmsg_build: magic_env: ' . $magic_env, LOGGER_DATA);
195 function diaspora_msg_build($msg,$user,$contact,$prvkey,$pubkey,$public = false) {
199 return diaspora_pubmsg_build($msg,$user,$contact,$prvkey,$pubkey);
201 logger('diaspora_msg_build: ' . $msg, LOGGER_DATA);
203 // without a public key nothing will work
206 logger('diaspora_msg_build: pubkey missing: contact id: ' . $contact['id']);
210 $inner_aes_key = random_string(32);
211 $b_inner_aes_key = base64_encode($inner_aes_key);
212 $inner_iv = random_string(16);
213 $b_inner_iv = base64_encode($inner_iv);
215 $outer_aes_key = random_string(32);
216 $b_outer_aes_key = base64_encode($outer_aes_key);
217 $outer_iv = random_string(16);
218 $b_outer_iv = base64_encode($outer_iv);
220 $handle = $user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
222 $padded_data = pkcs5_pad($msg,16);
223 $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
225 $b64_data = base64_encode($inner_encrypted);
228 $b64url_data = base64url_encode($b64_data);
229 $data = str_replace(array("\n","\r"," ","\t"),array('','','',''),$b64url_data);
231 $type = 'application/xml';
232 $encoding = 'base64url';
235 $signable_data = $data . '.' . base64url_encode($type) . '.'
236 . base64url_encode($encoding) . '.' . base64url_encode($alg) ;
238 $signature = rsa_sign($signable_data,$prvkey);
239 $sig = base64url_encode($signature);
241 $decrypted_header = <<< EOT
244 <aes_key>$b_inner_aes_key</aes_key>
245 <author_id>$handle</author_id>
249 $decrypted_header = pkcs5_pad($decrypted_header,16);
251 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
253 $outer_json = json_encode(array('iv' => $b_outer_iv,'key' => $b_outer_aes_key));
255 $encrypted_outer_key_bundle = '';
256 openssl_public_encrypt($outer_json,$encrypted_outer_key_bundle,$pubkey);
258 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
260 logger('outer_bundle: ' . $b64_encrypted_outer_key_bundle . ' key: ' . $pubkey, LOGGER_DATA);
262 $encrypted_header_json_object = json_encode(array('aes_key' => base64_encode($encrypted_outer_key_bundle),
263 'ciphertext' => base64_encode($ciphertext)));
264 $cipher_json = base64_encode($encrypted_header_json_object);
266 $encrypted_header = '<encrypted_header>' . $cipher_json . '</encrypted_header>';
269 <?xml version='1.0' encoding='UTF-8'?>
270 <diaspora xmlns="https://joindiaspora.com/protocol" xmlns:me="http://salmon-protocol.org/ns/magic-env" >
273 <me:encoding>base64url</me:encoding>
274 <me:alg>RSA-SHA256</me:alg>
275 <me:data type="application/xml">$data</me:data>
276 <me:sig>$sig</me:sig>
281 logger('diaspora_msg_build: magic_env: ' . $magic_env, LOGGER_DATA);
288 * diaspora_decode($importer,$xml)
289 * array $importer -> from user table
290 * string $xml -> urldecoded Diaspora salmon
293 * 'message' -> decoded Diaspora XML message
294 * 'author' -> author diaspora handle
295 * 'key' -> author public key (converted to pkcs#8)
297 * Author and key are used elsewhere to save a lookup for verifying replies and likes
301 function diaspora_decode($importer,$xml) {
304 $basedom = parse_xml_string($xml);
306 $children = $basedom->children('https://joindiaspora.com/protocol');
308 if($children->header) {
310 $author_link = str_replace('acct:','',$children->header->author_id);
314 $encrypted_header = json_decode(base64_decode($children->encrypted_header));
316 $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
317 $ciphertext = base64_decode($encrypted_header->ciphertext);
319 $outer_key_bundle = '';
320 openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
322 $j_outer_key_bundle = json_decode($outer_key_bundle);
324 $outer_iv = base64_decode($j_outer_key_bundle->iv);
325 $outer_key = base64_decode($j_outer_key_bundle->key);
327 $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
330 $decrypted = pkcs5_unpad($decrypted);
333 * $decrypted now contains something like
336 * <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
337 * <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
342 * <name>Ryan Hughes</name>
343 * <uri>acct:galaxor@diaspora.pirateship.org</uri>
348 * <author_id>galaxor@diaspora.priateship.org</author_id>
352 * </decrypted_header>
355 logger('decrypted: ' . $decrypted, LOGGER_DEBUG);
356 $idom = parse_xml_string($decrypted,false);
358 $inner_iv = base64_decode($idom->iv);
359 $inner_aes_key = base64_decode($idom->aes_key);
361 $author_link = str_replace('acct:','',$idom->author_id);
365 $dom = $basedom->children(NAMESPACE_SALMON_ME);
367 // figure out where in the DOM tree our data is hiding
369 if($dom->provenance->data)
370 $base = $dom->provenance;
371 elseif($dom->env->data)
377 logger('mod-diaspora: unable to locate salmon data in xml ');
378 http_status_exit(400);
382 // Stash the signature away for now. We have to find their key or it won't be good for anything.
383 $signature = base64url_decode($base->sig);
387 // strip whitespace so our data element will return to one big base64 blob
388 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
391 // stash away some other stuff for later
393 $type = $base->data[0]->attributes()->type[0];
394 $keyhash = $base->sig[0]->attributes()->keyhash[0];
395 $encoding = $base->encoding;
399 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
403 $data = base64url_decode($data);
407 $inner_decrypted = $data;
411 // Decode the encrypted blob
413 $inner_encrypted = base64_decode($data);
414 $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
415 $inner_decrypted = pkcs5_unpad($inner_decrypted);
419 logger('mod-diaspora: Could not retrieve author URI.');
420 http_status_exit(400);
423 // Once we have the author URI, go to the web and try to find their public key
424 // (first this will look it up locally if it is in the fcontact cache)
425 // This will also convert diaspora public key from pkcs#1 to pkcs#8
427 logger('mod-diaspora: Fetching key for ' . $author_link );
428 $key = get_diaspora_key($author_link);
431 logger('mod-diaspora: Could not retrieve author key.');
432 http_status_exit(400);
435 $verify = rsa_verify($signed_data,$signature,$key);
438 logger('mod-diaspora: Message did not verify. Discarding.');
439 http_status_exit(400);
442 logger('mod-diaspora: Message verified.');
444 return array('message' => $inner_decrypted, 'author' => $author_link, 'key' => $key);
449 function diaspora_request($importer,$xml) {
453 $sender_handle = unxmlify($xml->sender_handle);
454 $recipient_handle = unxmlify($xml->recipient_handle);
456 if(! $sender_handle || ! $recipient_handle)
459 $contact = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
463 // perhaps we were already sharing with this person. Now they're sharing with us.
464 // That makes us friends.
466 if($contact['rel'] == CONTACT_IS_FOLLOWER && $importer['page-flags'] != PAGE_COMMUNITY) {
467 q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
468 intval(CONTACT_IS_FRIEND),
469 intval($contact['id']),
470 intval($importer['uid'])
475 $r = q("SELECT `hide-friends` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
476 intval($importer['uid'])
479 if((count($r)) && (! $r[0]['hide-friends']) && (! $contact['hidden'])) {
480 require_once('include/items.php');
482 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
483 intval($importer['uid'])
486 // they are not CONTACT_IS_FOLLOWER anymore but that's what we have in the array
488 if(count($self) && $contact['rel'] == CONTACT_IS_FOLLOWER) {
491 $arr['uri'] = $arr['parent-uri'] = item_new_uri($a->get_hostname(), $importer['uid']);
492 $arr['uid'] = $importer['uid'];
493 $arr['contact-id'] = $self[0]['id'];
495 $arr['type'] = 'wall';
498 $arr['author-name'] = $arr['owner-name'] = $self[0]['name'];
499 $arr['author-link'] = $arr['owner-link'] = $self[0]['url'];
500 $arr['author-avatar'] = $arr['owner-avatar'] = $self[0]['thumb'];
501 $arr['verb'] = ACTIVITY_FRIEND;
502 $arr['object-type'] = ACTIVITY_OBJ_PERSON;
504 $A = '[url=' . $self[0]['url'] . ']' . $self[0]['name'] . '[/url]';
505 $B = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
506 $BPhoto = '[url=' . $contact['url'] . ']' . '[img]' . $contact['thumb'] . '[/img][/url]';
507 $arr['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
509 $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $contact['name'] . '</title>'
510 . '<id>' . $contact['url'] . '/' . $contact['name'] . '</id>';
511 $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $contact['url'] . '" />' . "\n");
512 $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $contact['thumb'] . '" />' . "\n");
513 $arr['object'] .= '</link></object>' . "\n";
514 $arr['last-child'] = 1;
516 $arr['allow_cid'] = $user[0]['allow_cid'];
517 $arr['allow_gid'] = $user[0]['allow_gid'];
518 $arr['deny_cid'] = $user[0]['deny_cid'];
519 $arr['deny_gid'] = $user[0]['deny_gid'];
521 $i = item_store($arr);
523 proc_run('php',"include/notifier.php","activity","$i");
532 $ret = find_diaspora_person_by_handle($sender_handle);
535 if((! count($ret)) || ($ret['network'] != NETWORK_DIASPORA)) {
536 logger('diaspora_request: Cannot resolve diaspora handle ' . $sender_handle . ' for ' . $recipient_handle);
540 $batch = (($ret['batch']) ? $ret['batch'] : implode('/', array_slice(explode('/',$ret['url']),0,3)) . '/receive/public');
544 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`nurl`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
545 VALUES ( %d, '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d) ",
546 intval($importer['uid']),
547 dbesc($ret['network']),
551 dbesc(normalise_link($ret['url'])),
555 dbesc($ret['photo']),
556 dbesc($ret['pubkey']),
557 dbesc($ret['notify']),
563 // find the contact record we just created
565 $contact_record = diaspora_get_contact_by_handle($importer['uid'],$sender_handle);
567 if(! $contact_record) {
568 logger('diaspora_request: unable to locate newly created contact record.');
572 $g = q("select def_gid from user where uid = %d limit 1",
573 intval($importer['uid'])
575 if($g && intval($g[0]['def_gid'])) {
576 require_once('include/group.php');
577 group_add_member($importer['uid'],'',$contact_record['id'],$g[0]['def_gid']);
580 if($importer['page-flags'] == PAGE_NORMAL) {
582 $hash = random_string() . (string) time(); // Generate a confirm_key
584 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime` )
585 VALUES ( %d, %d, %d, %d, '%s', '%s', '%s' )",
586 intval($importer['uid']),
587 intval($contact_record['id']),
590 dbesc( t('Sharing notification from Diaspora network')),
592 dbesc(datetime_convert())
597 // automatic friend approval
599 require_once('include/Photo.php');
601 $photos = import_profile_photo($contact_record['photo'],$importer['uid'],$contact_record['id']);
603 // technically they are sharing with us (CONTACT_IS_SHARING),
604 // but if our page-type is PAGE_COMMUNITY or PAGE_SOAPBOX
605 // we are going to change the relationship and make them a follower.
607 if($importer['page-flags'] == PAGE_FREELOVE)
608 $new_relation = CONTACT_IS_FRIEND;
610 $new_relation = CONTACT_IS_FOLLOWER;
612 $r = q("UPDATE `contact` SET
619 `avatar-date` = '%s',
622 WHERE `id` = %d LIMIT 1
627 intval($new_relation),
628 dbesc(datetime_convert()),
629 dbesc(datetime_convert()),
630 dbesc(datetime_convert()),
631 intval($contact_record['id'])
634 $u = q("select * from user where uid = %d limit 1",intval($importer['uid']));
636 $ret = diaspora_share($u[0],$contact_record);
642 function diaspora_post_allow($importer,$contact) {
643 if(($contact['blocked']) || ($contact['readonly']))
645 if($contact['rel'] == CONTACT_IS_SHARING || $contact['rel'] == CONTACT_IS_FRIEND)
647 if($contact['rel'] == CONTACT_IS_FOLLOWER)
648 if($importer['page-flags'] == PAGE_COMMUNITY)
654 function diaspora_post($importer,$xml) {
657 $guid = notags(unxmlify($xml->guid));
658 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
660 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
664 if(! diaspora_post_allow($importer,$contact)) {
665 logger('diaspora_post: Ignoring this author.');
669 $message_id = $diaspora_handle . ':' . $guid;
670 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
671 intval($importer['uid']),
676 logger('diaspora_post: message exists: ' . $guid);
680 // allocate a guid on our system - we aren't fixing any collisions.
681 // we're ignoring them
683 $g = q("select * from guid where guid = '%s' limit 1",
687 q("insert into guid ( guid ) values ( '%s' )",
692 $created = unxmlify($xml->created_at);
693 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
695 $body = diaspora2bb($xml->raw_message);
701 $tags = get_tags($body);
704 foreach($tags as $tag) {
705 if(strpos($tag,'#') === 0) {
706 if(strpos($tag,'[url='))
709 // don't link tags that are already embedded in links
711 if(preg_match('/\[(.*?)' . preg_quote($tag,'/') . '(.*?)\]/',$body))
713 if(preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag,'/') . '(.*?)\)/',$body))
716 $basetag = str_replace('_',' ',substr($tag,1));
717 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
718 if(strlen($str_tags))
720 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
726 $cnt = preg_match_all('/@\[url=(.*?)\[\/url\]/ism',$body,$matches,PREG_SET_ORDER);
728 foreach($matches as $mtch) {
729 if(strlen($str_tags))
731 $str_tags .= '@[url=' . $mtch[1] . '[/url]';
735 $datarray['uid'] = $importer['uid'];
736 $datarray['contact-id'] = $contact['id'];
737 $datarray['wall'] = 0;
738 $datarray['guid'] = $guid;
739 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
740 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
741 $datarray['private'] = $private;
742 $datarray['parent'] = 0;
743 $datarray['owner-name'] = $contact['name'];
744 $datarray['owner-link'] = $contact['url'];
745 $datarray['owner-avatar'] = $contact['thumb'];
746 $datarray['author-name'] = $contact['name'];
747 $datarray['author-link'] = $contact['url'];
748 $datarray['author-avatar'] = $contact['thumb'];
749 $datarray['body'] = $body;
750 $datarray['tag'] = $str_tags;
751 $datarray['app'] = 'Diaspora';
753 // if empty content it might be a photo that hasn't arrived yet. If a photo arrives, we'll make it visible.
755 $datarray['visible'] = ((strlen($body)) ? 1 : 0);
757 $message_id = item_store($datarray);
760 q("update item set plink = '%s' where id = %d limit 1",
761 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
770 function diaspora_reshare($importer,$xml) {
772 logger('diaspora_reshare: init: ' . print_r($xml,true));
775 $guid = notags(unxmlify($xml->guid));
776 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
779 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
783 if(! diaspora_post_allow($importer,$contact)) {
784 logger('diaspora_reshare: Ignoring this author: ' . $diaspora_handle . ' ' . print_r($xml,true));
788 $message_id = $diaspora_handle . ':' . $guid;
789 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
790 intval($importer['uid']),
795 logger('diaspora_reshare: message exists: ' . $guid);
799 $orig_author = notags(unxmlify($xml->root_diaspora_id));
800 $orig_guid = notags(unxmlify($xml->root_guid));
802 $source_url = 'https://' . substr($orig_author,strpos($orig_author,'@')+1) . '/p/' . $orig_guid . '.xml';
803 $x = fetch_url($source_url);
805 $x = fetch_url(str_replace('https://','http://',$source_url));
807 logger('diaspora_reshare: unable to fetch source url ' . $source_url);
810 logger('diaspora_reshare: source: ' . $x);
812 $x = str_replace(array('<activity_streams-photo>','</activity_streams-photo>'),array('<asphoto>','</asphoto>'),$x);
813 $source_xml = parse_xml_string($x,false);
815 if(strlen($source_xml->post->asphoto->objectId) && ($source_xml->post->asphoto->objectId != 0) && ($source_xml->post->asphoto->image_url)) {
816 $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
817 $body = scale_external_images($body,false);
819 elseif($source_xml->post->asphoto->image_url) {
820 $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
821 $body = scale_external_images($body);
823 elseif($source_xml->post->status_message) {
824 $body = diaspora2bb($source_xml->post->status_message->raw_message);
825 $body = scale_external_images($body);
829 logger('diaspora_reshare: no reshare content found: ' . print_r($source_xml,true));
833 logger('diaspora_reshare: empty body: source= ' . $x);
837 $person = find_diaspora_person_by_handle($orig_author);
839 if(is_array($person) && x($person,'name') && x($person,'url'))
840 $details = '[url=' . $person['url'] . ']' . $person['name'] . '[/url]';
842 $details = $orig_author;
844 $prefix = '♲ ' . $details . "\n";
847 // allocate a guid on our system - we aren't fixing any collisions.
848 // we're ignoring them
850 $g = q("select * from guid where guid = '%s' limit 1",
854 q("insert into guid ( guid ) values ( '%s' )",
859 $created = unxmlify($xml->created_at);
860 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
866 $tags = get_tags($body);
869 foreach($tags as $tag) {
870 if(strpos($tag,'#') === 0) {
871 if(strpos($tag,'[url='))
874 // don't link tags that are already embedded in links
876 if(preg_match('/\[(.*?)' . preg_quote($tag,'/') . '(.*?)\]/',$body))
878 if(preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag,'/') . '(.*?)\)/',$body))
882 $basetag = str_replace('_',' ',substr($tag,1));
883 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
884 if(strlen($str_tags))
886 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
892 $datarray['uid'] = $importer['uid'];
893 $datarray['contact-id'] = $contact['id'];
894 $datarray['wall'] = 0;
895 $datarray['guid'] = $guid;
896 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
897 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
898 $datarray['private'] = $private;
899 $datarray['parent'] = 0;
900 $datarray['owner-name'] = $contact['name'];
901 $datarray['owner-link'] = $contact['url'];
902 $datarray['owner-avatar'] = $contact['thumb'];
903 $datarray['author-name'] = $contact['name'];
904 $datarray['author-link'] = $contact['url'];
905 $datarray['author-avatar'] = $contact['thumb'];
906 $datarray['body'] = $prefix . $body;
907 $datarray['tag'] = $str_tags;
908 $datarray['app'] = 'Diaspora';
910 $message_id = item_store($datarray);
913 q("update item set plink = '%s' where id = %d limit 1",
914 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
924 function diaspora_asphoto($importer,$xml) {
925 logger('diaspora_asphoto called');
928 $guid = notags(unxmlify($xml->guid));
929 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
931 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
935 if(! diaspora_post_allow($importer,$contact)) {
936 logger('diaspora_asphoto: Ignoring this author.');
940 $message_id = $diaspora_handle . ':' . $guid;
941 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
942 intval($importer['uid']),
947 logger('diaspora_asphoto: message exists: ' . $guid);
951 // allocate a guid on our system - we aren't fixing any collisions.
952 // we're ignoring them
954 $g = q("select * from guid where guid = '%s' limit 1",
958 q("insert into guid ( guid ) values ( '%s' )",
963 $created = unxmlify($xml->created_at);
964 $private = ((unxmlify($xml->public) == 'false') ? 1 : 0);
966 if(strlen($xml->objectId) && ($xml->objectId != 0) && ($xml->image_url)) {
967 $body = '[url=' . notags(unxmlify($xml->image_url)) . '][img]' . notags(unxmlify($xml->objectId)) . '[/img][/url]' . "\n";
968 $body = scale_external_images($body,false);
970 elseif($xml->image_url) {
971 $body = '[img]' . notags(unxmlify($xml->image_url)) . '[/img]' . "\n";
972 $body = scale_external_images($body);
975 logger('diaspora_asphoto: no photo url found.');
982 $datarray['uid'] = $importer['uid'];
983 $datarray['contact-id'] = $contact['id'];
984 $datarray['wall'] = 0;
985 $datarray['guid'] = $guid;
986 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
987 $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
988 $datarray['private'] = $private;
989 $datarray['parent'] = 0;
990 $datarray['owner-name'] = $contact['name'];
991 $datarray['owner-link'] = $contact['url'];
992 $datarray['owner-avatar'] = $contact['thumb'];
993 $datarray['author-name'] = $contact['name'];
994 $datarray['author-link'] = $contact['url'];
995 $datarray['author-avatar'] = $contact['thumb'];
996 $datarray['body'] = $body;
998 $datarray['app'] = 'Diaspora/Cubbi.es';
1000 $message_id = item_store($datarray);
1003 q("update item set plink = '%s' where id = %d limit 1",
1004 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
1018 function diaspora_comment($importer,$xml,$msg) {
1021 $guid = notags(unxmlify($xml->guid));
1022 $parent_guid = notags(unxmlify($xml->parent_guid));
1023 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1024 $target_type = notags(unxmlify($xml->target_type));
1025 $text = unxmlify($xml->text);
1026 $author_signature = notags(unxmlify($xml->author_signature));
1028 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
1030 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1032 logger('diaspora_comment: cannot find contact: ' . $msg['author']);
1036 if(! diaspora_post_allow($importer,$contact)) {
1037 logger('diaspora_comment: Ignoring this author.');
1041 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1042 intval($importer['uid']),
1046 logger('diaspora_comment: our comment just got relayed back to us (or there was a guid collision) : ' . $guid);
1050 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1051 intval($importer['uid']),
1055 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
1058 $parent_item = $r[0];
1060 $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
1062 $author_signature = base64_decode($author_signature);
1064 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
1069 $person = find_diaspora_person_by_handle($diaspora_handle);
1071 if(is_array($person) && x($person,'pubkey'))
1072 $key = $person['pubkey'];
1074 logger('diaspora_comment: unable to find author details');
1079 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
1080 logger('diaspora_comment: verification failed.');
1084 if($parent_author_signature) {
1085 $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle;
1087 $parent_author_signature = base64_decode($parent_author_signature);
1091 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
1092 logger('diaspora_comment: owner verification failed.');
1097 // Phew! Everything checks out. Now create an item.
1099 $body = diaspora2bb($text);
1101 $message_id = $diaspora_handle . ':' . $guid;
1103 $datarray = array();
1107 $tags = get_tags($body);
1110 foreach($tags as $tag) {
1111 if(strpos($tag,'#') === 0) {
1112 if(strpos($tag,'[url='))
1115 // don't link tags that are already embedded in links
1117 if(preg_match('/\[(.*?)' . preg_quote($tag,'/') . '(.*?)\]/',$body))
1119 if(preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag,'/') . '(.*?)\)/',$body))
1123 $basetag = str_replace('_',' ',substr($tag,1));
1124 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
1125 if(strlen($str_tags))
1127 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1133 $datarray['uid'] = $importer['uid'];
1134 $datarray['contact-id'] = $contact['id'];
1135 $datarray['wall'] = $parent_item['wall'];
1136 $datarray['gravity'] = GRAVITY_COMMENT;
1137 $datarray['guid'] = $guid;
1138 $datarray['uri'] = $message_id;
1139 $datarray['parent-uri'] = $parent_item['uri'];
1141 // No timestamps for comments? OK, we'll the use current time.
1142 $datarray['created'] = $datarray['edited'] = datetime_convert();
1143 $datarray['private'] = $parent_item['private'];
1145 $datarray['owner-name'] = $parent_item['owner-name'];
1146 $datarray['owner-link'] = $parent_item['owner-link'];
1147 $datarray['owner-avatar'] = $parent_item['owner-avatar'];
1149 $datarray['author-name'] = $person['name'];
1150 $datarray['author-link'] = $person['url'];
1151 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
1152 $datarray['body'] = $body;
1153 $datarray['tag'] = $str_tags;
1155 // We can't be certain what the original app is if the message is relayed.
1156 if(($parent_item['origin']) && (! $parent_author_signature))
1157 $datarray['app'] = 'Diaspora';
1159 $message_id = item_store($datarray);
1162 q("update item set plink = '%s' where id = %d limit 1",
1163 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
1168 if(($parent_item['origin']) && (! $parent_author_signature)) {
1169 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1170 intval($message_id),
1171 dbesc($author_signed_data),
1172 dbesc(base64_encode($author_signature)),
1173 dbesc($diaspora_handle)
1176 // if the message isn't already being relayed, notify others
1177 // the existence of parent_author_signature means the parent_author or owner
1178 // is already relaying.
1180 proc_run('php','include/notifier.php','comment',$message_id);
1183 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0 ",
1184 dbesc($parent_item['uri']),
1185 intval($importer['uid'])
1188 if(count($myconv)) {
1189 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
1191 foreach($myconv as $conv) {
1193 // now if we find a match, it means we're in this conversation
1195 if(! link_compare($conv['author-link'],$importer_url))
1198 require_once('include/enotify.php');
1200 $conv_parent = $conv['parent'];
1203 'type' => NOTIFY_COMMENT,
1204 'notify_flags' => $importer['notify-flags'],
1205 'language' => $importer['language'],
1206 'to_name' => $importer['username'],
1207 'to_email' => $importer['email'],
1208 'uid' => $importer['uid'],
1209 'item' => $datarray,
1210 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id,
1211 'source_name' => $datarray['author-name'],
1212 'source_link' => $datarray['author-link'],
1213 'source_photo' => $datarray['author-avatar'],
1214 'verb' => ACTIVITY_POST,
1216 'parent' => $conv_parent,
1220 // only send one notification
1230 function diaspora_conversation($importer,$xml,$msg) {
1234 $guid = notags(unxmlify($xml->guid));
1235 $subject = notags(unxmlify($xml->subject));
1236 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1237 $participant_handles = notags(unxmlify($xml->participant_handles));
1238 $created_at = datetime_convert('UTC','UTC',notags(unxmlify($xml->created_at)));
1240 $parent_uri = $diaspora_handle . ':' . $guid;
1242 $messages = $xml->message;
1244 if(! count($messages)) {
1245 logger('diaspora_conversation: empty conversation');
1249 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1251 logger('diaspora_conversation: cannot find contact: ' . $msg['author']);
1255 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
1256 logger('diaspora_conversation: Ignoring this author.');
1260 $conversation = null;
1262 $c = q("select * from conv where uid = %d and guid = '%s' limit 1",
1263 intval($importer['uid']),
1267 $conversation = $c[0];
1269 $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
1270 intval($importer['uid']),
1272 dbesc($diaspora_handle),
1273 dbesc(datetime_convert('UTC','UTC',$created_at)),
1274 dbesc(datetime_convert()),
1276 dbesc($participant_handles)
1279 $c = q("select * from conv where uid = %d and guid = '%s' limit 1",
1280 intval($importer['uid']),
1284 $conversation = $c[0];
1286 if(! $conversation) {
1287 logger('diaspora_conversation: unable to create conversation.');
1291 foreach($messages as $mesg) {
1295 $msg_guid = notags(unxmlify($mesg->guid));
1296 $msg_parent_guid = notags(unxmlify($mesg->parent_guid));
1297 $msg_parent_author_signature = notags(unxmlify($mesg->parent_author_signature));
1298 $msg_author_signature = notags(unxmlify($mesg->author_signature));
1299 $msg_text = unxmlify($mesg->text);
1300 $msg_created_at = datetime_convert('UTC','UTC',notags(unxmlify($mesg->created_at)));
1301 $msg_diaspora_handle = notags(unxmlify($mesg->diaspora_handle));
1302 $msg_conversation_guid = notags(unxmlify($mesg->conversation_guid));
1303 if($msg_conversation_guid != $guid) {
1304 logger('diaspora_conversation: message conversation guid does not belong to the current conversation. ' . $xml);
1308 $body = diaspora2bb($msg_text);
1309 $message_id = $msg_diaspora_handle . ':' . $msg_guid;
1311 $author_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($mesg->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid;
1313 $author_signature = base64_decode($msg_author_signature);
1315 if(strcasecmp($msg_diaspora_handle,$msg['author']) == 0) {
1320 $person = find_diaspora_person_by_handle($msg_diaspora_handle);
1322 if(is_array($person) && x($person,'pubkey'))
1323 $key = $person['pubkey'];
1325 logger('diaspora_conversation: unable to find author details');
1330 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
1331 logger('diaspora_conversation: verification failed.');
1335 if($msg_parent_author_signature) {
1336 $owner_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($mesg->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid;
1338 $parent_author_signature = base64_decode($msg_parent_author_signature);
1342 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
1343 logger('diaspora_conversation: owner verification failed.');
1348 $r = q("select id from mail where `uri` = '%s' limit 1",
1352 logger('diaspora_conversation: duplicate message already delivered.', LOGGER_DEBUG);
1356 q("insert into mail ( `uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`) values ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
1357 intval($importer['uid']),
1359 intval($conversation['id']),
1360 dbesc($person['name']),
1361 dbesc($person['photo']),
1362 dbesc($person['url']),
1363 intval($contact['id']),
1370 dbesc($msg_created_at)
1373 q("update conv set updated = '%s' where id = %d limit 1",
1374 dbesc(datetime_convert()),
1375 intval($conversation['id'])
1378 require_once('include/enotify.php');
1380 'type' => NOTIFY_MAIL,
1381 'notify_flags' => $importer['notify-flags'],
1382 'language' => $importer['language'],
1383 'to_name' => $importer['username'],
1384 'to_email' => $importer['email'],
1385 'uid' =>$importer['importer_uid'],
1386 'item' => array('subject' => $subject, 'body' => $body),
1387 'source_name' => $person['name'],
1388 'source_link' => $person['url'],
1389 'source_photo' => $person['thumb'],
1390 'verb' => ACTIVITY_POST,
1398 function diaspora_message($importer,$xml,$msg) {
1402 $msg_guid = notags(unxmlify($xml->guid));
1403 $msg_parent_guid = notags(unxmlify($xml->parent_guid));
1404 $msg_parent_author_signature = notags(unxmlify($xml->parent_author_signature));
1405 $msg_author_signature = notags(unxmlify($xml->author_signature));
1406 $msg_text = unxmlify($xml->text);
1407 $msg_created_at = datetime_convert('UTC','UTC',notags(unxmlify($xml->created_at)));
1408 $msg_diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1409 $msg_conversation_guid = notags(unxmlify($xml->conversation_guid));
1411 $parent_uri = $diaspora_handle . ':' . $msg_parent_guid;
1413 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg_diaspora_handle);
1415 logger('diaspora_message: cannot find contact: ' . $msg_diaspora_handle);
1419 if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) {
1420 logger('diaspora_message: Ignoring this author.');
1424 $conversation = null;
1426 $c = q("select * from conv where uid = %d and guid = '%s' limit 1",
1427 intval($importer['uid']),
1428 dbesc($msg_conversation_guid)
1431 $conversation = $c[0];
1433 logger('diaspora_message: conversation not available.');
1439 $body = diaspora2bb($msg_text);
1440 $message_id = $msg_diaspora_handle . ':' . $msg_guid;
1442 $author_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($xml->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid;
1445 $author_signature = base64_decode($msg_author_signature);
1447 $person = find_diaspora_person_by_handle($msg_diaspora_handle);
1448 if(is_array($person) && x($person,'pubkey'))
1449 $key = $person['pubkey'];
1451 logger('diaspora_message: unable to find author details');
1455 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
1456 logger('diaspora_message: verification failed.');
1460 $r = q("select id from mail where `uri` = '%s' and uid = %d limit 1",
1462 intval($importer['uid'])
1465 logger('diaspora_message: duplicate message already delivered.', LOGGER_DEBUG);
1469 q("insert into mail ( `uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`) values ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
1470 intval($importer['uid']),
1472 intval($conversation['id']),
1473 dbesc($person['name']),
1474 dbesc($person['photo']),
1475 dbesc($person['url']),
1476 intval($contact['id']),
1477 dbesc($conversation['subject']),
1483 dbesc($msg_created_at)
1486 q("update conv set updated = '%s' where id = %d limit 1",
1487 dbesc(datetime_convert()),
1488 intval($conversation['id'])
1495 function diaspora_photo($importer,$xml,$msg) {
1499 logger('diaspora_photo: init',LOGGER_DEBUG);
1501 $remote_photo_path = notags(unxmlify($xml->remote_photo_path));
1503 $remote_photo_name = notags(unxmlify($xml->remote_photo_name));
1505 $status_message_guid = notags(unxmlify($xml->status_message_guid));
1507 $guid = notags(unxmlify($xml->guid));
1509 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1511 $public = notags(unxmlify($xml->public));
1513 $created_at = notags(unxmlify($xml_created_at));
1515 logger('diaspora_photo: status_message_guid: ' . $status_message_guid, LOGGER_DEBUG);
1517 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1519 logger('diaspora_photo: contact record not found: ' . $msg['author'] . ' handle: ' . $diaspora_handle);
1523 if(! diaspora_post_allow($importer,$contact)) {
1524 logger('diaspora_photo: Ignoring this author.');
1528 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1529 intval($importer['uid']),
1530 dbesc($status_message_guid)
1533 logger('diaspora_photo: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
1537 $parent_item = $r[0];
1539 $link_text = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n";
1541 $link_text = scale_external_images($link_text);
1543 if(strpos($parent_item['body'],$link_text) === false) {
1544 $r = q("update item set `body` = '%s', `visible` = 1 where `id` = %d and `uid` = %d limit 1",
1545 dbesc($link_text . $parent_item['body']),
1546 intval($parent_item['id']),
1547 intval($parent_item['uid'])
1557 function diaspora_like($importer,$xml,$msg) {
1560 $guid = notags(unxmlify($xml->guid));
1561 $parent_guid = notags(unxmlify($xml->parent_guid));
1562 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1563 $target_type = notags(unxmlify($xml->target_type));
1564 $positive = notags(unxmlify($xml->positive));
1565 $author_signature = notags(unxmlify($xml->author_signature));
1567 $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : '');
1569 // likes on comments not supported here and likes on photos not supported by Diaspora
1571 if($target_type !== 'Post')
1574 $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
1576 logger('diaspora_like: cannot find contact: ' . $msg['author']);
1580 if(! diaspora_post_allow($importer,$contact)) {
1581 logger('diaspora_like: Ignoring this author.');
1585 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1586 intval($importer['uid']),
1590 logger('diaspora_like: parent item not found: ' . $guid);
1594 $parent_item = $r[0];
1596 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1597 intval($importer['uid']),
1601 if($positive === 'true') {
1602 logger('diaspora_like: duplicate like: ' . $guid);
1605 if($positive === 'false') {
1606 q("UPDATE `item` SET `deleted` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
1607 intval($r[0]['id']),
1608 intval($importer['uid'])
1611 // send notification via proc_run()
1615 if($positive === 'false') {
1616 logger('diaspora_like: unlike received with no corresponding like');
1620 $author_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
1622 $author_signature = base64_decode($author_signature);
1624 if(strcasecmp($diaspora_handle,$msg['author']) == 0) {
1629 $person = find_diaspora_person_by_handle($diaspora_handle);
1630 if(is_array($person) && x($person,'pubkey'))
1631 $key = $person['pubkey'];
1633 logger('diaspora_like: unable to find author details');
1638 if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) {
1639 logger('diaspora_like: verification failed.');
1643 if($parent_author_signature) {
1645 $owner_signed_data = $guid . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $diaspora_handle;
1647 $parent_author_signature = base64_decode($parent_author_signature);
1651 if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) {
1652 logger('diaspora_like: owner verification failed.');
1657 // Phew! Everything checks out. Now create an item.
1659 $uri = $diaspora_handle . ':' . $guid;
1661 $activity = ACTIVITY_LIKE;
1662 $post_type = (($parent_item['resource-id']) ? t('photo') : t('status'));
1663 $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
1664 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . '" />' . "\n") ;
1665 $body = $parent_item['body'];
1670 <type>$objtype</type>
1672 <id>{$parent_item['uri']}</id>
1675 <content>$body</content>
1678 $bodyverb = t('%1$s likes %2$s\'s %3$s');
1683 $arr['uid'] = $importer['uid'];
1684 $arr['guid'] = $guid;
1685 $arr['contact-id'] = $contact['id'];
1686 $arr['type'] = 'activity';
1687 $arr['wall'] = $parent_item['wall'];
1688 $arr['gravity'] = GRAVITY_LIKE;
1689 $arr['parent'] = $parent_item['id'];
1690 $arr['parent-uri'] = $parent_item['uri'];
1692 $arr['owner-name'] = $parent_item['name'];
1693 $arr['owner-link'] = $parent_item['url'];
1694 $arr['owner-avatar'] = $parent_item['thumb'];
1696 $arr['author-name'] = $person['name'];
1697 $arr['author-link'] = $person['url'];
1698 $arr['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
1700 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
1701 $alink = '[url=' . $parent_item['author-link'] . ']' . $parent_item['author-name'] . '[/url]';
1702 $plink = '[url=' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . ']' . $post_type . '[/url]';
1703 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
1705 $arr['app'] = 'Diaspora';
1707 $arr['private'] = $parent_item['private'];
1708 $arr['verb'] = $activity;
1709 $arr['object-type'] = $objtype;
1710 $arr['object'] = $obj;
1711 $arr['visible'] = 1;
1713 $arr['last-child'] = 0;
1715 $message_id = item_store($arr);
1719 q("update item set plink = '%s' where id = %d limit 1",
1720 dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
1725 if(! $parent_author_signature) {
1726 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1727 intval($message_id),
1728 dbesc($author_signed_data),
1729 dbesc(base64_encode($author_signature)),
1730 dbesc($diaspora_handle)
1734 // if the message isn't already being relayed, notify others
1735 // the existence of parent_author_signature means the parent_author or owner
1736 // is already relaying. The parent_item['origin'] indicates the message was created on our system
1738 if(($parent_item['origin']) && (! $parent_author_signature))
1739 proc_run('php','include/notifier.php','comment',$message_id);
1744 function diaspora_retraction($importer,$xml) {
1747 $guid = notags(unxmlify($xml->guid));
1748 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1749 $type = notags(unxmlify($xml->type));
1751 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1755 if($type === 'Person') {
1756 require_once('include/Contact.php');
1757 contact_remove($contact['id']);
1759 elseif($type === 'Post') {
1760 $r = q("select * from item where guid = '%s' and uid = %d and not file like '%%[%%' limit 1",
1762 intval($importer['uid'])
1765 if(link_compare($r[0]['author-link'],$contact['url'])) {
1766 q("update item set `deleted` = 1, `changed` = '%s' where `id` = %d limit 1",
1767 dbesc(datetime_convert()),
1778 function diaspora_signed_retraction($importer,$xml,$msg) {
1781 $guid = notags(unxmlify($xml->target_guid));
1782 $diaspora_handle = notags(unxmlify($xml->sender_handle));
1783 $type = notags(unxmlify($xml->target_type));
1784 $sig = notags(unxmlify($xml->target_author_signature));
1786 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1788 logger('diaspora_signed_retraction: no contact');
1792 // this may not yet work for comments. Need to see how the relaying works
1793 // and figure out who signs it.
1796 $signed_data = $guid . ';' . $type ;
1798 $sig = base64_decode($sig);
1802 if(! rsa_verify($signed_data,$sig,$key,'sha256')) {
1803 logger('diaspora_signed_retraction: owner verification failed.' . print_r($msg,true));
1807 if($type === 'StatusMessage') {
1808 $r = q("select * from item where guid = '%s' and uid = %d and not file like '%%[%%' limit 1",
1810 intval($importer['uid'])
1813 if(link_compare($r[0]['author-link'],$contact['url'])) {
1814 q("update item set `deleted` = 1, `changed` = '%s' where `id` = %d limit 1",
1815 dbesc(datetime_convert()),
1822 logger('diaspora_signed_retraction: unknown type: ' . $type);
1828 function diaspora_profile($importer,$xml) {
1831 $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
1833 $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle);
1837 if($contact['blocked']) {
1838 logger('diaspora_post: Ignoring this author.');
1842 $name = unxmlify($xml->first_name) . ((strlen($xml->last_name)) ? ' ' . unxmlify($xml->last_name) : '');
1843 $image_url = unxmlify($xml->image_url);
1844 $birthday = unxmlify($xml->birthday);
1846 $r = q("SELECT DISTINCT ( `resource-id` ) FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `album` = 'Contact Photos' ",
1847 intval($importer['uid']),
1848 intval($contact['id'])
1850 $oldphotos = ((count($r)) ? $r : null);
1852 require_once('include/Photo.php');
1854 $images = import_profile_photo($image_url,$importer['uid'],$contact['id']);
1856 // Generic birthday. We don't know the timezone. The year is irrelevant.
1858 $birthday = str_replace('1000','1901',$birthday);
1860 $birthday = datetime_convert('UTC','UTC',$birthday,'Y-m-d');
1862 // this is to prevent multiple birthday notifications in a single year
1863 // if we already have a stored birthday and the 'm-d' part hasn't changed, preserve the entry, which will preserve the notify year
1865 if(substr($birthday,5) === substr($contact['bd'],5))
1866 $birthday = $contact['bd'];
1868 // TODO: update name on item['author-name'] if the name changed. See consume_feed()
1869 // Not doing this currently because D* protocol is scheduled for revision soon.
1871 $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",
1873 dbesc(datetime_convert()),
1877 dbesc(datetime_convert()),
1879 intval($contact['id']),
1880 intval($importer['uid'])
1885 foreach($oldphotos as $ph) {
1886 q("DELETE FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `album` = 'Contact Photos' AND `resource-id` = '%s' ",
1887 intval($importer['uid']),
1888 intval($contact['id']),
1889 dbesc($ph['resource-id'])
1899 function diaspora_share($me,$contact) {
1901 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1902 $theiraddr = $contact['addr'];
1904 $tpl = get_markup_template('diaspora_share.tpl');
1905 $msg = replace_macros($tpl, array(
1906 '$sender' => $myaddr,
1907 '$recipient' => $theiraddr
1910 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
1912 return(diaspora_transmit($owner,$contact,$slap, false));
1915 function diaspora_unshare($me,$contact) {
1918 $myaddr = $me['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1920 $tpl = get_markup_template('diaspora_retract.tpl');
1921 $msg = replace_macros($tpl, array(
1922 '$guid' => $me['guid'],
1923 '$type' => 'Person',
1924 '$handle' => $myaddr
1927 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
1929 return(diaspora_transmit($owner,$contact,$slap, false));
1935 function diaspora_send_status($item,$owner,$contact,$public_batch = false) {
1938 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
1939 $theiraddr = $contact['addr'];
1943 $title = $item['title'];
1944 $body = $item['body'];
1947 // We're trying to match Diaspora's split message/photo protocol but
1948 // all the photos are displayed on D* as links and not img's - even
1949 // though we're sending pretty much precisely what they send us when
1950 // doing the same operation.
1951 // Commented out for now, we'll use bb2diaspora to convert photos to markdown
1952 // which seems to get through intact.
1954 $cnt = preg_match_all('|\[img\](.*?)\[\/img\]|',$body,$matches,PREG_SET_ORDER);
1956 foreach($matches as $mtch) {
1958 $detail['str'] = $mtch[0];
1959 $detail['path'] = dirname($mtch[1]) . '/';
1960 $detail['file'] = basename($mtch[1]);
1961 $detail['guid'] = $item['guid'];
1962 $detail['handle'] = $myaddr;
1963 $images[] = $detail;
1964 $body = str_replace($detail['str'],$mtch[1],$body);
1968 $body = xmlify(html_entity_decode(bb2diaspora($body)));
1971 $body = xmlify('**' . html_entity_decode($title) . '**' . "\n") . $body;
1974 if($item['attach']) {
1975 $cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism',$item['attach'],$matches,PREG_SET_ORDER);
1977 $body .= "\n" . t('Attachments:') . "\n";
1978 foreach($matches as $mtch) {
1979 $body .= '[' . $mtch[3] . '](' . $mtch[1] . ')' . "\n";
1985 $public = (($item['private']) ? 'false' : 'true');
1987 require_once('include/datetime.php');
1988 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d H:i:s \U\T\C');
1990 $tpl = get_markup_template('diaspora_post.tpl');
1991 $msg = replace_macros($tpl, array(
1993 '$guid' => $item['guid'],
1994 '$handle' => xmlify($myaddr),
1995 '$public' => $public,
1996 '$created' => $created
1999 logger('diaspora_send_status: ' . $owner['username'] . ' -> ' . $contact['name'] . ' base message: ' . $msg, LOGGER_DATA);
2001 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
2003 $return_code = diaspora_transmit($owner,$contact,$slap,$public_batch);
2005 if(count($images)) {
2006 diaspora_send_images($item,$owner,$contact,$images,$public_batch);
2009 return $return_code;
2013 function diaspora_send_images($item,$owner,$contact,$images,$public_batch = false) {
2015 if(! count($images))
2017 $mysite = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://') + 3) . '/photo';
2019 $tpl = get_markup_template('diaspora_photo.tpl');
2020 foreach($images as $image) {
2021 if(! stristr($image['path'],$mysite))
2023 $resource = str_replace('.jpg','',$image['file']);
2024 $resource = substr($resource,0,strpos($resource,'-'));
2026 $r = q("select * from photo where `resource-id` = '%s' and `uid` = %d limit 1",
2028 intval($owner['uid'])
2032 $public = (($r[0]['allow_cid'] || $r[0]['allow_gid'] || $r[0]['deny_cid'] || $r[0]['deny_gid']) ? 'false' : 'true' );
2033 $msg = replace_macros($tpl,array(
2034 '$path' => xmlify($image['path']),
2035 '$filename' => xmlify($image['file']),
2036 '$msg_guid' => xmlify($image['guid']),
2037 '$guid' => xmlify($r[0]['guid']),
2038 '$handle' => xmlify($image['handle']),
2039 '$public' => xmlify($public),
2040 '$created_at' => xmlify(datetime_convert('UTC','UTC',$r[0]['created'],'Y-m-d H:i:s \U\T\C'))
2044 logger('diaspora_send_photo: base message: ' . $msg, LOGGER_DATA);
2045 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
2047 diaspora_transmit($owner,$contact,$slap,$public_batch);
2052 function diaspora_send_followup($item,$owner,$contact,$public_batch = false) {
2055 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
2056 $theiraddr = $contact['addr'];
2058 $p = q("select guid from item where parent = %d limit 1",
2062 $parent_guid = $p[0]['guid'];
2066 if($item['verb'] === ACTIVITY_LIKE) {
2067 $tpl = get_markup_template('diaspora_like.tpl');
2069 $target_type = 'Post';
2070 $positive = (($item['deleted']) ? 'false' : 'true');
2073 $tpl = get_markup_template('diaspora_comment.tpl');
2077 $text = html_entity_decode(bb2diaspora($item['body']));
2082 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
2084 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
2086 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
2088 $msg = replace_macros($tpl,array(
2089 '$guid' => xmlify($item['guid']),
2090 '$parent_guid' => xmlify($parent_guid),
2091 '$target_type' =>xmlify($target_type),
2092 '$authorsig' => xmlify($authorsig),
2093 '$body' => xmlify($text),
2094 '$positive' => xmlify($positive),
2095 '$handle' => xmlify($myaddr)
2098 logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
2100 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
2102 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
2106 function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
2110 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
2111 $theiraddr = $contact['addr'];
2114 $p = q("select guid from item where parent = %d limit 1",
2118 $parent_guid = $p[0]['guid'];
2122 if($item['verb'] === ACTIVITY_LIKE) {
2123 $tpl = get_markup_template('diaspora_like_relay.tpl');
2125 $target_type = 'Post';
2126 $positive = (($item['deleted']) ? 'false' : 'true');
2129 $tpl = get_markup_template('diaspora_comment_relay.tpl');
2133 $body = $item['body'];
2135 $text = html_entity_decode(bb2diaspora($body));
2137 // fetch the original signature if somebody sent the post to us to relay
2138 // If we are relaying for a reply originating on our own account, there wasn't a 'send to relay'
2139 // action. It wasn't needed. In that case create the original signature and the
2140 // owner (parent author) signature
2141 // comments from other networks will be relayed under our name, with a brief
2142 // preamble to describe what's happening and noting the real author
2144 $r = q("select * from sign where iid = %d limit 1",
2149 $signed_text = $orig_sign['signed_text'];
2150 $authorsig = $orig_sign['signature'];
2151 $handle = $orig_sign['signer'];
2155 $itemcontact = q("select * from contact where `id` = %d limit 1",
2156 intval($item['contact-id'])
2158 if(count($itemcontact)) {
2159 if(! $itemcontact[0]['self']) {
2160 $prefix = sprintf( t('[Relayed] Comment authored by %s from network %s'),
2161 '['. $item['author-name'] . ']' . '(' . $item['author-link'] . ')',
2162 network_to_name($itemcontact['network'])) . "\n";
2163 $body = $prefix . $body;
2169 $signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
2171 $signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
2173 $authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
2175 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
2176 intval($item['id']),
2177 dbesc($signed_text),
2178 dbesc(base64_encode($authorsig)),
2187 $parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
2189 $msg = replace_macros($tpl,array(
2190 '$guid' => xmlify($item['guid']),
2191 '$parent_guid' => xmlify($parent_guid),
2192 '$target_type' =>xmlify($target_type),
2193 '$authorsig' => xmlify($orig_sign['signature']),
2194 '$parentsig' => xmlify($parentauthorsig),
2195 '$body' => xmlify($text),
2196 '$positive' => xmlify($positive),
2197 '$handle' => xmlify($handle)
2200 logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
2202 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
2204 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
2210 function diaspora_send_retraction($item,$owner,$contact,$public_batch = false) {
2213 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
2215 $signed_text = $item['guid'] . ';' . 'StatusMessage';
2217 $tpl = get_markup_template('diaspora_signed_retract.tpl');
2218 $msg = replace_macros($tpl, array(
2219 '$guid' => $item['guid'],
2220 '$type' => 'StatusMessage',
2221 '$handle' => $myaddr,
2222 '$signature' => base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'))
2225 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
2227 return(diaspora_transmit($owner,$contact,$slap,$public_batch));
2230 function diaspora_send_mail($item,$owner,$contact) {
2233 $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
2235 $r = q("select * from conv where id = %d and uid = %d limit 1",
2236 intval($item['convid']),
2237 intval($item['uid'])
2241 logger('diaspora_send_mail: conversation not found.');
2247 'guid' => xmlify($cnv['guid']),
2248 'subject' => xmlify($cnv['subject']),
2249 'created_at' => xmlify(datetime_convert('UTC','UTC',$cnv['created'],'Y-m-d H:i:s \U\T\C')),
2250 'diaspora_handle' => xmlify($cnv['creator']),
2251 'participant_handles' => xmlify($cnv['recips'])
2254 $body = bb2diaspora($item['body']);
2255 $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d H:i:s \U\T\C');
2257 $signed_text = $item['guid'] . ';' . $cnv['guid'] . ';' . $body . ';'
2258 . $created . ';' . $myaddr . ';' . $cnv['guid'];
2260 $sig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
2263 'guid' => xmlify($item['guid']),
2264 'parent_guid' => xmlify($cnv['guid']),
2265 'parent_author_signature' => (($item['reply']) ? null : xmlify($sig)),
2266 'author_signature' => xmlify($sig),
2267 'text' => xmlify($body),
2268 'created_at' => xmlify($created),
2269 'diaspora_handle' => xmlify($myaddr),
2270 'conversation_guid' => xmlify($cnv['guid'])
2273 if($item['reply']) {
2274 $tpl = get_markup_template('diaspora_message.tpl');
2275 $xmsg = replace_macros($tpl, array('$msg' => $msg));
2278 $conv['messages'] = array($msg);
2279 $tpl = get_markup_template('diaspora_conversation.tpl');
2280 $xmsg = replace_macros($tpl, array('$conv' => $conv));
2283 logger('diaspora_conversation: ' . print_r($xmsg,true), LOGGER_DATA);
2285 $slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($xmsg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],false)));
2287 return(diaspora_transmit($owner,$contact,$slap,false));
2292 function diaspora_transmit($owner,$contact,$slap,$public_batch) {
2294 $enabled = intval(get_config('system','diaspora_enabled'));
2300 $logid = random_string(4);
2301 $dest_url = (($public_batch) ? $contact['batch'] : $contact['notify']);
2303 logger('diaspora_transmit: no url for contact: ' . $contact['id'] . ' batch mode =' . $public_batch);
2307 logger('diaspora_transmit: ' . $logid . ' ' . $dest_url);
2309 if(was_recently_delayed($contact['id'])) {
2313 if(! intval(get_config('system','diaspora_test'))) {
2314 post_url($dest_url . '/', $slap);
2315 $return_code = $a->get_curl_code();
2318 logger('diaspora_transmit: test_mode');
2323 logger('diaspora_transmit: ' . $logid . ' returns: ' . $return_code);
2325 if((! $return_code) || (($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))) {
2326 logger('diaspora_transmit: queue message');
2328 $r = q("SELECT id from queue where cid = %d and network = '%s' and content = '%s' and batch = %d limit 1",
2329 intval($contact['id']),
2330 dbesc(NETWORK_DIASPORA),
2332 intval($public_batch)
2335 logger('diaspora_transmit: add_to_queue ignored - identical item already in queue');
2338 // queue message for redelivery
2339 add_to_queue($contact['id'],NETWORK_DIASPORA,$slap,$public_batch);
2344 return(($return_code) ? $return_code : (-1));