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