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