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 `id`, `uid` FROM `contact` WHERE `issued-id` = '%s' LIMIT 1",
34 // We aren't really interested in anything this person has to say. But be polite and make them
35 // think we're listening intently by acknowledging receipt of their communications - which we quietly ignore.
37 if($r[0]['readonly']) {
44 $feed = new SimplePie();
45 $feed->set_raw_data($data);
46 $feed->enable_order_by_date(false);
51 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
52 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
54 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
57 $msg['uid'] = $importer['uid'];
58 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
59 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
60 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
61 $msg['contact-id'] = $importer['id'];
62 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
63 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
64 $msg['delivered'] = 1;
67 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
68 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
69 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
71 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
72 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
74 // send email notification if requested.
80 foreach($feed->get_items() as $item) {
84 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
85 if(isset($rawdelete[0]['attribs']['']['ref'])) {
86 $uri = $rawthread[0]['attribs']['']['ref'];
88 if(isset($rawdelete[0]['attribs']['']['when'])) {
89 $when = $rawthread[0]['attribs']['']['when'];
90 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
93 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
96 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
98 intval($importer['uid'])
101 if($r[0]['uri'] == $r[0]['parent-uri']) {
102 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
103 WHERE `parent-uri` = '%s'",
109 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
110 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
113 intval($importer['uid'])
121 $item_id = $item->get_id();
122 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
123 if(isset($rawthread[0]['attribs']['']['ref'])) {
125 $parent_uri = $rawthread[0]['attribs']['']['ref'];
130 if($feed->get_item_quantity() == 1) {
131 // remote reply to our post. Import and then notify everybody else.
132 $datarray = get_atom_elements($item);
133 $urn = explode(':',$parent_urn);
134 $datarray['type'] = 'remote-comment';
135 $datarray['parent-uri'] = $parent_uri;
136 $datarray['uid'] = $importer['uid'];
137 $datarray['contact-id'] = $importer['id'];
138 $posted_id = post_remote($a,$datarray);
140 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
142 intval($importer['uid'])
145 $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
146 intval($importer['uid']),
147 intval($r[0]['parent'])
150 $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
151 intval($importer['uid']),
155 $url = $a->get_baseurl();
157 proc_close(proc_open("php include/notifier.php $url comment-import $posted_id > remote-notify.log &", array(),$foo));
164 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
166 $item_id = $item->get_id();
168 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
170 intval($importer['uid'])
172 // FIXME update content if 'updated' changes
174 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
175 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
176 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
177 intval($allow[0]['data']),
179 intval($importer['uid'])
184 $datarray = get_atom_elements($item);
185 $datarray['parent-uri'] = $parent_uri;
186 $datarray['uid'] = $importer['uid'];
187 $datarray['contact-id'] = $importer['id'];
188 $r = post_remote($a,$datarray);
193 // Head post of a conversation. Have we seen it? If not, import it.
195 $item_id = $item->get_id();
196 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
198 intval($importer['uid'])
201 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
202 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
203 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
204 intval($allow[0]['data']),
206 intval($importer['uid'])
213 $datarray = get_atom_elements($item);
214 $datarray['parent-uri'] = $item_id;
215 $datarray['uid'] = $importer['uid'];
216 $datarray['contact-id'] = $importer['id'];
217 $r = post_remote($a,$datarray);
230 function dfrn_notify_content(&$a) {
232 if(x($_GET,'dfrn_id')) {
233 // initial communication from external contact
234 $hash = random_string();
238 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
240 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
241 VALUES( '%s', '%s', '%s') ",
243 dbesc(notags(trim($_GET['dfrn_id']))),
247 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
248 dbesc($_GET['dfrn_id']));
249 if((! count($r)) || (! strlen($r[0]['prvkey'])))
254 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
255 $challenge = bin2hex($challenge);
256 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $_GET['dfrn_id'] . '</dfrn_id>'
257 . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
258 session_write_close();