]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinegroup.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / apitimelinegroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a group's notices
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-2010 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 the most recent notices (default 20) posted to the group specified by ID
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 ApiTimelineGroupAction extends ApiPrivateAuthAction
51 {
52     var $group   = null;
53     var $notices = null;
54
55     /**
56      * Take arguments for running
57      *
58      * @param array $args $_REQUEST args
59      *
60      * @return boolean success flag
61      *
62      */
63     protected function prepare(array $args=array())
64     {
65         parent::prepare($args);
66
67         $this->group   = $this->getTargetGroup($this->arg('id'));
68
69         return true;
70     }
71
72     /**
73      * Handle the request
74      *
75      * Just show the notices
76      *
77      * @return void
78      */
79     protected function handle()
80     {
81         parent::handle();
82
83         if (empty($this->group)) {
84             // TRANS: Client error displayed requesting most recent notices to a group for a non-existing group.
85             $this->clientError(_('Group not found.'), 404);
86         }
87
88         $this->notices = $this->getNotices();
89         $this->showTimeline();
90     }
91
92     /**
93      * Show the timeline of notices
94      *
95      * @return void
96      */
97     function showTimeline()
98     {
99         // We'll pull common formatting out of this for other formats
100         $atom = new AtomGroupNoticeFeed($this->group, $this->auth_user);
101
102         $self = $this->getSelfUri();
103
104         $link = common_local_url('showgroup',
105                                  array('nickname' => $this->group->nickname));
106
107         switch($this->format) {
108         case 'xml':
109             $this->showXmlTimeline($this->notices);
110             break;
111         case 'rss':
112             $this->showRssTimeline(
113                 $this->notices,
114                 $atom->title,
115                 $this->group->homeUrl(),
116                 $atom->subtitle,
117                 null,
118                 $atom->logo,
119                 $self
120             );
121             break;
122         case 'atom':
123             header('Content-Type: application/atom+xml; charset=utf-8');
124                 $atom->addEntryFromNotices($this->notices);
125                 $this->raw($atom->getString());
126             break;
127         case 'json':
128             $this->showJsonTimeline($this->notices);
129             break;
130         case 'as':
131             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
132             $doc = new ActivityStreamJSONDocument($this->auth_user);
133             $doc->setTitle($atom->title);
134             $doc->addLink($link, 'alternate', 'text/html');
135             $doc->addItemsFromNotices($this->notices);
136             $this->raw($doc->asString());
137             break;
138         default:
139             // TRANS: Client error displayed when trying to handle an unknown API method.
140             $this->clientError(_('API method not found.'), 404);
141         }
142     }
143
144     /**
145      * Get notices
146      *
147      * @return array notices
148      */
149     function getNotices()
150     {
151         $notices = array();
152
153         $notice = $this->group->getNotices(
154             ($this->page-1) * $this->count,
155             $this->count,
156             $this->since_id,
157             $this->max_id
158         );
159
160         while ($notice->fetch()) {
161             $notices[] = clone($notice);
162         }
163
164         return $notices;
165     }
166
167     /**
168      * Is this action read only?
169      *
170      * @param array $args other arguments
171      *
172      * @return boolean true
173      */
174     function isReadOnly($args)
175     {
176         return true;
177     }
178
179     /**
180      * When was this feed last modified?
181      *
182      * @return string datestamp of the latest notice in the stream
183      */
184     function lastModified()
185     {
186         if (!empty($this->notices) && (count($this->notices) > 0)) {
187             return strtotime($this->notices[0]->created);
188         }
189
190         return null;
191     }
192
193     /**
194      * An entity tag for this stream
195      *
196      * Returns an Etag based on the action name, language, group ID and
197      * timestamps of the first and last notice in the timeline
198      *
199      * @return string etag
200      */
201     function etag()
202     {
203         if (!empty($this->notices) && (count($this->notices) > 0)) {
204
205             $last = count($this->notices) - 1;
206
207             return '"' . implode(
208                 ':',
209                 array($this->arg('action'),
210                       common_user_cache_hash($this->auth_user),
211                       common_language(),
212                       $this->group->id,
213                       strtotime($this->notices[0]->created),
214                       strtotime($this->notices[$last]->created))
215             )
216             . '"';
217         }
218
219         return null;
220     }
221 }