]>
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 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
75 if (db_num_rows($result) != 0) {
76 $uid = db_fetch_result($result, 0, "id");
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"));
93 print api_wrap_reply(API_STATUS_ERR
, $seq,
94 array("error" => "API_DISABLED"));
101 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
105 print api_wrap_reply(API_STATUS_OK
, $seq,
106 array("status" => $_SESSION["uid"] != ''));
110 $feed_id = db_escape_string($_REQUEST["feed_id"]);
111 $is_cat = db_escape_string($_REQUEST["is_cat"]);
114 print api_wrap_reply(API_STATUS_OK
, $seq,
115 array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
117 print api_wrap_reply(API_STATUS_OK
, $seq,
118 array("unread" => getGlobalUnread($link)));
122 /* Method added for ttrss-reader for Android */
125 /* flct (flc is the default) FIXME: document */
126 $output_mode = db_escape_string($_REQUEST["output_mode"]);
128 print api_wrap_reply(API_STATUS_OK
, $seq,
129 getAllCounters($link, $output_mode));
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"]);
138 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
140 print api_wrap_reply(API_STATUS_OK
, $seq, $feeds);
144 case "getCategories":
145 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
147 $result = db_query($link, "SELECT
148 id, title FROM ttrss_feed_categories
149 WHERE owner_uid = " .
154 while ($line = db_fetch_assoc($result)) {
155 $unread = getFeedUnread($link, $line["id"], true);
157 if ($unread ||
!$unread_only) {
158 array_push($cats, array("id" => $line["id"],
159 "title" => $line["title"],
160 "unread" => $unread));
164 print api_wrap_reply(API_STATUS_OK
, $seq, $cats);
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"]);
178 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
179 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false);
181 print api_wrap_reply(API_STATUS_OK
, $seq, $headlines);
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"]);
193 switch ($field_raw) {
198 $field = "published";
213 $set_to = "NOT $field";
217 if ($field && $set_to && count($article_ids) > 0) {
219 $article_ids = join(", ", $article_ids);
221 if ($field == "unread") {
222 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
224 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
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"]);
230 $num_updated = db_affected_rows($link, $result);
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)");
236 while ($line = db_fetch_assoc($result)) {
237 ccache_update($link, $line["feed_id"], $_SESSION["uid"]);
241 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK",
242 "updated" => $num_updated));
245 print api_wrap_reply(API_STATUS_ERR
, $seq,
246 array("error" => 'INCORRECT_USAGE'));
253 $article_id = db_escape_string($_REQUEST["article_id"]);
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,
259 FROM ttrss_entries,ttrss_user_entries
260 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
263 $result = db_query($link, $query);
267 if (db_num_rows($result) != 0) {
269 while ($line = db_fetch_assoc($result)) {
271 $attachments = get_article_enclosures($link, $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
289 array_push($articles, $article);
294 print api_wrap_reply(API_STATUS_OK
, $seq, $articles);
300 "icons_dir" => ICONS_DIR
,
301 "icons_url" => ICONS_URL
);
303 if (ENABLE_UPDATE_DAEMON
) {
304 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
307 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
308 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
310 $num_feeds = db_fetch_result($result, 0, "cf");
312 $config["num_feeds"] = (int)$num_feeds;
314 print api_wrap_reply(API_STATUS_OK
, $seq, $config);
319 $feed_id = db_escape_string($_REQUEST["feed_id"]);
321 update_rss_feed($link, $feed_id, true);
323 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
328 $feed_id = db_escape_string($_REQUEST["feed_id"]);
329 $is_cat = db_escape_string($_REQUEST["is_cat"]);
331 catchup_feed($link, $feed_id, $is_cat);
333 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
338 $pref_name = db_escape_string($_REQUEST["pref_name"]);
340 print api_wrap_reply(API_STATUS_OK
, $seq,
341 array("value" => get_pref($link, $pref_name)));
344 /* Method added for ttrss-reader for Android */
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"]);
352 if ($isCategory > 0) {
353 // Get Feeds of the category
356 $category_part = "cat_id is NULL";
358 $category_part = "cat_id = '$id'";
361 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE ".
362 $category_part." AND owner_uid = '".$_SESSION["uid"]."'");
364 while ($line = db_fetch_assoc($result)) {
365 array_push($feeds, $line["id"]);
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);
380 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
382 if (DB_TYPE
== "pgsql") {
383 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
385 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
387 } else if ($id == -4) {
388 $match_part = "true";
389 array_push($feeds, -4);
393 array_push($feeds, $id);
398 if (DB_TYPE
== "mysql") {
399 $limit_part = " LIMIT 0,".$limit;
400 } else if (DB_TYPE
== "pgsql") {
401 $limit_part = " LIMIT ".$limit;
406 // Fetch articles for the feeds
407 foreach ($feeds as $feed) {
410 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
411 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
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;
419 $result = db_query($link, $query);
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;
427 $result = db_query($link, $query);
432 while ($i < mysql_numrows($result)) {
434 $article_id = db_fetch_result($result, $i, "id");
436 $attachments = get_article_enclosures($link, $article_id);
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
454 array_push($ret, $article);
460 print api_wrap_reply(API_STATUS_OK
, $seq, $ret);
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"]);
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.")";
474 $db_time_function = "";
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.")";
482 $db_time_function2 = "";
488 // Add uncategorized feeds
489 $unread = getFeedUnread($link, 0, true);
490 if ($unread ||
$displayUnread > 0) {
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);
496 while ($line_feeds = db_fetch_assoc($result_0)) {
497 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
498 if ($unread ||
$displayUnread > 0) {
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);
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));
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
543 "title" => "Uncategorized Feeds",
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);
553 if ($unread ||
$displayUnread > 0) {
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);
560 while ($line_feeds = db_fetch_assoc($result_0)) {
561 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
562 if ($unread_feed ||
$displayUnread > 0) {
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);
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));
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
607 "title" => $line["title"],
612 print api_wrap_reply(API_STATUS_OK
, $seq, $cats);
616 print api_wrap_reply(API_STATUS_ERR
, $seq,
617 array("error" => 'UNKNOWN_METHOD'));