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