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 = $this->dbh
->escape_string($_REQUEST["id"]);
13 $_SESSION["profile"] = $id;
14 $_SESSION["prefs_cache"] = array();
17 function remprofiles() {
18 $ids = explode(",", $this->dbh
->escape_string(trim($_REQUEST["ids"])));
20 foreach ($ids as $id) {
21 if ($_SESSION["profile"] != $id) {
22 $this->dbh
->query("DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
23 owner_uid = " . $_SESSION["uid"]);
29 function addprofile() {
30 $title = $this->dbh
->escape_string(trim($_REQUEST["title"]));
32 $this->dbh
->query("BEGIN");
34 $result = $this->dbh
->query("SELECT id FROM ttrss_settings_profiles
35 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
37 if ($this->dbh
->num_rows($result) == 0) {
39 $this->dbh
->query("INSERT INTO ttrss_settings_profiles (title, owner_uid)
40 VALUES ('$title', ".$_SESSION["uid"] .")");
42 $result = $this->dbh
->query("SELECT id FROM ttrss_settings_profiles WHERE
45 if ($this->dbh
->num_rows($result) != 0) {
46 $profile_id = $this->dbh
->fetch_result($result, 0, "id");
49 initialize_user_prefs($_SESSION["uid"], $profile_id);
54 $this->dbh
->query("COMMIT");
59 function saveprofile() {
60 $id = $this->dbh
->escape_string($_REQUEST["id"]);
61 $title = $this->dbh
->escape_string(trim($_REQUEST["value"]));
64 print __("Default profile");
69 $this->dbh
->query("BEGIN");
71 $result = $this->dbh
->query("SELECT id FROM ttrss_settings_profiles
72 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
74 if ($this->dbh
->num_rows($result) == 0) {
75 $this->dbh
->query("UPDATE ttrss_settings_profiles
76 SET title = '$title' WHERE id = '$id' AND
77 owner_uid = " . $_SESSION["uid"]);
80 $result = $this->dbh
->query("SELECT title FROM ttrss_settings_profiles
81 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
82 print $this->dbh
->fetch_result($result, 0, "title");
85 $this->dbh
->query("COMMIT");
90 function remarchive() {
91 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
93 foreach ($ids as $id) {
94 $result = $this->dbh
->query("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 = $this->dbh
->affected_rows($result);
104 $feed = $this->dbh
->escape_string($_REQUEST['feed']);
105 $cat = $this->dbh
->escape_string($_REQUEST['cat']);
106 $login = $this->dbh
->escape_string($_REQUEST['login']);
107 $pass = trim($_REQUEST['pass']); // escaped later
109 $rc = subscribe_to_feed($feed, $cat, $login, $pass);
111 print json_encode(array("result" => $rc));
114 function togglepref() {
115 $key = $this->dbh
->escape_string($_REQUEST["key"]);
116 set_pref($key, !get_pref($key));
117 $value = get_pref($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($key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET');
129 print json_encode(array("param" =>$key, "value" => $value));
133 $mark = $_REQUEST["mark"];
134 $id = $this->dbh
->escape_string($_REQUEST["id"]);
142 $result = $this->dbh
->query("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 = $this->dbh
->escape_string($_REQUEST["ids"]);
152 $result = $this->dbh
->query("DELETE FROM ttrss_user_entries
153 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
157 print json_encode(array("message" => "UPDATE_COUNTERS"));
160 function unarchive() {
161 $ids = explode(",", $_REQUEST["ids"]);
163 foreach ($ids as $id) {
164 $id = $this->dbh
->escape_string(trim($id));
165 $this->dbh
->query("BEGIN");
167 $result = $this->dbh
->query("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 ($this->dbh
->num_rows($result) != 0) {
172 $feed_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "feed_url"));
173 $site_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "site_url"));
174 $title = $this->dbh
->escape_string(db_fetch_result($result, 0, "title"));
176 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_url'
177 AND owner_uid = " .$_SESSION["uid"]);
179 if ($this->dbh
->num_rows($result) == 0) {
181 if (!$title) $title = '[Unknown]';
183 $result = $this->dbh
->query(
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 = $this->dbh
->query(
193 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_url'
194 AND owner_uid = ".$_SESSION["uid"]);
196 if ($this->dbh
->num_rows($result) != 0) {
197 $feed_id = $this->dbh
->fetch_result($result, 0, "id");
201 $feed_id = $this->dbh
->fetch_result($result, 0, "id");
205 $result = $this->dbh
->query("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 $this->dbh
->query("COMMIT");
214 print json_encode(array("message" => "UPDATE_COUNTERS"));
218 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
220 foreach ($ids as $id) {
221 $this->archive_article($id, $_SESSION["uid"]);
224 print json_encode(array("message" => "UPDATE_COUNTERS"));
227 private function archive_article($id, $owner_uid) {
228 $this->dbh
->query("BEGIN");
230 $result = $this->dbh
->query("SELECT feed_id FROM ttrss_user_entries
231 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
233 if ($this->dbh
->num_rows($result) != 0) {
235 /* prepare the archived table */
237 $feed_id = (int) $this->dbh
->fetch_result($result, 0, "feed_id");
240 $result = $this->dbh
->query("SELECT id FROM ttrss_archived_feeds
241 WHERE id = '$feed_id'");
243 if ($this->dbh
->num_rows($result) == 0) {
244 $this->dbh
->query("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 $this->dbh
->query("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 $this->dbh
->query("COMMIT");
260 $pub = $_REQUEST["pub"];
261 $id = $this->dbh
->escape_string($_REQUEST["id"]);
262 $note = trim(strip_tags($this->dbh
->escape_string($_REQUEST["note"])));
270 $result = $this->dbh
->query("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(-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()) {
298 $reply['counters'] = getAllCounters();
301 $reply['runtime-info'] = make_runtime_info();
303 print json_encode($reply);
306 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
307 function catchupSelected() {
308 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
309 $cmode = sprintf("%d", $_REQUEST["cmode"]);
311 catchupArticlesById($ids, $cmode);
313 print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids));
316 function markSelected() {
317 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
318 $cmode = sprintf("%d", $_REQUEST["cmode"]);
320 $this->markArticlesById($ids, $cmode);
322 print json_encode(array("message" => "UPDATE_COUNTERS"));
325 function publishSelected() {
326 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
327 $cmode = sprintf("%d", $_REQUEST["cmode"]);
329 $this->publishArticlesById($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";
337 $_SESSION["hasMp3"] = $_REQUEST["hasMp3"] === "true";
341 $reply['error'] = sanity_check();
343 if ($reply['error']['code'] == 0) {
344 $reply['init-params'] = make_init_params();
345 $reply['runtime-info'] = make_runtime_info();
348 print json_encode($reply);
351 function completeLabels() {
352 $search = $this->dbh
->escape_string($_REQUEST["search"]);
354 $result = $this->dbh
->query("SELECT DISTINCT caption FROM
356 WHERE owner_uid = '".$_SESSION["uid"]."' AND
357 LOWER(caption) LIKE LOWER('$search%') ORDER BY caption
361 while ($line = $this->dbh
->fetch_assoc($result)) {
362 print "<li>" . $line["caption"] . "</li>";
368 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
369 $days = sprintf("%d", $_REQUEST["days"]);
371 foreach ($ids as $id) {
373 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
374 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
376 if ($this->dbh
->num_rows($result) == 1) {
377 purge_feed($id, $days);
382 function updateFeedBrowser() {
383 $search = $this->dbh
->escape_string($_REQUEST["search"]);
384 $limit = $this->dbh
->escape_string($_REQUEST["limit"]);
385 $mode = (int) $this->dbh
->escape_string($_REQUEST["mode"]);
387 require_once "feedbrowser.php";
389 print json_encode(array("content" =>
390 make_feed_browser($search, $limit, $mode),
395 function massSubscribe() {
397 $payload = json_decode($_REQUEST["payload"], false);
398 $mode = $_REQUEST["mode"];
400 if (!$payload ||
!is_array($payload)) return;
403 foreach ($payload as $feed) {
405 $title = $this->dbh
->escape_string($feed[0]);
406 $feed_url = $this->dbh
->escape_string($feed[1]);
408 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
409 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
411 if ($this->dbh
->num_rows($result) == 0) {
412 $result = $this->dbh
->query("INSERT INTO ttrss_feeds
413 (owner_uid,feed_url,title,cat_id,site_url)
414 VALUES ('".$_SESSION["uid"]."',
415 '$feed_url', '$title', NULL, '')");
418 } else if ($mode == 2) {
420 foreach ($payload as $id) {
421 $result = $this->dbh
->query("SELECT * FROM ttrss_archived_feeds
422 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
424 if ($this->dbh
->num_rows($result) != 0) {
425 $site_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "site_url"));
426 $feed_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "feed_url"));
427 $title = $this->dbh
->escape_string(db_fetch_result($result, 0, "title"));
429 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
430 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
432 if ($this->dbh
->num_rows($result) == 0) {
433 $result = $this->dbh
->query("INSERT INTO ttrss_feeds
434 (owner_uid,feed_url,title,cat_id,site_url)
435 VALUES ('$id','".$_SESSION["uid"]."',
436 '$feed_url', '$title', NULL, '$site_url')");
443 function catchupFeed() {
444 $feed_id = $this->dbh
->escape_string($_REQUEST['feed_id']);
445 $is_cat = $this->dbh
->escape_string($_REQUEST['is_cat']) == "true";
446 $mode = $this->dbh
->escape_string($_REQUEST['mode']);
448 catchup_feed($feed_id, $is_cat, false, false, $mode);
450 print json_encode(array("message" => "UPDATE_COUNTERS"));
453 function quickAddCat() {
454 $cat = $this->dbh
->escape_string($_REQUEST["cat"]);
456 add_feed_category($cat);
458 $result = $this->dbh
->query("SELECT id FROM ttrss_feed_categories WHERE
459 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
461 if ($this->dbh
->num_rows($result) == 1) {
462 $id = $this->dbh
->fetch_result($result, 0, "id");
467 print_feed_cat_select("cat_id", $id);
471 function clearArticleKeys() {
472 $this->dbh
->query("UPDATE ttrss_user_entries SET uuid = '' WHERE
473 owner_uid = " . $_SESSION["uid"]);
478 function setpanelmode() {
479 $wide = (int) $_REQUEST["wide"];
481 setcookie("ttrss_widescreen", $wide,
482 time() + SESSION_COOKIE_LIFETIME
);
484 print json_encode(array("wide" => $wide));
487 function updaterandomfeed() {
488 // Test if the feed need a update (update interval exceded).
489 if (DB_TYPE
== "pgsql") {
490 $update_limit_qpart = "AND ((
491 ttrss_feeds.update_interval = 0
492 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
494 ttrss_feeds.update_interval > 0
495 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
496 ) OR ttrss_feeds.last_updated IS NULL
497 OR last_updated = '1970-01-01 00:00:00')";
499 $update_limit_qpart = "AND ((
500 ttrss_feeds.update_interval = 0
501 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
503 ttrss_feeds.update_interval > 0
504 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
505 ) OR ttrss_feeds.last_updated IS NULL
506 OR last_updated = '1970-01-01 00:00:00')";
509 // Test if feed is currently being updated by another process.
510 if (DB_TYPE
== "pgsql") {
511 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
513 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
516 $random_qpart = sql_random_function();
518 // We search for feed needing update.
519 $result = $this->dbh
->query("SELECT ttrss_feeds.feed_url,ttrss_feeds.id
521 ttrss_feeds, ttrss_users, ttrss_user_prefs
523 ttrss_feeds.owner_uid = ttrss_users.id
524 AND ttrss_users.id = ttrss_user_prefs.owner_uid
525 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
526 AND ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
527 $update_limit_qpart $updstart_thresh_qpart
528 ORDER BY $random_qpart LIMIT 30");
532 require_once "rssfuncs.php";
538 while ($line = $this->dbh
->fetch_assoc($result)) {
539 $feed_id = $line["id"];
541 if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
542 update_rss_feed($feed_id, true);
549 // Purge orphans and cleanup tags
551 cleanup_tags(14, 50000);
553 if ($num_updated > 0) {
554 print json_encode(array("message" => "UPDATE_COUNTERS",
555 "num_updated" => $num_updated));
557 print json_encode(array("message" => "NOTHING_TO_UPDATE"));
562 private function markArticlesById($ids, $cmode) {
566 foreach ($ids as $id) {
567 array_push($tmp_ids, "ref_id = '$id'");
570 $ids_qpart = join(" OR ", $tmp_ids);
573 $this->dbh
->query("UPDATE ttrss_user_entries SET
574 marked = false, last_marked = NOW()
575 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
576 } else if ($cmode == 1) {
577 $this->dbh
->query("UPDATE ttrss_user_entries SET
578 marked = true, last_marked = NOW()
579 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
581 $this->dbh
->query("UPDATE ttrss_user_entries SET
582 marked = NOT marked,last_marked = NOW()
583 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
587 private function publishArticlesById($ids, $cmode) {
591 foreach ($ids as $id) {
592 array_push($tmp_ids, "ref_id = '$id'");
595 $ids_qpart = join(" OR ", $tmp_ids);
598 $this->dbh
->query("UPDATE ttrss_user_entries SET
599 published = false,last_published = NOW()
600 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
601 } else if ($cmode == 1) {
602 $this->dbh
->query("UPDATE ttrss_user_entries SET
603 published = true,last_published = NOW()
604 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
606 $this->dbh
->query("UPDATE ttrss_user_entries SET
607 published = NOT published,last_published = NOW()
608 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
611 if (PUBSUBHUBBUB_HUB
) {
612 $rss_link = get_self_url_prefix() .
613 "/public.php?op=rss&id=-2&key=" .
614 get_feed_access_key(-2, false);
616 $p = new Publisher(PUBSUBHUBBUB_HUB
);
618 $pubsub_result = $p->publish_update($rss_link);
622 function getlinktitlebyid() {
623 $id = $this->dbh
->escape_string($_REQUEST['id']);
625 $result = $this->dbh
->query("SELECT link, title FROM ttrss_entries, ttrss_user_entries
626 WHERE ref_id = '$id' AND ref_id = id AND owner_uid = ". $_SESSION["uid"]);
628 if ($this->dbh
->num_rows($result) != 0) {
629 $link = $this->dbh
->fetch_result($result, 0, "link");
630 $title = $this->dbh
->fetch_result($result, 0, "title");
632 echo json_encode(array("link" => $link, "title" => $title));
634 echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));