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