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.
37 require_once INSTALLDIR . '/lib/apiauth.php';
40 * Show a single subscription
44 * @author Evan Prodromou <evan@status.net>
45 * @copyright 2010 StatusNet, Inc.
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47 * @link http://status.net/
49 class AtompubshowsubscriptionAction extends ApiAuthAction
51 private $_subscriber = null;
52 private $_subscribed = null;
53 private $_subscription = null;
56 * For initializing members of the class.
58 * @param array $argarray misc. arguments
60 * @return boolean true
62 function prepare($argarray)
64 parent::prepare($argarray);
65 $subscriberId = $this->trimmed('subscriber');
67 $this->_subscriber = Profile::staticGet('id', $subscriberId);
69 if (empty($this->_subscriber)) {
70 // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
71 // TRANS: %d is the non-existing profile ID number.
72 throw new ClientException(sprintf(_('No such profile id: %d.'),
76 $subscribedId = $this->trimmed('subscribed');
78 $this->_subscribed = Profile::staticGet('id', $subscribedId);
80 if (empty($this->_subscribed)) {
81 // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
82 // TRANS: %d is the non-existing profile ID number.
83 throw new ClientException(sprintf(_('No such profile id: %d.'),
87 $this->_subscription =
88 Subscription::pkeyGet(array('subscriber' => $subscriberId,
89 'subscribed' => $subscribedId));
91 if (empty($this->_subscription)) {
92 // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
93 // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
94 $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
95 $subscriberId, $subscribedId);
96 throw new ClientException($msg, 404);
105 * @param array $argarray is ignored since it's now passed in in prepare()
109 function handle($argarray=null)
111 parent::handle($argarray);
112 switch ($_SERVER['REQUEST_METHOD']) {
115 $this->showSubscription();
118 $this->deleteSubscription();
121 // TRANS: Client error shown when using a non-supported HTTP method.
122 $this->clientError(_('HTTP method not supported.'), 405);
130 * Show the subscription in ActivityStreams Atom format.
134 function showSubscription()
136 $activity = $this->_subscription->asActivity();
138 header('Content-Type: application/atom+xml; charset=utf-8');
141 $this->raw($activity->asString(true, true, true));
148 * Delete the subscription
152 function deleteSubscription()
154 if (empty($this->auth_user) ||
155 $this->auth_user->id != $this->_subscriber->id) {
156 // TRANS: Client exception thrown when trying to delete a subscription of another user.
157 throw new ClientException(_("Cannot delete someone else's ".
158 "subscription."), 403);
161 Subscription::cancel($this->_subscriber,
168 * Is this action read only?
170 * @param array $args other arguments
172 * @return boolean true
174 function isReadOnly($args)
176 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
184 * Return last modified, if applicable.
186 * @return string last modified http header
188 function lastModified()
190 return max(strtotime($this->_subscriber->modified),
191 strtotime($this->_subscribed->modified),
192 strtotime($this->_subscription->modified));
196 * Etag for this object
198 * @return string etag http header
202 $mtime = strtotime($this->_subscription->modified);
204 return 'W/"' . implode(':', array('AtomPubShowSubscription',
205 $this->_subscriber->id,
206 $this->_subscribed->id,
211 * Does this require authentication?
213 * @return boolean true if delete, else false
215 function requiresAuth()
217 if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {