]>
git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
2 function handle_rpc_request($link) {
4 $subop = $_GET["subop"];
6 if ($subop == "setpref") {
13 $key = db_escape_string($_GET["key"]);
14 $value = db_escape_string($_GET["value"]);
16 set_pref($link, $key, $value);
18 print "<param-set key=\"$key\" value=\"$value\"/>";
25 if ($subop == "getLabelCounters") {
29 getLabelCounters($link);
31 getFeedCounter($link, $aid);
39 if ($subop == "getFeedCounters") {
42 getFeedCounters($link);
49 if ($subop == "getAllCounters") {
53 $omode = $_GET["omode"];
55 getAllCounters($link, $omode);
57 print_runtime_info($link);
63 if ($subop == "mark") {
64 $mark = $_GET["mark"];
65 $id = db_escape_string($_GET["id"]);
73 // FIXME this needs collision testing
75 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
76 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
78 print "<rpc-reply><counters>";
79 getGlobalCounters($link);
80 getLabelCounters($link);
81 if (get_pref($link, 'ENABLE_FEED_CATS')) {
82 getCategoryCounters($link);
84 print "</counters></rpc-reply>";
89 if ($subop == "publ") {
91 $id = db_escape_string($_GET["id"]);
99 // FIXME this needs collision testing
101 $result = db_query($link, "UPDATE ttrss_user_entries SET published = $pub
102 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
104 print "<rpc-reply><counters>";
105 getGlobalCounters($link);
106 getLabelCounters($link);
107 if (get_pref($link, 'ENABLE_FEED_CATS')) {
108 getCategoryCounters($link);
110 print "</counters></rpc-reply>";
115 if ($subop == "updateFeed") {
116 $feed_id = db_escape_string($_GET["feed"]);
118 $result = db_query($link,
119 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
120 AND owner_uid = " . $_SESSION["uid"]);
122 if (db_num_rows($result) > 0) {
123 $feed_url = db_fetch_result($result, 0, "feed_url");
124 update_rss_feed($link, $feed_url, $feed_id);
129 getFeedCounter($link, $feed_id);
131 print "</rpc-reply>";
136 if ($subop == "forceUpdateAllFeeds" ||
$subop == "updateAllFeeds") {
138 $global_unread_caller = sprintf("%d", $_GET["uctr"]);
139 $global_unread = getGlobalUnread($link);
145 if ($global_unread_caller != $global_unread) {
147 $omode = $_GET["omode"];
149 if (!$omode) $omode = "tflc";
151 if (strchr($omode, "l")) getLabelCounters($link);
153 if (strchr($omode, "c")) {
154 if (get_pref($link, 'ENABLE_FEED_CATS')) {
155 getCategoryCounters($link);
159 if (strchr($omode, "f")) getFeedCounters($link);
160 if (strchr($omode, "t")) getTagCounters($link);
162 getGlobalCounters($link, $global_unread);
167 print_runtime_info($link);
169 print "</rpc-reply>";
174 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
175 if ($subop == "catchupSelected") {
177 $ids = split(",", db_escape_string($_GET["ids"]));
178 $cmode = sprintf("%d", $_GET["cmode"]);
180 catchupArticlesById($link, $ids, $cmode);
184 getAllCounters($link, $_GET["omode"]);
186 print_runtime_info($link);
187 print "</rpc-reply>";
192 if ($subop == "markSelected") {
194 $ids = split(",", db_escape_string($_GET["ids"]));
195 $cmode = sprintf("%d", $_GET["cmode"]);
197 markArticlesById($link, $ids, $cmode);
201 getAllCounters($link, $_GET["omode"]);
203 print_runtime_info($link);
204 print "</rpc-reply>";
209 if ($subop == "publishSelected") {
211 $ids = split(",", db_escape_string($_GET["ids"]));
212 $cmode = sprintf("%d", $_GET["cmode"]);
214 publishArticlesById($link, $ids, $cmode);
218 getAllCounters($link, $_GET["omode"]);
220 print_runtime_info($link);
221 print "</rpc-reply>";
226 if ($subop == "sanityCheck") {
228 if (sanity_check($link)) {
229 print "<error error-code=\"0\"/>";
230 print_init_params($link);
231 print_runtime_info($link);
233 # assign client-passed params to session
234 $_SESSION["client.userAgent"] = $_GET["ua"];
237 print "</rpc-reply>";
242 if ($subop == "globalPurge") {
245 global_purge_old_posts($link, true);
246 print "</rpc-reply>";
251 if ($subop == "getArticleLink") {
253 $id = db_escape_string($_GET["id"]);
255 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
256 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
258 if (db_num_rows($result) == 1) {
259 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
260 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
262 print "<rpc-reply><error>Article not found</error></rpc-reply>";
268 if ($subop == "setArticleTags") {
270 $id = db_escape_string($_GET["id"]);
272 $tags_str = db_escape_string($_GET["tags_str"]);
274 $tags = array_unique(trim_array(split(",", $tags_str)));
276 db_query($link, "BEGIN");
278 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
279 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
281 if (db_num_rows($result) == 1) {
283 $int_id = db_fetch_result($result, 0, "int_id");
285 db_query($link, "DELETE FROM ttrss_tags WHERE
286 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
288 foreach ($tags as $tag) {
289 $tag = sanitize_tag($tag);
291 if (!tag_is_valid($tag)) {
295 if (preg_match("/^[0-9]*$/", $tag)) {
299 // print "<!-- $id : $int_id : $tag -->";
302 db_query($link, "INSERT INTO ttrss_tags
303 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
308 db_query($link, "COMMIT");
310 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
313 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
319 if ($subop == "regenPubKey") {
323 set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
325 $new_link = article_publish_url($link);
327 print "<link><![CDATA[$new_link]]></link>";
329 print "</rpc-reply>";
334 if ($subop == "logout") {
340 if ($subop == "completeTags") {
342 $search = db_escape_string($_REQUEST["search"]);
344 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
345 WHERE owner_uid = '".$_SESSION["uid"]."' AND
346 tag_name LIKE '$search%' ORDER BY tag_name
350 while ($line = db_fetch_assoc($result)) {
351 print "<li>" . $line["tag_name"] . "</li>";
358 if ($subop == "purge") {
359 $ids = split(",", db_escape_string($_GET["ids"]));
360 $days = sprintf("%d", $_GET["days"]);
364 print "<message><![CDATA[";
366 foreach ($ids as $id) {
368 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
369 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
371 if (db_num_rows($result) == 1) {
372 purge_feed($link, $id, $days, true);
376 print "]]></message>";
378 print "</rpc-reply>";
383 /* if ($subop == "setScore") {
384 $id = db_escape_string($_REQUEST["id"]);
385 $score = sprintf("%d", $_REQUEST["score"]);
387 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
388 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
390 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
396 if ($subop == "getArticles") {
397 $ids = split(",", db_escape_string($_REQUEST["ids"]));
401 foreach ($ids as $id) {
403 outputArticleXML($link, $id, 0, false);
406 print "</rpc-reply>";
411 if ($subop == "checkDate") {
413 $date = db_escape_string($_REQUEST["date"]);
414 $date_parsed = strtotime($date);
419 print "<result>1</result>";
421 print "<result>0</result>";
424 print "</rpc-reply>";
429 if ($subop == "removeFromLabel") {
431 $ids = split(",", db_escape_string($_REQUEST["ids"]));
432 $label_id = db_escape_string($_REQUEST["lid"]);
434 $label = label_find_caption($link, $label_id, $_SESSION["uid"]);
437 print "<info-for-headlines>";
441 foreach ($ids as $id) {
442 label_remove_article($link, $id, $label, $_SESSION["uid"]);
444 print "<entry id=\"$id\"><![CDATA[";
446 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
447 print format_article_labels($labels, $id);
454 print "</info-for-headlines>";
457 getAllCounters($link, $omode);
459 print "</rpc-reply>";
464 if ($subop == "assignToLabel") {
466 $ids = split(",", db_escape_string($_REQUEST["ids"]));
467 $label_id = db_escape_string($_REQUEST["lid"]);
469 $label = label_find_caption($link, $label_id, $_SESSION["uid"]);
473 print "<info-for-headlines>";
477 foreach ($ids as $id) {
478 label_add_article($link, $id, $label, $_SESSION["uid"]);
480 print "<entry id=\"$id\"><![CDATA[";
482 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
483 print format_article_labels($labels, $id);
490 print "</info-for-headlines>";
493 getAllCounters($link, $omode);
495 print "</rpc-reply>";
500 if ($subop == "feedBrowser") {
502 $search = db_escape_string($_REQUEST["search"]);
503 $limit = db_escape_string($_REQUEST["limit"]);
508 $ctr = print_feed_browser($link, $search, $limit);
511 print "<num-results value=\"$ctr\"/>";
512 print "</rpc-reply>";
517 if ($subop == "download") {
518 $stage = (int) $_REQUEST["stage"];
519 $cidt = db_escape_string($_REQUEST["cidt"]);
520 $cidb = db_escape_string($_REQUEST["cidb"]);
521 $sync = db_escape_string($_REQUEST["sync"]);
522 //$amount = (int) $_REQUEST["amount"];
523 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
524 //if (!$amount) $amount = 50;
531 $sync = split(";", $sync);
535 if (count($sync) > 0) {
536 if (strtotime($sync[0])) {
537 $last_online = db_escape_string($sync[0]);
539 print "<sync-point><![CDATA[$last_online]]></sync-point>";
541 for ($i = 1; $i < count($sync); $i++
) {
542 $e = split(",", $sync[$i]);
544 if (count($e) == 3) {
547 $unread = bool_to_sql_bool((bool) $e[1]);
548 $marked = bool_to_sql_bool((bool) $e[2]);
550 /* Marked status is not synchronized yet */
552 $query = "UPDATE ttrss_user_entries SET
554 last_read = '$last_online'
555 WHERE ref_id = '$id' AND
556 (last_read IS NULL OR last_read < '$last_online') AND
557 owner_uid = ".$_SESSION["uid"];
559 $result = db_query($link, $query);
561 // if (db_affected_rows($result) > 0) {
562 print "<sync-ok id=\"$id\"/>";
568 /* Maybe we need to further update local DB for this client */
570 $query = "SELECT ref_id,unread FROM ttrss_user_entries
571 WHERE last_read >= '$last_online' AND
572 owner_uid = ".$_SESSION["uid"];
574 $result = db_query($link, $query);
576 while ($line = db_fetch_assoc($result)) {
577 print "<sync-ok id=\"".$line["ref_id"]."\"/>";
588 $result = db_query($link, "SELECT id, title, cat_id FROM
589 ttrss_feeds WHERE hidden = false AND owner_uid = ".$_SESSION["uid"]);
591 while ($line = db_fetch_assoc($result)) {
593 $has_icon = (int) feed_has_icon($line["id"]);
595 print "<feed has_icon=\"$has_icon\"
596 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
597 print $line["title"];
603 print "<feed-categories>";
605 $result = db_query($link, "SELECT id, title, collapsed FROM
606 ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
608 print "<category id=\"0\" collapsed=\"".
609 (int)$_COOKIE["ttrss_vf_uclps"]."\"><![CDATA[";
610 print __("Uncategorized");
611 print "]]></category>";
613 print "<category id=\"-1\" collapsed=\"".
614 (int)$_COOKIE["ttrss_vf_vclps"]."\"><![CDATA[";
616 print "]]></category>";
618 print "<category id=\"-2\" collapsed=\"".
619 (int)$_COOKIE["ttrss_vf_lclps"]."\"><![CDATA[";
621 print "]]></category>";
623 while ($line = db_fetch_assoc($result)) {
625 id=\"".$line["id"]."\"
626 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
627 print $line["title"];
628 print "]]></category>";
631 print "</feed-categories>";
635 $result = db_query($link, "SELECT * FROM
636 ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
638 while ($line = db_fetch_assoc($result)) {
640 id=\"".$line["id"]."\"
641 fg_color=\"".$line["fg_color"]."\"
642 bg_color=\"".$line["bg_color"]."\"
644 print $line["caption"];
657 $skip = $limit*($stage-1);
659 print "<limit value=\"$limit\"/>";
661 if ($amount > 0) $amount -= $skip;
665 $limit = min($limit, $amount);
668 $unread_qpart = "(unread = true OR marked = true) AND ";
671 if ($cidt && $cidb) {
672 $cid_qpart = "(id > $cidt OR id < $cidb) AND ";
675 if (DB_TYPE
== "pgsql") {
676 $date_qpart = "updated >= NOW() - INTERVAL '1 month' AND";
678 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 MONTH) AND";
681 $result = db_query($link,
682 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
684 feed_id,content,updated,unread,marked FROM
685 ttrss_user_entries,ttrss_entries,ttrss_feeds
686 WHERE $unread_qpart $cid_qpart $date_qpart
688 ttrss_feeds.id = feed_id AND
689 ref_id = ttrss_entries.id AND
690 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
691 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
693 if (function_exists('json_encode')) {
695 while ($line = db_fetch_assoc($result)) {
696 print "<article><![CDATA[";
698 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
699 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
701 $line["labels"] = get_article_labels($link, $line["id"]);
704 // $line["tags"] = format_tags_string(
705 // get_article_tags($link, $line["id"]), $line["id"]);
707 print json_encode($line);
708 print "]]></article>";
718 print "</rpc-reply>";
723 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";