]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
like, dislike, activity streams, etc.
[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                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
212                                         $datarray['type'] = 'activity';
213                                         $datarray['gravity'] = GRAVITY_LIKE;
214                                 }
215                                 $posted_id = item_store($datarray);
216
217                                 if($posted_id) {
218                                         $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
219                                                 intval($posted_id),
220                                                 intval($importer['importer_uid'])
221                                         );
222                                         if(count($r)) {
223                                                 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
224                                                         dbesc(datetime_convert()),
225                                                         intval($importer['importer_uid']),
226                                                         intval($r[0]['parent'])
227                                                 );
228                                         }
229                                         $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
230                                                         dbesc(datetime_convert()),
231                                                         intval($importer['importer_uid']),
232                                                         intval($posted_id)
233                                         );
234
235                                         if($datarray['type'] == 'remote-comment') {
236                                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
237
238                                                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"comment-import\" \"$posted_id\" &", 
239                                                         array(),$foo));
240
241                                                 if(($importer['notify-flags'] & NOTIFY_COMMENT) && (! $importer['self'])) {
242                                                         require_once('bbcode.php');
243                                                         $from = stripslashes($datarray['author-name']);
244                                                         $tpl = file_get_contents('view/cmnt_received_eml.tpl');                 
245                                                         $email_tpl = replace_macros($tpl, array(
246                                                                 '$sitename' => $a->config['sitename'],
247                                                                 '$siteurl' =>  $a->get_baseurl(),
248                                                                 '$username' => $importer['username'],
249                                                                 '$email' => $importer['email'],
250                                                                 '$from' => $from,
251                                                         '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
252                                                         ));
253         
254                                                         $res = mail($importer['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
255                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
256                                                 }
257                                         }
258                                 }
259                                 xml_status(0);
260                                 return;
261
262                         }
263                         else {
264                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
265
266                                 $item_id = $item->get_id();
267
268                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
269                                         dbesc($item_id),
270                                         intval($importer['importer_uid'])
271                                 );
272                                 // FIXME update content if 'updated' changes
273                                 if(count($r)) {
274                                         $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
275                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
276                                                 $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
277                                                         intval($allow[0]['data']),
278                                                         dbesc(datetime_convert()),
279                                                         dbesc($item_id),
280                                                         intval($importer['importer_uid'])
281                                                 );
282                                         }
283                                         continue;
284                                 }
285                                 $datarray = get_atom_elements($item);
286                                 $datarray['parent-uri'] = $parent_uri;
287                                 $datarray['uid'] = $importer['importer_uid'];
288                                 $datarray['contact-id'] = $importer['id'];
289                                 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
290                                         $datarray['type'] = 'activity';
291                                         $datarray['gravity'] = GRAVITY_LIKE;
292                                 }
293
294                                 $r = item_store($datarray);
295
296                                 // find out if our user is involved in this conversation and wants to be notified.
297                         
298                                 if(($datarray['type'] != 'activity') && ($importer['notify-flags'] & NOTIFY_COMMENT)) {
299
300                                         $myconv = q("SELECT `author-link` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d",
301                                                 dbesc($parent_uri),
302                                                 intval($importer['importer_uid'])
303                                         );
304                                         if(count($myconv)) {
305                                                 foreach($myconv as $conv) {
306                                                         if($conv['author-link'] != $importer['url'])
307                                                                 continue;
308                                                         require_once('bbcode.php');
309                                                         $from = stripslashes($datarray['author-name']);
310                                                         $tpl = file_get_contents('view/cmnt_received_eml.tpl');                 
311                                                         $email_tpl = replace_macros($tpl, array(
312                                                                 '$sitename' => $a->config['sitename'],
313                                                                 '$siteurl' =>  $a->get_baseurl(),
314                                                                 '$username' => $importer['username'],
315                                                                 '$email' => $importer['email'],
316                                                                 '$from' => $from,
317                                                                 '$body' => strip_tags(bbcode(stripslashes($datarray['body'])))
318                                                         ));
319
320                                                         $res = mail($importer['email'], $from . t(" commented on an item at ") 
321                                                                 . $a->config['sitename'],
322                                                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
323                                                         break;
324                                                 }
325                                         }
326                                 }
327                                 continue;
328                         }
329                 }
330                 else {
331                         // Head post of a conversation. Have we seen it? If not, import it.
332
333                         $item_id = $item->get_id();
334                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
335                                 dbesc($item_id),
336                                 intval($importer['importer_uid'])
337                         );
338                         if(count($r)) {
339                                 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
340                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
341                                         $r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
342                                                 intval($allow[0]['data']),
343                                                 dbesc(datetime_convert()),
344                                                 dbesc($item_id),
345                                                 intval($importer['importer_uid'])
346                                         );
347                                 }
348                                 continue;
349                         }
350
351
352                         $datarray = get_atom_elements($item);
353                         $datarray['parent-uri'] = $item_id;
354                         $datarray['uid'] = $importer['importer_uid'];
355                         $datarray['contact-id'] = $importer['id'];
356                         $r = item_store($datarray);
357                         continue;
358
359                 }
360         
361         }
362
363         xml_status(0);
364         killme();
365
366 }
367
368
369 function dfrn_notify_content(&$a) {
370
371         if(x($_GET,'dfrn_id')) {
372
373                 // initial communication from external contact, $direction is their direction.
374                 // If this is a duplex communication, ours will be the opposite.
375
376                 $dfrn_id = notags(trim($_GET['dfrn_id']));
377
378                 $direction = (-1);
379                 if(strpos($dfrn_id,':') == 1) {
380                         $direction = intval(substr($dfrn_id,0,1));
381                         $dfrn_id = substr($dfrn_id,2);
382                 }
383
384                 $hash = random_string();
385
386                 $status = 0;
387
388                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
389
390                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
391                         VALUES( '%s', '%s', '%s') ",
392                         dbesc($hash),
393                         dbesc($dfrn_id),
394                         intval(time() + 60 )
395                 );
396
397
398                 $sql_extra = '';
399                 switch($direction) {
400                         case (-1):
401                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
402                                 $my_id = $dfrn_id;
403                                 break;
404                         case 0:
405                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
406                                 $my_id = '1:' . $dfrn_id;
407                                 break;
408                         case 1:
409                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
410                                 $my_id = '0:' . $dfrn_id;
411                                 break;
412                         default:
413                                 $status = 1;
414                                 break; // NOTREACHED
415                 }
416
417
418
419                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
420
421                 if(! count($r))
422                         $status = 1;
423
424                 $challenge = '';
425                 $encrypted_id = '';
426                 $id_str = $my_id . '.' . mt_rand(1000,9999);
427
428                 if(($r[0]['duplex']) && strlen($r[0]['pubkey'])) {
429                         openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
430                         openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
431                 }
432                 else {
433                         openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
434                         openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
435                 }
436
437                 $challenge    = bin2hex($challenge);
438                 $encrypted_id = bin2hex($encrypted_id);
439
440                 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_version>2.0</dfrn_version><dfrn_id>' . $encrypted_id . '</dfrn_id>' . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
441                 session_write_close();
442                 exit;
443                 
444         }
445
446 }