3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * AtomPub subscription feed
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
37 require_once INSTALLDIR . '/lib/apiauth.php';
40 * Subscription feed class for AtomPub
42 * Generates a list of the user's subscriptions
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/
52 class AtompubsubscriptionfeedAction extends ApiAuthAction
54 private $_profile = null;
55 private $_subscribed = null;
58 * For initializing members of the class.
60 * @param array $argarray misc. arguments
62 * @return boolean true
65 function prepare($argarray)
67 parent::prepare($argarray);
69 $subscriber = $this->trimmed('subscriber');
71 $this->_profile = Profile::staticGet('id', $subscriber);
73 if (empty($this->_profile)) {
74 throw new ClientException(sprintf(_('No such profile id: %d'),
78 // page and count from ApiAction
79 // Note: this is a list of profiles, not subscriptions
82 $this->_profile->getSubscriptions(($this->page-1) * $this->count,
91 * @param array $argarray is ignored since it's now passed in in prepare()
96 function handle($argarray=null)
98 parent::handle($argarray);
99 switch ($_SERVER['REQUEST_METHOD']) {
105 $this->addSubscription();
108 $this->clientError(_('HTTP method not supported.'), 405);
116 * Show the feed of subscriptions
123 header('Content-Type: application/atom+xml; charset=utf-8');
125 $url = common_local_url('AtomPubSubscriptionFeed',
126 array('subscriber' => $this->_profile->id));
128 $feed = new Atom10Feed(true);
130 $feed->addNamespace('activity',
131 'http://activitystrea.ms/spec/1.0/');
133 $feed->addNamespace('poco',
134 'http://portablecontacts.net/spec/1.0');
136 $feed->addNamespace('media',
137 'http://purl.org/syndication/atommedia');
141 $feed->setUpdated('now');
143 $feed->addAuthor($this->_profile->getBestName(),
144 $this->_profile->getURI());
146 $feed->setTitle(sprintf(_("%s subscriptions"),
147 $this->_profile->getBestName()));
149 $feed->setSubtitle(sprintf(_("People %s has subscribed to on %s"),
150 $this->_profile->getBestName()),
151 common_config('site', 'name'));
153 $feed->addLink(common_local_url('subscriptions',
155 $this->_profile->nickname)));
158 array('rel' => 'self',
159 'type' => 'application/atom+xml'));
161 // If there's more...
163 if ($this->page > 1) {
165 array('rel' => 'first',
166 'type' => 'application/atom+xml'));
168 $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
169 array('subscriber' =>
173 array('rel' => 'prev',
174 'type' => 'application/atom+xml'));
177 if ($this->_subscribed->N > $this->count) {
179 $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
180 array('subscriber' =>
184 array('rel' => 'next',
185 'type' => 'application/atom+xml'));
190 // XXX: This is kind of inefficient
192 while ($this->_subscribed->fetch()) {
194 // We get one more than needed; skip that one
198 if ($i > $this->count) {
202 $sub = Subscription::pkeyGet(array('subscriber' =>
205 $this->_subscribed->id));
206 $act = $sub->asActivity();
207 $feed->addEntryRaw($act->asString(false, false, false));
210 $this->raw($feed->getString());
213 function addSubscription()
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);
221 $xml = file_get_contents('php://input');
223 $dom = DOMDocument::loadXML($xml);
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.'));
232 $activity = new Activity($dom->documentElement);
236 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
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.'));
245 $person = $activity->objects[0];
247 if ($person->type != ActivityObject::PERSON) {
248 $this->clientError(_('Can only follow people.'));
252 // XXX: OStatus discovery (maybe)
254 $profile = Profile::fromURI($person->id);
256 if (empty($profile)) {
257 $this->clientError(sprintf(_('Unknown profile %s'), $person->id));
261 if (Subscription::start($this->_profile, $profile)) {
262 $sub = Subscription::pkeyGet(array('subscriber' => $this->_profile->id,
263 'subscribed' => $profile->id));
266 Event::handle('EndAtomPubNewActivity', array($activity, $sub));
270 $act = $sub->asActivity();
272 header('Content-Type: application/atom+xml; charset=utf-8');
273 header('Content-Location: ' . $act->selfLink);
276 $this->raw($act->asString(true, true, true));
282 * Return true if read only.
284 * @param array $args other arguments
286 * @return boolean is read only action?
289 function isReadOnly($args)
291 return $_SERVER['REQUEST_METHOD'] != 'POST';
295 * Return last modified, if applicable.
297 * @return string last modified http header
300 function lastModified()
306 * Return etag, if applicable.
308 * @return string etag http header
317 * Does this require authentication?
319 * @return boolean true if delete, else false
322 function requiresAuth()
324 if ($_SERVER['REQUEST_METHOD'] == 'POST') {