]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubsubscriptionfeed.php
Don't accept non-objects before testing with "instanceof".
[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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
32
33 /**
34  * Subscription feed class for AtomPub
35  *
36  * Generates a list of the user's subscriptions
37  *
38  * @category  AtomPub
39  * @package   StatusNet
40  * @author    Evan Prodromou <evan@status.net>
41  * @copyright 2010 StatusNet, Inc.
42  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
43  * @link      http://status.net/
44  */
45 class AtompubsubscriptionfeedAction extends AtompubAction
46 {
47     private $_profile       = null;
48     private $_subscriptions = null;
49
50     protected function atompubPrepare()
51     {
52         $subscriber = $this->trimmed('subscriber');
53
54         $this->_profile = Profile::getKV('id', $subscriber);
55
56         if (!$this->_profile instanceof Profile) {
57             // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
58             // TRANS: %d is the non-existing profile ID number.
59             throw new ClientException(sprintf(_('No such profile id: %d.'),
60                                               $subscriber), 404);
61         }
62
63         $this->_subscriptions = Subscription::bySubscriber($this->_profile->id,
64                                                            $this->offset,
65                                                            $this->limit);
66
67         return true;
68     }
69
70     protected function handleGet()
71     {
72         $this->showFeed();
73     }
74
75     protected function handlePost()
76     {
77         $this->addSubscription();
78     }
79
80     /**
81      * Show the feed of subscriptions
82      *
83      * @return void
84      */
85     function showFeed()
86     {
87         header('Content-Type: application/atom+xml; charset=utf-8');
88
89         $url = common_local_url('AtomPubSubscriptionFeed',
90                                 array('subscriber' => $this->_profile->id));
91
92         $feed = new Atom10Feed(true);
93
94         $feed->addNamespace('activity',
95                             'http://activitystrea.ms/spec/1.0/');
96
97         $feed->addNamespace('poco',
98                             'http://portablecontacts.net/spec/1.0');
99
100         $feed->addNamespace('media',
101                             'http://purl.org/syndication/atommedia');
102
103         $feed->addNamespace('georss',
104                             'http://www.georss.org/georss');
105
106         $feed->id = $url;
107
108         $feed->setUpdated('now');
109
110         $feed->addAuthor($this->_profile->getBestName(),
111                          $this->_profile->getURI());
112
113         // TRANS: Title for Atom subscription feed.
114         // TRANS: %s is a user nickname.
115         $feed->setTitle(sprintf(_("%s subscriptions"),
116                                 $this->_profile->getBestName()));
117
118         // TRANS: Subtitle for Atom subscription feed.
119         // TRANS: %1$s is a user nickname, %s$s is the StatusNet sitename.
120         $feed->setSubtitle(sprintf(_("People %1\$s has subscribed to on %2\$s"),
121                                    $this->_profile->getBestName(),
122                                    common_config('site', 'name')));
123
124         $feed->addLink(common_local_url('subscriptions',
125                                         array('nickname' =>
126                                               $this->_profile->nickname)));
127
128         $feed->addLink($url,
129                        array('rel' => 'self',
130                              'type' => 'application/atom+xml'));
131
132         // If there's more...
133
134         if ($this->page > 1) {
135             $feed->addLink($url,
136                            array('rel' => 'first',
137                                  'type' => 'application/atom+xml'));
138
139             $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
140                                             array('subscriber' =>
141                                                   $this->_profile->id),
142                                             array('page' =>
143                                                   $this->page - 1)),
144                            array('rel' => 'prev',
145                                  'type' => 'application/atom+xml'));
146         }
147
148         if ($this->_subscriptions->N > $this->count) {
149
150             $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
151                                             array('subscriber' =>
152                                                   $this->_profile->id),
153                                             array('page' =>
154                                                   $this->page + 1)),
155                            array('rel' => 'next',
156                                  'type' => 'application/atom+xml'));
157         }
158
159         $i = 0;
160
161         // XXX: This is kind of inefficient
162
163         while ($this->_subscriptions->fetch()) {
164
165             // We get one more than needed; skip that one
166
167             $i++;
168
169             if ($i > $this->count) {
170                 break;
171             }
172
173             $act = $this->_subscriptions->asActivity();
174             $feed->addEntryRaw($act->asString(false, false, false));
175         }
176
177         $this->raw($feed->getString());
178     }
179
180     /**
181      * Add a new subscription
182      *
183      * Handling the POST method for AtomPub
184      *
185      * @return void
186      */
187     function addSubscription()
188     {
189         if (empty($this->auth_user) ||
190             $this->auth_user->id != $this->_profile->id) {
191             // TRANS: Client exception thrown when trying to subscribe another user.
192             throw new ClientException(_("Cannot add someone else's".
193                                         " subscription."), 403);
194         }
195
196         $xml = file_get_contents('php://input');
197
198         $dom = DOMDocument::loadXML($xml);
199
200         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
201             $dom->documentElement->localName != 'entry') {
202             // TRANS: Client error displayed when not using an Atom entry.
203             $this->clientError(_('Atom post must be an Atom entry.'));
204         }
205
206         $activity = new Activity($dom->documentElement);
207
208         $sub = null;
209
210         if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
211
212             if ($activity->verb != ActivityVerb::FOLLOW) {
213                 // TRANS: Client error displayed when not using the follow verb.
214                 $this->clientError(_('Can only handle Follow activities.'));
215             }
216
217             $person = $activity->objects[0];
218
219             if ($person->type != ActivityObject::PERSON) {
220                 // TRANS: Client exception thrown when subscribing to an object that is not a person.
221                 $this->clientError(_('Can only follow people.'));
222             }
223
224             // XXX: OStatus discovery (maybe)
225             try {
226                 $profile = Profile::fromUri($person->id);
227             } catch (UnknownUriException $e) {
228                 // TRANS: Client exception thrown when subscribing to a non-existing profile.
229                 // TRANS: %s is the unknown profile ID.
230                 $this->clientError(sprintf(_('Unknown profile %s.'), $person->id));
231             }
232
233             try {
234                 $sub = Subscription::start($this->_profile, $profile);
235             } catch (AlreadyFulfilledException $e) {
236                 // 409 Conflict
237                 $this->clientError($e->getMessage(), 409);
238             }
239
240             Event::handle('EndAtomPubNewActivity', array($activity, $sub));
241         }
242
243         if (!empty($sub)) {
244             $act = $sub->asActivity();
245
246             header('Content-Type: application/atom+xml; charset=utf-8');
247             header('Content-Location: ' . $act->selfLink);
248
249             $this->startXML();
250             $this->raw($act->asString(true, true, true));
251             $this->endXML();
252         }
253     }
254 }