]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubmembershipfeed.php
Merge branch '1.0.x' into qna
[quix0rs-gnu-social.git] / actions / atompubmembershipfeed.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Feed of group memberships for a user, in ActivityStreams format
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  AtomPub
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  * Feed of group memberships for a user, in ActivityStreams format
41  *
42  * @category  Action
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 AtompubmembershipfeedAction extends ApiAuthAction
50 {
51     private $_profile     = null;
52     private $_memberships = 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         $profileId = $this->trimmed('profile');
66
67         $this->_profile = Profile::staticGet('id', $profileId);
68
69         if (empty($this->_profile)) {
70             // TRANS: Client exception.
71             throw new ClientException(_('No such profile.'), 404);
72         }
73
74         $offset = ($this->page-1) * $this->count;
75         $limit  = $this->count + 1;
76
77         $this->_memberships = Group_member::byMember($this->_profile->id,
78                                                      $offset,
79                                                      $limit);
80
81         return true;
82     }
83
84     /**
85      * Handler method
86      *
87      * @param array $argarray is ignored since it's now passed in in prepare()
88      *
89      * @return void
90      */
91     function handle($argarray=null)
92     {
93         parent::handle($argarray);
94
95         switch ($_SERVER['REQUEST_METHOD']) {
96         case 'HEAD':
97         case 'GET':
98             $this->showFeed();
99             break;
100         case 'POST':
101             $this->addMembership();
102             break;
103         default:
104             // TRANS: Client exception thrown when using an unsupported HTTP method.
105             throw new ClientException(_('HTTP method not supported.'), 405);
106             return;
107         }
108
109         return;
110     }
111
112     /**
113      * Show a feed of favorite activity streams objects
114      *
115      * @return void
116      */
117     function showFeed()
118     {
119         header('Content-Type: application/atom+xml; charset=utf-8');
120
121         $url = common_local_url('AtomPubMembershipFeed',
122                                 array('profile' => $this->_profile->id));
123
124         $feed = new Atom10Feed(true);
125
126         $feed->addNamespace('activity',
127                             'http://activitystrea.ms/spec/1.0/');
128
129         $feed->addNamespace('poco',
130                             'http://portablecontacts.net/spec/1.0');
131
132         $feed->addNamespace('media',
133                             'http://purl.org/syndication/atommedia');
134
135         $feed->id = $url;
136
137         $feed->setUpdated('now');
138
139         $feed->addAuthor($this->_profile->getBestName(),
140                          $this->_profile->getURI());
141
142         // TRANS: Title for group membership feed.
143         // TRANS: %s is a username.
144         $feed->setTitle(sprintf(_("%s group memberships"),
145                                 $this->_profile->getBestName()));
146
147         // TRANS: Subtitle for group membership feed.
148         // TRANS: %1$s is a username, %2$s is the StatusNet sitename.
149         $feed->setSubtitle(sprintf(_('Groups %1$s is a member of on %2$s'),
150                                    $this->_profile->getBestName(),
151                                    common_config('site', 'name')));
152
153         $feed->addLink(common_local_url('usergroups',
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('AtomPubMembershipFeed',
169                                             array('profile' =>
170                                                   $this->_profile->id),
171                                             array('page' =>
172                                                   $this->page - 1)),
173                            array('rel' => 'prev',
174                                  'type' => 'application/atom+xml'));
175         }
176
177         if ($this->_memberships->N > $this->count) {
178
179             $feed->addLink(common_local_url('AtomPubMembershipFeed',
180                                             array('profile' =>
181                                                   $this->_profile->id),
182                                             array('page' =>
183                                                   $this->page + 1)),
184                            array('rel' => 'next',
185                                  'type' => 'application/atom+xml'));
186         }
187
188         $i = 0;
189
190         while ($this->_memberships->fetch()) {
191
192             // We get one more than needed; skip that one
193
194             $i++;
195
196             if ($i > $this->count) {
197                 break;
198             }
199
200             $act = $this->_memberships->asActivity();
201             $feed->addEntryRaw($act->asString(false, false, false));
202         }
203
204         $this->raw($feed->getString());
205     }
206
207     /**
208      * add a new favorite
209      *
210      * @return void
211      */
212     function addMembership()
213     {
214         // XXX: Refactor this; all the same for atompub
215
216         if (empty($this->auth_user) ||
217             $this->auth_user->id != $this->_profile->id) {
218             // TRANS: Client exception thrown when trying subscribe someone else to a group.
219             throw new ClientException(_("Cannot add someone else's".
220                                         " membership."), 403);
221         }
222
223         $xml = file_get_contents('php://input');
224
225         $dom = DOMDocument::loadXML($xml);
226
227         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
228             $dom->documentElement->localName != 'entry') {
229             // TRANS: Client error displayed when not using an Atom entry.
230             throw new ClientException(_('Atom post must be an Atom entry.'));
231             return;
232         }
233
234         $activity = new Activity($dom->documentElement);
235
236         $membership = null;
237
238         if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
239             if ($activity->verb != ActivityVerb::JOIN) {
240                 // TRANS: Client error displayed when not using the POST verb.
241                 // TRANS: Do not translate POST.
242                 throw new ClientException(_('Can only handle join activities.'));
243                 return;
244             }
245
246             $groupObj = $activity->objects[0];
247
248             if ($groupObj->type != ActivityObject::GROUP) {
249                 // TRANS: Client exception thrown when trying favorite an object that is not a notice.
250                 throw new ClientException(_('Can only fave notices.'));
251                 return;
252             }
253
254             $group = User_group::staticGet('uri', $groupObj->id);
255
256             if (empty($group)) {
257                 // XXX: import from listed URL or something
258                 // TRANS: Client exception thrown when trying to subscribe to a non-existing group.
259                 throw new ClientException(_('Unknown group.'));
260             }
261
262             $old = Group_member::pkeyGet(array('profile_id' => $this->auth_user->id,
263                                                'group_id' => $group->id));
264
265             if (!empty($old)) {
266                 // TRANS: Client exception thrown when trying to subscribe to an already subscribed group.
267                 throw new ClientException(_('Already a member.'));
268             }
269
270             $profile = $this->auth_user->getProfile();
271
272             if (Group_block::isBlocked($group, $profile)) {
273                 // XXX: import from listed URL or something
274                 // TRANS: Client exception thrown when trying to subscribe to group while blocked from that group.
275                 throw new ClientException(_('Blocked by admin.'));
276             }
277
278             $this->auth_user->joinGroup($group);
279
280             Event::handle('EndAtomPubNewActivity', array($activity, $membership));
281         }
282
283         if (!empty($membership)) {
284             $act = $membership->asActivity();
285
286             header('Content-Type: application/atom+xml; charset=utf-8');
287             header('Content-Location: ' . $act->selfLink);
288
289             $this->startXML();
290             $this->raw($act->asString(true, true, true));
291             $this->endXML();
292         }
293     }
294
295     /**
296      * Return true if read only.
297      *
298      * MAY override
299      *
300      * @param array $args other arguments
301      *
302      * @return boolean is read only action?
303      */
304     function isReadOnly($args)
305     {
306         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
307             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
308             return true;
309         } else {
310             return false;
311         }
312     }
313
314     /**
315      * Return last modified, if applicable.
316      *
317      * MAY override
318      *
319      * @return string last modified http header
320      */
321     function lastModified()
322     {
323         // For comparison with If-Last-Modified
324         // If not applicable, return null
325         return null;
326     }
327
328     /**
329      * Return etag, if applicable.
330      *
331      * MAY override
332      *
333      * @return string etag http header
334      */
335     function etag()
336     {
337         return null;
338     }
339
340     /**
341      * Does this require authentication?
342      *
343      * @return boolean true if delete, else false
344      */
345     function requiresAuth()
346     {
347         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
348             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
349             return false;
350         } else {
351             return true;
352         }
353     }
354 }