Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

139 rindas
4.8 KiB

  1. <?php
  2. use Friendica\App;
  3. use Friendica\Core\Logger;
  4. use Friendica\Core\Protocol;
  5. use Friendica\Database\DBA;
  6. use Friendica\Model\Contact;
  7. use Friendica\Protocol\OStatus;
  8. use Friendica\Util\Strings;
  9. use Friendica\Core\System;
  10. function hub_return($valid, $body)
  11. {
  12. if ($valid) {
  13. header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
  14. echo $body;
  15. } else {
  16. System::httpExit(404);
  17. }
  18. killme();
  19. }
  20. // when receiving an XML feed, always return OK
  21. function hub_post_return()
  22. {
  23. System::httpExit(200);
  24. }
  25. function pubsub_init(App $a)
  26. {
  27. $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
  28. $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
  29. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  30. $hub_mode = Strings::escapeTags(trim(defaults($_GET, 'hub_mode', '')));
  31. $hub_topic = Strings::escapeTags(trim(defaults($_GET, 'hub_topic', '')));
  32. $hub_challenge = Strings::escapeTags(trim(defaults($_GET, 'hub_challenge', '')));
  33. $hub_lease = Strings::escapeTags(trim(defaults($_GET, 'hub_lease_seconds', '')));
  34. $hub_verify = Strings::escapeTags(trim(defaults($_GET, 'hub_verify_token', '')));
  35. Logger::log('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
  36. Logger::log('Data: ' . print_r($_GET,true), Logger::DATA);
  37. $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
  38. $owner = DBA::selectFirst('user', ['uid'], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
  39. if (!DBA::isResult($owner)) {
  40. Logger::log('Local account not found: ' . $nick);
  41. hub_return(false, '');
  42. }
  43. $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
  44. if (!empty($hub_verify)) {
  45. $condition['hub-verify'] = $hub_verify;
  46. }
  47. $contact = DBA::selectFirst('contact', ['id', 'poll'], $condition);
  48. if (!DBA::isResult($contact)) {
  49. Logger::log('Contact ' . $contact_id . ' not found.');
  50. hub_return(false, '');
  51. }
  52. if (!empty($hub_topic) && !Strings::compareLink($hub_topic, $contact['poll'])) {
  53. Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
  54. hub_return(false, '');
  55. }
  56. // We must initiate an unsubscribe request with a verify_token.
  57. // Don't allow outsiders to unsubscribe us.
  58. if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
  59. Logger::log('Bogus unsubscribe');
  60. hub_return(false, '');
  61. }
  62. if (!empty($hub_mode)) {
  63. DBA::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
  64. Logger::log($hub_mode . ' success for contact ' . $contact_id . '.');
  65. }
  66. hub_return(true, $hub_challenge);
  67. }
  68. }
  69. function pubsub_post(App $a)
  70. {
  71. $xml = file_get_contents('php://input');
  72. Logger::log('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $a->cmd . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']);
  73. Logger::log('Data: ' . $xml, Logger::DATA);
  74. $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
  75. $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
  76. $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
  77. if (!DBA::isResult($importer)) {
  78. hub_post_return();
  79. }
  80. $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
  81. $contact = DBA::selectFirst('contact', [], $condition);
  82. if (!DBA::isResult($contact)) {
  83. $author = OStatus::salmonAuthor($xml, $importer);
  84. if (!empty($author['contact-id'])) {
  85. $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
  86. $contact = DBA::selectFirst('contact', [], $condition);
  87. Logger::log('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.');
  88. }
  89. if (!DBA::isResult($contact)) {
  90. Logger::log('Contact ' . $author["author-link"] . ' (' . $contact_id . ') for user ' . $nick . " wasn't found - ignored. XML: " . $xml);
  91. hub_post_return();
  92. }
  93. }
  94. if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
  95. Logger::log('Contact ' . $contact['id'] . ' is not expected to share with us - ignored.');
  96. hub_post_return();
  97. }
  98. // We import feeds from OStatus, Friendica and ATOM/RSS.
  99. /// @todo Check if Friendica posts really arrive here - otherwise we can discard some stuff
  100. if (!in_array($contact['network'], [Protocol::OSTATUS, Protocol::DFRN, Protocol::FEED])) {
  101. hub_post_return();
  102. }
  103. Logger::log('Import item for ' . $nick . ' from ' . $contact['nick'] . ' (' . $contact['id'] . ')');
  104. $feedhub = '';
  105. consume_feed($xml, $importer, $contact, $feedhub);
  106. // do it a second time for DFRN so that any children find their parents.
  107. if ($contact['network'] === Protocol::DFRN) {
  108. consume_feed($xml, $importer, $contact, $feedhub);
  109. }
  110. hub_post_return();
  111. }