]>
git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
53635f9665bc45f3aca7de84fc436ae9d04d851b
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 $amount = (int) $_REQUEST["amount"];
520 $unread_only = db_escape_string($_REQUEST["unread_only"]);
522 if (!$amount) $amount = 50;
531 $result = db_query($link, "SELECT id, title FROM
532 ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
534 while ($line = db_fetch_assoc($result)) {
535 print "<feed id=\"".$line["id"]."\"><![CDATA[";
536 print $line["title"];
549 $skip = $limit*($stage-1);
551 if ($amount > 0) $amount -= $skip;
555 $limit = min($limit, $amount);
558 $unread_qpart = "unread = true AND ";
561 $result = db_query($link,
562 "SELECT DISTINCT id,title,guid,link,
563 feed_id,content,updated,unread,marked FROM
564 ttrss_user_entries,ttrss_entries
566 ref_id = id AND owner_uid = ".$_SESSION["uid"]."
567 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
569 while ($line = db_fetch_assoc($result)) {
570 print "<article><![CDATA[";
571 print json_encode($line);
572 print "]]></article>";
581 print "</rpc-reply>";
586 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";