]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubsubscriptionfeed.php
Merge branch '0.9.x' into activityatompub
[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 $_subscribed = 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         // Note: this is a list of profiles, not subscriptions
80
81         $this->_subscribed = 
82             $this->_profile->getSubscriptions(($this->page-1) * $this->count, 
83                                               $this->count + 1);
84
85         return true;
86     }
87
88     /**
89      * Handler method
90      *
91      * @param array $argarray is ignored since it's now passed in in prepare()
92      *
93      * @return void
94      */
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             $this->clientError(_('HTTP method not supported.'), 405);
109             return;
110         }
111
112         return;
113     }
114
115     /**
116      * Show the feed of subscriptions
117      *
118      * @return void
119      */
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         $feed->setTitle(sprintf(_("%s subscriptions"),
147                                 $this->_profile->getBestName()));
148
149         $feed->setSubtitle(sprintf(_("People %s has subscribed to on %s"),
150                                    $this->_profile->getBestName()),
151                            common_config('site', 'name'));
152
153         $feed->addLink(common_local_url('subscriptions',
154                                         array('nickname' => 
155                                               $this->_profile->nickname)));
156
157         $feed->addLink($url,
158                        array('rel' => 'self',
159                              'type' => 'application/atom+xml'));
160                                         
161         // If there's more...
162
163         if ($this->page > 1) {
164             $feed->addLink($url,
165                            array('rel' => 'first',
166                                  'type' => 'application/atom+xml'));
167
168             $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
169                                             array('subscriber' => 
170                                                   $this->_profile->id,
171                                                   'page' => 
172                                                   $this->page - 1)),
173                            array('rel' => 'prev',
174                                  'type' => 'application/atom+xml'));
175         }
176
177         if ($this->_subscribed->N > $this->count) {
178
179             $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
180                                             array('subscriber' =>
181                                                   $this->_profile->id,
182                                                   'page' =>
183                                                   $this->page + 1)),
184                            array('rel' => 'next',
185                                  'type' => 'application/atom+xml'));
186         }
187
188         $i = 0;
189
190         // XXX: This is kind of inefficient
191
192         while ($this->_subscribed->fetch()) {
193
194             // We get one more than needed; skip that one
195
196             $i++;
197
198             if ($i > $this->count) {
199                 break;
200             }
201
202             $sub = Subscription::pkeyGet(array('subscriber' =>
203                                                $this->_profile->id,
204                                                'subscribed' =>
205                                                $this->_subscribed->id));
206             $act = $sub->asActivity();
207             $feed->addEntryRaw($act->asString(false, false, false));
208         }
209
210         $this->raw($feed->getString());
211     }
212
213     function addSubscription()
214     {
215         if (empty($this->auth_user) ||
216             $this->auth_user->id != $this->_profile->id) {
217             throw new ClientException(_("Can't add someone else's".
218                                         " subscription"), 403);
219         }
220         
221         $xml = file_get_contents('php://input');
222
223         $dom = DOMDocument::loadXML($xml);
224
225         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
226             $dom->documentElement->localName != 'entry') {
227             // TRANS: Client error displayed when not using an Atom entry.
228             $this->clientError(_('Atom post must be an Atom entry.'));
229             return;
230         }
231
232         $activity = new Activity($dom->documentElement);
233
234         $sub = null;
235
236         if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
237
238             if ($activity->verb != ActivityVerb::FOLLOW) {
239                 // TRANS: Client error displayed when not using the POST verb.
240                 // TRANS: Do not translate POST.
241                 $this->clientError(_('Can only handle Follow activities.'));
242                 return;
243             }
244
245             $person = $activity->objects[0];
246
247             if ($person->type != ActivityObject::PERSON) {
248                 $this->clientError(_('Can only follow people.'));
249                 return;
250             }
251
252             // XXX: OStatus discovery (maybe)
253
254             $profile = Profile::fromURI($person->id);
255
256             if (empty($profile)) {
257                 $this->clientError(sprintf(_('Unknown profile %s'), $person->id));
258                 return;
259             }
260
261             if (Subscription::start($this->_profile, $profile)) {
262                 $sub = Subscription::pkeyGet(array('subscriber' => $this->_profile->id,
263                                                    'subscribed' => $profile->id));
264             }
265
266             Event::handle('EndAtomPubNewActivity', array($activity, $sub));
267         }
268
269         if (!empty($sub)) {
270             $act = $sub->asActivity();
271
272             header('Content-Type: application/atom+xml; charset=utf-8');
273             header('Content-Location: ' . $act->selfLink);
274
275             $this->startXML();
276             $this->raw($act->asString(true, true, true));
277             $this->endXML();
278         }
279     }
280
281     /**
282      * Return true if read only.
283      *
284      * @param array $args other arguments
285      *
286      * @return boolean is read only action?
287      */
288
289     function isReadOnly($args)
290     {
291         return $_SERVER['REQUEST_METHOD'] != 'POST';
292     }
293
294     /**
295      * Return last modified, if applicable.
296      *
297      * @return string last modified http header
298      */
299
300     function lastModified()
301     {
302         return null;
303     }
304
305     /**
306      * Return etag, if applicable.
307      *
308      * @return string etag http header
309      */
310
311     function etag()
312     {
313         return null;
314     }
315
316     /**
317      * Does this require authentication?
318      *
319      * @return boolean true if delete, else false
320      */
321
322     function requiresAuth()
323     {
324         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
325             return true;
326         } else {
327             return false;
328         }
329     }
330 }