]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
clean up some regex's for i18n, and eliminate old ereg patterns.
[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 = dbq("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                                                                 '$display' => $a->get_baseurl() . '/display/' . $posted_id, 
266                                                                 '$from' => $from,
267                                                         '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
268                                                         ));
269         
270                                                         $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
271                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
272                                                 }
273                                         }
274                                 }
275                                 xml_status(0);
276                                 // NOTREACHED
277
278                         }
279                         else {
280                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
281
282                                 $item_id = $item->get_id();
283
284                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
285                                         dbesc($item_id),
286                                         intval($importer['importer_uid'])
287                                 );
288                                 // FIXME update content if 'updated' changes
289                                 if(count($r)) {
290                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
291                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
292                                                 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
293                                                         intval($allow[0]['data']),
294                                                         dbesc(datetime_convert()),
295                                                         dbesc($item_id),
296                                                         intval($importer['importer_uid'])
297                                                 );
298                                         }
299                                         continue;
300                                 }
301                                 $datarray = get_atom_elements($feed,$item);
302                                 $datarray['parent-uri'] = $parent_uri;
303                                 $datarray['uid'] = $importer['importer_uid'];
304                                 $datarray['contact-id'] = $importer['id'];
305                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
306                                         $datarray['type'] = 'activity';
307                                         $datarray['gravity'] = GRAVITY_LIKE;
308                                 }
309
310                                 $r = item_store($datarray);
311
312                                 // find out if our user is involved in this conversation and wants to be notified.
313                         
314                                 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
315
316                                         $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
317                                                 dbesc($parent_uri),
318                                                 intval($importer['importer_uid'])
319                                         );
320                                         if(count($myconv)) {
321                                                 foreach($myconv as $conv) {
322                                                         if($conv['author-link'] != $importer['url'])
323                                                                 continue;
324                                                         require_once('bbcode.php');
325                                                         $from = stripslashes($datarray['author-name']);
326                                                         $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
327                                                         $email_tpl = replace_macros($tpl, array(
328                                                                 '$sitename' => $a->config['sitename'],
329                                                                 '$siteurl' =>  $a->get_baseurl(),
330                                                                 '$username' => $importer['username'],
331                                                                 '$email' => $importer['email'],
332                                                                 '$from' => $from,
333                                                                 '$display' => $a->get_baseurl() . '/display' . $r,
334                                                                 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
335                                                         ));
336
337                                                         $res = mail($importer['email'], $from . t(" commented on an item at ") 
338                                                                 . $a->config['sitename'],
339                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
340                                                         break;
341                                                 }
342                                         }
343                                 }
344                                 continue;
345                         }
346                 }
347                 else {
348                         // Head post of a conversation. Have we seen it? If not, import it.
349
350                         $item_id = $item->get_id();
351                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
352                                 dbesc($item_id),
353                                 intval($importer['importer_uid'])
354                         );
355                         if(count($r)) {
356                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
357                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
358                                         $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
359                                                 intval($allow[0]['data']),
360                                                 dbesc(datetime_convert()),
361                                                 dbesc($item_id),
362                                                 intval($importer['importer_uid'])
363                                         );
364                                 }
365                                 continue;
366                         }
367
368
369                         $datarray = get_atom_elements($feed,$item);
370                         $datarray['parent-uri'] = $item_id;
371                         $datarray['uid'] = $importer['importer_uid'];
372                         $datarray['contact-id'] = $importer['id'];
373                         $r = item_store($datarray);
374                         continue;
375                 }
376         }
377
378         xml_status(0);
379         // NOTREACHED
380
381 }
382
383
384 function dfrn_notify_content(&$a) {
385
386         if(x($_GET,'dfrn_id')) {
387
388                 // initial communication from external contact, $direction is their direction.
389                 // If this is a duplex communication, ours will be the opposite.
390
391                 $dfrn_id = notags(trim($_GET['dfrn_id']));
392                 $dfrn_version = (float) $_GET['dfrn_version'];
393
394
395                 $direction = (-1);
396                 if(strpos($dfrn_id,':') == 1) {
397                         $direction = intval(substr($dfrn_id,0,1));
398                         $dfrn_id = substr($dfrn_id,2);
399                 }
400
401                 $hash = random_string();
402
403                 $status = 0;
404
405                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
406
407                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
408                         VALUES( '%s', '%s', '%s') ",
409                         dbesc($hash),
410                         dbesc($dfrn_id),
411                         intval(time() + 60 )
412                 );
413
414
415                 $sql_extra = '';
416                 switch($direction) {
417                         case (-1):
418                                 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
419                                 $my_id = $dfrn_id;
420                                 break;
421                         case 0:
422                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
423                                 $my_id = '1:' . $dfrn_id;
424                                 break;
425                         case 1:
426                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
427                                 $my_id = '0:' . $dfrn_id;
428                                 break;
429                         default:
430                                 $status = 1;
431                                 break; // NOTREACHED
432                 }
433
434                 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
435                                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
436                                 dbesc($a->argv[1])
437                 );
438
439                 if(! count($r))
440                         $status = 1;
441
442                 $challenge = '';
443                 $encrypted_id = '';
444                 $id_str = $my_id . '.' . mt_rand(1000,9999);
445
446                 if((($r[0]['duplex']) && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
447                         openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
448                         openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
449                 }
450                 else {
451                         openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
452                         openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
453                 }
454
455                 $challenge    = bin2hex($challenge);
456                 $encrypted_id = bin2hex($encrypted_id);
457
458                 header("Content-type: text/xml");
459
460                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" 
461                         . '<dfrn_notify>' . "\r\n"
462                         . "\t" . '<status>' . $status . '</status>' . "\r\n"
463                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
464                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n" 
465                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
466                         . '</dfrn_notify>' . "\r\n" ;
467
468                 killme();
469         }
470
471 }