]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
80cd1a2fdcdeb0e201568eef1d4251f40914f668
[friendica.git] / mod / dfrn_notify.php
1 <?php
2
3 require_once('simplepie/simplepie.inc');
4 require_once('include/items.php');
5
6
7 function dfrn_notify_post(&$a) {
8
9         $dfrn_id = notags(trim($_POST['dfrn_id']));
10         $challenge = notags(trim($_POST['challenge']));
11         $data = $_POST['data'];
12
13         $direction = (-1);
14         if(strpos($dfrn_id,':') == 1) {
15                 $direction = intval(substr($dfrn_id,0,1));
16                 $dfrn_id = substr($dfrn_id,2);
17         }
18
19         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
20                 dbesc($dfrn_id),
21                 dbesc($challenge)
22         );
23         if(! count($r))
24                 xml_status(3);
25
26         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
27                 dbesc($dfrn_id),
28                 dbesc($challenge)
29         );
30
31         // find the local user who owns this relationship.
32
33         $sql_extra = '';
34         switch($direction) {
35                 case (-1):
36                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
37                         break;
38                 case 0:
39                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
40                         break;
41                 case 1:
42                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
43                         break;
44                 default:
45                         xml_status(3);
46                         break; // NOTREACHED
47         }
48                  
49
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"
53         );
54
55         if(! count($r)) {
56                 xml_status(3);
57                 return; //NOTREACHED
58         }
59
60         $importer = $r[0];
61
62         $feed = new SimplePie();
63         $feed->set_raw_data($data);
64         $feed->enable_order_by_date(false);
65         $feed->init();
66
67         $ismail = false;
68
69         $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
70         if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
71
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.
75                         xml_status(0);
76                         return; //NOTREACHED
77                 }
78
79                 $ismail = true;
80                 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
81
82                 $msg = array();
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;
91                 $msg['seen'] = 0;
92                 $msg['replied'] = 0;
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'])));
96                 
97                 dbesc_array($msg);
98
99                 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
100                         . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
101
102                 // send email notification if requested.
103
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']))
115                         ));
116
117                         $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
118                                 $email_tpl, 'From: ' . t('Administrator') . '@' . $a->get_hostname() );
119                 }
120                 xml_status(0);
121                 return; // NOTREACHED
122         }       
123
124         if($importer['readonly']) {
125
126                 xml_status(0);
127                 return; // NOTREACHED
128         }
129
130         foreach($feed->get_items() as $item) {
131
132                 $deleted = false;
133
134                 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB , 'deleted-entry');
135                 if(isset($rawdelete[0]['attribs']['']['ref'])) {
136                         $uri = $rawthread[0]['attribs']['']['ref'];
137                         $deleted = true;
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');
141                         }
142                         else
143                                 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
144                 }
145                 if($deleted) {
146                         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
147                                 dbesc($uri),
148                                 intval($importer['importer_uid'])
149                         );
150                         if(count($r)) {
151                                 $item = $r[0];
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",
155                                                 dbesc($when),
156                                                 dbesc(datetime_convert()),
157                                                 dbesc($item['uri']),
158                                                 intval($importer['importer_uid'])
159                                         );
160                                 }
161                                 else {
162                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' 
163                                                 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
164                                                 dbesc($when),
165                                                 dbesc(datetime_convert()),
166                                                 dbesc($uri),
167                                                 intval($importer['importer_uid'])
168                                         );
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']),
174                                                         intval($item['uid'])
175                                                 );
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'])
181                                                 );
182                                                 if(count($r)) {
183                                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
184                                                                 intval($r[0]['id'])
185                                                         );
186                                                 }       
187                                         }
188                                 }
189                         }       
190                         continue;
191                 }
192
193                 $is_reply = false;              
194                 $item_id = $item->get_id();
195                 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
196                 if(isset($rawthread[0]['attribs']['']['ref'])) {
197                         $is_reply = true;
198                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
199                 }
200
201
202                 if($is_reply) {
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 = item_store($datarray);
212
213                                 if($posted_id) {
214                                         $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
215                                                 intval($posted_id),
216                                                 intval($importer['importer_uid'])
217                                         );
218                                         if(count($r)) {
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'])
223                                                 );
224                                         }
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']),
228                                                         intval($posted_id)
229                                         );
230
231                                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
232
233                                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &", 
234                                                 array(),$foo));
235
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'],
245                                                         '$from' => $from,
246                                                 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
247                                                 ));
248
249                                                 $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
250                                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
251                                         }
252                                 }
253                                 xml_status(0);
254                                 return;
255
256                         }
257                         else {
258                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
259
260                                 $item_id = $item->get_id();
261
262                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
263                                         dbesc($item_id),
264                                         intval($importer['importer_uid'])
265                                 );
266                                 // FIXME update content if 'updated' changes
267                                 if(count($r)) {
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()),
273                                                         dbesc($item_id),
274                                                         intval($importer['importer_uid'])
275                                                 );
276                                         }
277                                         continue;
278                                 }
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 = item_store($datarray);
284
285                                 // find out if our user is involved in this conversation and wants to be notified.
286                         
287                                 if($importer['notify-flags'] & NOTIFY_COMMENT) {
288
289                                         $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
290                                                 dbesc($parent_uri),
291                                                 intval($importer['importer_uid'])
292                                         );
293                                         if(count($myconv)) {
294                                                 foreach($myconv as $conv) {
295                                                         if($conv['author-link'] != $importer['url'])
296                                                                 continue;
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'],
305                                                                 '$from' => $from,
306                                                                 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
307                                                         ));
308
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() );
312                                                         break;
313                                                 }
314                                         }
315                                 }
316                                 continue;
317                         }
318                 }
319                 else {
320                         // Head post of a conversation. Have we seen it? If not, import it.
321
322                         $item_id = $item->get_id();
323                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
324                                 dbesc($item_id),
325                                 intval($importer['importer_uid'])
326                         );
327                         if(count($r)) {
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()),
333                                                 dbesc($item_id),
334                                                 intval($importer['importer_uid'])
335                                         );
336                                 }
337                                 continue;
338                         }
339
340
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 = item_store($datarray);
346                         continue;
347
348                 }
349         
350         }
351
352         xml_status(0);
353         killme();
354
355 }
356
357
358 function dfrn_notify_content(&$a) {
359
360         if(x($_GET,'dfrn_id')) {
361
362                 // initial communication from external contact, $direction is their direction.
363                 // If this is a duplex communication, ours will be the opposite.
364
365                 $dfrn_id = notags(trim($_GET['dfrn_id']));
366
367                 $direction = (-1);
368                 if(strpos($dfrn_id,':') == 1) {
369                         $direction = intval(substr($dfrn_id,0,1));
370                         $dfrn_id = substr($dfrn_id,2);
371                 }
372
373                 $hash = random_string();
374
375                 $status = 0;
376
377                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
378
379                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
380                         VALUES( '%s', '%s', '%s') ",
381                         dbesc($hash),
382                         dbesc($dfrn_id),
383                         intval(time() + 60 )
384                 );
385
386
387                 $sql_extra = '';
388                 switch($direction) {
389                         case (-1):
390                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
391                                 $my_id = $dfrn_id;
392                                 break;
393                         case 0:
394                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
395                                 $my_id = '1:' . $dfrn_id;
396                                 break;
397                         case 1:
398                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
399                                 $my_id = '0:' . $dfrn_id;
400                                 break;
401                         default:
402                                 $status = 1;
403                                 break; // NOTREACHED
404                 }
405
406
407
408                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
409
410                 if(! count($r))
411                         $status = 1;
412
413                 $challenge = '';
414                 $encrypted_id = '';
415                 $id_str = $my_id . '.' . mt_rand(1000,9999);
416
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']);
420                 }
421                 else {
422                         openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
423                         openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
424                 }
425
426                 $challenge    = bin2hex($challenge);
427                 $encrypted_id = bin2hex($encrypted_id);
428
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();
431                 exit;
432                 
433         }
434
435 }