]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
4b25efe418c0b130824aa653f4d13b60e77e0bce
[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' && $dfrn_version < 2.2 ) {
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         $sec          = ((x($_POST,'sec'))          ? $_POST['sec']                  : '');
186         $ptype        = ((x($_POST,'type'))         ? $_POST['type']                  : '');
187         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
188
189         if($ptype === 'profile-check') {
190
191                 if((strlen($challenge)) && (strlen($sec))) {
192
193                         logger('dfrn_poll: POST: profile-check');
194  
195                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
196                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
197                                 dbesc($sec)
198                         );
199                         if(! count($r)) {
200                                 xml_status(3, 'No ticket');
201                                 // NOTREACHED
202                         }
203                         $orig_id = $r[0]['dfrn_id'];
204                         if(strpos($orig_id, ':'))
205                                 $orig_id = substr($orig_id,2);
206
207                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
208                                 intval($r[0]['cid'])
209                         );
210                         if(! count($c)) {
211                                 xml_status(3, 'No profile');
212                         }
213                         $contact = $c[0];
214
215                         $sent_dfrn_id = hex2bin($dfrn_id);
216                         $challenge    = hex2bin($challenge);
217
218                         $final_dfrn_id = '';
219
220                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
221                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
222                                 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
223                         }
224                         else {
225                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
226                                 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
227                         }
228
229                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
230
231                         if(strpos($final_dfrn_id,':') == 1)
232                                 $final_dfrn_id = substr($final_dfrn_id,2);
233
234                         if($final_dfrn_id != $orig_id) {
235                                 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
236                                 // did not decode properly - cannot trust this site 
237                                 xml_status(3, 'Bad decryption');
238                         }
239
240                         header("Content-type: text/xml");
241                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
242                         killme();
243                         // NOTREACHED
244                 }
245
246         }
247
248         $direction    = (-1);
249         if(strpos($dfrn_id,':') == 1) {
250                 $direction = intval(substr($dfrn_id,0,1));
251                 $dfrn_id   = substr($dfrn_id,2);
252         }
253
254
255         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
256                 dbesc($dfrn_id),
257                 dbesc($challenge)
258         );
259
260         if(! count($r))
261                 killme();
262
263         $type = $r[0]['type'];
264         $last_update = $r[0]['last_update'];
265
266         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
267                 dbesc($dfrn_id),
268                 dbesc($challenge)
269         );
270
271
272         $sql_extra = '';
273         switch($direction) {
274                 case (-1):
275                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
276                         $my_id = $dfrn_id;
277                         break;
278                 case 0:
279                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
280                         $my_id = '1:' . $dfrn_id;
281                         break;
282                 case 1:
283                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
284                         $my_id = '0:' . $dfrn_id;
285                         break;
286                 default:
287                         goaway($a->get_baseurl());
288                         break; // NOTREACHED
289         }
290
291
292         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
293
294
295         if(! count($r))
296                 killme();
297
298         $owner_uid = $r[0]['uid'];
299         $contact_id = $r[0]['id']; 
300
301
302         if($type === 'reputation' && strlen($url)) {
303                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
304                         dbesc($url),
305                         intval($owner_uid)
306                 );
307                 $reputation = 0;
308                 $text = '';
309
310                 if(count($r)) {
311                         $reputation = $r[0]['rating'];
312                         $text = $r[0]['reason'];
313
314                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
315                                 $reputation = 0;
316                                 $text = '';
317                         }
318                 }
319
320                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
321                 <reputation>
322                         <url>$url</url>
323                         <rating>$reputation</rating>
324                         <description>$text</description>
325                 </reputation>
326                 ";
327                 killme();
328                 // NOTREACHED
329         }
330         else {
331                 header("Content-type: application/atom+xml");
332                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
333                 echo $o;
334                 killme();
335
336         }
337 }
338
339 function dfrn_poll_content(&$a) {
340
341         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
342         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
343         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
344         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
345         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
346         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
347
348         $direction = (-1);
349         if(strpos($dfrn_id,':') == 1) {
350                 $direction = intval(substr($dfrn_id,0,1));
351                 $dfrn_id = substr($dfrn_id,2);
352         }
353
354
355         if($dfrn_id != '') {
356                 // initial communication from external contact
357                 $hash = random_string();
358
359                 $status = 0;
360
361                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
362
363                 if($type !== 'profile') {
364                         $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
365                                 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
366                                 dbesc($hash),
367                                 dbesc($dfrn_id),
368                                 intval(time() + 60 ),
369                                 dbesc($type),
370                                 dbesc($last_update)
371                         );
372                 }
373                 $sql_extra = '';
374                 switch($direction) {
375                         case (-1):
376                                 if($type === 'profile')
377                                         $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
378                                 else
379                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
380                                 $my_id = $dfrn_id;
381                                 break;
382                         case 0:
383                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
384                                 $my_id = '1:' . $dfrn_id;
385                                 break;
386                         case 1:
387                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
388                                 $my_id = '0:' . $dfrn_id;
389                                 break;
390                         default:
391                                 goaway($a->get_baseurl());
392                                 break; // NOTREACHED
393                 }
394
395                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` 
396                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
397                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
398                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
399                         dbesc($a->argv[1])
400                 );
401
402                 if(count($r)) {
403
404                         $challenge = '';
405                         $encrypted_id = '';
406                         $id_str = $my_id . '.' . mt_rand(1000,9999);
407
408                         if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
409                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
410                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
411                         }
412                         else {
413                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
414                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
415                         }
416
417                         $challenge = bin2hex($challenge);
418                         $encrypted_id = bin2hex($encrypted_id);
419                 }
420                 else {
421                         $status = 1;
422                         $challenge = '';
423                         $encrypted_id = '';
424                 }
425
426                 if(($type === 'profile') && (strlen($sec))) {
427
428                         // URL reply
429
430                         if($dfrn_version < 2.2) {
431                                 $s = fetch_url($r[0]['poll'] 
432                                         . '?dfrn_id=' . $encrypted_id 
433                                         . '&type=profile-check'
434                                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
435                                         . '&challenge=' . $challenge
436                                         . '&sec=' . $sec
437                                 );
438                         }
439                         else {
440                                 $s = post_url($r[0]['poll'], array(
441                                         'dfrn_id' => $encrypted_id,
442                                         'type' => 'profile-check',
443                                         'dfrn_version' => DFRN_PROTOCOL_VERSION,
444                                         'challenge' => $challenge,
445                                         'sec' => $sec
446                                 ));
447                         }
448
449                         $profile = $r[0]['nickname'];
450
451                         switch($destination_url) {
452                                 case 'profile':
453                                         $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
454                                         break;
455                                 case 'photos':
456                                         $dest = $a->get_baseurl() . '/photos/' . $profile;
457                                         break;
458                                 case 'status':
459                                 case '':
460                                         $dest = $a->get_baseurl() . '/profile/' . $profile;
461                                         break;          
462                                 default:
463                                         $dest = $destination_url;
464                                         break;
465                         }
466
467                         logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
468
469                         if(strlen($s) && strstr($s,'<?xml')) {
470
471                                 $xml = simplexml_load_string($s);
472
473                                 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
474
475                                 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
476                                 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
477  
478                                 
479                                 if(((int) $xml->status == 0) && ($xml->challenge == $hash)  && ($xml->sec == $sec)) {
480                                         $_SESSION['authenticated'] = 1;
481                                         $_SESSION['visitor_id'] = $r[0]['id'];
482                                         $_SESSION['visitor_home'] = $r[0]['url'];
483                                         notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
484                                         // Visitors get 1 day session.
485                                         $session_id = session_id();
486                                         $expire = time() + 86400;
487                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
488                                                 dbesc($expire),
489                                                 dbesc($session_id)
490                                         ); 
491                                 }
492                         
493                                 goaway($dest);
494                         }
495                         goaway($dest);
496                         // NOTREACHED
497
498                 }
499                 else {
500                         // XML reply
501                         header("Content-type: text/xml");
502                         echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
503                                 . '<dfrn_poll>' . "\r\n"
504                                 . "\t" . '<status>' . $status . '</status>' . "\r\n"
505                                 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
506                                 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
507                                 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
508                                 . '</dfrn_poll>' . "\r\n" ;
509                         killme();
510                         // NOTREACHED
511                 }
512         }
513 }
514
515