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