]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubsubscriptionfeed.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into testing
[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
52 class AtompubsubscriptionfeedAction extends ApiAuthAction
53 {
54     private $_profile       = null;
55     private $_subscriptions = null;
56
57     /**
58      * For initializing members of the class.
59      *
60      * @param array $argarray misc. arguments
61      *
62      * @return boolean true
63      */
64
65     function prepare($argarray)
66     {
67         parent::prepare($argarray);
68         
69         $subscriber = $this->trimmed('subscriber');
70
71         $this->_profile = Profile::staticGet('id', $subscriber);
72
73         if (empty($this->_profile)) {
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
97     function handle($argarray=null)
98     {
99         parent::handle($argarray);
100         switch ($_SERVER['REQUEST_METHOD']) {
101         case 'HEAD':
102         case 'GET':
103             $this->showFeed();
104             break;
105         case 'POST':
106             $this->addSubscription();
107             break;
108         default:
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
122     function showFeed()
123     {
124         header('Content-Type: application/atom+xml; charset=utf-8');
125
126         $url = common_local_url('AtomPubSubscriptionFeed',
127                                 array('subscriber' => $this->_profile->id));
128
129         $feed = new Atom10Feed(true);
130
131         $feed->addNamespace('activity',
132                             'http://activitystrea.ms/spec/1.0/');
133
134         $feed->addNamespace('poco',
135                             'http://portablecontacts.net/spec/1.0');
136
137         $feed->addNamespace('media',
138                             'http://purl.org/syndication/atommedia');
139
140         $feed->id = $url;
141
142         $feed->setUpdated('now');
143
144         $feed->addAuthor($this->_profile->getBestName(),
145                          $this->_profile->getURI());
146
147         $feed->setTitle(sprintf(_("%s subscriptions"),
148                                 $this->_profile->getBestName()));
149
150         $feed->setSubtitle(sprintf(_("People %s has subscribed to on %s"),
151                                    $this->_profile->getBestName(),
152                                    common_config('site', 'name')));
153
154         $feed->addLink(common_local_url('subscriptions',
155                                         array('nickname' => 
156                                               $this->_profile->nickname)));
157
158         $feed->addLink($url,
159                        array('rel' => 'self',
160                              'type' => 'application/atom+xml'));
161                                         
162         // If there's more...
163
164         if ($this->page > 1) {
165             $feed->addLink($url,
166                            array('rel' => 'first',
167                                  'type' => 'application/atom+xml'));
168
169             $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
170                                             array('subscriber' => 
171                                                   $this->_profile->id),
172                                             array('page' => 
173                                                   $this->page - 1)),
174                            array('rel' => 'prev',
175                                  'type' => 'application/atom+xml'));
176         }
177
178         if ($this->_subscriptions->N > $this->count) {
179
180             $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
181                                             array('subscriber' =>
182                                                   $this->_profile->id),
183                                             array('page' =>
184                                                   $this->page + 1)),
185                            array('rel' => 'next',
186                                  'type' => 'application/atom+xml'));
187         }
188
189         $i = 0;
190
191         // XXX: This is kind of inefficient
192
193         while ($this->_subscriptions->fetch()) {
194
195             // We get one more than needed; skip that one
196
197             $i++;
198
199             if ($i > $this->count) {
200                 break;
201             }
202
203             $act = $this->_subscriptions->asActivity();
204             $feed->addEntryRaw($act->asString(false, false, false));
205         }
206
207         $this->raw($feed->getString());
208     }
209
210     /**
211      * Add a new subscription
212      *
213      * Handling the POST method for AtomPub
214      *
215      * @return void
216      */
217
218     function addSubscription()
219     {
220         if (empty($this->auth_user) ||
221             $this->auth_user->id != $this->_profile->id) {
222             throw new ClientException(_("Can't add someone else's".
223                                         " subscription"), 403);
224         }
225         
226         $xml = file_get_contents('php://input');
227
228         $dom = DOMDocument::loadXML($xml);
229
230         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
231             $dom->documentElement->localName != 'entry') {
232             // TRANS: Client error displayed when not using an Atom entry.
233             $this->clientError(_('Atom post must be an Atom entry.'));
234             return;
235         }
236
237         $activity = new Activity($dom->documentElement);
238
239         $sub = null;
240
241         if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
242
243             if ($activity->verb != ActivityVerb::FOLLOW) {
244                 // TRANS: Client error displayed when not using the POST verb.
245                 // TRANS: Do not translate POST.
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                 $this->clientError(_('Can only follow people.'));
254                 return;
255             }
256
257             // XXX: OStatus discovery (maybe)
258
259             $profile = Profile::fromURI($person->id);
260
261             if (empty($profile)) {
262                 $this->clientError(sprintf(_('Unknown profile %s'), $person->id));
263                 return;
264             }
265
266             if (Subscription::exists($this->_profile, $profile)) {
267                 // 409 Conflict
268                 $this->clientError(sprintf(_('Already subscribed to %s'),
269                                            $person->id), 
270                                    409);
271                 return;
272             }
273
274             if (Subscription::start($this->_profile, $profile)) {
275                 $sub = Subscription::pkeyGet(array('subscriber' => $this->_profile->id,
276                                                    'subscribed' => $profile->id));
277             }
278
279             Event::handle('EndAtomPubNewActivity', array($activity, $sub));
280         }
281
282         if (!empty($sub)) {
283             $act = $sub->asActivity();
284
285             header('Content-Type: application/atom+xml; charset=utf-8');
286             header('Content-Location: ' . $act->selfLink);
287
288             $this->startXML();
289             $this->raw($act->asString(true, true, true));
290             $this->endXML();
291         }
292     }
293
294     /**
295      * Return true if read only.
296      *
297      * @param array $args other arguments
298      *
299      * @return boolean is read only action?
300      */
301
302     function isReadOnly($args)
303     {
304         return $_SERVER['REQUEST_METHOD'] != 'POST';
305     }
306
307     /**
308      * Return last modified, if applicable.
309      *
310      * @return string last modified http header
311      */
312
313     function lastModified()
314     {
315         return null;
316     }
317
318     /**
319      * Return etag, if applicable.
320      *
321      * @return string etag http header
322      */
323
324     function etag()
325     {
326         return null;
327     }
328
329     /**
330      * Does this require authentication?
331      *
332      * @return boolean true if delete, else false
333      */
334
335     function requiresAuth()
336     {
337         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
338             return true;
339         } else {
340             return false;
341         }
342     }
343 }