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 * 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]['uri'][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.
75 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
76 intval($importer['uid'])
78 require_once('bbcode.php');
79 if((count($r)) && ($r[0]['notify_flags'] & NOTIFY_MAIL)) {
80 $tpl = file_get_contents('view/mail_received_eml.tpl');
81 $email_tpl = replace_macros($tpl, array(
82 '$sitename' => $a->config['sitename'],
83 '$siteurl' => $a->get_baseurl(),
84 '$username' => $r[0]['username'],
85 '$email' => $r[0]['email'],
86 '$from' => $msg['from-name'],
87 '$fn' => $r[0]['name'],
88 '$title' => $msg['title'],
89 '$body' => strip_tags(bbcode($msg['body']))
92 $res = mail($r[0]['email'], t("New mail received at ") . $a->config['sitename'],
93 $email_tpl,t("From: Administrator@") . $_SERVER[SERVER_NAME] );
95 notice( t("Email notification failed.") . EOL );
103 foreach($feed->get_items() as $item) {
107 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
108 if(isset($rawdelete[0]['attribs']['']['ref'])) {
109 $uri = $rawthread[0]['attribs']['']['ref'];
111 if(isset($rawdelete[0]['attribs']['']['when'])) {
112 $when = $rawthread[0]['attribs']['']['when'];
113 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
116 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
119 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
121 intval($importer['uid'])
124 if($r[0]['uri'] == $r[0]['parent-uri']) {
125 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
126 WHERE `parent-uri` = '%s'",
132 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
133 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
136 intval($importer['uid'])
144 $item_id = $item->get_id();
145 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
146 if(isset($rawthread[0]['attribs']['']['ref'])) {
148 $parent_uri = $rawthread[0]['attribs']['']['ref'];
153 if($feed->get_item_quantity() == 1) {
154 // remote reply to our post. Import and then notify everybody else.
155 $datarray = get_atom_elements($item);
156 $urn = explode(':',$parent_urn);
157 $datarray['type'] = 'remote-comment';
158 $datarray['parent-uri'] = $parent_uri;
159 $datarray['uid'] = $importer['uid'];
160 $datarray['contact-id'] = $importer['id'];
161 $posted_id = post_remote($a,$datarray);
163 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
165 intval($importer['uid'])
168 $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
169 intval($importer['uid']),
170 intval($r[0]['parent'])
173 $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
174 intval($importer['uid']),
178 $url = $a->get_baseurl();
180 proc_close(proc_open("php include/notifier.php $url comment-import $posted_id > remote-notify.log &", array(),$foo));
187 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
189 $item_id = $item->get_id();
191 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
193 intval($importer['uid'])
195 // FIXME update content if 'updated' changes
197 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
198 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
199 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
200 intval($allow[0]['data']),
202 intval($importer['uid'])
207 $datarray = get_atom_elements($item);
208 $datarray['parent-uri'] = $parent_uri;
209 $datarray['uid'] = $importer['uid'];
210 $datarray['contact-id'] = $importer['id'];
211 $r = post_remote($a,$datarray);
216 // Head post of a conversation. Have we seen it? If not, import it.
218 $item_id = $item->get_id();
219 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
221 intval($importer['uid'])
224 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
225 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
226 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
227 intval($allow[0]['data']),
229 intval($importer['uid'])
236 $datarray = get_atom_elements($item);
237 $datarray['parent-uri'] = $item_id;
238 $datarray['uid'] = $importer['uid'];
239 $datarray['contact-id'] = $importer['id'];
240 $r = post_remote($a,$datarray);
253 function dfrn_notify_content(&$a) {
255 if(x($_GET,'dfrn_id')) {
256 // initial communication from external contact
257 $hash = random_string();
261 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
263 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
264 VALUES( '%s', '%s', '%s') ",
266 dbesc(notags(trim($_GET['dfrn_id']))),
270 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
271 dbesc($_GET['dfrn_id']));
272 if((! count($r)) || (! strlen($r[0]['prvkey'])))
277 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
278 $challenge = bin2hex($challenge);
281 $id_str = $_GET['dfrn_id'] . '.' . mt_rand(1000,9999);
283 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
284 $encrypted_id = bin2hex($encrypted_id);
286 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $encrypted_id . '</dfrn_id>'
287 . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
288 session_write_close();