]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
752088a5e06fa552301a633510762232eb8719c7
[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         $dfrn_version = (float) $_POST['dfrn_version'];
11         $challenge = notags(trim($_POST['challenge']));
12         $data = $_POST['data'];
13
14         $direction = (-1);
15         if(strpos($dfrn_id,':') == 1) {
16                 $direction = intval(substr($dfrn_id,0,1));
17                 $dfrn_id = substr($dfrn_id,2);
18         }
19
20         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
21                 dbesc($dfrn_id),
22                 dbesc($challenge)
23         );
24         if(! count($r))
25                 xml_status(3);
26
27         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
28                 dbesc($dfrn_id),
29                 dbesc($challenge)
30         );
31
32         // find the local user who owns this relationship.
33
34         $sql_extra = '';
35         switch($direction) {
36                 case (-1):
37                         $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
38                         break;
39                 case 0:
40                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
41                         break;
42                 case 1:
43                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
44                         break;
45                 default:
46                         xml_status(3);
47                         break; // NOTREACHED
48         }
49                  
50
51         $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, `user`.* FROM `contact` 
52                 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
53                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
54                 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
55                 dbesc($a->argv[1])
56         );
57
58         if(! count($r)) {
59                 xml_status(3);
60                 //NOTREACHED
61         }
62
63         $importer = $r[0];
64
65
66         if($importer['readonly']) {
67                 // We aren't receiving stuff from this person. But we will quietly ignore them
68                 // rather than a blatant "go away" message.
69                 xml_status(0);
70                 //NOTREACHED
71         }
72
73         // Consume notification feed. This may differ from consuming a public feed in several ways
74         // - might contain email
75         // - might contain remote followup to our message
76         //              - in which case we need to accept it and then notify other conversants
77         // - we may need to send various email notifications
78
79         $feed = new SimplePie();
80         $feed->set_raw_data($data);
81         $feed->enable_order_by_date(false);
82         $feed->init();
83
84         $ismail = false;
85
86         $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
87         if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
88
89                 $ismail = true;
90                 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
91
92                 $msg = array();
93                 $msg['uid'] = $importer['importer_uid'];
94                 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
95                 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
96                 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
97                 $msg['contact-id'] = $importer['id'];
98                 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
99                 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
100                 $msg['delivered'] = 1;
101                 $msg['seen'] = 0;
102                 $msg['replied'] = 0;
103                 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
104                 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
105                 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
106                 
107                 dbesc_array($msg);
108
109                 $r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
110                         . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
111
112                 // send email notification if requested.
113
114                 require_once('bbcode.php');
115                 if($importer['notify-flags'] & NOTIFY_MAIL) {
116
117                         $body = html_entity_decode(strip_tags(bbcode(stripslashes($msg['body']))),ENT_QUOTES,'UTF-8');
118
119                         if(function_exists('quoted_printable_encode'))
120                                 $body = quoted_printable_encode($body);
121                         else
122                                 $body = qp($body);
123
124                         $tpl = load_view_file('view/mail_received_eml.tpl');                    
125                         $email_tpl = replace_macros($tpl, array(
126                                 '$sitename' => $a->config['sitename'],
127                                 '$siteurl' =>  $a->get_baseurl(),
128                                 '$username' => $importer['username'],
129                                 '$email' => $importer['email'],
130                                 '$from' => $msg['from-name'],
131                                 '$title' => stripslashes($msg['title']),
132                                 '$body' => $body
133                         ));
134
135                         $res = mail($importer['email'], t('New mail received at ') . $a->config['sitename'],
136                                 $email_tpl, 'From: ' . t('Administrator') . '@' . $a->get_hostname() . "\r\n"
137                                         . 'MIME-Version: 1.0' . "\r\n"
138                                         . 'Content-type: text/plain; charset=UTF-8' . "\r\n" 
139                                         . 'Content-transfer-encoding: quoted-printable' . "\r\n"
140                         );
141                 }
142                 xml_status(0);
143                 // NOTREACHED
144         }       
145
146         foreach($feed->get_items() as $item) {
147
148                 $deleted = false;
149
150                 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB , 'deleted-entry');
151                 if(isset($rawdelete[0]['attribs']['']['ref'])) {
152                         $uri = $rawthread[0]['attribs']['']['ref'];
153                         $deleted = true;
154                         if(isset($rawdelete[0]['attribs']['']['when'])) {
155                                 $when = $rawthread[0]['attribs']['']['when'];
156                                 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
157                         }
158                         else
159                                 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
160                 }
161                 if($deleted) {
162                         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
163                                 dbesc($uri),
164                                 intval($importer['importer_uid'])
165                         );
166                         if(count($r)) {
167                                 $item = $r[0];
168                                 if($item['uri'] == $item['parent-uri']) {
169                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
170                                                 WHERE `parent-uri` = '%s' AND `uid` = %d",
171                                                 dbesc($when),
172                                                 dbesc(datetime_convert()),
173                                                 dbesc($item['uri']),
174                                                 intval($importer['importer_uid'])
175                                         );
176                                 }
177                                 else {
178                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' 
179                                                 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
180                                                 dbesc($when),
181                                                 dbesc(datetime_convert()),
182                                                 dbesc($uri),
183                                                 intval($importer['importer_uid'])
184                                         );
185                                         if($item['last-child']) {
186                                                 // ensure that last-child is set in case the comment that had it just got wiped.
187                                                 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
188                                                         dbesc(datetime_convert()),
189                                                         dbesc($item['parent-uri']),
190                                                         intval($item['uid'])
191                                                 );
192                                                 // who is the last child now? 
193                                                 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
194                                                         ORDER BY `created` DESC LIMIT 1",
195                                                                 dbesc($item['parent-uri']),
196                                                                 intval($importer['importer_uid'])
197                                                 );
198                                                 if(count($r)) {
199                                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
200                                                                 intval($r[0]['id'])
201                                                         );
202                                                 }       
203                                         }
204                                 }
205                         }       
206                         continue;
207                 }
208
209                 $is_reply = false;              
210                 $item_id = $item->get_id();
211                 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
212                 if(isset($rawthread[0]['attribs']['']['ref'])) {
213                         $is_reply = true;
214                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
215                 }
216
217                 if($is_reply) {
218                         if($feed->get_item_quantity() == 1) {
219                                 // remote reply to our post. Import and then notify everybody else.
220                                 $datarray = get_atom_elements($feed,$item);
221                                 $datarray['type'] = 'remote-comment';
222                                 $datarray['wall'] = 1;
223                                 $datarray['parent-uri'] = $parent_uri;
224                                 $datarray['uid'] = $importer['importer_uid'];
225                                 $datarray['contact-id'] = $importer['id'];
226                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
227                                         $datarray['type'] = 'activity';
228                                         $datarray['gravity'] = GRAVITY_LIKE;
229                                 }
230                                 $posted_id = item_store($datarray);
231
232                                 if($posted_id) {
233                                         $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
234                                                 intval($posted_id),
235                                                 intval($importer['importer_uid'])
236                                         );
237                                         if(count($r)) {
238                                                 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
239                                                         dbesc(datetime_convert()),
240                                                         intval($importer['importer_uid']),
241                                                         intval($r[0]['parent'])
242                                                 );
243                                         }
244                                         $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
245                                                         dbesc(datetime_convert()),
246                                                         intval($importer['importer_uid']),
247                                                         intval($posted_id)
248                                         );
249
250                                         if($datarray['type'] == 'remote-comment') {
251                                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
252
253                                                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &", 
254                                                         array(),$foo));
255
256                                                 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
257                                                         require_once('bbcode.php');
258                                                         $from = stripslashes($datarray['author-name']);
259                                                         $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
260                                                         $email_tpl = replace_macros($tpl, array(
261                                                                 '$sitename' => $a->config['sitename'],
262                                                                 '$siteurl' =>  $a->get_baseurl(),
263                                                                 '$username' => $importer['username'],
264                                                                 '$email' => $importer['email'],
265                                                                 '$from' => $from,
266                                                         '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
267                                                         ));
268         
269                                                         $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
270                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
271                                                 }
272                                         }
273                                 }
274                                 xml_status(0);
275                                 // NOTREACHED
276
277                         }
278                         else {
279                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
280
281                                 $item_id = $item->get_id();
282
283                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
284                                         dbesc($item_id),
285                                         intval($importer['importer_uid'])
286                                 );
287                                 // FIXME update content if 'updated' changes
288                                 if(count($r)) {
289                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
290                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
291                                                 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
292                                                         intval($allow[0]['data']),
293                                                         dbesc(datetime_convert()),
294                                                         dbesc($item_id),
295                                                         intval($importer['importer_uid'])
296                                                 );
297                                         }
298                                         continue;
299                                 }
300                                 $datarray = get_atom_elements($feed,$item);
301                                 $datarray['parent-uri'] = $parent_uri;
302                                 $datarray['uid'] = $importer['importer_uid'];
303                                 $datarray['contact-id'] = $importer['id'];
304                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
305                                         $datarray['type'] = 'activity';
306                                         $datarray['gravity'] = GRAVITY_LIKE;
307                                 }
308
309                                 $r = item_store($datarray);
310
311                                 // find out if our user is involved in this conversation and wants to be notified.
312                         
313                                 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
314
315                                         $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
316                                                 dbesc($parent_uri),
317                                                 intval($importer['importer_uid'])
318                                         );
319                                         if(count($myconv)) {
320                                                 foreach($myconv as $conv) {
321                                                         if($conv['author-link'] != $importer['url'])
322                                                                 continue;
323                                                         require_once('bbcode.php');
324                                                         $from = stripslashes($datarray['author-name']);
325                                                         $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
326                                                         $email_tpl = replace_macros($tpl, array(
327                                                                 '$sitename' => $a->config['sitename'],
328                                                                 '$siteurl' =>  $a->get_baseurl(),
329                                                                 '$username' => $importer['username'],
330                                                                 '$email' => $importer['email'],
331                                                                 '$from' => $from,
332                                                                 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
333                                                         ));
334
335                                                         $res = mail($importer['email'], $from . t(" commented on an item at ") 
336                                                                 . $a->config['sitename'],
337                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
338                                                         break;
339                                                 }
340                                         }
341                                 }
342                                 continue;
343                         }
344                 }
345                 else {
346                         // Head post of a conversation. Have we seen it? If not, import it.
347
348                         $item_id = $item->get_id();
349                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
350                                 dbesc($item_id),
351                                 intval($importer['importer_uid'])
352                         );
353                         if(count($r)) {
354                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
355                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
356                                         $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
357                                                 intval($allow[0]['data']),
358                                                 dbesc(datetime_convert()),
359                                                 dbesc($item_id),
360                                                 intval($importer['importer_uid'])
361                                         );
362                                 }
363                                 continue;
364                         }
365
366
367                         $datarray = get_atom_elements($feed,$item);
368                         $datarray['parent-uri'] = $item_id;
369                         $datarray['uid'] = $importer['importer_uid'];
370                         $datarray['contact-id'] = $importer['id'];
371                         $r = item_store($datarray);
372                         continue;
373                 }
374         }
375
376         xml_status(0);
377         // NOTREACHED
378
379 }
380
381
382 function dfrn_notify_content(&$a) {
383
384         if(x($_GET,'dfrn_id')) {
385
386                 // initial communication from external contact, $direction is their direction.
387                 // If this is a duplex communication, ours will be the opposite.
388
389                 $dfrn_id = notags(trim($_GET['dfrn_id']));
390                 $dfrn_version = (float) $_GET['dfrn_version'];
391
392
393                 $direction = (-1);
394                 if(strpos($dfrn_id,':') == 1) {
395                         $direction = intval(substr($dfrn_id,0,1));
396                         $dfrn_id = substr($dfrn_id,2);
397                 }
398
399                 $hash = random_string();
400
401                 $status = 0;
402
403                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
404
405                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
406                         VALUES( '%s', '%s', '%s') ",
407                         dbesc($hash),
408                         dbesc($dfrn_id),
409                         intval(time() + 60 )
410                 );
411
412
413                 $sql_extra = '';
414                 switch($direction) {
415                         case (-1):
416                                 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
417                                 $my_id = $dfrn_id;
418                                 break;
419                         case 0:
420                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
421                                 $my_id = '1:' . $dfrn_id;
422                                 break;
423                         case 1:
424                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
425                                 $my_id = '0:' . $dfrn_id;
426                                 break;
427                         default:
428                                 $status = 1;
429                                 break; // NOTREACHED
430                 }
431
432                 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
433                                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
434                                 dbesc($a->argv[1])
435                 );
436
437                 if(! count($r))
438                         $status = 1;
439
440                 $challenge = '';
441                 $encrypted_id = '';
442                 $id_str = $my_id . '.' . mt_rand(1000,9999);
443
444                 if((($r[0]['duplex']) && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
445                         openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
446                         openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
447                 }
448                 else {
449                         openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
450                         openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
451                 }
452
453                 $challenge    = bin2hex($challenge);
454                 $encrypted_id = bin2hex($encrypted_id);
455
456                 header("Content-type: text/xml");
457
458                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" 
459                         . '<dfrn_notify>' . "\r\n"
460                         . "\t" . '<status>' . $status . '</status>' . "\r\n"
461                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
462                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n" 
463                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
464                         . '</dfrn_notify>' . "\r\n" ;
465
466                 killme();
467         }
468
469 }