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));
61 // be careful - $importer will contain both the contact information for the contact
62 // sending us the post, and also the user information for the person receiving it.
63 // since they are mixed together, it is easy to get them confused.
65 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
66 `contact`.`pubkey` AS `cpubkey`,
67 `contact`.`prvkey` AS `cprvkey`,
68 `contact`.`thumb` AS `thumb`,
69 `contact`.`url` as `url`,
70 `contact`.`name` as `senderName`,
73 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
74 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
75 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
80 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
85 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
89 if(($writable != (-1)) && ($writable != $importer['writable'])) {
90 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
92 intval($importer['id'])
94 $importer['writable'] = $writable;
97 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
98 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
103 * Relationship is dissolved permanently
106 require_once('include/Contact.php');
107 contact_remove($importer['id']);
108 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
114 $rawkey = hex2bin(trim($key));
115 logger('rino: md5 raw key: ' . md5($rawkey));
118 if($dfrn_version >= 2.1) {
119 if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
120 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
123 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
127 if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
128 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
131 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
135 logger('rino: received key : ' . $final_key);
136 $data = aes_decrypt(hex2bin($data),$final_key);
137 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
141 if($importer['readonly']) {
142 // We aren't receiving stuff from this person. But we will quietly ignore them
143 // rather than a blatant "go away" message.
144 logger('dfrn_notify: ignoring');
149 // Consume notification feed. This may differ from consuming a public feed in several ways
150 // - might contain email or friend suggestions
151 // - might contain remote followup to our message
152 // - in which case we need to accept it and then notify other conversants
153 // - we may need to send various email notifications
155 $feed = new SimplePie();
156 $feed->set_raw_data($data);
157 $feed->enable_order_by_date(false);
160 $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
161 if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
162 $base = $reloc[0]['child'][NAMESPACE_DFRN];
164 $newloc['uid'] = $importer['importer_uid'];
165 $newloc['cid'] = $importer['id'];
166 $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
167 $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
168 $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
169 $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
170 $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
171 $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
172 $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
173 $newloc['site-pubkey'] = notags(unxmlify($base['site-pubkey'][0]['data']));
174 $newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
175 $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));
178 // merge with current record, current contents have priority
179 // update record, set url-updated
180 // update profile photos
185 // handle friend suggestion notification
187 $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
188 if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
189 $base = $sugg[0]['child'][NAMESPACE_DFRN];
191 $fsugg['uid'] = $importer['importer_uid'];
192 $fsugg['cid'] = $importer['id'];
193 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
194 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
195 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
196 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
197 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
199 // Does our member already have a friend matching this description?
201 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `url` = '%s' AND `uid` = %d LIMIT 1",
202 dbesc($fsugg['name']),
203 dbesc($fsugg['url']),
204 intval($fsugg['uid'])
209 // Do we already have an fcontact record for this person?
212 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
213 dbesc($fsugg['url']),
214 dbesc($fsugg['name']),
215 dbesc($fsugg['request'])
221 $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
222 dbesc($fsugg['name']),
223 dbesc($fsugg['url']),
224 dbesc($fsugg['photo']),
225 dbesc($fsugg['request'])
227 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
228 dbesc($fsugg['url']),
229 dbesc($fsugg['name']),
230 dbesc($fsugg['request'])
235 // database record did not get created. Quietly give up.
239 $hash = random_string();
241 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
242 VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
243 intval($fsugg['uid']),
245 intval($fsugg['cid']),
246 dbesc($fsugg['body']),
248 dbesc(datetime_convert()),
252 // TODO - send email notify (which may require a new notification preference)
259 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
260 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
262 logger('dfrn_notify: private message received');
265 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
268 $msg['uid'] = $importer['importer_uid'];
269 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
270 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
271 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
272 $msg['contact-id'] = $importer['id'];
273 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
274 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
277 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
278 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
279 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
283 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
284 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
286 // send email notification if requested.
288 require_once('bbcode.php');
289 if($importer['notify-flags'] & NOTIFY_MAIL) {
291 push_lang($importer['language']);
293 // name of the automated email sender
294 $msg['notificationfromname'] = t('Administrator');
295 // noreply address to send from
296 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
299 // process the message body to display properly in text mode
300 // 1) substitute a \n character for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
301 // 2) remove escape slashes
302 // 3) decode any bbcode from the message editor
303 // 4) decode any encoded html tags
304 // 5) remove html tags
306 = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",$msg['body']))),ENT_QUOTES,'UTF-8'));
309 // process the message body to display properly in text mode
310 // 1) substitute a <br /> tag for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
311 // 2) remove escape slashes
312 // 3) decode any bbcode from the message editor
313 // 4) decode any encoded html tags
315 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$msg['body']))));
317 // load the template for private message notifications
318 $tpl = get_intltext_template('mail_received_html_body_eml.tpl');
319 $email_html_body_tpl = replace_macros($tpl,array(
320 '$username' => $importer['username'],
321 '$siteName' => $a->config['sitename'], // name of this site
322 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
323 '$thumb' => $importer['thumb'], // thumbnail url for sender icon
324 '$email' => $importer['email'], // email address to send to
325 '$url' => $importer['url'], // full url for the site
326 '$from' => $msg['from-name'], // name of the person sending the message
327 '$title' => stripslashes($msg['title']), // subject of the message
328 '$htmlversion' => $msg['htmlversion'], // html version of the message
329 '$mimeboundary' => $msg['mimeboundary'], // mime message divider
330 '$hostname' => $a->get_hostname() // name of this host
333 // load the template for private message notifications
334 $tpl = get_intltext_template('mail_received_text_body_eml.tpl');
335 $email_text_body_tpl = replace_macros($tpl,array(
336 '$username' => $importer['username'],
337 '$siteName' => $a->config['sitename'], // name of this site
338 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
339 '$thumb' => $importer['thumb'], // thumbnail url for sender icon
340 '$email' => $importer['email'], // email address to send to
341 '$url' => $importer['url'], // full url for the site
342 '$from' => $msg['from-name'], // name of the person sending the message
343 '$title' => stripslashes($msg['title']), // subject of the message
344 '$textversion' => $msg['textversion'], // text version of the message
345 '$mimeboundary' => $msg['mimeboundary'], // mime message divider
346 '$hostname' => $a->get_hostname() // name of this host
349 // use the EmailNotification library to send the message
350 require_once("include/EmailNotification.php");
351 EmailNotification::sendTextHtmlEmail(
352 $msg['notificationfromname'],
353 $msg['notificationfromemail'],
354 $msg['notificationfromemail'],
356 t('New mail received at ') . $a->config['sitename'],
357 $email_html_body_tpl,
367 logger('dfrn_notify: feed item count = ' . $feed->get_item_quantity());
369 // process any deleted entries
371 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
372 if(is_array($del_entries) && count($del_entries)) {
373 foreach($del_entries as $dentry) {
375 if(isset($dentry['attribs']['']['ref'])) {
376 $uri = $dentry['attribs']['']['ref'];
378 if(isset($dentry['attribs']['']['when'])) {
379 $when = $dentry['attribs']['']['when'];
380 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
383 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
387 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
389 intval($importer['importer_uid']),
390 intval($importer['id'])
396 if(! $item['deleted'])
397 logger('dfrn_notify: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
399 if($item['uri'] == $item['parent-uri']) {
400 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
401 WHERE `parent-uri` = '%s' AND `uid` = %d",
403 dbesc(datetime_convert()),
405 intval($importer['importer_uid'])
409 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
410 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
412 dbesc(datetime_convert()),
414 intval($importer['importer_uid'])
416 if($item['last-child']) {
417 // ensure that last-child is set in case the comment that had it just got wiped.
418 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
419 dbesc(datetime_convert()),
420 dbesc($item['parent-uri']),
423 // who is the last child now?
424 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
425 ORDER BY `created` DESC LIMIT 1",
426 dbesc($item['parent-uri']),
427 intval($importer['importer_uid'])
430 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
442 foreach($feed->get_items() as $item) {
445 $item_id = $item->get_id();
446 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
447 if(isset($rawthread[0]['attribs']['']['ref'])) {
449 $parent_uri = $rawthread[0]['attribs']['']['ref'];
453 if($feed->get_item_quantity() == 1) {
454 logger('dfrn_notify: received remote comment');
456 // remote reply to our post. Import and then notify everybody else.
457 $datarray = get_atom_elements($feed,$item);
458 $datarray['type'] = 'remote-comment';
459 $datarray['wall'] = 1;
460 $datarray['parent-uri'] = $parent_uri;
461 $datarray['uid'] = $importer['importer_uid'];
462 $datarray['contact-id'] = $importer['id'];
463 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
465 $datarray['type'] = 'activity';
466 $datarray['gravity'] = GRAVITY_LIKE;
467 $datarray['last-child'] = 0;
469 $posted_id = item_store($datarray);
473 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
475 intval($importer['importer_uid'])
478 $parent = $r[0]['parent'];
481 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
482 dbesc(datetime_convert()),
483 intval($importer['importer_uid']),
484 intval($r[0]['parent'])
487 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
488 dbesc(datetime_convert()),
489 intval($importer['importer_uid']),
494 if($posted_id && $parent) {
496 proc_run('php',"include/notifier.php","comment-import","$posted_id");
498 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
499 push_lang($importer['language']);
500 require_once('bbcode.php');
501 $from = stripslashes($datarray['author-name']);
503 // name of the automated email sender
504 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
505 // noreply address to send from
506 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
509 // process the message body to display properly in text mode
511 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
514 // process the message body to display properly in text mode
516 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
518 $imgtouse = ((link_compare($datarray['author-link'],$importer['url'])) ? $importer['thumb'] : $datarray['author-avatar']);
520 // load the template for private message notifications
521 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
522 $email_html_body_tpl = replace_macros($tpl,array(
523 '$username' => $importer['username'],
524 '$sitename' => $a->config['sitename'], // name of this site
525 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
526 '$thumb' => $imgtouse, // thumbnail url for sender icon
527 '$email' => $importer['email'], // email address to send to
528 '$url' => $datarray['author-link'], // full url for the site
529 '$from' => $from, // name of the person sending the message
530 '$body' => $msg['htmlversion'], // html version of the message
531 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
534 // load the template for private message notifications
535 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
536 $email_text_body_tpl = replace_macros($tpl,array(
537 '$username' => $importer['username'],
538 '$sitename' => $a->config['sitename'], // name of this site
539 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
540 '$thumb' => $imgtouse, // thumbnail url for sender icon
541 '$email' => $importer['email'], // email address to send to
542 '$url' => $datarray['author-link'], // full url for the site
543 '$from' => $from, // name of the person sending the message
544 '$body' => $msg['textversion'], // text version of the message
545 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
548 // use the EmailNotification library to send the message
549 require_once("include/EmailNotification.php");
550 EmailNotification::sendTextHtmlEmail(
551 $msg['notificationfromname'],
552 t("Administrator") . '@' . $a->get_hostname(),
553 t("noreply") . '@' . $a->get_hostname(),
555 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
556 $email_html_body_tpl,
568 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
570 $item_id = $item->get_id();
571 $datarray = get_atom_elements($feed,$item);
573 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
575 intval($importer['importer_uid'])
578 // Update content if 'updated' changes
581 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
582 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
583 dbesc($datarray['body']),
584 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
586 intval($importer['importer_uid'])
590 // update last-child if it changes
592 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
593 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
594 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
595 dbesc(datetime_convert()),
597 intval($importer['importer_uid'])
599 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
600 intval($allow[0]['data']),
601 dbesc(datetime_convert()),
603 intval($importer['importer_uid'])
609 $datarray['parent-uri'] = $parent_uri;
610 $datarray['uid'] = $importer['importer_uid'];
611 $datarray['contact-id'] = $importer['id'];
612 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
613 $datarray['type'] = 'activity';
614 $datarray['gravity'] = GRAVITY_LIKE;
616 $posted_id = item_store($datarray);
618 // find out if our user is involved in this conversation and wants to be notified.
620 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
622 $myconv = q("SELECT `author-link`, `author-avatar` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
624 intval($importer['importer_uid'])
627 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
628 foreach($myconv as $conv) {
629 if(! link_compare($conv['author-link'],$importer_url))
632 push_lang($importer['language']);
633 require_once('bbcode.php');
634 $from = stripslashes($datarray['author-name']);
636 // name of the automated email sender
637 $msg['notificationfromname'] = stripslashes($datarray['author-name']);;
638 // noreply address to send from
639 $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
642 // process the message body to display properly in text mode
644 = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
647 // process the message body to display properly in text mode
649 = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
651 $imgtouse = ((link_compare($datarray['author-link'],$importer['url'])) ? $importer['thumb'] : $datarray['author-avatar']);
654 // load the template for private message notifications
655 $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
656 $email_html_body_tpl = replace_macros($tpl,array(
657 '$username' => $importer['username'],
658 '$sitename' => $a->config['sitename'], // name of this site
659 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
660 '$thumb' => $imgtouse, // thumbnail url for sender icon
661 '$url' => $datarray['author-link'], // full url for the site
662 '$from' => $from, // name of the person sending the message
663 '$body' => $msg['htmlversion'], // html version of the message
664 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
667 // load the template for private message notifications
668 $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
669 $email_text_body_tpl = replace_macros($tpl,array(
670 '$username' => $importer['username'],
671 '$sitename' => $a->config['sitename'], // name of this site
672 '$siteurl' => $a->get_baseurl(), // descriptive url of this site
673 '$thumb' => $imgtouse, // thumbnail url for sender icon
674 '$url' => $datarray['author-link'], // full url for the site
675 '$from' => $from, // name of the person sending the message
676 '$body' => $msg['textversion'], // text version of the message
677 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
680 // use the EmailNotification library to send the message
681 require_once("include/EmailNotification.php");
682 EmailNotification::sendTextHtmlEmail(
683 $msg['notificationfromname'],
684 t("Administrator@") . $a->get_hostname(),
685 t("noreply") . '@' . $a->get_hostname(),
687 sprintf( t('%s commented on an item at %s'), $from , $a->config['sitename']),
688 $email_html_body_tpl,
702 // Head post of a conversation. Have we seen it? If not, import it.
705 $item_id = $item->get_id();
706 $datarray = get_atom_elements($feed,$item);
708 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
709 $ev = bbtoevent($datarray['body']);
710 if(x($ev,'desc') && x($ev,'start')) {
711 $ev['cid'] = $importer['id'];
712 $ev['uid'] = $importer['uid'];
713 $ev['uri'] = $item_id;
714 $ev['edited'] = $datarray['edited'];
715 $ev['private'] = $datarray['private'];
717 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
719 intval($importer['uid'])
722 $ev['id'] = $r[0]['id'];
723 $xyz = event_store($ev);
728 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
730 intval($importer['importer_uid'])
733 // Update content if 'updated' changes
736 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
737 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
738 dbesc($datarray['body']),
739 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
741 intval($importer['importer_uid'])
745 // update last-child if it changes
747 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
748 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
749 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
750 intval($allow[0]['data']),
751 dbesc(datetime_convert()),
753 intval($importer['importer_uid'])
759 // This is my contact on another system, but it's really me.
760 // Turn this into a wall post.
762 if($contact['remote_self'])
763 $datarray['wall'] = 1;
765 $datarray['parent-uri'] = $item_id;
766 $datarray['uid'] = $importer['importer_uid'];
767 $datarray['contact-id'] = $importer['id'];
768 $r = item_store($datarray);
779 function dfrn_notify_content(&$a) {
781 if(x($_GET,'dfrn_id')) {
783 // initial communication from external contact, $direction is their direction.
784 // If this is a duplex communication, ours will be the opposite.
786 $dfrn_id = notags(trim($_GET['dfrn_id']));
787 $dfrn_version = (float) $_GET['dfrn_version'];
789 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
792 if(strpos($dfrn_id,':') == 1) {
793 $direction = intval(substr($dfrn_id,0,1));
794 $dfrn_id = substr($dfrn_id,2);
797 $hash = random_string();
801 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
803 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
804 VALUES( '%s', '%s', %d ) ",
810 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG );
815 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
819 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
820 $my_id = '1:' . $dfrn_id;
823 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
824 $my_id = '0:' . $dfrn_id;
831 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
832 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
833 AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
842 $id_str = $my_id . '.' . mt_rand(1000,9999);
844 $prv_key = trim($r[0]['prvkey']);
845 $pub_key = trim($r[0]['pubkey']);
846 $dplx = intval($r[0]['duplex']);
848 if((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
849 openssl_private_encrypt($hash,$challenge,$prv_key);
850 openssl_private_encrypt($id_str,$encrypted_id,$prv_key);
853 openssl_public_encrypt($hash,$challenge,$pub_key);
854 openssl_public_encrypt($id_str,$encrypted_id,$pub_key);
857 $challenge = bin2hex($challenge);
858 $encrypted_id = bin2hex($encrypted_id);
860 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
862 $rino_enable = get_config('system','rino_encrypt');
868 header("Content-type: text/xml");
870 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
871 . '<dfrn_notify>' . "\r\n"
872 . "\t" . '<status>' . $status . '</status>' . "\r\n"
873 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
874 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
875 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
876 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
877 . '</dfrn_notify>' . "\r\n" ;