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($_REQUEST["ids"]));
197 $cmode = sprintf("%d", $_REQUEST["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 = db_escape_string(label_find_caption($link, $label_id,
457 print "<info-for-headlines>";
461 foreach ($ids as $id) {
462 label_remove_article($link, $id, $label, $_SESSION["uid"]);
464 print "<entry id=\"$id\"><![CDATA[";
466 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
467 print format_article_labels($labels, $id);
474 print "</info-for-headlines>";
477 getAllCounters($link, $omode);
479 print "</rpc-reply>";
484 if ($subop == "assignToLabel") {
486 $ids = split(",", db_escape_string($_REQUEST["ids"]));
487 $label_id = db_escape_string($_REQUEST["lid"]);
489 $label = db_escape_string(label_find_caption($link, $label_id,
494 print "<info-for-headlines>";
498 foreach ($ids as $id) {
499 label_add_article($link, $id, $label, $_SESSION["uid"]);
501 print "<entry id=\"$id\"><![CDATA[";
503 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
504 print format_article_labels($labels, $id);
511 print "</info-for-headlines>";
514 getAllCounters($link, $omode);
516 print "</rpc-reply>";
521 if ($subop == "feedBrowser") {
523 $search = db_escape_string($_REQUEST["search"]);
524 $limit = db_escape_string($_REQUEST["limit"]);
529 $ctr = print_feed_browser($link, $search, $limit);
532 print "<num-results value=\"$ctr\"/>";
533 print "</rpc-reply>";
538 if ($subop == "download") {
539 $stage = (int) $_REQUEST["stage"];
540 $cidt = (int)db_escape_string($_REQUEST["cidt"]);
541 $cidb = (int)db_escape_string($_REQUEST["cidb"]);
542 $sync = db_escape_string($_REQUEST["sync"]);
543 //$amount = (int) $_REQUEST["amount"];
544 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
545 //if (!$amount) $amount = 50;
552 $sync = split(";", $sync);
556 if (count($sync) > 0) {
557 if (strtotime($sync[0])) {
558 $last_online = db_escape_string($sync[0]);
560 print "<sync-point><![CDATA[$last_online]]></sync-point>";
562 for ($i = 1; $i < count($sync); $i++) {
563 $e = split(",", $sync[$i]);
565 if (count($e) == 3) {
568 $unread = bool_to_sql_bool((bool) $e[1]);
569 $marked = (bool)$e[2];
572 $marked = bool_to_sql_bool($marked);
573 $marked_qpart = "marked = $marked,";
576 $query = "UPDATE ttrss_user_entries SET
579 last_read = '$last_online'
580 WHERE ref_id = '$id' AND
581 (last_read IS NULL OR last_read < '$last_online') AND
582 owner_uid = ".$_SESSION["uid"];
584 $result = db_query($link, $query);
586 print "<sync-ok id=\"$id\"/>";
591 /* Maybe we need to further update local DB for this client */
593 $query = "SELECT ref_id,unread,marked FROM ttrss_user_entries
594 WHERE last_read >= '$last_online' AND
595 owner_uid = ".$_SESSION["uid"] . " LIMIT 1000";
597 $result = db_query($link, $query);
599 while ($line = db_fetch_assoc($result)) {
600 $unread = (int) sql_bool_to_bool($line["unread"]);
601 $marked = (int) sql_bool_to_bool($line["marked"]);
603 print "<sync-ok unread=\"$unread\" marked=\"$marked\"
604 id=\"".$line["ref_id"]."\"/>";
615 $result = db_query($link, "SELECT id, title, cat_id FROM
616 ttrss_feeds WHERE hidden = false AND owner_uid = ".$_SESSION["uid"]);
618 while ($line = db_fetch_assoc($result)) {
620 $has_icon = (int) feed_has_icon($line["id"]);
622 print "<feed has_icon=\"$has_icon\"
623 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
624 print $line["title"];
630 print "<feed-categories>";
632 $result = db_query($link, "SELECT id, title, collapsed FROM
633 ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
635 print "<category id=\"0\" collapsed=\"".
636 (int)$_COOKIE["ttrss_vf_uclps"]."\"><![CDATA[";
637 print __("Uncategorized");
638 print "]]></category>";
640 print "<category id=\"-1\" collapsed=\"".
641 (int)$_COOKIE["ttrss_vf_vclps"]."\"><![CDATA[";
643 print "]]></category>";
645 print "<category id=\"-2\" collapsed=\"".
646 (int)$_COOKIE["ttrss_vf_lclps"]."\"><![CDATA[";
648 print "]]></category>";
650 while ($line = db_fetch_assoc($result)) {
652 id=\"".$line["id"]."\"
653 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
654 print $line["title"];
655 print "]]></category>";
658 print "</feed-categories>";
662 $result = db_query($link, "SELECT * FROM
663 ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
665 while ($line = db_fetch_assoc($result)) {
667 id=\"".$line["id"]."\"
668 fg_color=\"".$line["fg_color"]."\"
669 bg_color=\"".$line["bg_color"]."\"
671 print $line["caption"];
684 $skip = $limit*($stage-1);
686 print "<limit value=\"$limit\"/>";
688 if ($amount > 0) $amount -= $skip;
692 $limit = min($limit, $amount);
695 $unread_qpart = "(unread = true OR marked = true) AND ";
698 if ($cidt && $cidb) {
699 $cid_qpart = "(ttrss_entries.id > $cidt OR ttrss_entries.id < $cidb) AND ";
702 if (DB_TYPE == "pgsql") {
703 $date_qpart = "updated >= NOW() - INTERVAL '1 month' AND";
705 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 MONTH) AND";
708 $result = db_query($link,
709 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
711 feed_id,content,updated,unread,marked FROM
712 ttrss_user_entries,ttrss_entries,ttrss_feeds
713 WHERE $unread_qpart $cid_qpart $date_qpart
715 ttrss_feeds.id = feed_id AND
716 ref_id = ttrss_entries.id AND
717 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
718 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
720 if (function_exists('json_encode')) {
722 while ($line = db_fetch_assoc($result)) {
723 print "<article><![CDATA[";
725 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
726 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
728 $line["labels"] = get_article_labels($link, $line["id"]);
731 // $line["tags"] = format_tags_string(
732 // get_article_tags($link, $line["id"]), $line["id"]);
734 print json_encode($line);
735 print "]]></article>";
745 print "</rpc-reply>";
750 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";