3 require_once('simplepie/simplepie.inc');
4 require_once('include/items.php');
7 function dfrn_notify_post(&$a) {
9 $dfrn_id = notags(trim($_POST['dfrn_id']));
10 $dfrn_version = (float) $_POST['dfrn_version'];
11 $challenge = notags(trim($_POST['challenge']));
12 $data = $_POST['data'];
15 if(strpos($dfrn_id,':') == 1) {
16 $direction = intval(substr($dfrn_id,0,1));
17 $dfrn_id = substr($dfrn_id,2);
20 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
25 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id);
29 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
34 // find the local user who owns this relationship.
39 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
42 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
45 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
53 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, `user`.* FROM `contact`
54 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
55 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
56 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
61 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
68 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
69 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
71 if($importer['readonly']) {
72 // We aren't receiving stuff from this person. But we will quietly ignore them
73 // rather than a blatant "go away" message.
74 logger('dfrn_notify: ignoring');
79 // Consume notification feed. This may differ from consuming a public feed in several ways
80 // - might contain email
81 // - might contain remote followup to our message
82 // - in which case we need to accept it and then notify other conversants
83 // - we may need to send various email notifications
85 $feed = new SimplePie();
86 $feed->set_raw_data($data);
87 $feed->enable_order_by_date(false);
92 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
93 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
95 logger('dfrn_notify: private message received');
98 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
101 $msg['uid'] = $importer['importer_uid'];
102 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
103 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
104 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
105 $msg['contact-id'] = $importer['id'];
106 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
107 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
110 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
111 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
112 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
116 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
117 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
119 // send email notification if requested.
121 require_once('bbcode.php');
122 if($importer['notify-flags'] & NOTIFY_MAIL) {
124 $body = html_entity_decode(strip_tags(bbcode(stripslashes($msg['body']))),ENT_QUOTES,'UTF-8');
126 if(function_exists('quoted_printable_encode'))
127 $body = quoted_printable_encode($body);
131 $tpl = load_view_file('view/mail_received_eml.tpl');
132 $email_tpl = replace_macros($tpl, array(
133 '$sitename' => $a->config['sitename'],
134 '$siteurl' => $a->get_baseurl(),
135 '$username' => $importer['username'],
136 '$email' => $importer['email'],
137 '$from' => $msg['from-name'],
138 '$title' => stripslashes($msg['title']),
142 $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
143 $email_tpl, 'From: ' . t('Administrator') . '@' . $a->get_hostname() . "\r\n"
144 . 'MIME-Version: 1.0' . "\r\n"
145 . 'Content-type: text/plain; charset=UTF-8' . "\r\n"
146 . 'Content-transfer-encoding: quoted-printable' . "\r\n"
153 logger('dfrn_notify: feed item count = ' . $feed->get_item_quantity());
155 foreach($feed->get_items() as $item) {
159 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB , 'deleted-entry');
160 if(isset($rawdelete[0]['attribs']['']['ref'])) {
161 $uri = $rawthread[0]['attribs']['']['ref'];
163 if(isset($rawdelete[0]['attribs']['']['when'])) {
164 $when = $rawthread[0]['attribs']['']['when'];
165 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
168 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
171 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
173 intval($importer['importer_uid'])
177 if($item['uri'] == $item['parent-uri']) {
178 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
179 WHERE `parent-uri` = '%s' AND `uid` = %d",
181 dbesc(datetime_convert()),
183 intval($importer['importer_uid'])
187 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
188 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
190 dbesc(datetime_convert()),
192 intval($importer['importer_uid'])
194 if($item['last-child']) {
195 // ensure that last-child is set in case the comment that had it just got wiped.
196 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
197 dbesc(datetime_convert()),
198 dbesc($item['parent-uri']),
201 // who is the last child now?
202 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
203 ORDER BY `created` DESC LIMIT 1",
204 dbesc($item['parent-uri']),
205 intval($importer['importer_uid'])
208 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
219 $item_id = $item->get_id();
220 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
221 if(isset($rawthread[0]['attribs']['']['ref'])) {
223 $parent_uri = $rawthread[0]['attribs']['']['ref'];
227 if($feed->get_item_quantity() == 1) {
228 logger('dfrn_notify: received remote comment');
229 // remote reply to our post. Import and then notify everybody else.
230 $datarray = get_atom_elements($feed,$item);
231 $datarray['type'] = 'remote-comment';
232 $datarray['wall'] = 1;
233 $datarray['parent-uri'] = $parent_uri;
234 $datarray['uid'] = $importer['importer_uid'];
235 $datarray['contact-id'] = $importer['id'];
236 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
237 $datarray['type'] = 'activity';
238 $datarray['gravity'] = GRAVITY_LIKE;
240 $posted_id = item_store($datarray);
243 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
245 intval($importer['importer_uid'])
248 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
249 dbesc(datetime_convert()),
250 intval($importer['importer_uid']),
251 intval($r[0]['parent'])
254 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
255 dbesc(datetime_convert()),
256 intval($importer['importer_uid']),
260 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
262 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &",
265 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
266 require_once('bbcode.php');
267 $from = stripslashes($datarray['author-name']);
268 $tpl = load_view_file('view/cmnt_received_eml.tpl');
269 $email_tpl = replace_macros($tpl, array(
270 '$sitename' => $a->config['sitename'],
271 '$siteurl' => $a->get_baseurl(),
272 '$username' => $importer['username'],
273 '$email' => $importer['email'],
274 '$display' => $a->get_baseurl() . '/display/' . $posted_id,
276 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
279 $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
280 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
288 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
290 $item_id = $item->get_id();
292 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
294 intval($importer['importer_uid'])
296 // FIXME update content if 'updated' changes
298 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
299 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
300 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
301 intval($allow[0]['data']),
302 dbesc(datetime_convert()),
304 intval($importer['importer_uid'])
309 $datarray = get_atom_elements($feed,$item);
310 $datarray['parent-uri'] = $parent_uri;
311 $datarray['uid'] = $importer['importer_uid'];
312 $datarray['contact-id'] = $importer['id'];
313 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
314 $datarray['type'] = 'activity';
315 $datarray['gravity'] = GRAVITY_LIKE;
318 $r = item_store($datarray);
320 // find out if our user is involved in this conversation and wants to be notified.
322 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
324 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
326 intval($importer['importer_uid'])
329 foreach($myconv as $conv) {
330 if($conv['author-link'] != $importer['url'])
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'],
341 '$display' => $a->get_baseurl() . '/display' . $r,
342 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
345 $res = mail($importer['email'], $from . t(" commented on an item at ")
346 . $a->config['sitename'],
347 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
356 // Head post of a conversation. Have we seen it? If not, import it.
358 $item_id = $item->get_id();
359 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
361 intval($importer['importer_uid'])
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'])
377 $datarray = get_atom_elements($feed,$item);
378 $datarray['parent-uri'] = $item_id;
379 $datarray['uid'] = $importer['importer_uid'];
380 $datarray['contact-id'] = $importer['id'];
381 $r = item_store($datarray);
392 function dfrn_notify_content(&$a) {
394 if(x($_GET,'dfrn_id')) {
396 // initial communication from external contact, $direction is their direction.
397 // If this is a duplex communication, ours will be the opposite.
399 $dfrn_id = notags(trim($_GET['dfrn_id']));
400 $dfrn_version = (float) $_GET['dfrn_version'];
402 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
405 if(strpos($dfrn_id,':') == 1) {
406 $direction = intval(substr($dfrn_id,0,1));
407 $dfrn_id = substr($dfrn_id,2);
410 $hash = random_string();
414 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
416 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
417 VALUES( '%s', '%s', '%s') ",
427 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
431 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
432 $my_id = '1:' . $dfrn_id;
435 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
436 $my_id = '0:' . $dfrn_id;
443 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
444 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
453 $id_str = $my_id . '.' . mt_rand(1000,9999);
455 if((($r[0]['duplex']) && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
456 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
457 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
460 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
461 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
464 $challenge = bin2hex($challenge);
465 $encrypted_id = bin2hex($encrypted_id);
467 header("Content-type: text/xml");
469 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
470 . '<dfrn_notify>' . "\r\n"
471 . "\t" . '<status>' . $status . '</status>' . "\r\n"
472 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
473 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
474 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
475 . '</dfrn_notify>' . "\r\n" ;