]> git.wh0rd.org - tt-rss.git/blobdiff - modules/backend-rpc.php
functions.php: Fixed some mild errors.
[tt-rss.git] / modules / backend-rpc.php
index ea1867e0c4e2dc04739434f40f5896d1ff81b94a..a89c5ee815910570e657dfb35d861e93dcf6caf0 100644 (file)
@@ -15,7 +15,7 @@
 
                // Silent
                if ($subop == "remprofiles") {
-                       $ids = split(",", db_escape_string(trim($_REQUEST["ids"])));
+                       $ids = explode(",", db_escape_string(trim($_REQUEST["ids"])));
 
                        foreach ($ids as $id) {
                                if ($_SESSION["profile"] != $id) {
@@ -91,7 +91,7 @@
 
                // Silent
                if ($subop == "remarchive") {
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
 
                        foreach ($ids as $id) {
                                $result = db_query($link, "DELETE FROM ttrss_archived_feeds WHERE
                }
 
                if ($subop == "addfeed") {
-                       header("Content-Type: text/plain");
-
                        $feed = db_escape_string($_REQUEST['feed']);
                        $cat = db_escape_string($_REQUEST['cat']);
                        $login = db_escape_string($_REQUEST['login']);
                }
 
                if ($subop == "extractfeedurls") {
-                       header("Content-Type: text/plain");
 
                        $urls = get_feeds_from_html($_REQUEST['url']);
 
                }
 
                if ($subop == "togglepref") {
-                       header("Content-Type: text/plain");
-
                        $key = db_escape_string($_REQUEST["key"]);
                        set_pref($link, $key, !get_pref($link, $key));
                        $value = get_pref($link, $key);
                }
 
                if ($subop == "setpref") {
-                       header("Content-Type: text/plain");
-
                        $value = str_replace("\n", "<br/>", $_REQUEST['value']);
 
                        $key = db_escape_string($_REQUEST["key"]);
                }
 
                if ($subop == "mark") {
-                       header("Content-Type: text/plain");
-
                        $mark = $_REQUEST["mark"];
                        $id = db_escape_string($_REQUEST["id"]);
 
                }
 
                if ($subop == "delete") {
-                       header("Content-Type: text/plain");
-
                        $ids = db_escape_string($_REQUEST["ids"]);
 
                        $result = db_query($link, "DELETE FROM ttrss_user_entries
                }
 
                if ($subop == "unarchive") {
-                       header("Content-Type: text/plain");
-
                        $ids = db_escape_string($_REQUEST["ids"]);
 
                        $result = db_query($link, "UPDATE ttrss_user_entries
                }
 
                if ($subop == "archive") {
-                       header("Content-Type: text/plain");
-
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
 
                        foreach ($ids as $id) {
                                archive_article($link, $id, $_SESSION["uid"]);
                }
 
                if ($subop == "publ") {
-                       header("Content-Type: text/plain");
-
                        $pub = $_REQUEST["pub"];
                        $id = db_escape_string($_REQUEST["id"]);
                        $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
                                published = $pub
                                WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
 
-                       print json_encode(array("message" => "UPDATE_COUNTERS"));
+                       $pubsub_result = false;
+
+                       if (PUBSUBHUBBUB_HUB) {
+                               $rss_link = get_self_url_prefix() .
+                                       "/backend.php?op=rss&id=-2&key=" .
+                                       get_feed_access_key($link, -2, false);
+
+                               $p = new Publisher(PUBSUBHUBBUB_HUB);
+
+                               $pubsub_result = $p->publish_update($rss_link);
+                       }
+
+                       print json_encode(array("message" => "UPDATE_COUNTERS",
+                               "pubsub_result" => $pubsub_result));
                        return;
                }
 
-/*             if ($subop == "updateFeed") {
+               // Silent
+               /* if ($subop == "update") {
                        $feed_id = db_escape_string($_REQUEST["feed"]);
-
                        update_rss_feed($link, $feed_id);
-
-                       print "<rpc-reply>";
-                       print "<message>UPDATE_COUNTERS</message>";
-                       print "</rpc-reply>";
-
                        return;
                } */
 
                if ($subop == "updateAllFeeds" || $subop == "getAllCounters") {
-
-                       header("Content-Type: text/plain");
-
                        $last_article_id = (int) $_REQUEST["last_article_id"];
 
                        $reply = array();
 
                /* 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"]));
+                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                        $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
                        catchupArticlesById($link, $ids, $cmode);
                }
 
                if ($subop == "markSelected") {
-                       header("Content-Type: text/plain");
-
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                        $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
                        markArticlesById($link, $ids, $cmode);
                }
 
                if ($subop == "publishSelected") {
-                       header("Content-Type: text/plain");
-
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                        $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
                        publishArticlesById($link, $ids, $cmode);
                }
 
                if ($subop == "sanityCheck") {
-                       header("Content-Type: text/plain");
-
                        $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
 
                        $reply = array();
                } */
 
                if ($subop == "setArticleTags") {
-                       header("Content-Type: text/plain");
-
                        global $memcache;
 
                        $id = db_escape_string($_REQUEST["id"]);
 
                        $tags_str = db_escape_string($_REQUEST["tags_str"]);
-                       $tags = array_unique(trim_array(split(",", $tags_str)));
+                       $tags = array_unique(trim_array(explode(",", $tags_str)));
 
                        db_query($link, "BEGIN");
 
 
                                /* update tag cache */
 
+                               sort($tags_to_cache);
                                $tags_str = join(",", $tags_to_cache);
 
                                db_query($link, "UPDATE ttrss_user_entries
                                $memcache->delete($obj_id);
                        }
 
-                       $tags_str = format_tags_string(get_article_tags($link, $id), $id);
+                       $tags = get_article_tags($link, $id);
+                       $tags_str = format_tags_string($tags, $id);
+                       $tags_str_full = join(", ", $tags);
+
+                       if (!$tags_str_full) $tags_str_full = __("no tags");
 
                        print json_encode(array("tags_str" => array("id" => $id,
-                               "content" => $tags_str)));
+                               "content" => $tags_str, "content_full" => $tags_str_full)));
 
                        return;
                }
 
                if ($subop == "regenOPMLKey") {
-                       header("Content-Type: text/plain");
-
                        update_feed_access_key($link, 'OPML:Publish',
                                false, $_SESSION["uid"]);
 
                        return;
                }
 
-               // XML method
-               if ($subop == "logout") {
-                       logout_user();
-                       print_error_xml(6);
-                       return;
-               }
-
                if ($subop == "completeTags") {
-                       header("Content-Type: text/plain");
-
                        $search = db_escape_string($_REQUEST["search"]);
 
                        $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
                }
 
                if ($subop == "purge") {
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                        $days = sprintf("%d", $_REQUEST["days"]);
 
                        foreach ($ids as $id) {
                } */
 
                if ($subop == "getArticles") {
-                       header("Content-Type: text/plain");
-
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                        $articles = array();
 
                        foreach ($ids as $id) {
                }
 
                if ($subop == "checkDate") {
-                       header("Content-Type: text/plain");
-
                        $date = db_escape_string($_REQUEST["date"]);
                        $date_parsed = strtotime($date);
 
-                       print json_encode(array("result" => (bool)$date_parsed));
+                       print json_encode(array("result" => (bool)$date_parsed,
+                               "date" => date("c", $date_parsed)));
                        return;
                }
 
                if ($subop == "assignToLabel" || $subop == "removeFromLabel") {
-                       header("Content-Type: text/plain");
-
                        $reply = array();
 
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $ids = explode(",", db_escape_string($_REQUEST["ids"]));
                        $label_id = db_escape_string($_REQUEST["lid"]);
 
                        $label = db_escape_string(label_find_caption($link, $label_id,
                }
 
                if ($subop == "updateFeedBrowser") {
-                       header("Content-Type: text/plain");
-
                        $search = db_escape_string($_REQUEST["search"]);
                        $limit = db_escape_string($_REQUEST["limit"]);
                        $mode = (int) db_escape_string($_REQUEST["mode"]);
                // Silent
                if ($subop == "massSubscribe") {
 
-                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $payload = json_decode($_REQUEST["payload"], false);
                        $mode = $_REQUEST["mode"];
 
+                       if (!$payload || !is_array($payload)) return;
+
+                       if ($mode == 1) {
+                               foreach ($payload as $feed) {
+
+                                       $title = db_escape_string($feed[0]);
+                                       $feed_url = db_escape_string($feed[1]);
+
+                                       $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
+                                               feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
+
+                                       if (db_num_rows($result) == 0) {
+                                               $result = db_query($link, "INSERT INTO ttrss_feeds
+                                                               (owner_uid,feed_url,title,cat_id,site_url)
+                                                       VALUES ('".$_SESSION["uid"]."',
+                                                               '$feed_url', '$title', NULL, '')");
+                                       }
+                               }
+                       } else if ($mode == 2) {
+                               // feed archive
+                               foreach ($payload as $id) {
+                                       $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
+                                               WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+
+                                       if (db_num_rows($result) != 0) {
+                                               $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
+                                               $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
+                                               $title = db_escape_string(db_fetch_result($result, 0, "title"));
+
+                                               $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
+                                                       feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
+
+                                               if (db_num_rows($result) == 0) {
+                                                       $result = db_query($link, "INSERT INTO ttrss_feeds
+                                                                       (owner_uid,feed_url,title,cat_id,site_url)
+                                                               VALUES ('$id','".$_SESSION["uid"]."',
+                                                                       '$feed_url', '$title', NULL, '$site_url')");
+                                               }
+                                       }
+                               }
+                       }
+
+/*                     $ids = explode(",", db_escape_string($_REQUEST["ids"]));
+
                        $subscribed = array();
 
                        foreach ($ids as $id) {
                                        }
                                        array_push($subscribed, $title_orig);
                                }
-                       }
+                       } */
 
                        return;
                }
 
                if ($subop == "digest-get-contents") {
-                       header("Content-Type: text/plain");
-
                        $article_id = db_escape_string($_REQUEST['article_id']);
 
                        $result = db_query($link, "SELECT content
                }
 
                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 ($subop == "digest-init") {
-                       header("Content-Type: text/plain");
-
                        $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
 
                        $feeds = array();
                }
 
                if ($subop == "sendEmail") {
-                       header("Content-Type: text/plain");
-
                        $secretkey = $_REQUEST['secretkey'];
 
                        $reply = array();
                }
 
                if ($subop == "completeEmails") {
-                       header("Content-Type: text/plain");
-
                        $search = db_escape_string($_REQUEST["search"]);
 
                        print "<ul>";
                }
 
                if ($subop == "quickAddCat") {
-                       header("Content-Type: text/plain");
-
                        $cat = db_escape_string($_REQUEST["cat"]);
 
                        add_feed_category($link, $cat);
                }
 
                if ($subop == "regenFeedKey") {
-                       header("Content-Type: text/plain");
-
                        $feed_id = db_escape_string($_REQUEST['id']);
                        $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
 
                }
 
                if ($subop == "verifyRegexp") {
-                       header("Content-Type: text/plain");
-
                        $reg_exp = $_REQUEST["reg_exp"];
 
                        $status = @preg_match("/$reg_exp/i", "TEST") !== false;
 
                // TODO: unify with digest-get-contents?
                if ($subop == "cdmGetArticle") {
-                       header("Content-Type: text/plain");
-
                        $id = db_escape_string($_REQUEST["id"]);
 
                        $result = db_query($link, "SELECT content,
                }
 
                if ($subop == "scheduleFeedUpdate") {
-                       header("Content-Type: text/plain");
-
                        $feed_id = db_escape_string($_REQUEST["id"]);
                        $is_cat =  db_escape_string($_REQUEST['is_cat']) == 'true';
 
                }
 
                if ($subop == "getTweetInfo") {
-                       header("Content-Type: text/plain");
                        $id = db_escape_string($_REQUEST['id']);
 
                        $result = db_query($link, "SELECT title, link
                }
 
                if ($subop == "setNote") {
-                       header("Content-Type: text/plain");
-
                        $id = db_escape_string($_REQUEST["id"]);
-                       $note = strip_tags(db_escape_string($_REQUEST["note"]));
+                       $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
 
                        db_query($link, "UPDATE ttrss_user_entries SET note = '$note'
                                WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
 
                        $formatted_note = format_article_note($id, $note);
 
-                       print json_encode(array("note" => $formatted_note));
+                       print json_encode(array("note" => $formatted_note,
+                               "raw_length" => mb_strlen($note)));
+                       return;
+               }
+
+               if ($subop == "genHash") {
+                       $hash = sha1(uniqid(rand(), true));
+
+                       print json_encode(array("hash" => $hash));
                        return;
                }
 
-               print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
+               print json_encode(array("error" => array("code" => 7,
+                       "message" => "Unknown method: $subop")));
        }
 ?>