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'] : '');
16 if(strpos($dfrn_id,':') == 1) {
17 $direction = intval(substr($dfrn_id,0,1));
18 $dfrn_id = substr($dfrn_id,2);
21 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
26 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id);
30 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
35 // find the local user who owns this relationship.
40 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
43 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
54 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
55 `contact`.`pubkey` AS `cpubkey`, `contact`.`prvkey` AS `cprvkey`, `user`.* FROM `contact`
56 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
57 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
58 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
63 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
70 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
71 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
73 if($importer['readonly']) {
74 // We aren't receiving stuff from this person. But we will quietly ignore them
75 // rather than a blatant "go away" message.
76 logger('dfrn_notify: ignoring');
82 $rawkey = hex2bin(trim($key));
83 logger('rino: md5 raw key: ' . md5($rawkey));
86 if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
87 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
90 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
93 logger('rino: received key : ' . $final_key);
94 $data = aes_decrypt(hex2bin($data),$final_key);
95 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
98 // Consume notification feed. This may differ from consuming a public feed in several ways
99 // - might contain email
100 // - might contain remote followup to our message
101 // - in which case we need to accept it and then notify other conversants
102 // - we may need to send various email notifications
104 $feed = new SimplePie();
105 $feed->set_raw_data($data);
106 $feed->enable_order_by_date(false);
111 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
112 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
114 logger('dfrn_notify: private message received');
117 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
120 $msg['uid'] = $importer['importer_uid'];
121 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
122 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
123 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
124 $msg['contact-id'] = $importer['id'];
125 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
126 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
129 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
130 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
131 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
135 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
136 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
138 // send email notification if requested.
140 require_once('bbcode.php');
141 if($importer['notify-flags'] & NOTIFY_MAIL) {
143 $body = html_entity_decode(strip_tags(bbcode(stripslashes($msg['body']))),ENT_QUOTES,'UTF-8');
145 if(function_exists('quoted_printable_encode'))
146 $body = quoted_printable_encode($body);
150 $tpl = load_view_file('view/mail_received_eml.tpl');
151 $email_tpl = replace_macros($tpl, array(
152 '$sitename' => $a->config['sitename'],
153 '$siteurl' => $a->get_baseurl(),
154 '$username' => $importer['username'],
155 '$email' => $importer['email'],
156 '$from' => $msg['from-name'],
157 '$title' => stripslashes($msg['title']),
161 $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
162 $email_tpl, 'From: ' . t('Administrator') . '@' . $a->get_hostname() . "\r\n"
163 . 'MIME-Version: 1.0' . "\r\n"
164 . 'Content-type: text/plain; charset=UTF-8' . "\r\n"
165 . 'Content-transfer-encoding: quoted-printable' . "\r\n"
172 logger('dfrn_notify: feed item count = ' . $feed->get_item_quantity());
174 foreach($feed->get_items() as $item) {
178 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB , 'deleted-entry');
179 if(isset($rawdelete[0]['attribs']['']['ref'])) {
180 $uri = $rawthread[0]['attribs']['']['ref'];
182 if(isset($rawdelete[0]['attribs']['']['when'])) {
183 $when = $rawthread[0]['attribs']['']['when'];
184 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
187 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
190 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
192 intval($importer['importer_uid'])
196 if($item['uri'] == $item['parent-uri']) {
197 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
198 WHERE `parent-uri` = '%s' AND `uid` = %d",
200 dbesc(datetime_convert()),
202 intval($importer['importer_uid'])
206 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
207 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
209 dbesc(datetime_convert()),
211 intval($importer['importer_uid'])
213 if($item['last-child']) {
214 // ensure that last-child is set in case the comment that had it just got wiped.
215 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
216 dbesc(datetime_convert()),
217 dbesc($item['parent-uri']),
220 // who is the last child now?
221 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
222 ORDER BY `created` DESC LIMIT 1",
223 dbesc($item['parent-uri']),
224 intval($importer['importer_uid'])
227 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
238 $item_id = $item->get_id();
239 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
240 if(isset($rawthread[0]['attribs']['']['ref'])) {
242 $parent_uri = $rawthread[0]['attribs']['']['ref'];
246 if($feed->get_item_quantity() == 1) {
247 logger('dfrn_notify: received remote comment');
249 // remote reply to our post. Import and then notify everybody else.
250 $datarray = get_atom_elements($feed,$item);
251 $datarray['type'] = 'remote-comment';
252 $datarray['wall'] = 1;
253 $datarray['parent-uri'] = $parent_uri;
254 $datarray['uid'] = $importer['importer_uid'];
255 $datarray['contact-id'] = $importer['id'];
256 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
258 $datarray['type'] = 'activity';
259 $datarray['gravity'] = GRAVITY_LIKE;
260 $datarray['last-child'] = 0;
262 $posted_id = item_store($datarray);
266 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
268 intval($importer['importer_uid'])
271 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
272 dbesc(datetime_convert()),
273 intval($importer['importer_uid']),
274 intval($r[0]['parent'])
277 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
278 dbesc(datetime_convert()),
279 intval($importer['importer_uid']),
284 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
286 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &",
289 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
290 require_once('bbcode.php');
291 $from = stripslashes($datarray['author-name']);
292 $tpl = load_view_file('view/cmnt_received_eml.tpl');
293 $email_tpl = replace_macros($tpl, array(
294 '$sitename' => $a->config['sitename'],
295 '$siteurl' => $a->get_baseurl(),
296 '$username' => $importer['username'],
297 '$email' => $importer['email'],
298 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
300 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
303 $res = mail($importer['email'], $from . t(' commented on an item at ') . $a->config['sitename'],
304 $email_tpl, "From: " . t('Administrator') . '@' . $a->get_hostname() );
313 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
315 $item_id = $item->get_id();
317 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
319 intval($importer['importer_uid'])
321 // FIXME update content if 'updated' changes
323 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
324 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
325 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
326 intval($allow[0]['data']),
327 dbesc(datetime_convert()),
329 intval($importer['importer_uid'])
334 $datarray = get_atom_elements($feed,$item);
335 $datarray['parent-uri'] = $parent_uri;
336 $datarray['uid'] = $importer['importer_uid'];
337 $datarray['contact-id'] = $importer['id'];
338 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
339 $datarray['type'] = 'activity';
340 $datarray['gravity'] = GRAVITY_LIKE;
343 $r = item_store($datarray);
345 // find out if our user is involved in this conversation and wants to be notified.
347 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
349 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
351 intval($importer['importer_uid'])
354 foreach($myconv as $conv) {
355 if($conv['author-link'] != $importer['url'])
357 require_once('bbcode.php');
358 $from = stripslashes($datarray['author-name']);
359 $tpl = load_view_file('view/cmnt_received_eml.tpl');
360 $email_tpl = replace_macros($tpl, array(
361 '$sitename' => $a->config['sitename'],
362 '$siteurl' => $a->get_baseurl(),
363 '$username' => $importer['username'],
364 '$email' => $importer['email'],
366 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
367 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
370 $res = mail($importer['email'], $from . t(" commented on an item at ")
371 . $a->config['sitename'],
372 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
381 // Head post of a conversation. Have we seen it? If not, import it.
383 $item_id = $item->get_id();
384 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
386 intval($importer['importer_uid'])
389 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
390 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
391 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
392 intval($allow[0]['data']),
393 dbesc(datetime_convert()),
395 intval($importer['importer_uid'])
402 $datarray = get_atom_elements($feed,$item);
403 $datarray['parent-uri'] = $item_id;
404 $datarray['uid'] = $importer['importer_uid'];
405 $datarray['contact-id'] = $importer['id'];
406 $r = item_store($datarray);
417 function dfrn_notify_content(&$a) {
419 if(x($_GET,'dfrn_id')) {
421 // initial communication from external contact, $direction is their direction.
422 // If this is a duplex communication, ours will be the opposite.
424 $dfrn_id = notags(trim($_GET['dfrn_id']));
425 $dfrn_version = (float) $_GET['dfrn_version'];
427 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
430 if(strpos($dfrn_id,':') == 1) {
431 $direction = intval(substr($dfrn_id,0,1));
432 $dfrn_id = substr($dfrn_id,2);
435 $hash = random_string();
439 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
441 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
442 VALUES( '%s', '%s', '%s') ",
452 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
456 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
457 $my_id = '1:' . $dfrn_id;
460 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
461 $my_id = '0:' . $dfrn_id;
468 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
469 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
478 $id_str = $my_id . '.' . mt_rand(1000,9999);
480 if((($r[0]['duplex']) && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
481 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
482 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
485 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
486 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
489 $challenge = bin2hex($challenge);
490 $encrypted_id = bin2hex($encrypted_id);
492 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
494 $rino_enable = get_config('system','rino_encrypt');
500 header("Content-type: text/xml");
502 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
503 . '<dfrn_notify>' . "\r\n"
504 . "\t" . '<status>' . $status . '</status>' . "\r\n"
505 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
506 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
507 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
508 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
509 . '</dfrn_notify>' . "\r\n" ;