]>
git.wh0rd.org - tt-rss.git/blob - api/index.php
c9d134df12e6bcb25809c21dabf7c8615616c127
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)));
93 $cat_id = db_escape_string($_REQUEST["cat_id"]);
94 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
97 $result = db_query($link, "SELECT
98 id, feed_url, cat_id, title, ".
99 SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
100 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
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
106 cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
111 while ($line = db_fetch_assoc($result)) {
113 $unread = getFeedUnread($link, $line["id"]);
115 if ($unread ||
!$unread_only) {
118 "feed_url" => $line["feed_url"],
119 "title" => $line["title"],
120 "id" => (int)$line["id"],
121 "unread" => (int)$unread,
122 "cat_id" => (int)$line["cat_id"],
123 "last_updated" => strtotime($line["last_updated"])
126 array_push($feeds, $row);
132 if (!$cat_id ||
$cat_id == -2) {
133 $counters = getLabelCounters($link, false, true);
135 foreach (array_keys($counters) as $id) {
137 $unread = $counters[$id]["counter"];
139 if ($unread ||
!$unread_only) {
143 "title" => $counters[$id]["description"],
144 "unread" => $counters[$id]["counter"],
148 array_push($feeds, $row);
155 if (!$cat_id ||
$cat_id == -1) {
156 foreach (array(-1, -2, -3, -4) as $i) {
157 $unread = getFeedUnread($link, $i);
159 if ($unread ||
!$unread_only) {
160 $title = getFeedTitle($link, $i);
168 array_push($feeds, $row);
174 print json_encode($feeds);
177 case "getCategories":
178 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
180 $result = db_query($link, "SELECT
181 id, title FROM ttrss_feed_categories
182 WHERE owner_uid = " .
187 while ($line = db_fetch_assoc($result)) {
188 $unread = getFeedUnread($link, $line["id"], true);
190 if ($unread ||
!$unread_only) {
191 array_push($cats, array("id" => $line["id"],
192 "title" => $line["title"],
193 "unread" => $unread));
197 print json_encode($cats);
200 $feed_id = db_escape_string($_REQUEST["feed_id"]);
201 $limit = (int)db_escape_string($_REQUEST["limit"]);
202 $filter = db_escape_string($_REQUEST["filter"]);
203 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
204 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
205 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
207 /* do not rely on params below */
209 $search = db_escape_string($_REQUEST["search"]);
210 $search_mode = db_escape_string($_REQUEST["search_mode"]);
211 $match_on = db_escape_string($_REQUEST["match_on"]);
213 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
214 $view_mode, $is_cat, $search, $search_mode, $match_on);
216 $result = $qfh_ret[0];
217 $feed_title = $qfh_ret[1];
219 $headlines = array();
221 while ($line = db_fetch_assoc($result)) {
222 $is_updated = ($line["last_read"] == "" &&
223 ($line["unread"] != "t" && $line["unread"] != "1"));
225 $headline_row = array(
226 "id" => (int)$line["id"],
227 "unread" => sql_bool_to_bool($line["unread"]),
228 "marked" => sql_bool_to_bool($line["marked"]),
229 "updated" => strtotime($line["updated"]),
230 "is_updated" => $is_updated,
231 "title" => $line["title"],
232 "feed_id" => $line["feed_id"],
236 $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
237 $headline_row["excerpt"] = $excerpt;
241 $headline_row["content"] = $line["content_preview"];
244 array_push($headlines, $headline_row);
247 print json_encode($headlines);
250 case "updateArticle":
251 $article_id = (int) db_escape_string($_GET["article_id"]);
252 $mode = (int) db_escape_string($_REQUEST["mode"]);
253 $field_raw = (int)db_escape_string($_REQUEST["field"]);
258 switch ($field_raw) {
263 $field = "published";
278 $set_to = "NOT $field";
282 if ($field && $set_to) {
283 if ($field == "unread") {
284 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
286 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
288 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
289 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
297 $article_id = (int)db_escape_string($_REQUEST["article_id"]);
299 $query = "SELECT title,link,content,feed_id,comments,int_id,
300 marked,unread,published,
301 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
303 FROM ttrss_entries,ttrss_user_entries
304 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
307 $result = db_query($link, $query);
311 if (db_num_rows($result) != 0) {
312 $line = db_fetch_assoc($result);
315 "title" => $line["title"],
316 "link" => $line["link"],
317 "labels" => get_article_labels($link, $article_id),
318 "unread" => sql_bool_to_bool($line["unread"]),
319 "marked" => sql_bool_to_bool($line["marked"]),
320 "published" => sql_bool_to_bool($line["published"]),
321 "comments" => $line["comments"],
322 "author" => $line["author"],
323 "updated" => strtotime($line["updated"]),
324 "content" => $line["content"],
325 "feed_id" => $line["feed_id"],
329 print json_encode($article);
334 "icons_dir" => ICONS_DIR
,
335 "icons_url" => ICONS_URL
);
337 if (ENABLE_UPDATE_DAEMON
) {
338 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
341 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
342 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
344 $num_feeds = db_fetch_result($result, 0, "cf");
346 $config["num_feeds"] = (int)$num_feeds;
348 print json_encode($config);
353 $feed_id = db_escape_string($_REQUEST["feed_id"]);
355 $result = db_query($link,
356 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
357 AND owner_uid = " . $_SESSION["uid"]);
359 if (db_num_rows($result) > 0) {
360 $feed_url = db_fetch_result($result, 0, "feed_url");
361 update_rss_feed($link, $feed_url, $feed_id);
364 print json_encode(array("status" => "OK"));
369 $pref_name = db_escape_string($_REQUEST["pref_name"]);
370 print json_encode(array("value" => get_pref($link, $pref_name)));
374 print json_encode(array("error" => 'UNKNOWN_METHOD'));