3 require_once('simplepie/simplepie.inc');
4 require_once('include/items.php');
5 require_once('include/event.php');
8 function dfrn_notify_post(&$a) {
10 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
11 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
12 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
13 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
14 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
15 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
16 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
19 if($dfrn_version >= 2.21) {
20 $writable = (($perm === 'rw') ? 1 : 0);
24 if(strpos($dfrn_id,':') == 1) {
25 $direction = intval(substr($dfrn_id,0,1));
26 $dfrn_id = substr($dfrn_id,2);
29 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
34 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
38 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
43 // find the local user who owns this relationship.
48 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
51 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
54 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
62 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
63 `contact`.`pubkey` AS `cpubkey`,
64 `contact`.`prvkey` AS `cprvkey`,
65 `contact`.`thumb` AS `thumb`,
66 `contact`.`url` as `url`,
67 `contact`.`name` as `senderName`,
70 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
71 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
72 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
77 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
82 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
86 if(($writable != (-1)) && ($writable != $importer['writable'])) {
87 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
89 intval($importer['id'])
91 $importer['writable'] = $writable;
94 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
95 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
100 * Relationship is dissolved permanently
103 require_once('include/Contact.php');
104 contact_remove($importer['id']);
105 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
111 $rawkey = hex2bin(trim($key));
112 logger('rino: md5 raw key: ' . md5($rawkey));
115 if($dfrn_version >= 2.1) {
116 if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
117 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
120 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
124 if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
125 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
128 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
132 logger('rino: received key : ' . $final_key);
133 $data = aes_decrypt(hex2bin($data),$final_key);
134 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
138 if($importer['readonly']) {
139 // We aren't receiving stuff from this person. But we will quietly ignore them
140 // rather than a blatant "go away" message.
141 logger('dfrn_notify: ignoring');
146 // Consume notification feed. This may differ from consuming a public feed in several ways
147 // - might contain email
148 // - might contain remote followup to our message
149 // - in which case we need to accept it and then notify other conversants
150 // - we may need to send various email notifications
152 $feed = new SimplePie();
153 $feed->set_raw_data($data);
154 $feed->enable_order_by_date(false);
159 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
160 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
162 logger('dfrn_notify: private message received');
165 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
168 $msg['uid'] = $importer['importer_uid'];
169 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
170 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
171 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
172 $msg['contact-id'] = $importer['id'];
173 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
174 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
177 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
178 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
179 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
183 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
184 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
186 // send email notification if requested.
188 require_once('bbcode.php');
189 if($importer['notify-flags'] & NOTIFY_MAIL) {
191 push_lang($importer['language']);
193 // name of the automated email sender
194 $msg['notificationfromname'] = t('Administrator');
195 // noreply address to send from
196 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
199 // process the message body to display properly in text mode
200 // 1) substitute a \n character for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
201 // 2) remove escape slashes
202 // 3) decode any bbcode from the message editor
203 // 4) decode any encoded html tags
204 // 5) remove html tags
206 = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",$msg['body']))),ENT_QUOTES,'UTF-8'));
209 // process the message body to display properly in text mode
210 // 1) substitute a <br /> tag for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
211 // 2) remove escape slashes
212 // 3) decode any bbcode from the message editor
213 // 4) decode any encoded html tags
215 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$msg['body']))));
217 // load the template for private message notifications
218 $tpl = get_intltext_template('mail_received_html_body_eml.tpl');
219 $email_html_body_tpl = replace_macros($tpl,array(
220 '$siteName' => $a->config['sitename'], // name of this site
221 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
222 '$thumb' => $importer['thumb'], // thumbnail url for sender icon
223 '$email' => $importer['email'], // email address to send to
224 '$url' => $importer['url'], // full url for the site
225 '$from' => $msg['from-name'], // name of the person sending the message
226 '$title' => stripslashes($msg['title']), // subject of the message
227 '$htmlversion' => $msg['htmlversion'], // html version of the message
228 '$mimeboundary' => $msg['mimeboundary'], // mime message divider
229 '$hostname' => $a->get_hostname() // name of this host
232 // load the template for private message notifications
233 $tpl = get_intltext_template('mail_received_text_body_eml.tpl');
234 $email_text_body_tpl = replace_macros($tpl,array(
235 '$siteName' => $a->config['sitename'], // name of this site
236 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
237 '$thumb' => $importer['thumb'], // thumbnail url for sender icon
238 '$email' => $importer['email'], // email address to send to
239 '$url' => $importer['url'], // full url for the site
240 '$from' => $msg['from-name'], // name of the person sending the message
241 '$title' => stripslashes($msg['title']), // subject of the message
242 '$textversion' => $msg['textversion'], // text version of the message
243 '$mimeboundary' => $msg['mimeboundary'], // mime message divider
244 '$hostname' => $a->get_hostname() // name of this host
247 // use the EmailNotification library to send the message
248 require_once("include/EmailNotification.php");
249 EmailNotification::sendTextHtmlEmail(
250 $msg['notificationfromname'],
251 $msg['notificationfromemail'],
252 $msg['notificationfromemail'],
254 t('New mail received at ') . $a->config['sitename'],
255 $email_html_body_tpl,
265 logger('dfrn_notify: feed item count = ' . $feed->get_item_quantity());
267 // process any deleted entries
269 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
270 if(is_array($del_entries) && count($del_entries)) {
271 foreach($del_entries as $dentry) {
273 if(isset($dentry['attribs']['']['ref'])) {
274 $uri = $dentry['attribs']['']['ref'];
276 if(isset($dentry['attribs']['']['when'])) {
277 $when = $dentry['attribs']['']['when'];
278 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
281 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
285 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
287 intval($importer['importer_uid']),
288 intval($importer['id'])
294 if(! $item['deleted'])
295 logger('dfrn_notify: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
297 if($item['uri'] == $item['parent-uri']) {
298 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
299 WHERE `parent-uri` = '%s' AND `uid` = %d",
301 dbesc(datetime_convert()),
303 intval($importer['importer_uid'])
307 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
308 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
310 dbesc(datetime_convert()),
312 intval($importer['importer_uid'])
314 if($item['last-child']) {
315 // ensure that last-child is set in case the comment that had it just got wiped.
316 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
317 dbesc(datetime_convert()),
318 dbesc($item['parent-uri']),
321 // who is the last child now?
322 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
323 ORDER BY `created` DESC LIMIT 1",
324 dbesc($item['parent-uri']),
325 intval($importer['importer_uid'])
328 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
340 foreach($feed->get_items() as $item) {
343 $item_id = $item->get_id();
344 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
345 if(isset($rawthread[0]['attribs']['']['ref'])) {
347 $parent_uri = $rawthread[0]['attribs']['']['ref'];
351 if($feed->get_item_quantity() == 1) {
352 logger('dfrn_notify: received remote comment');
354 // remote reply to our post. Import and then notify everybody else.
355 $datarray = get_atom_elements($feed,$item);
356 $datarray['type'] = 'remote-comment';
357 $datarray['wall'] = 1;
358 $datarray['parent-uri'] = $parent_uri;
359 $datarray['uid'] = $importer['importer_uid'];
360 $datarray['contact-id'] = $importer['id'];
361 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
363 $datarray['type'] = 'activity';
364 $datarray['gravity'] = GRAVITY_LIKE;
365 $datarray['last-child'] = 0;
367 $posted_id = item_store($datarray);
371 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
373 intval($importer['importer_uid'])
376 $parent = $r[0]['parent'];
379 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
380 dbesc(datetime_convert()),
381 intval($importer['importer_uid']),
382 intval($r[0]['parent'])
385 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
386 dbesc(datetime_convert()),
387 intval($importer['importer_uid']),
392 if($posted_id && $parent) {
394 proc_run('php',"include/notifier.php","comment-import","$posted_id");
396 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
397 push_lang($importer['language']);
398 require_once('bbcode.php');
399 $from = stripslashes($datarray['author-name']);
401 // name of the automated email sender
402 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
403 // noreply address to send from
404 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
407 // process the message body to display properly in text mode
409 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
412 // process the message body to display properly in text mode
414 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
416 // load the template for private message notifications
417 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
418 $email_html_body_tpl = replace_macros($tpl,array(
419 '$sitename' => $a->config['sitename'], // name of this site
420 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
421 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
422 '$email' => $importer['email'], // email address to send to
423 '$url' => $datarray['author-link'], // full url for the site
424 '$from' => $from, // name of the person sending the message
425 '$body' => $msg['htmlversion'], // html version of the message
426 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
429 // load the template for private message notifications
430 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
431 $email_text_body_tpl = replace_macros($tpl,array(
432 '$sitename' => $a->config['sitename'], // name of this site
433 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
434 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
435 '$email' => $importer['email'], // email address to send to
436 '$url' => $datarray['author-link'], // full url for the site
437 '$from' => $from, // name of the person sending the message
438 '$body' => $msg['textversion'], // text version of the message
439 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
442 // use the EmailNotification library to send the message
443 require_once("include/EmailNotification.php");
444 EmailNotification::sendTextHtmlEmail(
445 $msg['notificationfromname'],
446 t("Administrator") . '@' . $a->get_hostname(),
447 t("noreply") . '@' . $a->get_hostname(),
449 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
450 $email_html_body_tpl,
462 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
464 $item_id = $item->get_id();
465 $datarray = get_atom_elements($feed,$item);
467 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
469 intval($importer['importer_uid'])
472 // Update content if 'updated' changes
475 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
476 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
477 dbesc($datarray['body']),
478 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
480 intval($importer['importer_uid'])
484 // update last-child if it changes
486 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
487 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
488 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
489 dbesc(datetime_convert()),
491 intval($importer['importer_uid'])
493 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
494 intval($allow[0]['data']),
495 dbesc(datetime_convert()),
497 intval($importer['importer_uid'])
503 $datarray['parent-uri'] = $parent_uri;
504 $datarray['uid'] = $importer['importer_uid'];
505 $datarray['contact-id'] = $importer['id'];
506 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
507 $datarray['type'] = 'activity';
508 $datarray['gravity'] = GRAVITY_LIKE;
510 $posted_id = item_store($datarray);
512 // find out if our user is involved in this conversation and wants to be notified.
514 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
516 $myconv = q("SELECT `author-link`, `author-avatar` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
518 intval($importer['importer_uid'])
521 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
522 foreach($myconv as $conv) {
523 if(! link_compare($conv['author-link'],$importer_url))
526 push_lang($importer['language']);
527 require_once('bbcode.php');
528 $from = stripslashes($datarray['author-name']);
530 // name of the automated email sender
531 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
532 // noreply address to send from
533 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
536 // process the message body to display properly in text mode
538 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
541 // process the message body to display properly in text mode
543 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
545 // load the template for private message notifications
546 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
547 $email_html_body_tpl = replace_macros($tpl,array(
548 '$sitename' => $a->config['sitename'], // name of this site
549 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
550 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
551 '$url' => $datarray['author-link'], // full url for the site
552 '$from' => $from, // name of the person sending the message
553 '$body' => $msg['htmlversion'], // html version of the message
554 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
557 // load the template for private message notifications
558 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
559 $email_text_body_tpl = replace_macros($tpl,array(
560 '$sitename' => $a->config['sitename'], // name of this site
561 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
562 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
563 '$url' => $datarray['author-link'], // full url for the site
564 '$from' => $from, // name of the person sending the message
565 '$body' => $msg['textversion'], // text version of the message
566 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
569 // use the EmailNotification library to send the message
570 require_once("include/EmailNotification.php");
571 EmailNotification::sendTextHtmlEmail(
572 $msg['notificationfromname'],
573 t("Administrator@") . $a->get_hostname(),
574 t("noreply") . '@' . $a->get_hostname(),
576 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
577 $email_html_body_tpl,
591 // Head post of a conversation. Have we seen it? If not, import it.
594 $item_id = $item->get_id();
595 $datarray = get_atom_elements($feed,$item);
597 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
598 $ev = bbtoevent($datarray['body']);
599 if(x($ev,'desc') && x($ev,'start')) {
600 $ev['cid'] = $importer['id'];
601 $ev['uid'] = $importer['uid'];
603 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
605 intval($importer['uid'])
608 $ev['id'] = $r[0]['id'];
609 $xyz = event_store($ev);
614 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
616 intval($importer['importer_uid'])
619 // Update content if 'updated' changes
622 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
623 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
624 dbesc($datarray['body']),
625 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
627 intval($importer['importer_uid'])
631 // update last-child if it changes
633 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
634 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
635 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
636 intval($allow[0]['data']),
637 dbesc(datetime_convert()),
639 intval($importer['importer_uid'])
645 $datarray['parent-uri'] = $item_id;
646 $datarray['uid'] = $importer['importer_uid'];
647 $datarray['contact-id'] = $importer['id'];
648 $r = item_store($datarray);
659 function dfrn_notify_content(&$a) {
661 if(x($_GET,'dfrn_id')) {
663 // initial communication from external contact, $direction is their direction.
664 // If this is a duplex communication, ours will be the opposite.
666 $dfrn_id = notags(trim($_GET['dfrn_id']));
667 $dfrn_version = (float) $_GET['dfrn_version'];
669 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
672 if(strpos($dfrn_id,':') == 1) {
673 $direction = intval(substr($dfrn_id,0,1));
674 $dfrn_id = substr($dfrn_id,2);
677 $hash = random_string();
681 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
683 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
684 VALUES( '%s', '%s', %d ) ",
690 logger('dfrn_notify: challenge=' . $hash );
695 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
699 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
700 $my_id = '1:' . $dfrn_id;
703 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
704 $my_id = '0:' . $dfrn_id;
711 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
712 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
721 $id_str = $my_id . '.' . mt_rand(1000,9999);
723 if((($r[0]['duplex']) && strlen($r[0]['prvkey'])) || (! strlen($r[0]['pubkey']))) {
724 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
725 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
728 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
729 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
732 $challenge = bin2hex($challenge);
733 $encrypted_id = bin2hex($encrypted_id);
735 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
737 $rino_enable = get_config('system','rino_encrypt');
743 header("Content-type: text/xml");
745 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
746 . '<dfrn_notify>' . "\r\n"
747 . "\t" . '<status>' . $status . '</status>' . "\r\n"
748 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
749 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
750 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
751 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
752 . '</dfrn_notify>' . "\r\n" ;