]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
03d96d27ea2fffab33ab6223bf11018de1434436
[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 * FROM `contact` 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['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                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
78                         intval($importer['uid'])
79                 );
80                 require_once('bbcode.php');
81                 if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_MAIL)) {
82                         $tpl = file_get_contents('view/mail_received_eml.tpl');                 
83                         $email_tpl = replace_macros($tpl, array(
84                                 '$sitename' => $a->config['sitename'],
85                                 '$siteurl' =>  $a->get_baseurl(),
86                                 '$username' => $r[0]['username'],
87                                 '$email' => $r[0]['email'],
88                                 '$from' => $msg['from-name'],
89                                 '$fn' => $r[0]['name'],
90                                 '$title' => $msg['title'],
91                                 '$body' => strip_tags(bbcode($msg['body']))
92                         ));
93
94                         $res = mail($r[0]['email'], t("New mail received at ") . $a->config['sitename'],
95                                 $email_tpl,t("From: Administrator@") . $a->get_hostname() );
96                 }
97                 xml_status(0);
98                 return; // NOTREACHED
99         }       
100
101         foreach($feed->get_items() as $item) {
102
103                 $deleted = false;
104
105                 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
106                 if(isset($rawdelete[0]['attribs']['']['ref'])) {
107                         $uri = $rawthread[0]['attribs']['']['ref'];
108                         $deleted = true;
109                         if(isset($rawdelete[0]['attribs']['']['when'])) {
110                                 $when = $rawthread[0]['attribs']['']['when'];
111                                 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
112                         }
113                         else
114                                 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
115                 }
116                 if($deleted) {
117                         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
118                                 dbesc($uri),
119                                 intval($importer['uid'])
120                         );
121                         if(count($r)) {
122                                 if($r[0]['uri'] == $r[0]['parent-uri']) {
123                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
124                                                 WHERE `parent-uri` = '%s'",
125                                                 dbesc($when),
126                                                 dbesc($r[0]['uri'])
127                                         );
128                                 }
129                                 else {
130                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' 
131                                                 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
132                                                 dbesc($when),
133                                                 dbesc($uri),
134                                                 intval($importer['uid'])
135                                         );
136                                 }
137                         }       
138                         continue;
139                 }
140
141                 $is_reply = false;              
142                 $item_id = $item->get_id();
143                 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
144                 if(isset($rawthread[0]['attribs']['']['ref'])) {
145                         $is_reply = true;
146                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
147                 }
148
149
150                 if($is_reply) {
151                         if($feed->get_item_quantity() == 1) {
152                                 // remote reply to our post. Import and then notify everybody else.
153                                 $datarray = get_atom_elements($item);
154                                 $urn = explode(':',$parent_urn);
155                                 $datarray['type'] = 'remote-comment';
156                                 $datarray['parent-uri'] = $parent_uri;
157                                 $datarray['uid'] = $importer['uid'];
158                                 $datarray['contact-id'] = $importer['id'];
159                                 $posted_id = post_remote($a,$datarray);
160
161                                 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
162                                         intval($posted_id),
163                                         intval($importer['uid'])
164                                 );
165                                 if(count($r)) {
166                                         $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
167                                                 intval($importer['uid']),
168                                                 intval($r[0]['parent'])
169                                         );
170                                 }
171                                 $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
172                                                 intval($importer['uid']),
173                                                 intval($posted_id)
174                                 );
175
176                                 $url = $a->get_baseurl();
177
178                                 proc_close(proc_open("php include/notifier.php $url comment-import $posted_id > remote-notify.log &", array(),$foo));
179
180                                 xml_status(0);
181                                 return;
182
183                         }
184                         else {
185                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
186
187                                 $item_id = $item->get_id();
188
189                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
190                                         dbesc($item_id),
191                                         intval($importer['uid'])
192                                 );
193                                 // FIXME update content if 'updated' changes
194                                 if(count($r)) {
195                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
196                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
197                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
198                                                         intval($allow[0]['data']),
199                                                         dbesc($item_id),
200                                                         intval($importer['uid'])
201                                                 );
202                                         }
203                                         continue;
204                                 }
205                                 $datarray = get_atom_elements($item);
206                                 $datarray['parent-uri'] = $parent_uri;
207                                 $datarray['uid'] = $importer['uid'];
208                                 $datarray['contact-id'] = $importer['id'];
209                                 $r = post_remote($a,$datarray);
210                                 continue;
211                         }
212                 }
213                 else {
214                         // Head post of a conversation. Have we seen it? If not, import it.
215
216                         $item_id = $item->get_id();
217                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
218                                 dbesc($item_id),
219                                 intval($importer['uid'])
220                         );
221                         if(count($r)) {
222                                 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
223                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
224                                         $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
225                                                 intval($allow[0]['data']),
226                                                 dbesc($item_id),
227                                                 intval($importer['uid'])
228                                         );
229                                 }
230                                 continue;
231                         }
232
233
234                         $datarray = get_atom_elements($item);
235                         $datarray['parent-uri'] = $item_id;
236                         $datarray['uid'] = $importer['uid'];
237                         $datarray['contact-id'] = $importer['id'];
238                         $r = post_remote($a,$datarray);
239                         continue;
240
241                 }
242         
243         }
244
245         xml_status(0);
246         killme();
247
248 }
249
250
251 function dfrn_notify_content(&$a) {
252
253         if(x($_GET,'dfrn_id')) {
254                 // initial communication from external contact
255                 $hash = random_string();
256
257                 $status = 0;
258
259                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
260
261                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
262                         VALUES( '%s', '%s', '%s') ",
263                         dbesc($hash),
264                         dbesc(notags(trim($_GET['dfrn_id']))),
265                         intval(time() + 60 )
266                 );
267
268                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
269                         dbesc($_GET['dfrn_id']));
270                 if((! count($r)) || (! strlen($r[0]['prvkey'])))
271                         $status = 1;
272
273                 $challenge = '';
274
275                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
276                 $challenge = bin2hex($challenge);
277
278                 $encrypted_id = '';
279                 $id_str = $_GET['dfrn_id'] . '.' . mt_rand(1000,9999);
280
281                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
282                 $encrypted_id = bin2hex($encrypted_id);
283
284                 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $encrypted_id . '</dfrn_id>'
285                         . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
286                 session_write_close();
287                 exit;
288                 
289         }
290
291 }