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