]> git.wh0rd.org - tt-rss.git/blob - api/index.php
api: use is_cat in getArticles and catchupFeed for consistency with other methods
[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 if ($_REQUEST["sid"]) {
25 session_id($_REQUEST["sid"]);
26 }
27
28 session_start();
29
30 if (!$link) {
31 if (DB_TYPE == "mysql") {
32 print mysql_error();
33 }
34 // PG seems to display its own errors just fine by default.
35 return;
36 }
37
38 init_connection($link);
39
40 $op = db_escape_string($_REQUEST["op"]);
41 $seq = (int) $_REQUEST["seq"];
42
43 // header("Content-Type: application/json");
44
45 function api_wrap_reply($status, $seq, $reply) {
46 print json_encode(array("seq" => $seq,
47 "status" => $status,
48 "content" => $reply));
49 }
50
51 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
52 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'NOT_LOGGED_IN'));
53 return;
54 }
55
56 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
57 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'API_DISABLED'));
58 return;
59 }
60
61 switch ($op) {
62
63 case "getVersion":
64 $rv = array("version" => VERSION);
65 print api_wrap_reply(API_STATUS_OK, $seq, $rv);
66 break;
67
68 case "login":
69 $login = db_escape_string($_REQUEST["user"]);
70 $password = db_escape_string($_REQUEST["password"]);
71 $password_base64 = db_escape_string(base64_decode($_REQUEST["password"]));
72
73 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
74
75 if (db_num_rows($result) != 0) {
76 $uid = db_fetch_result($result, 0, "id");
77 } else {
78 $uid = 0;
79 }
80
81 if ($uid && get_pref($link, "ENABLE_API_ACCESS", $uid)) {
82 if (authenticate_user($link, $login, $password)) { // try login with normal password
83 print api_wrap_reply(API_STATUS_OK, $seq,
84 array("session_id" => session_id()));
85 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
86 print api_wrap_reply(API_STATUS_OK, $seq,
87 array("session_id" => session_id()));
88 } else { // else we are not logged in
89 print api_wrap_reply(API_STATUS_ERR, $seq,
90 array("error" => "LOGIN_ERROR"));
91 }
92 } else {
93 print api_wrap_reply(API_STATUS_ERR, $seq,
94 array("error" => "API_DISABLED"));
95 }
96
97 break;
98
99 case "logout":
100 logout_user();
101 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
102 break;
103
104 case "isLoggedIn":
105 print api_wrap_reply(API_STATUS_OK, $seq,
106 array("status" => $_SESSION["uid"] != ''));
107 break;
108
109 case "getUnread":
110 $feed_id = db_escape_string($_REQUEST["feed_id"]);
111 $is_cat = db_escape_string($_REQUEST["is_cat"]);
112
113 if ($feed_id) {
114 print api_wrap_reply(API_STATUS_OK, $seq,
115 array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
116 } else {
117 print api_wrap_reply(API_STATUS_OK, $seq,
118 array("unread" => getGlobalUnread($link)));
119 }
120 break;
121
122 /* Method added for ttrss-reader for Android */
123 case "getCounters":
124
125 /* flct (flc is the default) FIXME: document */
126 $output_mode = db_escape_string($_REQUEST["output_mode"]);
127
128 print api_wrap_reply(API_STATUS_OK, $seq,
129 getAllCounters($link, $output_mode));
130 break;
131
132 case "getFeeds":
133 $cat_id = db_escape_string($_REQUEST["cat_id"]);
134 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
135 $limit = (int) db_escape_string($_REQUEST["limit"]);
136 $offset = (int) db_escape_string($_REQUEST["offset"]);
137
138 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
139
140 print api_wrap_reply(API_STATUS_OK, $seq, $feeds);
141
142 break;
143
144 case "getCategories":
145 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
146
147 $result = db_query($link, "SELECT
148 id, title FROM ttrss_feed_categories
149 WHERE owner_uid = " .
150 $_SESSION["uid"]);
151
152 $cats = array();
153
154 while ($line = db_fetch_assoc($result)) {
155 $unread = getFeedUnread($link, $line["id"], true);
156
157 if ($unread || !$unread_only) {
158 array_push($cats, array("id" => $line["id"],
159 "title" => $line["title"],
160 "unread" => $unread));
161 }
162 }
163
164 print api_wrap_reply(API_STATUS_OK, $seq, $cats);
165 break;
166
167 case "getHeadlines":
168 $feed_id = db_escape_string($_REQUEST["feed_id"]);
169 $limit = (int)db_escape_string($_REQUEST["limit"]);
170 $offset = (int)db_escape_string($_REQUEST["skip"]);
171 $filter = db_escape_string($_REQUEST["filter"]);
172 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
173 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
174 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
175 /* all_articles, unread, adaptive, marked, updated */
176 $view_mode = db_escape_string($_REQUEST["view_mode"]);
177
178 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
179 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false);
180
181 print api_wrap_reply(API_STATUS_OK, $seq, $headlines);
182
183 break;
184
185 case "updateArticle":
186 $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
187 $mode = (int) db_escape_string($_REQUEST["mode"]);
188 $field_raw = (int)db_escape_string($_REQUEST["field"]);
189
190 $field = "";
191 $set_to = "";
192
193 switch ($field_raw) {
194 case 0:
195 $field = "marked";
196 break;
197 case 1:
198 $field = "published";
199 break;
200 case 2:
201 $field = "unread";
202 break;
203 };
204
205 switch ($mode) {
206 case 1:
207 $set_to = "true";
208 break;
209 case 0:
210 $set_to = "false";
211 break;
212 case 2:
213 $set_to = "NOT $field";
214 break;
215 }
216
217 if ($field && $set_to && count($article_ids) > 0) {
218
219 $article_ids = join(", ", $article_ids);
220
221 if ($field == "unread") {
222 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
223 last_read = NOW()
224 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
225 } else {
226 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
227 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
228 }
229
230 $num_updated = db_affected_rows($link, $result);
231
232 if ($num_updated > 0 && $field == "unread") {
233 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
234 WHERE ref_id IN ($article_ids)");
235
236 while ($line = db_fetch_assoc($result)) {
237 ccache_update($link, $line["feed_id"], $_SESSION["uid"]);
238 }
239 }
240
241 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK",
242 "updated" => $num_updated));
243
244 } else {
245 print api_wrap_reply(API_STATUS_ERR, $seq,
246 array("error" => 'INCORRECT_USAGE'));
247 }
248
249 break;
250
251 case "getArticle":
252
253 $article_id = db_escape_string($_REQUEST["article_id"]);
254
255 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
256 marked,unread,published,
257 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
258 author
259 FROM ttrss_entries,ttrss_user_entries
260 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
261 $_SESSION["uid"] ;
262
263 $result = db_query($link, $query);
264
265 $articles = array();
266
267 if (db_num_rows($result) != 0) {
268
269 while ($line = db_fetch_assoc($result)) {
270
271 $attachments = get_article_enclosures($link, $line['id']);
272
273 $article = array(
274 "id" => $line["id"],
275 "title" => $line["title"],
276 "link" => $line["link"],
277 "labels" => get_article_labels($link, $line['id']),
278 "unread" => sql_bool_to_bool($line["unread"]),
279 "marked" => sql_bool_to_bool($line["marked"]),
280 "published" => sql_bool_to_bool($line["published"]),
281 "comments" => $line["comments"],
282 "author" => $line["author"],
283 "updated" => strtotime($line["updated"]),
284 "content" => $line["content"],
285 "feed_id" => $line["feed_id"],
286 "attachments" => $attachments
287 );
288
289 array_push($articles, $article);
290
291 }
292 }
293
294 print api_wrap_reply(API_STATUS_OK, $seq, $articles);
295
296 break;
297
298 case "getConfig":
299 $config = array(
300 "icons_dir" => ICONS_DIR,
301 "icons_url" => ICONS_URL);
302
303 if (ENABLE_UPDATE_DAEMON) {
304 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
305 }
306
307 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
308 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
309
310 $num_feeds = db_fetch_result($result, 0, "cf");
311
312 $config["num_feeds"] = (int)$num_feeds;
313
314 print api_wrap_reply(API_STATUS_OK, $seq, $config);
315
316 break;
317
318 case "updateFeed":
319 $feed_id = db_escape_string($_REQUEST["feed_id"]);
320
321 update_rss_feed($link, $feed_id, true);
322
323 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
324
325 break;
326
327 case "catchupFeed":
328 $feed_id = db_escape_string($_REQUEST["feed_id"]);
329 $is_cat = db_escape_string($_REQUEST["is_cat"]);
330
331 catchup_feed($link, $feed_id, $is_cat);
332
333 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
334
335 break;
336
337 case "getPref":
338 $pref_name = db_escape_string($_REQUEST["pref_name"]);
339
340 print api_wrap_reply(API_STATUS_OK, $seq,
341 array("value" => get_pref($link, $pref_name)));
342 break;
343
344 /* Method added for ttrss-reader for Android */
345 case "getArticles":
346 $isCategory = (int)db_escape_string($_REQUEST["is_cat"]);
347 $id = (int)db_escape_string($_REQUEST["id"]);
348 $displayUnread = (int)db_escape_string($_REQUEST["unread"]);
349 $limit = (int)db_escape_string($_REQUEST["limit"]);
350 $feeds = array();
351
352 if ($isCategory > 0) {
353 // Get Feeds of the category
354
355 if ($id == 0) {
356 $category_part = "cat_id is NULL";
357 } else {
358 $category_part = "cat_id = '$id'";
359 }
360
361 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE ".
362 $category_part." AND owner_uid = '".$_SESSION["uid"]."'");
363
364 while ($line = db_fetch_assoc($result)) {
365 array_push($feeds, $line["id"]);
366 }
367
368 // Virtual feeds
369 $match_part = "";
370 if ($id == -1) {
371 $match_part = "marked = true";
372 array_push($feeds, -1);
373 } else if ($id == -2) {
374 $match_part = "published = true";
375 array_push($feeds, -2);
376 } else if ($id == -3) {
377 $match_part = "unread = true";
378 array_push($feeds, -3);
379
380 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
381
382 if (DB_TYPE == "pgsql") {
383 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
384 } else {
385 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
386 }
387 } else if ($id == -4) {
388 $match_part = "true";
389 array_push($feeds, -4);
390 }
391 } else {
392 // Only add one feed
393 array_push($feeds, $id);
394 }
395
396 $ret = array();
397
398 if (DB_TYPE == "mysql") {
399 $limit_part = " LIMIT 0,".$limit;
400 } else if (DB_TYPE == "pgsql") {
401 $limit_part = " LIMIT ".$limit;
402 } else {
403 $limit_part = "";
404 }
405
406 // Fetch articles for the feeds
407 foreach ($feeds as $feed) {
408
409 if ($match_part) {
410 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
411 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
412
413 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
414 marked,unread,published,".SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
415 FROM $from_qpart WHERE
416 ttrss_user_entries.ref_id = ttrss_entries.id AND
417 $feeds_qpart ($match_part) AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]." ORDER BY updated DESC".$limit_part;
418
419 $result = db_query($link, $query);
420 } else {
421 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
422 marked,unread,published,".SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
423 FROM ttrss_entries,ttrss_user_entries
424 WHERE feed_id = '".$feed."' AND ref_id = id AND owner_uid = ".
425 $_SESSION["uid"]." AND unread >= '".$displayUnread."' ORDER BY updated DESC".$limit_part;
426
427 $result = db_query($link, $query);
428 }
429
430 $articles = array();
431 $i=0;
432 while ($i < mysql_numrows($result)) {
433
434 $article_id = db_fetch_result($result, $i, "id");
435
436 $attachments = get_article_enclosures($link, $article_id);
437
438 $article = array(
439 "id" => db_fetch_result($result, $i, "ttrss_entries.id"),
440 "title" => db_fetch_result($result, $i, "ttrss_entries.title"),
441 "link" => db_fetch_result($result, $i, "link"),
442 "labels" => get_article_labels($link, $article_id),
443 "unread" => sql_bool_to_bool(db_fetch_result($result, $i, "unread")),
444 "marked" => sql_bool_to_bool(db_fetch_result($result, $i, "marked")),
445 "published" => sql_bool_to_bool(db_fetch_result($result, $i, "published")),
446 "comments" => db_fetch_result($result, $i, "comments"),
447 "author" => db_fetch_result($result, $i, "author"),
448 "updated" => strtotime(db_fetch_result($result, $i, "updated")),
449 "content" => db_fetch_result($result, $i, "content"),
450 "feed_id" => db_fetch_result($result, $i, "feed_id"),
451 "attachments" => $attachments
452 );
453
454 array_push($ret, $article);
455
456 $i++;
457 }
458 }
459
460 print api_wrap_reply(API_STATUS_OK, $seq, $ret);
461 break;
462
463 /* Method added for ttrss-reader for Android */
464 case "getNewArticles":
465 $time = (int) db_escape_string($_REQUEST["time"]);
466 // unread=1 zeigt alle an, unread=0 nur ungelesene
467 $displayUnread = (int) db_escape_string($_REQUEST["unread"]);
468
469 if (DB_TYPE == "mysql") {
470 $db_time_function = " AND last_updated > FROM_UNIXTIME(".$time.")";
471 } else if (DB_TYPE == "pgsql") {
472 $db_time_function = " AND last_updated > to_timestamp(".$time.")";
473 } else {
474 $db_time_function = "";
475 }
476
477 if (DB_TYPE == "mysql") {
478 $db_time_function2 = " AND updated > FROM_UNIXTIME(".$time.")";
479 } else if (DB_TYPE == "pgsql") {
480 $db_time_function2 = " AND updated > to_timestamp(".$time.")";
481 } else {
482 $db_time_function2 = "";
483 }
484
485 $cats = array();
486
487
488 // Add uncategorized feeds
489 $unread = getFeedUnread($link, 0, true);
490 if ($unread || $displayUnread > 0) {
491 $feeds = array();
492 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
493 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated ".
494 "FROM ttrss_feeds WHERE cat_id IS null AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
495
496 while ($line_feeds = db_fetch_assoc($result_0)) {
497 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
498 if ($unread || $displayUnread > 0) {
499
500 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
501 marked,unread,published,".
502 SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
503 FROM ttrss_entries,ttrss_user_entries
504 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
505 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
506
507 $articles = array();
508 while ($line_articles = db_fetch_assoc($result_1)) {
509 $article_id = db_fetch_result($result, $i, "id");
510 $attachments = get_article_enclosures($link, $article_id);
511 array_push($articles, $article = array(
512 "id" => $line_articles["id"],
513 "title" => $line_articles["title"],
514 "link" => $line_articles["link"],
515 "labels" => $article_id,
516 "unread" => $line_articles["unread"],
517 "marked" => $line_articles["marked"],
518 "published" => $line_articles["published"],
519 "comments" => $line_articles["comments"],
520 "author" => $line_articles["author"],
521 "updated" => strtotime($line_articles["updated"]),
522 "content" => $line_articles["content"],
523 "feed_id" => $line_articles["feed_id"],
524 "attachments" => $attachments));
525 }
526
527 array_push($feeds, array(
528 "feed_url" => $line_feeds["feed_url"],
529 "title" => $line_feeds["title"],
530 "id" => (int)$line_feeds["id"],
531 "unread" => (int)$unread_feed,
532 "has_icon" => $has_icon,
533 "cat_id" => (int)$line_feeds["cat_id"],
534 "last_updated" => strtotime($line_feeds["last_updated"]),
535 "articles" => $articles
536 ));
537 }
538 }
539
540 array_push($cats,
541 array(
542 "id" => 0,
543 "title" => "Uncategorized Feeds",
544 "unread" => $unread,
545 "feeds" => $feeds));
546 }
547
548
549 $result = db_query($link, "SELECT id, title FROM ttrss_feed_categories WHERE owner_uid = " . $_SESSION["uid"]);
550 while ($line = db_fetch_assoc($result)) {
551 $unread = getFeedUnread($link, $line["id"], true);
552
553 if ($unread || $displayUnread > 0) {
554 $feeds = array();
555 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
556 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated ".
557 "FROM ttrss_feeds WHERE cat_id = '".
558 $line["id"]."' AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
559
560 while ($line_feeds = db_fetch_assoc($result_0)) {
561 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
562 if ($unread_feed || $displayUnread > 0) {
563
564 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
565 marked,unread,published,".
566 SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
567 FROM ttrss_entries,ttrss_user_entries
568 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
569 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
570
571 $articles = array();
572 while ($line_articles = db_fetch_assoc($result_1)) {
573 $article_id = db_fetch_result($result, $i, "id");
574 $attachments = get_article_enclosures($link, $article_id);
575 array_push($articles, $article = array(
576 "id" => $line_articles["id"],
577 "title" => $line_articles["title"],
578 "link" => $line_articles["link"],
579 "labels" => $article_id,
580 "unread" => $line_articles["unread"],
581 "marked" => $line_articles["marked"],
582 "published" => $line_articles["published"],
583 "comments" => $line_articles["comments"],
584 "author" => $line_articles["author"],
585 "updated" => strtotime($line_articles["updated"]),
586 "content" => $line_articles["content"],
587 "feed_id" => $line_articles["feed_id"],
588 "attachments" => $attachments));
589 }
590
591 array_push($feeds, array(
592 "feed_url" => $line_feeds["feed_url"],
593 "title" => $line_feeds["title"],
594 "id" => (int)$line_feeds["id"],
595 "unread" => (int)$unread_feed,
596 "cat_id" => (int)$line_feeds["cat_id"],
597 "last_updated" => strtotime($line_feeds["last_updated"]),
598 "articles" => $articles
599 ));
600
601 }
602 }
603
604 array_push($cats,
605 array(
606 "id" => $line["id"],
607 "title" => $line["title"],
608 "unread" => $unread,
609 "feeds" => $feeds));
610 }
611 }
612 print api_wrap_reply(API_STATUS_OK, $seq, $cats);
613 break;
614
615 default:
616 print api_wrap_reply(API_STATUS_ERR, $seq,
617 array("error" => 'UNKNOWN_METHOD'));
618 break;
619
620 }
621
622 db_close($link);
623
624 ?>