]>
git.wh0rd.org - tt-rss.git/blob - classes/rpc.php
1e844a6f341e096f6a95ac494165be888f29df60
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, false, $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"].") 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' AND owner_uid = " . $_SESSION["uid"]);
242 if ($this->dbh
->num_rows($result) == 0) {
243 $result = db_query("SELECT MAX(id) AS id FROM ttrss_archived_feeds");
244 $new_feed_id = (int)db_fetch_result($result, 0, "id") +
1;
246 $this->dbh
->query("INSERT INTO ttrss_archived_feeds
247 (id, owner_uid, title, feed_url, site_url)
248 SELECT $new_feed_id, owner_uid, title, feed_url, site_url from ttrss_feeds
249 WHERE id = '$feed_id'");
251 $new_feed_id = $this->dbh
->fetch_result($result, 0, "id");
254 $this->dbh
->query("UPDATE ttrss_user_entries
255 SET orig_feed_id = $new_feed_id, feed_id = NULL
256 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
260 $this->dbh
->query("COMMIT");
264 $pub = $_REQUEST["pub"];
265 $id = $this->dbh
->escape_string($_REQUEST["id"]);
273 $this->dbh
->query("UPDATE ttrss_user_entries SET
274 published = $pub, last_published = NOW()
275 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
277 $pubsub_result = false;
279 if (PUBSUBHUBBUB_HUB
) {
280 $rss_link = get_self_url_prefix() .
281 "/public.php?op=rss&id=-2&key=" .
282 get_feed_access_key(-2, false);
284 $p = new pubsubhubbub\publisher\
Publisher(PUBSUBHUBBUB_HUB
);
286 $pubsub_result = $p->publish_update($rss_link);
289 print json_encode(array("message" => "UPDATE_COUNTERS",
290 "pubsub_result" => $pubsub_result));
293 function getAllCounters() {
294 $last_article_id = (int) $_REQUEST["last_article_id"];
298 if (!empty($_REQUEST['seq'])) $reply['seq'] = (int) $_REQUEST['seq'];
300 if ($last_article_id != getLastArticleId()) {
301 $reply['counters'] = getAllCounters();
304 $reply['runtime-info'] = make_runtime_info();
306 print json_encode($reply);
309 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
310 function catchupSelected() {
311 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
312 $cmode = sprintf("%d", $_REQUEST["cmode"]);
314 catchupArticlesById($ids, $cmode);
316 print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids));
319 function markSelected() {
320 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
321 $cmode = sprintf("%d", $_REQUEST["cmode"]);
323 $this->markArticlesById($ids, $cmode);
325 print json_encode(array("message" => "UPDATE_COUNTERS"));
328 function publishSelected() {
329 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
330 $cmode = sprintf("%d", $_REQUEST["cmode"]);
332 $this->publishArticlesById($ids, $cmode);
334 print json_encode(array("message" => "UPDATE_COUNTERS"));
337 function sanityCheck() {
338 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
339 $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true";
340 $_SESSION["hasMp3"] = $_REQUEST["hasMp3"] === "true";
341 $_SESSION["clientTzOffset"] = $_REQUEST["clientTzOffset"];
345 $reply['error'] = sanity_check();
347 if ($reply['error']['code'] == 0) {
348 $reply['init-params'] = make_init_params();
349 $reply['runtime-info'] = make_runtime_info(true);
352 print json_encode($reply);
355 function completeLabels() {
356 $search = $this->dbh
->escape_string($_REQUEST["search"]);
358 $result = $this->dbh
->query("SELECT DISTINCT caption FROM
360 WHERE owner_uid = '".$_SESSION["uid"]."' AND
361 LOWER(caption) LIKE LOWER('$search%') ORDER BY caption
365 while ($line = $this->dbh
->fetch_assoc($result)) {
366 print "<li>" . $line["caption"] . "</li>";
372 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
373 $days = sprintf("%d", $_REQUEST["days"]);
375 foreach ($ids as $id) {
377 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
378 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
380 if ($this->dbh
->num_rows($result) == 1) {
381 purge_feed($id, $days);
386 function updateFeedBrowser() {
387 if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER
) return;
389 $search = $this->dbh
->escape_string($_REQUEST["search"]);
390 $limit = $this->dbh
->escape_string($_REQUEST["limit"]);
391 $mode = (int) $this->dbh
->escape_string($_REQUEST["mode"]);
393 require_once "feedbrowser.php";
395 print json_encode(array("content" =>
396 make_feed_browser($search, $limit, $mode),
401 function massSubscribe() {
403 $payload = json_decode($_REQUEST["payload"], false);
404 $mode = $_REQUEST["mode"];
406 if (!$payload ||
!is_array($payload)) return;
409 foreach ($payload as $feed) {
411 $title = $this->dbh
->escape_string($feed[0]);
412 $feed_url = $this->dbh
->escape_string($feed[1]);
414 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
415 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
417 if ($this->dbh
->num_rows($result) == 0) {
418 $result = $this->dbh
->query("INSERT INTO ttrss_feeds
419 (owner_uid,feed_url,title,cat_id,site_url)
420 VALUES ('".$_SESSION["uid"]."',
421 '$feed_url', '$title', NULL, '')");
424 } else if ($mode == 2) {
426 foreach ($payload as $id) {
427 $result = $this->dbh
->query("SELECT * FROM ttrss_archived_feeds
428 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
430 if ($this->dbh
->num_rows($result) != 0) {
431 $site_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "site_url"));
432 $feed_url = $this->dbh
->escape_string(db_fetch_result($result, 0, "feed_url"));
433 $title = $this->dbh
->escape_string(db_fetch_result($result, 0, "title"));
435 $result = $this->dbh
->query("SELECT id FROM ttrss_feeds WHERE
436 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
438 if ($this->dbh
->num_rows($result) == 0) {
439 $result = $this->dbh
->query("INSERT INTO ttrss_feeds
440 (owner_uid,feed_url,title,cat_id,site_url)
441 VALUES ('".$_SESSION["uid"]."',
442 '$feed_url', '$title', NULL, '$site_url')");
449 function catchupFeed() {
450 $feed_id = $this->dbh
->escape_string($_REQUEST['feed_id']);
451 $is_cat = $this->dbh
->escape_string($_REQUEST['is_cat']) == "true";
452 $mode = $this->dbh
->escape_string($_REQUEST['mode']);
453 $search_query = $this->dbh
->escape_string($_REQUEST['search_query']);
454 $search_lang = $this->dbh
->escape_string($_REQUEST['search_lang']);
456 catchup_feed($feed_id, $is_cat, false, $mode, [$search_query, $search_lang]);
458 print json_encode(array("message" => "UPDATE_COUNTERS"));
461 function quickAddCat() {
462 $cat = $this->dbh
->escape_string($_REQUEST["cat"]);
464 add_feed_category($cat);
466 $result = $this->dbh
->query("SELECT id FROM ttrss_feed_categories WHERE
467 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
469 if ($this->dbh
->num_rows($result) == 1) {
470 $id = $this->dbh
->fetch_result($result, 0, "id");
475 print_feed_cat_select("cat_id", $id, '');
478 function setpanelmode() {
479 $wide = (int) $_REQUEST["wide"];
481 setcookie("ttrss_widescreen", $wide,
482 time() + COOKIE_LIFETIME_LONG
);
484 print json_encode(array("wide" => $wide));
487 static function updaterandomfeed_real($dbh) {
489 // Test if the feed need a update (update interval exceded).
490 if (DB_TYPE
== "pgsql") {
491 $update_limit_qpart = "AND ((
492 ttrss_feeds.update_interval = 0
493 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
495 ttrss_feeds.update_interval > 0
496 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
497 ) OR ttrss_feeds.last_updated IS NULL
498 OR last_updated = '1970-01-01 00:00:00')";
500 $update_limit_qpart = "AND ((
501 ttrss_feeds.update_interval = 0
502 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
504 ttrss_feeds.update_interval > 0
505 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
506 ) OR ttrss_feeds.last_updated IS NULL
507 OR last_updated = '1970-01-01 00:00:00')";
510 // Test if feed is currently being updated by another process.
511 if (DB_TYPE
== "pgsql") {
512 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
514 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
517 $random_qpart = sql_random_function();
519 // we could be invoked from public.php with no active session
520 if ($_SESSION["uid"]) {
521 $owner_check_qpart = "AND ttrss_feeds.owner_uid = '".$_SESSION["uid"]."'";
523 $owner_check_qpart = "";
526 // We search for feed needing update.
527 $result = $dbh->query("SELECT ttrss_feeds.feed_url,ttrss_feeds.id
529 ttrss_feeds, ttrss_users, ttrss_user_prefs
531 ttrss_feeds.owner_uid = ttrss_users.id
532 AND ttrss_users.id = ttrss_user_prefs.owner_uid
533 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
536 $updstart_thresh_qpart
537 ORDER BY $random_qpart LIMIT 30");
541 require_once "rssfuncs.php";
547 while ($line = $dbh->fetch_assoc($result)) {
548 $feed_id = $line["id"];
550 if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
551 update_rss_feed($feed_id, true);
558 // Purge orphans and cleanup tags
560 //cleanup_tags(14, 50000);
562 if ($num_updated > 0) {
563 print json_encode(array("message" => "UPDATE_COUNTERS",
564 "num_updated" => $num_updated));
566 print json_encode(array("message" => "NOTHING_TO_UPDATE"));
571 function updaterandomfeed() {
572 RPC
::updaterandomfeed_real($this->dbh
);
575 private function markArticlesById($ids, $cmode) {
579 foreach ($ids as $id) {
580 array_push($tmp_ids, "ref_id = '$id'");
583 $ids_qpart = join(" OR ", $tmp_ids);
586 $this->dbh
->query("UPDATE ttrss_user_entries SET
587 marked = false, last_marked = NOW()
588 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
589 } else if ($cmode == 1) {
590 $this->dbh
->query("UPDATE ttrss_user_entries SET
591 marked = true, last_marked = NOW()
592 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
594 $this->dbh
->query("UPDATE ttrss_user_entries SET
595 marked = NOT marked,last_marked = NOW()
596 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
600 private function publishArticlesById($ids, $cmode) {
604 foreach ($ids as $id) {
605 array_push($tmp_ids, "ref_id = '$id'");
608 $ids_qpart = join(" OR ", $tmp_ids);
611 $this->dbh
->query("UPDATE ttrss_user_entries SET
612 published = false,last_published = NOW()
613 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
614 } else if ($cmode == 1) {
615 $this->dbh
->query("UPDATE ttrss_user_entries SET
616 published = true,last_published = NOW()
617 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
619 $this->dbh
->query("UPDATE ttrss_user_entries SET
620 published = NOT published,last_published = NOW()
621 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
624 if (PUBSUBHUBBUB_HUB
) {
625 $rss_link = get_self_url_prefix() .
626 "/public.php?op=rss&id=-2&key=" .
627 get_feed_access_key(-2, false);
629 $p = new pubsubhubbub\publisher\
Publisher(PUBSUBHUBBUB_HUB
);
631 /* $pubsub_result = */ $p->publish_update($rss_link);
635 function getlinktitlebyid() {
636 $id = $this->dbh
->escape_string($_REQUEST['id']);
638 $result = $this->dbh
->query("SELECT link, title FROM ttrss_entries, ttrss_user_entries
639 WHERE ref_id = '$id' AND ref_id = id AND owner_uid = ". $_SESSION["uid"]);
641 if ($this->dbh
->num_rows($result) != 0) {
642 $link = $this->dbh
->fetch_result($result, 0, "link");
643 $title = $this->dbh
->fetch_result($result, 0, "title");
645 echo json_encode(array("link" => $link, "title" => $title));
647 echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));
652 $msg = $this->dbh
->escape_string($_REQUEST['msg']);
653 $file = $this->dbh
->escape_string(basename($_REQUEST['file']));
654 $line = (int) $_REQUEST['line'];
655 $context = $this->dbh
->escape_string($_REQUEST['context']);
658 Logger
::get()->log_error(E_USER_WARNING
,
659 $msg, 'client-js:' . $file, $line, $context);
661 echo json_encode(array("message" => "HOST_ERROR_LOGGED"));
663 echo json_encode(array("error" => "MESSAGE_NOT_FOUND"));