]>
git.wh0rd.org - tt-rss.git/blob - classes/api.php
1a79ffe2d6c827aa8be1ee2ea7edc88febe48179
3 class API
extends Handler
{
12 function before($method) {
13 if (parent
::before($method)) {
15 if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") {
16 print $this->wrap(self
::STATUS_ERR
, array("error" => 'NOT_LOGGED_IN'));
20 if ($_SESSION["uid"] && $method != "logout" && !get_pref($this->link
, 'ENABLE_API_ACCESS')) {
21 print $this->wrap(self
::STATUS_ERR
, array("error" => 'API_DISABLED'));
25 $this->seq
= (int) $_REQUEST['seq'];
27 header("Content-Type: text/plain");
33 function wrap($status, $reply) {
34 print json_encode(array("seq" => $this->seq
,
36 "content" => $reply));
39 function getVersion() {
40 $rv = array("version" => VERSION
);
41 print $this->wrap(self
::STATUS_OK
, $rv);
44 function getApiLevel() {
45 $rv = array("level" => self
::API_LEVEL
);
46 print $this->wrap(self
::STATUS_OK
, $rv);
50 $login = db_escape_string($_REQUEST["user"]);
51 $password = $_REQUEST["password"];
52 $password_base64 = base64_decode($_REQUEST["password"]);
54 if (SINGLE_USER_MODE
) $login = "admin";
56 $result = db_query($this->link
, "SELECT id FROM ttrss_users WHERE login = '$login'");
58 if (db_num_rows($result) != 0) {
59 $uid = db_fetch_result($result, 0, "id");
65 print $this->wrap(self
::STATUS_ERR
, array("error" => "LOGIN_ERROR"));
69 if (get_pref($this->link
, "ENABLE_API_ACCESS", $uid)) {
70 if (authenticate_user($this->link
, $login, $password)) { // try login with normal password
71 print $this->wrap(self
::STATUS_OK
, array("session_id" => session_id()));
72 } else if (authenticate_user($this->link
, $login, $password_base64)) { // else try with base64_decoded password
73 print $this->wrap(self
::STATUS_OK
, array("session_id" => session_id()));
74 } else { // else we are not logged in
75 print $this->wrap(self
::STATUS_ERR
, array("error" => "LOGIN_ERROR"));
78 print $this->wrap(self
::STATUS_ERR
, array("error" => "API_DISABLED"));
85 print $this->wrap(self
::STATUS_OK
, array("status" => "OK"));
88 function isLoggedIn() {
89 print $this->wrap(self
::STATUS_OK
, array("status" => $_SESSION["uid"] != ''));
92 function getUnread() {
93 $feed_id = db_escape_string($_REQUEST["feed_id"]);
94 $is_cat = db_escape_string($_REQUEST["is_cat"]);
97 print $this->wrap(self
::STATUS_OK
, array("unread" => getFeedUnread($this->link
, $feed_id, $is_cat)));
99 print $this->wrap(self
::STATUS_OK
, array("unread" => getGlobalUnread($this->link
)));
103 /* Method added for ttrss-reader for Android */
104 function getCounters() {
106 /* flct (flc is the default) FIXME: document */
107 $output_mode = db_escape_string($_REQUEST["output_mode"]);
109 print $this->wrap(self
::STATUS_OK
, getAllCounters($this->link
, $output_mode));
112 function getFeeds() {
113 $cat_id = db_escape_string($_REQUEST["cat_id"]);
114 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
115 $limit = (int) db_escape_string($_REQUEST["limit"]);
116 $offset = (int) db_escape_string($_REQUEST["offset"]);
118 $feeds = api_get_feeds($this->link
, $cat_id, $unread_only, $limit, $offset);
120 print $this->wrap(self
::STATUS_OK
, $feeds);
123 function getCategories() {
124 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
126 // TODO do not return empty categories, return Uncategorized and standard virtual cats
128 $result = db_query($this->link
, "SELECT
129 id, title FROM ttrss_feed_categories
130 WHERE owner_uid = " .
135 while ($line = db_fetch_assoc($result)) {
136 $unread = getFeedUnread($this->link
, $line["id"], true);
138 if ($unread ||
!$unread_only) {
139 array_push($cats, array("id" => $line["id"],
140 "title" => $line["title"],
141 "unread" => $unread));
145 foreach (array(-2,-1,0) as $cat_id) {
146 $unread = getFeedUnread($this->link
, $cat_id, true);
148 if ($unread ||
!$unread_only) {
149 array_push($cats, array("id" => $cat_id,
150 "title" => getCategoryTitle($this->link
, $cat_id),
151 "unread" => $unread));
155 print $this->wrap(self
::STATUS_OK
, $cats);
158 function getHeadlines() {
159 $feed_id = db_escape_string($_REQUEST["feed_id"]);
160 if ($feed_id != "") {
162 $limit = (int)db_escape_string($_REQUEST["limit"]);
164 if (!$limit ||
$limit >= 60) $limit = 60;
166 $offset = (int)db_escape_string($_REQUEST["skip"]);
167 $filter = db_escape_string($_REQUEST["filter"]);
168 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
169 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
170 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
171 /* all_articles, unread, adaptive, marked, updated */
172 $view_mode = db_escape_string($_REQUEST["view_mode"]);
173 $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]);
174 $since_id = (int)db_escape_string($_REQUEST["since_id"]);
176 /* do not rely on params below */
178 $search = db_escape_string($_REQUEST["search"]);
179 $search_mode = db_escape_string($_REQUEST["search_mode"]);
180 $match_on = db_escape_string($_REQUEST["match_on"]);
182 $headlines = api_get_headlines($this->link
, $feed_id, $limit, $offset,
183 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
184 $include_attachments, $since_id, $search, $search_mode, $match_on);
186 print $this->wrap(self
::STATUS_OK
, $headlines);
188 print $this->wrap(self
::STATUS_ERR
, array("error" => 'INCORRECT_USAGE'));
192 function updateArticle() {
193 $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric
);
194 $mode = (int) db_escape_string($_REQUEST["mode"]);
195 $data = db_escape_string($_REQUEST["data"]);
196 $field_raw = (int)db_escape_string($_REQUEST["field"]);
201 switch ($field_raw) {
206 $field = "published";
223 $set_to = "NOT $field";
227 if ($field == "note") $set_to = "'$data'";
229 if ($field && $set_to && count($article_ids) > 0) {
231 $article_ids = join(", ", $article_ids);
233 if ($field == "unread") {
234 $result = db_query($this->link
, "UPDATE ttrss_user_entries SET $field = $set_to,
236 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
238 $result = db_query($this->link
, "UPDATE ttrss_user_entries SET $field = $set_to
239 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
242 $num_updated = db_affected_rows($this->link
, $result);
244 if ($num_updated > 0 && $field == "unread") {
245 $result = db_query($this->link
, "SELECT DISTINCT feed_id FROM ttrss_user_entries
246 WHERE ref_id IN ($article_ids)");
248 while ($line = db_fetch_assoc($result)) {
249 ccache_update($this->link
, $line["feed_id"], $_SESSION["uid"]);
253 print $this->wrap(self
::STATUS_OK
, array("status" => "OK",
254 "updated" => $num_updated));
257 print $this->wrap(self
::STATUS_ERR
, array("error" => 'INCORRECT_USAGE'));
262 function getArticle() {
264 $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric
));
266 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
267 marked,unread,published,
268 ".SUBSTRING_FOR_DATE
."(updated,1,16) as updated,
270 FROM ttrss_entries,ttrss_user_entries
271 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
274 $result = db_query($this->link
, $query);
278 if (db_num_rows($result) != 0) {
280 while ($line = db_fetch_assoc($result)) {
282 $attachments = get_article_enclosures($this->link
, $line['id']);
286 "title" => $line["title"],
287 "link" => $line["link"],
288 "labels" => get_article_labels($this->link
, $line['id']),
289 "unread" => sql_bool_to_bool($line["unread"]),
290 "marked" => sql_bool_to_bool($line["marked"]),
291 "published" => sql_bool_to_bool($line["published"]),
292 "comments" => $line["comments"],
293 "author" => $line["author"],
294 "updated" => strtotime($line["updated"]),
295 "content" => $line["content"],
296 "feed_id" => $line["feed_id"],
297 "attachments" => $attachments
300 array_push($articles, $article);
305 print $this->wrap(self
::STATUS_OK
, $articles);
309 function getConfig() {
311 "icons_dir" => ICONS_DIR
,
312 "icons_url" => ICONS_URL
);
314 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
316 $result = db_query($this->link
, "SELECT COUNT(*) AS cf FROM
317 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
319 $num_feeds = db_fetch_result($result, 0, "cf");
321 $config["num_feeds"] = (int)$num_feeds;
323 print $this->wrap(self
::STATUS_OK
, $config);
326 function updateFeed() {
327 $feed_id = db_escape_string($_REQUEST["feed_id"]);
329 update_rss_feed($this->link
, $feed_id, true);
331 print $this->wrap(self
::STATUS_OK
, array("status" => "OK"));
334 function catchupFeed() {
335 $feed_id = db_escape_string($_REQUEST["feed_id"]);
336 $is_cat = db_escape_string($_REQUEST["is_cat"]);
338 catchup_feed($this->link
, $feed_id, $is_cat);
340 print $this->wrap(self
::STATUS_OK
, array("status" => "OK"));
344 $pref_name = db_escape_string($_REQUEST["pref_name"]);
346 print $this->wrap(self
::STATUS_OK
, array("value" => get_pref($this->link
, $pref_name)));
349 function getLabels() {
350 //$article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric);
352 $article_id = (int)$_REQUEST['article_id'];
356 $result = db_query($this->link
, "SELECT id, caption, fg_color, bg_color
358 WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption");
361 $article_labels = get_article_labels($this->link
, $article_id);
363 $article_labels = array();
365 while ($line = db_fetch_assoc($result)) {
368 foreach ($article_labels as $al) {
369 if ($al[0] == $line['id']) {
375 array_push($rv, array(
376 "id" => (int)$line['id'],
377 "caption" => $line['caption'],
378 "fg_color" => $line['fg_color'],
379 "bg_color" => $line['bg_color'],
380 "checked" => $checked));
383 print $this->wrap(self
::STATUS_OK
, $rv);
386 function setArticleLabel() {
388 $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric
);
389 $label_id = (int) db_escape_string($_REQUEST['label_id']);
390 $assign = (bool) db_escape_string($_REQUEST['assign']) == "true";
392 $label = db_escape_string(label_find_caption($this->link
,
393 $label_id, $_SESSION["uid"]));
399 foreach ($article_ids as $id) {
402 label_add_article($this->link
, $id, $label, $_SESSION["uid"]);
404 label_remove_article($this->link
, $id, $label, $_SESSION["uid"]);
411 print $this->wrap(self
::STATUS_OK
, array("status" => "OK",
412 "updated" => $num_updated));
417 print $this->wrap(self
::STATUS_ERR
, array("error" => 'UNKNOWN_METHOD'));