]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplistall.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / apigrouplistall.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the newest groups
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 of the lastest 20 groups for the site
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 ApiGroupListAllAction extends ApiPrivateAuthAction
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     function prepare($args)
62     {
63         parent::prepare($args);
64
65         $this->user   = $this->getTargetUser(null);
66         $this->groups = $this->getGroups();
67
68         return true;
69     }
70
71     /**
72      * Handle the request
73      *
74      * Show the user's groups
75      *
76      * @param array $args $_REQUEST data (unused)
77      *
78      * @return void
79      */
80     function handle($args)
81     {
82         parent::handle($args);
83
84         $sitename   = common_config('site', 'name');
85         // TRANS: Message is used as a title when listing the lastest 20 groups. %s is a site name.
86         $title      = sprintf(_("%s groups"), $sitename);
87         $taguribase = TagURI::base();
88         $id         = "tag:$taguribase:Groups";
89         $link       = common_local_url('groups');
90         // TRANS: Message is used as a subtitle when listing the latest 20 groups. %s is a site name.
91         $subtitle   = sprintf(_("groups on %s"), $sitename);
92
93         switch($this->format) {
94         case 'xml':
95             $this->showXmlGroups($this->groups);
96             break;
97         case 'rss':
98             $this->showRssGroups($this->groups, $title, $link, $subtitle);
99             break;
100         case 'atom':
101             $selfuri = common_root_url() .
102                 'api/statusnet/groups/list_all.atom';
103             $this->showAtomGroups(
104                 $this->groups,
105                 $title,
106                 $id,
107                 $link,
108                 $subtitle,
109                 $selfuri
110             );
111             break;
112         case 'json':
113             $this->showJsonGroups($this->groups);
114             break;
115         default:
116             $this->clientError(
117                 // TRANS: Client error displayed when coming across a non-supported API method.
118                 _('API method not found.'),
119                 404,
120                 $this->format
121             );
122             break;
123         }
124     }
125
126     /**
127      * Get groups
128      *
129      * @return array groups
130      */
131     function getGroups()
132     {
133         $qry = 'SELECT user_group.* '.
134           'from user_group join local_group on user_group.id = local_group.group_id '.
135           'order by created desc ';
136         $offset = intval($this->page - 1) * intval($this->count);
137         $limit = intval($this->count);
138         if (common_config('db', 'type') == 'pgsql') {
139             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
140         } else {
141             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
142         }
143         $group = new User_group();
144
145         $group->query($qry);
146
147         $groups = array();
148         while ($group->fetch()) {
149             $groups[] = clone($group);
150         }
151
152         return $groups;
153     }
154
155     /**
156      * Is this action read only?
157      *
158      * @param array $args other arguments
159      *
160      * @return boolean true
161      */
162     function isReadOnly($args)
163     {
164         return true;
165     }
166
167     /**
168      * When was this feed last modified?
169      *
170      * @return string datestamp of the site's latest group
171      */
172     function lastModified()
173     {
174         if (!empty($this->groups) && (count($this->groups) > 0)) {
175             return strtotime($this->groups[0]->created);
176         }
177
178         return null;
179     }
180
181     /**
182      * An entity tag for this list of groups
183      *
184      * Returns an Etag based on the action name, language, and
185      * timestamps of the first and last group the user has joined
186      *
187      * @return string etag
188      */
189     function etag()
190     {
191         if (!empty($this->groups) && (count($this->groups) > 0)) {
192
193             $last = count($this->groups) - 1;
194
195             return '"' . implode(
196                 ':',
197                 array($this->arg('action'),
198                       common_user_cache_hash($this->auth_user),
199                       common_language(),
200                       strtotime($this->groups[0]->created),
201                       strtotime($this->groups[$last]->created))
202             )
203             . '"';
204         }
205
206         return null;
207     }
208 }