]>
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 define('API_STATUS_OK', 0);
11 define('API_STATUS_ERR', 1);
13 if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT
) {
14 ob_start("ob_gzhandler");
17 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
19 $session_expire = SESSION_EXPIRE_TIME
; //seconds
20 $session_name = (!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid_api" : TTRSS_SESSION_NAME
. "_api";
22 session_name($session_name);
24 if ($_REQUEST["sid"]) {
25 session_id($_REQUEST["sid"]);
31 if (DB_TYPE
== "mysql") {
34 // PG seems to display its own errors just fine by default.
38 init_connection($link);
40 $op = db_escape_string($_REQUEST["op"]);
41 $seq = (int) $_REQUEST["seq"];
43 // header("Content-Type: application/json");
45 function api_wrap_reply($status, $seq, $reply) {
46 print json_encode(array("seq" => $seq,
48 "content" => $reply));
51 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
52 print api_wrap_reply(API_STATUS_ERR
, $seq, array("error" => 'NOT_LOGGED_IN'));
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'));
64 $rv = array("version" => VERSION
);
65 print api_wrap_reply(API_STATUS_OK
, $seq, $rv);
69 $login = db_escape_string($_REQUEST["user"]);
70 $password = db_escape_string($_REQUEST["password"]);
71 $password_base64 = db_escape_string(base64_decode($_REQUEST["password"]));
73 if (SINGLE_USER_MODE
) $login = "admin";
75 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
77 if (db_num_rows($result) != 0) {
78 $uid = db_fetch_result($result, 0, "id");
83 if ($uid && get_pref($link, "ENABLE_API_ACCESS", $uid)) {
84 if (authenticate_user($link, $login, $password)) { // try login with normal password
85 print api_wrap_reply(API_STATUS_OK
, $seq,
86 array("session_id" => session_id()));
87 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
88 print api_wrap_reply(API_STATUS_OK
, $seq,
89 array("session_id" => session_id()));
90 } else { // else we are not logged in
91 print api_wrap_reply(API_STATUS_ERR
, $seq,
92 array("error" => "LOGIN_ERROR"));
95 print api_wrap_reply(API_STATUS_ERR
, $seq,
96 array("error" => "API_DISABLED"));
103 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
107 print api_wrap_reply(API_STATUS_OK
, $seq,
108 array("status" => $_SESSION["uid"] != ''));
112 $feed_id = db_escape_string($_REQUEST["feed_id"]);
113 $is_cat = db_escape_string($_REQUEST["is_cat"]);
116 print api_wrap_reply(API_STATUS_OK
, $seq,
117 array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
119 print api_wrap_reply(API_STATUS_OK
, $seq,
120 array("unread" => getGlobalUnread($link)));
124 /* Method added for ttrss-reader for Android */
127 /* flct (flc is the default) FIXME: document */
128 $output_mode = db_escape_string($_REQUEST["output_mode"]);
130 print api_wrap_reply(API_STATUS_OK
, $seq,
131 getAllCounters($link, $output_mode));
135 $cat_id = db_escape_string($_REQUEST["cat_id"]);
136 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
137 $limit = (int) db_escape_string($_REQUEST["limit"]);
138 $offset = (int) db_escape_string($_REQUEST["offset"]);
140 chdir(".."); // so feed_has_icon() would work properly for relative ICONS_DIR
142 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
144 print api_wrap_reply(API_STATUS_OK
, $seq, $feeds);
148 case "getCategories":
149 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
151 $result = db_query($link, "SELECT
152 id, title FROM ttrss_feed_categories
153 WHERE owner_uid = " .
158 while ($line = db_fetch_assoc($result)) {
159 $unread = getFeedUnread($link, $line["id"], true);
161 if ($unread ||
!$unread_only) {
162 array_push($cats, array("id" => $line["id"],
163 "title" => $line["title"],
164 "unread" => $unread));
168 print api_wrap_reply(API_STATUS_OK
, $seq, $cats);
172 $feed_id = db_escape_string($_REQUEST["feed_id"]);
173 $limit = (int)db_escape_string($_REQUEST["limit"]);
174 $offset = (int)db_escape_string($_REQUEST["skip"]);
175 $filter = db_escape_string($_REQUEST["filter"]);
176 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
177 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
178 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
179 /* all_articles, unread, adaptive, marked, updated */
180 $view_mode = db_escape_string($_REQUEST["view_mode"]);
182 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
183 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false);
185 print api_wrap_reply(API_STATUS_OK
, $seq, $headlines);
189 case "updateArticle":
190 $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
191 $mode = (int) db_escape_string($_REQUEST["mode"]);
192 $field_raw = (int)db_escape_string($_REQUEST["field"]);
197 switch ($field_raw) {
202 $field = "published";
217 $set_to = "NOT $field";
221 if ($field && $set_to && count($article_ids) > 0) {
223 $article_ids = join(", ", $article_ids);
225 if ($field == "unread") {
226 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
228 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
230 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
231 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
234 $num_updated = db_affected_rows($link, $result);
236 if ($num_updated > 0 && $field == "unread") {
237 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
238 WHERE ref_id IN ($article_ids)");
240 while ($line = db_fetch_assoc($result)) {
241 ccache_update($link, $line["feed_id"], $_SESSION["uid"]);
245 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK",
246 "updated" => $num_updated));
249 print api_wrap_reply(API_STATUS_ERR
, $seq,
250 array("error" => 'INCORRECT_USAGE'));
257 $article_id = db_escape_string($_REQUEST["article_id"]);
259 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
260 marked,unread,published,
261 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
263 FROM ttrss_entries,ttrss_user_entries
264 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
267 $result = db_query($link, $query);
271 if (db_num_rows($result) != 0) {
273 while ($line = db_fetch_assoc($result)) {
275 $attachments = get_article_enclosures($link, $line['id']);
279 "title" => $line["title"],
280 "link" => $line["link"],
281 "labels" => get_article_labels($link, $line['id']),
282 "unread" => sql_bool_to_bool($line["unread"]),
283 "marked" => sql_bool_to_bool($line["marked"]),
284 "published" => sql_bool_to_bool($line["published"]),
285 "comments" => $line["comments"],
286 "author" => $line["author"],
287 "updated" => strtotime($line["updated"]),
288 "content" => $line["content"],
289 "feed_id" => $line["feed_id"],
290 "attachments" => $attachments
293 array_push($articles, $article);
298 print api_wrap_reply(API_STATUS_OK
, $seq, $articles);
304 "icons_dir" => ICONS_DIR
,
305 "icons_url" => ICONS_URL
);
307 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
309 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
310 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
312 $num_feeds = db_fetch_result($result, 0, "cf");
314 $config["num_feeds"] = (int)$num_feeds;
316 print api_wrap_reply(API_STATUS_OK
, $seq, $config);
321 $feed_id = db_escape_string($_REQUEST["feed_id"]);
323 update_rss_feed($link, $feed_id, true);
325 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
330 $feed_id = db_escape_string($_REQUEST["feed_id"]);
331 $is_cat = db_escape_string($_REQUEST["is_cat"]);
333 catchup_feed($link, $feed_id, $is_cat);
335 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
340 $pref_name = db_escape_string($_REQUEST["pref_name"]);
342 print api_wrap_reply(API_STATUS_OK
, $seq,
343 array("value" => get_pref($link, $pref_name)));
346 /* Method added for ttrss-reader for Android */
348 $isCategory = (int)db_escape_string($_REQUEST["is_cat"]);
349 $id = (int)db_escape_string($_REQUEST["id"]);
350 $displayUnread = (int)db_escape_string($_REQUEST["unread"]);
351 $limit = (int)db_escape_string($_REQUEST["limit"]);
354 if ($isCategory > 0) {
355 // Get Feeds of the category
358 $category_part = "cat_id is NULL";
360 $category_part = "cat_id = '$id'";
363 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE ".
364 $category_part." AND owner_uid = '".$_SESSION["uid"]."'");
366 while ($line = db_fetch_assoc($result)) {
367 array_push($feeds, $line["id"]);
373 $match_part = "marked = true";
374 array_push($feeds, -1);
375 } else if ($id == -2) {
376 $match_part = "published = true";
377 array_push($feeds, -2);
378 } else if ($id == -3) {
379 $match_part = "unread = true";
380 array_push($feeds, -3);
382 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
384 if (DB_TYPE
== "pgsql") {
385 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
387 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
389 } else if ($id == -4) {
390 $match_part = "true";
391 array_push($feeds, -4);
395 array_push($feeds, $id);
400 if (DB_TYPE
== "mysql") {
401 $limit_part = " LIMIT 0,".$limit;
402 } else if (DB_TYPE
== "pgsql") {
403 $limit_part = " LIMIT ".$limit;
408 // Fetch articles for the feeds
409 foreach ($feeds as $feed) {
412 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
413 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
415 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
416 marked,unread,published,".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,author
417 FROM $from_qpart WHERE
418 ttrss_user_entries.ref_id = ttrss_entries.id AND
419 $feeds_qpart ($match_part) AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]." ORDER BY updated DESC".$limit_part;
421 $result = db_query($link, $query);
423 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
424 marked,unread,published,".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,author
425 FROM ttrss_entries,ttrss_user_entries
426 WHERE feed_id = '".$feed."' AND ref_id = id AND owner_uid = ".
427 $_SESSION["uid"]." AND unread >= '".$displayUnread."' ORDER BY updated DESC".$limit_part;
429 $result = db_query($link, $query);
434 while ($i < mysql_numrows($result)) {
436 $article_id = db_fetch_result($result, $i, "id");
438 $attachments = get_article_enclosures($link, $article_id);
441 "id" => db_fetch_result($result, $i, "ttrss_entries.id"),
442 "title" => db_fetch_result($result, $i, "ttrss_entries.title"),
443 "link" => db_fetch_result($result, $i, "link"),
444 "labels" => get_article_labels($link, $article_id),
445 "unread" => sql_bool_to_bool(db_fetch_result($result, $i, "unread")),
446 "marked" => sql_bool_to_bool(db_fetch_result($result, $i, "marked")),
447 "published" => sql_bool_to_bool(db_fetch_result($result, $i, "published")),
448 "comments" => db_fetch_result($result, $i, "comments"),
449 "author" => db_fetch_result($result, $i, "author"),
450 "updated" => strtotime(db_fetch_result($result, $i, "updated")),
451 "content" => db_fetch_result($result, $i, "content"),
452 "feed_id" => db_fetch_result($result, $i, "feed_id"),
453 "attachments" => $attachments
456 array_push($ret, $article);
462 print api_wrap_reply(API_STATUS_OK
, $seq, $ret);
465 /* Method added for ttrss-reader for Android */
466 case "getNewArticles":
467 $time = (int) db_escape_string($_REQUEST["time"]);
468 // unread=1 zeigt alle an, unread=0 nur ungelesene
469 $displayUnread = (int) db_escape_string($_REQUEST["unread"]);
471 if (DB_TYPE
== "mysql") {
472 $db_time_function = " AND last_updated > FROM_UNIXTIME(".$time.")";
473 } else if (DB_TYPE
== "pgsql") {
474 $db_time_function = " AND last_updated > to_timestamp(".$time.")";
476 $db_time_function = "";
479 if (DB_TYPE
== "mysql") {
480 $db_time_function2 = " AND updated > FROM_UNIXTIME(".$time.")";
481 } else if (DB_TYPE
== "pgsql") {
482 $db_time_function2 = " AND updated > to_timestamp(".$time.")";
484 $db_time_function2 = "";
490 // Add uncategorized feeds
491 $unread = getFeedUnread($link, 0, true);
492 if ($unread ||
$displayUnread > 0) {
494 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
495 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated ".
496 "FROM ttrss_feeds WHERE cat_id IS null AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
498 while ($line_feeds = db_fetch_assoc($result_0)) {
499 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
500 if ($unread ||
$displayUnread > 0) {
502 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
503 marked,unread,published,".
504 SUBSTRING_FOR_DATE
."(updated,1,16) as updated,author
505 FROM ttrss_entries,ttrss_user_entries
506 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
507 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
510 while ($line_articles = db_fetch_assoc($result_1)) {
511 $article_id = db_fetch_result($result, $i, "id");
512 $attachments = get_article_enclosures($link, $article_id);
513 array_push($articles, $article = array(
514 "id" => $line_articles["id"],
515 "title" => $line_articles["title"],
516 "link" => $line_articles["link"],
517 "labels" => $article_id,
518 "unread" => $line_articles["unread"],
519 "marked" => $line_articles["marked"],
520 "published" => $line_articles["published"],
521 "comments" => $line_articles["comments"],
522 "author" => $line_articles["author"],
523 "updated" => strtotime($line_articles["updated"]),
524 "content" => $line_articles["content"],
525 "feed_id" => $line_articles["feed_id"],
526 "attachments" => $attachments));
529 array_push($feeds, array(
530 "feed_url" => $line_feeds["feed_url"],
531 "title" => $line_feeds["title"],
532 "id" => (int)$line_feeds["id"],
533 "unread" => (int)$unread_feed,
534 "has_icon" => $has_icon,
535 "cat_id" => (int)$line_feeds["cat_id"],
536 "last_updated" => strtotime($line_feeds["last_updated"]),
537 "articles" => $articles
545 "title" => "Uncategorized Feeds",
551 $result = db_query($link, "SELECT id, title FROM ttrss_feed_categories WHERE owner_uid = " . $_SESSION["uid"]);
552 while ($line = db_fetch_assoc($result)) {
553 $unread = getFeedUnread($link, $line["id"], true);
555 if ($unread ||
$displayUnread > 0) {
557 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
558 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated ".
559 "FROM ttrss_feeds WHERE cat_id = '".
560 $line["id"]."' AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
562 while ($line_feeds = db_fetch_assoc($result_0)) {
563 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
564 if ($unread_feed ||
$displayUnread > 0) {
566 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
567 marked,unread,published,".
568 SUBSTRING_FOR_DATE
."(updated,1,16) as updated,author
569 FROM ttrss_entries,ttrss_user_entries
570 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
571 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
574 while ($line_articles = db_fetch_assoc($result_1)) {
575 $article_id = db_fetch_result($result, $i, "id");
576 $attachments = get_article_enclosures($link, $article_id);
577 array_push($articles, $article = array(
578 "id" => $line_articles["id"],
579 "title" => $line_articles["title"],
580 "link" => $line_articles["link"],
581 "labels" => $article_id,
582 "unread" => $line_articles["unread"],
583 "marked" => $line_articles["marked"],
584 "published" => $line_articles["published"],
585 "comments" => $line_articles["comments"],
586 "author" => $line_articles["author"],
587 "updated" => strtotime($line_articles["updated"]),
588 "content" => $line_articles["content"],
589 "feed_id" => $line_articles["feed_id"],
590 "attachments" => $attachments));
593 array_push($feeds, array(
594 "feed_url" => $line_feeds["feed_url"],
595 "title" => $line_feeds["title"],
596 "id" => (int)$line_feeds["id"],
597 "unread" => (int)$unread_feed,
598 "cat_id" => (int)$line_feeds["cat_id"],
599 "last_updated" => strtotime($line_feeds["last_updated"]),
600 "articles" => $articles
609 "title" => $line["title"],
614 print api_wrap_reply(API_STATUS_OK
, $seq, $cats);
618 print api_wrap_reply(API_STATUS_ERR
, $seq,
619 array("error" => 'UNKNOWN_METHOD'));