3 require_once('library/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 or friend suggestions
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);
157 // handle friend suggestion notification
159 $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
160 if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
161 $base = $sugg[0]['child'][NAMESPACE_DFRN];
163 $fsugg['uid'] = $importer['importer_uid'];
164 $fsugg['cid'] = $importer['id'];
165 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
166 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
167 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
168 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
169 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
171 // Does our member already have a friend matching this description?
173 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `url` = '%s' AND `uid` = %d LIMIT 1",
174 dbesc($fsugg['name']),
175 dbesc($fsugg['url']),
176 intval($fsugg['uid'])
181 // Do we already have an fcontact record for this person?
184 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
185 dbesc($fsugg['url']),
186 dbesc($fsugg['name']),
187 dbesc($fsugg['request'])
193 $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
194 dbesc($fsugg['name']),
195 dbesc($fsugg['url']),
196 dbesc($fsugg['photo']),
197 dbesc($fsugg['request'])
199 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
200 dbesc($fsugg['url']),
201 dbesc($fsugg['name']),
202 dbesc($fsugg['request'])
207 // database record did not get created. Quietly give up.
211 $hash = random_string();
213 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
214 VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
215 intval($fsugg['uid']),
217 intval($fsugg['cid']),
218 dbesc($fsugg['body']),
220 dbesc(datetime_convert()),
224 // TODO - send email notify (which may require a new notification preference)
231 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
232 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
234 logger('dfrn_notify: private message received');
237 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
240 $msg['uid'] = $importer['importer_uid'];
241 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
242 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
243 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
244 $msg['contact-id'] = $importer['id'];
245 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
246 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
249 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
250 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
251 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
255 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
256 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
258 // send email notification if requested.
260 require_once('bbcode.php');
261 if($importer['notify-flags'] & NOTIFY_MAIL) {
263 push_lang($importer['language']);
265 // name of the automated email sender
266 $msg['notificationfromname'] = t('Administrator');
267 // noreply address to send from
268 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
271 // process the message body to display properly in text mode
272 // 1) substitute a \n character for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
273 // 2) remove escape slashes
274 // 3) decode any bbcode from the message editor
275 // 4) decode any encoded html tags
276 // 5) remove html tags
278 = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",$msg['body']))),ENT_QUOTES,'UTF-8'));
281 // process the message body to display properly in text mode
282 // 1) substitute a <br /> tag for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
283 // 2) remove escape slashes
284 // 3) decode any bbcode from the message editor
285 // 4) decode any encoded html tags
287 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$msg['body']))));
289 // load the template for private message notifications
290 $tpl = get_intltext_template('mail_received_html_body_eml.tpl');
291 $email_html_body_tpl = replace_macros($tpl,array(
292 '$username' => $importer['username'],
293 '$siteName' => $a->config['sitename'], // name of this site
294 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
295 '$thumb' => $importer['thumb'], // thumbnail url for sender icon
296 '$email' => $importer['email'], // email address to send to
297 '$url' => $importer['url'], // full url for the site
298 '$from' => $msg['from-name'], // name of the person sending the message
299 '$title' => stripslashes($msg['title']), // subject of the message
300 '$htmlversion' => $msg['htmlversion'], // html version of the message
301 '$mimeboundary' => $msg['mimeboundary'], // mime message divider
302 '$hostname' => $a->get_hostname() // name of this host
305 // load the template for private message notifications
306 $tpl = get_intltext_template('mail_received_text_body_eml.tpl');
307 $email_text_body_tpl = replace_macros($tpl,array(
308 '$username' => $importer['username'],
309 '$siteName' => $a->config['sitename'], // name of this site
310 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
311 '$thumb' => $importer['thumb'], // thumbnail url for sender icon
312 '$email' => $importer['email'], // email address to send to
313 '$url' => $importer['url'], // full url for the site
314 '$from' => $msg['from-name'], // name of the person sending the message
315 '$title' => stripslashes($msg['title']), // subject of the message
316 '$textversion' => $msg['textversion'], // text version of the message
317 '$mimeboundary' => $msg['mimeboundary'], // mime message divider
318 '$hostname' => $a->get_hostname() // name of this host
321 // use the EmailNotification library to send the message
322 require_once("include/EmailNotification.php");
323 EmailNotification::sendTextHtmlEmail(
324 $msg['notificationfromname'],
325 $msg['notificationfromemail'],
326 $msg['notificationfromemail'],
328 t('New mail received at ') . $a->config['sitename'],
329 $email_html_body_tpl,
339 logger('dfrn_notify: feed item count = ' . $feed->get_item_quantity());
341 // process any deleted entries
343 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
344 if(is_array($del_entries) && count($del_entries)) {
345 foreach($del_entries as $dentry) {
347 if(isset($dentry['attribs']['']['ref'])) {
348 $uri = $dentry['attribs']['']['ref'];
350 if(isset($dentry['attribs']['']['when'])) {
351 $when = $dentry['attribs']['']['when'];
352 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
355 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
359 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
361 intval($importer['importer_uid']),
362 intval($importer['id'])
368 if(! $item['deleted'])
369 logger('dfrn_notify: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
371 if($item['uri'] == $item['parent-uri']) {
372 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
373 WHERE `parent-uri` = '%s' AND `uid` = %d",
375 dbesc(datetime_convert()),
377 intval($importer['importer_uid'])
381 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
382 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
384 dbesc(datetime_convert()),
386 intval($importer['importer_uid'])
388 if($item['last-child']) {
389 // ensure that last-child is set in case the comment that had it just got wiped.
390 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
391 dbesc(datetime_convert()),
392 dbesc($item['parent-uri']),
395 // who is the last child now?
396 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
397 ORDER BY `created` DESC LIMIT 1",
398 dbesc($item['parent-uri']),
399 intval($importer['importer_uid'])
402 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
414 foreach($feed->get_items() as $item) {
417 $item_id = $item->get_id();
418 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
419 if(isset($rawthread[0]['attribs']['']['ref'])) {
421 $parent_uri = $rawthread[0]['attribs']['']['ref'];
425 if($feed->get_item_quantity() == 1) {
426 logger('dfrn_notify: received remote comment');
428 // remote reply to our post. Import and then notify everybody else.
429 $datarray = get_atom_elements($feed,$item);
430 $datarray['type'] = 'remote-comment';
431 $datarray['wall'] = 1;
432 $datarray['parent-uri'] = $parent_uri;
433 $datarray['uid'] = $importer['importer_uid'];
434 $datarray['contact-id'] = $importer['id'];
435 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
437 $datarray['type'] = 'activity';
438 $datarray['gravity'] = GRAVITY_LIKE;
439 $datarray['last-child'] = 0;
441 $posted_id = item_store($datarray);
445 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
447 intval($importer['importer_uid'])
450 $parent = $r[0]['parent'];
453 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
454 dbesc(datetime_convert()),
455 intval($importer['importer_uid']),
456 intval($r[0]['parent'])
459 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
460 dbesc(datetime_convert()),
461 intval($importer['importer_uid']),
466 if($posted_id && $parent) {
468 proc_run('php',"include/notifier.php","comment-import","$posted_id");
470 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
471 push_lang($importer['language']);
472 require_once('bbcode.php');
473 $from = stripslashes($datarray['author-name']);
475 // name of the automated email sender
476 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
477 // noreply address to send from
478 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
481 // process the message body to display properly in text mode
483 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
486 // process the message body to display properly in text mode
488 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
490 // load the template for private message notifications
491 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
492 $email_html_body_tpl = replace_macros($tpl,array(
493 '$username' => $importer['username'],
494 '$sitename' => $a->config['sitename'], // name of this site
495 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
496 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
497 '$email' => $importer['email'], // email address to send to
498 '$url' => $datarray['author-link'], // full url for the site
499 '$from' => $from, // name of the person sending the message
500 '$body' => $msg['htmlversion'], // html version of the message
501 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
504 // load the template for private message notifications
505 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
506 $email_text_body_tpl = replace_macros($tpl,array(
507 '$username' => $importer['username'],
508 '$sitename' => $a->config['sitename'], // name of this site
509 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
510 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
511 '$email' => $importer['email'], // email address to send to
512 '$url' => $datarray['author-link'], // full url for the site
513 '$from' => $from, // name of the person sending the message
514 '$body' => $msg['textversion'], // text version of the message
515 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
518 // use the EmailNotification library to send the message
519 require_once("include/EmailNotification.php");
520 EmailNotification::sendTextHtmlEmail(
521 $msg['notificationfromname'],
522 t("Administrator") . '@' . $a->get_hostname(),
523 t("noreply") . '@' . $a->get_hostname(),
525 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
526 $email_html_body_tpl,
538 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
540 $item_id = $item->get_id();
541 $datarray = get_atom_elements($feed,$item);
543 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
545 intval($importer['importer_uid'])
548 // Update content if 'updated' changes
551 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
552 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
553 dbesc($datarray['body']),
554 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
556 intval($importer['importer_uid'])
560 // update last-child if it changes
562 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
563 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
564 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
565 dbesc(datetime_convert()),
567 intval($importer['importer_uid'])
569 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
570 intval($allow[0]['data']),
571 dbesc(datetime_convert()),
573 intval($importer['importer_uid'])
579 $datarray['parent-uri'] = $parent_uri;
580 $datarray['uid'] = $importer['importer_uid'];
581 $datarray['contact-id'] = $importer['id'];
582 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
583 $datarray['type'] = 'activity';
584 $datarray['gravity'] = GRAVITY_LIKE;
586 $posted_id = item_store($datarray);
588 // find out if our user is involved in this conversation and wants to be notified.
590 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
592 $myconv = q("SELECT `author-link`, `author-avatar` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
594 intval($importer['importer_uid'])
597 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
598 foreach($myconv as $conv) {
599 if(! link_compare($conv['author-link'],$importer_url))
602 push_lang($importer['language']);
603 require_once('bbcode.php');
604 $from = stripslashes($datarray['author-name']);
606 // name of the automated email sender
607 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
608 // noreply address to send from
609 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
612 // process the message body to display properly in text mode
614 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
617 // process the message body to display properly in text mode
619 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
621 // load the template for private message notifications
622 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
623 $email_html_body_tpl = replace_macros($tpl,array(
624 '$username' => $importer['username'],
625 '$sitename' => $a->config['sitename'], // name of this site
626 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
627 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
628 '$url' => $datarray['author-link'], // full url for the site
629 '$from' => $from, // name of the person sending the message
630 '$body' => $msg['htmlversion'], // html version of the message
631 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
634 // load the template for private message notifications
635 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
636 $email_text_body_tpl = replace_macros($tpl,array(
637 '$username' => $importer['username'],
638 '$sitename' => $a->config['sitename'], // name of this site
639 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
640 '$thumb' => $datarray['author-avatar'], // thumbnail url for sender icon
641 '$url' => $datarray['author-link'], // full url for the site
642 '$from' => $from, // name of the person sending the message
643 '$body' => $msg['textversion'], // text version of the message
644 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
647 // use the EmailNotification library to send the message
648 require_once("include/EmailNotification.php");
649 EmailNotification::sendTextHtmlEmail(
650 $msg['notificationfromname'],
651 t("Administrator@") . $a->get_hostname(),
652 t("noreply") . '@' . $a->get_hostname(),
654 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
655 $email_html_body_tpl,
669 // Head post of a conversation. Have we seen it? If not, import it.
672 $item_id = $item->get_id();
673 $datarray = get_atom_elements($feed,$item);
675 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
676 $ev = bbtoevent($datarray['body']);
677 if(x($ev,'desc') && x($ev,'start')) {
678 $ev['cid'] = $importer['id'];
679 $ev['uid'] = $importer['uid'];
680 $ev['uri'] = $item_id;
681 $ev['edited'] = $datarray['edited'];
683 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
685 intval($importer['uid'])
688 $ev['id'] = $r[0]['id'];
689 $xyz = event_store($ev);
694 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
696 intval($importer['importer_uid'])
699 // Update content if 'updated' changes
702 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
703 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
704 dbesc($datarray['body']),
705 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
707 intval($importer['importer_uid'])
711 // update last-child if it changes
713 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
714 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
715 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
716 intval($allow[0]['data']),
717 dbesc(datetime_convert()),
719 intval($importer['importer_uid'])
725 $datarray['parent-uri'] = $item_id;
726 $datarray['uid'] = $importer['importer_uid'];
727 $datarray['contact-id'] = $importer['id'];
728 $r = item_store($datarray);
739 function dfrn_notify_content(&$a) {
741 if(x($_GET,'dfrn_id')) {
743 // initial communication from external contact, $direction is their direction.
744 // If this is a duplex communication, ours will be the opposite.
746 $dfrn_id = notags(trim($_GET['dfrn_id']));
747 $dfrn_version = (float) $_GET['dfrn_version'];
749 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
752 if(strpos($dfrn_id,':') == 1) {
753 $direction = intval(substr($dfrn_id,0,1));
754 $dfrn_id = substr($dfrn_id,2);
757 $hash = random_string();
761 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
763 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
764 VALUES( '%s', '%s', %d ) ",
770 logger('dfrn_notify: challenge=' . $hash );
775 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
779 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
780 $my_id = '1:' . $dfrn_id;
783 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
784 $my_id = '0:' . $dfrn_id;
791 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
792 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
801 $id_str = $my_id . '.' . mt_rand(1000,9999);
803 if((($r[0]['duplex']) && strlen($r[0]['prvkey'])) || (! strlen($r[0]['pubkey']))) {
804 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
805 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
808 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
809 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
812 $challenge = bin2hex($challenge);
813 $encrypted_id = bin2hex($encrypted_id);
815 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
817 $rino_enable = get_config('system','rino_encrypt');
823 header("Content-type: text/xml");
825 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
826 . '<dfrn_notify>' . "\r\n"
827 . "\t" . '<status>' . $status . '</status>' . "\r\n"
828 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
829 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
830 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
831 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
832 . '</dfrn_notify>' . "\r\n" ;