4 use Friendica\Database\DBM;
5 use Friendica\Protocol\OStatus;
7 require_once('include/security.php');
8 require_once('include/items.php');
10 function hub_return($valid, $body)
13 header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
16 header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
21 // when receiving an XML feed, always return OK
23 function hub_post_return()
25 header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
29 function pubsub_init(App $a)
31 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
32 $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
34 if ($_SERVER['REQUEST_METHOD'] === 'GET') {
35 $hub_mode = notags(trim(defaults($_GET, 'hub_mode', '')));
36 $hub_topic = notags(trim(defaults($_GET, 'hub_topic', '')));
37 $hub_challenge = notags(trim(defaults($_GET, 'hub_challenge', '')));
38 $hub_lease = notags(trim(defaults($_GET, 'hub_lease_seconds', '')));
39 $hub_verify = notags(trim(defaults($_GET, 'hub_verify_token', '')));
41 logger('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
42 logger('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 (!DBM::is_result($owner)) {
48 logger('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 (!DBM::is_result($contact)) {
60 logger('Contact ' . $contact_id . ' not found.');
61 hub_return(false, '');
64 if (!empty($hub_topic) && !link_compare($hub_topic, $contact['poll'])) {
65 logger('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('Bogus unsubscribe');
74 hub_return(false, '');
77 if (!empty($hub_mode)) {
78 dba::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
79 logger($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('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $a->cmd . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']);
90 logger('Data: ' . $xml, LOGGER_DATA);
92 $nick = (($a->argc > 1) ? notags(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 (!DBM::is_result($importer)) {
100 $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
101 $contact = dba::selectFirst('contact', [], $condition);
103 if (!DBM::is_result($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('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.');
110 if (!DBM::is_result($contact)) {
111 logger('Contact ' . $author["author-link"] . ' (' . $contact_id . ') for user ' . $nick . " wasn't found - ignored. XML: " . $xml);
116 if (!in_array($contact['rel'], [CONTACT_IS_SHARING, CONTACT_IS_FRIEND]) && ($contact['network'] != NETWORK_FEED)) {
117 logger('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'], [NETWORK_OSTATUS, NETWORK_DFRN, NETWORK_FEED])) {
127 logger('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'] === NETWORK_DFRN) {
133 consume_feed($xml, $importer, $contact, $feedhub);