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