+
+ static function api_get_feeds( $cat_id, $unread_only, $limit, $offset, $include_nested = false) {
+
+ $feeds = array();
+
+ /* Labels */
+
+ if ($cat_id == -4 || $cat_id == -2) {
+ $counters = getLabelCounters( true);
+
+ foreach (array_values($counters) as $cv) {
+
+ $unread = $cv["counter"];
+
+ if ($unread || !$unread_only) {
+
+ $row = array(
+ "id" => $cv["id"],
+ "title" => $cv["description"],
+ "unread" => $cv["counter"],
+ "cat_id" => -2,
+ );
+
+ array_push($feeds, $row);
+ }
+ }
+ }
+
+ /* Virtual feeds */
+
+ if ($cat_id == -4 || $cat_id == -1) {
+ foreach (array(-1, -2, -3, -4, -6, 0) as $i) {
+ $unread = getFeedUnread( $i);
+
+ if ($unread || !$unread_only) {
+ $title = getFeedTitle( $i);
+
+ $row = array(
+ "id" => $i,
+ "title" => $title,
+ "unread" => $unread,
+ "cat_id" => -1,
+ );
+ array_push($feeds, $row);
+ }
+
+ }
+ }
+
+ /* Child cats */
+
+ if ($include_nested && $cat_id) {
+ $result = db_query( "SELECT
+ id, title FROM ttrss_feed_categories
+ WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
+ " ORDER BY id, title");
+
+ while ($line = db_fetch_assoc($result)) {
+ $unread = getFeedUnread( $line["id"], true) +
+ getCategoryChildrenUnread( $line["id"]);
+
+ if ($unread || !$unread_only) {
+ $row = array(
+ "id" => $line["id"],
+ "title" => $line["title"],
+ "unread" => $unread,
+ "is_cat" => true,
+ );
+ array_push($feeds, $row);
+ }
+ }
+ }
+
+ /* Real feeds */
+
+ if ($limit) {
+ $limit_qpart = "LIMIT $limit OFFSET $offset";
+ } else {
+ $limit_qpart = "";
+ }
+
+ if ($cat_id == -4 || $cat_id == -3) {
+ $result = db_query( "SELECT
+ id, feed_url, cat_id, title, order_id, ".
+ SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
+ FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
+ " ORDER BY cat_id, title " . $limit_qpart);
+ } else {
+
+ if ($cat_id)
+ $cat_qpart = "cat_id = '$cat_id'";
+ else
+ $cat_qpart = "cat_id IS NULL";
+
+ $result = db_query( "SELECT
+ id, feed_url, cat_id, title, order_id, ".
+ SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
+ FROM ttrss_feeds WHERE
+ $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
+ " ORDER BY cat_id, title " . $limit_qpart);
+ }
+
+ while ($line = db_fetch_assoc($result)) {
+
+ $unread = getFeedUnread( $line["id"]);
+
+ $has_icon = feed_has_icon($line['id']);
+
+ if ($unread || !$unread_only) {
+
+ $row = array(
+ "feed_url" => $line["feed_url"],
+ "title" => $line["title"],
+ "id" => (int)$line["id"],
+ "unread" => (int)$unread,
+ "has_icon" => $has_icon,
+ "cat_id" => (int)$line["cat_id"],
+ "last_updated" => (int) strtotime($line["last_updated"]),
+ "order_id" => (int) $line["order_id"],
+ );
+
+ array_push($feeds, $row);
+ }
+ }
+
+ return $feeds;
+ }
+
+ static function api_get_headlines( $feed_id, $limit, $offset,
+ $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
+ $include_attachments, $since_id,
+ $search = "", $search_mode = "",
+ $include_nested = false, $sanitize_content = true) {
+
+ $qfh_ret = queryFeedHeadlines( $feed_id, $limit,
+ $view_mode, $is_cat, $search, $search_mode,
+ $order, $offset, 0, false, $since_id, $include_nested);
+
+ $result = $qfh_ret[0];
+ $feed_title = $qfh_ret[1];
+
+ $headlines = array();
+
+ while ($line = db_fetch_assoc($result)) {
+ $is_updated = ($line["last_read"] == "" &&
+ ($line["unread"] != "t" && $line["unread"] != "1"));
+
+ $tags = explode(",", $line["tag_cache"]);
+ $labels = json_decode($line["label_cache"], true);
+
+ //if (!$tags) $tags = get_article_tags( $line["id"]);
+ //if (!$labels) $labels = get_article_labels( $line["id"]);
+
+ $headline_row = array(
+ "id" => (int)$line["id"],
+ "unread" => sql_bool_to_bool($line["unread"]),
+ "marked" => sql_bool_to_bool($line["marked"]),
+ "published" => sql_bool_to_bool($line["published"]),
+ "updated" => (int) strtotime($line["updated"]),
+ "is_updated" => $is_updated,
+ "title" => $line["title"],
+ "link" => $line["link"],
+ "feed_id" => $line["feed_id"],
+ "tags" => $tags,
+ );
+
+ if ($include_attachments)
+ $headline_row['attachments'] = get_article_enclosures(
+ $line['id']);
+
+ if ($show_excerpt) {
+ $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
+ $headline_row["excerpt"] = $excerpt;
+ }
+
+ if ($show_content) {
+
+ if ($line["cached_content"] != "") {
+ $line["content_preview"] =& $line["cached_content"];
+ }
+
+ if ($sanitize_content) {
+ $headline_row["content"] = sanitize(
+ $line["content_preview"],
+ sql_bool_to_bool($line['hide_images']),
+ false, $line["site_url"]);
+ } else {
+ $headline_row["content"] = $line["content_preview"];
+ }
+ }
+
+ // unify label output to ease parsing
+ if ($labels["no-labels"] == 1) $labels = array();
+
+ $headline_row["labels"] = $labels;
+
+ $headline_row["feed_title"] = $line["feed_title"];
+
+ $headline_row["comments_count"] = (int)$line["num_comments"];
+ $headline_row["comments_link"] = $line["comments"];
+
+ $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]);
+
+ $headline_row["author"] = $line["author"];
+ $headline_row["score"] = (int)$line["score"];
+
+ global $pluginhost;
+ foreach ($pluginhost->get_hooks($pluginhost::HOOK_RENDER_ARTICLE_API) as $p) {
+ $headline_row = $p->hook_render_article_api(array("headline" => $headline_row));
+ }
+
+ array_push($headlines, $headline_row);
+ }
+
+ return $headlines;
+ }
+
+ function unsubscribeFeed() {
+ $feed_id = (int) db_escape_string( $_REQUEST["feed_id"]);
+
+ $result = db_query( "SELECT id FROM ttrss_feeds WHERE
+ id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
+
+ if (db_num_rows($result) != 0) {
+ Pref_Feeds::remove_feed( $feed_id, $_SESSION["uid"]);
+ print $this->wrap(self::STATUS_OK, array("status" => "OK"));
+ } else {
+ print $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
+ }
+ }
+
+ function subscribeToFeed() {
+ $feed_url = db_escape_string( $_REQUEST["feed_url"]);
+ $category_id = (int) db_escape_string( $_REQUEST["category_id"]);
+ $login = db_escape_string( $_REQUEST["login"]);
+ $password = db_escape_string( $_REQUEST["password"]);
+
+ if ($feed_url) {
+ $rc = subscribe_to_feed( $feed_url, $category_id,
+ $login, $password, false);
+
+ print $this->wrap(self::STATUS_OK, array("status" => $rc));
+ } else {
+ print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
+ }
+ }
+
+ function getFeedTree() {
+ $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
+
+ $pf = new Pref_Feeds( $_REQUEST);
+
+ $_REQUEST['mode'] = 2;
+ $_REQUEST['force_show_empty'] = $include_empty;
+
+ if ($pf){
+ $data = $pf->makefeedtree();
+ print $this->wrap(self::STATUS_OK, array("categories" => $data));
+ } else {
+ print $this->wrap(self::STATUS_ERR, array("error" =>
+ 'UNABLE_TO_INSTANTIATE_OBJECT'));
+ }
+
+ }
+
+ // only works for labels or uncategorized for the time being
+ private function isCategoryEmpty($id) {
+
+ if ($id == -2) {
+ $result = db_query( "SELECT COUNT(*) AS count FROM ttrss_labels2
+ WHERE owner_uid = " . $_SESSION["uid"]);
+
+ return db_fetch_result($result, 0, "count") == 0;
+
+ } else if ($id == 0) {
+ $result = db_query( "SELECT COUNT(*) AS count FROM ttrss_feeds
+ WHERE cat_id IS NULL AND owner_uid = " . $_SESSION["uid"]);
+
+ return db_fetch_result($result, 0, "count") == 0;
+
+ }
+
+ return false;
+ }
+
+