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'])));
73 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
74 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
76 // send email notification if requested.
77 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
78 intval($importer['uid'])
80 require_once('bbcode.php');
81 if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_MAIL)) {
82 $tpl = file_get_contents('view/mail_received_eml.tpl');
83 $email_tpl = replace_macros($tpl, array(
84 '$sitename' => $a->config['sitename'],
85 '$siteurl' => $a->get_baseurl(),
86 '$username' => $r[0]['username'],
87 '$email' => $r[0]['email'],
88 '$from' => $msg['from-name'],
89 '$fn' => $r[0]['name'],
90 '$title' => $msg['title'],
91 '$body' => strip_tags(bbcode($msg['body']))
94 $res = mail($r[0]['email'], t("New mail received at ") . $a->config['sitename'],
95 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
101 foreach($feed->get_items() as $item) {
105 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
106 if(isset($rawdelete[0]['attribs']['']['ref'])) {
107 $uri = $rawthread[0]['attribs']['']['ref'];
109 if(isset($rawdelete[0]['attribs']['']['when'])) {
110 $when = $rawthread[0]['attribs']['']['when'];
111 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
114 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
117 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
119 intval($importer['uid'])
122 if($r[0]['uri'] == $r[0]['parent-uri']) {
123 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
124 WHERE `parent-uri` = '%s'",
130 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
131 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
134 intval($importer['uid'])
142 $item_id = $item->get_id();
143 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
144 if(isset($rawthread[0]['attribs']['']['ref'])) {
146 $parent_uri = $rawthread[0]['attribs']['']['ref'];
151 if($feed->get_item_quantity() == 1) {
152 // remote reply to our post. Import and then notify everybody else.
153 $datarray = get_atom_elements($item);
154 $urn = explode(':',$parent_urn);
155 $datarray['type'] = 'remote-comment';
156 $datarray['parent-uri'] = $parent_uri;
157 $datarray['uid'] = $importer['uid'];
158 $datarray['contact-id'] = $importer['id'];
159 $posted_id = post_remote($a,$datarray);
161 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
163 intval($importer['uid'])
166 $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
167 intval($importer['uid']),
168 intval($r[0]['parent'])
171 $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
172 intval($importer['uid']),
176 $url = $a->get_baseurl();
178 proc_close(proc_open("php include/notifier.php $url comment-import $posted_id > remote-notify.log &", array(),$foo));
185 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
187 $item_id = $item->get_id();
189 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
191 intval($importer['uid'])
193 // FIXME update content if 'updated' changes
195 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
196 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
197 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
198 intval($allow[0]['data']),
200 intval($importer['uid'])
205 $datarray = get_atom_elements($item);
206 $datarray['parent-uri'] = $parent_uri;
207 $datarray['uid'] = $importer['uid'];
208 $datarray['contact-id'] = $importer['id'];
209 $r = post_remote($a,$datarray);
214 // Head post of a conversation. Have we seen it? If not, import it.
216 $item_id = $item->get_id();
217 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
219 intval($importer['uid'])
222 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
223 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
224 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
225 intval($allow[0]['data']),
227 intval($importer['uid'])
234 $datarray = get_atom_elements($item);
235 $datarray['parent-uri'] = $item_id;
236 $datarray['uid'] = $importer['uid'];
237 $datarray['contact-id'] = $importer['id'];
238 $r = post_remote($a,$datarray);
251 function dfrn_notify_content(&$a) {
253 if(x($_GET,'dfrn_id')) {
254 // initial communication from external contact
255 $hash = random_string();
259 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
261 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
262 VALUES( '%s', '%s', '%s') ",
264 dbesc(notags(trim($_GET['dfrn_id']))),
268 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
269 dbesc($_GET['dfrn_id']));
270 if((! count($r)) || (! strlen($r[0]['prvkey'])))
275 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
276 $challenge = bin2hex($challenge);
279 $id_str = $_GET['dfrn_id'] . '.' . mt_rand(1000,9999);
281 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
282 $encrypted_id = bin2hex($encrypted_id);
284 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $encrypted_id . '</dfrn_id>'
285 . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
286 session_write_close();