]>
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 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
12 $session_expire = SESSION_EXPIRE_TIME
; //seconds
13 $session_name = (!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid_api" : TTRSS_SESSION_NAME
. "_api";
18 if (DB_TYPE
== "mysql") {
21 // PG seems to display its own errors just fine by default.
25 init_connection($link);
27 $op = db_escape_string($_REQUEST["op"]);
29 // header("Content-Type: application/json");
31 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
32 print json_encode(array("error" => 'NOT_LOGGED_IN'));
36 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
37 print json_encode(array("error" => 'API_DISABLED'));
43 $rv = array("version" => VERSION
);
44 print json_encode($rv);
47 $login = db_escape_string($_REQUEST["user"]);
48 $password = db_escape_string($_REQUEST["password"]);
50 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
52 if (db_num_rows($result) != 0) {
53 $uid = db_fetch_result($result, 0, "id");
58 if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
59 if (authenticate_user($link, $login, $password)) {
60 print json_encode(array("uid" => $_SESSION["uid"]));
62 print json_encode(array("error" => "LOGIN_ERROR"));
65 print json_encode(array("error" => "API_DISABLED"));
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) {
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, $row);
126 if (!$cat_id ||
$cat_id == -2) {
127 $counters = getLabelCounters($link, false, true);
129 foreach (array_keys($counters) as $id) {
131 $unread = $counters[$id]["counter"];
133 if ($unread ||
!$unread_only) {
137 "title" => $counters[$id]["description"],
138 "unread" => $counters[$id]["counter"],
142 array_push($feeds, $row);
149 if (!$cat_id ||
$cat_id == -1) {
150 foreach (array(-1, -2, -3, -4) as $i) {
151 $unread = getFeedUnread($link, $i);
153 if ($unread ||
!$unread_only) {
154 $title = getFeedTitle($link, $i);
162 array_push($feeds, $row);
168 print json_encode($feeds);
171 case "getCategories":
172 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
174 $result = db_query($link, "SELECT
175 id, title FROM ttrss_feed_categories
176 WHERE owner_uid = " .
181 while ($line = db_fetch_assoc($result)) {
182 $unread = getFeedUnread($link, $line["id"], true);
184 if ($unread ||
!$unread_only) {
185 array_push($cats, array("id" => $line["id"],
186 "title" => $line["title"],
187 "unread" => $unread));
191 print json_encode($cats);
194 $feed_id = db_escape_string($_REQUEST["feed_id"]);
195 $limit = (int)db_escape_string($_REQUEST["limit"]);
196 $filter = db_escape_string($_REQUEST["filter"]);
197 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
198 $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]);
200 /* do not rely on params below */
202 $search = db_escape_string($_REQUEST["search"]);
203 $search_mode = db_escape_string($_REQUEST["search_mode"]);
204 $match_on = db_escape_string($_REQUEST["match_on"]);
206 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
207 $view_mode, $is_cat, $search, $search_mode, $match_on);
209 $result = $qfh_ret[0];
210 $feed_title = $qfh_ret[1];
212 $headlines = array();
214 while ($line = db_fetch_assoc($result)) {
215 $is_updated = ($line["last_read"] == "" &&
216 ($line["unread"] != "t" && $line["unread"] != "1"));
218 $headline_row = array(
219 "id" => (int)$line["id"],
220 "unread" => sql_bool_to_bool($line["unread"]),
221 "marked" => sql_bool_to_bool($line["marked"]),
222 "updated" => strtotime($line["updated"]),
223 "is_updated" => $is_updated,
224 "title" => $line["title"],
225 "feed_id" => $line["feed_id"],
228 if ($show_except) $headline_row["excerpt"] = $line["content_preview"];
230 array_push($headlines, $headline_row);
233 print json_encode($headlines);
236 case "updateArticle":
237 $article_id = (int) db_escape_string($_GET["article_id"]);
238 $mode = (int) db_escape_string($_REQUEST["mode"]);
239 $field_raw = (int)db_escape_string($_REQUEST["field"]);
244 switch ($field_raw) {
249 $field = "published";
264 $set_to = "NOT $field";
268 if ($field && $set_to) {
269 if ($field == "unread") {
270 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
272 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
274 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
275 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
283 $article_id = (int)db_escape_string($_REQUEST["article_id"]);
285 $query = "SELECT title,link,content,feed_id,comments,int_id,
286 marked,unread,published,
287 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
289 FROM ttrss_entries,ttrss_user_entries
290 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
293 $result = db_query($link, $query);
297 if (db_num_rows($result) != 0) {
298 $line = db_fetch_assoc($result);
301 "title" => $line["title"],
302 "link" => $line["link"],
303 "labels" => get_article_labels($link, $article_id),
304 "unread" => sql_bool_to_bool($line["unread"]),
305 "marked" => sql_bool_to_bool($line["marked"]),
306 "published" => sql_bool_to_bool($line["published"]),
307 "comments" => $line["comments"],
308 "author" => $line["author"],
309 "updated" => strtotime($line["updated"]),
310 "content" => $line["content"],
311 "feed_id" => $line["feed_id"],
315 print json_encode($article);
320 "icons_dir" => ICONS_DIR
,
321 "icons_url" => ICONS_URL
);
323 if (ENABLE_UPDATE_DAEMON
) {
324 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
327 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
328 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
330 $num_feeds = db_fetch_result($result, 0, "cf");
332 $config["num_feeds"] = (int)$num_feeds;
334 print json_encode($config);
339 $pref_name = db_escape_string($_REQUEST["pref_name"]);
340 print json_encode(array("value" => get_pref($link, $pref_name)));