]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
Limit the number of feed entries (Can solve resub problems)
[friendica.git] / mod / dfrn_poll.php
1 <?php
2
3 use Friendica\App;
4
5 require_once('include/items.php');
6 require_once('include/auth.php');
7 require_once('include/dfrn.php');
8 require_once('include/ostatus.php');
9
10 function dfrn_poll_init(App $a) {
11         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
12         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
13         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
14         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
15         $challenge       = ((x($_GET,'challenge'))       ? $_GET['challenge']            : '');
16         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
17         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
18         $perm            = ((x($_GET,'perm'))            ? $_GET['perm']                 : 'r');
19         $quiet                   = ((x($_GET,'quiet'))                   ? true                                                  : false);
20
21         // Possibly it is an OStatus compatible server that requests a user feed
22         if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
23                 $nickname = $a->argv[1];
24                 header("Content-type: application/atom+xml");
25                 echo ostatus::feed($a, $nickname, $last_update, 10);
26                 killme();
27         }
28
29         $direction = (-1);
30
31
32         if(strpos($dfrn_id,':') == 1) {
33                 $direction = intval(substr($dfrn_id,0,1));
34                 $dfrn_id   = substr($dfrn_id,2);
35         }
36
37         $hidewall = false;
38
39         if(($dfrn_id === '') && (! x($_POST,'dfrn_id'))) {
40                 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
41                         http_status_exit(403);
42                 }
43
44                 $user = '';
45                 if($a->argc > 1) {
46                         $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
47                                 dbesc($a->argv[1])
48                         );
49                         if (!$r)
50                                 http_status_exit(404);
51
52                         $hidewall = ($r[0]['hidewall'] && !local_user());
53
54                         $user = $r[0]['nickname'];
55                 }
56
57                 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
58                 header("Content-type: application/atom+xml");
59                 echo dfrn::feed('', $user,$last_update, 0, $hidewall);
60                 killme();
61         }
62
63         if(($type === 'profile') && (! strlen($sec))) {
64
65                 $sql_extra = '';
66                 switch($direction) {
67                         case (-1):
68                                 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
69                                 $my_id = $dfrn_id;
70                                 break;
71                         case 0:
72                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
73                                 $my_id = '1:' . $dfrn_id;
74                                 break;
75                         case 1:
76                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
77                                 $my_id = '0:' . $dfrn_id;
78                                 break;
79                         default:
80                                 goaway(z_root());
81                                 break; // NOTREACHED
82                 }
83
84                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
85                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
86                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
87                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
88                         dbesc($a->argv[1])
89                 );
90
91                 if (dbm::is_result($r)) {
92
93                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
94
95                         logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
96
97                         if(strlen($s)) {
98
99                                 $xml = parse_xml_string($s);
100
101                                 if((int) $xml->status == 1) {
102                                         $_SESSION['authenticated'] = 1;
103                                         if(! x($_SESSION,'remote'))
104                                                 $_SESSION['remote'] = array();
105
106                                         $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
107
108                                         $_SESSION['visitor_id'] = $r[0]['id'];
109                                         $_SESSION['visitor_home'] = $r[0]['url'];
110                                         $_SESSION['visitor_handle'] = $r[0]['addr'];
111                                         $_SESSION['visitor_visiting'] = $r[0]['uid'];
112                                         if(!$quiet)
113                                                 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
114                                         // Visitors get 1 day session.
115                                         $session_id = session_id();
116                                         $expire = time() + 86400;
117                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
118                                                 dbesc($expire),
119                                                 dbesc($session_id)
120                                         );
121                                 }
122                         }
123                         $profile = $r[0]['nickname'];
124                         goaway((strlen($destination_url)) ? $destination_url : App::get_baseurl() . '/profile/' . $profile);
125                 }
126                 goaway(z_root());
127
128         }
129
130         if($type === 'profile-check' && $dfrn_version < 2.2 ) {
131
132                 if((strlen($challenge)) && (strlen($sec))) {
133
134                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
135                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
136                                 dbesc($sec)
137                         );
138                         if (! dbm::is_result($r)) {
139                                 xml_status(3, 'No ticket');
140                                 // NOTREACHED
141                         }
142                         $orig_id = $r[0]['dfrn_id'];
143                         if(strpos($orig_id, ':'))
144                                 $orig_id = substr($orig_id,2);
145
146                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
147                                 intval($r[0]['cid'])
148                         );
149                         if (! dbm::is_result($c)) {
150                                 xml_status(3, 'No profile');
151                         }
152                         $contact = $c[0];
153
154                         $sent_dfrn_id = hex2bin($dfrn_id);
155                         $challenge    = hex2bin($challenge);
156
157                         $final_dfrn_id = '';
158
159                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
160                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
161                                 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
162                         }
163                         else {
164                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
165                                 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
166                         }
167
168                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
169
170                         if(strpos($final_dfrn_id,':') == 1)
171                                 $final_dfrn_id = substr($final_dfrn_id,2);
172
173                         if($final_dfrn_id != $orig_id) {
174                                 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
175                                 // did not decode properly - cannot trust this site
176                                 xml_status(3, 'Bad decryption');
177                         }
178
179                         header("Content-type: text/xml");
180                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
181                         killme();
182                         // NOTREACHED
183                 }
184                 else {
185                                 // old protocol
186
187                         switch($direction) {
188                                 case 1:
189                                         $dfrn_id = '0:' . $dfrn_id;
190                                         break;
191                                 case 0:
192                                         $dfrn_id = '1:' . $dfrn_id;
193                                         break;
194                                 default:
195                                         break;
196                         }
197
198
199                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
200                         $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
201                                 dbesc($dfrn_id));
202                         if (dbm::is_result($r)) {
203                                 xml_status(1);
204                                 return; // NOTREACHED
205                         }
206                         xml_status(0);
207                         return; // NOTREACHED
208                 }
209         }
210
211 }
212
213
214
215 function dfrn_poll_post(App $a) {
216
217         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? $_POST['dfrn_id']              : '');
218         $challenge    = ((x($_POST,'challenge'))    ? $_POST['challenge']            : '');
219         $url          = ((x($_POST,'url'))          ? $_POST['url']                  : '');
220         $sec          = ((x($_POST,'sec'))          ? $_POST['sec']                  : '');
221         $ptype        = ((x($_POST,'type'))         ? $_POST['type']                 : '');
222         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
223         $perm         = ((x($_POST,'perm'))         ? $_POST['perm']                 : 'r');
224
225         if($ptype === 'profile-check') {
226
227                 if((strlen($challenge)) && (strlen($sec))) {
228
229                         logger('dfrn_poll: POST: profile-check');
230
231                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
232                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
233                                 dbesc($sec)
234                         );
235                         if (! dbm::is_result($r)) {
236                                 xml_status(3, 'No ticket');
237                                 // NOTREACHED
238                         }
239                         $orig_id = $r[0]['dfrn_id'];
240                         if(strpos($orig_id, ':'))
241                                 $orig_id = substr($orig_id,2);
242
243                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
244                                 intval($r[0]['cid'])
245                         );
246                         if (! dbm::is_result($c)) {
247                                 xml_status(3, 'No profile');
248                         }
249                         $contact = $c[0];
250
251                         $sent_dfrn_id = hex2bin($dfrn_id);
252                         $challenge    = hex2bin($challenge);
253
254                         $final_dfrn_id = '';
255
256                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
257                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
258                                 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
259                         }
260                         else {
261                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
262                                 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
263                         }
264
265                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
266
267                         if(strpos($final_dfrn_id,':') == 1)
268                                 $final_dfrn_id = substr($final_dfrn_id,2);
269
270                         if($final_dfrn_id != $orig_id) {
271                                 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
272                                 // did not decode properly - cannot trust this site
273                                 xml_status(3, 'Bad decryption');
274                         }
275
276                         header("Content-type: text/xml");
277                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
278                         killme();
279                         // NOTREACHED
280                 }
281
282         }
283
284         $direction    = (-1);
285         if(strpos($dfrn_id,':') == 1) {
286                 $direction = intval(substr($dfrn_id,0,1));
287                 $dfrn_id   = substr($dfrn_id,2);
288         }
289
290
291         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
292                 dbesc($dfrn_id),
293                 dbesc($challenge)
294         );
295
296         if (! dbm::is_result($r)) {
297                 killme();
298         }
299
300         $type = $r[0]['type'];
301         $last_update = $r[0]['last_update'];
302
303         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
304                 dbesc($dfrn_id),
305                 dbesc($challenge)
306         );
307
308
309         $sql_extra = '';
310         switch($direction) {
311                 case (-1):
312                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
313                         $my_id = $dfrn_id;
314                         break;
315                 case 0:
316                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
317                         $my_id = '1:' . $dfrn_id;
318                         break;
319                 case 1:
320                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
321                         $my_id = '0:' . $dfrn_id;
322                         break;
323                 default:
324                         goaway(z_root());
325                         break; // NOTREACHED
326         }
327
328
329         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
330
331
332         if (! dbm::is_result($r)) {
333                 killme();
334         }
335
336         $contact = $r[0];
337         $owner_uid = $r[0]['uid'];
338         $contact_id = $r[0]['id'];
339
340
341         if($type === 'reputation' && strlen($url)) {
342                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
343                         dbesc($url),
344                         intval($owner_uid)
345                 );
346                 $reputation = 0;
347                 $text = '';
348
349                 if (dbm::is_result($r)) {
350                         $reputation = $r[0]['rating'];
351                         $text = $r[0]['reason'];
352
353                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
354                                 $reputation = 0;
355                                 $text = '';
356                         }
357                 }
358
359                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
360                 <reputation>
361                         <url>$url</url>
362                         <rating>$reputation</rating>
363                         <description>$text</description>
364                 </reputation>
365                 ";
366                 killme();
367                 // NOTREACHED
368         }
369         else {
370
371                 // Update the writable flag if it changed
372                 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
373                 if($dfrn_version >= 2.21) {
374                         if($perm === 'rw')
375                                 $writable = 1;
376                         else
377                                 $writable = 0;
378
379                         if($writable !=  $contact['writable']) {
380                                 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
381                                         intval($writable),
382                                         intval($contact_id)
383                                 );
384                         }
385                 }
386
387                 header("Content-type: application/atom+xml");
388                 $o = dfrn::feed($dfrn_id, $a->argv[1], $last_update, $direction);
389                 echo $o;
390                 killme();
391
392         }
393 }
394
395 function dfrn_poll_content(App $a) {
396
397         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
398         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
399         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
400         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
401         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
402         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
403         $perm            = ((x($_GET,'perm'))            ? $_GET['perm']                 : 'r');
404         $quiet           = ((x($_GET,'quiet'))           ? true                          : false);
405
406         $direction = (-1);
407         if(strpos($dfrn_id,':') == 1) {
408                 $direction = intval(substr($dfrn_id,0,1));
409                 $dfrn_id = substr($dfrn_id,2);
410         }
411
412
413         if($dfrn_id != '') {
414                 // initial communication from external contact
415                 $hash = random_string();
416
417                 $status = 0;
418
419                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
420
421                 if($type !== 'profile') {
422                         $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
423                                 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
424                                 dbesc($hash),
425                                 dbesc($dfrn_id),
426                                 intval(time() + 60 ),
427                                 dbesc($type),
428                                 dbesc($last_update)
429                         );
430                 }
431                 $sql_extra = '';
432                 switch($direction) {
433                         case (-1):
434                                 if($type === 'profile')
435                                         $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
436                                 else
437                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
438                                 $my_id = $dfrn_id;
439                                 break;
440                         case 0:
441                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
442                                 $my_id = '1:' . $dfrn_id;
443                                 break;
444                         case 1:
445                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
446                                 $my_id = '0:' . $dfrn_id;
447                                 break;
448                         default:
449                                 goaway(z_root());
450                                 break; // NOTREACHED
451                 }
452
453                 $nickname = $a->argv[1];
454
455                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
456                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
457                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
458                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
459                         dbesc($nickname)
460                 );
461
462                 if (dbm::is_result($r)) {
463
464                         $challenge = '';
465                         $encrypted_id = '';
466                         $id_str = $my_id . '.' . mt_rand(1000,9999);
467
468                         if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
469                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
470                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
471                         }
472                         else {
473                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
474                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
475                         }
476
477                         $challenge = bin2hex($challenge);
478                         $encrypted_id = bin2hex($encrypted_id);
479                 }
480                 else {
481                         $status = 1;
482                         $challenge = '';
483                         $encrypted_id = '';
484                 }
485
486                 if(($type === 'profile') && (strlen($sec))) {
487
488                         // URL reply
489
490                         if($dfrn_version < 2.2) {
491                                 $s = fetch_url($r[0]['poll']
492                                         . '?dfrn_id=' . $encrypted_id
493                                         . '&type=profile-check'
494                                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
495                                         . '&challenge=' . $challenge
496                                         . '&sec=' . $sec
497                                 );
498                         }
499                         else {
500                                 $s = post_url($r[0]['poll'], array(
501                                         'dfrn_id' => $encrypted_id,
502                                         'type' => 'profile-check',
503                                         'dfrn_version' => DFRN_PROTOCOL_VERSION,
504                                         'challenge' => $challenge,
505                                         'sec' => $sec
506                                 ));
507                         }
508
509                         $profile = ((dbm::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
510
511                         switch($destination_url) {
512                                 case 'profile':
513                                         $dest = App::get_baseurl() . '/profile/' . $profile . '?f=&tab=profile';
514                                         break;
515                                 case 'photos':
516                                         $dest = App::get_baseurl() . '/photos/' . $profile;
517                                         break;
518                                 case 'status':
519                                 case '':
520                                         $dest = App::get_baseurl() . '/profile/' . $profile;
521                                         break;
522                                 default:
523                                         $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
524                                         $dest = $destination_url . $appendix;
525                                         break;
526                         }
527
528                         logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
529
530                         if(strlen($s) && strstr($s,'<?xml')) {
531
532                                 $xml = parse_xml_string($s);
533
534                                 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
535
536                                 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
537                                 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
538
539
540                                 if(((int) $xml->status == 0) && ($xml->challenge == $hash)  && ($xml->sec == $sec)) {
541                                         $_SESSION['authenticated'] = 1;
542                                         if(! x($_SESSION,'remote'))
543                                                 $_SESSION['remote'] = array();
544                                         $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
545                                         $_SESSION['visitor_id'] = $r[0]['id'];
546                                         $_SESSION['visitor_home'] = $r[0]['url'];
547                                         $_SESSION['visitor_visiting'] = $r[0]['uid'];
548                                         if(!$quiet)
549                                                 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
550                                         // Visitors get 1 day session.
551                                         $session_id = session_id();
552                                         $expire = time() + 86400;
553                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
554                                                 dbesc($expire),
555                                                 dbesc($session_id)
556                                         );
557                                 }
558
559                                 goaway($dest);
560                         }
561                         goaway($dest);
562                         // NOTREACHED
563
564                 }
565                 else {
566                         // XML reply
567                         header("Content-type: text/xml");
568                         echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
569                                 . '<dfrn_poll>' . "\r\n"
570                                 . "\t" . '<status>' . $status . '</status>' . "\r\n"
571                                 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
572                                 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
573                                 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
574                                 . '</dfrn_poll>' . "\r\n" ;
575                         killme();
576                         // NOTREACHED
577                 }
578         }
579 }