]> git.wh0rd.org - tt-rss.git/blobdiff - api/index.php
Merge branch 'master' of madoka.volgo-balt.ru:public_html/testbox/tt-rss
[tt-rss.git] / api / index.php
index 5c1efd15e97797d5b448a1e5a19179fc8ad92a42..f02f92a778d2f855f302f06639c232d98de6275a 100644 (file)
@@ -61,7 +61,7 @@
                                $uid = 0;
                        }
 
-                       if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
+                       if ($uid && get_pref($link, "ENABLE_API_ACCESS", $uid)) {
                                if (authenticate_user($link, $login, $password)) {
                                        print json_encode(array("session_id" => session_id()));
                                } else {
                        } else {
                                print json_encode(array("unread" => getGlobalUnread($link)));
                        }
+                       break;
+               case "getCounters":
+
+                       /* TODO */
+
                        break;
                case "getFeeds":
                        $cat_id = db_escape_string($_REQUEST["cat_id"]);
                        $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
+                       $limit = (int) db_escape_string($_REQUEST["limit"]);
+                       $offset = (int) db_escape_string($_REQUEST["offset"]);
 
-                       if (!$cat_id) {
-                               $result = db_query($link, "SELECT 
-                                       id, feed_url, cat_id, title, ".
-                                               SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
-                                               FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
-                       } else {
-                               $result = db_query($link, "SELECT 
-                                       id, feed_url, cat_id, title, ".
-                                               SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
-                                               FROM ttrss_feeds WHERE 
-                                                       cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
-                       }
-
-                       $feeds = array();
-
-                       while ($line = db_fetch_assoc($result)) {
-
-                               $unread = getFeedUnread($link, $line["id"]);
-
-                               if ($unread || !$unread_only) {
-
-                                       $row = array(
-                                                       "feed_url" => $line["feed_url"],
-                                                       "title" => $line["title"],
-                                                       "id" => (int)$line["id"],
-                                                       "unread" => (int)$unread,
-                                                       "cat_id" => (int)$line["cat_id"],
-                                                       "last_updated" => strtotime($line["last_updated"])
-                                               );
-       
-                                       array_push($feeds, $row);
-                               }
-                       }
-
-                       /* Labels */
-
-                       if (!$cat_id || $cat_id == -2) {
-                               $counters = getLabelCounters($link, false, true);
-
-                               foreach (array_keys($counters) as $id) {
-
-                                       $unread = $counters[$id]["counter"];
-       
-                                       if ($unread || !$unread_only) {
-       
-                                               $row = array(
-                                                               "id" => $id,
-                                                               "title" => $counters[$id]["description"],
-                                                               "unread" => $counters[$id]["counter"],
-                                                               "cat_id" => -2,
-                                                       );
-       
-                                               array_push($feeds, $row);
-                                       }
-                               }
-                       }
-
-                       /* Virtual feeds */
-
-                       if (!$cat_id || $cat_id == -1) {
-                               foreach (array(-1, -2, -3, -4) as $i) {
-                                       $unread = getFeedUnread($link, $i);
-
-                                       if ($unread || !$unread_only) {
-                                               $title = getFeedTitle($link, $i);
-
-                                               $row = array(
-                                                               "id" => $i,
-                                                               "title" => $title,
-                                                               "unread" => $unread,
-                                                               "cat_id" => -1,
-                                                       );
-                                               array_push($feeds, $row);
-                                       }
-
-                               }
-                       }
+                       $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
 
                        print json_encode($feeds);
 
                case "getHeadlines":
                        $feed_id = db_escape_string($_REQUEST["feed_id"]);
                        $limit = (int)db_escape_string($_REQUEST["limit"]);
+                       $offset = (int)db_escape_string($_REQUEST["skip"]);
                        $filter = db_escape_string($_REQUEST["filter"]);
                        $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
-                       $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]);
+                       $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
+                       $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
+                       /* all_articles, unread, adaptive, marked, updated */
+                       $view_mode = db_escape_string($_REQUEST["view_mode"]);
 
-                       /* do not rely on params below */
-
-                       $search = db_escape_string($_REQUEST["search"]);
-                       $search_mode = db_escape_string($_REQUEST["search_mode"]);
-                       $match_on = db_escape_string($_REQUEST["match_on"]);
-                       
-                       $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, 
-                               $view_mode, $is_cat, $search, $search_mode, $match_on);
-
-                       $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"));
-
-                               $headline_row = array(
-                                               "id" => (int)$line["id"],
-                                               "unread" => sql_bool_to_bool($line["unread"]),
-                                               "marked" => sql_bool_to_bool($line["marked"]),
-                                               "updated" => strtotime($line["updated"]),
-                                               "is_updated" => $is_updated,
-                                               "title" => $line["title"],
-                                               "feed_id" => $line["feed_id"],
-                                       );
-
-                               if ($show_except) {
-                                       $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
-                                       $headline_row["excerpt"] = $excerpt;
-                               }
-                       
-                               array_push($headlines, $headline_row);
-                       }
+                       $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
+                               $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false);
 
                        print json_encode($headlines);
 
                        break;
                case "updateArticle":
-                       $article_id = (int) db_escape_string($_GET["article_id"]);
+                       $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
                        $mode = (int) db_escape_string($_REQUEST["mode"]);
                        $field_raw = (int)db_escape_string($_REQUEST["field"]);
 
                                        break;
                        }
 
-                       if ($field && $set_to) {
+                       if ($field && $set_to && count($article_ids) > 0) {
+
+                               $article_ids = join(", ", $article_ids);
+
                                if ($field == "unread") {
                                        $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
                                                last_read = NOW()
-                                               WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
+                                               WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
                                } else {
                                        $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
-                                               WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
+                                               WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
                                }
                        }
 
 
                case "getArticle":
 
-                       $article_id = (int)db_escape_string($_REQUEST["article_id"]);
+                       $article_id = db_escape_string($_REQUEST["article_id"]);
 
-                       $query = "SELECT title,link,content,feed_id,comments,int_id,
+                       $query = "SELECT id,title,link,content,feed_id,comments,int_id,
                                marked,unread,published,
                                ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
                                author
                                FROM ttrss_entries,ttrss_user_entries
-                               WHERE   id = '$article_id' AND ref_id = id AND owner_uid = " . 
+                               WHERE   id IN ($article_id) AND ref_id = id AND owner_uid = " . 
                                        $_SESSION["uid"] ;
 
                        $result = db_query($link, $query);
-
-                       $article = array();
-                       
+               
                        if (db_num_rows($result) != 0) {
-                               $line = db_fetch_assoc($result);
-       
-                               $article = array(
-                                       "title" => $line["title"],
-                                       "link" => $line["link"],
-                                       "labels" => get_article_labels($link, $article_id),
-                                       "unread" => sql_bool_to_bool($line["unread"]),
-                                       "marked" => sql_bool_to_bool($line["marked"]),
-                                       "published" => sql_bool_to_bool($line["published"]),
-                                       "comments" => $line["comments"],
-                                       "author" => $line["author"],
-                                       "updated" => strtotime($line["updated"]),
-                                       "content" => $line["content"],
-                                       "feed_id" => $line["feed_id"],                  
-                               );
-                       }
 
-                       print json_encode($article);
+                               while ($line = db_fetch_assoc($result)) {
+
+                                       $attachments = get_article_enclosures($link, $line['id']);
+
+                                       $article = array(
+                                               "id" => $line["id"],
+                                               "title" => $line["title"],
+                                               "link" => $line["link"],
+                                               "labels" => get_article_labels($link, $line['id']),
+                                               "unread" => sql_bool_to_bool($line["unread"]),
+                                               "marked" => sql_bool_to_bool($line["marked"]),
+                                               "published" => sql_bool_to_bool($line["published"]),
+                                               "comments" => $line["comments"],
+                                               "author" => $line["author"],
+                                               "updated" => strtotime($line["updated"]),
+                                               "content" => $line["content"],
+                                               "feed_id" => $line["feed_id"],
+                                               "attachments" => $attachments
+                                       );
+
+                                       print json_encode($article);
+                               }
+                       }
 
                        break;
                case "getConfig":
                case "updateFeed":
                        $feed_id = db_escape_string($_REQUEST["feed_id"]);
 
-                       $result = db_query($link, 
-                               "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
-                                       AND owner_uid = " . $_SESSION["uid"]);
+                       update_rss_feed($link, $feed_id, true);
 
-                       if (db_num_rows($result) > 0) {                 
-                               $feed_url = db_fetch_result($result, 0, "feed_url");
-                               update_rss_feed($link, $feed_url, $feed_id);
-                       }
+                       print json_encode(array("status" => "OK"));
+
+                       break;
+
+               case "catchupFeed":
+                       $feed_id = db_escape_string($_REQUEST["feed_id"]);
+                       $is_cat = db_escape_string($_REQUEST["category"]);
+
+                       catchup_feed($link, $feed_id, $is_cat);
 
                        print json_encode(array("status" => "OK"));