]> git.wh0rd.org - tt-rss.git/blobdiff - classes/api.php
implement ttrss_feeds.cache_content
[tt-rss.git] / classes / api.php
index 1a79ffe2d6c827aa8be1ee2ea7edc88febe48179..e3dce0c174078c61ed0593c8466f2eb5905f2a93 100644 (file)
@@ -2,7 +2,7 @@
 
 class API extends Handler {
 
-       const API_LEVEL  = 2;
+       const API_LEVEL  = 4;
 
        const STATUS_OK  = 0;
        const STATUS_ERR = 1;
@@ -11,6 +11,7 @@ class API extends Handler {
 
        function before($method) {
                if (parent::before($method)) {
+                       header("Content-Type: text/plain");
 
                        if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") {
                                print $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN'));
@@ -24,7 +25,6 @@ class API extends Handler {
 
                        $this->seq = (int) $_REQUEST['seq'];
 
-                       header("Content-Type: text/plain");
                        return true;
                }
                return false;
@@ -68,9 +68,11 @@ class API extends Handler {
 
                if (get_pref($this->link, "ENABLE_API_ACCESS", $uid)) {
                        if (authenticate_user($this->link, $login, $password)) {               // try login with normal password
-                               print $this->wrap(self::STATUS_OK, array("session_id" => session_id()));
+                               print $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
+                                       "api_level" => self::API_LEVEL));
                        } else if (authenticate_user($this->link, $login, $password_base64)) { // else try with base64_decoded password
-                               print $this->wrap(self::STATUS_OK,      array("session_id" => session_id()));
+                               print $this->wrap(self::STATUS_OK,      array("session_id" => session_id(),
+                                       "api_level" => self::API_LEVEL));
                        } else {                                                         // else we are not logged in
                                print $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
                        }
@@ -114,20 +116,27 @@ class API extends Handler {
                $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
                $limit = (int) db_escape_string($_REQUEST["limit"]);
                $offset = (int) db_escape_string($_REQUEST["offset"]);
+               $include_nested = (bool)db_escape_string($_REQUEST["include_nested"]);
 
-               $feeds = api_get_feeds($this->link, $cat_id, $unread_only, $limit, $offset);
+               $feeds = api_get_feeds($this->link, $cat_id, $unread_only, $limit, $offset, $include_nested);
 
                print $this->wrap(self::STATUS_OK, $feeds);
        }
 
        function getCategories() {
                $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
+               $enable_nested = (bool)db_escape_string($_REQUEST["enable_nested"]);
 
                // TODO do not return empty categories, return Uncategorized and standard virtual cats
 
+               if ($enable_nested)
+                       $nested_qpart = "parent_cat IS NULL";
+               else
+                       $nested_qpart = "true";
+
                $result = db_query($this->link, "SELECT
-                               id, title FROM ttrss_feed_categories
-                       WHERE owner_uid = " .
+                               id, title, order_id FROM ttrss_feed_categories
+                       WHERE $nested_qpart AND owner_uid = " .
                        $_SESSION["uid"]);
 
                $cats = array();
@@ -135,10 +144,15 @@ class API extends Handler {
                while ($line = db_fetch_assoc($result)) {
                        $unread = getFeedUnread($this->link, $line["id"], true);
 
+                       if ($enable_nested)
+                               $unread += getCategoryChildrenUnread($this->link, $line["id"]);
+
                        if ($unread || !$unread_only) {
                                array_push($cats, array("id" => $line["id"],
                                        "title" => $line["title"],
-                                       "unread" => $unread));
+                                       "unread" => $unread,
+                                       "order_id" => (int) $line["order_id"],
+                               ));
                        }
                }
 
@@ -172,6 +186,8 @@ class API extends Handler {
                        $view_mode = db_escape_string($_REQUEST["view_mode"]);
                        $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]);
                        $since_id = (int)db_escape_string($_REQUEST["since_id"]);
+                       $include_nested = (bool)db_escape_string($_REQUEST["include_nested"]);
+                       $sanitize_content = true;
 
                        /* do not rely on params below */
 
@@ -181,7 +197,8 @@ class API extends Handler {
 
                        $headlines = api_get_headlines($this->link, $feed_id, $limit, $offset,
                                $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
-                               $include_attachments, $since_id, $search, $search_mode, $match_on);
+                               $include_attachments, $since_id, $search, $search_mode, $match_on,
+                               $include_nested, $sanitize_content);
 
                        print $this->wrap(self::STATUS_OK, $headlines);
                } else {
@@ -263,7 +280,7 @@ class API extends Handler {
 
                $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric));
 
-               $query = "SELECT id,title,link,content,feed_id,comments,int_id,
+               $query = "SELECT id,title,link,content,cached_content,feed_id,comments,int_id,
                        marked,unread,published,
                        ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
                        author
@@ -292,7 +309,7 @@ class API extends Handler {
                                        "comments" => $line["comments"],
                                        "author" => $line["author"],
                                        "updated" => strtotime($line["updated"]),
-                                       "content" => $line["content"],
+                                       "content" => $line["cached_content"] != "" ? $line["cached_content"] : $line["content"],
                                        "feed_id" => $line["feed_id"],
                                        "attachments" => $attachments
                                );
@@ -417,6 +434,17 @@ class API extends Handler {
                print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD'));
        }
 
+       function shareToPublished() {
+               $title = db_escape_string(strip_tags($_REQUEST["title"]));
+               $url = db_escape_string(strip_tags($_REQUEST["url"]));
+               $content = db_escape_string(strip_tags($_REQUEST["content"]));
+
+               if (create_published_article($this->link, $title, $url, $content, "", $_SESSION["uid"])) {
+                       print $this->wrap(self::STATUS_OK, array("status" => 'OK'));
+               } else {
+                       print $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
+               }
+       }
 }
 
 ?>