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 $parent = $r[0]['parent'];
303 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
304 dbesc(datetime_convert()),
305 intval($importer['importer_uid']),
306 intval($r[0]['parent'])
309 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
310 dbesc(datetime_convert()),
311 intval($importer['importer_uid']),
316 if($posted_id && $parent) {
318 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
320 proc_run($php_path,"include/notifier.php","comment-import","$posted_id");
322 if((! $is_like) && ($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
323 require_once('bbcode.php');
324 $from = stripslashes($datarray['author-name']);
325 $tpl = load_view_file('view/cmnt_received_eml.tpl');
326 $email_tpl = replace_macros($tpl, array(
327 '$sitename' => $a->config['sitename'],
328 '$siteurl' => $a->get_baseurl(),
329 '$username' => $importer['username'],
330 '$email' => $importer['email'],
331 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
333 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
336 $res = mail($importer['email'], $from . t(' commented on an item at ') . $a->config['sitename'],
337 $email_tpl, "From: " . t('Administrator') . '@' . $a->get_hostname() );
346 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
348 $item_id = $item->get_id();
350 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
352 intval($importer['importer_uid'])
354 // FIXME update content if 'updated' changes
356 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
357 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
358 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
359 intval($allow[0]['data']),
360 dbesc(datetime_convert()),
362 intval($importer['importer_uid'])
367 $datarray = get_atom_elements($feed,$item);
368 $datarray['parent-uri'] = $parent_uri;
369 $datarray['uid'] = $importer['importer_uid'];
370 $datarray['contact-id'] = $importer['id'];
371 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
372 $datarray['type'] = 'activity';
373 $datarray['gravity'] = GRAVITY_LIKE;
376 $r = item_store($datarray);
378 // find out if our user is involved in this conversation and wants to be notified.
380 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
382 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
384 intval($importer['importer_uid'])
387 foreach($myconv as $conv) {
388 if(! link_compare($conv['author-link'],$importer['url']))
390 require_once('bbcode.php');
391 $from = stripslashes($datarray['author-name']);
392 $tpl = load_view_file('view/cmnt_received_eml.tpl');
393 $email_tpl = replace_macros($tpl, array(
394 '$sitename' => $a->config['sitename'],
395 '$siteurl' => $a->get_baseurl(),
396 '$username' => $importer['username'],
397 '$email' => $importer['email'],
399 '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
400 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
403 $res = mail($importer['email'], $from . t(" commented on an item at ")
404 . $a->config['sitename'],
405 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
414 // Head post of a conversation. Have we seen it? If not, import it.
416 $item_id = $item->get_id();
417 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
419 intval($importer['importer_uid'])
422 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
423 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
424 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
425 intval($allow[0]['data']),
426 dbesc(datetime_convert()),
428 intval($importer['importer_uid'])
435 $datarray = get_atom_elements($feed,$item);
436 $datarray['parent-uri'] = $item_id;
437 $datarray['uid'] = $importer['importer_uid'];
438 $datarray['contact-id'] = $importer['id'];
439 $r = item_store($datarray);
450 function dfrn_notify_content(&$a) {
452 if(x($_GET,'dfrn_id')) {
454 // initial communication from external contact, $direction is their direction.
455 // If this is a duplex communication, ours will be the opposite.
457 $dfrn_id = notags(trim($_GET['dfrn_id']));
458 $dfrn_version = (float) $_GET['dfrn_version'];
460 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
463 if(strpos($dfrn_id,':') == 1) {
464 $direction = intval(substr($dfrn_id,0,1));
465 $dfrn_id = substr($dfrn_id,2);
468 $hash = random_string();
472 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
474 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
475 VALUES( '%s', '%s', %d ) ",
481 logger('dfrn_notify: challenge=' . $hash );
486 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
490 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
491 $my_id = '1:' . $dfrn_id;
494 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
495 $my_id = '0:' . $dfrn_id;
502 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
503 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
512 $id_str = $my_id . '.' . mt_rand(1000,9999);
514 if((($r[0]['duplex']) && strlen($r[0]['prvkey'])) || (! strlen($r[0]['pubkey']))) {
515 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
516 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
519 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
520 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
523 $challenge = bin2hex($challenge);
524 $encrypted_id = bin2hex($encrypted_id);
526 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
528 $rino_enable = get_config('system','rino_encrypt');
534 header("Content-type: text/xml");
536 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
537 . '<dfrn_notify>' . "\r\n"
538 . "\t" . '<status>' . $status . '</status>' . "\r\n"
539 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
540 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
541 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
542 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
543 . '</dfrn_notify>' . "\r\n" ;