]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WebFinger/lib/xrdaction.php
Implemented WebFinger and replaced our XRD with PEAR XML_XRD
[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 Action
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 $resource = null;
35     protected $target   = null;
36     protected $xrd      = null;
37
38     public function isReadOnly($args)
39     {
40         return true;
41     }
42
43     /*
44      * Configures $this->xrd which will later be printed. Must be
45      * implemented by child classes.
46      */
47     abstract protected function setXRD();
48
49     protected function prepare(array $args=array())
50     {
51         if (!isset($args['format'])) {
52             $args['format'] = $this->defaultformat;
53         }
54
55         parent::prepare($args);
56         
57         $this->xrd = new XML_XRD();
58
59         return true;
60     }
61
62     protected function handle()
63     {
64         parent::handle();
65
66         $this->setXRD();
67
68         if (common_config('discovery', 'cors')) {
69             header('Access-Control-Allow-Origin: *');
70         }
71
72         $this->showPage();
73     }
74
75     public function mimeType()
76     {
77         try {
78             return $this->checkAccept();
79         } catch (Exception $e) {
80             $supported = Discovery::supportedMimeTypes();
81             $docformat = $this->arg('format');
82
83             if (!empty($docformat) && isset($supported[$docformat])) {
84                 return $supported[$docformat];
85             }
86         }
87
88         /*
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
96          */
97         return Discovery::JRD_MIMETYPE;
98     }
99
100     public function showPage()
101     {
102         $mimeType = $this->mimeType();
103         header("Content-type: {$mimeType}");
104
105         switch ($mimeType) {
106         case Discovery::XRD_MIMETYPE:
107             print $this->xrd->toXML();
108             break;
109         case Discovery::JRD_MIMETYPE:
110         case Discovery::JRD_MIMETYPE_OLD:
111             print $this->xrd->to('json');
112             break;
113         default:
114             throw new Exception(_('No supported MIME type in Accept header.'));
115         }
116     }
117
118     protected function checkAccept()
119     {
120         $type = null;
121         $httpaccept = isset($_SERVER['HTTP_ACCEPT'])
122                         ? $_SERVER['HTTP_ACCEPT'] : null;
123         $useragent  = isset($_SERVER['HTTP_USER_AGENT'])
124                         ? $_SERVER['HTTP_USER_AGENT'] : null;
125
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));
130         } else {
131             /*
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)
135              */
136             $matches = array();
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;
141             }
142         }
143
144         if (empty($type)) {
145             throw new Exception(_('No specified MIME type in Accept header.'));
146         }
147
148         return $type;
149     }
150 }