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_lease = Strings::escapeTags(trim(defaults($_GET, 'hub_lease_seconds', '')));
40 $hub_verify = Strings::escapeTags(trim(defaults($_GET, 'hub_verify_token', '')));
42 Logger::log('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
43 Logger::log('Data: ' . print_r($_GET,true), Logger::DATA);
45 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
47 $owner = DBA::selectFirst('user', ['uid'], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
48 if (!DBA::isResult($owner)) {
49 Logger::log('Local account not found: ' . $nick);
50 hub_return(false, '');
53 $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
55 if (!empty($hub_verify)) {
56 $condition['hub-verify'] = $hub_verify;
59 $contact = DBA::selectFirst('contact', ['id', 'poll'], $condition);
60 if (!DBA::isResult($contact)) {
61 Logger::log('Contact ' . $contact_id . ' not found.');
62 hub_return(false, '');
65 if (!empty($hub_topic) && !Strings::compareLink($hub_topic, $contact['poll'])) {
66 Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
67 hub_return(false, '');
70 // We must initiate an unsubscribe request with a verify_token.
71 // Don't allow outsiders to unsubscribe us.
73 if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
74 Logger::log('Bogus unsubscribe');
75 hub_return(false, '');
78 if (!empty($hub_mode)) {
79 DBA::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
80 Logger::log($hub_mode . ' success for contact ' . $contact_id . '.');
82 hub_return(true, $hub_challenge);
86 function pubsub_post(App $a)
88 $xml = file_get_contents('php://input');
90 Logger::log('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $a->cmd . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']);
91 Logger::log('Data: ' . $xml, Logger::DATA);
93 $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
94 $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
96 $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
97 if (!DBA::isResult($importer)) {
101 $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
102 $contact = DBA::selectFirst('contact', [], $condition);
104 if (!DBA::isResult($contact)) {
105 $author = OStatus::salmonAuthor($xml, $importer);
106 if (!empty($author['contact-id'])) {
107 $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
108 $contact = DBA::selectFirst('contact', [], $condition);
109 Logger::log('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.');
111 if (!DBA::isResult($contact)) {
112 Logger::log('Contact ' . $author["author-link"] . ' (' . $contact_id . ') for user ' . $nick . " wasn't found - ignored. XML: " . $xml);
117 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
118 Logger::log('Contact ' . $contact['id'] . ' is not expected to share with us - ignored.');
122 // We import feeds from OStatus, Friendica and ATOM/RSS.
123 /// @todo Check if Friendica posts really arrive here - otherwise we can discard some stuff
124 if (!in_array($contact['network'], [Protocol::OSTATUS, Protocol::DFRN, Protocol::FEED])) {
128 Logger::log('Import item for ' . $nick . ' from ' . $contact['nick'] . ' (' . $contact['id'] . ')');
130 consume_feed($xml, $importer, $contact, $feedhub);
132 // do it a second time for DFRN so that any children find their parents.
133 if ($contact['network'] === Protocol::DFRN) {
134 consume_feed($xml, $importer, $contact, $feedhub);