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",
27 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
32 // find the local user who owns this relationship.
37 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
40 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
43 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
51 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, `user`.* FROM `contact`
52 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
53 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
54 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
66 if($importer['readonly']) {
67 // We aren't receiving stuff from this person. But we will quietly ignore them
68 // rather than a blatant "go away" message.
73 // Consume notification feed. This may differ from consuming a public feed in several ways
74 // - might contain email
75 // - might contain remote followup to our message
76 // - in which case we need to accept it and then notify other conversants
77 // - we may need to send various email notifications
79 $feed = new SimplePie();
80 $feed->set_raw_data($data);
81 $feed->enable_order_by_date(false);
86 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
87 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
90 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
93 $msg['uid'] = $importer['importer_uid'];
94 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
95 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
96 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
97 $msg['contact-id'] = $importer['id'];
98 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
99 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
100 $msg['delivered'] = 1;
103 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
104 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
105 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
109 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
110 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
112 // send email notification if requested.
114 require_once('bbcode.php');
115 if($importer['notify-flags'] & NOTIFY_MAIL) {
117 $body = html_entity_decode(strip_tags(bbcode(stripslashes($msg['body']))),ENT_QUOTES,'UTF-8');
119 if(function_exists('quoted_printable_encode'))
120 $body = quoted_printable_encode($body);
124 $tpl = load_view_file('view/mail_received_eml.tpl');
125 $email_tpl = replace_macros($tpl, array(
126 '$sitename' => $a->config['sitename'],
127 '$siteurl' => $a->get_baseurl(),
128 '$username' => $importer['username'],
129 '$email' => $importer['email'],
130 '$from' => $msg['from-name'],
131 '$title' => stripslashes($msg['title']),
135 $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
136 $email_tpl, 'From: ' . t('Administrator') . '@' . $a->get_hostname() . "\r\n"
137 . 'MIME-Version: 1.0' . "\r\n"
138 . 'Content-type: text/plain; charset=UTF-8' . "\r\n"
139 . 'Content-transfer-encoding: quoted-printable' . "\r\n"
146 foreach($feed->get_items() as $item) {
150 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB , 'deleted-entry');
151 if(isset($rawdelete[0]['attribs']['']['ref'])) {
152 $uri = $rawthread[0]['attribs']['']['ref'];
154 if(isset($rawdelete[0]['attribs']['']['when'])) {
155 $when = $rawthread[0]['attribs']['']['when'];
156 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
159 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
162 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
164 intval($importer['importer_uid'])
168 if($item['uri'] == $item['parent-uri']) {
169 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
170 WHERE `parent-uri` = '%s' AND `uid` = %d",
172 dbesc(datetime_convert()),
174 intval($importer['importer_uid'])
178 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
179 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
181 dbesc(datetime_convert()),
183 intval($importer['importer_uid'])
185 if($item['last-child']) {
186 // ensure that last-child is set in case the comment that had it just got wiped.
187 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
188 dbesc(datetime_convert()),
189 dbesc($item['parent-uri']),
192 // who is the last child now?
193 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
194 ORDER BY `created` DESC LIMIT 1",
195 dbesc($item['parent-uri']),
196 intval($importer['importer_uid'])
199 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
210 $item_id = $item->get_id();
211 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
212 if(isset($rawthread[0]['attribs']['']['ref'])) {
214 $parent_uri = $rawthread[0]['attribs']['']['ref'];
218 if($feed->get_item_quantity() == 1) {
219 // remote reply to our post. Import and then notify everybody else.
220 $datarray = get_atom_elements($feed,$item);
221 $datarray['type'] = 'remote-comment';
222 $datarray['wall'] = 1;
223 $datarray['parent-uri'] = $parent_uri;
224 $datarray['uid'] = $importer['importer_uid'];
225 $datarray['contact-id'] = $importer['id'];
226 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
227 $datarray['type'] = 'activity';
228 $datarray['gravity'] = GRAVITY_LIKE;
230 $posted_id = item_store($datarray);
233 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
235 intval($importer['importer_uid'])
238 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
239 dbesc(datetime_convert()),
240 intval($importer['importer_uid']),
241 intval($r[0]['parent'])
244 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
245 dbesc(datetime_convert()),
246 intval($importer['importer_uid']),
250 if($datarray['type'] == 'remote-comment') {
251 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
253 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &",
256 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
257 require_once('bbcode.php');
258 $from = stripslashes($datarray['author-name']);
259 $tpl = load_view_file('view/cmnt_received_eml.tpl');
260 $email_tpl = replace_macros($tpl, array(
261 '$sitename' => $a->config['sitename'],
262 '$siteurl' => $a->get_baseurl(),
263 '$username' => $importer['username'],
264 '$email' => $importer['email'],
266 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
269 $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
270 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
279 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
281 $item_id = $item->get_id();
283 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
285 intval($importer['importer_uid'])
287 // FIXME update content if 'updated' changes
289 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
290 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
291 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
292 intval($allow[0]['data']),
293 dbesc(datetime_convert()),
295 intval($importer['importer_uid'])
300 $datarray = get_atom_elements($feed,$item);
301 $datarray['parent-uri'] = $parent_uri;
302 $datarray['uid'] = $importer['importer_uid'];
303 $datarray['contact-id'] = $importer['id'];
304 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
305 $datarray['type'] = 'activity';
306 $datarray['gravity'] = GRAVITY_LIKE;
309 $r = item_store($datarray);
311 // find out if our user is involved in this conversation and wants to be notified.
313 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
315 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
317 intval($importer['importer_uid'])
320 foreach($myconv as $conv) {
321 if($conv['author-link'] != $importer['url'])
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'],
332 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
335 $res = mail($importer['email'], $from . t(" commented on an item at ")
336 . $a->config['sitename'],
337 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
346 // Head post of a conversation. Have we seen it? If not, import it.
348 $item_id = $item->get_id();
349 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
351 intval($importer['importer_uid'])
354 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
355 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
356 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
357 intval($allow[0]['data']),
358 dbesc(datetime_convert()),
360 intval($importer['importer_uid'])
367 $datarray = get_atom_elements($feed,$item);
368 $datarray['parent-uri'] = $item_id;
369 $datarray['uid'] = $importer['importer_uid'];
370 $datarray['contact-id'] = $importer['id'];
371 $r = item_store($datarray);
382 function dfrn_notify_content(&$a) {
384 if(x($_GET,'dfrn_id')) {
386 // initial communication from external contact, $direction is their direction.
387 // If this is a duplex communication, ours will be the opposite.
389 $dfrn_id = notags(trim($_GET['dfrn_id']));
390 $dfrn_version = (float) $_GET['dfrn_version'];
394 if(strpos($dfrn_id,':') == 1) {
395 $direction = intval(substr($dfrn_id,0,1));
396 $dfrn_id = substr($dfrn_id,2);
399 $hash = random_string();
403 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
405 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
406 VALUES( '%s', '%s', '%s') ",
416 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
420 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
421 $my_id = '1:' . $dfrn_id;
424 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
425 $my_id = '0:' . $dfrn_id;
432 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
433 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
442 $id_str = $my_id . '.' . mt_rand(1000,9999);
444 if((($r[0]['duplex']) && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
445 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
446 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
449 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
450 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
453 $challenge = bin2hex($challenge);
454 $encrypted_id = bin2hex($encrypted_id);
456 header("Content-type: text/xml");
458 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
459 . '<dfrn_notify>' . "\r\n"
460 . "\t" . '<status>' . $status . '</status>' . "\r\n"
461 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
462 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
463 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
464 . '</dfrn_notify>' . "\r\n" ;