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` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` WHERE `issued-id` = '%s' LIMIT 1",
37 $feed = new SimplePie();
38 $feed->set_raw_data($data);
39 $feed->enable_order_by_date(false);
44 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
45 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
47 if($importer['readonly']) {
48 // We aren't receiving email from this person. But we will quietly ignore them
49 // rather than a blatant "go away" message.
55 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
58 $msg['uid'] = $importer['importer_uid'];
59 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
60 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
61 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
62 $msg['contact-id'] = $importer['id'];
63 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
64 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
65 $msg['delivered'] = 1;
68 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
69 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
70 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
74 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
75 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
77 // send email notification if requested.
79 require_once('bbcode.php');
80 if($importer['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' => $importer['username'],
86 '$email' => $importer['email'],
87 '$from' => $msg['from-name'],
88 '$title' => $msg['title'],
89 '$body' => strip_tags(bbcode($msg['body']))
92 $res = mail($importer['email'], t("New mail received at ") . $a->config['sitename'],
93 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
99 if($importer['readonly']) {
102 return; // NOTREACHED
105 foreach($feed->get_items() as $item) {
109 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
110 if(isset($rawdelete[0]['attribs']['']['ref'])) {
111 $uri = $rawthread[0]['attribs']['']['ref'];
113 if(isset($rawdelete[0]['attribs']['']['when'])) {
114 $when = $rawthread[0]['attribs']['']['when'];
115 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
118 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
121 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
123 intval($importer['importer_uid'])
126 if($r[0]['uri'] == $r[0]['parent-uri']) {
127 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
128 WHERE `parent-uri` = '%s'",
134 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
135 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
138 intval($importer['importer_uid'])
146 $item_id = $item->get_id();
147 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
148 if(isset($rawthread[0]['attribs']['']['ref'])) {
150 $parent_uri = $rawthread[0]['attribs']['']['ref'];
155 if($feed->get_item_quantity() == 1) {
156 // remote reply to our post. Import and then notify everybody else.
157 $datarray = get_atom_elements($item);
158 $datarray['type'] = 'remote-comment';
159 $datarray['parent-uri'] = $parent_uri;
160 $datarray['uid'] = $importer['importer_uid'];
161 $datarray['contact-id'] = $importer['id'];
162 $posted_id = post_remote($a,$datarray);
164 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
166 intval($importer['importer_uid'])
169 $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
170 intval($importer['importer_uid']),
171 intval($r[0]['parent'])
174 $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
175 intval($importer['importer_uid']),
179 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
181 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &",
184 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
185 require_once('bbcode.php');
186 $from = stripslashes($datarray['author-name']);
187 $tpl = file_get_contents('view/cmnt_received_eml.tpl');
188 $email_tpl = replace_macros($tpl, array(
189 '$sitename' => $a->config['sitename'],
190 '$siteurl' => $a->get_baseurl(),
191 '$username' => $importer['username'],
192 '$email' => $importer['email'],
194 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
197 $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
198 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
205 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
207 $item_id = $item->get_id();
209 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
211 intval($importer['importer_uid'])
213 // FIXME update content if 'updated' changes
215 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
216 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
217 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
218 intval($allow[0]['data']),
220 intval($importer['importer_uid'])
225 $datarray = get_atom_elements($item);
226 $datarray['parent-uri'] = $parent_uri;
227 $datarray['uid'] = $importer['importer_uid'];
228 $datarray['contact-id'] = $importer['id'];
229 $r = post_remote($a,$datarray);
231 // find out if our user is involved in this conversation and wants to be notified.
233 if($importer['notify-flags'] & NOTIFY_COMMENT) {
235 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s'",
239 foreach($myconv as $conv) {
240 if($conv['author-link'] != $importer['url'])
242 require_once('bbcode.php');
243 $from = stripslashes($datarray['author-name']);
244 $tpl = file_get_contents('view/cmnt_received_eml.tpl');
245 $email_tpl = replace_macros($tpl, array(
246 '$sitename' => $a->config['sitename'],
247 '$siteurl' => $a->get_baseurl(),
248 '$username' => $importer['username'],
249 '$email' => $importer['email'],
251 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
254 $res = mail($importer['email'], $from . t(" commented on an item at ")
255 . $a->config['sitename'],
256 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
265 // Head post of a conversation. Have we seen it? If not, import it.
267 $item_id = $item->get_id();
268 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
270 intval($importer['importer_uid'])
273 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
274 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
275 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
276 intval($allow[0]['data']),
278 intval($importer['importer_uid'])
285 $datarray = get_atom_elements($item);
286 $datarray['parent-uri'] = $item_id;
287 $datarray['uid'] = $importer['importer_uid'];
288 $datarray['contact-id'] = $importer['id'];
289 $r = post_remote($a,$datarray);
302 function dfrn_notify_content(&$a) {
304 if(x($_GET,'dfrn_id')) {
305 // initial communication from external contact
306 $hash = random_string();
310 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
312 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
313 VALUES( '%s', '%s', '%s') ",
315 dbesc(notags(trim($_GET['dfrn_id']))),
319 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
320 dbesc($_GET['dfrn_id']));
321 if((! count($r)) || (! strlen($r[0]['prvkey'])))
326 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
327 $challenge = bin2hex($challenge);
330 $id_str = $_GET['dfrn_id'] . '.' . mt_rand(1000,9999);
332 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
333 $encrypted_id = bin2hex($encrypted_id);
335 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $encrypted_id . '</dfrn_id>' . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
336 session_write_close();