2 class RPC extends Handler_Protected {
4 function csrf_ignore($method) {
5 $csrf_ignored = array("sanitycheck", "completelabels");
7 return array_search($method, $csrf_ignored) !== false;
10 function setprofile() {
11 $id = db_escape_string($this->link, $_REQUEST["id"]);
13 $_SESSION["profile"] = $id;
14 $_SESSION["prefs_cache"] = array();
17 function remprofiles() {
18 $ids = explode(",", db_escape_string($this->link, trim($_REQUEST["ids"])));
20 foreach ($ids as $id) {
21 if ($_SESSION["profile"] != $id) {
22 db_query($this->link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
23 owner_uid = " . $_SESSION["uid"]);
29 function addprofile() {
30 $title = db_escape_string($this->link, trim($_REQUEST["title"]));
32 db_query($this->link, "BEGIN");
34 $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles
35 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
37 if (db_num_rows($result) == 0) {
39 db_query($this->link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
40 VALUES ('$title', ".$_SESSION["uid"] .")");
42 $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles WHERE
45 if (db_num_rows($result) != 0) {
46 $profile_id = db_fetch_result($result, 0, "id");
49 initialize_user_prefs($this->link, $_SESSION["uid"], $profile_id);
54 db_query($this->link, "COMMIT");
59 function saveprofile() {
60 $id = db_escape_string($this->link, $_REQUEST["id"]);
61 $title = db_escape_string($this->link, trim($_REQUEST["value"]));
64 print __("Default profile");
69 db_query($this->link, "BEGIN");
71 $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles
72 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
74 if (db_num_rows($result) == 0) {
75 db_query($this->link, "UPDATE ttrss_settings_profiles
76 SET title = '$title' WHERE id = '$id' AND
77 owner_uid = " . $_SESSION["uid"]);
80 $result = db_query($this->link, "SELECT title FROM ttrss_settings_profiles
81 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
82 print db_fetch_result($result, 0, "title");
85 db_query($this->link, "COMMIT");
90 function remarchive() {
91 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
93 foreach ($ids as $id) {
94 $result = db_query($this->link, "DELETE FROM ttrss_archived_feeds WHERE
95 (SELECT COUNT(*) FROM ttrss_user_entries
96 WHERE orig_feed_id = '$id') = 0 AND
97 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
99 $rc = db_affected_rows($this->link, $result);
104 $feed = db_escape_string($this->link, $_REQUEST['feed']);
105 $cat = db_escape_string($this->link, $_REQUEST['cat']);
106 $login = db_escape_string($this->link, $_REQUEST['login']);
107 $pass = db_escape_string($this->link, $_REQUEST['pass']);
109 $rc = subscribe_to_feed($this->link, $feed, $cat, $login, $pass);
111 print json_encode(array("result" => $rc));
114 function togglepref() {
115 $key = db_escape_string($this->link, $_REQUEST["key"]);
116 set_pref($this->link, $key, !get_pref($this->link, $key));
117 $value = get_pref($this->link, $key);
119 print json_encode(array("param" =>$key, "value" => $value));
123 // set_pref escapes input, so no need to double escape it here
124 $key = $_REQUEST['key'];
125 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
127 set_pref($this->link, $key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET');
129 print json_encode(array("param" =>$key, "value" => $value));
133 $mark = $_REQUEST["mark"];
134 $id = db_escape_string($this->link, $_REQUEST["id"]);
142 $result = db_query($this->link, "UPDATE ttrss_user_entries SET marked = $mark,
144 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
146 print json_encode(array("message" => "UPDATE_COUNTERS"));
150 $ids = db_escape_string($this->link, $_REQUEST["ids"]);
152 $result = db_query($this->link, "DELETE FROM ttrss_user_entries
153 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
155 purge_orphans($this->link);
157 print json_encode(array("message" => "UPDATE_COUNTERS"));
160 function unarchive() {
161 $ids = explode(",", $_REQUEST["ids"]);
163 foreach ($ids as $id) {
164 $id = db_escape_string($this->link, trim($id));
165 db_query($this->link, "BEGIN");
167 $result = db_query($this->link, "SELECT feed_url,site_url,title FROM ttrss_archived_feeds
168 WHERE id = (SELECT orig_feed_id FROM ttrss_user_entries WHERE ref_id = $id
169 AND owner_uid = ".$_SESSION["uid"].")");
171 if (db_num_rows($result) != 0) {
172 $feed_url = db_escape_string($this->link, db_fetch_result($result, 0, "feed_url"));
173 $site_url = db_escape_string($this->link, db_fetch_result($result, 0, "site_url"));
174 $title = db_escape_string($this->link, db_fetch_result($result, 0, "title"));
176 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_url'
177 AND owner_uid = " .$_SESSION["uid"]);
179 if (db_num_rows($result) == 0) {
181 if (!$title) $title = '[Unknown]';
183 $result = db_query($this->link,
184 "INSERT INTO ttrss_feeds
185 (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
186 VALUES (".$_SESSION["uid"].",
192 $result = db_query($this->link,
193 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_url'
194 AND owner_uid = ".$_SESSION["uid"]);
196 if (db_num_rows($result) != 0) {
197 $feed_id = db_fetch_result($result, 0, "id");
201 $feed_id = db_fetch_result($result, 0, "id");
205 $result = db_query($this->link, "UPDATE ttrss_user_entries
206 SET feed_id = '$feed_id', orig_feed_id = NULL
207 WHERE ref_id = $id AND owner_uid = " . $_SESSION["uid"]);
211 db_query($this->link, "COMMIT");
214 print json_encode(array("message" => "UPDATE_COUNTERS"));
218 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
220 foreach ($ids as $id) {
221 $this->archive_article($this->link, $id, $_SESSION["uid"]);
224 print json_encode(array("message" => "UPDATE_COUNTERS"));
227 private function archive_article($link, $id, $owner_uid) {
228 db_query($link, "BEGIN");
230 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
231 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
233 if (db_num_rows($result) != 0) {
235 /* prepare the archived table */
237 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
240 $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
241 WHERE id = '$feed_id'");
243 if (db_num_rows($result) == 0) {
244 db_query($link, "INSERT INTO ttrss_archived_feeds
245 (id, owner_uid, title, feed_url, site_url)
246 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
247 WHERE id = '$feed_id'");
250 db_query($link, "UPDATE ttrss_user_entries
251 SET orig_feed_id = feed_id, feed_id = NULL
252 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
256 db_query($link, "COMMIT");
260 $pub = $_REQUEST["pub"];
261 $id = db_escape_string($this->link, $_REQUEST["id"]);
262 $note = trim(strip_tags(db_escape_string($this->link, $_REQUEST["note"])));
270 $result = db_query($this->link, "UPDATE ttrss_user_entries SET
271 published = $pub, last_published = NOW()
272 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
274 $pubsub_result = false;
276 if (PUBSUBHUBBUB_HUB) {
277 $rss_link = get_self_url_prefix() .
278 "/public.php?op=rss&id=-2&key=" .
279 get_feed_access_key($this->link, -2, false);
281 $p = new Publisher(PUBSUBHUBBUB_HUB);
283 $pubsub_result = $p->publish_update($rss_link);
286 print json_encode(array("message" => "UPDATE_COUNTERS",
287 "pubsub_result" => $pubsub_result));
290 function getAllCounters() {
291 $last_article_id = (int) $_REQUEST["last_article_id"];
295 if ($seq) $reply['seq'] = $seq;
297 if ($last_article_id != getLastArticleId($this->link)) {
298 $reply['counters'] = getAllCounters($this->link);
301 $reply['runtime-info'] = make_runtime_info($this->link);
303 print json_encode($reply);
306 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
307 function catchupSelected() {
308 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
309 $cmode = sprintf("%d", $_REQUEST["cmode"]);
311 catchupArticlesById($this->link, $ids, $cmode);
313 print json_encode(array("message" => "UPDATE_COUNTERS"));
316 function markSelected() {
317 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
318 $cmode = sprintf("%d", $_REQUEST["cmode"]);
320 $this->markArticlesById($this->link, $ids, $cmode);
322 print json_encode(array("message" => "UPDATE_COUNTERS"));
325 function publishSelected() {
326 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
327 $cmode = sprintf("%d", $_REQUEST["cmode"]);
329 $this->publishArticlesById($this->link, $ids, $cmode);
331 print json_encode(array("message" => "UPDATE_COUNTERS"));
334 function sanityCheck() {
335 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
336 $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true";
340 $reply['error'] = sanity_check($this->link);
342 if ($reply['error']['code'] == 0) {
343 $reply['init-params'] = make_init_params($this->link);
344 $reply['runtime-info'] = make_runtime_info($this->link);
347 print json_encode($reply);
350 function setArticleTags() {
352 $id = db_escape_string($this->link, $_REQUEST["id"]);
354 $tags_str = db_escape_string($this->link, $_REQUEST["tags_str"]);
355 $tags = array_unique(trim_array(explode(",", $tags_str)));
357 db_query($this->link, "BEGIN");
359 $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries WHERE
360 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
362 if (db_num_rows($result) == 1) {
364 $tags_to_cache = array();
366 $int_id = db_fetch_result($result, 0, "int_id");
368 db_query($this->link, "DELETE FROM ttrss_tags WHERE
369 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
371 foreach ($tags as $tag) {
372 $tag = sanitize_tag($tag);
374 if (!tag_is_valid($tag)) {
378 if (preg_match("/^[0-9]*$/", $tag)) {
382 // print "<!-- $id : $int_id : $tag -->";
385 db_query($this->link, "INSERT INTO ttrss_tags
386 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
389 array_push($tags_to_cache, $tag);
392 /* update tag cache */
394 sort($tags_to_cache);
395 $tags_str = join(",", $tags_to_cache);
397 db_query($this->link, "UPDATE ttrss_user_entries
398 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
399 AND owner_uid = " . $_SESSION["uid"]);
402 db_query($this->link, "COMMIT");
404 $tags = get_article_tags($this->link, $id);
405 $tags_str = format_tags_string($tags, $id);
406 $tags_str_full = join(", ", $tags);
408 if (!$tags_str_full) $tags_str_full = __("no tags");
410 print json_encode(array("id" => (int)$id,
411 "content" => $tags_str, "content_full" => $tags_str_full));
414 function completeLabels() {
415 $search = db_escape_string($this->link, $_REQUEST["search"]);
417 $result = db_query($this->link, "SELECT DISTINCT caption FROM
419 WHERE owner_uid = '".$_SESSION["uid"]."' AND
420 LOWER(caption) LIKE LOWER('$search%') ORDER BY caption
424 while ($line = db_fetch_assoc($result)) {
425 print "<li>" . $line["caption"] . "</li>";
431 function completeTags() {
432 $search = db_escape_string($this->link, $_REQUEST["search"]);
434 $result = db_query($this->link, "SELECT DISTINCT tag_name FROM ttrss_tags
435 WHERE owner_uid = '".$_SESSION["uid"]."' AND
436 tag_name LIKE '$search%' ORDER BY tag_name
440 while ($line = db_fetch_assoc($result)) {
441 print "<li>" . $line["tag_name"] . "</li>";
447 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
448 $days = sprintf("%d", $_REQUEST["days"]);
450 foreach ($ids as $id) {
452 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
453 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
455 if (db_num_rows($result) == 1) {
456 purge_feed($this->link, $id, $days);
461 function getArticles() {
462 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
465 foreach ($ids as $id) {
467 array_push($articles, format_article($this->link, $id, 0, false));
471 print json_encode($articles);
474 function assigntolabel() {
475 return $this->labelops(true);
478 function removefromlabel() {
479 return $this->labelops(false);
482 function labelops($assign) {
485 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
486 $label_id = db_escape_string($this->link, $_REQUEST["lid"]);
488 $label = db_escape_string($this->link, label_find_caption($this->link, $label_id,
491 $reply["info-for-headlines"] = array();
495 foreach ($ids as $id) {
498 label_add_article($this->link, $id, $label, $_SESSION["uid"]);
500 label_remove_article($this->link, $id, $label, $_SESSION["uid"]);
502 $labels = get_article_labels($this->link, $id, $_SESSION["uid"]);
504 array_push($reply["info-for-headlines"],
505 array("id" => $id, "labels" => format_article_labels($labels, $id)));
510 $reply["message"] = "UPDATE_COUNTERS";
512 print json_encode($reply);
515 function updateFeedBrowser() {
516 $search = db_escape_string($this->link, $_REQUEST["search"]);
517 $limit = db_escape_string($this->link, $_REQUEST["limit"]);
518 $mode = (int) db_escape_string($this->link, $_REQUEST["mode"]);
520 require_once "feedbrowser.php";
522 print json_encode(array("content" =>
523 make_feed_browser($this->link, $search, $limit, $mode),
528 function massSubscribe() {
530 $payload = json_decode($_REQUEST["payload"], false);
531 $mode = $_REQUEST["mode"];
533 if (!$payload || !is_array($payload)) return;
536 foreach ($payload as $feed) {
538 $title = db_escape_string($this->link, $feed[0]);
539 $feed_url = db_escape_string($this->link, $feed[1]);
541 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
542 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
544 if (db_num_rows($result) == 0) {
545 $result = db_query($this->link, "INSERT INTO ttrss_feeds
546 (owner_uid,feed_url,title,cat_id,site_url)
547 VALUES ('".$_SESSION["uid"]."',
548 '$feed_url', '$title', NULL, '')");
551 } else if ($mode == 2) {
553 foreach ($payload as $id) {
554 $result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds
555 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
557 if (db_num_rows($result) != 0) {
558 $site_url = db_escape_string($this->link, db_fetch_result($result, 0, "site_url"));
559 $feed_url = db_escape_string($this->link, db_fetch_result($result, 0, "feed_url"));
560 $title = db_escape_string($this->link, db_fetch_result($result, 0, "title"));
562 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
563 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
565 if (db_num_rows($result) == 0) {
566 $result = db_query($this->link, "INSERT INTO ttrss_feeds
567 (owner_uid,feed_url,title,cat_id,site_url)
568 VALUES ('$id','".$_SESSION["uid"]."',
569 '$feed_url', '$title', NULL, '$site_url')");
576 function catchupFeed() {
577 $feed_id = db_escape_string($this->link, $_REQUEST['feed_id']);
578 $is_cat = db_escape_string($this->link, $_REQUEST['is_cat']) == "true";
579 $mode = db_escape_string($this->link, $_REQUEST['mode']);
581 catchup_feed($this->link, $feed_id, $is_cat, false, false, $mode);
583 print json_encode(array("message" => "UPDATE_COUNTERS"));
586 function quickAddCat() {
587 $cat = db_escape_string($this->link, $_REQUEST["cat"]);
589 add_feed_category($this->link, $cat);
591 $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE
592 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
594 if (db_num_rows($result) == 1) {
595 $id = db_fetch_result($result, 0, "id");
600 print_feed_cat_select($this->link, "cat_id", $id);
604 function clearArticleKeys() {
605 db_query($this->link, "UPDATE ttrss_user_entries SET uuid = '' WHERE
606 owner_uid = " . $_SESSION["uid"]);
611 function setpanelmode() {
612 $wide = (int) $_REQUEST["wide"];
614 setcookie("ttrss_widescreen", $wide,
615 time() + SESSION_COOKIE_LIFETIME);
617 print json_encode(array("wide" => $wide));
620 function updaterandomfeed() {
621 // Test if the feed need a update (update interval exceded).
622 if (DB_TYPE == "pgsql") {
623 $update_limit_qpart = "AND ((
624 ttrss_feeds.update_interval = 0
625 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
627 ttrss_feeds.update_interval > 0
628 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
629 ) OR ttrss_feeds.last_updated IS NULL
630 OR last_updated = '1970-01-01 00:00:00')";
632 $update_limit_qpart = "AND ((
633 ttrss_feeds.update_interval = 0
634 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
636 ttrss_feeds.update_interval > 0
637 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
638 ) OR ttrss_feeds.last_updated IS NULL
639 OR last_updated = '1970-01-01 00:00:00')";
642 // Test if feed is currently being updated by another process.
643 if (DB_TYPE == "pgsql") {
644 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
646 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
649 $random_qpart = sql_random_function();
651 // We search for feed needing update.
652 $result = db_query($this->link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id
654 ttrss_feeds, ttrss_users, ttrss_user_prefs
656 ttrss_feeds.owner_uid = ttrss_users.id
657 AND ttrss_users.id = ttrss_user_prefs.owner_uid
658 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
659 AND ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
660 $update_limit_qpart $updstart_thresh_qpart
661 ORDER BY $random_qpart LIMIT 30");
665 require_once "rssfuncs.php";
671 while ($line = db_fetch_assoc($result)) {
672 $feed_id = $line["id"];
674 if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
675 update_rss_feed($this->link, $feed_id, true);
682 // Purge orphans and cleanup tags
683 purge_orphans($this->link);
684 cleanup_tags($this->link, 14, 50000);
686 if ($num_updated > 0) {
687 print json_encode(array("message" => "UPDATE_COUNTERS",
688 "num_updated" => $num_updated));
690 print json_encode(array("message" => "NOTHING_TO_UPDATE"));
695 private function markArticlesById($link, $ids, $cmode) {
699 foreach ($ids as $id) {
700 array_push($tmp_ids, "ref_id = '$id'");
703 $ids_qpart = join(" OR ", $tmp_ids);
706 db_query($link, "UPDATE ttrss_user_entries SET
707 marked = false, last_marked = NOW()
708 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
709 } else if ($cmode == 1) {
710 db_query($link, "UPDATE ttrss_user_entries SET
711 marked = true, last_marked = NOW()
712 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
714 db_query($link, "UPDATE ttrss_user_entries SET
715 marked = NOT marked,last_marked = NOW()
716 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
720 private function publishArticlesById($link, $ids, $cmode) {
724 foreach ($ids as $id) {
725 array_push($tmp_ids, "ref_id = '$id'");
728 $ids_qpart = join(" OR ", $tmp_ids);
731 db_query($link, "UPDATE ttrss_user_entries SET
732 published = false,last_published = NOW()
733 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
734 } else if ($cmode == 1) {
735 db_query($link, "UPDATE ttrss_user_entries SET
736 published = true,last_published = NOW()
737 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
739 db_query($link, "UPDATE ttrss_user_entries SET
740 published = NOT published,last_published = NOW()
741 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
744 if (PUBSUBHUBBUB_HUB) {
745 $rss_link = get_self_url_prefix() .
746 "/public.php?op=rss&id=-2&key=" .
747 get_feed_access_key($link, -2, false);
749 $p = new Publisher(PUBSUBHUBBUB_HUB);
751 $pubsub_result = $p->publish_update($rss_link);
755 function getlinktitlebyid() {
756 $id = db_escape_string($this->link, $_REQUEST['id']);
758 $result = db_query($this->link, "SELECT link, title FROM ttrss_entries, ttrss_user_entries
759 WHERE ref_id = '$id' AND ref_id = id AND owner_uid = ". $_SESSION["uid"]);
761 if (db_num_rows($result) != 0) {
762 $link = db_fetch_result($result, 0, "link");
763 $title = db_fetch_result($result, 0, "title");
765 echo json_encode(array("link" => $link, "title" => $title));
767 echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));