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