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