]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/rsd.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / actions / rsd.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2010, StatusNet, Inc.
5  *
6  * Really Simple Discovery (RSD) for API access
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category API
24  * @package  StatusNet
25  * @author   Evan Prodromou <evan@status.net>
26  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
27  * @link     http://status.net/
28  *
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * RSD action class
37  *
38  * Really Simple Discovery (RSD) is a simple (to a fault, maybe)
39  * discovery tool for blog APIs.
40  *
41  * http://tales.phrasewise.com/rfc/rsd
42  *
43  * Anil Dash suggested that RSD be used for services that implement
44  * the Twitter API:
45  *
46  * http://dashes.com/anil/2009/12/the-twitter-api-is-finished.html
47  *
48  * It's in use now for WordPress.com blogs:
49  *
50  * http://matt.wordpress.com/xmlrpc.php?rsd
51  *
52  * I (evan@status.net) have tried to stay faithful to the premise of
53  * RSD, while adding information useful to StatusNet client developers.
54  * In particular:
55  *
56  * - There is a link from each user's profile page to their personal
57  *   RSD feed. A personal rsd.xml includes a 'blogID' element that is
58  *   their username.
59  * - There is a link from the public root to '/rsd.xml', a public RSD
60  *   feed. It's identical to the personal rsd except it doesn't include
61  *   a blogId.
62  * - I've added a setting to the API to indicate that OAuth support is
63  *   available.
64  *
65  * @category API
66  * @package  StatusNet
67  * @author   Evan Prodromou <evan@status.net>
68  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
69  * @link     http://status.net/
70  */
71 class RsdAction extends Action
72 {
73     /**
74      * Optional attribute for the personal rsd.xml file.
75      */
76     var $user = null;
77
78     /**
79      * Prepare the action for use.
80      *
81      * Check for a nickname; redirect if non-canonical; if
82      * not provided, assume public rsd.xml.
83      *
84      * @param array $args GET, POST, and URI arguments.
85      *
86      * @return boolean success flag
87      */
88     function prepare(array $args=array())
89     {
90         parent::prepare($args);
91
92         // optional argument
93
94         $nickname_arg = $this->arg('nickname');
95
96         if (empty($nickname_arg)) {
97             $this->user = null;
98         } else {
99             $nickname = common_canonical_nickname($nickname_arg);
100
101             // Permanent redirect on non-canonical nickname
102
103             if ($nickname_arg != $nickname) {
104                 common_redirect(common_local_url('rsd', array('nickname' => $nickname)), 301);
105             }
106
107             $this->user = User::getKV('nickname', $nickname);
108
109             if (empty($this->user)) {
110                 // TRANS: Client error.
111                 $this->clientError(_('No such user.'), 404);
112             }
113         }
114
115         return true;
116     }
117
118     /**
119      * Action handler.
120      *
121      * Outputs the XML format for an RSD file. May include
122      * personal information if this is a personal file
123      * (based on whether $user attribute is set).
124      *
125      * @param array $args array of arguments
126      *
127      * @return nothing
128      */
129     function handle(array $args=array())
130     {
131         header('Content-Type: application/rsd+xml');
132
133         $this->startXML();
134
135         $rsdNS = 'http://archipelago.phrasewise.com/rsd';
136         $this->elementStart('rsd', array('version' => '1.0',
137                                          'xmlns' => $rsdNS));
138         $this->elementStart('service');
139         // TRANS: Engine name for RSD.
140         $this->element('engineName', null, _('StatusNet'));
141         $this->element('engineLink', null, 'http://status.net/');
142         $this->elementStart('apis');
143         if (Event::handle('StartRsdListApis', array($this, $this->user))) {
144
145             $blogID   = (empty($this->user)) ? '' : $this->user->nickname;
146             $apiAttrs = array('name' => 'Twitter',
147                               'preferred' => 'true',
148                               'apiLink' => $this->_apiRoot(),
149                               'blogID' => $blogID);
150
151             $this->elementStart('api', $apiAttrs);
152             $this->elementStart('settings');
153             $this->element('docs', null,
154                            common_local_url('doc', array('title' => 'api')));
155             $this->element('setting', array('name' => 'OAuth'),
156                            'true');
157             $this->elementEnd('settings');
158             $this->elementEnd('api');
159
160             // Atom API
161
162             if (empty($this->user)) {
163                 $service = common_local_url('ApiAtomService');
164             } else {
165                 $service = common_local_url('ApiAtomService', array('id' => $this->user->nickname));
166             }
167
168             $this->element('api', array('name' => 'Atom',
169                                         'preferred' => 'false',
170                                         'apiLink' => $service,
171                                         'blogID' => $blogID));
172
173             Event::handle('EndRsdListApis', array($this, $this->user));
174         }
175         $this->elementEnd('apis');
176         $this->elementEnd('service');
177         $this->elementEnd('rsd');
178
179         $this->endXML();
180
181         return true;
182     }
183
184     /**
185      * Returns last-modified date for use in caching
186      *
187      * Per-user rsd.xml is dated to last change of user
188      * (in case of nickname change); public has no date.
189      *
190      * @return string date of last change of this page
191      */
192     function lastModified()
193     {
194         if (!empty($this->user)) {
195             return $this->user->modified;
196         } else {
197             return null;
198         }
199     }
200
201     /**
202      * Flag to indicate if this action is read-only
203      *
204      * It is; it doesn't change the DB.
205      *
206      * @param array $args ignored
207      *
208      * @return boolean true
209      */
210     function isReadOnly(array $args=array())
211     {
212         return true;
213     }
214
215     /**
216      * Return current site's API root
217      *
218      * Varies based on URL parameters, like if fancy URLs are
219      * turned on.
220      *
221      * @return string API root URI for this site
222      */
223     private function _apiRoot()
224     {
225         if (common_config('site', 'fancy')) {
226             return common_path('api/', true);
227         } else {
228             return common_path('index.php/api/', true);
229         }
230     }
231 }