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 $challenge = notags(trim($_POST['challenge']));
11 $data = $_POST['data'];
12 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
19 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
24 // find the local user who owns this relationship.
26 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, `user`.* FROM `contact`
27 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
28 WHERE ( `issued-id` = '%s' OR ( `duplex` = 1 AND `dfrn-id` = '%s' )) LIMIT 1",
40 $feed = new SimplePie();
41 $feed->set_raw_data($data);
42 $feed->enable_order_by_date(false);
47 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
48 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
50 if($importer['readonly']) {
51 // We aren't receiving email from this person. But we will quietly ignore them
52 // rather than a blatant "go away" message.
58 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
61 $msg['uid'] = $importer['importer_uid'];
62 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
63 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
64 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
65 $msg['contact-id'] = $importer['id'];
66 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
67 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
68 $msg['delivered'] = 1;
71 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
72 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
73 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
77 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
78 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
80 // send email notification if requested.
82 require_once('bbcode.php');
83 if($importer['notify-flags'] & NOTIFY_MAIL) {
84 $tpl = file_get_contents('view/mail_received_eml.tpl');
85 $email_tpl = replace_macros($tpl, array(
86 '$sitename' => $a->config['sitename'],
87 '$siteurl' => $a->get_baseurl(),
88 '$username' => $importer['username'],
89 '$email' => $importer['email'],
90 '$from' => $msg['from-name'],
91 '$title' => $msg['title'],
92 '$body' => strip_tags(bbcode($msg['body']))
95 $res = mail($importer['email'], t("New mail received at ") . $a->config['sitename'],
96 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
102 if($importer['readonly']) {
105 return; // NOTREACHED
108 foreach($feed->get_items() as $item) {
112 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
113 if(isset($rawdelete[0]['attribs']['']['ref'])) {
114 $uri = $rawthread[0]['attribs']['']['ref'];
116 if(isset($rawdelete[0]['attribs']['']['when'])) {
117 $when = $rawthread[0]['attribs']['']['when'];
118 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
121 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
124 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
126 intval($importer['importer_uid'])
130 if($item['uri'] == $item['parent-uri']) {
131 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
132 WHERE `parent-uri` = '%s'",
134 dbesc(datetime_convert()),
139 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
140 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
142 dbesc(datetime_convert()),
144 intval($importer['importer_uid'])
146 if($item['last-child']) {
147 // ensure that last-child is set in case the comment that had it just got wiped.
148 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
149 dbesc(datetime_convert()),
150 dbesc($item['parent-uri']),
153 // who is the last child now?
154 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0
155 ORDER BY `edited` DESC LIMIT 1",
156 dbesc($item['parent-uri'])
159 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
170 $item_id = $item->get_id();
171 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
172 if(isset($rawthread[0]['attribs']['']['ref'])) {
174 $parent_uri = $rawthread[0]['attribs']['']['ref'];
179 if($feed->get_item_quantity() == 1) {
180 // remote reply to our post. Import and then notify everybody else.
181 $datarray = get_atom_elements($item);
182 $datarray['type'] = 'remote-comment';
183 $datarray['parent-uri'] = $parent_uri;
184 $datarray['uid'] = $importer['importer_uid'];
185 $datarray['contact-id'] = $importer['id'];
186 $posted_id = post_remote($a,$datarray);
189 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
191 intval($importer['importer_uid'])
194 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
195 dbesc(datetime_convert()),
196 intval($importer['importer_uid']),
197 intval($r[0]['parent'])
200 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
201 dbesc(datetime_convert()),
202 intval($importer['importer_uid']),
206 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
208 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &",
211 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
212 require_once('bbcode.php');
213 $from = stripslashes($datarray['author-name']);
214 $tpl = file_get_contents('view/cmnt_received_eml.tpl');
215 $email_tpl = replace_macros($tpl, array(
216 '$sitename' => $a->config['sitename'],
217 '$siteurl' => $a->get_baseurl(),
218 '$username' => $importer['username'],
219 '$email' => $importer['email'],
221 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
224 $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
225 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
233 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
235 $item_id = $item->get_id();
237 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
239 intval($importer['importer_uid'])
241 // FIXME update content if 'updated' changes
243 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
244 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
245 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
246 intval($allow[0]['data']),
247 dbesc(datetime_convert()),
249 intval($importer['importer_uid'])
254 $datarray = get_atom_elements($item);
255 $datarray['parent-uri'] = $parent_uri;
256 $datarray['uid'] = $importer['importer_uid'];
257 $datarray['contact-id'] = $importer['id'];
258 $r = post_remote($a,$datarray);
260 // find out if our user is involved in this conversation and wants to be notified.
262 if($importer['notify-flags'] & NOTIFY_COMMENT) {
264 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s'",
268 foreach($myconv as $conv) {
269 if($conv['author-link'] != $importer['url'])
271 require_once('bbcode.php');
272 $from = stripslashes($datarray['author-name']);
273 $tpl = file_get_contents('view/cmnt_received_eml.tpl');
274 $email_tpl = replace_macros($tpl, array(
275 '$sitename' => $a->config['sitename'],
276 '$siteurl' => $a->get_baseurl(),
277 '$username' => $importer['username'],
278 '$email' => $importer['email'],
280 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
283 $res = mail($importer['email'], $from . t(" commented on an item at ")
284 . $a->config['sitename'],
285 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
294 // Head post of a conversation. Have we seen it? If not, import it.
296 $item_id = $item->get_id();
297 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
299 intval($importer['importer_uid'])
302 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
303 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
304 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
305 intval($allow[0]['data']),
306 dbesc(datetime_convert()),
308 intval($importer['importer_uid'])
315 $datarray = get_atom_elements($item);
316 $datarray['parent-uri'] = $item_id;
317 $datarray['uid'] = $importer['importer_uid'];
318 $datarray['contact-id'] = $importer['id'];
319 $r = post_remote($a,$datarray);
332 function dfrn_notify_content(&$a) {
334 if(x($_GET,'dfrn_id')) {
335 // initial communication from external contact
336 $hash = random_string();
340 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
342 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
343 VALUES( '%s', '%s', '%s') ",
345 dbesc(notags(trim($_GET['dfrn_id']))),
349 $r = q("SELECT * FROM `contact` WHERE ( `issued-id` = '%s' OR ( `duplex` = 1 AND `dfrn-id` = '%s'))
350 AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
351 dbesc($_GET['dfrn_id']),
352 dbesc($_GET['dfrn_id'])
359 $id_str = $_GET['dfrn_id'] . '.' . mt_rand(1000,9999);
361 if(($r[0]['duplex']) && strlen($r[0]['pubkey'])) {
362 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
363 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
366 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
367 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
370 $challenge = bin2hex($challenge);
371 $encrypted_id = bin2hex($encrypted_id);
373 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_version>2.0</dfrn_version><dfrn_id>' . $encrypted_id . '</dfrn_id>' . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
374 session_write_close();