]> git.wh0rd.org - tt-rss.git/blob - api/index.php
api: support passing input parameters using JSON in HTTP POST data
[tt-rss.git] / api / index.php
1 <?php
2 error_reporting(E_ERROR | E_PARSE);
3
4 require_once "../config.php";
5
6 require_once "../db.php";
7 require_once "../db-prefs.php";
8 require_once "../functions.php";
9
10 define('API_STATUS_OK', 0);
11 define('API_STATUS_ERR', 1);
12
13 if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT) {
14 ob_start("ob_gzhandler");
15 }
16
17 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
18
19 $session_expire = SESSION_EXPIRE_TIME; //seconds
20 $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api";
21
22 session_name($session_name);
23
24 $input = file_get_contents("php://input");
25
26 // Override $_REQUEST with JSON-encoded data if available
27 if ($input) {
28 $input = json_decode($input, true);
29
30 if ($input) {
31 $_REQUEST = $input;
32 }
33 }
34
35 if ($_REQUEST["sid"]) {
36 session_id($_REQUEST["sid"]);
37 }
38
39 session_start();
40
41 if (!$link) {
42 if (DB_TYPE == "mysql") {
43 print mysql_error();
44 }
45 // PG seems to display its own errors just fine by default.
46 return;
47 }
48
49 init_connection($link);
50
51 $op = db_escape_string($_REQUEST["op"]);
52 $seq = (int) $_REQUEST["seq"];
53
54 // header("Content-Type: application/json");
55
56 function api_wrap_reply($status, $seq, $reply) {
57 print json_encode(array("seq" => $seq,
58 "status" => $status,
59 "content" => $reply));
60 }
61
62 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
63 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'NOT_LOGGED_IN'));
64 return;
65 }
66
67 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
68 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'API_DISABLED'));
69 return;
70 }
71
72 switch ($op) {
73
74 case "getVersion":
75 $rv = array("version" => VERSION);
76 print api_wrap_reply(API_STATUS_OK, $seq, $rv);
77 break;
78
79 case "login":
80 $login = db_escape_string($_REQUEST["user"]);
81 $password = db_escape_string($_REQUEST["password"]);
82 $password_base64 = db_escape_string(base64_decode($_REQUEST["password"]));
83
84 if (SINGLE_USER_MODE) $login = "admin";
85
86 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
87
88 if (db_num_rows($result) != 0) {
89 $uid = db_fetch_result($result, 0, "id");
90 } else {
91 $uid = 0;
92 }
93
94 if (!$uid) {
95 print api_wrap_reply(API_STATUS_ERR, $seq,
96 array("error" => "LOGIN_ERROR"));
97 return;
98 }
99
100 if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
101 if (authenticate_user($link, $login, $password)) { // try login with normal password
102 print api_wrap_reply(API_STATUS_OK, $seq,
103 array("session_id" => session_id()));
104 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
105 print api_wrap_reply(API_STATUS_OK, $seq,
106 array("session_id" => session_id()));
107 } else { // else we are not logged in
108 print api_wrap_reply(API_STATUS_ERR, $seq,
109 array("error" => "LOGIN_ERROR"));
110 }
111 } else {
112 print api_wrap_reply(API_STATUS_ERR, $seq,
113 array("error" => "API_DISABLED"));
114 }
115
116 break;
117
118 case "logout":
119 logout_user();
120 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
121 break;
122
123 case "isLoggedIn":
124 print api_wrap_reply(API_STATUS_OK, $seq,
125 array("status" => $_SESSION["uid"] != ''));
126 break;
127
128 case "getUnread":
129 $feed_id = db_escape_string($_REQUEST["feed_id"]);
130 $is_cat = db_escape_string($_REQUEST["is_cat"]);
131
132 if ($feed_id) {
133 print api_wrap_reply(API_STATUS_OK, $seq,
134 array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
135 } else {
136 print api_wrap_reply(API_STATUS_OK, $seq,
137 array("unread" => getGlobalUnread($link)));
138 }
139 break;
140
141 /* Method added for ttrss-reader for Android */
142 case "getCounters":
143
144 /* flct (flc is the default) FIXME: document */
145 $output_mode = db_escape_string($_REQUEST["output_mode"]);
146
147 print api_wrap_reply(API_STATUS_OK, $seq,
148 getAllCounters($link, $output_mode));
149 break;
150
151 case "getFeeds":
152 $cat_id = db_escape_string($_REQUEST["cat_id"]);
153 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
154 $limit = (int) db_escape_string($_REQUEST["limit"]);
155 $offset = (int) db_escape_string($_REQUEST["offset"]);
156
157 chdir(".."); // so feed_has_icon() would work properly for relative ICONS_DIR
158
159 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
160
161 print api_wrap_reply(API_STATUS_OK, $seq, $feeds);
162
163 break;
164
165 case "getCategories":
166 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
167
168 $result = db_query($link, "SELECT
169 id, title FROM ttrss_feed_categories
170 WHERE owner_uid = " .
171 $_SESSION["uid"]);
172
173 $cats = array();
174
175 while ($line = db_fetch_assoc($result)) {
176 $unread = getFeedUnread($link, $line["id"], true);
177
178 if ($unread || !$unread_only) {
179 array_push($cats, array("id" => $line["id"],
180 "title" => $line["title"],
181 "unread" => $unread));
182 }
183 }
184
185 print api_wrap_reply(API_STATUS_OK, $seq, $cats);
186 break;
187
188 case "getHeadlines":
189 $feed_id = db_escape_string($_REQUEST["feed_id"]);
190 $limit = (int)db_escape_string($_REQUEST["limit"]);
191 $offset = (int)db_escape_string($_REQUEST["skip"]);
192 $filter = db_escape_string($_REQUEST["filter"]);
193 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
194 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
195 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
196 /* all_articles, unread, adaptive, marked, updated */
197 $view_mode = db_escape_string($_REQUEST["view_mode"]);
198 $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]);
199
200 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
201 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
202 $include_attachments);
203
204 print api_wrap_reply(API_STATUS_OK, $seq, $headlines);
205
206 break;
207
208 case "updateArticle":
209 $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
210 $mode = (int) db_escape_string($_REQUEST["mode"]);
211 $field_raw = (int)db_escape_string($_REQUEST["field"]);
212
213 $field = "";
214 $set_to = "";
215
216 switch ($field_raw) {
217 case 0:
218 $field = "marked";
219 break;
220 case 1:
221 $field = "published";
222 break;
223 case 2:
224 $field = "unread";
225 break;
226 };
227
228 switch ($mode) {
229 case 1:
230 $set_to = "true";
231 break;
232 case 0:
233 $set_to = "false";
234 break;
235 case 2:
236 $set_to = "NOT $field";
237 break;
238 }
239
240 if ($field && $set_to && count($article_ids) > 0) {
241
242 $article_ids = join(", ", $article_ids);
243
244 if ($field == "unread") {
245 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
246 last_read = NOW()
247 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
248 } else {
249 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
250 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
251 }
252
253 $num_updated = db_affected_rows($link, $result);
254
255 if ($num_updated > 0 && $field == "unread") {
256 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
257 WHERE ref_id IN ($article_ids)");
258
259 while ($line = db_fetch_assoc($result)) {
260 ccache_update($link, $line["feed_id"], $_SESSION["uid"]);
261 }
262 }
263
264 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK",
265 "updated" => $num_updated));
266
267 } else {
268 print api_wrap_reply(API_STATUS_ERR, $seq,
269 array("error" => 'INCORRECT_USAGE'));
270 }
271
272 break;
273
274 case "getArticle":
275
276 $article_id = db_escape_string($_REQUEST["article_id"]);
277
278 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
279 marked,unread,published,
280 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
281 author
282 FROM ttrss_entries,ttrss_user_entries
283 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
284 $_SESSION["uid"] ;
285
286 $result = db_query($link, $query);
287
288 $articles = array();
289
290 if (db_num_rows($result) != 0) {
291
292 while ($line = db_fetch_assoc($result)) {
293
294 $attachments = get_article_enclosures($link, $line['id']);
295
296 $article = array(
297 "id" => $line["id"],
298 "title" => $line["title"],
299 "link" => $line["link"],
300 "labels" => get_article_labels($link, $line['id']),
301 "unread" => sql_bool_to_bool($line["unread"]),
302 "marked" => sql_bool_to_bool($line["marked"]),
303 "published" => sql_bool_to_bool($line["published"]),
304 "comments" => $line["comments"],
305 "author" => $line["author"],
306 "updated" => strtotime($line["updated"]),
307 "content" => $line["content"],
308 "feed_id" => $line["feed_id"],
309 "attachments" => $attachments
310 );
311
312 array_push($articles, $article);
313
314 }
315 }
316
317 print api_wrap_reply(API_STATUS_OK, $seq, $articles);
318
319 break;
320
321 case "getConfig":
322 $config = array(
323 "icons_dir" => ICONS_DIR,
324 "icons_url" => ICONS_URL);
325
326 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
327
328 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
329 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
330
331 $num_feeds = db_fetch_result($result, 0, "cf");
332
333 $config["num_feeds"] = (int)$num_feeds;
334
335 print api_wrap_reply(API_STATUS_OK, $seq, $config);
336
337 break;
338
339 case "updateFeed":
340 $feed_id = db_escape_string($_REQUEST["feed_id"]);
341
342 update_rss_feed($link, $feed_id, true);
343
344 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
345
346 break;
347
348 case "catchupFeed":
349 $feed_id = db_escape_string($_REQUEST["feed_id"]);
350 $is_cat = db_escape_string($_REQUEST["is_cat"]);
351
352 catchup_feed($link, $feed_id, $is_cat);
353
354 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
355
356 break;
357
358 case "getPref":
359 $pref_name = db_escape_string($_REQUEST["pref_name"]);
360
361 print api_wrap_reply(API_STATUS_OK, $seq,
362 array("value" => get_pref($link, $pref_name)));
363 break;
364
365 default:
366 print api_wrap_reply(API_STATUS_ERR, $seq,
367 array("error" => 'UNKNOWN_METHOD'));
368 break;
369
370 }
371
372 db_close($link);
373
374 ?>