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