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($this->link, $_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($this->link, $_REQUEST["feed_id"]);
96 $is_cat = db_escape_string($this->link, $_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($this->link, $_REQUEST["cat_id"]);
112 $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
113 $limit = (int) db_escape_string($this->link, $_REQUEST["limit"]);
114 $offset = (int) db_escape_string($this->link, $_REQUEST["offset"]);
115 $include_nested = sql_bool_to_bool($_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 = sql_bool_to_bool($_REQUEST["unread_only"]);
124 $enable_nested = sql_bool_to_bool($_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 (SELECT COUNT(id) FROM
138 ttrss_feed_categories AS c2 WHERE
139 c2.parent_cat = ttrss_feed_categories.id) AS num_cats
140 FROM ttrss_feed_categories
141 WHERE $nested_qpart AND owner_uid = " .
146 while ($line = db_fetch_assoc($result)) {
147 if ($line["num_feeds"] > 0 || $line["num_cats"] > 0) {
148 $unread = getFeedUnread($this->link, $line["id"], true);
151 $unread += getCategoryChildrenUnread($this->link, $line["id"]);
153 if ($unread || !$unread_only) {
154 array_push($cats, array("id" => $line["id"],
155 "title" => $line["title"],
157 "order_id" => (int) $line["order_id"],
163 foreach (array(-2,-1,0) as $cat_id) {
164 $unread = getFeedUnread($this->link, $cat_id, true);
166 if ($unread || !$unread_only) {
167 array_push($cats, array("id" => $cat_id,
168 "title" => getCategoryTitle($this->link, $cat_id),
169 "unread" => $unread));
173 print $this->wrap(self::STATUS_OK, $cats);
176 function getHeadlines() {
177 $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]);
178 if ($feed_id != "") {
180 $limit = (int)db_escape_string($this->link, $_REQUEST["limit"]);
182 if (!$limit || $limit >= 60) $limit = 60;
184 $offset = (int)db_escape_string($this->link, $_REQUEST["skip"]);
185 $filter = db_escape_string($this->link, $_REQUEST["filter"]);
186 $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]);
187 $show_excerpt = sql_bool_to_bool($_REQUEST["show_excerpt"]);
188 $show_content = sql_bool_to_bool($_REQUEST["show_content"]);
189 /* all_articles, unread, adaptive, marked, updated */
190 $view_mode = db_escape_string($this->link, $_REQUEST["view_mode"]);
191 $include_attachments = sql_bool_to_bool($_REQUEST["include_attachments"]);
192 $since_id = (int)db_escape_string($this->link, $_REQUEST["since_id"]);
193 $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
194 $sanitize_content = true;
196 /* do not rely on params below */
198 $search = db_escape_string($this->link, $_REQUEST["search"]);
199 $search_mode = db_escape_string($this->link, $_REQUEST["search_mode"]);
201 $headlines = $this->api_get_headlines($this->link, $feed_id, $limit, $offset,
202 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
203 $include_attachments, $since_id, $search, $search_mode,
204 $include_nested, $sanitize_content);
206 print $this->wrap(self::STATUS_OK, $headlines);
208 print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
212 function updateArticle() {
213 $article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric);
214 $mode = (int) db_escape_string($this->link, $_REQUEST["mode"]);
215 $data = db_escape_string($this->link, $_REQUEST["data"]);
216 $field_raw = (int)db_escape_string($this->link, $_REQUEST["field"]);
221 switch ($field_raw) {
224 $additional_fields = ",last_marked = NOW()";
227 $field = "published";
228 $additional_fields = ",last_published = NOW()";
232 $additional_fields = ",last_read = NOW()";
246 $set_to = "NOT $field";
250 if ($field == "note") $set_to = "'$data'";
252 if ($field && $set_to && count($article_ids) > 0) {
254 $article_ids = join(", ", $article_ids);
256 $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"]);
258 $num_updated = db_affected_rows($this->link, $result);
260 if ($num_updated > 0 && $field == "unread") {
261 $result = db_query($this->link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
262 WHERE ref_id IN ($article_ids)");
264 while ($line = db_fetch_assoc($result)) {
265 ccache_update($this->link, $line["feed_id"], $_SESSION["uid"]);
269 if ($num_updated > 0 && $field == "published") {
270 if (PUBSUBHUBBUB_HUB) {
271 $rss_link = get_self_url_prefix() .
272 "/public.php?op=rss&id=-2&key=" .
273 get_feed_access_key($this->link, -2, false);
275 $p = new Publisher(PUBSUBHUBBUB_HUB);
276 $pubsub_result = $p->publish_update($rss_link);
280 print $this->wrap(self::STATUS_OK, array("status" => "OK",
281 "updated" => $num_updated));
284 print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
289 function getArticle() {
291 $article_id = join(",", array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_id"])), is_numeric));
293 $query = "SELECT id,title,link,content,cached_content,feed_id,comments,int_id,
294 marked,unread,published,
295 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
297 FROM ttrss_entries,ttrss_user_entries
298 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
301 $result = db_query($this->link, $query);
305 if (db_num_rows($result) != 0) {
307 while ($line = db_fetch_assoc($result)) {
309 $attachments = get_article_enclosures($this->link, $line['id']);
313 "title" => $line["title"],
314 "link" => $line["link"],
315 "labels" => get_article_labels($this->link, $line['id']),
316 "unread" => sql_bool_to_bool($line["unread"]),
317 "marked" => sql_bool_to_bool($line["marked"]),
318 "published" => sql_bool_to_bool($line["published"]),
319 "comments" => $line["comments"],
320 "author" => $line["author"],
321 "updated" => (int) strtotime($line["updated"]),
322 "content" => $line["cached_content"] != "" ? $line["cached_content"] : $line["content"],
323 "feed_id" => $line["feed_id"],
324 "attachments" => $attachments
327 array_push($articles, $article);
332 print $this->wrap(self::STATUS_OK, $articles);
336 function getConfig() {
338 "icons_dir" => ICONS_DIR,
339 "icons_url" => ICONS_URL);
341 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
343 $result = db_query($this->link, "SELECT COUNT(*) AS cf FROM
344 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
346 $num_feeds = db_fetch_result($result, 0, "cf");
348 $config["num_feeds"] = (int)$num_feeds;
350 print $this->wrap(self::STATUS_OK, $config);
353 function updateFeed() {
354 $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]);
356 update_rss_feed($this->link, $feed_id, true);
358 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
361 function catchupFeed() {
362 $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]);
363 $is_cat = db_escape_string($this->link, $_REQUEST["is_cat"]);
365 catchup_feed($this->link, $feed_id, $is_cat);
367 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
371 $pref_name = db_escape_string($this->link, $_REQUEST["pref_name"]);
373 print $this->wrap(self::STATUS_OK, array("value" => get_pref($this->link, $pref_name)));
376 function getLabels() {
377 //$article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric);
379 $article_id = (int)$_REQUEST['article_id'];
383 $result = db_query($this->link, "SELECT id, caption, fg_color, bg_color
385 WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption");
388 $article_labels = get_article_labels($this->link, $article_id);
390 $article_labels = array();
392 while ($line = db_fetch_assoc($result)) {
395 foreach ($article_labels as $al) {
396 if ($al[0] == $line['id']) {
402 array_push($rv, array(
403 "id" => (int)$line['id'],
404 "caption" => $line['caption'],
405 "fg_color" => $line['fg_color'],
406 "bg_color" => $line['bg_color'],
407 "checked" => $checked));
410 print $this->wrap(self::STATUS_OK, $rv);
413 function setArticleLabel() {
415 $article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric);
416 $label_id = (int) db_escape_string($this->link, $_REQUEST['label_id']);
417 $assign = (bool) db_escape_string($this->link, $_REQUEST['assign']) == "true";
419 $label = db_escape_string($this->link, label_find_caption($this->link,
420 $label_id, $_SESSION["uid"]));
426 foreach ($article_ids as $id) {
429 label_add_article($this->link, $id, $label, $_SESSION["uid"]);
431 label_remove_article($this->link, $id, $label, $_SESSION["uid"]);
438 print $this->wrap(self::STATUS_OK, array("status" => "OK",
439 "updated" => $num_updated));
444 print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD'));
447 function shareToPublished() {
448 $title = db_escape_string($this->link, strip_tags($_REQUEST["title"]));
449 $url = db_escape_string($this->link, strip_tags($_REQUEST["url"]));
450 $content = db_escape_string($this->link, strip_tags($_REQUEST["content"]));
452 if (Article::create_published_article($this->link, $title, $url, $content, "", $_SESSION["uid"])) {
453 print $this->wrap(self::STATUS_OK, array("status" => 'OK'));
455 print $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
459 static function api_get_feeds($link, $cat_id, $unread_only, $limit, $offset, $include_nested = false) {
465 if ($cat_id == -4 || $cat_id == -2) {
466 $counters = getLabelCounters($link, true);
468 foreach (array_values($counters) as $cv) {
470 $unread = $cv["counter"];
472 if ($unread || !$unread_only) {
476 "title" => $cv["description"],
477 "unread" => $cv["counter"],
481 array_push($feeds, $row);
488 if ($cat_id == -4 || $cat_id == -1) {
489 foreach (array(-1, -2, -3, -4, -6, 0) as $i) {
490 $unread = getFeedUnread($link, $i);
492 if ($unread || !$unread_only) {
493 $title = getFeedTitle($link, $i);
501 array_push($feeds, $row);
509 if ($include_nested && $cat_id) {
510 $result = db_query($link, "SELECT
511 id, title FROM ttrss_feed_categories
512 WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
513 " ORDER BY id, title");
515 while ($line = db_fetch_assoc($result)) {
516 $unread = getFeedUnread($link, $line["id"], true) +
517 getCategoryChildrenUnread($link, $line["id"]);
519 if ($unread || !$unread_only) {
522 "title" => $line["title"],
526 array_push($feeds, $row);
534 $limit_qpart = "LIMIT $limit OFFSET $offset";
539 if ($cat_id == -4 || $cat_id == -3) {
540 $result = db_query($link, "SELECT
541 id, feed_url, cat_id, title, order_id, ".
542 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
543 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
544 " ORDER BY cat_id, title " . $limit_qpart);
548 $cat_qpart = "cat_id = '$cat_id'";
550 $cat_qpart = "cat_id IS NULL";
552 $result = db_query($link, "SELECT
553 id, feed_url, cat_id, title, order_id, ".
554 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
555 FROM ttrss_feeds WHERE
556 $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
557 " ORDER BY cat_id, title " . $limit_qpart);
560 while ($line = db_fetch_assoc($result)) {
562 $unread = getFeedUnread($link, $line["id"]);
564 $has_icon = feed_has_icon($line['id']);
566 if ($unread || !$unread_only) {
569 "feed_url" => $line["feed_url"],
570 "title" => $line["title"],
571 "id" => (int)$line["id"],
572 "unread" => (int)$unread,
573 "has_icon" => $has_icon,
574 "cat_id" => (int)$line["cat_id"],
575 "last_updated" => (int) strtotime($line["last_updated"]),
576 "order_id" => (int) $line["order_id"],
579 array_push($feeds, $row);
586 static function api_get_headlines($link, $feed_id, $limit, $offset,
587 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
588 $include_attachments, $since_id,
589 $search = "", $search_mode = "",
590 $include_nested = false, $sanitize_content = true) {
592 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
593 $view_mode, $is_cat, $search, $search_mode,
594 $order, $offset, 0, false, $since_id, $include_nested);
596 $result = $qfh_ret[0];
597 $feed_title = $qfh_ret[1];
599 $headlines = array();
601 while ($line = db_fetch_assoc($result)) {
602 $is_updated = ($line["last_read"] == "" &&
603 ($line["unread"] != "t" && $line["unread"] != "1"));
605 $tags = explode(",", $line["tag_cache"]);
606 $labels = json_decode($line["label_cache"], true);
608 //if (!$tags) $tags = get_article_tags($link, $line["id"]);
609 //if (!$labels) $labels = get_article_labels($link, $line["id"]);
611 $headline_row = array(
612 "id" => (int)$line["id"],
613 "unread" => sql_bool_to_bool($line["unread"]),
614 "marked" => sql_bool_to_bool($line["marked"]),
615 "published" => sql_bool_to_bool($line["published"]),
616 "updated" => (int) strtotime($line["updated"]),
617 "is_updated" => $is_updated,
618 "title" => $line["title"],
619 "link" => $line["link"],
620 "feed_id" => $line["feed_id"],
624 if ($include_attachments)
625 $headline_row['attachments'] = get_article_enclosures($link,
629 $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
630 $headline_row["excerpt"] = $excerpt;
635 if ($line["cached_content"] != "") {
636 $line["content_preview"] =& $line["cached_content"];
639 if ($sanitize_content) {
640 $headline_row["content"] = sanitize($link,
641 $line["content_preview"],
642 sql_bool_to_bool($line['hide_images']),
643 false, $line["site_url"]);
645 $headline_row["content"] = $line["content_preview"];
649 // unify label output to ease parsing
650 if ($labels["no-labels"] == 1) $labels = array();
652 $headline_row["labels"] = $labels;
654 $headline_row["feed_title"] = $line["feed_title"];
656 $headline_row["comments_count"] = (int)$line["num_comments"];
657 $headline_row["comments_link"] = $line["comments"];
659 $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]);
662 foreach ($pluginhost->get_hooks($pluginhost::HOOK_RENDER_ARTICLE_API) as $p) {
663 $headline_row = $p->hook_render_article_api($headline_row);
666 array_push($headlines, $headline_row);
672 function unsubscribeFeed() {
673 $feed_id = (int) db_escape_string($this->link, $_REQUEST["feed_id"]);
675 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
676 id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
678 if (db_num_rows($result) != 0) {
679 Pref_Feeds::remove_feed($this->link, $feed_id, $_SESSION["uid"]);
680 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
682 print $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
686 function subscribeToFeed() {
687 $feed_url = db_escape_string($this->link, $_REQUEST["feed_url"]);
688 $category_id = (int) db_escape_string($this->link, $_REQUEST["category_id"]);
689 $login = db_escape_string($this->link, $_REQUEST["login"]);
690 $password = db_escape_string($this->link, $_REQUEST["password"]);
693 $rc = subscribe_to_feed($this->link, $feed_url, $category_id,
694 $login, $password, false);
696 print $this->wrap(self::STATUS_OK, array("status" => $rc));
698 print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));