]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowmembership.php
Merge branch 'master' into 1.0.x
[quix0rs-gnu-social.git] / actions / atompubshowmembership.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Show a single membership as an Activity Streams entry
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  AtomPub
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 require_once INSTALLDIR . '/lib/apiauth.php';
38
39 /**
40  * Show (or delete) a single membership event as an ActivityStreams entry
41  *
42  * @category  AtomPub
43  * @package   StatusNet
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/
48  */
49 class AtompubshowmembershipAction extends ApiAuthAction
50 {
51     private $_profile    = null;
52     private $_group      = null;
53     private $_membership = null;
54
55     /**
56      * For initializing members of the class.
57      *
58      * @param array $argarray misc. arguments
59      *
60      * @return boolean true
61      */
62     function prepare($argarray)
63     {
64         parent::prepare($argarray);
65
66         $profileId = $this->trimmed('profile');
67
68         $this->_profile = Profile::staticGet('id', $profileId);
69
70         if (empty($this->_profile)) {
71             // TRANS: Client exception.
72             throw new ClientException(_('No such profile.'), 404);
73         }
74
75         $groupId = $this->trimmed('group');
76
77         $this->_group = User_group::staticGet('id', $groupId);
78
79         if (empty($this->_group)) {
80             // TRANS: Client exception thrown when referencing a non-existing group.
81             throw new ClientException(_('No such group.'), 404);
82         }
83
84         $kv = array('group_id' => $groupId,
85                     'profile_id' => $profileId);
86
87         $this->_membership = Group_member::pkeyGet($kv);
88
89         if (empty($this->_membership)) {
90             // TRANS: Client exception thrown when trying to show membership of a non-subscribed group
91             throw new ClientException(_('Not a member.'), 404);
92         }
93
94         return true;
95     }
96
97     /**
98      * Handler method
99      *
100      * @param array $argarray is ignored since it's now passed in in prepare()
101      *
102      * @return void
103      */
104     function handle($argarray=null)
105     {
106         switch ($_SERVER['REQUEST_METHOD']) {
107         case 'GET':
108         case 'HEAD':
109             $this->showMembership();
110             break;
111         case 'DELETE':
112             $this->deleteMembership();
113             break;
114         default:
115             // TRANS: Client exception thrown when using an unsupported HTTP method.
116             throw new ClientException(_('HTTP method not supported.'), 405);
117             break;
118         }
119         return;
120     }
121
122     /**
123      * show a single membership
124      *
125      * @return void
126      */
127     function showMembership()
128     {
129         $activity = $this->_membership->asActivity();
130
131         header('Content-Type: application/atom+xml; charset=utf-8');
132
133         $this->startXML();
134         $this->raw($activity->asString(true, true, true));
135         $this->endXML();
136
137         return;
138     }
139
140     /**
141      * Delete the membership (leave the group)
142      *
143      * @return void
144      */
145     function deleteMembership()
146     {
147         if (empty($this->auth_user) ||
148             $this->auth_user->id != $this->_profile->id) {
149             // TRANS: Client exception thrown when deleting someone else's membership.
150             throw new ClientException(_("Cannot delete someone else's".
151                                         " membership."), 403);
152         }
153
154         $this->auth_user->leaveGroup($this->_group);
155
156         return;
157     }
158
159     /**
160      * Return true if read only.
161      *
162      * MAY override
163      *
164      * @param array $args other arguments
165      *
166      * @return boolean is read only action?
167      */
168     function isReadOnly($args)
169     {
170         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
171             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
172             return true;
173         } else {
174             return false;
175         }
176     }
177
178     /**
179      * Return last modified, if applicable.
180      *
181      * Because the representation depends on the profile and group,
182      * our last modified value is the maximum of their mod time
183      * with the actual membership's mod time.
184      *
185      * @return string last modified http header
186      */
187     function lastModified()
188     {
189         return max(strtotime($this->_profile->modified),
190                    strtotime($this->_group->modified),
191                    strtotime($this->_membership->modified));
192     }
193
194     /**
195      * Return etag, if applicable.
196      *
197      * A "weak" Etag including the profile and group id as well as
198      * the admin flag and ctime of the membership.
199      *
200      * @return string etag http header
201      */
202     function etag()
203     {
204         $ctime = strtotime($this->_membership->created);
205
206         $adminflag = ($this->_membership->is_admin) ? 't' : 'f';
207
208         return 'W/"' . implode(':', array('AtomPubShowMembership',
209                                           $this->_profile->id,
210                                           $this->_group->id,
211                                           $adminflag,
212                                           $ctime)) . '"';
213     }
214
215     /**
216      * Does this require authentication?
217      *
218      * @return boolean true if delete, else false
219      */
220     function requiresAuth()
221     {
222         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
223             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
224             return false;
225         } else {
226             return true;
227         }
228     }
229 }