]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WebFinger/lib/xrdaction.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / WebFinger / lib / xrdaction.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package WebFingerPlugin
22  * @author James Walker <james@status.net>
23  * @author Mikael Nordfeldth <mmn@hethane.se>
24  */
25
26 if (!defined('GNUSOCIAL')) { exit(1); }
27
28 abstract class XrdAction extends ManagedAction
29 {
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;
33
34     protected $xrd      = null;
35
36     public function isReadOnly(array $args=array())
37     {
38         return true;
39     }
40
41     /*
42      * Configures $this->xrd which will later be printed. Must be
43      * implemented by child classes.
44      */
45     abstract protected function setXRD();
46
47     protected function prepare(array $args=array())
48     {
49         if (!isset($args['format'])) {
50             $args['format'] = $this->defaultformat;
51         }
52
53         parent::prepare($args);
54         
55         $this->xrd = new XML_XRD();
56
57         return true;
58     }
59
60     protected function handle()
61     {
62         $this->setXRD();
63
64         if (common_config('discovery', 'cors')) {
65             header('Access-Control-Allow-Origin: *');
66         }
67
68         parent::handle();
69     }
70
71     public function mimeType()
72     {
73         try {
74             return $this->checkAccept();
75         } catch (Exception $e) {
76             $supported = Discovery::supportedMimeTypes();
77             $docformat = $this->arg('format');
78
79             if (!empty($docformat) && isset($supported[$docformat])) {
80                 return $supported[$docformat];
81             }
82         }
83
84         /*
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
92          */
93         return Discovery::JRD_MIMETYPE;
94     }
95
96     public function showPage()
97     {
98         $mimeType = $this->mimeType();
99         header("Content-type: {$mimeType}");
100
101         switch ($mimeType) {
102         case Discovery::XRD_MIMETYPE:
103             print $this->xrd->toXML();
104             break;
105         case Discovery::JRD_MIMETYPE:
106         case Discovery::JRD_MIMETYPE_OLD:
107             print $this->xrd->to('json');
108             break;
109         default:
110             throw new Exception(_('No supported MIME type in Accept header.'));
111         }
112     }
113
114     protected function checkAccept()
115     {
116         $type = null;
117         $httpaccept = isset($_SERVER['HTTP_ACCEPT'])
118                         ? $_SERVER['HTTP_ACCEPT'] : null;
119         $useragent  = isset($_SERVER['HTTP_USER_AGENT'])
120                         ? $_SERVER['HTTP_USER_AGENT'] : null;
121
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));
126         } else {
127             /*
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)
131              */
132             $matches = array();
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;
137             }
138         }
139
140         if (empty($type)) {
141             throw new Exception(_('No specified MIME type in Accept header.'));
142         }
143
144         return $type;
145     }
146 }