]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
5011471426301a1d782eabb75f3f6076f201e6e2
[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'] = $author->get_name();
12         $res['remote-link'] = $author->get_link();
13         $res['remote-avatar'] = $author->get_avatar();
14         $res['remote-id'] = $item->get_id();
15         $res['title'] = $item->get_title();
16         $res['body'] = $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'] = $rawcreated[0]['data'];
30
31         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
32         if($rawedited)
33                 $res['edited'] = $rawcreated[0]['data'];
34
35
36
37         return $res;
38
39 }
40
41 function post_remote($arr) {
42
43         $arr['hash'] = random_string();
44         $arr['type'] = 'remote';
45         $arr['remote-name'] = notags(trim($arr['remote-name']));
46         $arr['remote-link'] = notags(trim($arr['remote-link']));
47         $arr['remote-avatar'] = notags(trim($arr['remote-avatar']));
48         if(! strlen($arr['remote-avatar']))
49                 $arr['remote-avatar'] = $a->get_baseurl() . '/images/default-profile-sm.jpg';
50         $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
51         $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
52         $arr['title'] = notags(trim($arr['title']));
53         $arr['body'] = escape_tags(trim($arr['body']));
54         $arr['last-child'] = intval($arr['last_child']);
55         $arr['visible'] = 1;
56         $arr['deleted'] = 0;
57
58         $parent = $arr['parent_urn'];
59
60         unset($arr['parent_urn']);
61
62         $parent_id = 0;
63
64         dbesc_array($arr);
65
66         $r = q("INSERT INTO `item` (`" 
67                         . implode("`, `", array_keys($arr)) 
68                         . "`) VALUES ('" 
69                         . implode("', '", array_values($arr)) 
70                         . "')" );
71
72
73         $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
74                 dbesc($parent),
75                 intval($arr['uid'])
76         );
77         if(count($r))
78                 $parent_id = $r[0]['id'];
79         
80
81         $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
82                 $arr['remote-id'],
83                 intval($arr['uid'])
84         );
85         if(count($r))
86                 $current_post = $r[0]['id'];
87
88         $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
89                 intval($parent_id),
90                 intval($current_post)
91         );
92
93 }
94
95 function dfrn_notify_post(&$a) {
96 dbg(3);
97         $dfrn_id = notags(trim($_POST['dfrn_id']));
98         $challenge = notags(trim($_POST['challenge']));
99         $data = $_POST['data'];
100         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
101                 dbesc($dfrn_id),
102                 dbesc($challenge)
103         );
104         if(! count($r))
105                 xml_status(3);
106
107         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
108                 dbesc($dfrn_id),
109                 dbesc($challenge)
110         );
111
112         // find the local user who owns this relationship.
113
114         $r = q("SELECT `id`, `uid` FROM `contact` WHERE `issued-id` = '%s' LIMIT 1",
115                 dbesc($dfrn_id)
116         );
117         if(! count($r))
118                 xml_status(3);
119
120
121         $importer = $r[0];
122
123         $feed = new SimplePie();
124         $feed->set_raw_data($data);
125         $feed->enable_order_by_date(false);
126         $feed->init();
127
128         foreach($feed->get_items() as $item) {
129
130                 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
131                 print_r($rawdelete);
132                 if($deleted) {
133                         // pick out ref and when from attribs
134                         // check hasn't happened already, verify ownership and then process it
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_urn = $rawthread[0]['attribs']['']['ref'];
146                 }
147
148
149                 if($is_reply) {
150                         if($x == ($total_items - 1)) {
151                                 // remote reply to our post. Import and then notify everybody else.
152                         }
153                         else {
154                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
155
156                                 $item_id = $item->get_id();
157
158                                 $r = q("SELECT `uid`, `last-child` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
159                                         dbesc($item_id),
160                                         intval($importer['uid'])
161                                 );
162                                 if(count($r)) {
163                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
164                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
165                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
166                                                         intval($allow[0]['data']),
167                                                         dbesc($item_id)
168                                                 );
169                                         }
170                                         continue;
171                                 }
172                                 $datarray = get_atom_elements($item);
173                                 $datarray['parent_urn'] = $parent_urn;
174                                 $datarray['uid'] = $importer['uid'];
175                                 $datarray['contact-id'] = $importer['id'];
176                                 $r = post_remote($datarray);
177                                 continue;
178                         }
179                 }
180                 else {
181                         // Head post of a conversation. Have we seen it? If not, import it.
182
183                         $item_id = $item->get_id();
184                         $r = q("SELECT `uid` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
185                                 dbesc($item_id),
186                                 intval($importer['uid'])
187                         );
188                         if(count($r)) {
189                                 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
190                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
191                                         $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
192                                                 intval($allow[0]['data']),
193                                                 dbesc($item_id)
194                                         );
195                                 }
196                                 continue;
197                         }
198
199
200                         $datarray = get_atom_elements($item);
201                         $datarray['parent_urn'] = $item_id;
202                         $datarray['uid'] = $importer['uid'];
203                         $datarray['contact-id'] = $importer['id'];
204                         $r = post_remote($datarray);
205                         continue;
206
207                 }
208         
209         }
210
211
212         killme();
213
214 }
215
216
217
218
219
220
221
222
223
224
225 function dfrn_notify_content(&$a) {
226
227         if(x($_GET,'dfrn_id')) {
228                 // initial communication from external contact
229                 $hash = random_string();
230
231                 $status = 0;
232
233                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
234
235                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
236                         VALUES( '%s', '%s', '%s') ",
237                         dbesc($hash),
238                         dbesc(notags(trim($_GET['dfrn_id']))),
239                         intval(time() + 60 )
240                 );
241
242                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 LIMIT 1",
243                         dbesc($_GET['dfrn_id']));
244                 if((! count($r)) || (! strlen($r[0]['prvkey'])))
245                         $status = 1;
246
247                 $challenge = '';
248
249                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
250                 $challenge = bin2hex($challenge);
251                 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $_GET['dfrn_id'] . '</dfrn_id>'
252                         . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
253                 session_write_close();
254                 exit;
255                 
256         }
257
258 }