]>
git.wh0rd.org - tt-rss.git/blob - api/index.php
3 /* This is experimental JSON-based API. It has to be manually enabled:
5 * Add define('_JSON_API_ENABLED', true) to config.php
8 error_reporting(E_ERROR | E_PARSE
);
10 require_once "../config.php";
12 require_once "../db.php";
13 require_once "../db-prefs.php";
14 require_once "../functions.php";
16 if (!defined('_JSON_API_ENABLED')) {
17 print json_encode(array("error" => "API_DISABLED"));
21 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
23 $session_expire = SESSION_EXPIRE_TIME
; //seconds
24 $session_name = (!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid_api" : TTRSS_SESSION_NAME
. "_api";
29 if (DB_TYPE
== "mysql") {
32 // PG seems to display its own errors just fine by default.
36 init_connection($link);
38 $op = db_escape_string($_REQUEST["op"]);
40 // header("Content-Type: application/json");
42 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
43 print json_encode(array("error" => 'NOT_LOGGED_IN'));
47 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
48 print json_encode(array("error" => 'API_DISABLED'));
54 $rv = array("version" => VERSION
);
55 print json_encode($rv);
58 $login = db_escape_string($_REQUEST["user"]);
59 $password = db_escape_string($_REQUEST["password"]);
61 if (authenticate_user($link, $login, $password)) {
62 print json_encode(array("uid" => $_SESSION["uid"]));
64 print json_encode(array("error" => "LOGIN_ERROR"));
70 print json_encode(array("uid" => 0));
73 print json_encode(array("status" => $_SESSION["uid"] != ''));
76 $feed_id = db_escape_string($_REQUEST["feed_id"]);
77 $is_cat = db_escape_string($_REQUEST["is_cat"]);
80 print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
82 print json_encode(array("unread" => getGlobalUnread($link)));
86 $cat_id = db_escape_string($_REQUEST["cat_id"]);
87 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
90 $result = db_query($link, "SELECT
91 id, feed_url, cat_id, title, ".
92 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
93 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
95 $result = db_query($link, "SELECT
96 id, feed_url, cat_id, title, ".
97 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
98 FROM ttrss_feeds WHERE
99 cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
104 while ($line = db_fetch_assoc($result)) {
106 $unread = getFeedUnread($link, $line["id"]);
108 if ($unread ||
!$unread_only) {
111 "feed_url" => $line["feed_url"],
112 "title" => $line["title"],
113 "id" => (int)$line["id"],
114 "unread" => (int)$unread,
115 "cat_id" => (int)$line["cat_id"],
116 "last_updated" => strtotime($line["last_updated"])
119 array_push($feeds, $row);
125 if (!$cat_id ||
$cat_id == -2) {
126 $counters = getLabelCounters($link, false, true);
128 foreach (array_keys($counters) as $id) {
130 $unread = $counters[$id]["counter"];
132 if ($unread ||
!$unread_only) {
136 "title" => $counters[$id]["description"],
137 "unread" => $counters[$id]["counter"],
141 array_push($feeds, $row);
148 if (!$cat_id ||
$cat_id == -1) {
149 foreach (array(-1, -2, -3, -4) as $i) {
150 $unread = getFeedUnread($link, $i);
152 if ($unread ||
!$unread_only) {
153 $title = getFeedTitle($link, $i);
161 array_push($feeds, $row);
167 print json_encode($feeds);
170 case "getCategories":
171 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
173 $result = db_query($link, "SELECT
174 id, title FROM ttrss_feed_categories
175 WHERE owner_uid = " .
180 while ($line = db_fetch_assoc($result)) {
181 $unread = getFeedUnread($link, $line["id"], true);
183 if ($unread ||
!$unread_only) {
184 array_push($cats, array("id" => $line["id"],
185 "title" => $line["title"],
186 "unread" => $unread));
190 print json_encode($cats);
193 $feed_id = db_escape_string($_REQUEST["feed_id"]);
194 $limit = (int)db_escape_string($_REQUEST["limit"]);
195 $filter = db_escape_string($_REQUEST["filter"]);
196 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
197 $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]);
199 /* do not rely on params below */
201 $search = db_escape_string($_REQUEST["search"]);
202 $search_mode = db_escape_string($_REQUEST["search_mode"]);
203 $match_on = db_escape_string($_REQUEST["match_on"]);
205 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
206 $view_mode, $is_cat, $search, $search_mode, $match_on);
208 $result = $qfh_ret[0];
209 $feed_title = $qfh_ret[1];
211 $headlines = array();
213 while ($line = db_fetch_assoc($result)) {
214 $is_updated = ($line["last_read"] == "" &&
215 ($line["unread"] != "t" && $line["unread"] != "1"));
217 $headline_row = array(
218 "id" => (int)$line["id"],
219 "unread" => sql_bool_to_bool($line["unread"]),
220 "marked" => sql_bool_to_bool($line["marked"]),
221 "updated" => strtotime($line["updated"]),
222 "is_updated" => $is_updated,
223 "title" => $line["title"],
224 "feed_id" => $line["feed_id"],
227 if ($show_except) $headline_row["excerpt"] = $line["content_preview"];
229 array_push($headlines, $headline_row);
232 print json_encode($headlines);
235 case "updateArticle":
236 $article_id = (int) db_escape_string($_GET["article_id"]);
237 $mode = (int) db_escape_string($_REQUEST["mode"]);
238 $field_raw = (int)db_escape_string($_REQUEST["field"]);
243 switch ($field_raw) {
248 $field = "published";
263 $set_to = "NOT $field";
267 if ($field && $set_to) {
268 if ($field == "unread") {
269 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
271 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
273 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
274 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
282 $article_id = (int)db_escape_string($_REQUEST["article_id"]);
284 $query = "SELECT title,link,content,feed_id,comments,int_id,
285 marked,unread,published,
286 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
288 FROM ttrss_entries,ttrss_user_entries
289 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
292 $result = db_query($link, $query);
296 if (db_num_rows($result) != 0) {
297 $line = db_fetch_assoc($result);
300 "title" => $line["title"],
301 "link" => $line["link"],
302 "labels" => get_article_labels($link, $article_id),
303 "unread" => sql_bool_to_bool($line["unread"]),
304 "marked" => sql_bool_to_bool($line["marked"]),
305 "published" => sql_bool_to_bool($line["published"]),
306 "comments" => $line["comments"],
307 "author" => $line["author"],
308 "updated" => strtotime($line["updated"]),
309 "content" => $line["content"],
310 "feed_id" => $line["feed_id"],
314 print json_encode($article);