]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apisearchjson.php
Qvitter API changes (thanks hannes2peer)
[quix0rs-gnu-social.git] / actions / apisearchjson.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Action for showing Twitter-like JSON 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   GNUSocial
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2008-2010 StatusNet, Inc.
26  * @copyright 2013 Free Software Foundation, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://www.gnu.org/software/social/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Action handler for Twitter-compatible API search
35  *
36  * @category Search
37  * @package  StatusNet
38  * @author   Zach Copley <zach@status.net>
39  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
40  * @link     http://status.net/
41  * @see      ApiAction
42  */
43 class ApiSearchJSONAction extends ApiPrivateAuthAction
44 {
45     var $query;
46     var $lang;
47     var $rpp;
48     var $page;
49     var $since_id;
50     var $limit;
51     var $geocode;
52
53     /**
54      * Initialization.
55      *
56      * @param array $args Web and URL arguments
57      *
58      * @return boolean true if nothing goes wrong
59      */
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         $this->query = $this->trimmed('q');
65         $this->lang  = $this->trimmed('lang');
66         $this->rpp   = $this->trimmed('rpp');
67
68         if (!$this->rpp) {
69             $this->rpp = 15;
70         }
71
72         if ($this->rpp > 100) {
73             $this->rpp = 100;
74         }
75
76         $this->page = $this->trimmed('page');
77
78         if (!$this->page) {
79             $this->page = 1;
80         }
81
82         // TODO: Suppport max_id -- we need to tweak the backend
83         // Search classes to support it.
84
85         $this->since_id = $this->trimmed('since_id');
86         $this->geocode  = $this->trimmed('geocode');
87
88         return true;
89     }
90
91     /**
92      * Handle a request
93      *
94      * @param array $args Arguments from $_REQUEST
95      *
96      * @return void
97      */
98     function handle($args)
99     {
100         parent::handle($args);
101         $this->showResults();
102     }
103
104     /**
105      * Show search results
106      *
107      * @return void
108      */
109     function showResults()
110     {
111         $q = strtolower($this->query);
112
113         // TODO: Support search operators like from: and to:, boolean, etc.
114
115         if (preg_match('/^#([\pL\pN_\-\.]{1,64})$/ue', $q)) {
116             $stream = new TagNoticeStream(substr($q, 1), $this->scoped);
117         } else if ($this->isAnURL($q)) {
118             $canon = File_redirection::_canonUrl($q);
119             $file = File::getKV('url', $canon);
120             if (!empty($file)) {
121                 $stream = new FileNoticeStream($file, $this->scoped);
122             }
123         } else {
124             $stream = new SearchNoticeStream($q, $this->scoped);
125         }
126
127         if (empty($stream)) {
128             // XXX: This is hackish, but need some simple way to say "There's no results"
129             $notice = new ArrayWrapper(array());
130         } else {
131             $notice = $stream->getNotices(($this->page - 1) * $this->rpp, $this->rpp + 1);
132         }
133
134         // TODO: max_id, lang, geocode
135
136         $results = new JSONSearchResultsList($notice, $q, $this->rpp, $this->page, $this->since_id);
137
138         $this->initDocument('json');
139         $results->show();
140         $this->endDocument('json');
141     }
142
143     function isAnURL($q) {
144         $regex = '#^'.
145             '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'.
146             '('.
147             '(?:'.
148             '(?:'. //Known protocols
149             '(?:'.
150             '(?:(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://)'.
151             '|'.
152             '(?:(?:mailto|aim|tel|xmpp):)'.
153             ')'.
154             '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@
155             '(?:'.
156             '(?:'.
157             '\[[\pN\pL\-\_\:\.]+(?<![\.\:])\]'. //[dns]
158             ')|(?:'.
159             '[\pN\pL\-\_\:\.]+(?<![\.\:])'. //dns
160             ')'.
161             ')'.
162             ')'.
163             '|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'. //IPv4
164             '|(?:'. //IPv6
165             '\[?(?:(?:(?:[0-9A-Fa-f]{1,4}:){7}(?:(?:[0-9A-Fa-f]{1,4})|:))|(?:(?:[0-9A-Fa-f]{1,4}:){6}(?::|(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(?::[0-9A-Fa-f]{1,4})))|(?:(?:[0-9A-Fa-f]{1,4}:){5}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){4}(?::[0-9A-Fa-f]{1,4}){0,1}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){3}(?::[0-9A-Fa-f]{1,4}){0,2}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){2}(?::[0-9A-Fa-f]{1,4}){0,3}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:)(?::[0-9A-Fa-f]{1,4}){0,4}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?::(?::[0-9A-Fa-f]{1,4}){0,5}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))\]?(?<!:)'.
166             ')|(?:'. //DNS
167             '(?:[\pN\pL\-\_\+\%\~]+(?:\:[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@
168             '[\pN\pL\-\_]+(?:\.[\pN\pL\-\_]+)*\.'.
169             //tld list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt, also added local, loc, and onion
170             '(?:AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|ASIA|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|BZ|CA|CAT|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EDU|EE|EG|ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GOV|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|ID|IE|IL|IM|IN|INFO|INT|IO|IQ|IR|IS|IT|JE|JM|JO|JOBS|JP|KE|KG|KH|KI|KM|KN|KP|KR|KW|KY|KZ|LA|LB|LC|LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|ME|MG|MH|MIL|MK|ML|MM|MN|MO|MOBI|MP|MQ|MR|MS|MT|MU|MUSEUM|MV|MW|MX|MY|MZ|NA|NAME|NC|NE|NET|NF|NG|NI|NL|NO|NP|NR|NU|NZ|OM|ORG|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PRO|PS|PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|ST|SU|SV|SY|SZ|TC|TD|TEL|TF|TG|TH|TJ|TK|TL|TM|TN|TO|TP|TR|TRAVEL|TT|TV|TW|TZ|UA|UG|UK|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|XN--0ZWM56D|测试|XN--11B5BS3A9AJ6G|परीक्षा|XN--80AKHBYKNJ4F|испытание|XN--9T4B11YI5A|테스트|XN--DEBA0AD|טעסט|XN--G6W251D|測試|XN--HGBK6AJ7F53BBA|آزمایشی|XN--HLCJ6AYA9ESC7A|பரிட்சை|XN--JXALPDLP|δοκιμή|XN--KGBECHTV|إختبار|XN--ZCKZAH|テスト|YE|YT|YU|ZA|ZM|ZW|local|loc|onion)'.
171             ')(?![\pN\pL\-\_])'.
172             ')'.
173             '(?:'.
174             '(?:\:\d+)?'. //:port
175             '(?:/[\pN\pL$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'@]*)?'. // /path
176             '(?:\?[\pN\pL\$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'@\/]*)?'. // ?query string
177             '(?:\#[\pN\pL$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'\@/\?\#]*)?'. // #fragment
178             ')(?<![\?\.\,\#\,])'.
179             ')'.
180             '$#ixu';
181         return preg_match($regex, $q);
182     }
183
184     /**
185      * Do we need to write to the database?
186      *
187      * @return boolean true
188      */
189     function isReadOnly($args)
190     {
191         return true;
192     }
193 }