]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
cleanup untracked files
[friendica.git] / mod / dfrn_poll.php
1 <?php
2
3
4
5 require_once('include/items.php');
6 require_once('include/auth.php');
7
8
9 function dfrn_poll_init(&$a) {
10
11
12         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
13         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
14         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
15         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
16         $challenge       = ((x($_GET,'challenge'))       ? $_GET['challenge']            : '');
17         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
18         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
19
20         $direction = (-1);
21
22
23         if(strpos($dfrn_id,':') == 1) {
24                 $direction = intval(substr($dfrn_id,0,1));
25                 $dfrn_id   = substr($dfrn_id,2);
26         }
27
28         if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
29                 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] );
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(($type === 'profile') && (! strlen($sec))) {
37
38                 $sql_extra = '';
39                 switch($direction) {
40                         case (-1):
41                                 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),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`.`username`, `user`.`nickname` 
58                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
59                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
60                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
61                         dbesc($a->argv[1])
62                 );
63                 
64                 if(count($r)) {
65
66                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
67
68                         logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
69
70                         if(strlen($s)) {
71
72                                 $xml = simplexml_load_string($s);
73
74                                 if((int) $xml->status == 1) {
75                                         $_SESSION['authenticated'] = 1;
76                                         $_SESSION['visitor_id'] = $r[0]['id'];
77                                         $_SESSION['visitor_home'] = $r[0]['url'];
78                                         notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
79                                         // Visitors get 1 day session.
80                                         $session_id = session_id();
81                                         $expire = time() + 86400;
82                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
83                                                 dbesc($expire),
84                                                 dbesc($session_id)
85                                         ); 
86                                 }
87                         }
88                         $profile = $r[0]['nickname'];
89                         goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
90                 }
91                 goaway($a->get_baseurl());
92
93         }
94
95         if($type === 'profile-check') {
96
97                 if((strlen($challenge)) && (strlen($sec))) {
98
99                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
100                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
101                                 dbesc($sec)
102                         );
103                         if(! count($r)) {
104                                 xml_status(3, 'No ticket');
105                                 // NOTREACHED
106                         }
107                         $orig_id = $r[0]['dfrn_id'];
108                         if(strpos($orig_id, ':'))
109                                 $orig_id = substr($orig_id,2);
110
111                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
112                                 intval($r[0]['cid'])
113                         );
114                         if(! count($c)) {
115                                 xml_status(3, 'No profile');
116                         }
117                         $contact = $c[0];
118
119                         $sent_dfrn_id = hex2bin($dfrn_id);
120                         $challenge    = hex2bin($challenge);
121
122                         $final_dfrn_id = '';
123
124                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
125                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
126                                 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
127                         }
128                         else {
129                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
130                                 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
131                         }
132
133                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
134
135                         if(strpos($final_dfrn_id,':') == 1)
136                                 $final_dfrn_id = substr($final_dfrn_id,2);
137
138                         if($final_dfrn_id != $orig_id) {
139                                 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
140                                 // did not decode properly - cannot trust this site 
141                                 xml_status(3, 'Bad decryption');
142                         }
143
144                         header("Content-type: text/xml");
145                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
146                         killme();
147                         // NOTREACHED
148                 }
149                 else {
150                                 // old protocol
151
152                         switch($direction) {
153                                 case 1:
154                                         $dfrn_id = '0:' . $dfrn_id;
155                                         break;
156                                 case 0:
157                                         $dfrn_id = '1:' . $dfrn_id;
158                                         break;
159                                 default:
160                                         break;
161                         }
162
163
164                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
165                         $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
166                                 dbesc($dfrn_id));
167                         if(count($r)) {
168                                 xml_status(1);
169                                 return; // NOTREACHED
170                         }
171                         xml_status(0);
172                         return; // NOTREACHED
173                 }
174         }
175
176 }
177
178
179
180 function dfrn_poll_post(&$a) {
181
182         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? $_POST['dfrn_id']              : '');
183         $challenge    = ((x($_POST,'challenge'))    ? $_POST['challenge']            : '');
184         $url          = ((x($_POST,'url'))          ? $_POST['url']                  : '');
185         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
186
187         $direction    = (-1);
188         if(strpos($dfrn_id,':') == 1) {
189                 $direction = intval(substr($dfrn_id,0,1));
190                 $dfrn_id   = substr($dfrn_id,2);
191         }
192
193
194         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
195                 dbesc($dfrn_id),
196                 dbesc($challenge)
197         );
198
199         if(! count($r))
200                 killme();
201
202         $type = $r[0]['type'];
203         $last_update = $r[0]['last_update'];
204
205         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
206                 dbesc($dfrn_id),
207                 dbesc($challenge)
208         );
209
210
211         $sql_extra = '';
212         switch($direction) {
213                 case (-1):
214                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
215                         $my_id = $dfrn_id;
216                         break;
217                 case 0:
218                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
219                         $my_id = '1:' . $dfrn_id;
220                         break;
221                 case 1:
222                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
223                         $my_id = '0:' . $dfrn_id;
224                         break;
225                 default:
226                         goaway($a->get_baseurl());
227                         break; // NOTREACHED
228         }
229
230
231         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
232
233
234         if(! count($r))
235                 killme();
236
237         $owner_uid = $r[0]['uid'];
238         $contact_id = $r[0]['id']; 
239
240
241         if($type === 'reputation' && strlen($url)) {
242                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
243                         dbesc($url),
244                         intval($owner_uid)
245                 );
246                 $reputation = 0;
247                 $text = '';
248
249                 if(count($r)) {
250                         $reputation = $r[0]['rating'];
251                         $text = $r[0]['reason'];
252
253                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
254                                 $reputation = 0;
255                                 $text = '';
256                         }
257                 }
258
259                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
260                 <reputation>
261                         <url>$url</url>
262                         <rating>$reputation</rating>
263                         <description>$text</description>
264                 </reputation>
265                 ";
266                 killme();
267                 // NOTREACHED
268         }
269         else {
270                 header("Content-type: application/atom+xml");
271                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
272                 echo $o;
273                 killme();
274
275         }
276 }
277
278 function dfrn_poll_content(&$a) {
279
280         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
281         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
282         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
283         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
284         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
285         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
286
287         $direction = (-1);
288         if(strpos($dfrn_id,':') == 1) {
289                 $direction = intval(substr($dfrn_id,0,1));
290                 $dfrn_id = substr($dfrn_id,2);
291         }
292
293
294         if($dfrn_id != '') {
295                 // initial communication from external contact
296                 $hash = random_string();
297
298                 $status = 0;
299
300                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
301
302                 if($type !== 'profile') {
303                         $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
304                                 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
305                                 dbesc($hash),
306                                 dbesc($dfrn_id),
307                                 intval(time() + 60 ),
308                                 dbesc($type),
309                                 dbesc($last_update)
310                         );
311                 }
312                 $sql_extra = '';
313                 switch($direction) {
314                         case (-1):
315                                 if($type === 'profile')
316                                         $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
317                                 else
318                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
319                                 $my_id = $dfrn_id;
320                                 break;
321                         case 0:
322                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
323                                 $my_id = '1:' . $dfrn_id;
324                                 break;
325                         case 1:
326                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
327                                 $my_id = '0:' . $dfrn_id;
328                                 break;
329                         default:
330                                 goaway($a->get_baseurl());
331                                 break; // NOTREACHED
332                 }
333
334                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` 
335                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
336                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
337                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
338                         dbesc($a->argv[1])
339                 );
340
341                 if(count($r)) {
342
343                         $challenge = '';
344                         $encrypted_id = '';
345                         $id_str = $my_id . '.' . mt_rand(1000,9999);
346
347                         if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
348                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
349                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
350                         }
351                         else {
352                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
353                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
354                         }
355
356                         $challenge = bin2hex($challenge);
357                         $encrypted_id = bin2hex($encrypted_id);
358                 }
359                 else {
360                         $status = 1;
361                         $challenge = '';
362                         $encrypted_id = '';
363                 }
364
365                 if(($type === 'profile') && (strlen($sec))) {
366                         // URL reply
367
368                         $s = fetch_url($r[0]['poll'] 
369                                 . '?dfrn_id=' . $encrypted_id 
370                                 . '&type=profile-check'
371                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
372                                 . '&challenge=' . $challenge
373                                 . '&sec=' . $sec
374                         );
375
376                         logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
377
378                         if(strlen($s) && strstr($s,'<?xml')) {
379
380                                 $xml = simplexml_load_string($s);
381
382                                 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
383
384                                 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
385                                 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
386  
387                                 
388                                 if(((int) $xml->status == 0) && ($xml->challenge == $hash)  && ($xml->sec == $sec)) {
389                                         $_SESSION['authenticated'] = 1;
390                                         $_SESSION['visitor_id'] = $r[0]['id'];
391                                         $_SESSION['visitor_home'] = $r[0]['url'];
392                                         notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
393                                         // Visitors get 1 day session.
394                                         $session_id = session_id();
395                                         $expire = time() + 86400;
396                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
397                                                 dbesc($expire),
398                                                 dbesc($session_id)
399                                         ); 
400                                 }
401                                 $profile = $r[0]['nickname'];
402                                 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
403                         }
404                         goaway($a->get_baseurl());
405                         // NOTREACHED
406
407                 }
408                 else {
409                         // XML reply
410                         header("Content-type: text/xml");
411                         echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
412                                 . '<dfrn_poll>' . "\r\n"
413                                 . "\t" . '<status>' . $status . '</status>' . "\r\n"
414                                 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
415                                 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
416                                 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
417                                 . '</dfrn_poll>' . "\r\n" ;
418                         killme();
419                         // NOTREACHED
420                 }
421         }
422 }
423
424