]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinementions.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / apitimelinementions.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show notices mentioning a user (@nickname)
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    mac65 <mac65@mac65.com>
28  * @author    Mike Cochrane <mikec@mikenz.geek.nz>
29  * @author    Robin Millette <robin@millette.info>
30  * @author    Zach Copley <zach@status.net>
31  * @copyright 2009 StatusNet, Inc.
32  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
34  * @link      http://status.net/
35  */
36
37 if (!defined('STATUSNET')) {
38     exit(1);
39 }
40
41 /**
42  * Returns the most recent (default 20) mentions (status containing @nickname)
43  *
44  * @category API
45  * @package  StatusNet
46  * @author   Craig Andrews <candrews@integralblue.com>
47  * @author   Evan Prodromou <evan@status.net>
48  * @author   Jeffery To <jeffery.to@gmail.com>
49  * @author   mac65 <mac65@mac65.com>
50  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
51  * @author   Robin Millette <robin@millette.info>
52  * @author   Zach Copley <zach@status.net>
53  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
54  * @link     http://status.net/
55  */
56 class ApiTimelineMentionsAction extends ApiBareAuthAction
57 {
58     var $notices = null;
59
60     /**
61      * Take arguments for running
62      *
63      * @param array $args $_REQUEST args
64      *
65      * @return boolean success flag
66      */
67     protected function prepare(array $args=array())
68     {
69         parent::prepare($args);
70
71         $this->target = $this->getTargetProfile($this->arg('id'));
72
73         if (!($this->target instanceof Profile)) {
74             // TRANS: Client error displayed when requesting most recent mentions for a non-existing user.
75             $this->clientError(_('No such user.'), 404);
76         }
77
78         $this->notices = $this->getNotices();
79
80         return true;
81     }
82
83     /**
84      * Handle the request
85      *
86      * Just show the notices
87      *
88      * @return void
89      */
90     protected function handle()
91     {
92         parent::handle();
93         $this->showTimeline();
94     }
95
96     /**
97      * Show the timeline of notices
98      *
99      * @return void
100      */
101     function showTimeline()
102     {
103         $sitename   = common_config('site', 'name');
104         $title      = sprintf(
105             // TRANS: Title for timeline of most recent mentions of a user.
106             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
107             _('%1$s / Updates mentioning %2$s'),
108             $sitename, $this->target->nickname
109         );
110         $taguribase = TagURI::base();
111         $id         = "tag:$taguribase:Mentions:" . $this->target->id;
112
113         $logo = $this->target->avatarUrl(AVATAR_PROFILE_SIZE);
114         $link = common_local_url('replies',
115                     array('nickname' => $this->target->nickname));
116         $self = $this->getSelfUri();
117
118         $subtitle   = sprintf(
119             // TRANS: Subtitle for timeline of most recent mentions of a user.
120             // TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname,
121             // TRANS: %3$s is a user's full name.
122             _('%1$s updates that reply to updates from %3$s / %2$s.'),
123             $sitename, $this->target->nickname, $this->target->getBestName()
124         );
125
126         switch($this->format) {
127         case 'xml':
128             $this->showXmlTimeline($this->notices);
129             break;
130         case 'rss':
131             $this->showRssTimeline(
132                 $this->notices,
133                 $title,
134                 $link,
135                 $subtitle,
136                 null,
137                 $logo,
138                 $self
139             );
140             break;
141         case 'atom':
142             header('Content-Type: application/atom+xml; charset=utf-8');
143
144             $atom = new AtomNoticeFeed($this->auth_user);
145
146             $atom->setId($id);
147             $atom->setTitle($title);
148             $atom->setSubtitle($subtitle);
149             $atom->setLogo($logo);
150             $atom->setUpdated('now');
151
152             $atom->addLink($link);
153             $atom->setSelfLink($self);
154
155             $atom->addEntryFromNotices($this->notices);
156             $this->raw($atom->getString());
157
158             break;
159         case 'json':
160             $this->showJsonTimeline($this->notices);
161             break;
162         case 'as':
163             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
164             $doc = new ActivityStreamJSONDocument($this->auth_user);
165             $doc->setTitle($title);
166             $doc->addLink($link, 'alternate', 'text/html');
167             $doc->addItemsFromNotices($this->notices);
168             $this->raw($doc->asString());
169             break;
170         default:
171             // TRANS: Client error displayed when coming across a non-supported API method.
172             $this->clientError(_('API method not found.'), $code = 404);
173             break;
174         }
175     }
176
177     /**
178      * Get notices
179      *
180      * @return array notices
181      */
182     function getNotices()
183     {
184         $notices = array();
185
186         $stream = new ReplyNoticeStream($this->target->id, $this->scoped);
187
188         $notice = $stream->getNotices(($this->page - 1) * $this->count,
189                                       $this->count,
190                                       $this->since_id,
191                                       $this->max_id);
192
193         while ($notice->fetch()) {
194             $notices[] = clone($notice);
195         }
196
197         return $notices;
198     }
199
200     /**
201      * Is this action read only?
202      *
203      * @param array $args other arguments
204      *
205      * @return boolean true
206      */
207     function isReadOnly($args)
208     {
209         return true;
210     }
211
212     /**
213      * When was this feed last modified?
214      *
215      * @return string datestamp of the latest notice in the stream
216      */
217     function lastModified()
218     {
219         if (!empty($this->notices) && (count($this->notices) > 0)) {
220             return strtotime($this->notices[0]->created);
221         }
222
223         return null;
224     }
225
226     /**
227      * An entity tag for this stream
228      *
229      * Returns an Etag based on the action name, language, user ID, and
230      * timestamps of the first and last notice in the timeline
231      *
232      * @return string etag
233      */
234     function etag()
235     {
236         if (!empty($this->notices) && (count($this->notices) > 0)) {
237
238             $last = count($this->notices) - 1;
239
240             return '"' . implode(
241                 ':',
242                 array($this->arg('action'),
243                       common_user_cache_hash($this->auth_user),
244                       common_language(),
245                       $this->target->id,
246                       strtotime($this->notices[0]->created),
247                       strtotime($this->notices[$last]->created))
248             )
249             . '"';
250         }
251
252         return null;
253     }
254 }