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