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 $_subscriptions = 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
80 $offset = ($this->page-1) * $this->count;
82 $this->_subscriptions = Subscription::bySubscriber($subscriber,
92 * @param array $argarray is ignored since it's now passed in in prepare()
97 function handle($argarray=null)
99 parent::handle($argarray);
100 switch ($_SERVER['REQUEST_METHOD']) {
106 $this->addSubscription();
109 $this->clientError(_('HTTP method not supported.'), 405);
117 * Show the feed of subscriptions
124 header('Content-Type: application/atom+xml; charset=utf-8');
126 $url = common_local_url('AtomPubSubscriptionFeed',
127 array('subscriber' => $this->_profile->id));
129 $feed = new Atom10Feed(true);
131 $feed->addNamespace('activity',
132 'http://activitystrea.ms/spec/1.0/');
134 $feed->addNamespace('poco',
135 'http://portablecontacts.net/spec/1.0');
137 $feed->addNamespace('media',
138 'http://purl.org/syndication/atommedia');
142 $feed->setUpdated('now');
144 $feed->addAuthor($this->_profile->getBestName(),
145 $this->_profile->getURI());
147 $feed->setTitle(sprintf(_("%s subscriptions"),
148 $this->_profile->getBestName()));
150 $feed->setSubtitle(sprintf(_("People %s has subscribed to on %s"),
151 $this->_profile->getBestName(),
152 common_config('site', 'name')));
154 $feed->addLink(common_local_url('subscriptions',
156 $this->_profile->nickname)));
159 array('rel' => 'self',
160 'type' => 'application/atom+xml'));
162 // If there's more...
164 if ($this->page > 1) {
166 array('rel' => 'first',
167 'type' => 'application/atom+xml'));
169 $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
170 array('subscriber' =>
171 $this->_profile->id),
174 array('rel' => 'prev',
175 'type' => 'application/atom+xml'));
178 if ($this->_subscriptions->N > $this->count) {
180 $feed->addLink(common_local_url('AtomPubSubscriptionFeed',
181 array('subscriber' =>
182 $this->_profile->id),
185 array('rel' => 'next',
186 'type' => 'application/atom+xml'));
191 // XXX: This is kind of inefficient
193 while ($this->_subscriptions->fetch()) {
195 // We get one more than needed; skip that one
199 if ($i > $this->count) {
203 $act = $this->_subscriptions->asActivity();
204 $feed->addEntryRaw($act->asString(false, false, false));
207 $this->raw($feed->getString());
211 * Add a new subscription
213 * Handling the POST method for AtomPub
218 function addSubscription()
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);
226 $xml = file_get_contents('php://input');
228 $dom = DOMDocument::loadXML($xml);
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.'));
237 $activity = new Activity($dom->documentElement);
241 if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
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.'));
250 $person = $activity->objects[0];
252 if ($person->type != ActivityObject::PERSON) {
253 $this->clientError(_('Can only follow people.'));
257 // XXX: OStatus discovery (maybe)
259 $profile = Profile::fromURI($person->id);
261 if (empty($profile)) {
262 $this->clientError(sprintf(_('Unknown profile %s'), $person->id));
266 if (Subscription::start($this->_profile, $profile)) {
267 $sub = Subscription::pkeyGet(array('subscriber' => $this->_profile->id,
268 'subscribed' => $profile->id));
271 Event::handle('EndAtomPubNewActivity', array($activity, $sub));
275 $act = $sub->asActivity();
277 header('Content-Type: application/atom+xml; charset=utf-8');
278 header('Content-Location: ' . $act->selfLink);
281 $this->raw($act->asString(true, true, true));
287 * Return true if read only.
289 * @param array $args other arguments
291 * @return boolean is read only action?
294 function isReadOnly($args)
296 return $_SERVER['REQUEST_METHOD'] != 'POST';
300 * Return last modified, if applicable.
302 * @return string last modified http header
305 function lastModified()
311 * Return etag, if applicable.
313 * @return string etag http header
322 * Does this require authentication?
324 * @return boolean true if delete, else false
327 function requiresAuth()
329 if ($_SERVER['REQUEST_METHOD'] == 'POST') {