]>
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";
15 session_name($session_name);
17 if ($_REQUEST["sid"]) {
18 session_id($_REQUEST["sid"]);
24 if (DB_TYPE
== "mysql") {
27 // PG seems to display its own errors just fine by default.
31 init_connection($link);
33 $op = db_escape_string($_REQUEST["op"]);
35 // header("Content-Type: application/json");
37 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
38 print json_encode(array("error" => 'NOT_LOGGED_IN'));
42 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
43 print json_encode(array("error" => 'API_DISABLED'));
49 $rv = array("version" => VERSION
);
50 print json_encode($rv);
53 $login = db_escape_string($_REQUEST["user"]);
54 $password = db_escape_string($_REQUEST["password"]);
56 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
58 if (db_num_rows($result) != 0) {
59 $uid = db_fetch_result($result, 0, "id");
64 if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
65 if (authenticate_user($link, $login, $password)) {
66 print json_encode(array("session_id" => session_id()));
68 print json_encode(array("error" => "LOGIN_ERROR"));
71 print json_encode(array("error" => "API_DISABLED"));
77 print json_encode(array("status" => "OK"));
80 print json_encode(array("status" => $_SESSION["uid"] != ''));
83 $feed_id = db_escape_string($_REQUEST["feed_id"]);
84 $is_cat = db_escape_string($_REQUEST["is_cat"]);
87 print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
89 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"]);
100 $limit = (int) db_escape_string($_REQUEST["limit"]);
101 $offset = (int) db_escape_string($_REQUEST["offset"]);
104 $limit_qpart = "LIMIT $limit OFFSET $offset";
110 $result = db_query($link, "SELECT
111 id, feed_url, cat_id, title, ".
112 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
113 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
114 "ORDER BY cat_id, title " . $limit_qpart);
116 $result = db_query($link, "SELECT
117 id, feed_url, cat_id, title, ".
118 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
119 FROM ttrss_feeds WHERE
120 cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
121 "ORDER BY cat_id, title " . $limit_qpart);
126 while ($line = db_fetch_assoc($result)) {
128 $unread = getFeedUnread($link, $line["id"]);
130 $icon_path = "../" . ICONS_DIR
. "/" . $line["id"] . ".ico";
131 $has_icon = file_exists($icon_path) && filesize($icon_path) > 0;
133 if ($unread ||
!$unread_only) {
136 "feed_url" => $line["feed_url"],
137 "title" => $line["title"],
138 "id" => (int)$line["id"],
139 "unread" => (int)$unread,
140 "has_icon" => $has_icon,
141 "cat_id" => (int)$line["cat_id"],
142 "last_updated" => strtotime($line["last_updated"])
145 array_push($feeds, $row);
151 if (!$cat_id ||
$cat_id == -2) {
152 $counters = getLabelCounters($link, true);
154 foreach (array_keys($counters) as $id) {
156 $unread = $counters[$id]["counter"];
158 if ($unread ||
!$unread_only) {
162 "title" => $counters[$id]["description"],
163 "unread" => $counters[$id]["counter"],
167 array_push($feeds, $row);
174 if (!$cat_id ||
$cat_id == -1) {
175 foreach (array(-1, -2, -3, -4, 0) as $i) {
176 $unread = getFeedUnread($link, $i);
178 if ($unread ||
!$unread_only) {
179 $title = getFeedTitle($link, $i);
187 array_push($feeds, $row);
193 print json_encode($feeds);
196 case "getCategories":
197 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
199 $result = db_query($link, "SELECT
200 id, title FROM ttrss_feed_categories
201 WHERE owner_uid = " .
206 while ($line = db_fetch_assoc($result)) {
207 $unread = getFeedUnread($link, $line["id"], true);
209 if ($unread ||
!$unread_only) {
210 array_push($cats, array("id" => $line["id"],
211 "title" => $line["title"],
212 "unread" => $unread));
216 print json_encode($cats);
219 $feed_id = db_escape_string($_REQUEST["feed_id"]);
220 $limit = (int)db_escape_string($_REQUEST["limit"]);
221 $offset = (int)db_escape_string($_REQUEST["skip"]);
222 $filter = db_escape_string($_REQUEST["filter"]);
223 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
224 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
225 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
226 /* all_articles, unread, adaptive, marked, updated */
227 $view_mode = db_escape_string($_REQUEST["view_mode"]);
229 /* do not rely on params below */
231 $search = db_escape_string($_REQUEST["search"]);
232 $search_mode = db_escape_string($_REQUEST["search_mode"]);
233 $match_on = db_escape_string($_REQUEST["match_on"]);
235 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
236 $view_mode, $is_cat, $search, $search_mode, $match_on,
239 $result = $qfh_ret[0];
240 $feed_title = $qfh_ret[1];
242 $headlines = array();
244 while ($line = db_fetch_assoc($result)) {
245 $is_updated = ($line["last_read"] == "" &&
246 ($line["unread"] != "t" && $line["unread"] != "1"));
248 $headline_row = array(
249 "id" => (int)$line["id"],
250 "unread" => sql_bool_to_bool($line["unread"]),
251 "marked" => sql_bool_to_bool($line["marked"]),
252 "updated" => strtotime($line["updated"]),
253 "is_updated" => $is_updated,
254 "title" => $line["title"],
255 "link" => $line["link"],
256 "feed_id" => $line["feed_id"],
260 $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
261 $headline_row["excerpt"] = $excerpt;
265 $headline_row["content"] = $line["content_preview"];
268 array_push($headlines, $headline_row);
271 print json_encode($headlines);
274 case "updateArticle":
275 $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
276 $mode = (int) db_escape_string($_REQUEST["mode"]);
277 $field_raw = (int)db_escape_string($_REQUEST["field"]);
282 switch ($field_raw) {
287 $field = "published";
302 $set_to = "NOT $field";
306 if ($field && $set_to && count($article_ids) > 0) {
308 $article_ids = join(", ", $article_ids);
310 if ($field == "unread") {
311 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
313 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
315 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
316 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
324 $article_id = (int)db_escape_string($_REQUEST["article_id"]);
326 $query = "SELECT title,link,content,feed_id,comments,int_id,
327 marked,unread,published,
328 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
330 FROM ttrss_entries,ttrss_user_entries
331 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
334 $result = db_query($link, $query);
338 if (db_num_rows($result) != 0) {
339 $line = db_fetch_assoc($result);
342 "title" => $line["title"],
343 "link" => $line["link"],
344 "labels" => get_article_labels($link, $article_id),
345 "unread" => sql_bool_to_bool($line["unread"]),
346 "marked" => sql_bool_to_bool($line["marked"]),
347 "published" => sql_bool_to_bool($line["published"]),
348 "comments" => $line["comments"],
349 "author" => $line["author"],
350 "updated" => strtotime($line["updated"]),
351 "content" => $line["content"],
352 "feed_id" => $line["feed_id"],
356 print json_encode($article);
361 "icons_dir" => ICONS_DIR
,
362 "icons_url" => ICONS_URL
);
364 if (ENABLE_UPDATE_DAEMON
) {
365 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
368 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
369 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
371 $num_feeds = db_fetch_result($result, 0, "cf");
373 $config["num_feeds"] = (int)$num_feeds;
375 print json_encode($config);
380 $feed_id = db_escape_string($_REQUEST["feed_id"]);
382 update_rss_feed($link, $feed_id, true);
384 print json_encode(array("status" => "OK"));
389 $pref_name = db_escape_string($_REQUEST["pref_name"]);
390 print json_encode(array("value" => get_pref($link, $pref_name)));
394 print json_encode(array("error" => 'UNKNOWN_METHOD'));