]> git.wh0rd.org - tt-rss.git/blobdiff - modules/backend-rpc.php
assign/remove to label rpc: use JSON
[tt-rss.git] / modules / backend-rpc.php
index e93571207c50423635a7b4303d7e84b21fbc9977..e027ff39d1f906179a15c2c800ca91b8abf523d1 100644 (file)
                }
 
                if ($subop == "togglepref") {
-                       print "<rpc-reply>";
+                       header("Content-Type: text/plain");
 
                        $key = db_escape_string($_REQUEST["key"]);
-
                        set_pref($link, $key, !get_pref($link, $key));
-
                        $value = get_pref($link, $key);
 
-                       print "<param-set key=\"$key\" value=\"$value\"/>";
-
-                       print "</rpc-reply>";
-
+                       print json_encode(array("param" =>$key, "value" => $value));
                        return;
                }
 
                }
 
                if ($subop == "mark") {
+                       header("Content-Type: text/plain");
+
                        $mark = $_REQUEST["mark"];
                        $id = db_escape_string($_REQUEST["id"]);
 
                        $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
                                WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
 
-                       print "<rpc-reply>";
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
+                       print json_encode(array("message" => "UPDATE_COUNTERS"));
                        return;
                }
 
                if ($subop == "delete") {
+                       header("Content-Type: text/plain");
+
                        $ids = db_escape_string($_REQUEST["ids"]);
 
                        $result = db_query($link, "DELETE FROM ttrss_user_entries                               
                                WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
 
-                       print "<rpc-reply>";
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
+                       print json_encode(array("message" => "UPDATE_COUNTERS"));
                        return;
                }
 
                if ($subop == "unarchive") {
+                       header("Content-Type: text/plain");
+
                        $ids = db_escape_string($_REQUEST["ids"]);
 
                        $result = db_query($link, "UPDATE ttrss_user_entries 
                                SET feed_id = orig_feed_id, orig_feed_id = NULL
                                WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
 
-                       print "<rpc-reply>";
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
+                       print json_encode(array("message" => "UPDATE_COUNTERS"));
                        return;
                }
 
                if ($subop == "archive") {
+                       header("Content-Type: text/plain");
+
                        $ids = split(",", db_escape_string($_REQUEST["ids"]));
 
                        foreach ($ids as $id) {
                                archive_article($link, $id, $_SESSION["uid"]);
                        }
 
-                       print "<rpc-reply>";
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
+                       print json_encode(array("message" => "UPDATE_COUNTERS"));
                        return;
                }
 
                        return;
                }
 
-               if ($subop == "updateFeed") {
+/*             if ($subop == "updateFeed") {
                        $feed_id = db_escape_string($_REQUEST["feed"]);
 
                        update_rss_feed($link, $feed_id);
                        print "</rpc-reply>";
 
                        return;
-               }
+               } */
 
                if ($subop == "updateAllFeeds" || $subop == "getAllCounters") {
 
+                       header("Content-Type: text/plain");
+
                        $last_article_id = (int) $_REQUEST["last_article_id"];  
 
-                       print "<rpc-reply>";
+                       $reply = array();
 
-                       if ($seq)
-                               print "<seq>$seq</seq>";
+                       if ($seq) $reply['seq'] = $seq;
 
                        if ($last_article_id != getLastArticleId($link)) {
-                               print "<counters><![CDATA[";
                                $omode = $_REQUEST["omode"];
 
                                if ($omode != "T") 
-                                       print json_encode(getAllCounters($link, $omode));
+                                       $reply['counters'] = getAllCounters($link, $omode);
                                else
-                                       print json_encode(getGlobalCounters($link));
-
-                               print "]]></counters>";
+                                       $reply['counters'] = getGlobalCounters($link);
                        }
  
-                       print_runtime_info($link);
+                       $reply['runtime-info'] = make_runtime_info($link);
 
-                       print "</rpc-reply>";
 
+                       print json_encode($reply);
                        return;
                }
 
                /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
                if ($subop == "catchupSelected") {
+                       header("Content-Type: text/plain");
 
                        $ids = split(",", db_escape_string($_REQUEST["ids"]));
                        $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
                        catchupArticlesById($link, $ids, $cmode);
 
-                       print "<rpc-reply>";
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
+                       print json_encode(array("message" => "UPDATE_COUNTERS"));
                        return;
                }
 
                if ($subop == "markSelected") {
+                       header("Content-Type: text/plain");
 
                        $ids = split(",", db_escape_string($_REQUEST["ids"]));
                        $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
                        markArticlesById($link, $ids, $cmode);
 
-                       print "<rpc-reply>";
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
+                       print json_encode(array("message" => "UPDATE_COUNTERS"));
                        return;
                }
 
                if ($subop == "publishSelected") {
+                       header("Content-Type: text/plain");
 
                        $ids = split(",", db_escape_string($_REQUEST["ids"]));
                        $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
                        publishArticlesById($link, $ids, $cmode);
 
-                       print "<rpc-reply>";
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
+                       print json_encode(array("message" => "UPDATE_COUNTERS"));
                        return;
                }
 
                }
 
                if ($subop == "completeTags") {
+                       header("Content-Type: text/plain");
 
                        $search = db_escape_string($_REQUEST["search"]);
 
                        $ids = split(",", db_escape_string($_REQUEST["ids"]));
                        $days = sprintf("%d", $_REQUEST["days"]);
 
-                       print "<rpc-reply>";
-
-                       print "<message><![CDATA[";
-
                        foreach ($ids as $id) {
 
                                $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
                                        id = '$id' AND owner_uid = ".$_SESSION["uid"]);
 
                                if (db_num_rows($result) == 1) {
-                                       purge_feed($link, $id, $days, true);
+                                       purge_feed($link, $id, $days);
                                }
                        }
 
-                       print "]]></message>";
-
-                       print "</rpc-reply>";
-
                        return;
                }
 
                        return;
                }
 
-               if ($subop == "removeFromLabel") {
-
-                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
-                       $label_id = db_escape_string($_REQUEST["lid"]);
-
-                       $label = db_escape_string(label_find_caption($link, $label_id, 
-                               $_SESSION["uid"]));
-
-                       print "<rpc-reply>";
-                       print "<info-for-headlines>";
-
-                       if ($label) {
-
-                               foreach ($ids as $id) {
-                                       label_remove_article($link, $id, $label, $_SESSION["uid"]);
-
-                                       print "<entry id=\"$id\"><![CDATA[";
-
-                                       $labels = get_article_labels($link, $id, $_SESSION["uid"]);
-                                       print format_article_labels($labels, $id);
-
-                                       print "]]></entry>";
+               if ($subop == "assignToLabel" || $subop == "removeFromLabel") {
+                       header("Content-Type: text/plain");
 
-                               }
-                       }
-
-                       print "</info-for-headlines>";
-
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
-                       return;
-               }
-
-               if ($subop == "assignToLabel") {
+                       $reply = array();
 
                        $ids = split(",", db_escape_string($_REQUEST["ids"]));
                        $label_id = db_escape_string($_REQUEST["lid"]);
                        $label = db_escape_string(label_find_caption($link, $label_id, 
                                $_SESSION["uid"]));
 
-                       print "<rpc-reply>";                    
-
-                       print "<info-for-headlines>";
+                       $reply["info-for-headlines"] = array();
 
                        if ($label) {
 
                                foreach ($ids as $id) {
-                                       label_add_article($link, $id, $label, $_SESSION["uid"]);
 
-                                       print "<entry id=\"$id\"><![CDATA[";
+                                       if ($subop == "assignToLabel")
+                                               label_add_article($link, $id, $label, $_SESSION["uid"]);
+                                       else
+                                               label_remove_article($link, $id, $label, $_SESSION["uid"]);
 
                                        $labels = get_article_labels($link, $id, $_SESSION["uid"]);
-                                       print format_article_labels($labels, $id);
-
-                                       print "]]></entry>";
+                                 
+                                       array_push($reply["info-for-headlines"],
+                                         array("id" => $id, "labels" => format_article_labels($labels, $id)));
 
                                }
                        }
 
-                       print "</info-for-headlines>";
+                       $reply["message"] = "UPDATE_COUNTERS";
 
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
+                       print json_encode($reply);
 
                        return;
                }
                }
 
                if ($subop == "digest-update") {
+                       header("Content-Type: text/plain");
+
                        $feed_id = db_escape_string($_REQUEST['feed_id']);
                        $offset = db_escape_string($_REQUEST['offset']);
                        $seq = db_escape_string($_REQUEST['seq']);
                
                        if (!$feed_id) $feed_id = -4;
                        if (!$offset) $offset = 0;
-                       print "<rpc-reply>";
 
-                       print "<seq>$seq</seq>";
+                       $reply = array();
+
+                       $reply['seq'] = $seq;
 
                        $headlines = api_get_headlines($link, $feed_id, 10, $offset,
                                '', ($feed_id == -4), true, false, "unread", "updated DESC");
                        //function api_get_headlines($link, $feed_id, $limit, $offset,
                        //              $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
 
-                       print "<headlines-title><![CDATA[" . getFeedTitle($link, $feed_id) . 
-                               "]]></headlines-title>";
+                       $reply['headlines'] = array();
+                       $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
+                       $reply['headlines']['content'] = $headlines;
 
-                       print "<headlines><![CDATA[" . json_encode($headlines) . "]]></headlines>";
-
-                       print "</rpc-reply>";
+                       print json_encode($reply);
                        return;
                }
 
                if ($subop == "digest-init") {
-                       print "<rpc-reply>";
-               
+                       header("Content-Type: text/plain");
+
                        $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
 
                        $feeds = array();
                                if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
                        }
 
-                       print "<feeds><![CDATA[" . json_encode($feeds) . "]]></feeds>";
+                       print json_encode(array("feeds" => $feeds));
 
-                       print "</rpc-reply>";
                        return;
                }
 
                }
 
                if ($subop == "cdmGetArticle") {
+                       header("Content-Type: text/plain");
+
                        $id = db_escape_string($_REQUEST["id"]);
 
                        $result = db_query($link, "SELECT content, 
                                $article_content = '';
                        }
 
-                       print "<rpc-reply><article id=\"$id\"><![CDATA[";
-                       print "$article_content";
-                       print "]]></article></rpc-reply>";
+                       print json_encode(array("article" =>
+                               array("id" => $id, "content" => $article_content)));
 
                        return;
                }
 
                if ($subop == "scheduleFeedUpdate") {
+                       header("Content-Type: text/plain");
+
                        $feed_id = db_escape_string($_REQUEST["id"]);
                        $is_cat =  db_escape_string($_REQUEST['is_cat']) == 'true';
 
                                $message = __("Can't update this kind of feed.");
                        }
 
-                       print "<rpc-reply>";
-                       print "<message>$message</message>";
-                       print "</rpc-reply>";
-
+                       print json_encode(array("message" => $message));
                        return;
                }
 
                if ($subop == "getTweetInfo") {
-                       header("Content-Type: text/html");
+                       header("Content-Type: text/plain");
                        $id = db_escape_string($_REQUEST['id']);
 
                        $result = db_query($link, "SELECT title, link