X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FOStatus%2Factions%2Fpushhub.php;h=5d0b9fbf903f64686388e4eeefc494bdddccc511;hb=1e89540c3f52f95e9224d781c01b2c927d3c3f09;hp=6909b8539131f4c57dc38425561398147350a045;hpb=6c14235d6c3359a6c9012ec49077f8defe117779;p=quix0rs-gnu-social.git diff --git a/plugins/OStatus/actions/pushhub.php b/plugins/OStatus/actions/pushhub.php index 6909b85391..5d0b9fbf90 100644 --- a/plugins/OStatus/actions/pushhub.php +++ b/plugins/OStatus/actions/pushhub.php @@ -23,19 +23,19 @@ * @maintainer Brion Vibber */ -/** - - -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? - -*/ +if (!defined('STATUSNET')) { + exit(1); +} +/** + * 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) @@ -47,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) { @@ -62,11 +62,11 @@ class PushHubAction extends Action $this->subunsub($mode); break; case "publish": - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Publishing outside feeds not supported.", 400); + // TRANS: Client exception. + throw new ClientException(_m('Publishing outside feeds not supported.'), 400); default: - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Unrecognized mode '$mode'.", 400); + // TRANS: Client exception. %s is a mode. + throw new ClientException(sprintf(_m('Unrecognized mode "%s".'),$mode), 400); } } @@ -85,32 +85,24 @@ class PushHubAction extends Action $topic = $this->argUrl('hub.topic'); if (!$this->recognizedFeed($topic)) { - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Unsupported hub.topic $topic; this hub only serves local user and group Atom feeds."); - } - - $verify = $this->arg('hub.verify'); // @fixme may be multiple - if ($verify != 'sync' && $verify != 'async') { - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Invalid hub.verify $verify; must be sync or async."); + // TRANS: Client exception. %s is a topic. + throw new ClientException(sprintf(_m('Unsupported hub.topic %s this hub only serves local user and group Atom feeds.'),$topic)); } $lease = $this->arg('hub.lease_seconds', null); if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) { - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Invalid hub.lease $lease; must be empty or positive integer."); + // 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) { - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Invalid hub.secret $secret; must be under 200 bytes."); + // 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; @@ -125,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'); } } @@ -143,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) { - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Invalid hub.topic $feed; user doesn't exist."); - } 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) { - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Invalid hub.topic $feed; group doesn't exist."); - } 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; } @@ -187,11 +200,13 @@ 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 { - // @todo i18n FIXME: added i18n and use sprintf when using parameters. - throw new ClientException("Invalid URL passed for $arg: '$url'"); + // TRANS: Client exception. + // TRANS: %1$s is this argument to the method this exception occurs in, %2$s is a URL. + throw new ClientException(sprintf(_m('Invalid URL passed for %1$s: "%2$s"'),$arg,$url)); } } @@ -204,6 +219,6 @@ class PushHubAction extends Action */ protected function getSub($feed, $callback) { - return HubSub::staticGet($feed, $callback); + return HubSub::getByHashkey($feed, $callback); } }