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'])));
72 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
73 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
75 // send email notification if requested.
76 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
77 intval($importer['uid'])
79 require_once('bbcode.php');
80 if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_MAIL)) {
81 $tpl = file_get_contents('view/mail_received_eml.tpl');
82 $email_tpl = replace_macros($tpl, array(
83 '$sitename' => $a->config['sitename'],
84 '$siteurl' => $a->get_baseurl(),
85 '$username' => $r[0]['username'],
86 '$email' => $r[0]['email'],
87 '$from' => $msg['from-name'],
88 '$fn' => $r[0]['name'],
89 '$title' => $msg['title'],
90 '$body' => strip_tags(bbcode($msg['body']))
93 $res = mail($r[0]['email'], t("New mail received at ") . $a->config['sitename'],
94 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
100 foreach($feed->get_items() as $item) {
104 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
105 if(isset($rawdelete[0]['attribs']['']['ref'])) {
106 $uri = $rawthread[0]['attribs']['']['ref'];
108 if(isset($rawdelete[0]['attribs']['']['when'])) {
109 $when = $rawthread[0]['attribs']['']['when'];
110 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
113 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
116 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
118 intval($importer['uid'])
121 if($r[0]['uri'] == $r[0]['parent-uri']) {
122 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
123 WHERE `parent-uri` = '%s'",
129 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
130 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
133 intval($importer['uid'])
141 $item_id = $item->get_id();
142 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
143 if(isset($rawthread[0]['attribs']['']['ref'])) {
145 $parent_uri = $rawthread[0]['attribs']['']['ref'];
150 if($feed->get_item_quantity() == 1) {
151 // remote reply to our post. Import and then notify everybody else.
152 $datarray = get_atom_elements($item);
153 $urn = explode(':',$parent_urn);
154 $datarray['type'] = 'remote-comment';
155 $datarray['parent-uri'] = $parent_uri;
156 $datarray['uid'] = $importer['uid'];
157 $datarray['contact-id'] = $importer['id'];
158 $posted_id = post_remote($a,$datarray);
160 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
162 intval($importer['uid'])
165 $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
166 intval($importer['uid']),
167 intval($r[0]['parent'])
170 $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
171 intval($importer['uid']),
175 $url = $a->get_baseurl();
177 proc_close(proc_open("php include/notifier.php $url comment-import $posted_id > remote-notify.log &", array(),$foo));
184 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
186 $item_id = $item->get_id();
188 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
190 intval($importer['uid'])
192 // FIXME update content if 'updated' changes
194 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
195 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
196 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
197 intval($allow[0]['data']),
199 intval($importer['uid'])
204 $datarray = get_atom_elements($item);
205 $datarray['parent-uri'] = $parent_uri;
206 $datarray['uid'] = $importer['uid'];
207 $datarray['contact-id'] = $importer['id'];
208 $r = post_remote($a,$datarray);
213 // Head post of a conversation. Have we seen it? If not, import it.
215 $item_id = $item->get_id();
216 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
218 intval($importer['uid'])
221 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
222 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
223 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
224 intval($allow[0]['data']),
226 intval($importer['uid'])
233 $datarray = get_atom_elements($item);
234 $datarray['parent-uri'] = $item_id;
235 $datarray['uid'] = $importer['uid'];
236 $datarray['contact-id'] = $importer['id'];
237 $r = post_remote($a,$datarray);
250 function dfrn_notify_content(&$a) {
252 if(x($_GET,'dfrn_id')) {
253 // initial communication from external contact
254 $hash = random_string();
258 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
260 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
261 VALUES( '%s', '%s', '%s') ",
263 dbesc(notags(trim($_GET['dfrn_id']))),
267 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
268 dbesc($_GET['dfrn_id']));
269 if((! count($r)) || (! strlen($r[0]['prvkey'])))
274 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
275 $challenge = bin2hex($challenge);
278 $id_str = $_GET['dfrn_id'] . '.' . mt_rand(1000,9999);
280 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
281 $encrypted_id = bin2hex($encrypted_id);
283 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $encrypted_id . '</dfrn_id>'
284 . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
285 session_write_close();