]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apisearchatom.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / apisearchatom.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Action for showing Twitter-like Atom search results
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  Search
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2008-2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Action for outputting search results in Twitter compatible Atom
36  * format.
37  *
38  * TODO: abstract Atom stuff into a ruseable base class like
39  * RSS10Action.
40  *
41  * @category Search
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  *
47  * @see      ApiPrivateAuthAction
48  */
49 class ApiSearchAtomAction extends ApiPrivateAuthAction
50 {
51     var $cnt;
52     var $query;
53     var $lang;
54     var $rpp;
55     var $page;
56     var $since_id;
57     var $geocode;
58
59     /**
60      * Constructor
61      *
62      * Just wraps the Action constructor.
63      *
64      * @param string  $output URI to output to, default = stdout
65      * @param boolean $indent Whether to indent output, default true
66      *
67      * @see Action::__construct
68      */
69     function __construct($output='php://output', $indent=null)
70     {
71         parent::__construct($output, $indent);
72     }
73
74     /**
75      * Do we need to write to the database?
76      *
77      * @return boolean true
78      */
79     function isReadonly()
80     {
81         return true;
82     }
83
84     /**
85      * Read arguments and initialize members
86      *
87      * @param array $args Arguments from $_REQUEST
88      *
89      * @return boolean success
90      */
91     function prepare($args)
92     {
93         parent::prepare($args);
94
95         $this->query = $this->trimmed('q');
96         $this->lang  = $this->trimmed('lang');
97         $this->rpp   = $this->trimmed('rpp');
98
99         if (!$this->rpp) {
100             $this->rpp = 15;
101         }
102
103         if ($this->rpp > 100) {
104             $this->rpp = 100;
105         }
106
107         $this->page = $this->trimmed('page');
108
109         if (!$this->page) {
110             $this->page = 1;
111         }
112
113         // TODO: Suppport max_id -- we need to tweak the backend
114         // Search classes to support it.
115
116         $this->since_id = $this->trimmed('since_id');
117         $this->geocode  = $this->trimmed('geocode');
118
119         // TODO: Also, language and geocode
120
121         return true;
122     }
123
124     /**
125      * Handle a request
126      *
127      * @param array $args Arguments from $_REQUEST
128      *
129      * @return void
130      */
131     function handle($args)
132     {
133         parent::handle($args);
134         common_debug("In apisearchatom handle()");
135         $this->showAtom();
136     }
137
138     /**
139      * Get the notices to output as results. This also sets some class
140      * attrs so we can use them to calculate pagination, and output
141      * since_id and max_id.
142      *
143      * @return array an array of Notice objects sorted in reverse chron
144      */
145     function getNotices()
146     {
147         // TODO: Support search operators like from: and to:, boolean, etc.
148
149         $notices = array();
150         $notice = new Notice();
151
152         // lcase it for comparison
153         $q = strtolower($this->query);
154
155         $search_engine = $notice->getSearchEngine('notice');
156         $search_engine->set_sort_mode('chron');
157         $search_engine->limit(($this->page - 1) * $this->rpp,
158             $this->rpp + 1, true);
159         if (false === $search_engine->query($q)) {
160             $this->cnt = 0;
161         } else {
162             $this->cnt = $notice->find();
163         }
164
165         $cnt = 0;
166         $this->max_id = 0;
167
168         if ($this->cnt > 0) {
169             while ($notice->fetch()) {
170                 ++$cnt;
171
172                 if (!$this->max_id) {
173                     $this->max_id = $notice->id;
174                 }
175
176                 if ($this->since_id && $notice->id <= $this->since_id) {
177                     break;
178                 }
179
180                 if ($cnt > $this->rpp) {
181                     break;
182                 }
183
184                 $notices[] = clone($notice);
185             }
186         }
187
188         return $notices;
189     }
190
191     /**
192      * Output search results as an Atom feed
193      *
194      * @return void
195      */
196     function showAtom()
197     {
198         $notices = $this->getNotices();
199
200         $this->initAtom();
201         $this->showFeed();
202
203         foreach ($notices as $n) {
204             $profile = $n->getProfile();
205
206             // Don't show notices from deleted users
207
208             if (!empty($profile)) {
209                 $this->showEntry($n);
210             }
211         }
212
213         $this->endAtom();
214     }
215
216     /**
217      * Show feed specific Atom elements
218      *
219      * @return void
220      */
221     function showFeed()
222     {
223         // TODO: A9 OpenSearch stuff like search.twitter.com?
224
225         $server   = common_config('site', 'server');
226         $sitename = common_config('site', 'name');
227
228         // XXX: Use xmlns:statusnet instead?
229
230         $this->elementStart('feed',
231             array('xmlns' => 'http://www.w3.org/2005/Atom',
232
233                              // XXX: xmlns:twitter causes Atom validation to fail
234                              // It's used for the source attr on notices
235
236                              'xmlns:twitter' => 'http://api.twitter.com/',
237                              'xml:lang' => 'en-US')); // XXX Other locales ?
238
239         $taguribase = TagURI::base();
240         $this->element('id', null, "tag:$taguribase:search/$server");
241
242         $site_uri = common_path(false);
243
244         $search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
245
246         if ($this->rpp != 15) {
247             $search_uri .= '&rpp=' . $this->rpp;
248         }
249
250         // FIXME: this alternate link is not quite right because our
251         // web-based notice search doesn't support a rpp (responses per
252         // page) param yet
253
254         $this->element('link', array('type' => 'text/html',
255                                      'rel'  => 'alternate',
256                                      'href' => $site_uri . 'search/notice?q=' .
257                                         urlencode($this->query)));
258
259         // self link
260
261         $self_uri = $search_uri;
262         $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
263
264         $this->element('link', array('type' => 'application/atom+xml',
265                                      'rel'  => 'self',
266                                      'href' => $self_uri));
267
268         // @todo Needs i18n?
269         $this->element('title', null, "$this->query - $sitename Search");
270         $this->element('updated', null, common_date_iso8601('now'));
271
272         // XXX: The below "rel" links are not valid Atom, but it's what
273         // Twitter does...
274
275         // refresh link
276
277         $refresh_uri = $search_uri . "&since_id=" . $this->max_id;
278
279         $this->element('link', array('type' => 'application/atom+xml',
280                                      'rel'  => 'refresh',
281                                      'href' => $refresh_uri));
282
283         // pagination links
284
285         if ($this->cnt > $this->rpp) {
286
287             $next_uri = $search_uri . "&max_id=" . $this->max_id .
288                 '&page=' . ($this->page + 1);
289
290             $this->element('link', array('type' => 'application/atom+xml',
291                                          'rel'  => 'next',
292                                          'href' => $next_uri));
293         }
294
295         if ($this->page > 1) {
296
297             $previous_uri = $search_uri . "&max_id=" . $this->max_id .
298                 '&page=' . ($this->page - 1);
299
300             $this->element('link', array('type' => 'application/atom+xml',
301                                          'rel'  => 'previous',
302                                          'href' => $previous_uri));
303         }
304     }
305
306     /**
307      * Build an Atom entry similar to search.twitter.com's based on
308      * a given notice
309      *
310      * @param Notice $notice the notice to use
311      *
312      * @return void
313      */
314     function showEntry($notice)
315     {
316         $server  = common_config('site', 'server');
317         $profile = $notice->getProfile();
318         $nurl    = common_local_url('shownotice', array('notice' => $notice->id));
319
320         $this->elementStart('entry');
321
322         $taguribase = TagURI::base();
323
324         $this->element('id', null, "tag:$taguribase:$notice->id");
325         $this->element('published', null, common_date_w3dtf($notice->created));
326         $this->element('link', array('type' => 'text/html',
327                                      'rel'  => 'alternate',
328                                      'href' => $nurl));
329         $this->element('title', null, common_xml_safe_str(trim($notice->content)));
330         $this->element('content', array('type' => 'html'), $notice->rendered);
331         $this->element('updated', null, common_date_w3dtf($notice->created));
332         $this->element('link', array('type' => 'image/png',
333                                      // XXX: Twitter uses rel="image" (not valid)
334                                      'rel' => 'related',
335                                      'href' => $profile->avatarUrl()));
336
337         // @todo: Here is where we'd put in a link to an atom feed for threads
338
339         $source = null;
340
341         $ns = $notice->getSource();
342         if ($ns instanceof Notice_source) {
343             if (!empty($ns->name) && !empty($ns->url)) {
344                 $source = '<a href="'
345                    . htmlspecialchars($ns->url)
346                    . '" rel="nofollow">'
347                    . htmlspecialchars($ns->name)
348                    . '</a>';
349             } else {
350                 $source = $ns->code;
351             }
352         }
353
354         $this->element("twitter:source", null, $source);
355
356         $this->elementStart('author');
357
358         $name = $profile->nickname;
359
360         if ($profile->fullname) {
361             // @todo Needs proper i18n?
362             $name .= ' (' . $profile->fullname . ')';
363         }
364
365         $this->element('name', null, $name);
366         $this->element('uri', null, common_profile_uri($profile));
367         $this->elementEnd('author');
368
369         $this->elementEnd('entry');
370     }
371
372     /**
373      * Initialize the Atom output, send headers
374      *
375      * @return void
376      */
377     function initAtom()
378     {
379         header('Content-Type: application/atom+xml; charset=utf-8');
380         $this->startXml();
381     }
382
383     /**
384      * End the Atom feed
385      *
386      * @return void
387      */
388     function endAtom()
389     {
390         $this->elementEnd('feed');
391     }
392 }