X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=mod%2Fpubsub.php;h=ef97bb7f7f625fbfef87d3355bd719b9c4ce11e7;hb=c0e728e7e5d12c3c54a8c140e004992660701244;hp=fa092049f41295511c69055d644297158fce25de;hpb=0d2650b29b92943a1570896491865c12fd28fda8;p=friendica.git diff --git a/mod/pubsub.php b/mod/pubsub.php index fa092049f4..ef97bb7f7f 100644 --- a/mod/pubsub.php +++ b/mod/pubsub.php @@ -1,113 +1,138 @@ argc > 1) ? notags(trim($a->argv[1])) : ''); - $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0); + $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 ); - if($_SERVER['REQUEST_METHOD'] === 'GET') { + if ($_SERVER['REQUEST_METHOD'] === 'GET') { + $hub_mode = notags(trim(defaults($_GET, 'hub_mode', ''))); + $hub_topic = notags(trim(defaults($_GET, 'hub_topic', ''))); + $hub_challenge = notags(trim(defaults($_GET, 'hub_challenge', ''))); + $hub_lease = notags(trim(defaults($_GET, 'hub_lease_seconds', ''))); + $hub_verify = notags(trim(defaults($_GET, 'hub_verify_token', ''))); - $hub_mode = notags(trim($_GET['hub.mode'])); - $hub_topic = notags(trim($_GET['hub.topic'])); - $hub_challenge = notags(trim($_GET['hub.challenge'])); - $hub_lease = notags(trim($_GET['hub.lease_seconds'])); - $hub_verify = notags(trim($_GET['hub.verify_token'])); + logger('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick); + logger('Data: ' . print_r($_GET,true), LOGGER_DATA); $subscribe = (($hub_mode === 'subscribe') ? 1 : 0); - $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1", - dbesc($nick) - ); - if(! count($r)) + $owner = DBA::selectFirst('user', ['uid'], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]); + if (!DBA::isResult($owner)) { + logger('Local account not found: ' . $nick); hub_return(false, ''); + } - $owner = $r[0]; + $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false]; - $sql_extra = ((strlen($hub_verify)) ? sprintf(" AND `hub-verify` = '%s' ", dbesc($hub_verify)) : ''); + if (!empty($hub_verify)) { + $condition['hub-verify'] = $hub_verify; + } - $r = q("SELECT * FROM `contact` WHERE `poll` = '%s' AND `id` = %d AND `uid` = %d AND `blocked` = 0 $sql_extra LIMIT 1", - dbesc($hub_topic), - intval($contact_id), - intval($owner['uid']) - ); - if(! count($r)) + $contact = DBA::selectFirst('contact', ['id', 'poll'], $condition); + if (!DBA::isResult($contact)) { + logger('Contact ' . $contact_id . ' not found.'); hub_return(false, ''); + } - $contact = $r[0]; + if (!empty($hub_topic) && !link_compare($hub_topic, $contact['poll'])) { + logger('Hub topic ' . $hub_topic . ' != ' . $contact['poll']); + hub_return(false, ''); + } - // We must initiate an unsubscribe request with a verify_token. + // We must initiate an unsubscribe request with a verify_token. // Don't allow outsiders to unsubscribe us. - if(($hub_mode === 'unsubscribe') && (! strlen($hub_verify))) + if (($hub_mode === 'unsubscribe') && empty($hub_verify)) { + logger('Bogus unsubscribe'); hub_return(false, ''); + } - $r = q("UPDATE `contact` SET `subhub` = %d WHERE `id` = %d LIMIT 1", - intval($subscribe), - intval($contact['id']) - ); - + if (!empty($hub_mode)) { + DBA::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]); + logger($hub_mode . ' success for contact ' . $contact_id . '.'); + } hub_return(true, $hub_challenge); - } } - -function pubsub_post(&$a) { - +function pubsub_post(App $a) +{ $xml = file_get_contents('php://input'); + logger('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $a->cmd . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']); + logger('Data: ' . $xml, LOGGER_DATA); + $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : ''); - $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0); + $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 ); - $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1", - dbesc($nick) - ); - if(! count($r)) + $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]); + if (!DBA::isResult($importer)) { hub_post_return(); + } - $importer = $r[0]; + $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false]; + $contact = DBA::selectFirst('contact', [], $condition); + + if (!DBA::isResult($contact)) { + $author = OStatus::salmonAuthor($xml, $importer); + if (!empty($author['contact-id'])) { + $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false]; + $contact = DBA::selectFirst('contact', [], $condition); + logger('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.'); + } + if (!DBA::isResult($contact)) { + logger('Contact ' . $author["author-link"] . ' (' . $contact_id . ') for user ' . $nick . " wasn't found - ignored. XML: " . $xml); + hub_post_return(); + } + } - $r = q("SELECT * FROM `contact` WHERE `subhub` = 1 AND `id` = %d AND `uid` = %d AND `blocked` = 0 LIMIT 1", - intval($contact_id), - intval($importer['uid']) - ); - if(! count($r)) + if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != NETWORK_FEED)) { + logger('Contact ' . $contact['id'] . ' is not expected to share with us - ignored.'); hub_post_return(); + } - $contact = $r[0]; + // We import feeds from OStatus, Friendica and ATOM/RSS. + /// @todo Check if Friendica posts really arrive here - otherwise we can discard some stuff + if (!in_array($contact['network'], [NETWORK_OSTATUS, NETWORK_DFRN, NETWORK_FEED])) { + hub_post_return(); + } + logger('Import item for ' . $nick . ' from ' . $contact['nick'] . ' (' . $contact['id'] . ')'); $feedhub = ''; - consume_feed($xml,$importer,$contact,$feedhub); + consume_feed($xml, $importer, $contact, $feedhub); - hub_post_return(); + // do it a second time for DFRN so that any children find their parents. + if ($contact['network'] === NETWORK_DFRN) { + consume_feed($xml, $importer, $contact, $feedhub); + } + hub_post_return(); } - - -