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