]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplist.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / apigrouplist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Check to see whether a user a member of a group
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Jeffery To <jeffery.to@gmail.com>
27  * @author    Zach Copley <zach@status.net>
28  * @copyright 2009 StatusNet, Inc.
29  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
31  * @link      http://status.net/
32  */
33
34 if (!defined('STATUSNET')) {
35     exit(1);
36 }
37
38 /**
39  * Returns whether a user is a member of a specified group.
40  *
41  * @category API
42  * @package  StatusNet
43  * @author   Craig Andrews <candrews@integralblue.com>
44  * @author   Evan Prodromou <evan@status.net>
45  * @author   Jeffery To <jeffery.to@gmail.com>
46  * @author   Zach Copley <zach@status.net>
47  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48  * @link     http://status.net/
49  */
50 class ApiGroupListAction extends ApiBareAuthAction
51 {
52     var $groups   = null;
53
54     /**
55      * Take arguments for running
56      *
57      * @param array $args $_REQUEST args
58      *
59      * @return boolean success flag
60      */
61     protected function prepare(array $args=array())
62     {
63         parent::prepare($args);
64
65         //TODO: Make sure this doesn't leak unwantedly for federated users
66         $this->target = $this->getTargetProfile(null);
67
68         if (!($this->target instanceof Profile)) {
69             // TRANS: Client error displayed when user not found for an action.
70             $this->clientError(_('No such user.'), 404);
71         }
72
73         $this->groups = $this->getGroups();
74
75         return true;
76     }
77
78     /**
79      * Handle the request
80      *
81      * Show the user's groups
82      *
83      * @return void
84      */
85     protected function handle()
86     {
87         parent::handle();
88
89         $sitename   = common_config('site', 'name');
90         // TRANS: Used as title in check for group membership. %s is a user name.
91         $title      = sprintf(_("%s's groups"), $this->target->nickname);
92         $taguribase = TagURI::base();
93         $id         = "tag:$taguribase:Groups";
94         $link       = common_local_url(
95             'usergroups',
96             array('nickname' => $this->target->nickname)
97         );
98
99         $subtitle   = sprintf(
100             // TRANS: Used as subtitle in check for group membership. %1$s is the site name, %2$s is a user name.
101             _('%1$s groups %2$s is a member of.'),
102             $sitename,
103             $this->target->nickname
104         );
105
106         switch($this->format) {
107         case 'xml':
108             $this->showXmlGroups($this->groups);
109             break;
110         case 'rss':
111             $this->showRssGroups($this->groups, $title, $link, $subtitle);
112             break;
113         case 'atom':
114             $selfuri = common_local_url('ApiGroupList', array('id'=>$this->target->id, 'format'=>'atom'));
115             $this->showAtomGroups(
116                 $this->groups,
117                 $title,
118                 $id,
119                 $link,
120                 $subtitle,
121                 $selfuri
122             );
123             break;
124         case 'json':
125             $this->showJsonGroups($this->groups);
126             break;
127         default:
128             // TRANS: Client error displayed when coming across a non-supported API method.
129             $this->clientError(_('API method not found.'), 404);
130         }
131     }
132
133     /**
134      * Get groups
135      *
136      * @return array groups
137      */
138     function getGroups()
139     {
140         $groups = array();
141
142         $group = $this->target->getGroups(
143             ($this->page - 1) * $this->count,
144             $this->count,
145             $this->since_id,
146             $this->max_id
147         );
148
149         while ($group->fetch()) {
150             $groups[] = clone($group);
151         }
152
153         return $groups;
154     }
155
156     /**
157      * Is this action read only?
158      *
159      * @param array $args other arguments
160      *
161      * @return boolean true
162      */
163     function isReadOnly($args)
164     {
165         return true;
166     }
167
168     /**
169      * When was this feed last modified?
170      *
171      * @return string datestamp of the latest group the user has joined
172      */
173
174     function lastModified()
175     {
176         if (!empty($this->groups) && (count($this->groups) > 0)) {
177             return strtotime($this->groups[0]->created);
178         }
179
180         return null;
181     }
182
183     /**
184      * An entity tag for this list of groups
185      *
186      * Returns an Etag based on the action name, language, user ID and
187      * timestamps of the first and last group the user has joined
188      *
189      * @return string etag
190      */
191     function etag()
192     {
193         if (!empty($this->groups) && (count($this->groups) > 0)) {
194
195             $last = count($this->groups) - 1;
196
197             return '"' . implode(
198                 ':',
199                 array($this->arg('action'),
200                       common_user_cache_hash($this->auth_user),
201                       common_language(),
202                       $this->target->id,
203                       strtotime($this->groups[0]->created),
204                       strtotime($this->groups[$last]->created))
205             )
206             . '"';
207         }
208
209         return null;
210     }
211 }