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 foreach($feed->get_items() as $item) {
207 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB , 'deleted-entry');
208 if(isset($rawdelete[0]['attribs']['']['ref'])) {
209 $uri = $rawthread[0]['attribs']['']['ref'];
211 if(isset($rawdelete[0]['attribs']['']['when'])) {
212 $when = $rawthread[0]['attribs']['']['when'];
213 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
216 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
219 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
221 intval($importer['importer_uid'])
225 if($item['uri'] == $item['parent-uri']) {
226 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
227 WHERE `parent-uri` = '%s' AND `uid` = %d",
229 dbesc(datetime_convert()),
231 intval($importer['importer_uid'])
235 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
236 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
238 dbesc(datetime_convert()),
240 intval($importer['importer_uid'])
242 if($item['last-child']) {
243 // ensure that last-child is set in case the comment that had it just got wiped.
244 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
245 dbesc(datetime_convert()),
246 dbesc($item['parent-uri']),
249 // who is the last child now?
250 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
251 ORDER BY `created` DESC LIMIT 1",
252 dbesc($item['parent-uri']),
253 intval($importer['importer_uid'])
256 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
267 $item_id = $item->get_id();
268 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
269 if(isset($rawthread[0]['attribs']['']['ref'])) {
271 $parent_uri = $rawthread[0]['attribs']['']['ref'];
275 if($feed->get_item_quantity() == 1) {
276 logger('dfrn_notify: received remote comment');
278 // remote reply to our post. Import and then notify everybody else.
279 $datarray = get_atom_elements($feed,$item);
280 $datarray['type'] = 'remote-comment';
281 $datarray['wall'] = 1;
282 $datarray['parent-uri'] = $parent_uri;
283 $datarray['uid'] = $importer['importer_uid'];
284 $datarray['contact-id'] = $importer['id'];
285 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
287 $datarray['type'] = 'activity';
288 $datarray['gravity'] = GRAVITY_LIKE;
289 $datarray['last-child'] = 0;
291 $posted_id = item_store($datarray);
295 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
297 intval($importer['importer_uid'])
300 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
301 dbesc(datetime_convert()),
302 intval($importer['importer_uid']),
303 intval($r[0]['parent'])
306 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
307 dbesc(datetime_convert()),
308 intval($importer['importer_uid']),
313 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
315 //proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &", array(),$foo));
316 proc_run($php_path,"include/notifier.php","comment-import","$posted_id");
318 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
319 require_once('bbcode.php');
320 $from = stripslashes($datarray['author-name']);
321 $tpl = load_view_file('view/cmnt_received_eml.tpl');
322 $email_tpl = replace_macros($tpl, array(
323 '$sitename' => $a->config['sitename'],
324 '$siteurl' => $a->get_baseurl(),
325 '$username' => $importer['username'],
326 '$email' => $importer['email'],
327 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
329 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
332 $res = mail($importer['email'], $from . t(' commented on an item at ') . $a->config['sitename'],
333 $email_tpl, "From: " . t('Administrator') . '@' . $a->get_hostname() );
342 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
344 $item_id = $item->get_id();
346 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
348 intval($importer['importer_uid'])
350 // FIXME update content if 'updated' changes
352 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
353 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
354 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
355 intval($allow[0]['data']),
356 dbesc(datetime_convert()),
358 intval($importer['importer_uid'])
363 $datarray = get_atom_elements($feed,$item);
364 $datarray['parent-uri'] = $parent_uri;
365 $datarray['uid'] = $importer['importer_uid'];
366 $datarray['contact-id'] = $importer['id'];
367 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
368 $datarray['type'] = 'activity';
369 $datarray['gravity'] = GRAVITY_LIKE;
372 $r = item_store($datarray);
374 // find out if our user is involved in this conversation and wants to be notified.
376 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
378 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
380 intval($importer['importer_uid'])
383 foreach($myconv as $conv) {
384 if($conv['author-link'] != $importer['url'])
386 require_once('bbcode.php');
387 $from = stripslashes($datarray['author-name']);
388 $tpl = load_view_file('view/cmnt_received_eml.tpl');
389 $email_tpl = replace_macros($tpl, array(
390 '$sitename' => $a->config['sitename'],
391 '$siteurl' => $a->get_baseurl(),
392 '$username' => $importer['username'],
393 '$email' => $importer['email'],
395 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
396 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
399 $res = mail($importer['email'], $from . t(" commented on an item at ")
400 . $a->config['sitename'],
401 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
410 // Head post of a conversation. Have we seen it? If not, import it.
412 $item_id = $item->get_id();
413 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
415 intval($importer['importer_uid'])
418 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
419 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
420 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
421 intval($allow[0]['data']),
422 dbesc(datetime_convert()),
424 intval($importer['importer_uid'])
431 $datarray = get_atom_elements($feed,$item);
432 $datarray['parent-uri'] = $item_id;
433 $datarray['uid'] = $importer['importer_uid'];
434 $datarray['contact-id'] = $importer['id'];
435 $r = item_store($datarray);
446 function dfrn_notify_content(&$a) {
448 if(x($_GET,'dfrn_id')) {
450 // initial communication from external contact, $direction is their direction.
451 // If this is a duplex communication, ours will be the opposite.
453 $dfrn_id = notags(trim($_GET['dfrn_id']));
454 $dfrn_version = (float) $_GET['dfrn_version'];
456 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
459 if(strpos($dfrn_id,':') == 1) {
460 $direction = intval(substr($dfrn_id,0,1));
461 $dfrn_id = substr($dfrn_id,2);
464 $hash = random_string();
468 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
470 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
471 VALUES( '%s', '%s', %d ) ",
477 logger('dfrn_notify: challenge=' . $hash );
482 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
486 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
487 $my_id = '1:' . $dfrn_id;
490 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
491 $my_id = '0:' . $dfrn_id;
498 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
499 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
508 $id_str = $my_id . '.' . mt_rand(1000,9999);
510 if((($r[0]['duplex']) && strlen($r[0]['prvkey'])) || (! strlen($r[0]['pubkey']))) {
511 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
512 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
515 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
516 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
519 $challenge = bin2hex($challenge);
520 $encrypted_id = bin2hex($encrypted_id);
522 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
524 $rino_enable = get_config('system','rino_encrypt');
530 header("Content-type: text/xml");
532 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
533 . '<dfrn_notify>' . "\r\n"
534 . "\t" . '<status>' . $status . '</status>' . "\r\n"
535 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
536 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
537 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
538 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
539 . '</dfrn_notify>' . "\r\n" ;