]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubshowmembership.php
Opps, PEAR sucks. Need to call find() before fetch() ... :-(
[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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
32
33 /**
34  * Show (or delete) a single membership event as an ActivityStreams entry
35  *
36  * @category  AtomPub
37  * @package   StatusNet
38  * @author    Evan Prodromou <evan@status.net>
39  * @copyright 2010 StatusNet, Inc.
40  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41  * @link      http://status.net/
42  */
43 class AtompubshowmembershipAction extends AtompubAction
44 {
45     private $_private    = null;
46     private $_group      = null;
47     private $_membership = null;
48
49     protected function atompubPrepare()
50     {
51         $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
52
53         if (!$this->_profile instanceof Profile) {
54             // TRANS: Client exception.
55             throw new ClientException(_('No such profile.'), 404);
56         }
57
58         $this->_group = User_group::getKV('id', $this->trimmed('group'));
59
60         if (!$this->_group instanceof User_group) {
61             // TRANS: Client exception thrown when referencing a non-existing group.
62             throw new ClientException(_('No such group.'), 404);
63         }
64
65         $kv = array('group_id' => $groupId,
66                     'profile_id' => $this->_profile->id);
67
68         $this->_membership = Group_member::pkeyGet($kv);
69
70         if (!$this->_membership instanceof Group_member) {
71             // TRANS: Client exception thrown when trying to show membership of a non-subscribed group
72             throw new ClientException(_('Not a member.'), 404);
73         }
74
75         return true;
76     }
77
78     protected function handleGet() {
79         return $this->showMembership();
80     }
81
82     protected function handleDelete() {
83         return $this->deleteMembership();
84     }
85
86     /**
87      * show a single membership
88      *
89      * @return void
90      */
91     function showMembership()
92     {
93         $activity = $this->_membership->asActivity();
94
95         header('Content-Type: application/atom+xml; charset=utf-8');
96
97         $this->startXML();
98         $this->raw($activity->asString(true, true, true));
99         $this->endXML();
100
101         return;
102     }
103
104     /**
105      * Delete the membership (leave the group)
106      *
107      * @return void
108      */
109     function deleteMembership()
110     {
111         if (empty($this->auth_user) ||
112             $this->auth_user->id != $this->_profile->id) {
113             // TRANS: Client exception thrown when deleting someone else's membership.
114             throw new ClientException(_("Cannot delete someone else's".
115                                         " membership."), 403);
116         }
117
118         $this->auth_user->leaveGroup($this->_group);
119
120         return;
121     }
122
123     /**
124      * Return last modified, if applicable.
125      *
126      * Because the representation depends on the profile and group,
127      * our last modified value is the maximum of their mod time
128      * with the actual membership's mod time.
129      *
130      * @return string last modified http header
131      */
132     function lastModified()
133     {
134         return max(strtotime($this->_profile->modified),
135                    strtotime($this->_group->modified),
136                    strtotime($this->_membership->modified));
137     }
138
139     /**
140      * Return etag, if applicable.
141      *
142      * A "weak" Etag including the profile and group id as well as
143      * the admin flag and ctime of the membership.
144      *
145      * @return string etag http header
146      */
147     function etag()
148     {
149         $ctime = strtotime($this->_membership->created);
150
151         $adminflag = ($this->_membership->is_admin) ? 't' : 'f';
152
153         return 'W/"' . implode(':', array('AtomPubShowMembership',
154                                           $this->_profile->id,
155                                           $this->_group->id,
156                                           $adminflag,
157                                           $ctime)) . '"';
158     }
159 }