4 use Friendica\Core\Protocol;
5 use Friendica\Database\DBA;
6 use Friendica\Model\Contact;
7 use Friendica\Protocol\OStatus;
9 require_once('include/security.php');
10 require_once('include/items.php');
12 function hub_return($valid, $body)
15 header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
18 header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
23 // when receiving an XML feed, always return OK
25 function hub_post_return()
27 header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
31 function pubsub_init(App $a)
33 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
34 $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
36 if ($_SERVER['REQUEST_METHOD'] === 'GET') {
37 $hub_mode = notags(trim(defaults($_GET, 'hub_mode', '')));
38 $hub_topic = notags(trim(defaults($_GET, 'hub_topic', '')));
39 $hub_challenge = notags(trim(defaults($_GET, 'hub_challenge', '')));
40 $hub_lease = notags(trim(defaults($_GET, 'hub_lease_seconds', '')));
41 $hub_verify = notags(trim(defaults($_GET, 'hub_verify_token', '')));
43 logger('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
44 logger('Data: ' . print_r($_GET,true), LOGGER_DATA);
46 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
48 $owner = DBA::selectFirst('user', ['uid'], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
49 if (!DBA::isResult($owner)) {
50 logger('Local account not found: ' . $nick);
51 hub_return(false, '');
54 $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
56 if (!empty($hub_verify)) {
57 $condition['hub-verify'] = $hub_verify;
60 $contact = DBA::selectFirst('contact', ['id', 'poll'], $condition);
61 if (!DBA::isResult($contact)) {
62 logger('Contact ' . $contact_id . ' not found.');
63 hub_return(false, '');
66 if (!empty($hub_topic) && !link_compare($hub_topic, $contact['poll'])) {
67 logger('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
68 hub_return(false, '');
71 // We must initiate an unsubscribe request with a verify_token.
72 // Don't allow outsiders to unsubscribe us.
74 if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
75 logger('Bogus unsubscribe');
76 hub_return(false, '');
79 if (!empty($hub_mode)) {
80 DBA::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
81 logger($hub_mode . ' success for contact ' . $contact_id . '.');
83 hub_return(true, $hub_challenge);
87 function pubsub_post(App $a)
89 $xml = file_get_contents('php://input');
91 logger('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $a->cmd . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']);
92 logger('Data: ' . $xml, LOGGER_DATA);
94 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
95 $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
97 $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
98 if (!DBA::isResult($importer)) {
102 $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
103 $contact = DBA::selectFirst('contact', [], $condition);
105 if (!DBA::isResult($contact)) {
106 $author = OStatus::salmonAuthor($xml, $importer);
107 if (!empty($author['contact-id'])) {
108 $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
109 $contact = DBA::selectFirst('contact', [], $condition);
110 logger('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.');
112 if (!DBA::isResult($contact)) {
113 logger('Contact ' . $author["author-link"] . ' (' . $contact_id . ') for user ' . $nick . " wasn't found - ignored. XML: " . $xml);
118 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
119 logger('Contact ' . $contact['id'] . ' is not expected to share with us - ignored.');
123 // We import feeds from OStatus, Friendica and ATOM/RSS.
124 /// @todo Check if Friendica posts really arrive here - otherwise we can discard some stuff
125 if (!in_array($contact['network'], [Protocol::OSTATUS, Protocol::DFRN, Protocol::FEED])) {
129 logger('Import item for ' . $nick . ' from ' . $contact['nick'] . ' (' . $contact['id'] . ')');
131 consume_feed($xml, $importer, $contact, $feedhub);
133 // do it a second time for DFRN so that any children find their parents.
134 if ($contact['network'] === Protocol::DFRN) {
135 consume_feed($xml, $importer, $contact, $feedhub);