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 ManagedAction
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 $xrd = null;
36 public function isReadOnly($args)
42 * Configures $this->xrd which will later be printed. Must be
43 * implemented by child classes.
45 abstract protected function setXRD();
47 protected function prepare(array $args=array())
49 if (!isset($args['format'])) {
50 $args['format'] = $this->defaultformat;
53 parent::prepare($args);
55 $this->xrd = new XML_XRD();
60 protected function handle()
64 if (common_config('discovery', 'cors')) {
65 header('Access-Control-Allow-Origin: *');
71 public function mimeType()
74 return $this->checkAccept();
75 } catch (Exception $e) {
76 $supported = Discovery::supportedMimeTypes();
77 $docformat = $this->arg('format');
79 if (!empty($docformat) && isset($supported[$docformat])) {
80 return $supported[$docformat];
85 * "A WebFinger resource MUST return a JRD as the representation
86 * for the resource if the client requests no other supported
87 * format explicitly via the HTTP "Accept" header. [...]
88 * The WebFinger resource MUST silently ignore any requested
89 * representations that it does not understand and support."
90 * -- RFC 7033 (WebFinger)
91 * http://tools.ietf.org/html/rfc7033
93 return Discovery::JRD_MIMETYPE;
96 public function showPage()
98 $mimeType = $this->mimeType();
99 header("Content-type: {$mimeType}");
102 case Discovery::XRD_MIMETYPE:
103 print $this->xrd->toXML();
105 case Discovery::JRD_MIMETYPE:
106 case Discovery::JRD_MIMETYPE_OLD:
107 print $this->xrd->to('json');
110 throw new Exception(_('No supported MIME type in Accept header.'));
114 protected function checkAccept()
117 $httpaccept = isset($_SERVER['HTTP_ACCEPT'])
118 ? $_SERVER['HTTP_ACCEPT'] : null;
119 $useragent = isset($_SERVER['HTTP_USER_AGENT'])
120 ? $_SERVER['HTTP_USER_AGENT'] : null;
122 if ($httpaccept !== null && $httpaccept != '*/*') {
123 $can_serve = implode(',', Discovery::supportedMimeTypes());
124 $type = common_negotiate_type(common_accept_to_prefs($httpaccept),
125 common_accept_to_prefs($can_serve));
128 * HACK: for StatusNet to work against us, we must always serve an
129 * XRD to at least versions <1.1.1 (at time of writing) since they
130 * don't send Accept headers (in their 'Discovery::fetchXrd' calls)
133 preg_match('/(StatusNet)\/(\d+\.\d+(\.\d+)?)/', $useragent, $browser);
134 if (count($browser)>2 && $browser[1] === 'StatusNet'
135 && version_compare($browser[2], '1.1.1') < 1) {
136 return Discovery::XRD_MIMETYPE;
141 throw new Exception(_('No specified MIME type in Accept header.'));