]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
f81eb9cfb7ad6b83577cd80fcbbc401f32ab4812
[friendica.git] / mod / dfrn_notify.php
1 <?php
2
3 require_once('simplepie/simplepie.inc');
4
5
6 function get_atom_elements($item) {
7
8         $res = array();
9
10         $author = $item->get_author();
11         $res['remote-name'] = unxmlify($author->get_name());
12         $res['remote-link'] = unxmlify($author->get_link());
13         $res['remote-avatar'] = unxmlify($author->get_avatar());
14         $res['remote-id'] = unxmlify($item->get_id());
15         $res['title'] = unxmlify($item->get_title());
16         $res['body'] = unxmlify($item->get_content());
17
18         if(strlen($res['body']) > 100000)
19                 $res['body'] = substr($res['body'],0,10000) . "\r\n[Extremely large post truncated.]\r\n"  ;
20
21         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
22         if($allow && $allow[0]['data'] == 1)
23                 $res['last-child'] = 1;
24         else
25                 $res['last-child'] = 0;
26
27         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
28         if($rawcreated)
29                 $res['created'] = unxmlify($rawcreated[0]['data']);
30
31         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
32         if($rawedited)
33                 $res['edited'] = unxmlify($rawcreated[0]['data']);
34
35         $rawowner = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0', 'owner');
36         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['name'][0]['data'])
37                 $res['owner-name'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['name'][0]['data']);
38         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['uri'][0]['data'])
39                 $res['owner-link'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['uri'][0]['data']);
40         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['avatar'][0]['data'])
41                 $res['owner-avatar'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['avatar'][0]['data']);
42
43
44         return $res;
45
46 }
47
48 function post_remote($a,$arr) {
49
50         $arr['hash'] = random_string();
51         if(! x($arr,'type'))
52                 $arr['type'] = 'remote';
53         $arr['remote-name'] = notags(trim($arr['remote-name']));
54         $arr['remote-link'] = notags(trim($arr['remote-link']));
55         $arr['remote-avatar'] = notags(trim($arr['remote-avatar']));
56         $arr['owner-name'] = notags(trim($arr['owner-name']));
57         $arr['owner-link'] = notags(trim($arr['owner-link']));
58         $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
59         if(! strlen($arr['remote-avatar']))
60                 $arr['remote-avatar'] = $a->get_baseurl() . '/images/default-profile-sm.jpg';
61         if(! strlen($arr['owner-avatar']))
62                 $arr['owner-avatar'] = $a->get_baseurl() . '/images/default-profile-sm.jpg';
63         $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
64         $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
65         $arr['title'] = notags(trim($arr['title']));
66         $arr['body'] = escape_tags(trim($arr['body']));
67         $arr['last-child'] = intval($arr['last-child']);
68         $arr['visible'] = 1;
69         $arr['deleted'] = 0;
70
71         $local_parent = false;
72
73         if(isset($arr['parent_hash'])) {
74                 $local_parent = true;
75                 $parent = $arr['parent_hash'];
76                 unset($arr['parent_hash']);
77         }
78         else {
79                 $parent = $arr['parent_urn'];
80                 unset($arr['parent_urn']);
81         }
82
83         $parent_id = 0;
84
85         dbesc_array($arr);
86 dbg(3);
87         $r = q("INSERT INTO `item` (`" 
88                         . implode("`, `", array_keys($arr)) 
89                         . "`) VALUES ('" 
90                         . implode("', '", array_values($arr)) 
91                         . "')" );
92
93         if($local_parent) {
94                 $r = q("SELECT `id` FROM `item` WHERE `hash` = '%s' AND `uid` = %d LIMIT 1",
95                         dbesc($parent),
96                         intval($arr['uid'])
97                 );
98         }
99         else {
100                 $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
101                         dbesc($parent),
102                         intval($arr['uid'])
103                 );
104         }
105         if(count($r))
106                 $parent_id = $r[0]['id'];
107         
108
109         $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
110                 $arr['remote-id'],
111                 intval($arr['uid'])
112         );
113         if(count($r))
114                 $current_post = $r[0]['id'];
115
116         $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
117                 intval($parent_id),
118                 intval($current_post)
119         );
120
121         return $current_post;
122 }
123
124 function dfrn_notify_post(&$a) {
125 dbg(3);
126         $dfrn_id = notags(trim($_POST['dfrn_id']));
127         $challenge = notags(trim($_POST['challenge']));
128         $data = $_POST['data'];
129         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
130                 dbesc($dfrn_id),
131                 dbesc($challenge)
132         );
133         if(! count($r))
134                 xml_status(3);
135
136         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
137                 dbesc($dfrn_id),
138                 dbesc($challenge)
139         );
140
141         // find the local user who owns this relationship.
142
143         $r = q("SELECT `id`, `uid` FROM `contact` WHERE `issued-id` = '%s' LIMIT 1",
144                 dbesc($dfrn_id)
145         );
146         if(! count($r))
147                 xml_status(3);
148
149
150         $importer = $r[0];
151
152         $feed = new SimplePie();
153         $feed->set_raw_data($data);
154         $feed->enable_order_by_date(false);
155         $feed->init();
156
157         foreach($feed->get_items() as $item) {
158
159                 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
160                 print_r($rawdelete);
161                 if($deleted) {
162                         // pick out ref and when from attribs
163                         // check hasn't happened already, verify ownership and then process it
164
165
166                         continue;
167                 }
168
169                 $is_reply = false;              
170                 $item_id = $item->get_id();
171                 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
172                 if(isset($rawthread[0]['attribs']['']['ref'])) {
173                         $is_reply = true;
174                         $parent_urn = $rawthread[0]['attribs']['']['ref'];
175                 }
176
177
178                 if($is_reply) {
179                         if($feed->get_item_quantity() == 1) {
180                                 // remote reply to our post. Import and then notify everybody else.
181                                 $datarray = get_atom_elements($item);
182                                 $urn = explode(':',$parent_urn);
183                                 $datarray['type'] = 'remote-comment';
184                                 $datarray['parent_hash'] = $urn[5];
185                                 $datarray['uid'] = $importer['uid'];
186                                 $datarray['contact-id'] = $importer['id'];
187                                 $posted_id = post_remote($a,$datarray);
188
189                                 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
190                                         intval($posted_id),
191                                         intval($importer['uid'])
192                                 );
193                                 if(count($r)) {
194                                         $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
195                                                 intval($importer['uid']),
196                                                 intval($r[0]['parent'])
197                                         );
198                                 }
199                                 $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
200                                                 intval($importer['uid']),
201                                                 intval($posted_id)
202                                 );
203
204                                 $url = bin2hex($a->get_baseurl());
205
206                                 proc_close(proc_open("php include/notifier.php $url comment-import $posted_id > remote-notify.log &", array(),$foo));
207
208                                 xml_status(0);
209                                 return;
210
211                         }
212                         else {
213                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
214
215                                 $item_id = $item->get_id();
216
217                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
218                                         dbesc($item_id),
219                                         intval($importer['uid'])
220                                 );
221                                 // FIXME update content if 'updated' changes
222                                 if(count($r)) {
223                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
224                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
225                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
226                                                         intval($allow[0]['data']),
227                                                         dbesc($item_id),
228                                                         intval($importer['uid'])
229                                                 );
230                                         }
231                                         continue;
232                                 }
233                                 $datarray = get_atom_elements($item);
234                                 $datarray['parent_urn'] = $parent_urn;
235                                 $datarray['uid'] = $importer['uid'];
236                                 $datarray['contact-id'] = $importer['id'];
237                                 $r = post_remote($a,$datarray);
238                                 continue;
239                         }
240                 }
241                 else {
242                         // Head post of a conversation. Have we seen it? If not, import it.
243
244                         $item_id = $item->get_id();
245                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
246                                 dbesc($item_id),
247                                 intval($importer['uid'])
248                         );
249                         if(count($r)) {
250                                 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
251                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
252                                         $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
253                                                 intval($allow[0]['data']),
254                                                 dbesc($item_id),
255                                                 intval($importer['uid'])
256                                         );
257                                 }
258                                 continue;
259                         }
260
261
262                         $datarray = get_atom_elements($item);
263                         $datarray['parent_urn'] = $item_id;
264                         $datarray['uid'] = $importer['uid'];
265                         $datarray['contact-id'] = $importer['id'];
266                         $r = post_remote($a,$datarray);
267                         continue;
268
269                 }
270         
271         }
272
273         xml_status(0);
274         killme();
275
276 }
277
278
279
280
281
282
283
284
285
286
287 function dfrn_notify_content(&$a) {
288
289         if(x($_GET,'dfrn_id')) {
290                 // initial communication from external contact
291                 $hash = random_string();
292
293                 $status = 0;
294
295                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
296
297                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
298                         VALUES( '%s', '%s', '%s') ",
299                         dbesc($hash),
300                         dbesc(notags(trim($_GET['dfrn_id']))),
301                         intval(time() + 60 )
302                 );
303
304                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 LIMIT 1",
305                         dbesc($_GET['dfrn_id']));
306                 if((! count($r)) || (! strlen($r[0]['prvkey'])))
307                         $status = 1;
308
309                 $challenge = '';
310
311                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
312                 $challenge = bin2hex($challenge);
313                 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $_GET['dfrn_id'] . '</dfrn_id>'
314                         . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
315                 session_write_close();
316                 exit;
317                 
318         }
319
320 }