]>
git.wh0rd.org - tt-rss.git/blob - api/index.php
2 error_reporting(E_ERROR | E_PARSE
);
4 require_once "../config.php";
6 require_once "../db.php";
7 require_once "../db-prefs.php";
8 require_once "../functions.php";
10 if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT
) {
11 ob_start("ob_gzhandler");
14 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
16 $session_expire = SESSION_EXPIRE_TIME
; //seconds
17 $session_name = (!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid_api" : TTRSS_SESSION_NAME
. "_api";
19 session_name($session_name);
21 if ($_REQUEST["sid"]) {
22 session_id($_REQUEST["sid"]);
28 if (DB_TYPE
== "mysql") {
31 // PG seems to display its own errors just fine by default.
35 init_connection($link);
37 $op = db_escape_string($_REQUEST["op"]);
39 // header("Content-Type: application/json");
41 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
42 print json_encode(array("error" => 'NOT_LOGGED_IN'));
46 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
47 print json_encode(array("error" => 'API_DISABLED'));
54 $rv = array("version" => VERSION
);
55 print json_encode($rv);
59 $login = db_escape_string($_REQUEST["user"]);
60 $password = db_escape_string($_REQUEST["password"]);
61 $password_base64 = db_escape_string(base64_decode($_REQUEST["password"]));
63 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
65 if (db_num_rows($result) != 0) {
66 $uid = db_fetch_result($result, 0, "id");
71 if ($uid && get_pref($link, "ENABLE_API_ACCESS", $uid)) {
72 if (authenticate_user($link, $login, $password)) { // try login with normal password
73 print json_encode(array("session_id" => session_id()));
74 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
75 print json_encode(array("session_id" => session_id()));
76 } else { // else we are not logged in
77 print json_encode(array("error" => "LOGIN_ERROR"));
80 print json_encode(array("error" => "API_DISABLED"));
87 print json_encode(array("status" => "OK"));
91 print json_encode(array("status" => $_SESSION["uid"] != ''));
95 $feed_id = db_escape_string($_REQUEST["feed_id"]);
96 $is_cat = db_escape_string($_REQUEST["is_cat"]);
99 print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
101 print json_encode(array("unread" => getGlobalUnread($link)));
105 /* Method added for ttrss-reader for Android */
109 $result = db_query($link, "SELECT
110 id FROM ttrss_feed_categories
111 WHERE owner_uid = " .
116 while ($line = db_fetch_assoc($result)) {
117 array_push($cats, $line["id"]);
119 array_push($cats, "0");
120 array_push($cats, "-1");
121 array_push($cats, "-2");
122 array_push($cats, "-3");
123 array_push($cats, "-4");
125 foreach ($cats as $cat) {
127 $cat_part = "cat_id = '$cat'";
129 $cat_part = "cat_id IS null";
132 $result = db_query($link, "SELECT
133 id FROM ttrss_feeds WHERE ".
134 $cat_part." AND owner_uid = " . $_SESSION["uid"]);
138 while ($line = db_fetch_assoc($result)) {
140 $unread = getFeedArticles($link, $line["id"], false, true, $_SESSION["uid"]);
144 "feed_id" => (int)$line["id"],
145 "unread" => (int)$unread
147 array_push($feeds, $row);
156 $unread_cat = getFeedArticles($link, $cat, $is_cat, true, $_SESSION["uid"]);
161 "unread" => $unread_cat,
167 "unread" => $unread_cat
171 array_push($counters, $count);
173 print json_encode($counters);
177 $cat_id = db_escape_string($_REQUEST["cat_id"]);
178 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
179 $limit = (int) db_escape_string($_REQUEST["limit"]);
180 $offset = (int) db_escape_string($_REQUEST["offset"]);
182 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
184 print json_encode($feeds);
188 case "getCategories":
189 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
191 $result = db_query($link, "SELECT
192 id, title FROM ttrss_feed_categories
193 WHERE owner_uid = " .
198 while ($line = db_fetch_assoc($result)) {
199 $unread = getFeedUnread($link, $line["id"], true);
201 if ($unread ||
!$unread_only) {
202 array_push($cats, array("id" => $line["id"],
203 "title" => $line["title"],
204 "unread" => $unread));
208 print json_encode($cats);
212 $feed_id = db_escape_string($_REQUEST["feed_id"]);
213 $limit = (int)db_escape_string($_REQUEST["limit"]);
214 $offset = (int)db_escape_string($_REQUEST["skip"]);
215 $filter = db_escape_string($_REQUEST["filter"]);
216 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
217 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
218 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
219 /* all_articles, unread, adaptive, marked, updated */
220 $view_mode = db_escape_string($_REQUEST["view_mode"]);
222 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
223 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false);
225 print json_encode($headlines);
229 case "updateArticle":
230 $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
231 $mode = (int) db_escape_string($_REQUEST["mode"]);
232 $field_raw = (int)db_escape_string($_REQUEST["field"]);
237 switch ($field_raw) {
242 $field = "published";
257 $set_to = "NOT $field";
261 if ($field && $set_to && count($article_ids) > 0) {
263 $article_ids = join(", ", $article_ids);
265 if ($field == "unread") {
266 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
268 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
270 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
271 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
274 // FIXME: find out which feeds reference this article id and do ccache_update() on them
281 $article_id = db_escape_string($_REQUEST["article_id"]);
283 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
284 marked,unread,published,
285 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
287 FROM ttrss_entries,ttrss_user_entries
288 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
291 $result = db_query($link, $query);
293 if (db_num_rows($result) != 0) {
295 while ($line = db_fetch_assoc($result)) {
297 $attachments = get_article_enclosures($link, $line['id']);
301 "title" => $line["title"],
302 "link" => $line["link"],
303 "labels" => get_article_labels($link, $line['id']),
304 "unread" => sql_bool_to_bool($line["unread"]),
305 "marked" => sql_bool_to_bool($line["marked"]),
306 "published" => sql_bool_to_bool($line["published"]),
307 "comments" => $line["comments"],
308 "author" => $line["author"],
309 "updated" => strtotime($line["updated"]),
310 "content" => $line["content"],
311 "feed_id" => $line["feed_id"],
312 "attachments" => $attachments
315 print json_encode($article);
323 "icons_dir" => ICONS_DIR
,
324 "icons_url" => ICONS_URL
);
326 if (ENABLE_UPDATE_DAEMON
) {
327 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
330 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
331 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
333 $num_feeds = db_fetch_result($result, 0, "cf");
335 $config["num_feeds"] = (int)$num_feeds;
337 print json_encode($config);
342 $feed_id = db_escape_string($_REQUEST["feed_id"]);
344 update_rss_feed($link, $feed_id, true);
346 print json_encode(array("status" => "OK"));
351 $feed_id = db_escape_string($_REQUEST["feed_id"]);
352 $is_cat = db_escape_string($_REQUEST["category"]);
354 catchup_feed($link, $feed_id, $is_cat);
356 print json_encode(array("status" => "OK"));
361 $pref_name = db_escape_string($_REQUEST["pref_name"]);
362 print json_encode(array("value" => get_pref($link, $pref_name)));
365 /* Method added for ttrss-reader for Android */
367 $isCategory = (int)db_escape_string($_REQUEST["is_category"]);
368 $id = (int)db_escape_string($_REQUEST["id"]);
369 $displayUnread = (int)db_escape_string($_REQUEST["unread"]);
370 $limit = (int)db_escape_string($_REQUEST["limit"]);
373 if ($isCategory > 0) {
374 // Get Feeds of the category
377 $category_part = "cat_id is NULL";
379 $category_part = "cat_id = '$id'";
382 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE ".
383 $category_part." AND owner_uid = '".$_SESSION["uid"]."'");
385 while ($line = db_fetch_assoc($result)) {
386 array_push($feeds, $line["id"]);
392 $match_part = "marked = true";
393 array_push($feeds, -1);
394 } else if ($id == -2) {
395 $match_part = "published = true";
396 array_push($feeds, -2);
397 } else if ($id == -3) {
398 $match_part = "unread = true";
399 array_push($feeds, -3);
401 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
403 if (DB_TYPE
== "pgsql") {
404 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
406 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
408 } else if ($id == -4) {
409 $match_part = "true";
410 array_push($feeds, -4);
414 array_push($feeds, $id);
419 if (DB_TYPE
== "mysql") {
420 $limit_part = " LIMIT 0,".$limit;
421 } else if (DB_TYPE
== "pgsql") {
422 $limit_part = " LIMIT ".$limit;
427 // Fetch articles for the feeds
428 foreach ($feeds as $feed) {
431 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
432 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
434 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
435 marked,unread,published,".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,author
436 FROM $from_qpart WHERE
437 ttrss_user_entries.ref_id = ttrss_entries.id AND
438 $feeds_qpart ($match_part) AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]." ORDER BY updated DESC".$limit_part;
440 $result = db_query($link, $query);
442 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
443 marked,unread,published,".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,author
444 FROM ttrss_entries,ttrss_user_entries
445 WHERE feed_id = '".$feed."' AND ref_id = id AND owner_uid = ".
446 $_SESSION["uid"]." AND unread >= '".$displayUnread."' ORDER BY updated DESC".$limit_part;
448 $result = db_query($link, $query);
453 while ($i < mysql_numrows($result)) {
455 $article_id = db_fetch_result($result, $i, "id");
457 $attachments = get_article_enclosures($link, $article_id);
460 "id" => db_fetch_result($result, $i, "ttrss_entries.id"),
461 "title" => db_fetch_result($result, $i, "ttrss_entries.title"),
462 "link" => db_fetch_result($result, $i, "link"),
463 "labels" => get_article_labels($link, $article_id),
464 "unread" => sql_bool_to_bool(db_fetch_result($result, $i, "unread")),
465 "marked" => sql_bool_to_bool(db_fetch_result($result, $i, "marked")),
466 "published" => sql_bool_to_bool(db_fetch_result($result, $i, "published")),
467 "comments" => db_fetch_result($result, $i, "comments"),
468 "author" => db_fetch_result($result, $i, "author"),
469 "updated" => strtotime(db_fetch_result($result, $i, "updated")),
470 "content" => db_fetch_result($result, $i, "content"),
471 "feed_id" => db_fetch_result($result, $i, "feed_id"),
472 "attachments" => $attachments
475 array_push($ret, $article);
481 print json_encode($ret);
484 /* Method added for ttrss-reader for Android */
485 case "getNewArticles":
486 $time = (int) db_escape_string($_REQUEST["time"]);
487 // unread=1 zeigt alle an, unread=0 nur ungelesene
488 $displayUnread = (int) db_escape_string($_REQUEST["unread"]);
490 if (DB_TYPE
== "mysql") {
491 $db_time_function = " AND last_updated > FROM_UNIXTIME(".$time.")";
492 } else if (DB_TYPE
== "pgsql") {
493 $db_time_function = " AND last_updated > to_timestamp(".$time.")";
495 $db_time_function = "";
498 if (DB_TYPE
== "mysql") {
499 $db_time_function2 = " AND updated > FROM_UNIXTIME(".$time.")";
500 } else if (DB_TYPE
== "pgsql") {
501 $db_time_function2 = " AND updated > to_timestamp(".$time.")";
503 $db_time_function2 = "";
509 // Add uncategorized feeds
510 $unread = getFeedUnread($link, 0, true);
511 if ($unread ||
$displayUnread > 0) {
513 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
514 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated ".
515 "FROM ttrss_feeds WHERE cat_id IS null AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
517 while ($line_feeds = db_fetch_assoc($result_0)) {
518 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
519 if ($unread ||
$displayUnread > 0) {
521 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
522 marked,unread,published,".
523 SUBSTRING_FOR_DATE
."(updated,1,16) as updated,author
524 FROM ttrss_entries,ttrss_user_entries
525 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
526 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
529 while ($line_articles = db_fetch_assoc($result_1)) {
530 $article_id = db_fetch_result($result, $i, "id");
531 $attachments = get_article_enclosures($link, $article_id);
532 array_push($articles, $article = array(
533 "id" => $line_articles["id"],
534 "title" => $line_articles["title"],
535 "link" => $line_articles["link"],
536 "labels" => $article_id,
537 "unread" => $line_articles["unread"],
538 "marked" => $line_articles["marked"],
539 "published" => $line_articles["published"],
540 "comments" => $line_articles["comments"],
541 "author" => $line_articles["author"],
542 "updated" => strtotime($line_articles["updated"]),
543 "content" => $line_articles["content"],
544 "feed_id" => $line_articles["feed_id"],
545 "attachments" => $attachments));
548 array_push($feeds, array(
549 "feed_url" => $line_feeds["feed_url"],
550 "title" => $line_feeds["title"],
551 "id" => (int)$line_feeds["id"],
552 "unread" => (int)$unread_feed,
553 "has_icon" => $has_icon,
554 "cat_id" => (int)$line_feeds["cat_id"],
555 "last_updated" => strtotime($line_feeds["last_updated"]),
556 "articles" => $articles
564 "title" => "Uncategorized Feeds",
570 $result = db_query($link, "SELECT id, title FROM ttrss_feed_categories WHERE owner_uid = " . $_SESSION["uid"]);
571 while ($line = db_fetch_assoc($result)) {
572 $unread = getFeedUnread($link, $line["id"], true);
574 if ($unread ||
$displayUnread > 0) {
576 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
577 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated ".
578 "FROM ttrss_feeds WHERE cat_id = '".
579 $line["id"]."' AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
581 while ($line_feeds = db_fetch_assoc($result_0)) {
582 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
583 if ($unread_feed ||
$displayUnread > 0) {
585 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
586 marked,unread,published,".
587 SUBSTRING_FOR_DATE
."(updated,1,16) as updated,author
588 FROM ttrss_entries,ttrss_user_entries
589 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
590 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
593 while ($line_articles = db_fetch_assoc($result_1)) {
594 $article_id = db_fetch_result($result, $i, "id");
595 $attachments = get_article_enclosures($link, $article_id);
596 array_push($articles, $article = array(
597 "id" => $line_articles["id"],
598 "title" => $line_articles["title"],
599 "link" => $line_articles["link"],
600 "labels" => $article_id,
601 "unread" => $line_articles["unread"],
602 "marked" => $line_articles["marked"],
603 "published" => $line_articles["published"],
604 "comments" => $line_articles["comments"],
605 "author" => $line_articles["author"],
606 "updated" => strtotime($line_articles["updated"]),
607 "content" => $line_articles["content"],
608 "feed_id" => $line_articles["feed_id"],
609 "attachments" => $attachments));
612 array_push($feeds, array(
613 "feed_url" => $line_feeds["feed_url"],
614 "title" => $line_feeds["title"],
615 "id" => (int)$line_feeds["id"],
616 "unread" => (int)$unread_feed,
617 "cat_id" => (int)$line_feeds["cat_id"],
618 "last_updated" => strtotime($line_feeds["last_updated"]),
619 "articles" => $articles
628 "title" => $line["title"],
633 print json_encode($cats);
637 print json_encode(array("error" => 'UNKNOWN_METHOD'));