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('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Show a single subscription
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2010 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class AtompubshowsubscriptionAction extends ApiAuthAction
49 private $_subscriber = null;
50 private $_subscribed = null;
51 private $_subscription = null;
54 * For initializing members of the class.
56 * @param array $argarray misc. arguments
58 * @return boolean true
60 function prepare($argarray)
62 parent::prepare($argarray);
63 $subscriberId = $this->trimmed('subscriber');
65 $this->_subscriber = Profile::getKV('id', $subscriberId);
67 if (empty($this->_subscriber)) {
68 // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
69 // TRANS: %d is the non-existing profile ID number.
70 throw new ClientException(sprintf(_('No such profile id: %d.'),
74 $subscribedId = $this->trimmed('subscribed');
76 $this->_subscribed = Profile::getKV('id', $subscribedId);
78 if (empty($this->_subscribed)) {
79 // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
80 // TRANS: %d is the non-existing profile ID number.
81 throw new ClientException(sprintf(_('No such profile id: %d.'),
85 $this->_subscription =
86 Subscription::pkeyGet(array('subscriber' => $subscriberId,
87 'subscribed' => $subscribedId));
89 if (empty($this->_subscription)) {
90 // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
91 // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
92 $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
93 $subscriberId, $subscribedId);
94 throw new ClientException($msg, 404);
103 * @param array $argarray is ignored since it's now passed in in prepare()
107 function handle($argarray=null)
109 parent::handle($argarray);
110 switch ($_SERVER['REQUEST_METHOD']) {
113 $this->showSubscription();
116 $this->deleteSubscription();
119 // TRANS: Client error shown when using a non-supported HTTP method.
120 $this->clientError(_('HTTP method not supported.'), 405);
127 * Show the subscription in ActivityStreams Atom format.
131 function showSubscription()
133 $activity = $this->_subscription->asActivity();
135 header('Content-Type: application/atom+xml; charset=utf-8');
138 $this->raw($activity->asString(true, true, true));
145 * Delete the subscription
149 function deleteSubscription()
151 if (empty($this->auth_user) ||
152 $this->auth_user->id != $this->_subscriber->id) {
153 // TRANS: Client exception thrown when trying to delete a subscription of another user.
154 throw new ClientException(_("Cannot delete someone else's ".
155 "subscription."), 403);
158 Subscription::cancel($this->_subscriber,
165 * Is this action read only?
167 * @param array $args other arguments
169 * @return boolean true
171 function isReadOnly($args)
173 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
181 * Return last modified, if applicable.
183 * @return string last modified http header
185 function lastModified()
187 return max(strtotime($this->_subscriber->modified),
188 strtotime($this->_subscribed->modified),
189 strtotime($this->_subscription->modified));
193 * Etag for this object
195 * @return string etag http header
199 $mtime = strtotime($this->_subscription->modified);
201 return 'W/"' . implode(':', array('AtomPubShowSubscription',
202 $this->_subscriber->id,
203 $this->_subscribed->id,
208 * Does this require authentication?
210 * @return boolean true if delete, else false
212 function requiresAuth()
214 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {