]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubsubscriptionfeed.php
Using GNUSOCIAL_VERSION instead of STATUSNET_VERSION
[quix0rs-gnu-social.git] / actions / atompubsubscriptionfeed.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * AtomPub subscription feed
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  Cache
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Subscription feed class for AtomPub
39  *
40  * Generates a list of the user's subscriptions
41  *
42  * @category  AtomPub
43  * @package   StatusNet
44  * @author    Evan Prodromou <evan@status.net>
45  * @copyright 2010 StatusNet, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49 class AtompubsubscriptionfeedAction extends ApiAuthAction
50 {
51     private $_profile       = null;
52     private $_subscriptions = null;
53
54     /**
55      * For initializing members of the class.
56      *
57      * @param array $argarray misc. arguments
58      *
59      * @return boolean true
60      */
61     function prepare($argarray)
62     {
63         parent::prepare($argarray);
64
65         $subscriber = $this->trimmed('subscriber');
66
67         $this->_profile = Profile::getKV('id', $subscriber);
68
69         if (empty($this->_profile)) {
70             // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
71             // TRANS: %d is the non-existing profile ID number.
72             throw new ClientException(sprintf(_('No such profile id: %d.'),
73                                               $subscriber), 404);
74         }
75
76         // page and count from ApiAction
77
78         $offset = ($this->page-1) * $this->count;
79
80         $this->_subscriptions = Subscription::bySubscriber($subscriber,
81                                                            $offset,
82                                                            $this->count + 1);
83
84         return true;
85     }
86
87     /**
88      * Handler method
89      *
90      * @param array $argarray is ignored since it's now passed in in prepare()
91      *
92      * @return void
93      */
94     function handle($argarray=null)
95     {
96         parent::handle($argarray);
97         switch ($_SERVER['REQUEST_METHOD']) {
98         case 'HEAD':
99         case 'GET':
100             $this->showFeed();
101             break;
102         case 'POST':
103             $this->addSubscription();
104             break;
105         default:
106             // TRANS: Client exception thrown when using an unsupported HTTP method.
107             $this->clientError(_('HTTP method not supported.'), 405);
108             return;
109         }
110
111         return;
112     }
113
114     /**
115      * Show the feed of subscriptions
116      *
117      * @return void
118      */
119     function showFeed()
120     {
121         header('Content-Type: application/atom+xml; charset=utf-8');
122
123         $url = common_local_url('AtomPubSubscriptionFeed',
124                                 array('subscriber' => $this->_profile->id));
125
126         $feed = new Atom10Feed(true);
127
128         $feed->addNamespace('activity',
129                             'http://activitystrea.ms/spec/1.0/');
130
131         $feed->addNamespace('poco',
132                             'http://portablecontacts.net/spec/1.0');
133
134         $feed->addNamespace('media',
135                             'http://purl.org/syndication/atommedia');
136
137         $feed->id = $url;
138
139         $feed->setUpdated('now');
140
141         $feed->addAuthor($this->_profile->getBestName(),
142                          $this->_profile->getURI());
143
144         // TRANS: Title for Atom subscription feed.
145         // TRANS: %s is a user nickname.
146         $feed->setTitle(sprintf(_("%s subscriptions"),
147                                 $this->_profile->getBestName()));
148
149         // TRANS: Subtitle for Atom subscription feed.
150         // TRANS: %1$s is a user nickname, %s$s is the StatusNet sitename.
151         $feed->setSubtitle(sprintf(_("People %1\$s has subscribed to on %2\$s"),
152                                    $this->_profile->getBestName(),
153                                    common_config('site', 'name')));
154
155         $feed->addLink(common_local_url('subscriptions',
156                                         array('nickname' =>
157                                               $this->_profile->nickname)));
158
159         $feed->addLink($url,
160                        array('rel' => 'self',
161                              'type' => 'application/atom+xml'));
162
163         // If there's more...
164
165         if ($this->page > 1) {
166             $feed->addLink($url,
167                            array('rel' => 'first',
168                                  'type' => 'application/atom+xml'));
169
170             $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
171                                             array('subscriber' =>
172                                                   $this->_profile->id),
173                                             array('page' =>
174                                                   $this->page - 1)),
175                            array('rel' => 'prev',
176                                  'type' => 'application/atom+xml'));
177         }
178
179         if ($this->_subscriptions->N > $this->count) {
180
181             $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
182                                             array('subscriber' =>
183                                                   $this->_profile->id),
184                                             array('page' =>
185                                                   $this->page + 1)),
186                            array('rel' => 'next',
187                                  'type' => 'application/atom+xml'));
188         }
189
190         $i = 0;
191
192         // XXX: This is kind of inefficient
193
194         while ($this->_subscriptions->fetch()) {
195
196             // We get one more than needed; skip that one
197
198             $i++;
199
200             if ($i > $this->count) {
201                 break;
202             }
203
204             $act = $this->_subscriptions->asActivity();
205             $feed->addEntryRaw($act->asString(false, false, false));
206         }
207
208         $this->raw($feed->getString());
209     }
210
211     /**
212      * Add a new subscription
213      *
214      * Handling the POST method for AtomPub
215      *
216      * @return void
217      */
218     function addSubscription()
219     {
220         if (empty($this->auth_user) ||
221             $this->auth_user->id != $this->_profile->id) {
222             // TRANS: Client exception thrown when trying to subscribe another user.
223             throw new ClientException(_("Cannot add someone else's".
224                                         " subscription."), 403);
225         }
226
227         $xml = file_get_contents('php://input');
228
229         $dom = DOMDocument::loadXML($xml);
230
231         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
232             $dom->documentElement->localName != 'entry') {
233             // TRANS: Client error displayed when not using an Atom entry.
234             $this->clientError(_('Atom post must be an Atom entry.'));
235             return;
236         }
237
238         $activity = new Activity($dom->documentElement);
239
240         $sub = null;
241
242         if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
243
244             if ($activity->verb != ActivityVerb::FOLLOW) {
245                 // TRANS: Client error displayed when not using the follow verb.
246                 $this->clientError(_('Can only handle Follow activities.'));
247                 return;
248             }
249
250             $person = $activity->objects[0];
251
252             if ($person->type != ActivityObject::PERSON) {
253                 // TRANS: Client exception thrown when subscribing to an object that is not a person.
254                 $this->clientError(_('Can only follow people.'));
255                 return;
256             }
257
258             // XXX: OStatus discovery (maybe)
259
260             $profile = Profile::fromURI($person->id);
261
262             if (empty($profile)) {
263                 // TRANS: Client exception thrown when subscribing to a non-existing profile.
264                 // TRANS: %s is the unknown profile ID.
265                 $this->clientError(sprintf(_('Unknown profile %s.'), $person->id));
266                 return;
267             }
268
269             if (Subscription::exists($this->_profile, $profile)) {
270                 // 409 Conflict
271                 // TRANS: Client error displayed trying to subscribe to an already subscribed profile.
272                 // TRANS: %s is the profile the user already has a subscription on.
273                 $this->clientError(sprintf(_('Already subscribed to %s.'),
274                                            $person->id),
275                                    409);
276                 return;
277             }
278
279             if (Subscription::start($this->_profile, $profile)) {
280                 $sub = Subscription::pkeyGet(array('subscriber' => $this->_profile->id,
281                                                    'subscribed' => $profile->id));
282             }
283
284             Event::handle('EndAtomPubNewActivity', array($activity, $sub));
285         }
286
287         if (!empty($sub)) {
288             $act = $sub->asActivity();
289
290             header('Content-Type: application/atom+xml; charset=utf-8');
291             header('Content-Location: ' . $act->selfLink);
292
293             $this->startXML();
294             $this->raw($act->asString(true, true, true));
295             $this->endXML();
296         }
297     }
298
299     /**
300      * Return true if read only.
301      *
302      * @param array $args other arguments
303      *
304      * @return boolean is read only action?
305      */
306     function isReadOnly($args)
307     {
308         return $_SERVER['REQUEST_METHOD'] != 'POST';
309     }
310
311     /**
312      * Return last modified, if applicable.
313      *
314      * @return string last modified http header
315      */
316     function lastModified()
317     {
318         return null;
319     }
320
321     /**
322      * Return etag, if applicable.
323      *
324      * @return string etag http header
325      */
326     function etag()
327     {
328         return null;
329     }
330
331     /**
332      * Does this require authentication?
333      *
334      * @return boolean true if delete, else false
335      */
336     function requiresAuth()
337     {
338         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
339             return true;
340         } else {
341             return false;
342         }
343     }
344 }