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