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 '$username' => $importer['username'],
221 '$siteName' => $a->config['sitename'], // name of this site
222 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
223 '$thumb' => $importer['thumb'], // thumbnail url for sender icon
224 '$email' => $importer['email'], // email address to send to
225 '$url' => $importer['url'], // full url for the site
226 '$from' => $msg['from-name'], // name of the person sending the message
227 '$title' => stripslashes($msg['title']), // subject of the message
228 '$htmlversion' => $msg['htmlversion'], // html version of the message
229 '$mimeboundary' => $msg['mimeboundary'], // mime message divider
230 '$hostname' => $a->get_hostname() // name of this host
233 // load the template for private message notifications
234 $tpl = get_intltext_template('mail_received_text_body_eml.tpl');
235 $email_text_body_tpl = replace_macros($tpl,array(
236 '$username' => $importer['username'],
237 '$siteName' => $a->config['sitename'], // name of this site
238 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
239 '$thumb' => $importer['thumb'], // thumbnail url for sender icon
240 '$email' => $importer['email'], // email address to send to
241 '$url' => $importer['url'], // full url for the site
242 '$from' => $msg['from-name'], // name of the person sending the message
243 '$title' => stripslashes($msg['title']), // subject of the message
244 '$textversion' => $msg['textversion'], // text version of the message
245 '$mimeboundary' => $msg['mimeboundary'], // mime message divider
246 '$hostname' => $a->get_hostname() // name of this host
249 // use the EmailNotification library to send the message
250 require_once("include/EmailNotification.php");
251 EmailNotification::sendTextHtmlEmail(
252 $msg['notificationfromname'],
253 $msg['notificationfromemail'],
254 $msg['notificationfromemail'],
256 t('New mail received at ') . $a->config['sitename'],
257 $email_html_body_tpl,
267 logger('dfrn_notify: feed item count = ' . $feed->get_item_quantity());
269 // process any deleted entries
271 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
272 if(is_array($del_entries) && count($del_entries)) {
273 foreach($del_entries as $dentry) {
275 if(isset($dentry['attribs']['']['ref'])) {
276 $uri = $dentry['attribs']['']['ref'];
278 if(isset($dentry['attribs']['']['when'])) {
279 $when = $dentry['attribs']['']['when'];
280 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
283 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
287 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
289 intval($importer['importer_uid']),
290 intval($importer['id'])
296 if(! $item['deleted'])
297 logger('dfrn_notify: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
299 if($item['uri'] == $item['parent-uri']) {
300 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
301 WHERE `parent-uri` = '%s' AND `uid` = %d",
303 dbesc(datetime_convert()),
305 intval($importer['importer_uid'])
309 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
310 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
312 dbesc(datetime_convert()),
314 intval($importer['importer_uid'])
316 if($item['last-child']) {
317 // ensure that last-child is set in case the comment that had it just got wiped.
318 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
319 dbesc(datetime_convert()),
320 dbesc($item['parent-uri']),
323 // who is the last child now?
324 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
325 ORDER BY `created` DESC LIMIT 1",
326 dbesc($item['parent-uri']),
327 intval($importer['importer_uid'])
330 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
342 foreach($feed->get_items() as $item) {
345 $item_id = $item->get_id();
346 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
347 if(isset($rawthread[0]['attribs']['']['ref'])) {
349 $parent_uri = $rawthread[0]['attribs']['']['ref'];
353 if($feed->get_item_quantity() == 1) {
354 logger('dfrn_notify: received remote comment');
356 // remote reply to our post. Import and then notify everybody else.
357 $datarray = get_atom_elements($feed,$item);
358 $datarray['type'] = 'remote-comment';
359 $datarray['wall'] = 1;
360 $datarray['parent-uri'] = $parent_uri;
361 $datarray['uid'] = $importer['importer_uid'];
362 $datarray['contact-id'] = $importer['id'];
363 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
365 $datarray['type'] = 'activity';
366 $datarray['gravity'] = GRAVITY_LIKE;
367 $datarray['last-child'] = 0;
369 $posted_id = item_store($datarray);
373 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
375 intval($importer['importer_uid'])
378 $parent = $r[0]['parent'];
381 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
382 dbesc(datetime_convert()),
383 intval($importer['importer_uid']),
384 intval($r[0]['parent'])
387 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
388 dbesc(datetime_convert()),
389 intval($importer['importer_uid']),
394 if($posted_id && $parent) {
396 proc_run('php',"include/notifier.php","comment-import","$posted_id");
398 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
399 push_lang($importer['language']);
400 require_once('bbcode.php');
401 $from = stripslashes($datarray['author-name']);
403 // name of the automated email sender
404 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
405 // noreply address to send from
406 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
409 // process the message body to display properly in text mode
411 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
414 // process the message body to display properly in text mode
416 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
418 // load the template for private message notifications
419 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
420 $email_html_body_tpl = replace_macros($tpl,array(
421 '$username' => $importer['username'],
422 '$sitename' => $a->config['sitename'], // name of this site
423 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
424 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
425 '$email' => $importer['email'], // email address to send to
426 '$url' => $datarray['author-link'], // full url for the site
427 '$from' => $from, // name of the person sending the message
428 '$body' => $msg['htmlversion'], // html version of the message
429 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
432 // load the template for private message notifications
433 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
434 $email_text_body_tpl = replace_macros($tpl,array(
435 '$username' => $importer['username'],
436 '$sitename' => $a->config['sitename'], // name of this site
437 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
438 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
439 '$email' => $importer['email'], // email address to send to
440 '$url' => $datarray['author-link'], // full url for the site
441 '$from' => $from, // name of the person sending the message
442 '$body' => $msg['textversion'], // text version of the message
443 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
446 // use the EmailNotification library to send the message
447 require_once("include/EmailNotification.php");
448 EmailNotification::sendTextHtmlEmail(
449 $msg['notificationfromname'],
450 t("Administrator") . '@' . $a->get_hostname(),
451 t("noreply") . '@' . $a->get_hostname(),
453 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
454 $email_html_body_tpl,
466 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
468 $item_id = $item->get_id();
469 $datarray = get_atom_elements($feed,$item);
471 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
473 intval($importer['importer_uid'])
476 // Update content if 'updated' changes
479 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
480 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
481 dbesc($datarray['body']),
482 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
484 intval($importer['importer_uid'])
488 // update last-child if it changes
490 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
491 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
492 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
493 dbesc(datetime_convert()),
495 intval($importer['importer_uid'])
497 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
498 intval($allow[0]['data']),
499 dbesc(datetime_convert()),
501 intval($importer['importer_uid'])
507 $datarray['parent-uri'] = $parent_uri;
508 $datarray['uid'] = $importer['importer_uid'];
509 $datarray['contact-id'] = $importer['id'];
510 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
511 $datarray['type'] = 'activity';
512 $datarray['gravity'] = GRAVITY_LIKE;
514 $posted_id = item_store($datarray);
516 // find out if our user is involved in this conversation and wants to be notified.
518 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
520 $myconv = q("SELECT `author-link`, `author-avatar` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
522 intval($importer['importer_uid'])
525 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
526 foreach($myconv as $conv) {
527 if(! link_compare($conv['author-link'],$importer_url))
530 push_lang($importer['language']);
531 require_once('bbcode.php');
532 $from = stripslashes($datarray['author-name']);
534 // name of the automated email sender
535 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
536 // noreply address to send from
537 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
540 // process the message body to display properly in text mode
542 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
545 // process the message body to display properly in text mode
547 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
549 // load the template for private message notifications
550 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
551 $email_html_body_tpl = replace_macros($tpl,array(
552 '$username' => $importer['username'],
553 '$sitename' => $a->config['sitename'], // name of this site
554 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
555 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
556 '$url' => $datarray['author-link'], // full url for the site
557 '$from' => $from, // name of the person sending the message
558 '$body' => $msg['htmlversion'], // html version of the message
559 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
562 // load the template for private message notifications
563 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
564 $email_text_body_tpl = replace_macros($tpl,array(
565 '$username' => $importer['username'],
566 '$sitename' => $a->config['sitename'], // name of this site
567 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
568 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
569 '$url' => $datarray['author-link'], // full url for the site
570 '$from' => $from, // name of the person sending the message
571 '$body' => $msg['textversion'], // text version of the message
572 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
575 // use the EmailNotification library to send the message
576 require_once("include/EmailNotification.php");
577 EmailNotification::sendTextHtmlEmail(
578 $msg['notificationfromname'],
579 t("Administrator@") . $a->get_hostname(),
580 t("noreply") . '@' . $a->get_hostname(),
582 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
583 $email_html_body_tpl,
597 // Head post of a conversation. Have we seen it? If not, import it.
600 $item_id = $item->get_id();
601 $datarray = get_atom_elements($feed,$item);
603 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
604 $ev = bbtoevent($datarray['body']);
605 if(x($ev,'desc') && x($ev,'start')) {
606 $ev['cid'] = $importer['id'];
607 $ev['uid'] = $importer['uid'];
608 $ev['uri'] = $item_id;
610 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
612 intval($importer['uid'])
615 $ev['id'] = $r[0]['id'];
616 $xyz = event_store($ev);
621 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
623 intval($importer['importer_uid'])
626 // Update content if 'updated' changes
629 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
630 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
631 dbesc($datarray['body']),
632 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
634 intval($importer['importer_uid'])
638 // update last-child if it changes
640 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
641 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
642 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
643 intval($allow[0]['data']),
644 dbesc(datetime_convert()),
646 intval($importer['importer_uid'])
652 $datarray['parent-uri'] = $item_id;
653 $datarray['uid'] = $importer['importer_uid'];
654 $datarray['contact-id'] = $importer['id'];
655 $r = item_store($datarray);
666 function dfrn_notify_content(&$a) {
668 if(x($_GET,'dfrn_id')) {
670 // initial communication from external contact, $direction is their direction.
671 // If this is a duplex communication, ours will be the opposite.
673 $dfrn_id = notags(trim($_GET['dfrn_id']));
674 $dfrn_version = (float) $_GET['dfrn_version'];
676 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
679 if(strpos($dfrn_id,':') == 1) {
680 $direction = intval(substr($dfrn_id,0,1));
681 $dfrn_id = substr($dfrn_id,2);
684 $hash = random_string();
688 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
690 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
691 VALUES( '%s', '%s', %d ) ",
697 logger('dfrn_notify: challenge=' . $hash );
702 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
706 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
707 $my_id = '1:' . $dfrn_id;
710 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
711 $my_id = '0:' . $dfrn_id;
718 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
719 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
728 $id_str = $my_id . '.' . mt_rand(1000,9999);
730 if((($r[0]['duplex']) && strlen($r[0]['prvkey'])) || (! strlen($r[0]['pubkey']))) {
731 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
732 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
735 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
736 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
739 $challenge = bin2hex($challenge);
740 $encrypted_id = bin2hex($encrypted_id);
742 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
744 $rino_enable = get_config('system','rino_encrypt');
750 header("Content-type: text/xml");
752 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
753 . '<dfrn_notify>' . "\r\n"
754 . "\t" . '<status>' . $status . '</status>' . "\r\n"
755 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
756 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
757 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
758 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
759 . '</dfrn_notify>' . "\r\n" ;