X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FOStatus%2Factions%2Fpushhub.php;h=5d0b9fbf903f64686388e4eeefc494bdddccc511;hb=1e89540c3f52f95e9224d781c01b2c927d3c3f09;hp=bfd51ec02f65b1d5ce17dc7d537f3c3528aaf9ad;hpb=f11c1c77cab7d7310ec0d2c17bc6f35c491f2871;p=quix0rs-gnu-social.git diff --git a/plugins/OStatus/actions/pushhub.php b/plugins/OStatus/actions/pushhub.php index bfd51ec02f..5d0b9fbf90 100644 --- a/plugins/OStatus/actions/pushhub.php +++ b/plugins/OStatus/actions/pushhub.php @@ -28,18 +28,14 @@ if (!defined('STATUSNET')) { } /** - - -Things to consider... -* should we purge incomplete subscriptions that never get a verification pingback? -* when can we send subscription renewal checks? - - at next send time probably ok -* when can we handle trimming of subscriptions? - - at next send time probably ok -* should we keep a fail count? - -*/ - + * Things to consider... + * should we purge incomplete subscriptions that never get a verification pingback? + * when can we send subscription renewal checks? + * - at next send time probably ok + * when can we handle trimming of subscriptions? + * - at next send time probably ok + * should we keep a fail count? + */ class PushHubAction extends Action { function arg($arg, $def=null) @@ -51,13 +47,13 @@ class PushHubAction extends Action return parent::arg($arg, $def); } - function prepare($args) + protected function prepare(array $args=array()) { - StatusNet::setApi(true); // reduce exception reports to aid in debugging + GNUsocial::setApi(true); // reduce exception reports to aid in debugging return parent::prepare($args); } - function handle() + protected function handle() { $mode = $this->trimmed('hub.mode'); switch ($mode) { @@ -93,28 +89,20 @@ class PushHubAction extends Action throw new ClientException(sprintf(_m('Unsupported hub.topic %s this hub only serves local user and group Atom feeds.'),$topic)); } - $verify = $this->arg('hub.verify'); // @fixme may be multiple - if ($verify != 'sync' && $verify != 'async') { - // TRANS: Client exception. - throw new ClientException(sprintf(_m('Invalid hub.verify "%s". It must be sync or async.'),$verify)); - } - $lease = $this->arg('hub.lease_seconds', null); if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) { - // TRANS: Client exception. + // TRANS: Client exception. %s is the invalid lease value. throw new ClientException(sprintf(_m('Invalid hub.lease "%s". It must be empty or positive integer.'),$lease)); } - $token = $this->arg('hub.verify_token', null); - $secret = $this->arg('hub.secret', null); if ($secret != '' && strlen($secret) >= 200) { - // TRANS: Client exception. + // TRANS: Client exception. %s is the invalid hub secret. throw new ClientException(sprintf(_m('Invalid hub.secret "%s". It must be under 200 bytes.'),$secret)); } - $sub = HubSub::staticGet($topic, $callback); - if (!$sub) { + $sub = HubSub::getByHashkey($topic, $callback); + if (!$sub instanceof HubSub) { // Creating a new one! $sub = new HubSub(); $sub->topic = $topic; @@ -129,16 +117,14 @@ class PushHubAction extends Action } } - if (!common_config('queue', 'enabled')) { - // Won't be able to background it. - $verify = 'sync'; - } - if ($verify == 'async') { - $sub->scheduleVerify($mode, $token); - header('HTTP/1.1 202 Accepted'); - } else { + $verify = $this->arg('hub.verify'); // TODO: deprecated + $token = $this->arg('hub.verify_token', null); // TODO: deprecated + if ($verify == 'sync') { // pre-0.4 PuSH $sub->verify($mode, $token); header('HTTP/1.1 204 No Content'); + } else { // If $verify is not "sync", we might be using PuSH 0.4 + $sub->scheduleVerify($mode, $token); // If we were certain it's PuSH 0.4, token could be removed + header('HTTP/1.1 202 Accepted'); } } @@ -147,38 +133,61 @@ class PushHubAction extends Action * user or group Atom feeds. * * @param string $feed URL - * @return boolean true if it matches + * @return boolean true if it matches, false if not a recognized local feed + * @throws exception if local entity does not exist */ - function recognizedFeed($feed) + protected function recognizedFeed($feed) { $matches = array(); + // Simple mapping to local ID for user or group if (preg_match('!/(\d+)\.atom$!', $feed, $matches)) { $id = $matches[1]; $params = array('id' => $id, 'format' => 'atom'); - $userFeed = common_local_url('ApiTimelineUser', $params); - $groupFeed = common_local_url('ApiTimelineGroup', $params); - - if ($feed == $userFeed) { - $user = User::staticGet('id', $id); - if (!$user) { - // TRANS: Client exception. - throw new ClientException(sprintt(_m('Invalid hub.topic "%s". User doesn\'t exist.'),$feed)); - } else { - return true; + + // Double-check against locally generated URLs + switch ($feed) { + case common_local_url('ApiTimelineUser', $params): + $user = User::getKV('id', $id); + if (!$user instanceof User) { + // TRANS: Client exception. %s is a feed URL. + throw new ClientException(sprintf(_m('Invalid hub.topic "%s". User does not exist.'),$feed)); } + return true; + + case common_local_url('ApiTimelineGroup', $params): + $group = Local_group::getKV('group_id', $id); + if (!$group instanceof Local_group) { + // TRANS: Client exception. %s is a feed URL. + throw new ClientException(sprintf(_m('Invalid hub.topic "%s". Local_group does not exist.'),$feed)); + } + return true; } - if ($feed == $groupFeed) { - $user = User_group::staticGet('id', $id); - if (!$user) { - // TRANS: Client exception. - throw new ClientException(sprintf(_m('Invalid hub.topic "%s". Group doesn\'t exist.'),$feed)); - } else { - return true; + common_debug("Feed was not recognized by any local User or Group Atom feed URLs: {$feed}"); + return false; + } + + // Profile lists are unique per user, so we need both IDs + if (preg_match('!/(\d+)/lists/(\d+)/statuses\.atom$!', $feed, $matches)) { + $user = $matches[1]; + $id = $matches[2]; + $params = array('user' => $user, 'id' => $id, 'format' => 'atom'); + + // Double-check against locally generated URLs + switch ($feed) { + case common_local_url('ApiTimelineList', $params): + $list = Profile_list::getKV('id', $id); + $user = User::getKV('id', $user); + if (!$list instanceof Profile_list || !$user instanceof User || $list->tagger != $user->id) { + // TRANS: Client exception. %s is a feed URL. + throw new ClientException(sprintf(_m('Invalid hub.topic %s; list does not exist.'),$feed)); } + return true; } - common_log(LOG_DEBUG, "Not a user or group feed? $feed $userFeed $groupFeed"); + common_debug("Feed was not recognized by any local Profile_list Atom feed URL: {$feed}"); + return false; } - common_log(LOG_DEBUG, "LOST $feed"); + + common_debug("Unknown feed URL structure, can't match against local user, group or profile_list: {$feed}"); return false; } @@ -191,7 +200,8 @@ class PushHubAction extends Action $url = $this->arg($arg); $params = array('domain_check' => false, // otherwise breaks my local tests :P 'allowed_schemes' => array('http', 'https')); - if (Validate::uri($url, $params)) { + $validate = new Validate; + if ($validate->uri($url, $params)) { return $url; } else { // TRANS: Client exception. @@ -209,6 +219,6 @@ class PushHubAction extends Action */ protected function getSub($feed, $callback) { - return HubSub::staticGet($feed, $callback); + return HubSub::getByHashkey($feed, $callback); } }