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 $this->_subscription = Subscription::pkeyGet(array('subscriber' => $subscriberId,
74 'subscribed' => $subscribedId));
75 if (!$this->_subscription instanceof Subscription) {
76 // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
77 // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
78 $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
79 $subscriberId, $subscribedId);
80 throw new ClientException($msg, 404);
86 protected function handleGet()
88 $this->showSubscription();
91 protected function handleDelete()
93 $this->deleteSubscription();
97 * Show the subscription in ActivityStreams Atom format.
101 function showSubscription()
103 $activity = $this->_subscription->asActivity();
105 header('Content-Type: application/atom+xml; charset=utf-8');
108 $this->raw($activity->asString(true, true, true));
113 * Delete the subscription
117 function deleteSubscription()
119 if (!$this->scoped instanceof Profile ||
120 $this->scoped->id != $this->_subscriber->id) {
121 // TRANS: Client exception thrown when trying to delete a subscription of another user.
122 throw new ClientException(_("Cannot delete someone else's subscription."), 403);
125 Subscription::cancel($this->_subscriber, $this->_subscribed);
129 * Is this action read only?
131 * @param array $args other arguments
133 * @return boolean true
135 function isReadOnly($args)
137 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
145 * Return last modified, if applicable.
147 * @return string last modified http header
149 function lastModified()
151 return max(strtotime($this->_subscriber->modified),
152 strtotime($this->_subscribed->modified),
153 strtotime($this->_subscription->modified));
157 * Etag for this object
159 * @return string etag http header
163 $mtime = strtotime($this->_subscription->modified);
165 return 'W/"' . implode(':', array('AtomPubShowSubscription',
166 $this->_subscriber->id,
167 $this->_subscribed->id,
172 * Does this require authentication?
174 * @return boolean true if delete, else false
176 function requiresAuth()
178 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {