]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
c5c05134aefff58a8b9ec18677e2d70c1be09591
[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         $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'] = $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'] = $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'] = $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($arr) {
49
50         $arr['hash'] = random_string();
51         $arr['type'] = 'remote';
52         $arr['remote-name'] = notags(trim($arr['remote-name']));
53         $arr['remote-link'] = notags(trim($arr['remote-link']));
54         $arr['remote-avatar'] = notags(trim($arr['remote-avatar']));
55         $arr['owner-name'] = notags(trim($arr['owner-name']));
56         $arr['owner-link'] = notags(trim($arr['owner-link']));
57         $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
58 // must pass $a
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         $parent = $arr['parent_urn'];
72
73         unset($arr['parent_urn']);
74
75         $parent_id = 0;
76
77         dbesc_array($arr);
78
79         $r = q("INSERT INTO `item` (`" 
80                         . implode("`, `", array_keys($arr)) 
81                         . "`) VALUES ('" 
82                         . implode("', '", array_values($arr)) 
83                         . "')" );
84
85
86         $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
87                 dbesc($parent),
88                 intval($arr['uid'])
89         );
90         if(count($r))
91                 $parent_id = $r[0]['id'];
92         
93
94         $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
95                 $arr['remote-id'],
96                 intval($arr['uid'])
97         );
98         if(count($r))
99                 $current_post = $r[0]['id'];
100
101         $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
102                 intval($parent_id),
103                 intval($current_post)
104         );
105
106 }
107
108 function dfrn_notify_post(&$a) {
109 dbg(3);
110         $dfrn_id = notags(trim($_POST['dfrn_id']));
111         $challenge = notags(trim($_POST['challenge']));
112         $data = $_POST['data'];
113         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
114                 dbesc($dfrn_id),
115                 dbesc($challenge)
116         );
117         if(! count($r))
118                 xml_status(3);
119
120         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
121                 dbesc($dfrn_id),
122                 dbesc($challenge)
123         );
124
125         // find the local user who owns this relationship.
126
127         $r = q("SELECT `id`, `uid` FROM `contact` WHERE `issued-id` = '%s' LIMIT 1",
128                 dbesc($dfrn_id)
129         );
130         if(! count($r))
131                 xml_status(3);
132
133
134         $importer = $r[0];
135
136         $feed = new SimplePie();
137         $feed->set_raw_data($data);
138         $feed->enable_order_by_date(false);
139         $feed->init();
140
141         foreach($feed->get_items() as $item) {
142
143                 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
144                 print_r($rawdelete);
145                 if($deleted) {
146                         // pick out ref and when from attribs
147                         // check hasn't happened already, verify ownership and then process it
148
149
150                         continue;
151                 }
152
153                 $is_reply = false;              
154                 $item_id = $item->get_id();
155                 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
156                 if(isset($rawthread[0]['attribs']['']['ref'])) {
157                         $is_reply = true;
158                         $parent_urn = $rawthread[0]['attribs']['']['ref'];
159                 }
160
161
162                 if($is_reply) {
163                         if($x == ($total_items - 1)) {
164                                 // remote reply to our post. Import and then notify everybody else.
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` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
172                                         dbesc($item_id),
173                                         intval($importer['uid'])
174                                 );
175                                 if(count($r)) {
176                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
177                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
178                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
179                                                         intval($allow[0]['data']),
180                                                         dbesc($item_id)
181                                                 );
182                                         }
183                                         continue;
184                                 }
185                                 $datarray = get_atom_elements($item);
186                                 $datarray['parent_urn'] = $parent_urn;
187                                 $datarray['uid'] = $importer['uid'];
188                                 $datarray['contact-id'] = $importer['id'];
189                                 $r = post_remote($datarray);
190                                 continue;
191                         }
192                 }
193                 else {
194                         // Head post of a conversation. Have we seen it? If not, import it.
195
196                         $item_id = $item->get_id();
197                         $r = q("SELECT `uid` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
198                                 dbesc($item_id),
199                                 intval($importer['uid'])
200                         );
201                         if(count($r)) {
202                                 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
203                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
204                                         $r = q("UPDATE `item` SET `last-child` = %d WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
205                                                 intval($allow[0]['data']),
206                                                 dbesc($item_id)
207                                         );
208                                 }
209                                 continue;
210                         }
211
212
213                         $datarray = get_atom_elements($item);
214                         $datarray['parent_urn'] = $item_id;
215                         $datarray['uid'] = $importer['uid'];
216                         $datarray['contact-id'] = $importer['id'];
217                         $r = post_remote($datarray);
218                         continue;
219
220                 }
221         
222         }
223
224
225         killme();
226
227 }
228
229
230
231
232
233
234
235
236
237
238 function dfrn_notify_content(&$a) {
239
240         if(x($_GET,'dfrn_id')) {
241                 // initial communication from external contact
242                 $hash = random_string();
243
244                 $status = 0;
245
246                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
247
248                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
249                         VALUES( '%s', '%s', '%s') ",
250                         dbesc($hash),
251                         dbesc(notags(trim($_GET['dfrn_id']))),
252                         intval(time() + 60 )
253                 );
254
255                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 LIMIT 1",
256                         dbesc($_GET['dfrn_id']));
257                 if((! count($r)) || (! strlen($r[0]['prvkey'])))
258                         $status = 1;
259
260                 $challenge = '';
261
262                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
263                 $challenge = bin2hex($challenge);
264                 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $_GET['dfrn_id'] . '</dfrn_id>'
265                         . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
266                 session_write_close();
267                 exit;
268                 
269         }
270
271 }