]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
fc663f797709d2009fca50785056060ef5dbbadc
[friendica.git] / mod / dfrn_poll.php
1 <?php
2
3 require_once('include/items.php');
4 require_once('include/auth.php');
5
6
7 function dfrn_poll_init(&$a) {
8
9         $dfrn_id = '';
10
11         if(x($_GET,'dfrn_id'))
12                 $dfrn_id = $_GET['dfrn_id'];
13         if(x($_GET,'type'))
14                 $type = $_GET['type'];
15         if(x($_GET,'last_update'))
16                 $last_update = $_GET['last_update'];
17         $dfrn_version    = (float) $_GET['dfrn_version'] ;
18         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
19
20
21         $direction = (-1);
22
23
24         if(strpos($dfrn_id,':') == 1) {
25                 $direction = intval(substr($dfrn_id,0,1));
26                 $dfrn_id = substr($dfrn_id,2);
27         }
28
29         if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
30                 header("Content-type: application/atom+xml");
31                 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
32                 echo $o;
33                 killme();
34         }
35
36         if((x($type)) && ($type === 'profile')) {
37
38                 $sql_extra = '';
39                 switch($direction) {
40                         case (-1):
41                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($dfrn_id));
42                                 $my_id = $dfrn_id;
43                                 break;
44                         case 0:
45                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46                                 $my_id = '1:' . $dfrn_id;
47                                 break;
48                         case 1:
49                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
50                                 $my_id = '0:' . $dfrn_id;
51                                 break;
52                         default:
53                                 goaway($a->get_baseurl());
54                                 break; // NOTREACHED
55                 }
56
57                 $r = q("SELECT `contact`.*, `user`.`nickname` 
58                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
59                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 $sql_extra LIMIT 1");
60                 
61                 if(count($r)) {
62
63                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
64
65                         if(strlen($s)) {
66
67                                 $xml = simplexml_load_string($s);
68
69                                 if((int) $xml->status == 1) {
70                                         $_SESSION['authenticated'] = 1;
71                                         $_SESSION['visitor_id'] = $r[0]['id'];
72                                         notice( t('Hi ') . $r[0]['name'] . EOL);
73                                         // Visitors get 1 day session.
74                                         $session_id = session_id();
75                                         $expire = time() + 86400;
76                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
77                                                 dbesc($expire),
78                                                 dbesc($session_id)
79                                         ); 
80                                 }
81                         }
82                         $profile = $r[0]['nickname'];
83                         goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
84                 }
85                 goaway($a->get_baseurl());
86
87         }
88
89         if((x($type)) && ($type === 'profile-check')) {
90
91                 switch($direction) {
92                         case 1:
93                                 $dfrn_id = '0:' . $dfrn_id;
94                                 break;
95                         case 0:
96                                 $dfrn_id = '1:' . $dfrn_id;
97                                 break;
98                         default:
99                                 break;
100                 }
101                 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
102                 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
103                         dbesc($dfrn_id));
104                 if(count($r)) {
105                         xml_status(1);
106                         return; // NOTREACHED
107                 }
108                 xml_status(0);
109                 return; // NOTREACHED
110         }
111
112
113 }
114
115
116
117 function dfrn_poll_post(&$a) {
118
119         $dfrn_id      = $_POST['dfrn_id'];
120         $challenge    = $_POST['challenge'];
121         $url          = $_POST['url'];
122         $dfrn_version = (float) $_POST['dfrn_version'];
123
124         $direction    = (-1);
125         if(strpos($dfrn_id,':') == 1) {
126                 $direction = intval(substr($dfrn_id,0,1));
127                 $dfrn_id = substr($dfrn_id,2);
128         }
129
130
131         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
132                 dbesc($dfrn_id),
133                 dbesc($challenge)
134         );
135
136         if(! count($r))
137                 killme();
138
139         $type = $r[0]['type'];
140         $last_update = $r[0]['last_update'];
141
142         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
143                 dbesc($dfrn_id),
144                 dbesc($challenge)
145         );
146
147
148         $sql_extra = '';
149         switch($direction) {
150                 case (-1):
151                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
152                         $my_id = $dfrn_id;
153                         break;
154                 case 0:
155                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
156                         $my_id = '1:' . $dfrn_id;
157                         break;
158                 case 1:
159                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
160                         $my_id = '0:' . $dfrn_id;
161                         break;
162                 default:
163                         goaway($a->get_baseurl());
164                         break; // NOTREACHED
165         }
166
167
168         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
169
170
171         if(! count($r))
172                 killme();
173
174         $owner_uid = $r[0]['uid'];
175         $contact_id = $r[0]['id']; 
176
177
178         if($type === 'reputation' && strlen($url)) {
179                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
180                         dbesc($url),
181                         intval($owner_uid)
182                 );
183                 $reputation = 0;
184                 $text = '';
185
186                 if(count($r)) {
187                         $reputation = $r[0]['rating'];
188                         $text = $r[0]['reason'];
189
190                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
191                                 $reputation = 0;
192                                 $text = '';
193                         }
194                 }
195
196                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
197                 <reputation>
198                         <url>$url</url>
199                         <rating>$reputation</rating>
200                         <description>$text</description>
201                 </reputation>
202                 ";
203                 killme();
204                 // NOTREACHED
205         }
206         else {
207                 header("Content-type: application/atom+xml");
208                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
209                 echo $o;
210                 killme();
211
212         }
213 }
214
215 function dfrn_poll_content(&$a) {
216
217
218         $dfrn_id = '';
219         $type = 'data';
220
221         if(x($_GET,'dfrn_id'))
222                 $dfrn_id = $_GET['dfrn_id'];
223         if(x($_GET,'type'))
224                 $type = $_GET['type'];
225         if(x($_GET,'last_update'))
226                 $last_update = $_GET['last_update'];
227
228         $dfrn_version = (float) $_GET['dfrn_version'];
229
230         $direction = (-1);
231         if(strpos($dfrn_id,':') == 1) {
232                 $direction = intval(substr($dfrn_id,0,1));
233                 $dfrn_id = substr($dfrn_id,2);
234         }
235
236
237         if($dfrn_id != '') {
238                 // initial communication from external contact
239                 $hash = random_string();
240
241                 $status = 0;
242
243                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
244
245                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
246                         VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
247                         dbesc($hash),
248                         dbesc($dfrn_id),
249                         intval(time() + 60 ),
250                         dbesc($type),
251                         dbesc($last_update)
252                 );
253
254
255                 $sql_extra = '';
256                 switch($direction) {
257                         case (-1):
258                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
259                                 $my_id = $dfrn_id;
260                                 break;
261                         case 0:
262                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
263                                 $my_id = '1:' . $dfrn_id;
264                                 break;
265                         case 1:
266                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
267                                 $my_id = '0:' . $dfrn_id;
268                                 break;
269                         default:
270                                 goaway($a->get_baseurl());
271                                 break; // NOTREACHED
272                 }
273
274
275
276
277                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
278
279                 if(count($r)) {
280
281                         $challenge = '';
282                         $encrypted_id = '';
283                         $id_str = $my_id . '.' . mt_rand(1000,9999);
284
285
286                         if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
287                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
288                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
289                         }
290                         else {
291                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
292                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
293                         }
294
295                         $challenge = bin2hex($challenge);
296                         $encrypted_id = bin2hex($encrypted_id);
297                 }
298                 else {
299                         $status = 1;
300                 }
301                 header("Content-type: text/xml");
302                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
303                         . '<dfrn_poll>' . "\r\n"
304                         . "\t" . '<status>' .$status . '</status>' . "\r\n"
305                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
306                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
307                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
308                         . '</dfrn_poll>' . "\r\n" ;
309                 killme();
310         }
311 }
312
313