3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package WebFingerPlugin
22 * @author James Walker <james@status.net>
23 * @author Mikael Nordfeldth <mmn@hethane.se>
26 if (!defined('GNUSOCIAL')) { exit(1); }
28 abstract class XrdAction extends Action
30 // json or xml for now, this may still be overriden because of
31 // our back-compatibility with StatusNet <=1.1.1
32 protected $defaultformat = null;
34 protected $resource = null;
35 protected $target = null;
36 protected $xrd = null;
38 public function isReadOnly($args)
44 * Configures $this->xrd which will later be printed. Must be
45 * implemented by child classes.
47 abstract protected function setXRD();
49 protected function prepare(array $args=array())
51 if (!isset($args['format'])) {
52 $args['format'] = $this->defaultformat;
55 parent::prepare($args);
57 $this->xrd = new XML_XRD();
62 protected function handle()
68 if (common_config('discovery', 'cors')) {
69 header('Access-Control-Allow-Origin: *');
75 public function mimeType()
78 return $this->checkAccept();
79 } catch (Exception $e) {
80 $supported = Discovery::supportedMimeTypes();
81 $docformat = $this->arg('format');
83 if (!empty($docformat) && isset($supported[$docformat])) {
84 return $supported[$docformat];
89 * "A WebFinger resource MUST return a JRD as the representation
90 * for the resource if the client requests no other supported
91 * format explicitly via the HTTP "Accept" header. [...]
92 * The WebFinger resource MUST silently ignore any requested
93 * representations that it does not understand and support."
94 * -- RFC 7033 (WebFinger)
95 * http://tools.ietf.org/html/rfc7033
97 return Discovery::JRD_MIMETYPE;
100 public function showPage()
102 $mimeType = $this->mimeType();
103 header("Content-type: {$mimeType}");
106 case Discovery::XRD_MIMETYPE:
107 print $this->xrd->toXML();
109 case Discovery::JRD_MIMETYPE:
110 case Discovery::JRD_MIMETYPE_OLD:
111 print $this->xrd->to('json');
114 throw new Exception(_('No supported MIME type in Accept header.'));
118 protected function checkAccept()
121 $httpaccept = isset($_SERVER['HTTP_ACCEPT'])
122 ? $_SERVER['HTTP_ACCEPT'] : null;
123 $useragent = isset($_SERVER['HTTP_USER_AGENT'])
124 ? $_SERVER['HTTP_USER_AGENT'] : null;
126 if ($httpaccept !== null && $httpaccept != '*/*') {
127 $can_serve = implode(',', Discovery::supportedMimeTypes());
128 $type = common_negotiate_type(common_accept_to_prefs($httpaccept),
129 common_accept_to_prefs($can_serve));
132 * HACK: for StatusNet to work against us, we must always serve an
133 * XRD to at least versions <1.1.1 (at time of writing) since they
134 * don't send Accept headers (in their 'Discovery::fetchXrd' calls)
137 preg_match('/(StatusNet)\/(\d+\.\d+(\.\d+)?)/', $useragent, $browser);
138 if (count($browser)>2 && $browser[1] === 'StatusNet'
139 && version_compare($browser[2], '1.1.1') < 1) {
140 return Discovery::XRD_MIMETYPE;
145 throw new Exception(_('No specified MIME type in Accept header.'));