3 require_once('simplepie/simplepie.inc');
4 require_once('include/items.php');
7 function dfrn_notify_post(&$a) {
9 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
10 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
11 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
12 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
13 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
14 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
17 if(strpos($dfrn_id,':') == 1) {
18 $direction = intval(substr($dfrn_id,0,1));
19 $dfrn_id = substr($dfrn_id,2);
22 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
27 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
31 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
36 // find the local user who owns this relationship.
41 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
44 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
47 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
55 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
56 `contact`.`pubkey` AS `cpubkey`, `contact`.`prvkey` AS `cprvkey`, `user`.* FROM `contact`
57 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
58 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
59 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
64 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
69 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
73 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
74 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
79 * Relationship is dissolved permanently
82 require_once('include/Contact.php');
83 contact_remove($importer['id']);
84 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
90 $rawkey = hex2bin(trim($key));
91 logger('rino: md5 raw key: ' . md5($rawkey));
94 if($dfrn_version >= 2.1) {
95 if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
96 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
99 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
103 if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
104 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
107 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
111 logger('rino: received key : ' . $final_key);
112 $data = aes_decrypt(hex2bin($data),$final_key);
113 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
119 if($importer['readonly']) {
120 // We aren't receiving stuff from this person. But we will quietly ignore them
121 // rather than a blatant "go away" message.
122 logger('dfrn_notify: ignoring');
127 // Consume notification feed. This may differ from consuming a public feed in several ways
128 // - might contain email
129 // - might contain remote followup to our message
130 // - in which case we need to accept it and then notify other conversants
131 // - we may need to send various email notifications
133 $feed = new SimplePie();
134 $feed->set_raw_data($data);
135 $feed->enable_order_by_date(false);
140 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
141 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
143 logger('dfrn_notify: private message received');
146 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
149 $msg['uid'] = $importer['importer_uid'];
150 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
151 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
152 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
153 $msg['contact-id'] = $importer['id'];
154 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
155 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
158 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
159 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
160 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
164 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
165 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
167 // send email notification if requested.
169 require_once('bbcode.php');
170 if($importer['notify-flags'] & NOTIFY_MAIL) {
172 $body = html_entity_decode(strip_tags(bbcode(stripslashes($msg['body']))),ENT_QUOTES,'UTF-8');
174 if(function_exists('quoted_printable_encode'))
175 $body = quoted_printable_encode($body);
179 $tpl = load_view_file('view/mail_received_eml.tpl');
180 $email_tpl = replace_macros($tpl, array(
181 '$sitename' => $a->config['sitename'],
182 '$siteurl' => $a->get_baseurl(),
183 '$username' => $importer['username'],
184 '$email' => $importer['email'],
185 '$from' => $msg['from-name'],
186 '$title' => stripslashes($msg['title']),
190 $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
191 $email_tpl, 'From: ' . t('Administrator') . '@' . $a->get_hostname() . "\r\n"
192 . 'MIME-Version: 1.0' . "\r\n"
193 . 'Content-type: text/plain; charset=UTF-8' . "\r\n"
194 . 'Content-transfer-encoding: quoted-printable' . "\r\n"
201 logger('dfrn_notify: feed item count = ' . $feed->get_item_quantity());
203 // process any deleted entries
205 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
206 if(is_array($del_entries) && count($del_entries)) {
207 foreach($del_entries as $dentry) {
209 if(isset($dentry['attribs']['']['ref'])) {
210 $uri = $dentry['attribs']['']['ref'];
212 if(isset($dentry['attribs']['']['when'])) {
213 $when = $dentry['attribs']['']['when'];
214 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
217 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
220 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
222 intval($importer['importer_uid'])
227 if(! $item['deleted'])
228 logger('dfrn_notify: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
230 if($item['uri'] == $item['parent-uri']) {
231 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
232 WHERE `parent-uri` = '%s' AND `uid` = %d",
234 dbesc(datetime_convert()),
236 intval($importer['importer_uid'])
240 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
241 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
243 dbesc(datetime_convert()),
245 intval($importer['importer_uid'])
247 if($item['last-child']) {
248 // ensure that last-child is set in case the comment that had it just got wiped.
249 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
250 dbesc(datetime_convert()),
251 dbesc($item['parent-uri']),
254 // who is the last child now?
255 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
256 ORDER BY `created` DESC LIMIT 1",
257 dbesc($item['parent-uri']),
258 intval($importer['importer_uid'])
261 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
273 foreach($feed->get_items() as $item) {
276 $item_id = $item->get_id();
277 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
278 if(isset($rawthread[0]['attribs']['']['ref'])) {
280 $parent_uri = $rawthread[0]['attribs']['']['ref'];
284 if($feed->get_item_quantity() == 1) {
285 logger('dfrn_notify: received remote comment');
287 // remote reply to our post. Import and then notify everybody else.
288 $datarray = get_atom_elements($feed,$item);
289 $datarray['type'] = 'remote-comment';
290 $datarray['wall'] = 1;
291 $datarray['parent-uri'] = $parent_uri;
292 $datarray['uid'] = $importer['importer_uid'];
293 $datarray['contact-id'] = $importer['id'];
294 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
296 $datarray['type'] = 'activity';
297 $datarray['gravity'] = GRAVITY_LIKE;
298 $datarray['last-child'] = 0;
300 $posted_id = item_store($datarray);
304 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
306 intval($importer['importer_uid'])
309 $parent = $r[0]['parent'];
312 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
313 dbesc(datetime_convert()),
314 intval($importer['importer_uid']),
315 intval($r[0]['parent'])
318 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
319 dbesc(datetime_convert()),
320 intval($importer['importer_uid']),
325 if($posted_id && $parent) {
327 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
329 proc_run($php_path,"include/notifier.php","comment-import","$posted_id");
331 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
332 require_once('bbcode.php');
333 $from = stripslashes($datarray['author-name']);
334 $tpl = load_view_file('view/cmnt_received_eml.tpl');
335 $email_tpl = replace_macros($tpl, array(
336 '$sitename' => $a->config['sitename'],
337 '$siteurl' => $a->get_baseurl(),
338 '$username' => $importer['username'],
339 '$email' => $importer['email'],
340 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
342 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
345 $res = mail($importer['email'], $from . t(' commented on an item at ') . $a->config['sitename'],
346 $email_tpl, "From: " . t('Administrator') . '@' . $a->get_hostname() );
354 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
356 $item_id = $item->get_id();
358 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
360 intval($importer['importer_uid'])
362 // FIXME update content if 'updated' changes
364 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
365 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
366 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
367 intval($allow[0]['data']),
368 dbesc(datetime_convert()),
370 intval($importer['importer_uid'])
375 $datarray = get_atom_elements($feed,$item);
376 $datarray['parent-uri'] = $parent_uri;
377 $datarray['uid'] = $importer['importer_uid'];
378 $datarray['contact-id'] = $importer['id'];
379 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
380 $datarray['type'] = 'activity';
381 $datarray['gravity'] = GRAVITY_LIKE;
383 $r = item_store($datarray);
385 // find out if our user is involved in this conversation and wants to be notified.
387 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
389 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
391 intval($importer['importer_uid'])
394 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
395 foreach($myconv as $conv) {
396 if(! link_compare($conv['author-link'],$importer_url))
398 require_once('bbcode.php');
399 $from = stripslashes($datarray['author-name']);
400 $tpl = load_view_file('view/cmnt_received_eml.tpl');
401 $email_tpl = replace_macros($tpl, array(
402 '$sitename' => $a->config['sitename'],
403 '$siteurl' => $a->get_baseurl(),
404 '$username' => $importer['username'],
405 '$email' => $importer['email'],
407 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
408 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
411 $res = mail($importer['email'], $from . t(" commented on an item at ")
412 . $a->config['sitename'],
413 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
422 // Head post of a conversation. Have we seen it? If not, import it.
424 $item_id = $item->get_id();
425 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
427 intval($importer['importer_uid'])
430 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
431 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
432 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
433 intval($allow[0]['data']),
434 dbesc(datetime_convert()),
436 intval($importer['importer_uid'])
443 $datarray = get_atom_elements($feed,$item);
444 $datarray['parent-uri'] = $item_id;
445 $datarray['uid'] = $importer['importer_uid'];
446 $datarray['contact-id'] = $importer['id'];
447 $r = item_store($datarray);
458 function dfrn_notify_content(&$a) {
460 if(x($_GET,'dfrn_id')) {
462 // initial communication from external contact, $direction is their direction.
463 // If this is a duplex communication, ours will be the opposite.
465 $dfrn_id = notags(trim($_GET['dfrn_id']));
466 $dfrn_version = (float) $_GET['dfrn_version'];
468 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
471 if(strpos($dfrn_id,':') == 1) {
472 $direction = intval(substr($dfrn_id,0,1));
473 $dfrn_id = substr($dfrn_id,2);
476 $hash = random_string();
480 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
482 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
483 VALUES( '%s', '%s', %d ) ",
489 logger('dfrn_notify: challenge=' . $hash );
494 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
498 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
499 $my_id = '1:' . $dfrn_id;
502 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
503 $my_id = '0:' . $dfrn_id;
510 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
511 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
520 $id_str = $my_id . '.' . mt_rand(1000,9999);
522 if((($r[0]['duplex']) && strlen($r[0]['prvkey'])) || (! strlen($r[0]['pubkey']))) {
523 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
524 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
527 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
528 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
531 $challenge = bin2hex($challenge);
532 $encrypted_id = bin2hex($encrypted_id);
534 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
536 $rino_enable = get_config('system','rino_encrypt');
542 header("Content-type: text/xml");
544 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
545 . '<dfrn_notify>' . "\r\n"
546 . "\t" . '<status>' . $status . '</status>' . "\r\n"
547 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
548 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
549 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
550 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
551 . '</dfrn_notify>' . "\r\n" ;