]>
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 /* TODO: add pref key to disable/enable API
48 if ($_SESSION["uid"] && !get_pref($link, 'API_ENABLED')) {
49 print json_encode(array("error" => 'API_DISABLED'));
55 $rv = array("version" => VERSION
);
56 print json_encode($rv);
59 $login = db_escape_string($_REQUEST["user"]);
60 $password = db_escape_string($_REQUEST["password"]);
62 if (authenticate_user($link, $login, $password)) {
63 print json_encode(array("uid" => $_SESSION["uid"]));
65 print json_encode(array("uid" => 0));
71 print json_encode(array("uid" => 0));
74 print json_encode(array("status" => $_SESSION["uid"] != ''));
77 $feed_id = db_escape_string($_REQUEST["feed_id"]);
78 $is_cat = db_escape_string($_REQUEST["is_cat"]);
81 print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
83 print json_encode(array("unread" => getGlobalUnread($link)));
87 $cat_id = db_escape_string($_REQUEST["cat_id"]);
88 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
91 $result = db_query($link, "SELECT
92 id, feed_url, cat_id, title, ".
93 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
94 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
96 $result = db_query($link, "SELECT
97 id, feed_url, cat_id, title, ".
98 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
99 FROM ttrss_feeds WHERE
100 cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
105 while ($line = db_fetch_assoc($result)) {
107 $unread = getFeedUnread($link, $line["id"]);
109 if ($unread ||
!$unread_only) {
111 $line_struct = array(
112 "feed_url" => $line["feed_url"],
113 "title" => $line["title"],
114 "id" => (int)$line["id"],
115 "unread" => (int)$unread,
116 "cat_id" => (int)$line["cat_id"],
117 "last_updated" => strtotime($line["last_updated"])
120 array_push($feeds, $line_struct);
124 print json_encode($feeds);
127 case "getCategories":
128 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
130 $result = db_query($link, "SELECT
131 id, title FROM ttrss_feed_categories
132 WHERE owner_uid = " .
137 while ($line = db_fetch_assoc($result)) {
138 $unread = getFeedUnread($link, $line["id"], true);
140 if ($unread ||
!$unread_only) {
141 array_push($cats, array($line["id"] =>
142 array("title" => $line["title"], "unread" => $unread)));
146 print json_encode($cats);
149 $feed_id = db_escape_string($_REQUEST["feed_id"]);
150 $limit = (int)db_escape_string($_REQUEST["limit"]);
151 $filter = db_escape_string($_REQUEST["filter"]);
152 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
153 $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]);
155 /* do not rely on params below */
157 $search = db_escape_string($_REQUEST["search"]);
158 $search_mode = db_escape_string($_REQUEST["search_mode"]);
159 $match_on = db_escape_string($_REQUEST["match_on"]);
161 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
162 $view_mode, $is_cat, $search, $search_mode, $match_on);
164 $result = $qfh_ret[0];
165 $feed_title = $qfh_ret[1];
167 $headlines = array();
169 while ($line = db_fetch_assoc($result)) {
170 $is_updated = ($line["last_read"] == "" &&
171 ($line["unread"] != "t" && $line["unread"] != "1"));
173 $headline_row = array(
174 "id" => (int)$line["id"],
175 "unread" => sql_bool_to_bool($line["unread"]),
176 "marked" => sql_bool_to_bool($line["marked"]),
177 "updated" => strtotime($line["updated"]),
178 "is_updated" => $is_updated,
179 "title" => $line["title"],
180 "feed_id" => $line["feed_id"],
183 if ($show_except) $headline_row["excerpt"] = $line["content_preview"];
185 array_push($headlines, $headline_row);
188 print json_encode($headlines);
193 $article_id = (int)db_escape_string($_REQUEST["article_id"]);
195 $query = "SELECT title,link,content,feed_id,comments,int_id,
197 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
199 FROM ttrss_entries,ttrss_user_entries
200 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
203 $result = db_query($link, $query);
207 if (db_num_rows($result) != 0) {
208 $line = db_fetch_assoc($result);
211 "title" => $line["title"],
212 "link" => $line["link"],
213 "unread" => sql_bool_to_bool($line["unread"]),
214 "marked" => sql_bool_to_bool($line["marked"]),
215 "comments" => $line["comments"],
216 "author" => $line["author"],
217 "updated" => strtotime($line["updated"]),
218 "content" => $line["content"],
219 "feed_id" => $line["feed_id"],
223 print json_encode($article);