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