3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Show a single membership as an Activity Streams entry
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 (or delete) a single membership event as an ActivityStreams entry
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/
50 class AtompubshowmembershipAction extends ApiAuthAction
52 private $_profile = null;
53 private $_group = null;
54 private $_membership = null;
57 * For initializing members of the class.
59 * @param array $argarray misc. arguments
61 * @return boolean true
64 function prepare($argarray)
66 parent::prepare($argarray);
68 $profileId = $this->trimmed('profile');
70 $this->_profile = Profile::staticGet('id', $profileId);
72 if (empty($this->_profile)) {
73 throw new ClientException(_('No such profile.'), 404);
76 $groupId = $this->trimmed('group');
78 $this->_group = User_group::staticGet('id', $groupId);
80 if (empty($this->_group)) {
81 throw new ClientException(_('No such group'), 404);
84 $kv = array('group_id' => $groupId,
85 'profile_id' => $profileId);
87 $this->_membership = Group_member::pkeyGet($kv);
89 if (empty($this->_membership)) {
90 throw new ClientException(_('Not a member'), 404);
99 * @param array $argarray is ignored since it's now passed in in prepare()
104 function handle($argarray=null)
106 switch ($_SERVER['REQUEST_METHOD']) {
109 $this->showMembership();
112 $this->deleteMembership();
115 throw new ClientException(_('Method not supported'), 405);
122 * show a single membership
127 function showMembership()
129 $activity = $this->_membership->asActivity();
131 header('Content-Type: application/atom+xml; charset=utf-8');
134 $this->raw($activity->asString(true, true, true));
141 * Delete the membership (leave the group)
146 function deleteMembership()
148 if (empty($this->auth_user) ||
149 $this->auth_user->id != $this->_profile->id) {
150 throw new ClientException(_("Can't delete someone else's".
151 " membership"), 403);
154 if (Event::handle('StartLeaveGroup', array($this->_group, $this->auth_user))) {
155 Group_member::leave($this->_group->id, $this->auth_user->id);
156 Event::handle('EndLeaveGroup', array($this->_group, $this->auth_user));
163 * Return true if read only.
167 * @param array $args other arguments
169 * @return boolean is read only action?
172 function isReadOnly($args)
174 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
175 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
183 * Return last modified, if applicable.
185 * Because the representation depends on the profile and group,
186 * our last modified value is the maximum of their mod time
187 * with the actual membership's mod time.
189 * @return string last modified http header
191 function lastModified()
193 return max(strtotime($this->_profile->modified),
194 strtotime($this->_group->modified),
195 strtotime($this->_membership->modified));
199 * Return etag, if applicable.
201 * A "weak" Etag including the profile and group id as well as
202 * the admin flag and ctime of the membership.
204 * @return string etag http header
209 $ctime = strtotime($this->_membership->created);
211 $adminflag = ($this->_membership->is_admin) ? 't' : 'f';
213 return 'W/"' . implode(':', array('AtomPubShowMembership',
221 * Does this require authentication?
223 * @return boolean true if delete, else false
226 function requiresAuth()
228 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
229 $_SERVER['REQUEST_METHOD'] == 'HEAD') {