3 class API extends Handler {
12 function before($method) {
13 if (parent::before($method)) {
14 header("Content-Type: text/json");
16 if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") {
17 print $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN'));
21 if ($_SESSION["uid"] && $method != "logout" && !get_pref($this->link, 'ENABLE_API_ACCESS')) {
22 print $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED'));
26 $this->seq = (int) $_REQUEST['seq'];
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 "api_level" => self::API_LEVEL));
73 } else if (authenticate_user($this->link, $login, $password_base64)) { // else try with base64_decoded password
74 print $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
75 "api_level" => self::API_LEVEL));
76 } else { // else we are not logged in
77 print $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
80 print $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
87 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
90 function isLoggedIn() {
91 print $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != ''));
94 function getUnread() {
95 $feed_id = db_escape_string($_REQUEST["feed_id"]);
96 $is_cat = db_escape_string($_REQUEST["is_cat"]);
99 print $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($this->link, $feed_id, $is_cat)));
101 print $this->wrap(self::STATUS_OK, array("unread" => getGlobalUnread($this->link)));
105 /* Method added for ttrss-reader for Android */
106 function getCounters() {
107 print $this->wrap(self::STATUS_OK, getAllCounters($this->link));
110 function getFeeds() {
111 $cat_id = db_escape_string($_REQUEST["cat_id"]);
112 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
113 $limit = (int) db_escape_string($_REQUEST["limit"]);
114 $offset = (int) db_escape_string($_REQUEST["offset"]);
115 $include_nested = (bool)db_escape_string($_REQUEST["include_nested"]);
117 $feeds = $this->api_get_feeds($this->link, $cat_id, $unread_only, $limit, $offset, $include_nested);
119 print $this->wrap(self::STATUS_OK, $feeds);
122 function getCategories() {
123 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
124 $enable_nested = (bool)db_escape_string($_REQUEST["enable_nested"]);
126 // TODO do not return empty categories, return Uncategorized and standard virtual cats
129 $nested_qpart = "parent_cat IS NULL";
131 $nested_qpart = "true";
133 $result = db_query($this->link, "SELECT
134 id, title, order_id, (SELECT COUNT(id) FROM
136 ttrss_feed_categories.id IS NOT NULL AND cat_id = ttrss_feed_categories.id) AS num_feeds
137 FROM ttrss_feed_categories
138 WHERE $nested_qpart AND owner_uid = " .
143 while ($line = db_fetch_assoc($result)) {
144 if ($line["num_feeds"] > 0) {
145 $unread = getFeedUnread($this->link, $line["id"], true);
148 $unread += getCategoryChildrenUnread($this->link, $line["id"]);
150 if ($unread || !$unread_only) {
151 array_push($cats, array("id" => $line["id"],
152 "title" => $line["title"],
154 "order_id" => (int) $line["order_id"],
160 foreach (array(-2,-1,0) as $cat_id) {
161 $unread = getFeedUnread($this->link, $cat_id, true);
163 if ($unread || !$unread_only) {
164 array_push($cats, array("id" => $cat_id,
165 "title" => getCategoryTitle($this->link, $cat_id),
166 "unread" => $unread));
170 print $this->wrap(self::STATUS_OK, $cats);
173 function getHeadlines() {
174 $feed_id = db_escape_string($_REQUEST["feed_id"]);
175 if ($feed_id != "") {
177 $limit = (int)db_escape_string($_REQUEST["limit"]);
179 if (!$limit || $limit >= 60) $limit = 60;
181 $offset = (int)db_escape_string($_REQUEST["skip"]);
182 $filter = db_escape_string($_REQUEST["filter"]);
183 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
184 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
185 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
186 /* all_articles, unread, adaptive, marked, updated */
187 $view_mode = db_escape_string($_REQUEST["view_mode"]);
188 $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]);
189 $since_id = (int)db_escape_string($_REQUEST["since_id"]);
190 $include_nested = (bool)db_escape_string($_REQUEST["include_nested"]);
191 $sanitize_content = true;
193 /* do not rely on params below */
195 $search = db_escape_string($_REQUEST["search"]);
196 $search_mode = db_escape_string($_REQUEST["search_mode"]);
197 $match_on = db_escape_string($_REQUEST["match_on"]);
199 $headlines = $this->api_get_headlines($this->link, $feed_id, $limit, $offset,
200 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
201 $include_attachments, $since_id, $search, $search_mode, $match_on,
202 $include_nested, $sanitize_content);
204 print $this->wrap(self::STATUS_OK, $headlines);
206 print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
210 function updateArticle() {
211 $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric);
212 $mode = (int) db_escape_string($_REQUEST["mode"]);
213 $data = db_escape_string($_REQUEST["data"]);
214 $field_raw = (int)db_escape_string($_REQUEST["field"]);
219 switch ($field_raw) {
222 $additional_fields = ",last_marked = NOW()";
225 $field = "published";
226 $additional_fields = ",last_published = NOW()";
230 $additional_fields = ",last_read = NOW()";
244 $set_to = "NOT $field";
248 if ($field == "note") $set_to = "'$data'";
250 if ($field && $set_to && count($article_ids) > 0) {
252 $article_ids = join(", ", $article_ids);
254 $result = db_query($this->link, "UPDATE ttrss_user_entries SET $field = $set_to $additional_fields WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
256 $num_updated = db_affected_rows($this->link, $result);
258 if ($num_updated > 0 && $field == "unread") {
259 $result = db_query($this->link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
260 WHERE ref_id IN ($article_ids)");
262 while ($line = db_fetch_assoc($result)) {
263 ccache_update($this->link, $line["feed_id"], $_SESSION["uid"]);
267 if ($num_updated > 0 && $field == "published") {
268 if (PUBSUBHUBBUB_HUB) {
269 $rss_link = get_self_url_prefix() .
270 "/public.php?op=rss&id=-2&key=" .
271 get_feed_access_key($this->link, -2, false);
273 $p = new Publisher(PUBSUBHUBBUB_HUB);
274 $pubsub_result = $p->publish_update($rss_link);
278 print $this->wrap(self::STATUS_OK, array("status" => "OK",
279 "updated" => $num_updated));
282 print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
287 function getArticle() {
289 $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric));
291 $query = "SELECT id,title,link,content,cached_content,feed_id,comments,int_id,
292 marked,unread,published,
293 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
295 FROM ttrss_entries,ttrss_user_entries
296 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
299 $result = db_query($this->link, $query);
303 if (db_num_rows($result) != 0) {
305 while ($line = db_fetch_assoc($result)) {
307 $attachments = get_article_enclosures($this->link, $line['id']);
311 "title" => $line["title"],
312 "link" => $line["link"],
313 "labels" => get_article_labels($this->link, $line['id']),
314 "unread" => sql_bool_to_bool($line["unread"]),
315 "marked" => sql_bool_to_bool($line["marked"]),
316 "published" => sql_bool_to_bool($line["published"]),
317 "comments" => $line["comments"],
318 "author" => $line["author"],
319 "updated" => (int) strtotime($line["updated"]),
320 "content" => $line["cached_content"] != "" ? $line["cached_content"] : $line["content"],
321 "feed_id" => $line["feed_id"],
322 "attachments" => $attachments
325 array_push($articles, $article);
330 print $this->wrap(self::STATUS_OK, $articles);
334 function getConfig() {
336 "icons_dir" => ICONS_DIR,
337 "icons_url" => ICONS_URL);
339 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
341 $result = db_query($this->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 $this->wrap(self::STATUS_OK, $config);
351 function updateFeed() {
352 $feed_id = db_escape_string($_REQUEST["feed_id"]);
354 update_rss_feed($this->link, $feed_id, true);
356 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
359 function catchupFeed() {
360 $feed_id = db_escape_string($_REQUEST["feed_id"]);
361 $is_cat = db_escape_string($_REQUEST["is_cat"]);
363 catchup_feed($this->link, $feed_id, $is_cat);
365 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
369 $pref_name = db_escape_string($_REQUEST["pref_name"]);
371 print $this->wrap(self::STATUS_OK, array("value" => get_pref($this->link, $pref_name)));
374 function getLabels() {
375 //$article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric);
377 $article_id = (int)$_REQUEST['article_id'];
381 $result = db_query($this->link, "SELECT id, caption, fg_color, bg_color
383 WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption");
386 $article_labels = get_article_labels($this->link, $article_id);
388 $article_labels = array();
390 while ($line = db_fetch_assoc($result)) {
393 foreach ($article_labels as $al) {
394 if ($al[0] == $line['id']) {
400 array_push($rv, array(
401 "id" => (int)$line['id'],
402 "caption" => $line['caption'],
403 "fg_color" => $line['fg_color'],
404 "bg_color" => $line['bg_color'],
405 "checked" => $checked));
408 print $this->wrap(self::STATUS_OK, $rv);
411 function setArticleLabel() {
413 $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric);
414 $label_id = (int) db_escape_string($_REQUEST['label_id']);
415 $assign = (bool) db_escape_string($_REQUEST['assign']) == "true";
417 $label = db_escape_string(label_find_caption($this->link,
418 $label_id, $_SESSION["uid"]));
424 foreach ($article_ids as $id) {
427 label_add_article($this->link, $id, $label, $_SESSION["uid"]);
429 label_remove_article($this->link, $id, $label, $_SESSION["uid"]);
436 print $this->wrap(self::STATUS_OK, array("status" => "OK",
437 "updated" => $num_updated));
442 print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD'));
445 function shareToPublished() {
446 $title = db_escape_string(strip_tags($_REQUEST["title"]));
447 $url = db_escape_string(strip_tags($_REQUEST["url"]));
448 $content = db_escape_string(strip_tags($_REQUEST["content"]));
450 if (Article::create_published_article($this->link, $title, $url, $content, "", $_SESSION["uid"])) {
451 print $this->wrap(self::STATUS_OK, array("status" => 'OK'));
453 print $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
457 static function api_get_feeds($link, $cat_id, $unread_only, $limit, $offset, $include_nested = false) {
463 if ($cat_id == -4 || $cat_id == -2) {
464 $counters = getLabelCounters($link, true);
466 foreach (array_values($counters) as $cv) {
468 $unread = $cv["counter"];
470 if ($unread || !$unread_only) {
474 "title" => $cv["description"],
475 "unread" => $cv["counter"],
479 array_push($feeds, $row);
486 if ($cat_id == -4 || $cat_id == -1) {
487 foreach (array(-1, -2, -3, -4, -6, 0) as $i) {
488 $unread = getFeedUnread($link, $i);
490 if ($unread || !$unread_only) {
491 $title = getFeedTitle($link, $i);
499 array_push($feeds, $row);
507 if ($include_nested && $cat_id) {
508 $result = db_query($link, "SELECT
509 id, title FROM ttrss_feed_categories
510 WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
511 " ORDER BY id, title");
513 while ($line = db_fetch_assoc($result)) {
514 $unread = getFeedUnread($link, $line["id"], true) +
515 getCategoryChildrenUnread($link, $line["id"]);
517 if ($unread || !$unread_only) {
520 "title" => $line["title"],
524 array_push($feeds, $row);
532 $limit_qpart = "LIMIT $limit OFFSET $offset";
537 if ($cat_id == -4 || $cat_id == -3) {
538 $result = db_query($link, "SELECT
539 id, feed_url, cat_id, title, order_id, ".
540 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
541 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
542 " ORDER BY cat_id, title " . $limit_qpart);
546 $cat_qpart = "cat_id = '$cat_id'";
548 $cat_qpart = "cat_id IS NULL";
550 $result = db_query($link, "SELECT
551 id, feed_url, cat_id, title, order_id, ".
552 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
553 FROM ttrss_feeds WHERE
554 $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
555 " ORDER BY cat_id, title " . $limit_qpart);
558 while ($line = db_fetch_assoc($result)) {
560 $unread = getFeedUnread($link, $line["id"]);
562 $has_icon = feed_has_icon($line['id']);
564 if ($unread || !$unread_only) {
567 "feed_url" => $line["feed_url"],
568 "title" => $line["title"],
569 "id" => (int)$line["id"],
570 "unread" => (int)$unread,
571 "has_icon" => $has_icon,
572 "cat_id" => (int)$line["cat_id"],
573 "last_updated" => (int) strtotime($line["last_updated"]),
574 "order_id" => (int) $line["order_id"],
577 array_push($feeds, $row);
584 static function api_get_headlines($link, $feed_id, $limit, $offset,
585 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
586 $include_attachments, $since_id,
587 $search = "", $search_mode = "", $match_on = "",
588 $include_nested = false, $sanitize_content = true) {
590 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
591 $view_mode, $is_cat, $search, $search_mode, $match_on,
592 $order, $offset, 0, false, $since_id, $include_nested);
594 $result = $qfh_ret[0];
595 $feed_title = $qfh_ret[1];
597 $headlines = array();
599 while ($line = db_fetch_assoc($result)) {
600 $is_updated = ($line["last_read"] == "" &&
601 ($line["unread"] != "t" && $line["unread"] != "1"));
603 $tags = explode(",", $line["tag_cache"]);
604 $labels = json_decode($line["label_cache"], true);
606 //if (!$tags) $tags = get_article_tags($link, $line["id"]);
607 //if (!$labels) $labels = get_article_labels($link, $line["id"]);
609 $headline_row = array(
610 "id" => (int)$line["id"],
611 "unread" => sql_bool_to_bool($line["unread"]),
612 "marked" => sql_bool_to_bool($line["marked"]),
613 "published" => sql_bool_to_bool($line["published"]),
614 "updated" => (int) strtotime($line["updated"]),
615 "is_updated" => $is_updated,
616 "title" => $line["title"],
617 "link" => $line["link"],
618 "feed_id" => $line["feed_id"],
622 if ($include_attachments)
623 $headline_row['attachments'] = get_article_enclosures($link,
627 $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
628 $headline_row["excerpt"] = $excerpt;
633 if ($line["cached_content"] != "") {
634 $line["content_preview"] =& $line["cached_content"];
637 if ($sanitize_content) {
638 $headline_row["content"] = sanitize($link,
639 $line["content_preview"], false, false, $line["site_url"]);
641 $headline_row["content"] = $line["content_preview"];
645 // unify label output to ease parsing
646 if ($labels["no-labels"] == 1) $labels = array();
648 $headline_row["labels"] = $labels;
650 $headline_row["feed_title"] = $line["feed_title"];
652 $headline_row["comments_count"] = (int)$line["num_comments"];
653 $headline_row["comments_link"] = $line["comments"];
655 $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]);
657 array_push($headlines, $headline_row);