]>
git.wh0rd.org - tt-rss.git/blob - api/index.php
fd22f4b8bddf47a40c59abf0c1c9811daf43e86f
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 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
63 if (db_num_rows($result) != 0) {
64 $uid = db_fetch_result($result, 0, "id");
69 if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
70 if (authenticate_user($link, $login, $password)) {
71 print json_encode(array("uid" => $_SESSION["uid"]));
73 print json_encode(array("error" => "LOGIN_ERROR"));
76 print json_encode(array("error" => "API_DISABLED"));
82 print json_encode(array("uid" => 0));
85 print json_encode(array("status" => $_SESSION["uid"] != ''));
88 $feed_id = db_escape_string($_REQUEST["feed_id"]);
89 $is_cat = db_escape_string($_REQUEST["is_cat"]);
92 print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
94 print json_encode(array("unread" => getGlobalUnread($link)));
98 $cat_id = db_escape_string($_REQUEST["cat_id"]);
99 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
102 $result = db_query($link, "SELECT
103 id, feed_url, cat_id, title, ".
104 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
105 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
107 $result = db_query($link, "SELECT
108 id, feed_url, cat_id, title, ".
109 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
110 FROM ttrss_feeds WHERE
111 cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
116 while ($line = db_fetch_assoc($result)) {
118 $unread = getFeedUnread($link, $line["id"]);
120 if ($unread ||
!$unread_only) {
123 "feed_url" => $line["feed_url"],
124 "title" => $line["title"],
125 "id" => (int)$line["id"],
126 "unread" => (int)$unread,
127 "cat_id" => (int)$line["cat_id"],
128 "last_updated" => strtotime($line["last_updated"])
131 array_push($feeds, $row);
137 if (!$cat_id ||
$cat_id == -2) {
138 $counters = getLabelCounters($link, false, true);
140 foreach (array_keys($counters) as $id) {
142 $unread = $counters[$id]["counter"];
144 if ($unread ||
!$unread_only) {
148 "title" => $counters[$id]["description"],
149 "unread" => $counters[$id]["counter"],
153 array_push($feeds, $row);
160 if (!$cat_id ||
$cat_id == -1) {
161 foreach (array(-1, -2, -3, -4) as $i) {
162 $unread = getFeedUnread($link, $i);
164 if ($unread ||
!$unread_only) {
165 $title = getFeedTitle($link, $i);
173 array_push($feeds, $row);
179 print json_encode($feeds);
182 case "getCategories":
183 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
185 $result = db_query($link, "SELECT
186 id, title FROM ttrss_feed_categories
187 WHERE owner_uid = " .
192 while ($line = db_fetch_assoc($result)) {
193 $unread = getFeedUnread($link, $line["id"], true);
195 if ($unread ||
!$unread_only) {
196 array_push($cats, array("id" => $line["id"],
197 "title" => $line["title"],
198 "unread" => $unread));
202 print json_encode($cats);
205 $feed_id = db_escape_string($_REQUEST["feed_id"]);
206 $limit = (int)db_escape_string($_REQUEST["limit"]);
207 $filter = db_escape_string($_REQUEST["filter"]);
208 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
209 $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]);
211 /* do not rely on params below */
213 $search = db_escape_string($_REQUEST["search"]);
214 $search_mode = db_escape_string($_REQUEST["search_mode"]);
215 $match_on = db_escape_string($_REQUEST["match_on"]);
217 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
218 $view_mode, $is_cat, $search, $search_mode, $match_on);
220 $result = $qfh_ret[0];
221 $feed_title = $qfh_ret[1];
223 $headlines = array();
225 while ($line = db_fetch_assoc($result)) {
226 $is_updated = ($line["last_read"] == "" &&
227 ($line["unread"] != "t" && $line["unread"] != "1"));
229 $headline_row = array(
230 "id" => (int)$line["id"],
231 "unread" => sql_bool_to_bool($line["unread"]),
232 "marked" => sql_bool_to_bool($line["marked"]),
233 "updated" => strtotime($line["updated"]),
234 "is_updated" => $is_updated,
235 "title" => $line["title"],
236 "feed_id" => $line["feed_id"],
239 if ($show_except) $headline_row["excerpt"] = $line["content_preview"];
241 array_push($headlines, $headline_row);
244 print json_encode($headlines);
247 case "updateArticle":
248 $article_id = (int) db_escape_string($_GET["article_id"]);
249 $mode = (int) db_escape_string($_REQUEST["mode"]);
250 $field_raw = (int)db_escape_string($_REQUEST["field"]);
255 switch ($field_raw) {
260 $field = "published";
275 $set_to = "NOT $field";
279 if ($field && $set_to) {
280 if ($field == "unread") {
281 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
283 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
285 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
286 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
294 $article_id = (int)db_escape_string($_REQUEST["article_id"]);
296 $query = "SELECT title,link,content,feed_id,comments,int_id,
297 marked,unread,published,
298 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
300 FROM ttrss_entries,ttrss_user_entries
301 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
304 $result = db_query($link, $query);
308 if (db_num_rows($result) != 0) {
309 $line = db_fetch_assoc($result);
312 "title" => $line["title"],
313 "link" => $line["link"],
314 "labels" => get_article_labels($link, $article_id),
315 "unread" => sql_bool_to_bool($line["unread"]),
316 "marked" => sql_bool_to_bool($line["marked"]),
317 "published" => sql_bool_to_bool($line["published"]),
318 "comments" => $line["comments"],
319 "author" => $line["author"],
320 "updated" => strtotime($line["updated"]),
321 "content" => $line["content"],
322 "feed_id" => $line["feed_id"],
326 print json_encode($article);
331 "icons_dir" => ICONS_DIR
,
332 "icons_url" => ICONS_URL
);
334 if (ENABLE_UPDATE_DAEMON
) {
335 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
338 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
339 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
341 $num_feeds = db_fetch_result($result, 0, "cf");
343 $config["num_feeds"] = (int)$num_feeds;
345 print json_encode($config);
350 $pref_name = db_escape_string($_REQUEST["pref_name"]);
351 print json_encode(array("value" => get_pref($link, $pref_name)));