]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinelist.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / apitimelinelist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a list'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 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 require_once INSTALLDIR . '/lib/atomlistnoticefeed.php';
39
40 /**
41  * Returns the most recent notices (default 20) posted to the list specified by ID
42  *
43  * @category API
44  * @package  StatusNet
45  * @author   Craig Andrews <candrews@integralblue.com>
46  * @author   Evan Prodromou <evan@status.net>
47  * @author   Jeffery To <jeffery.to@gmail.com>
48  * @author   Zach Copley <zach@status.net>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://status.net/
51  */
52 class ApiTimelineListAction extends ApiPrivateAuthAction
53 {
54
55     var $list   = null;
56     var $notices = array();
57     var $next_cursor = 0;
58     var $prev_cursor = 0;
59     var $cursor = -1;
60
61     /**
62      * Take arguments for running
63      *
64      * @param array $args $_REQUEST args
65      *
66      * @return boolean success flag
67      *
68      */
69     protected function prepare(array $args=array())
70     {
71         parent::prepare($args);
72
73         $this->cursor = (int) $this->arg('cursor', -1);
74         $this->list = $this->getTargetList($this->arg('user'), $this->arg('id'));
75
76         return true;
77     }
78
79     /**
80      * Handle the request
81      *
82      * Just show the notices
83      *
84      * @return void
85      */
86     protected function handle()
87     {
88         parent::handle();
89
90         if (empty($this->list)) {
91             // TRANS: Client error displayed trying to perform an action related to a non-existing list.
92             $this->clientError(_('List not found.'), 404);
93         }
94
95         $this->getNotices();
96         $this->showTimeline();
97     }
98
99     /**
100      * Show the timeline of notices
101      *
102      * @return void
103      */
104     function showTimeline()
105     {
106         // We'll pull common formatting out of this for other formats
107         $atom = new AtomListNoticeFeed($this->list, $this->auth_user);
108
109         $self = $this->getSelfUri();
110
111         switch($this->format) {
112         case 'xml':
113             $this->initDocument('xml');
114             $this->elementStart('statuses_list',
115                     array('xmlns:statusnet' => 'http://status.net/schema/api/1/'));
116             $this->elementStart('statuses', array('type' => 'array'));
117
118             foreach ($this->notices as $n) {
119                 $twitter_status = $this->twitterStatusArray($n);
120                 $this->showTwitterXmlStatus($twitter_status);
121             }
122
123             $this->elementEnd('statuses');
124             $this->element('next_cursor', null, $this->next_cursor);
125             $this->element('previous_cursor', null, $this->prev_cursor);
126             $this->elementEnd('statuses_list');
127             $this->endDocument('xml');
128             break;
129         case 'rss':
130             $this->showRssTimeline(
131                 $this->notices,
132                 $atom->title,
133                 $this->list->getUri(),
134                 $atom->subtitle,
135                 null,
136                 $atom->logo,
137                 $self
138             );
139             break;
140         case 'atom':
141             header('Content-Type: application/atom+xml; charset=utf-8');
142
143             try {
144                 $atom->setId($self);
145                 $atom->setSelfLink($self);
146                 $atom->addEntryFromNotices($this->notices);
147                 $this->raw($atom->getString());
148             } catch (Atom10FeedException $e) {
149                 // TRANS: Server error displayed whe trying to get a timeline fails.
150                 // TRANS: %s is the error message.
151                 $this->serverError(sprintf(_('Could not generate feed for list - %s'), $e->getMessage()));
152             }
153
154             break;
155         case 'json':
156             $this->initDocument('json');
157
158             $statuses = array();
159             foreach ($this->notices as $n) {
160                 $twitter_status = $this->twitterStatusArray($n);
161                 array_push($statuses, $twitter_status);
162             }
163
164             $statuses_list = array('statuses' => $statuses,
165                                    'next_cursor' => $this->next_cusror,
166                                    'next_cursor_str' => strval($this->next_cusror),
167                                    'previous_cursor' => $this->prev_cusror,
168                                    'previous_cursor_str' => strval($this->prev_cusror)
169                                    );
170             $this->showJsonObjects($statuses_list);
171
172             $this->initDocument('json');
173             break;
174         default:
175             // TRANS: Client error displayed when coming across a non-supported API method.
176             $this->clientError(_('API method not found.'), 404);
177         }
178     }
179
180     /**
181      * Get notices
182      *
183      * @return array notices
184      */
185     function getNotices()
186     {
187         $fn = array($this->list, 'getNotices');
188         list($this->notices, $this->next_cursor, $this->prev_cursor) =
189                 Profile_list::getAtCursor($fn, array(), $this->cursor, 20);
190         if (!$this->notices) {
191             $this->notices = array();
192         }
193     }
194
195     /**
196      * Is this action read only?
197      *
198      * @param array $args other arguments
199      *
200      * @return boolean true
201      */
202     function isReadOnly($args)
203     {
204         return true;
205     }
206
207     /**
208      * When was this feed last modified?
209      *
210      * @return string datestamp of the latest notice in the stream
211      */
212     function lastModified()
213     {
214         if (!empty($this->notices) && (count($this->notices) > 0)) {
215             return strtotime($this->notices[0]->created);
216         }
217
218         return null;
219     }
220
221     /**
222      * An entity tag for this stream
223      *
224      * Returns an Etag based on the action name, language, list ID and
225      * timestamps of the first and last notice in the timeline
226      *
227      * @return string etag
228      */
229     function etag()
230     {
231         if (!empty($this->notices) && (count($this->notices) > 0)) {
232
233             $last = count($this->notices) - 1;
234
235             return '"' . implode(
236                 ':',
237                 array($this->arg('action'),
238                       common_language(),
239                       $this->list->id,
240                       strtotime($this->notices[0]->created),
241                       strtotime($this->notices[$last]->created))
242             )
243             . '"';
244         }
245
246         return null;
247     }
248 }