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 = load_view_file('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 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
212 $datarray['type'] = 'activity';
213 $datarray['gravity'] = GRAVITY_LIKE;
215 $posted_id = item_store($datarray);
218 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
220 intval($importer['importer_uid'])
223 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
224 dbesc(datetime_convert()),
225 intval($importer['importer_uid']),
226 intval($r[0]['parent'])
229 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
230 dbesc(datetime_convert()),
231 intval($importer['importer_uid']),
235 if($datarray['type'] == 'remote-comment') {
236 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
238 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &",
241 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
242 require_once('bbcode.php');
243 $from = stripslashes($datarray['author-name']);
244 $tpl = load_view_file('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 your item at ") . $a->config['sitename'],
255 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
264 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
266 $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'])
272 // FIXME update content if 'updated' changes
274 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
275 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
276 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
277 intval($allow[0]['data']),
278 dbesc(datetime_convert()),
280 intval($importer['importer_uid'])
285 $datarray = get_atom_elements($item);
286 $datarray['parent-uri'] = $parent_uri;
287 $datarray['uid'] = $importer['importer_uid'];
288 $datarray['contact-id'] = $importer['id'];
289 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
290 $datarray['type'] = 'activity';
291 $datarray['gravity'] = GRAVITY_LIKE;
294 $r = item_store($datarray);
296 // find out if our user is involved in this conversation and wants to be notified.
298 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
300 $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
302 intval($importer['importer_uid'])
305 foreach($myconv as $conv) {
306 if($conv['author-link'] != $importer['url'])
308 require_once('bbcode.php');
309 $from = stripslashes($datarray['author-name']);
310 $tpl = load_view_file('view/cmnt_received_eml.tpl');
311 $email_tpl = replace_macros($tpl, array(
312 '$sitename' => $a->config['sitename'],
313 '$siteurl' => $a->get_baseurl(),
314 '$username' => $importer['username'],
315 '$email' => $importer['email'],
317 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
320 $res = mail($importer['email'], $from . t(" commented on an item at ")
321 . $a->config['sitename'],
322 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
331 // Head post of a conversation. Have we seen it? If not, import it.
333 $item_id = $item->get_id();
334 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
336 intval($importer['importer_uid'])
339 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
340 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
341 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
342 intval($allow[0]['data']),
343 dbesc(datetime_convert()),
345 intval($importer['importer_uid'])
352 $datarray = get_atom_elements($item);
353 $datarray['parent-uri'] = $item_id;
354 $datarray['uid'] = $importer['importer_uid'];
355 $datarray['contact-id'] = $importer['id'];
356 $r = item_store($datarray);
369 function dfrn_notify_content(&$a) {
371 if(x($_GET,'dfrn_id')) {
373 // initial communication from external contact, $direction is their direction.
374 // If this is a duplex communication, ours will be the opposite.
376 $dfrn_id = notags(trim($_GET['dfrn_id']));
379 if(strpos($dfrn_id,':') == 1) {
380 $direction = intval(substr($dfrn_id,0,1));
381 $dfrn_id = substr($dfrn_id,2);
384 $hash = random_string();
388 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
390 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
391 VALUES( '%s', '%s', '%s') ",
401 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
405 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
406 $my_id = '1:' . $dfrn_id;
409 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
410 $my_id = '0:' . $dfrn_id;
419 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
426 $id_str = $my_id . '.' . mt_rand(1000,9999);
428 if(($r[0]['duplex']) && strlen($r[0]['pubkey'])) {
429 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
430 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
433 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
434 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
437 $challenge = bin2hex($challenge);
438 $encrypted_id = bin2hex($encrypted_id);
440 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" ;
441 session_write_close();