123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class PushHubAction extends Action
- {
- function arg($arg, $def=null)
- {
-
-
-
- $arg = str_replace('hub.', 'hub_', $arg);
- return parent::arg($arg, $def);
- }
- protected function prepare(array $args=array())
- {
- GNUsocial::setApi(true);
- return parent::prepare($args);
- }
- protected function handle()
- {
- $mode = $this->trimmed('hub.mode');
- switch ($mode) {
- case "subscribe":
- case "unsubscribe":
- $this->subunsub($mode);
- break;
- case "publish":
-
- throw new ClientException(_m('Publishing outside feeds not supported.'), 400);
- default:
-
- throw new ClientException(sprintf(_m('Unrecognized mode "%s".'),$mode), 400);
- }
- }
-
- function subunsub($mode)
- {
- $callback = $this->argUrl('hub.callback');
- common_debug('New WebSub hub request ('._ve($mode).') for callback '._ve($callback));
- $topic = $this->argUrl('hub.topic');
- if (!$this->recognizedFeed($topic)) {
- common_debug('WebSub hub request had unrecognized feed topic=='._ve($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)) {
- common_debug('WebSub hub request had invalid lease_seconds=='._ve($lease));
-
- throw new ClientException(sprintf(_m('Invalid hub.lease "%s". It must be empty or positive integer.'),$lease));
- }
- $secret = $this->arg('hub.secret', null);
- if ($secret != '' && strlen($secret) >= 200) {
- common_debug('WebSub hub request had invalid secret=='._ve($secret));
-
- throw new ClientException(sprintf(_m('Invalid hub.secret "%s". It must be under 200 bytes.'),$secret));
- }
- $sub = HubSub::getByHashkey($topic, $callback);
- if (!$sub instanceof HubSub) {
-
- common_debug('WebSub creating new HubSub entry for topic=='._ve($topic).' to remote callback '._ve($callback));
- $sub = new HubSub();
- $sub->topic = $topic;
- $sub->callback = $callback;
- }
- if ($mode == 'subscribe') {
- if ($secret) {
- $sub->secret = $secret;
- }
- if ($lease) {
- $sub->setLease(intval($lease));
- }
- }
- $verify = $this->arg('hub.verify');
- $token = $this->arg('hub.verify_token', null);
- if ($verify == 'sync') {
- $sub->verify($mode, $token);
- header('HTTP/1.1 204 No Content');
- } else {
- $sub->scheduleVerify($mode, $token);
- header('HTTP/1.1 202 Accepted');
- }
- }
-
- protected function recognizedFeed($feed)
- {
- $matches = array();
-
- if (preg_match('!/(\d+)\.atom$!', $feed, $matches)) {
- $id = $matches[1];
- $params = array('id' => $id, 'format' => 'atom');
-
- switch ($feed) {
- case common_local_url('ApiTimelineUser', $params):
- $user = User::getKV('id', $id);
- if (!$user instanceof User) {
-
- 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) {
-
- throw new ClientException(sprintf(_m('Invalid hub.topic "%s". Local_group does not exist.'),$feed));
- }
- return true;
- }
- common_debug("Feed was not recognized by any local User or Group Atom feed URLs: {$feed}");
- return false;
- }
-
- if (preg_match('!/(\d+)/lists/(\d+)/statuses\.atom$!', $feed, $matches)) {
- $user = $matches[1];
- $id = $matches[2];
- $params = array('user' => $user, 'id' => $id, 'format' => 'atom');
-
- 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) {
-
- throw new ClientException(sprintf(_m('Invalid hub.topic %s; list does not exist.'),$feed));
- }
- return true;
- }
- common_debug("Feed was not recognized by any local Profile_list Atom feed URL: {$feed}");
- return false;
- }
- common_debug("Unknown feed URL structure, can't match against local user, group or profile_list: {$feed}");
- return false;
- }
-
- protected function argUrl($arg)
- {
- $url = $this->arg($arg);
- $params = array('domain_check' => false,
- 'allowed_schemes' => array('http', 'https'));
- $validate = new Validate();
- if (!$validate->uri($url, $params)) {
-
-
- throw new ClientException(sprintf(_m('Invalid URL passed for %1$s: "%2$s"'),$arg,$url));
- }
- Event::handle('UrlBlacklistTest', array($url));
- return $url;
- }
-
- protected function getSub($feed, $callback)
- {
- return HubSub::getByHashkey($feed, $callback);
- }
- }
|