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'];
14 if(strpos($dfrn_id,':') == 1) {
15 $direction = intval(substr($dfrn_id,0,1));
16 $dfrn_id = substr($dfrn_id,2);
19 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
26 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
31 // find the local user who owns this relationship.
36 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
39 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
42 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
50 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, `user`.* FROM `contact`
51 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
52 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 $sql_extra LIMIT 1"
62 $feed = new SimplePie();
63 $feed->set_raw_data($data);
64 $feed->enable_order_by_date(false);
69 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
70 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
72 if($importer['readonly']) {
73 // We aren't receiving email from this person. But we will quietly ignore them
74 // rather than a blatant "go away" message.
80 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
83 $msg['uid'] = $importer['importer_uid'];
84 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
85 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
86 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
87 $msg['contact-id'] = $importer['id'];
88 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
89 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
90 $msg['delivered'] = 1;
93 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
94 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
95 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
99 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
100 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
102 // send email notification if requested.
104 require_once('bbcode.php');
105 if($importer['notify-flags'] & NOTIFY_MAIL) {
106 $tpl = file_get_contents('view/mail_received_eml.tpl');
107 $email_tpl = replace_macros($tpl, array(
108 '$sitename' => $a->config['sitename'],
109 '$siteurl' => $a->get_baseurl(),
110 '$username' => $importer['username'],
111 '$email' => $importer['email'],
112 '$from' => $msg['from-name'],
113 '$title' => $msg['title'],
114 '$body' => strip_tags(bbcode($msg['body']))
117 $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
118 $email_tpl, 'From: ' . t('Administrator') . '@' . $a->get_hostname() );
121 return; // NOTREACHED
124 if($importer['readonly']) {
127 return; // NOTREACHED
130 foreach($feed->get_items() as $item) {
134 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB , 'deleted-entry');
135 if(isset($rawdelete[0]['attribs']['']['ref'])) {
136 $uri = $rawthread[0]['attribs']['']['ref'];
138 if(isset($rawdelete[0]['attribs']['']['when'])) {
139 $when = $rawthread[0]['attribs']['']['when'];
140 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
143 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
146 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
148 intval($importer['importer_uid'])
152 if($item['uri'] == $item['parent-uri']) {
153 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
154 WHERE `parent-uri` = '%s' AND `uid` = %d",
156 dbesc(datetime_convert()),
158 intval($importer['importer_uid'])
162 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
163 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
165 dbesc(datetime_convert()),
167 intval($importer['importer_uid'])
169 if($item['last-child']) {
170 // ensure that last-child is set in case the comment that had it just got wiped.
171 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
172 dbesc(datetime_convert()),
173 dbesc($item['parent-uri']),
176 // who is the last child now?
177 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
178 ORDER BY `edited` DESC LIMIT 1",
179 dbesc($item['parent-uri']),
180 intval($importer['importer_uid'])
183 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
194 $item_id = $item->get_id();
195 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
196 if(isset($rawthread[0]['attribs']['']['ref'])) {
198 $parent_uri = $rawthread[0]['attribs']['']['ref'];
203 if($feed->get_item_quantity() == 1) {
204 // remote reply to our post. Import and then notify everybody else.
205 $datarray = get_atom_elements($item);
206 $datarray['type'] = 'remote-comment';
207 $datarray['wall'] = 1;
208 $datarray['parent-uri'] = $parent_uri;
209 $datarray['uid'] = $importer['importer_uid'];
210 $datarray['contact-id'] = $importer['id'];
211 $posted_id = post_remote($a,$datarray);
214 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
216 intval($importer['importer_uid'])
219 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
220 dbesc(datetime_convert()),
221 intval($importer['importer_uid']),
222 intval($r[0]['parent'])
225 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
226 dbesc(datetime_convert()),
227 intval($importer['importer_uid']),
231 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
233 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &",
236 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
237 require_once('bbcode.php');
238 $from = stripslashes($datarray['author-name']);
239 $tpl = file_get_contents('view/cmnt_received_eml.tpl');
240 $email_tpl = replace_macros($tpl, array(
241 '$sitename' => $a->config['sitename'],
242 '$siteurl' => $a->get_baseurl(),
243 '$username' => $importer['username'],
244 '$email' => $importer['email'],
246 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
249 $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
250 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
258 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
260 $item_id = $item->get_id();
262 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
264 intval($importer['importer_uid'])
266 // FIXME update content if 'updated' changes
268 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
269 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
270 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
271 intval($allow[0]['data']),
272 dbesc(datetime_convert()),
274 intval($importer['importer_uid'])
279 $datarray = get_atom_elements($item);
280 $datarray['parent-uri'] = $parent_uri;
281 $datarray['uid'] = $importer['importer_uid'];
282 $datarray['contact-id'] = $importer['id'];
283 $r = post_remote($a,$datarray);
285 // find out if our user is involved in this conversation and wants to be notified.
287 if($importer['notify-flags'] & NOTIFY_COMMENT) {
289 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
291 intval($importer['importer_uid'])
294 foreach($myconv as $conv) {
295 if($conv['author-link'] != $importer['url'])
297 require_once('bbcode.php');
298 $from = stripslashes($datarray['author-name']);
299 $tpl = file_get_contents('view/cmnt_received_eml.tpl');
300 $email_tpl = replace_macros($tpl, array(
301 '$sitename' => $a->config['sitename'],
302 '$siteurl' => $a->get_baseurl(),
303 '$username' => $importer['username'],
304 '$email' => $importer['email'],
306 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
309 $res = mail($importer['email'], $from . t(" commented on an item at ")
310 . $a->config['sitename'],
311 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
320 // Head post of a conversation. Have we seen it? If not, import it.
322 $item_id = $item->get_id();
323 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
325 intval($importer['importer_uid'])
328 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
329 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
330 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
331 intval($allow[0]['data']),
332 dbesc(datetime_convert()),
334 intval($importer['importer_uid'])
341 $datarray = get_atom_elements($item);
342 $datarray['parent-uri'] = $item_id;
343 $datarray['uid'] = $importer['importer_uid'];
344 $datarray['contact-id'] = $importer['id'];
345 $r = post_remote($a,$datarray);
358 function dfrn_notify_content(&$a) {
360 if(x($_GET,'dfrn_id')) {
362 // initial communication from external contact, $direction is their direction.
363 // If this is a duplex communication, ours will be the opposite.
365 $dfrn_id = notags(trim($_GET['dfrn_id']));
368 if(strpos($dfrn_id,':') == 1) {
369 $direction = intval(substr($dfrn_id,0,1));
370 $dfrn_id = substr($dfrn_id,2);
373 $hash = random_string();
377 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
379 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
380 VALUES( '%s', '%s', '%s') ",
390 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
394 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
395 $my_id = '1:' . $dfrn_id;
398 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
399 $my_id = '0:' . $dfrn_id;
408 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
415 $id_str = $my_id . '.' . mt_rand(1000,9999);
417 if(($r[0]['duplex']) && strlen($r[0]['pubkey'])) {
418 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
419 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
422 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
423 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
426 $challenge = bin2hex($challenge);
427 $encrypted_id = bin2hex($encrypted_id);
429 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_version>2.0</dfrn_version><dfrn_id>' . $encrypted_id . '</dfrn_id>' . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
430 session_write_close();