]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
redirect to contact editor after introduction approval
[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
32
33         $importer = $r[0];
34
35         $feed = new SimplePie();
36         $feed->set_raw_data($data);
37         $feed->enable_order_by_date(false);
38         $feed->init();
39
40         foreach($feed->get_items() as $item) {
41
42                 $deleted = false;
43
44                 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
45                 if(isset($rawdelete[0]['attribs']['']['ref'])) {
46                         $uri = $rawthread[0]['attribs']['']['ref'];
47                         $deleted = true;
48                         if(isset($rawdelete[0]['attribs']['']['when'])) {
49                                 $when = $rawthread[0]['attribs']['']['when'];
50                                 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
51                         }
52                         else
53                                 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
54                 }
55                 if($deleted) {
56                         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
57                                 dbesc($uri),
58                                 intval($importer['uid'])
59                         );
60                         if(count($r)) {
61                                 if($r[0]['uri'] == $r[0]['parent-uri']) {
62                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
63                                                 WHERE `parent-uri` = '%s'"
64                                                 dbesc($when),
65                                                 dbesc($r[0]['uri'])
66                                         );
67                                 }
68                                 else {
69                                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' 
70                                                 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
71                                                 dbesc($when),
72                                                 dbesc($uri),
73                                                 intval($importer['uid'])
74                                         );
75                                 }
76                         }       
77                         continue;
78                 }
79
80                 $is_reply = false;              
81                 $item_id = $item->get_id();
82                 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
83                 if(isset($rawthread[0]['attribs']['']['ref'])) {
84                         $is_reply = true;
85                         $parent_uri = $rawthread[0]['attribs']['']['ref'];
86                 }
87
88
89                 if($is_reply) {
90                         if($feed->get_item_quantity() == 1) {
91                                 // remote reply to our post. Import and then notify everybody else.
92                                 $datarray = get_atom_elements($item);
93                                 $urn = explode(':',$parent_urn);
94                                 $datarray['type'] = 'remote-comment';
95                                 $datarray['parent-uri'] = $parent_uri;
96                                 $datarray['uid'] = $importer['uid'];
97                                 $datarray['contact-id'] = $importer['id'];
98                                 $posted_id = post_remote($a,$datarray);
99
100                                 $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
101                                         intval($posted_id),
102                                         intval($importer['uid'])
103                                 );
104                                 if(count($r)) {
105                                         $r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
106                                                 intval($importer['uid']),
107                                                 intval($r[0]['parent'])
108                                         );
109                                 }
110                                 $r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
111                                                 intval($importer['uid']),
112                                                 intval($posted_id)
113                                 );
114
115                                 $url = $a->get_baseurl();
116
117                                 proc_close(proc_open("php include/notifier.php $url comment-import $posted_id > remote-notify.log &", array(),$foo));
118
119                                 xml_status(0);
120                                 return;
121
122                         }
123                         else {
124                                 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
125
126                                 $item_id = $item->get_id();
127
128                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
129                                         dbesc($item_id),
130                                         intval($importer['uid'])
131                                 );
132                                 // FIXME update content if 'updated' changes
133                                 if(count($r)) {
134                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
135                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
136                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
137                                                         intval($allow[0]['data']),
138                                                         dbesc($item_id),
139                                                         intval($importer['uid'])
140                                                 );
141                                         }
142                                         continue;
143                                 }
144                                 $datarray = get_atom_elements($item);
145                                 $datarray['parent-uri'] = $parent_uri;
146                                 $datarray['uid'] = $importer['uid'];
147                                 $datarray['contact-id'] = $importer['id'];
148                                 $r = post_remote($a,$datarray);
149                                 continue;
150                         }
151                 }
152                 else {
153                         // Head post of a conversation. Have we seen it? If not, import it.
154
155                         $item_id = $item->get_id();
156                         $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
157                                 dbesc($item_id),
158                                 intval($importer['uid'])
159                         );
160                         if(count($r)) {
161                                 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
162                                 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
163                                         $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
164                                                 intval($allow[0]['data']),
165                                                 dbesc($item_id),
166                                                 intval($importer['uid'])
167                                         );
168                                 }
169                                 continue;
170                         }
171
172
173                         $datarray = get_atom_elements($item);
174                         $datarray['parent-uri'] = $item_id;
175                         $datarray['uid'] = $importer['uid'];
176                         $datarray['contact-id'] = $importer['id'];
177                         $r = post_remote($a,$datarray);
178                         continue;
179
180                 }
181         
182         }
183
184         xml_status(0);
185         killme();
186
187 }
188
189
190 function dfrn_notify_content(&$a) {
191
192         if(x($_GET,'dfrn_id')) {
193                 // initial communication from external contact
194                 $hash = random_string();
195
196                 $status = 0;
197
198                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
199
200                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
201                         VALUES( '%s', '%s', '%s') ",
202                         dbesc($hash),
203                         dbesc(notags(trim($_GET['dfrn_id']))),
204                         intval(time() + 60 )
205                 );
206
207                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
208                         dbesc($_GET['dfrn_id']));
209                 if((! count($r)) || (! strlen($r[0]['prvkey'])))
210                         $status = 1;
211
212                 $challenge = '';
213
214                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
215                 $challenge = bin2hex($challenge);
216                 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_notify><status>' .$status . '</status><dfrn_id>' . $_GET['dfrn_id'] . '</dfrn_id>'
217                         . '<challenge>' . $challenge . '</challenge></dfrn_notify>' . "\r\n" ;
218                 session_write_close();
219                 exit;
220                 
221         }
222
223 }