]>
git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
2 function handle_rpc_request($link) {
4 $subop = $_REQUEST["subop"];
6 if ($subop == "addfeed") {
8 $feed = db_escape_string($_REQUEST['feed']);
9 $cat = db_escape_string($_REQUEST['cat']);
10 $login = db_escape_string($_REQUEST['login']);
11 $pass = db_escape_string($_REQUEST['pass']);
13 $rc = subscribe_to_feed($link, $feed, $cat, $login, $pass);
16 print "<result code='$rc'/>";
23 if ($subop == "setpref") {
30 $key = db_escape_string($_REQUEST["key"]);
31 $value = db_escape_string($_REQUEST["value"]);
33 set_pref($link, $key, $value);
35 print "<param-set key=\"$key\" value=\"$value\"/>";
42 if ($subop == "getAllCounters") {
46 $omode = $_REQUEST["omode"];
48 getAllCounters($link, $omode);
50 print_runtime_info($link);
56 if ($subop == "mark") {
57 $mark = $_REQUEST["mark"];
58 $id = db_escape_string($_REQUEST["id"]);
66 // FIXME this needs collision testing
68 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
69 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
71 print "<rpc-reply><counters>";
72 getGlobalCounters($link);
73 getVirtCounters($link);
74 getLabelCounters($link);
75 if (get_pref($link, 'ENABLE_FEED_CATS')) {
76 getCategoryCounters($link);
78 print "</counters></rpc-reply>";
83 if ($subop == "delete") {
84 $ids = db_escape_string($_REQUEST["ids"]);
86 $result = db_query($link, "DELETE FROM ttrss_user_entries
87 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
89 print "<rpc-reply><counters>";
90 getGlobalCounters($link);
91 getVirtCounters($link);
92 if (get_pref($link, 'ENABLE_FEED_CATS')) {
93 getCategoryCounters($link);
95 print "</counters></rpc-reply>";
100 if ($subop == "unarchive") {
101 $ids = db_escape_string($_REQUEST["ids"]);
103 $result = db_query($link, "UPDATE ttrss_user_entries
104 SET feed_id = orig_feed_id, orig_feed_id = NULL
105 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
107 print "<rpc-reply><counters>";
108 getGlobalCounters($link);
109 getVirtCounters($link);
110 if (get_pref($link, 'ENABLE_FEED_CATS')) {
111 getCategoryCounters($link);
113 print "</counters></rpc-reply>";
118 if ($subop == "archive") {
119 $ids = split(",", db_escape_string($_REQUEST["ids"]));
121 foreach ($ids as $id) {
122 archive_article($link, $id, $_SESSION["uid"]);
125 print "<rpc-reply><counters>";
126 getGlobalCounters($link);
127 getVirtCounters($link);
128 if (get_pref($link, 'ENABLE_FEED_CATS')) {
129 getCategoryCounters($link);
131 print "</counters></rpc-reply>";
137 if ($subop == "publ") {
138 $pub = $_REQUEST["pub"];
139 $id = db_escape_string($_REQUEST["id"]);
140 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
148 if ($note != 'undefined') {
149 $note_qpart = "note = '$note',";
152 // FIXME this needs collision testing
154 $result = db_query($link, "UPDATE ttrss_user_entries SET
157 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
163 getGlobalCounters($link);
164 getVirtCounters($link);
165 getLabelCounters($link);
166 if (get_pref($link, 'ENABLE_FEED_CATS')) {
167 getCategoryCounters($link);
171 if ($note != 'undefined') {
172 $note_size = strlen($note);
173 print "<note id=\"$id\" size=\"$note_size\">";
174 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
178 print "</rpc-reply>";
183 if ($subop == "updateFeed") {
184 $feed_id = db_escape_string($_REQUEST["feed"]);
186 $result = db_query($link,
187 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
188 AND owner_uid = " . $_SESSION["uid"]);
190 if (db_num_rows($result) > 0) {
191 $feed_url = db_fetch_result($result, 0, "feed_url");
192 update_rss_feed($link, $feed_url, $feed_id);
197 getFeedCounter($link, $feed_id);
199 print "</rpc-reply>";
204 if ($subop == "forceUpdateAllFeeds" ||
$subop == "updateAllFeeds") {
206 $global_unread_caller = sprintf("%d", $_REQUEST["uctr"]);
207 $global_unread = getGlobalUnread($link);
213 if ($global_unread_caller != $global_unread) {
215 $omode = $_REQUEST["omode"];
217 if (!$omode) $omode = "tflc";
219 getVirtCounters($link);
221 if (strchr($omode, "l")) getLabelCounters($link);
223 if (strchr($omode, "c")) {
224 if (get_pref($link, 'ENABLE_FEED_CATS')) {
225 getCategoryCounters($link);
229 if (strchr($omode, "f")) getFeedCounters($link);
230 if (strchr($omode, "t")) getTagCounters($link);
232 getGlobalCounters($link, $global_unread);
237 print_runtime_info($link);
239 print "</rpc-reply>";
244 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
245 if ($subop == "catchupSelected") {
247 $ids = split(",", db_escape_string($_REQUEST["ids"]));
248 $cmode = sprintf("%d", $_REQUEST["cmode"]);
250 catchupArticlesById($link, $ids, $cmode);
254 getAllCounters($link, $_REQUEST["omode"]);
256 print_runtime_info($link);
257 print "</rpc-reply>";
262 if ($subop == "markSelected") {
264 $ids = split(",", db_escape_string($_REQUEST["ids"]));
265 $cmode = sprintf("%d", $_REQUEST["cmode"]);
267 markArticlesById($link, $ids, $cmode);
271 getAllCounters($link, $_REQUEST["omode"]);
273 print_runtime_info($link);
274 print "</rpc-reply>";
279 if ($subop == "publishSelected") {
281 $ids = split(",", db_escape_string($_REQUEST["ids"]));
282 $cmode = sprintf("%d", $_REQUEST["cmode"]);
284 publishArticlesById($link, $ids, $cmode);
288 getAllCounters($link, $_REQUEST["omode"]);
290 print_runtime_info($link);
291 print "</rpc-reply>";
296 if ($subop == "sanityCheck") {
298 if (sanity_check($link)) {
299 print "<error error-code=\"0\"/>";
300 print_init_params($link);
301 print_runtime_info($link);
303 # assign client-passed params to session
304 $_SESSION["client.userAgent"] = $_REQUEST["ua"];
307 print "</rpc-reply>";
312 if ($subop == "globalPurge") {
315 global_purge_old_posts($link, true);
316 print "</rpc-reply>";
321 if ($subop == "getArticleLink") {
323 $id = db_escape_string($_REQUEST["id"]);
325 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
326 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
328 if (db_num_rows($result) == 1) {
329 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
330 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
332 print "<rpc-reply><error>Article not found</error></rpc-reply>";
338 if ($subop == "setArticleTags") {
340 $id = db_escape_string($_REQUEST["id"]);
342 $tags_str = db_escape_string($_REQUEST["tags_str"]);
344 $tags = array_unique(trim_array(split(",", $tags_str)));
346 db_query($link, "BEGIN");
348 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
349 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
351 if (db_num_rows($result) == 1) {
353 $int_id = db_fetch_result($result, 0, "int_id");
355 db_query($link, "DELETE FROM ttrss_tags WHERE
356 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
358 foreach ($tags as $tag) {
359 $tag = sanitize_tag($tag);
361 if (!tag_is_valid($tag)) {
365 if (preg_match("/^[0-9]*$/", $tag)) {
369 // print "<!-- $id : $int_id : $tag -->";
372 db_query($link, "INSERT INTO ttrss_tags
373 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
378 db_query($link, "COMMIT");
380 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
383 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
389 if ($subop == "regenPubKey") {
393 set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
395 $new_link = article_publish_url($link);
397 print "<link><![CDATA[$new_link]]></link>";
399 print "</rpc-reply>";
404 if ($subop == "logout") {
410 if ($subop == "completeTags") {
412 $search = db_escape_string($_REQUEST["search"]);
414 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
415 WHERE owner_uid = '".$_SESSION["uid"]."' AND
416 tag_name LIKE '$search%' ORDER BY tag_name
420 while ($line = db_fetch_assoc($result)) {
421 print "<li>" . $line["tag_name"] . "</li>";
428 if ($subop == "purge") {
429 $ids = split(",", db_escape_string($_REQUEST["ids"]));
430 $days = sprintf("%d", $_REQUEST["days"]);
434 print "<message><![CDATA[";
436 foreach ($ids as $id) {
438 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
439 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
441 if (db_num_rows($result) == 1) {
442 purge_feed($link, $id, $days, true);
446 print "]]></message>";
448 print "</rpc-reply>";
453 /* if ($subop == "setScore") {
454 $id = db_escape_string($_REQUEST["id"]);
455 $score = sprintf("%d", $_REQUEST["score"]);
457 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
458 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
460 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
466 if ($subop == "getArticles") {
467 $ids = split(",", db_escape_string($_REQUEST["ids"]));
471 foreach ($ids as $id) {
473 outputArticleXML($link, $id, 0, false);
476 print "</rpc-reply>";
481 if ($subop == "checkDate") {
483 $date = db_escape_string($_REQUEST["date"]);
484 $date_parsed = strtotime($date);
489 print "<result>1</result>";
491 print "<result>0</result>";
494 print "</rpc-reply>";
499 if ($subop == "removeFromLabel") {
501 $ids = split(",", db_escape_string($_REQUEST["ids"]));
502 $label_id = db_escape_string($_REQUEST["lid"]);
504 $label = db_escape_string(label_find_caption($link, $label_id,
508 print "<info-for-headlines>";
512 foreach ($ids as $id) {
513 label_remove_article($link, $id, $label, $_SESSION["uid"]);
515 print "<entry id=\"$id\"><![CDATA[";
517 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
518 print format_article_labels($labels, $id);
525 print "</info-for-headlines>";
528 getAllCounters($link, $omode);
530 print "</rpc-reply>";
535 if ($subop == "assignToLabel") {
537 $ids = split(",", db_escape_string($_REQUEST["ids"]));
538 $label_id = db_escape_string($_REQUEST["lid"]);
540 $label = db_escape_string(label_find_caption($link, $label_id,
545 print "<info-for-headlines>";
549 foreach ($ids as $id) {
550 label_add_article($link, $id, $label, $_SESSION["uid"]);
552 print "<entry id=\"$id\"><![CDATA[";
554 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
555 print format_article_labels($labels, $id);
562 print "</info-for-headlines>";
565 getAllCounters($link, $omode);
567 print "</rpc-reply>";
572 if ($subop == "feedBrowser") {
574 $search = db_escape_string($_REQUEST["search"]);
575 $limit = db_escape_string($_REQUEST["limit"]);
576 $mode = db_escape_string($_REQUEST["mode"]);
581 $ctr = print_feed_browser($link, $search, $limit, $mode);
584 print "<num-results value=\"$ctr\"/>";
585 print "</rpc-reply>";
590 if ($subop == "download") {
591 $stage = (int) $_REQUEST["stage"];
592 $cidt = (int)db_escape_string($_REQUEST["cidt"]);
593 $cidb = (int)db_escape_string($_REQUEST["cidb"]);
594 $sync = db_escape_string($_REQUEST["sync"]);
595 //$amount = (int) $_REQUEST["amount"];
596 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
597 //if (!$amount) $amount = 50;
599 /* Amount is not used by the frontend offline.js anymore, it goes by
600 * date_qpart below + cidb/cidt IDs */
607 $sync = split(";", $sync);
611 if (count($sync) > 0) {
612 if (strtotime($sync[0])) {
613 $last_online = db_escape_string($sync[0]);
615 print "<sync-point><![CDATA[$last_online]]></sync-point>";
617 for ($i = 1; $i < count($sync); $i++
) {
618 $e = split(",", $sync[$i]);
620 if (count($e) == 3) {
623 $unread = bool_to_sql_bool((bool) $e[1]);
624 $marked = (bool)$e[2];
627 $marked = bool_to_sql_bool($marked);
628 $marked_qpart = "marked = $marked,";
631 $query = "UPDATE ttrss_user_entries SET
634 last_read = '$last_online'
635 WHERE ref_id = '$id' AND
636 (last_read IS NULL OR last_read < '$last_online') AND
637 owner_uid = ".$_SESSION["uid"];
639 $result = db_query($link, $query);
641 print "<sync-ok id=\"$id\"/>";
646 /* Maybe we need to further update local DB for this client */
648 $query = "SELECT ref_id,unread,marked FROM ttrss_user_entries
649 WHERE last_read >= '$last_online' AND
650 owner_uid = ".$_SESSION["uid"] . " LIMIT 1000";
652 $result = db_query($link, $query);
654 while ($line = db_fetch_assoc($result)) {
655 $unread = (int) sql_bool_to_bool($line["unread"]);
656 $marked = (int) sql_bool_to_bool($line["marked"]);
658 print "<sync-ok unread=\"$unread\" marked=\"$marked\"
659 id=\"".$line["ref_id"]."\"/>";
670 $result = db_query($link, "SELECT id, title, cat_id FROM
671 ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
673 while ($line = db_fetch_assoc($result)) {
675 $has_icon = (int) feed_has_icon($line["id"]);
677 print "<feed has_icon=\"$has_icon\"
678 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
679 print $line["title"];
685 print "<feed-categories>";
687 $result = db_query($link, "SELECT id, title, collapsed FROM
688 ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
690 print "<category id=\"0\" collapsed=\"".
691 (int)get_pref($link, "_COLLAPSED_UNCAT")."\"><![CDATA[";
692 print __("Uncategorized");
693 print "]]></category>";
695 print "<category id=\"-1\" collapsed=\"".
696 (int)get_pref($link, "_COLLAPSED_SPECIAL")."\"><![CDATA[";
698 print "]]></category>";
700 print "<category id=\"-2\" collapsed=\"".
701 (int)get_pref($link, "_COLLAPSED_LABELS")."\"><![CDATA[";
703 print "]]></category>";
705 while ($line = db_fetch_assoc($result)) {
707 id=\"".$line["id"]."\"
708 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
709 print $line["title"];
710 print "]]></category>";
713 print "</feed-categories>";
717 $result = db_query($link, "SELECT * FROM
718 ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
720 while ($line = db_fetch_assoc($result)) {
722 id=\"".$line["id"]."\"
723 fg_color=\"".$line["fg_color"]."\"
724 bg_color=\"".$line["bg_color"]."\"
726 print $line["caption"];
739 $skip = $limit*($stage-1);
741 print "<limit value=\"$limit\"/>";
743 if ($amount > 0) $amount -= $skip;
747 $limit = min($limit, $amount);
750 $unread_qpart = "(unread = true OR marked = true) AND ";
753 if ($cidt && $cidb) {
754 $cid_qpart = "(ttrss_entries.id > $cidt OR ttrss_entries.id < $cidb) AND ";
757 if (DB_TYPE
== "pgsql") {
758 $date_qpart = "updated >= NOW() - INTERVAL '1 week' AND";
760 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 WEEK) AND";
763 $result = db_query($link,
764 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
766 feed_id,content,updated,unread,marked FROM
767 ttrss_user_entries,ttrss_entries,ttrss_feeds
768 WHERE $unread_qpart $cid_qpart $date_qpart
769 ttrss_feeds.id = feed_id AND
770 ref_id = ttrss_entries.id AND
771 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
772 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
774 if (function_exists('json_encode')) {
776 while ($line = db_fetch_assoc($result)) {
777 print "<article><![CDATA[";
779 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
780 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
782 $line["labels"] = get_article_labels($link, $line["id"]);
785 // $line["tags"] = format_tags_string(
786 // get_article_tags($link, $line["id"]), $line["id"]);
788 print json_encode($line);
789 print "]]></article>";
799 print "</rpc-reply>";
804 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";