]>
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 define('API_LEVEL', 1);
12 define('API_STATUS_OK', 0);
13 define('API_STATUS_ERR', 1);
17 if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT
) {
18 ob_start("ob_gzhandler");
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";
26 session_name($session_name);
28 $input = file_get_contents("php://input");
30 // Override $_REQUEST with JSON-encoded data if available
32 $input = json_decode($input, true);
39 if ($_REQUEST["sid"]) {
40 session_id($_REQUEST["sid"]);
46 if (DB_TYPE
== "mysql") {
49 // PG seems to display its own errors just fine by default.
53 init_connection($link);
55 $op = db_escape_string($_REQUEST["op"]);
56 $seq = (int) $_REQUEST["seq"];
58 header("Content-Type: text/plain");
60 function api_wrap_reply($status, $seq, $reply) {
61 print json_encode(array("seq" => $seq,
63 "content" => $reply));
66 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
67 print api_wrap_reply(API_STATUS_ERR
, $seq, array("error" => 'NOT_LOGGED_IN'));
71 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
72 print api_wrap_reply(API_STATUS_ERR
, $seq, array("error" => 'API_DISABLED'));
79 $rv = array("version" => VERSION
);
80 print api_wrap_reply(API_STATUS_OK
, $seq, $rv);
84 $rv = array("level" => API_LEVEL
);
85 print api_wrap_reply(API_STATUS_OK
, $seq, $rv);
89 $login = db_escape_string($_REQUEST["user"]);
90 $password = $_REQUEST["password"];
91 $password_base64 = base64_decode($_REQUEST["password"]);
93 if (SINGLE_USER_MODE
) $login = "admin";
95 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
97 if (db_num_rows($result) != 0) {
98 $uid = db_fetch_result($result, 0, "id");
104 print api_wrap_reply(API_STATUS_ERR
, $seq,
105 array("error" => "LOGIN_ERROR"));
109 if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
110 if (authenticate_user($link, $login, $password)) { // try login with normal password
111 print api_wrap_reply(API_STATUS_OK
, $seq,
112 array("session_id" => session_id()));
113 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
114 print api_wrap_reply(API_STATUS_OK
, $seq,
115 array("session_id" => session_id()));
116 } else { // else we are not logged in
117 print api_wrap_reply(API_STATUS_ERR
, $seq,
118 array("error" => "LOGIN_ERROR"));
121 print api_wrap_reply(API_STATUS_ERR
, $seq,
122 array("error" => "API_DISABLED"));
129 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
133 print api_wrap_reply(API_STATUS_OK
, $seq,
134 array("status" => $_SESSION["uid"] != ''));
138 $feed_id = db_escape_string($_REQUEST["feed_id"]);
139 $is_cat = db_escape_string($_REQUEST["is_cat"]);
142 print api_wrap_reply(API_STATUS_OK
, $seq,
143 array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
145 print api_wrap_reply(API_STATUS_OK
, $seq,
146 array("unread" => getGlobalUnread($link)));
150 /* Method added for ttrss-reader for Android */
153 /* flct (flc is the default) FIXME: document */
154 $output_mode = db_escape_string($_REQUEST["output_mode"]);
156 print api_wrap_reply(API_STATUS_OK
, $seq,
157 getAllCounters($link, $output_mode));
161 $cat_id = db_escape_string($_REQUEST["cat_id"]);
162 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
163 $limit = (int) db_escape_string($_REQUEST["limit"]);
164 $offset = (int) db_escape_string($_REQUEST["offset"]);
166 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
168 print api_wrap_reply(API_STATUS_OK
, $seq, $feeds);
172 case "getCategories":
173 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
175 $result = db_query($link, "SELECT
176 id, title FROM ttrss_feed_categories
177 WHERE owner_uid = " .
182 while ($line = db_fetch_assoc($result)) {
183 $unread = getFeedUnread($link, $line["id"], true);
185 if ($unread ||
!$unread_only) {
186 array_push($cats, array("id" => $line["id"],
187 "title" => $line["title"],
188 "unread" => $unread));
192 print api_wrap_reply(API_STATUS_OK
, $seq, $cats);
196 $feed_id = db_escape_string($_REQUEST["feed_id"]);
197 $limit = (int)db_escape_string($_REQUEST["limit"]);
198 $offset = (int)db_escape_string($_REQUEST["skip"]);
199 $filter = db_escape_string($_REQUEST["filter"]);
200 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
201 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
202 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
203 /* all_articles, unread, adaptive, marked, updated */
204 $view_mode = db_escape_string($_REQUEST["view_mode"]);
205 $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]);
206 $since_id = (int)db_escape_string($_REQUEST["since_id"]);
208 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
209 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
210 $include_attachments, $since_id);
212 print api_wrap_reply(API_STATUS_OK
, $seq, $headlines);
216 case "updateArticle":
217 $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric
);
218 $mode = (int) db_escape_string($_REQUEST["mode"]);
219 $field_raw = (int)db_escape_string($_REQUEST["field"]);
224 switch ($field_raw) {
229 $field = "published";
244 $set_to = "NOT $field";
248 if ($field && $set_to && count($article_ids) > 0) {
250 $article_ids = join(", ", $article_ids);
252 if ($field == "unread") {
253 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
255 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
257 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
258 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
261 $num_updated = db_affected_rows($link, $result);
263 if ($num_updated > 0 && $field == "unread") {
264 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
265 WHERE ref_id IN ($article_ids)");
267 while ($line = db_fetch_assoc($result)) {
268 ccache_update($link, $line["feed_id"], $_SESSION["uid"]);
272 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK",
273 "updated" => $num_updated));
276 print api_wrap_reply(API_STATUS_ERR
, $seq,
277 array("error" => 'INCORRECT_USAGE'));
284 $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric
));
286 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
287 marked,unread,published,
288 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
290 FROM ttrss_entries,ttrss_user_entries
291 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
294 $result = db_query($link, $query);
298 if (db_num_rows($result) != 0) {
300 while ($line = db_fetch_assoc($result)) {
302 $attachments = get_article_enclosures($link, $line['id']);
306 "title" => $line["title"],
307 "link" => $line["link"],
308 "labels" => get_article_labels($link, $line['id']),
309 "unread" => sql_bool_to_bool($line["unread"]),
310 "marked" => sql_bool_to_bool($line["marked"]),
311 "published" => sql_bool_to_bool($line["published"]),
312 "comments" => $line["comments"],
313 "author" => $line["author"],
314 "updated" => strtotime($line["updated"]),
315 "content" => $line["content"],
316 "feed_id" => $line["feed_id"],
317 "attachments" => $attachments
320 array_push($articles, $article);
325 print api_wrap_reply(API_STATUS_OK
, $seq, $articles);
331 "icons_dir" => ICONS_DIR
,
332 "icons_url" => ICONS_URL
);
334 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
336 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
337 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
339 $num_feeds = db_fetch_result($result, 0, "cf");
341 $config["num_feeds"] = (int)$num_feeds;
343 print api_wrap_reply(API_STATUS_OK
, $seq, $config);
348 $feed_id = db_escape_string($_REQUEST["feed_id"]);
350 update_rss_feed($link, $feed_id, true);
352 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
357 $feed_id = db_escape_string($_REQUEST["feed_id"]);
358 $is_cat = db_escape_string($_REQUEST["is_cat"]);
360 catchup_feed($link, $feed_id, $is_cat);
362 print api_wrap_reply(API_STATUS_OK
, $seq, array("status" => "OK"));
367 $pref_name = db_escape_string($_REQUEST["pref_name"]);
369 print api_wrap_reply(API_STATUS_OK
, $seq,
370 array("value" => get_pref($link, $pref_name)));
374 print api_wrap_reply(API_STATUS_ERR
, $seq,
375 array("error" => 'UNKNOWN_METHOD'));