]>
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") {
90 $pub = $_REQUEST["pub"];
91 $id = db_escape_string($_REQUEST["id"]);
92 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
100 if ($note != 'undefined') {
101 $note_qpart = "note = '$note',";
104 // FIXME this needs collision testing
106 $result = db_query($link, "UPDATE ttrss_user_entries SET
109 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
115 getGlobalCounters($link);
116 getLabelCounters($link);
117 if (get_pref($link, 'ENABLE_FEED_CATS')) {
118 getCategoryCounters($link);
122 if ($note != 'undefined') {
123 $note_size = strlen($note);
124 print "<note id=\"$id\" size=\"$note_size\">";
125 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
129 print "</rpc-reply>";
134 if ($subop == "updateFeed") {
135 $feed_id = db_escape_string($_GET["feed"]);
137 $result = db_query($link,
138 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
139 AND owner_uid = " . $_SESSION["uid"]);
141 if (db_num_rows($result) > 0) {
142 $feed_url = db_fetch_result($result, 0, "feed_url");
143 update_rss_feed($link, $feed_url, $feed_id);
148 getFeedCounter($link, $feed_id);
150 print "</rpc-reply>";
155 if ($subop == "forceUpdateAllFeeds" ||
$subop == "updateAllFeeds") {
157 $global_unread_caller = sprintf("%d", $_GET["uctr"]);
158 $global_unread = getGlobalUnread($link);
164 if ($global_unread_caller != $global_unread) {
166 $omode = $_GET["omode"];
168 if (!$omode) $omode = "tflc";
170 if (strchr($omode, "l")) getLabelCounters($link);
172 if (strchr($omode, "c")) {
173 if (get_pref($link, 'ENABLE_FEED_CATS')) {
174 getCategoryCounters($link);
178 if (strchr($omode, "f")) getFeedCounters($link);
179 if (strchr($omode, "t")) getTagCounters($link);
181 getGlobalCounters($link, $global_unread);
186 print_runtime_info($link);
188 print "</rpc-reply>";
193 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
194 if ($subop == "catchupSelected") {
196 $ids = split(",", db_escape_string($_GET["ids"]));
197 $cmode = sprintf("%d", $_GET["cmode"]);
199 catchupArticlesById($link, $ids, $cmode);
203 getAllCounters($link, $_GET["omode"]);
205 print_runtime_info($link);
206 print "</rpc-reply>";
211 if ($subop == "markSelected") {
213 $ids = split(",", db_escape_string($_GET["ids"]));
214 $cmode = sprintf("%d", $_GET["cmode"]);
216 markArticlesById($link, $ids, $cmode);
220 getAllCounters($link, $_GET["omode"]);
222 print_runtime_info($link);
223 print "</rpc-reply>";
228 if ($subop == "publishSelected") {
230 $ids = split(",", db_escape_string($_GET["ids"]));
231 $cmode = sprintf("%d", $_GET["cmode"]);
233 publishArticlesById($link, $ids, $cmode);
237 getAllCounters($link, $_GET["omode"]);
239 print_runtime_info($link);
240 print "</rpc-reply>";
245 if ($subop == "sanityCheck") {
247 if (sanity_check($link)) {
248 print "<error error-code=\"0\"/>";
249 print_init_params($link);
250 print_runtime_info($link);
252 # assign client-passed params to session
253 $_SESSION["client.userAgent"] = $_GET["ua"];
256 print "</rpc-reply>";
261 if ($subop == "globalPurge") {
264 global_purge_old_posts($link, true);
265 print "</rpc-reply>";
270 if ($subop == "getArticleLink") {
272 $id = db_escape_string($_GET["id"]);
274 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
275 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
277 if (db_num_rows($result) == 1) {
278 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
279 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
281 print "<rpc-reply><error>Article not found</error></rpc-reply>";
287 if ($subop == "setArticleTags") {
289 $id = db_escape_string($_GET["id"]);
291 $tags_str = db_escape_string($_GET["tags_str"]);
293 $tags = array_unique(trim_array(split(",", $tags_str)));
295 db_query($link, "BEGIN");
297 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
298 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
300 if (db_num_rows($result) == 1) {
302 $int_id = db_fetch_result($result, 0, "int_id");
304 db_query($link, "DELETE FROM ttrss_tags WHERE
305 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
307 foreach ($tags as $tag) {
308 $tag = sanitize_tag($tag);
310 if (!tag_is_valid($tag)) {
314 if (preg_match("/^[0-9]*$/", $tag)) {
318 // print "<!-- $id : $int_id : $tag -->";
321 db_query($link, "INSERT INTO ttrss_tags
322 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
327 db_query($link, "COMMIT");
329 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
332 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
338 if ($subop == "regenPubKey") {
342 set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
344 $new_link = article_publish_url($link);
346 print "<link><![CDATA[$new_link]]></link>";
348 print "</rpc-reply>";
353 if ($subop == "logout") {
359 if ($subop == "completeTags") {
361 $search = db_escape_string($_REQUEST["search"]);
363 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
364 WHERE owner_uid = '".$_SESSION["uid"]."' AND
365 tag_name LIKE '$search%' ORDER BY tag_name
369 while ($line = db_fetch_assoc($result)) {
370 print "<li>" . $line["tag_name"] . "</li>";
377 if ($subop == "purge") {
378 $ids = split(",", db_escape_string($_GET["ids"]));
379 $days = sprintf("%d", $_GET["days"]);
383 print "<message><![CDATA[";
385 foreach ($ids as $id) {
387 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
388 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
390 if (db_num_rows($result) == 1) {
391 purge_feed($link, $id, $days, true);
395 print "]]></message>";
397 print "</rpc-reply>";
402 /* if ($subop == "setScore") {
403 $id = db_escape_string($_REQUEST["id"]);
404 $score = sprintf("%d", $_REQUEST["score"]);
406 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
407 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
409 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
415 if ($subop == "getArticles") {
416 $ids = split(",", db_escape_string($_REQUEST["ids"]));
420 foreach ($ids as $id) {
422 outputArticleXML($link, $id, 0, false);
425 print "</rpc-reply>";
430 if ($subop == "checkDate") {
432 $date = db_escape_string($_REQUEST["date"]);
433 $date_parsed = strtotime($date);
438 print "<result>1</result>";
440 print "<result>0</result>";
443 print "</rpc-reply>";
448 if ($subop == "removeFromLabel") {
450 $ids = split(",", db_escape_string($_REQUEST["ids"]));
451 $label_id = db_escape_string($_REQUEST["lid"]);
453 $label = label_find_caption($link, $label_id, $_SESSION["uid"]);
456 print "<info-for-headlines>";
460 foreach ($ids as $id) {
461 label_remove_article($link, $id, $label, $_SESSION["uid"]);
463 print "<entry id=\"$id\"><![CDATA[";
465 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
466 print format_article_labels($labels, $id);
473 print "</info-for-headlines>";
476 getAllCounters($link, $omode);
478 print "</rpc-reply>";
483 if ($subop == "assignToLabel") {
485 $ids = split(",", db_escape_string($_REQUEST["ids"]));
486 $label_id = db_escape_string($_REQUEST["lid"]);
488 $label = label_find_caption($link, $label_id, $_SESSION["uid"]);
492 print "<info-for-headlines>";
496 foreach ($ids as $id) {
497 label_add_article($link, $id, $label, $_SESSION["uid"]);
499 print "<entry id=\"$id\"><![CDATA[";
501 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
502 print format_article_labels($labels, $id);
509 print "</info-for-headlines>";
512 getAllCounters($link, $omode);
514 print "</rpc-reply>";
519 if ($subop == "feedBrowser") {
521 $search = db_escape_string($_REQUEST["search"]);
522 $limit = db_escape_string($_REQUEST["limit"]);
527 $ctr = print_feed_browser($link, $search, $limit);
530 print "<num-results value=\"$ctr\"/>";
531 print "</rpc-reply>";
536 if ($subop == "download") {
537 $stage = (int) $_REQUEST["stage"];
538 $cidt = (int)db_escape_string($_REQUEST["cidt"]);
539 $cidb = (int)db_escape_string($_REQUEST["cidb"]);
540 $sync = db_escape_string($_REQUEST["sync"]);
541 //$amount = (int) $_REQUEST["amount"];
542 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
543 //if (!$amount) $amount = 50;
550 $sync = split(";", $sync);
554 if (count($sync) > 0) {
555 if (strtotime($sync[0])) {
556 $last_online = db_escape_string($sync[0]);
558 print "<sync-point><![CDATA[$last_online]]></sync-point>";
560 for ($i = 1; $i < count($sync); $i++
) {
561 $e = split(",", $sync[$i]);
563 if (count($e) == 3) {
566 $unread = bool_to_sql_bool((bool) $e[1]);
567 $marked = (bool)$e[2];
570 $marked = bool_to_sql_bool($marked);
571 $marked_qpart = "marked = $marked,";
574 $query = "UPDATE ttrss_user_entries SET
577 last_read = '$last_online'
578 WHERE ref_id = '$id' AND
579 (last_read IS NULL OR last_read < '$last_online') AND
580 owner_uid = ".$_SESSION["uid"];
582 $result = db_query($link, $query);
584 print "<sync-ok id=\"$id\"/>";
589 /* Maybe we need to further update local DB for this client */
591 $query = "SELECT ref_id,unread,marked FROM ttrss_user_entries
592 WHERE last_read >= '$last_online' AND
593 owner_uid = ".$_SESSION["uid"] . " LIMIT 1000";
595 $result = db_query($link, $query);
597 while ($line = db_fetch_assoc($result)) {
598 $unread = (int) sql_bool_to_bool($line["unread"]);
599 $marked = (int) sql_bool_to_bool($line["marked"]);
601 print "<sync-ok unread=\"$unread\" marked=\"$marked\"
602 id=\"".$line["ref_id"]."\"/>";
613 $result = db_query($link, "SELECT id, title, cat_id FROM
614 ttrss_feeds WHERE hidden = false AND owner_uid = ".$_SESSION["uid"]);
616 while ($line = db_fetch_assoc($result)) {
618 $has_icon = (int) feed_has_icon($line["id"]);
620 print "<feed has_icon=\"$has_icon\"
621 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
622 print $line["title"];
628 print "<feed-categories>";
630 $result = db_query($link, "SELECT id, title, collapsed FROM
631 ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
633 print "<category id=\"0\" collapsed=\"".
634 (int)$_COOKIE["ttrss_vf_uclps"]."\"><![CDATA[";
635 print __("Uncategorized");
636 print "]]></category>";
638 print "<category id=\"-1\" collapsed=\"".
639 (int)$_COOKIE["ttrss_vf_vclps"]."\"><![CDATA[";
641 print "]]></category>";
643 print "<category id=\"-2\" collapsed=\"".
644 (int)$_COOKIE["ttrss_vf_lclps"]."\"><![CDATA[";
646 print "]]></category>";
648 while ($line = db_fetch_assoc($result)) {
650 id=\"".$line["id"]."\"
651 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
652 print $line["title"];
653 print "]]></category>";
656 print "</feed-categories>";
660 $result = db_query($link, "SELECT * FROM
661 ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
663 while ($line = db_fetch_assoc($result)) {
665 id=\"".$line["id"]."\"
666 fg_color=\"".$line["fg_color"]."\"
667 bg_color=\"".$line["bg_color"]."\"
669 print $line["caption"];
682 $skip = $limit*($stage-1);
684 print "<limit value=\"$limit\"/>";
686 if ($amount > 0) $amount -= $skip;
690 $limit = min($limit, $amount);
693 $unread_qpart = "(unread = true OR marked = true) AND ";
696 if ($cidt && $cidb) {
697 $cid_qpart = "(ttrss_entries.id > $cidt OR ttrss_entries.id < $cidb) AND ";
700 if (DB_TYPE
== "pgsql") {
701 $date_qpart = "updated >= NOW() - INTERVAL '1 month' AND";
703 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 MONTH) AND";
706 $result = db_query($link,
707 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
709 feed_id,content,updated,unread,marked FROM
710 ttrss_user_entries,ttrss_entries,ttrss_feeds
711 WHERE $unread_qpart $cid_qpart $date_qpart
713 ttrss_feeds.id = feed_id AND
714 ref_id = ttrss_entries.id AND
715 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
716 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
718 if (function_exists('json_encode')) {
720 while ($line = db_fetch_assoc($result)) {
721 print "<article><![CDATA[";
723 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
724 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
726 $line["labels"] = get_article_labels($link, $line["id"]);
729 // $line["tags"] = format_tags_string(
730 // get_article_tags($link, $line["id"]), $line["id"]);
732 print json_encode($line);
733 print "]]></article>";
743 print "</rpc-reply>";
748 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";