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