3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
34 * Show a single subscription
38 * @author Evan Prodromou <evan@status.net>
39 * @copyright 2010 StatusNet, Inc.
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41 * @link http://status.net/
43 class AtompubshowsubscriptionAction extends AtompubAction
45 private $_subscriber = null;
46 private $_subscribed = null;
47 private $_subscription = null;
49 protected function atompubPrepare()
51 $subscriberId = $this->trimmed('subscriber');
53 $this->_subscriber = Profile::getKV('id', $subscriberId);
55 if (!$this->_subscriber instanceof Profile) {
56 // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
57 // TRANS: %d is the non-existing profile ID number.
58 throw new ClientException(sprintf(_('No such profile id: %d.'),
62 $subscribedId = $this->trimmed('subscribed');
64 $this->_subscribed = Profile::getKV('id', $subscribedId);
66 if (!$this->_subscribed instanceof Profile) {
67 // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
68 // TRANS: %d is the non-existing profile ID number.
69 throw new ClientException(sprintf(_('No such profile id: %d.'),
73 if (!$this->_subscriber->isSubscribed($this->_subscribed)) {
74 // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
75 // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
76 $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
77 $subscriberId, $subscribedId);
78 throw new ClientException($msg, 404);
84 protected function handleGet()
86 $this->showSubscription();
89 protected function handleDelete()
91 $this->deleteSubscription();
95 * Show the subscription in ActivityStreams Atom format.
99 function showSubscription()
101 $activity = $this->_subscription->asActivity();
103 header('Content-Type: application/atom+xml; charset=utf-8');
106 $this->raw($activity->asString(true, true, true));
111 * Delete the subscription
115 function deleteSubscription()
117 if (!$this->scoped instanceof Profile ||
118 $this->scoped->id != $this->_subscriber->id) {
119 // TRANS: Client exception thrown when trying to delete a subscription of another user.
120 throw new ClientException(_("Cannot delete someone else's subscription."), 403);
123 Subscription::cancel($this->_subscriber, $this->_subscribed);
127 * Is this action read only?
129 * @param array $args other arguments
131 * @return boolean true
133 function isReadOnly($args)
135 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
143 * Return last modified, if applicable.
145 * @return string last modified http header
147 function lastModified()
149 return max(strtotime($this->_subscriber->modified),
150 strtotime($this->_subscribed->modified),
151 strtotime($this->_subscription->modified));
155 * Etag for this object
157 * @return string etag http header
161 $mtime = strtotime($this->_subscription->modified);
163 return 'W/"' . implode(':', array('AtomPubShowSubscription',
164 $this->_subscriber->id,
165 $this->_subscribed->id,
170 * Does this require authentication?
172 * @return boolean true if delete, else false
174 function requiresAuth()
176 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {