]>
git.wh0rd.org - tt-rss.git/blob - classes/rpc.php
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;
16 function remprofiles() {
17 $ids = explode(",", $this->dbh
->escape_string(trim($_REQUEST["ids"])));
19 foreach ($ids as $id) {
20 if ($_SESSION["profile"] != $id) {
21 $this->dbh
->query("DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
22 owner_uid = " . $_SESSION["uid"]);
28 function addprofile() {
29 $title = $this->dbh
->escape_string(trim($_REQUEST["title"]));
31 $this->dbh
->query("BEGIN");
33 $result = $this->dbh
->query("SELECT id FROM ttrss_settings_profiles
34 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
36 if ($this->dbh
->num_rows($result) == 0) {
38 $this->dbh
->query("INSERT INTO ttrss_settings_profiles (title, owner_uid)
39 VALUES ('$title', ".$_SESSION["uid"] .")");
41 $result = $this->dbh
->query("SELECT id FROM ttrss_settings_profiles WHERE
44 if ($this->dbh
->num_rows($result) != 0) {
45 $profile_id = $this->dbh
->fetch_result($result, 0, "id");
48 initialize_user_prefs($_SESSION["uid"], $profile_id);
53 $this->dbh
->query("COMMIT");
58 function saveprofile() {
59 $id = $this->dbh
->escape_string($_REQUEST["id"]);
60 $title = $this->dbh
->escape_string(trim($_REQUEST["value"]));
63 print __("Default profile");
68 $this->dbh
->query("BEGIN");
70 $result = $this->dbh
->query("SELECT id FROM ttrss_settings_profiles
71 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
73 if ($this->dbh
->num_rows($result) == 0) {
74 $this->dbh
->query("UPDATE ttrss_settings_profiles
75 SET title = '$title' WHERE id = '$id' AND
76 owner_uid = " . $_SESSION["uid"]);
79 $result = $this->dbh
->query("SELECT title FROM ttrss_settings_profiles
80 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
81 print $this->dbh
->fetch_result($result, 0, "title");
84 $this->dbh
->query("COMMIT");
89 function remarchive() {
90 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
92 foreach ($ids as $id) {
93 $result = $this->dbh
->query("DELETE FROM ttrss_archived_feeds WHERE
94 (SELECT COUNT(*) FROM ttrss_user_entries
95 WHERE orig_feed_id = '$id') = 0 AND
96 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
98 $this->dbh
->affected_rows($result);
103 $feed = $this->dbh
->escape_string($_REQUEST['feed']);
104 $cat = $this->dbh
->escape_string($_REQUEST['cat']);
105 $login = $this->dbh
->escape_string($_REQUEST['login']);
106 $pass = trim($_REQUEST['pass']); // escaped later
108 $rc = subscribe_to_feed($feed, $cat, $login, $pass);
110 print json_encode(array("result" => $rc));
113 function togglepref() {
114 $key = $this->dbh
->escape_string($_REQUEST["key"]);
115 set_pref($key, !get_pref($key));
116 $value = get_pref($key);
118 print json_encode(array("param" =>$key, "value" => $value));
122 // set_pref escapes input, so no need to double escape it here
123 $key = $_REQUEST['key'];
124 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
126 set_pref($key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET');
128 print json_encode(array("param" =>$key, "value" => $value));
132 $mark = $_REQUEST["mark"];
133 $id = $this->dbh
->escape_string($_REQUEST["id"]);
141 $this->dbh
->query("UPDATE ttrss_user_entries SET marked = $mark,
143 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
145 print json_encode(array("message" => "UPDATE_COUNTERS"));
149 $ids = $this->dbh
->escape_string($_REQUEST["ids"]);
151 $this->dbh
->query("DELETE FROM ttrss_user_entries
152 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
156 print json_encode(array("message" => "UPDATE_COUNTERS"));
159 function unarchive() {
160 $ids = explode(",", $_REQUEST["ids"]);
162 foreach ($ids as $id) {
163 $id = $this->dbh
->escape_string(trim($id));
164 $this->dbh
->query("BEGIN");
166 $result = $this->dbh
->query("SELECT feed_url,site_url,title FROM ttrss_archived_feeds
167 WHERE id = (SELECT orig_feed_id FROM ttrss_user_entries WHERE ref_id = $id
168 AND owner_uid = ".$_SESSION["uid"].")");
170 if ($this->dbh
->num_rows($result) != 0) {
171 $feed_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "feed_url"));
172 $site_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "site_url"));
173 $title = $this->dbh
->escape_string(db_fetch_result($result, 0, "title"));
175 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_url'
176 AND owner_uid = " .$_SESSION["uid"]);
178 if ($this->dbh
->num_rows($result) == 0) {
180 if (!$title) $title = '[Unknown]';
182 $result = $this->dbh
->query(
183 "INSERT INTO ttrss_feeds
184 (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
185 VALUES (".$_SESSION["uid"].",
191 $result = $this->dbh
->query(
192 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_url'
193 AND owner_uid = ".$_SESSION["uid"]);
195 if ($this->dbh
->num_rows($result) != 0) {
196 $feed_id = $this->dbh
->fetch_result($result, 0, "id");
200 $feed_id = $this->dbh
->fetch_result($result, 0, "id");
204 $result = $this->dbh
->query("UPDATE ttrss_user_entries
205 SET feed_id = '$feed_id', orig_feed_id = NULL
206 WHERE ref_id = $id AND owner_uid = " . $_SESSION["uid"]);
210 $this->dbh
->query("COMMIT");
213 print json_encode(array("message" => "UPDATE_COUNTERS"));
217 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
219 foreach ($ids as $id) {
220 $this->archive_article($id, $_SESSION["uid"]);
223 print json_encode(array("message" => "UPDATE_COUNTERS"));
226 private function archive_article($id, $owner_uid) {
227 $this->dbh
->query("BEGIN");
229 $result = $this->dbh
->query("SELECT feed_id FROM ttrss_user_entries
230 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
232 if ($this->dbh
->num_rows($result) != 0) {
234 /* prepare the archived table */
236 $feed_id = (int) $this->dbh
->fetch_result($result, 0, "feed_id");
239 $result = $this->dbh
->query("SELECT id FROM ttrss_archived_feeds
240 WHERE id = '$feed_id'");
242 if ($this->dbh
->num_rows($result) == 0) {
243 $this->dbh
->query("INSERT INTO ttrss_archived_feeds
244 (id, owner_uid, title, feed_url, site_url)
245 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
246 WHERE id = '$feed_id'");
249 $this->dbh
->query("UPDATE ttrss_user_entries
250 SET orig_feed_id = feed_id, feed_id = NULL
251 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
255 $this->dbh
->query("COMMIT");
259 $pub = $_REQUEST["pub"];
260 $id = $this->dbh
->escape_string($_REQUEST["id"]);
268 $this->dbh
->query("UPDATE ttrss_user_entries SET
269 published = $pub, last_published = NOW()
270 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
272 $pubsub_result = false;
274 if (PUBSUBHUBBUB_HUB
) {
275 $rss_link = get_self_url_prefix() .
276 "/public.php?op=rss&id=-2&key=" .
277 get_feed_access_key(-2, false);
279 $p = new Publisher(PUBSUBHUBBUB_HUB
);
281 $pubsub_result = $p->publish_update($rss_link);
284 print json_encode(array("message" => "UPDATE_COUNTERS",
285 "pubsub_result" => $pubsub_result));
288 function getAllCounters() {
289 $last_article_id = (int) $_REQUEST["last_article_id"];
293 if (!empty($_REQUEST['seq'])) $reply['seq'] = (int) $_REQUEST['seq'];
295 if ($last_article_id != getLastArticleId()) {
296 $reply['counters'] = getAllCounters();
299 $reply['runtime-info'] = make_runtime_info();
301 print json_encode($reply);
304 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
305 function catchupSelected() {
306 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
307 $cmode = sprintf("%d", $_REQUEST["cmode"]);
309 catchupArticlesById($ids, $cmode);
311 print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids));
314 function markSelected() {
315 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
316 $cmode = sprintf("%d", $_REQUEST["cmode"]);
318 $this->markArticlesById($ids, $cmode);
320 print json_encode(array("message" => "UPDATE_COUNTERS"));
323 function publishSelected() {
324 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
325 $cmode = sprintf("%d", $_REQUEST["cmode"]);
327 $this->publishArticlesById($ids, $cmode);
329 print json_encode(array("message" => "UPDATE_COUNTERS"));
332 function sanityCheck() {
333 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
334 $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true";
335 $_SESSION["hasMp3"] = $_REQUEST["hasMp3"] === "true";
336 $_SESSION["clientTzOffset"] = $_REQUEST["clientTzOffset"];
340 $reply['error'] = sanity_check();
342 if ($reply['error']['code'] == 0) {
343 $reply['init-params'] = make_init_params();
344 $reply['runtime-info'] = make_runtime_info();
347 print json_encode($reply);
350 function completeLabels() {
351 $search = $this->dbh
->escape_string($_REQUEST["search"]);
353 $result = $this->dbh
->query("SELECT DISTINCT caption FROM
355 WHERE owner_uid = '".$_SESSION["uid"]."' AND
356 LOWER(caption) LIKE LOWER('$search%') ORDER BY caption
360 while ($line = $this->dbh
->fetch_assoc($result)) {
361 print "<li>" . $line["caption"] . "</li>";
367 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
368 $days = sprintf("%d", $_REQUEST["days"]);
370 foreach ($ids as $id) {
372 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
373 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
375 if ($this->dbh
->num_rows($result) == 1) {
376 purge_feed($id, $days);
381 function updateFeedBrowser() {
382 if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER
) return;
384 $search = $this->dbh
->escape_string($_REQUEST["search"]);
385 $limit = $this->dbh
->escape_string($_REQUEST["limit"]);
386 $mode = (int) $this->dbh
->escape_string($_REQUEST["mode"]);
388 require_once "feedbrowser.php";
390 print json_encode(array("content" =>
391 make_feed_browser($search, $limit, $mode),
396 function massSubscribe() {
398 $payload = json_decode($_REQUEST["payload"], false);
399 $mode = $_REQUEST["mode"];
401 if (!$payload ||
!is_array($payload)) return;
404 foreach ($payload as $feed) {
406 $title = $this->dbh
->escape_string($feed[0]);
407 $feed_url = $this->dbh
->escape_string($feed[1]);
409 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
410 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
412 if ($this->dbh
->num_rows($result) == 0) {
413 $result = $this->dbh
->query("INSERT INTO ttrss_feeds
414 (owner_uid,feed_url,title,cat_id,site_url)
415 VALUES ('".$_SESSION["uid"]."',
416 '$feed_url', '$title', NULL, '')");
419 } else if ($mode == 2) {
421 foreach ($payload as $id) {
422 $result = $this->dbh
->query("SELECT * FROM ttrss_archived_feeds
423 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
425 if ($this->dbh
->num_rows($result) != 0) {
426 $site_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "site_url"));
427 $feed_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "feed_url"));
428 $title = $this->dbh
->escape_string(db_fetch_result($result, 0, "title"));
430 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
431 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
433 if ($this->dbh
->num_rows($result) == 0) {
434 $result = $this->dbh
->query("INSERT INTO ttrss_feeds
435 (owner_uid,feed_url,title,cat_id,site_url)
436 VALUES ('$id','".$_SESSION["uid"]."',
437 '$feed_url', '$title', NULL, '$site_url')");
444 function catchupFeed() {
445 $feed_id = $this->dbh
->escape_string($_REQUEST['feed_id']);
446 $is_cat = $this->dbh
->escape_string($_REQUEST['is_cat']) == "true";
447 $mode = $this->dbh
->escape_string($_REQUEST['mode']);
449 catchup_feed($feed_id, $is_cat, false, false, $mode);
451 print json_encode(array("message" => "UPDATE_COUNTERS"));
454 function quickAddCat() {
455 $cat = $this->dbh
->escape_string($_REQUEST["cat"]);
457 add_feed_category($cat);
459 $result = $this->dbh
->query("SELECT id FROM ttrss_feed_categories WHERE
460 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
462 if ($this->dbh
->num_rows($result) == 1) {
463 $id = $this->dbh
->fetch_result($result, 0, "id");
468 print_feed_cat_select("cat_id", $id, '');
471 function setpanelmode() {
472 $wide = (int) $_REQUEST["wide"];
474 setcookie("ttrss_widescreen", $wide,
475 time() + COOKIE_LIFETIME_LONG
);
477 print json_encode(array("wide" => $wide));
480 static function updaterandomfeed_real($dbh) {
482 // Test if the feed need a update (update interval exceded).
483 if (DB_TYPE
== "pgsql") {
484 $update_limit_qpart = "AND ((
485 ttrss_feeds.update_interval = 0
486 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
488 ttrss_feeds.update_interval > 0
489 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
490 ) OR ttrss_feeds.last_updated IS NULL
491 OR last_updated = '1970-01-01 00:00:00')";
493 $update_limit_qpart = "AND ((
494 ttrss_feeds.update_interval = 0
495 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
497 ttrss_feeds.update_interval > 0
498 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
499 ) OR ttrss_feeds.last_updated IS NULL
500 OR last_updated = '1970-01-01 00:00:00')";
503 // Test if feed is currently being updated by another process.
504 if (DB_TYPE
== "pgsql") {
505 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
507 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
510 $random_qpart = sql_random_function();
512 // we could be invoked from public.php with no active session
513 if ($_SESSION["uid"]) {
514 $owner_check_qpart = "AND ttrss_feeds.owner_uid = '".$_SESSION["uid"]."'";
516 $owner_check_qpart = "";
519 // We search for feed needing update.
520 $result = $dbh->query("SELECT ttrss_feeds.feed_url,ttrss_feeds.id
522 ttrss_feeds, ttrss_users, ttrss_user_prefs
524 ttrss_feeds.owner_uid = ttrss_users.id
525 AND ttrss_users.id = ttrss_user_prefs.owner_uid
526 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
529 $updstart_thresh_qpart
530 ORDER BY $random_qpart LIMIT 30");
534 require_once "rssfuncs.php";
540 while ($line = $dbh->fetch_assoc($result)) {
541 $feed_id = $line["id"];
543 if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
544 update_rss_feed($feed_id, true);
551 // Purge orphans and cleanup tags
553 cleanup_tags(14, 50000);
555 if ($num_updated > 0) {
556 print json_encode(array("message" => "UPDATE_COUNTERS",
557 "num_updated" => $num_updated));
559 print json_encode(array("message" => "NOTHING_TO_UPDATE"));
564 function updaterandomfeed() {
565 RPC
::updaterandomfeed_real($this->dbh
);
568 private function markArticlesById($ids, $cmode) {
572 foreach ($ids as $id) {
573 array_push($tmp_ids, "ref_id = '$id'");
576 $ids_qpart = join(" OR ", $tmp_ids);
579 $this->dbh
->query("UPDATE ttrss_user_entries SET
580 marked = false, last_marked = NOW()
581 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
582 } else if ($cmode == 1) {
583 $this->dbh
->query("UPDATE ttrss_user_entries SET
584 marked = true, last_marked = NOW()
585 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
587 $this->dbh
->query("UPDATE ttrss_user_entries SET
588 marked = NOT marked,last_marked = NOW()
589 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
593 private function publishArticlesById($ids, $cmode) {
597 foreach ($ids as $id) {
598 array_push($tmp_ids, "ref_id = '$id'");
601 $ids_qpart = join(" OR ", $tmp_ids);
604 $this->dbh
->query("UPDATE ttrss_user_entries SET
605 published = false,last_published = NOW()
606 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
607 } else if ($cmode == 1) {
608 $this->dbh
->query("UPDATE ttrss_user_entries SET
609 published = true,last_published = NOW()
610 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
612 $this->dbh
->query("UPDATE ttrss_user_entries SET
613 published = NOT published,last_published = NOW()
614 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
617 if (PUBSUBHUBBUB_HUB
) {
618 $rss_link = get_self_url_prefix() .
619 "/public.php?op=rss&id=-2&key=" .
620 get_feed_access_key(-2, false);
622 $p = new Publisher(PUBSUBHUBBUB_HUB
);
624 /* $pubsub_result = */ $p->publish_update($rss_link);
628 function getlinktitlebyid() {
629 $id = $this->dbh
->escape_string($_REQUEST['id']);
631 $result = $this->dbh
->query("SELECT link, title FROM ttrss_entries, ttrss_user_entries
632 WHERE ref_id = '$id' AND ref_id = id AND owner_uid = ". $_SESSION["uid"]);
634 if ($this->dbh
->num_rows($result) != 0) {
635 $link = $this->dbh
->fetch_result($result, 0, "link");
636 $title = $this->dbh
->fetch_result($result, 0, "title");
638 echo json_encode(array("link" => $link, "title" => $title));
640 echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));
645 $logmsg = $this->dbh
->escape_string($_REQUEST['logmsg']);
648 Logger
::get()->log_error(E_USER_WARNING
,
649 $logmsg, '[client-js]', 0, false);
652 echo json_encode(array("message" => "HOST_ERROR_LOGGED"));