]>
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", "saveprofile");
7 return array_search($method, $csrf_ignored) !== false;
10 function setprofile() {
11 $_SESSION["profile"] = $_REQUEST["id"];
14 if (!$_SESSION["profile"]) $_SESSION["profile"] = null;
17 function remprofiles() {
18 $ids = explode(",", trim($_REQUEST["ids"]));
20 foreach ($ids as $id) {
21 if ($_SESSION["profile"] != $id) {
22 $sth = $this->pdo
->prepare("DELETE FROM ttrss_settings_profiles WHERE id = ? AND
24 $sth->execute([$id, $_SESSION['uid']]);
30 function addprofile() {
31 $title = trim($_REQUEST["title"]);
34 $this->pdo
->beginTransaction();
36 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_settings_profiles
37 WHERE title = ? AND owner_uid = ?");
38 $sth->execute([$title, $_SESSION['uid']]);
42 $sth = $this->pdo
->prepare("INSERT INTO ttrss_settings_profiles (title, owner_uid)
45 $sth->execute([$title, $_SESSION['uid']]);
47 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_settings_profiles WHERE
48 title = ? AND owner_uid = ?");
49 $sth->execute([$title, $_SESSION['uid']]);
51 if ($row = $sth->fetch()) {
52 $profile_id = $row['id'];
55 initialize_user_prefs($_SESSION["uid"], $profile_id);
64 function saveprofile() {
65 $id = $_REQUEST["id"];
66 $title = trim($_REQUEST["value"]);
69 print __("Default profile");
74 $sth = $this->pdo
->prepare("UPDATE ttrss_settings_profiles
75 SET title = ? WHERE id = ? AND
78 $sth->execute([$title, $id, $_SESSION['uid']]);
84 function remarchive() {
85 $ids = explode(",", $_REQUEST["ids"]);
87 $sth = $this->pdo
->prepare("DELETE FROM ttrss_archived_feeds WHERE
88 (SELECT COUNT(*) FROM ttrss_user_entries
89 WHERE orig_feed_id = :id) = 0 AND
90 id = :id AND owner_uid = :uid");
92 foreach ($ids as $id) {
93 $sth->execute([":id" => $id, ":uid" => $_SESSION['uid']]);
98 $feed = $_REQUEST['feed'];
99 $cat = $_REQUEST['cat'];
100 $login = $_REQUEST['login'];
101 $pass = trim($_REQUEST['pass']);
103 $rc = Feeds
::subscribe_to_feed($feed, $cat, $login, $pass);
105 print json_encode(array("result" => $rc));
108 function togglepref() {
109 $key = $_REQUEST["key"];
110 set_pref($key, !get_pref($key));
111 $value = get_pref($key);
113 print json_encode(array("param" =>$key, "value" => $value));
117 // set_pref escapes input, so no need to double escape it here
118 $key = $_REQUEST['key'];
119 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
121 set_pref($key, $value, false, $key != 'USER_STYLESHEET');
123 print json_encode(array("param" =>$key, "value" => $value));
127 $mark = $_REQUEST["mark"];
128 $id = $_REQUEST["id"];
130 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET marked = ?,
132 WHERE ref_id = ? AND owner_uid = ?");
134 $sth->execute([$mark, $id, $_SESSION['uid']]);
136 print json_encode(array("message" => "UPDATE_COUNTERS"));
140 $ids = explode(",", $_REQUEST["ids"]);
141 $ids_qmarks = arr_qmarks($ids);
143 $sth = $this->pdo
->prepare("DELETE FROM ttrss_user_entries
144 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
145 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
147 Article
::purge_orphans();
149 print json_encode(array("message" => "UPDATE_COUNTERS"));
152 function unarchive() {
153 $ids = explode(",", $_REQUEST["ids"]);
155 foreach ($ids as $id) {
156 $this->pdo
->beginTransaction();
158 $sth = $this->pdo
->prepare("SELECT feed_url,site_url,title FROM ttrss_archived_feeds
159 WHERE id = (SELECT orig_feed_id FROM ttrss_user_entries WHERE ref_id = :id
160 AND owner_uid = :uid) AND owner_uid = :uid");
161 $sth->execute([":uid" => $_SESSION['uid'], ":id" => $id]);
163 if ($row = $sth->fetch()) {
164 $feed_url = $row['feed_url'];
165 $site_url = $row['site_url'];
166 $title = $row['title'];
168 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
170 $sth->execute([$feed_url, $_SESSION['uid']]);
172 if ($row = $sth->fetch()) {
173 $feed_id = $row["id"];
175 if (!$title) $title = '[Unknown]';
177 $sth = $this->pdo
->prepare("INSERT INTO ttrss_feeds
178 (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
179 VALUES (?, ?, ?, ?, NULL, '', '', 0)");
180 $sth->execute([$_SESSION['uid'], $feed_url, $site_url, $title]);
182 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
184 $sth->execute([$feed_url, $_SESSION['uid']]);
186 if ($row = $sth->fetch()) {
187 $feed_id = $row['id'];
192 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries
193 SET feed_id = ?, orig_feed_id = NULL
194 WHERE ref_id = ? AND owner_uid = ?");
195 $sth->execute([$feed_id, $id, $_SESSION['uid']]);
199 $this->pdo
->commit();
202 print json_encode(array("message" => "UPDATE_COUNTERS"));
206 $ids = explode(",", $_REQUEST["ids"]);
208 foreach ($ids as $id) {
209 $this->archive_article($id, $_SESSION["uid"]);
212 print json_encode(array("message" => "UPDATE_COUNTERS"));
215 private function archive_article($id, $owner_uid) {
216 $this->pdo
->beginTransaction();
218 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
220 $sth = $this->pdo
->prepare("SELECT feed_id FROM ttrss_user_entries
221 WHERE ref_id = ? AND owner_uid = ?");
222 $sth->execute([$id, $owner_uid]);
224 if ($row = $sth->fetch()) {
226 /* prepare the archived table */
228 $feed_id = (int) $row['feed_id'];
231 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_archived_feeds
232 WHERE id = ? AND owner_uid = ?");
233 $sth->execute([$feed_id, $owner_uid]);
235 if ($row = $sth->fetch()) {
236 $new_feed_id = $row['id'];
238 $row = $this->pdo
->query("SELECT MAX(id) AS id FROM ttrss_archived_feeds")->fetch();
239 $new_feed_id = (int)$row['id'] +
1;
241 $sth = $this->pdo
->prepare("INSERT INTO ttrss_archived_feeds
242 (id, owner_uid, title, feed_url, site_url)
243 SELECT ?, owner_uid, title, feed_url, site_url from ttrss_feeds
246 $sth->execute([$new_feed_id, $feed_id]);
249 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries
250 SET orig_feed_id = ?, feed_id = NULL
251 WHERE ref_id = ? AND owner_uid = ?");
252 $sth->execute([$new_feed_id, $id, $owner_uid]);
256 $this->pdo
->commit();
260 $pub = $_REQUEST["pub"];
261 $id = $_REQUEST["id"];
263 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
264 published = ?, last_published = NOW()
265 WHERE ref_id = ? AND owner_uid = ?");
267 $sth->execute([$pub, $id, $_SESSION['uid']]);
269 print json_encode(array("message" => "UPDATE_COUNTERS"));
272 function getAllCounters() {
273 $last_article_id = (int) $_REQUEST["last_article_id"];
277 if (!empty($_REQUEST['seq'])) $reply['seq'] = (int) $_REQUEST['seq'];
279 if ($last_article_id != Article
::getLastArticleId()) {
280 $reply['counters'] = Counters
::getAllCounters();
283 $reply['runtime-info'] = make_runtime_info();
285 print json_encode($reply);
288 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
289 function catchupSelected() {
290 $ids = explode(",", $_REQUEST["ids"]);
291 $cmode = sprintf("%d", $_REQUEST["cmode"]);
293 Article
::catchupArticlesById($ids, $cmode);
295 print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids));
298 function markSelected() {
299 $ids = explode(",", $_REQUEST["ids"]);
300 $cmode = (int)$_REQUEST["cmode"];
302 $this->markArticlesById($ids, $cmode);
304 print json_encode(array("message" => "UPDATE_COUNTERS"));
307 function publishSelected() {
308 $ids = explode(",", $_REQUEST["ids"]);
309 $cmode = (int)$_REQUEST["cmode"];
311 $this->publishArticlesById($ids, $cmode);
313 print json_encode(array("message" => "UPDATE_COUNTERS"));
316 function sanityCheck() {
317 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
318 $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true";
319 $_SESSION["hasMp3"] = $_REQUEST["hasMp3"] === "true";
320 $_SESSION["clientTzOffset"] = $_REQUEST["clientTzOffset"];
324 $reply['error'] = sanity_check();
326 if ($reply['error']['code'] == 0) {
327 $reply['init-params'] = make_init_params();
328 $reply['runtime-info'] = make_runtime_info(true);
331 print json_encode($reply);
334 function completeLabels() {
335 $search = $_REQUEST["search"];
337 $sth = $this->pdo
->query("SELECT DISTINCT caption FROM
339 WHERE owner_uid = ? AND
340 LOWER(caption) LIKE LOWER(?) ORDER BY caption
342 $sth->execute([$_SESSION['uid'], "%$search%"]);
345 while ($line = $sth->fetch()) {
346 print "<li>" . $line["caption"] . "</li>";
352 $ids = explode(",", $_REQUEST["ids"]);
353 $days = (int) $_REQUEST["days"];
355 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE
356 id = ? AND owner_uid = ?");
358 foreach ($ids as $id) {
359 $sth->execute([$id, $_SESSION['uid']]);
362 purge_feed($id, $days);
367 function updateFeedBrowser() {
368 if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER
) return;
370 $search = $_REQUEST["search"];
371 $limit = $_REQUEST["limit"];
372 $mode = (int) $_REQUEST["mode"];
374 require_once "feedbrowser.php";
376 print json_encode(array("content" =>
377 make_feed_browser($search, $limit, $mode),
382 function massSubscribe() {
384 $payload = json_decode($_REQUEST["payload"], false);
385 $mode = $_REQUEST["mode"];
387 if (!$payload ||
!is_array($payload)) return;
390 foreach ($payload as $feed) {
393 $feed_url = $feed[1];
395 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE
396 feed_url = ? AND owner_uid = ?");
397 $sth->execute([$feed_url, $_SESSION['uid']]);
399 if (!$sth->fetch()) {
400 $sth = $this->pdo
->prepare("INSERT INTO ttrss_feeds
401 (owner_uid,feed_url,title,cat_id,site_url)
402 VALUES (?, ?, ?, NULL, '')");
404 $sth->execute([$_SESSION['uid'], $feed_url, $title]);
407 } else if ($mode == 2) {
409 foreach ($payload as $id) {
410 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_archived_feeds
411 WHERE id = ? AND owner_uid = ?");
412 $sth->execute([$id, $_SESSION['uid']]);
414 if ($row = $sth->fetch()) {
415 $site_url = $row['site_url'];
416 $feed_url = $row['feed_url'];
417 $title = $row['title'];
419 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE
420 feed_url = ? AND owner_uid = ?");
421 $sth->execute([$feed_url, $_SESSION['uid']]);
423 if (!$sth->fetch()) {
424 $sth = $this->pdo
->prepare("INSERT INTO ttrss_feeds
425 (owner_uid,feed_url,title,cat_id,site_url)
426 VALUES (?, ?, ?, NULL, ?)");
428 $sth->execute([$_SESSION['uid'], $feed_url, $title, $site_url]);
435 function catchupFeed() {
436 $feed_id = $_REQUEST['feed_id'];
437 $is_cat = $_REQUEST['is_cat'] == "true";
438 $mode = $_REQUEST['mode'];
439 $search_query = $_REQUEST['search_query'];
440 $search_lang = $_REQUEST['search_lang'];
442 Feeds
::catchup_feed($feed_id, $is_cat, false, $mode, [$search_query, $search_lang]);
444 print json_encode(array("message" => "UPDATE_COUNTERS"));
447 function setpanelmode() {
448 $wide = (int) $_REQUEST["wide"];
450 setcookie("ttrss_widescreen", $wide,
451 time() + COOKIE_LIFETIME_LONG
);
453 print json_encode(array("wide" => $wide));
456 static function updaterandomfeed_real() {
458 // Test if the feed need a update (update interval exceded).
459 if (DB_TYPE
== "pgsql") {
460 $update_limit_qpart = "AND ((
461 ttrss_feeds.update_interval = 0
462 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
464 ttrss_feeds.update_interval > 0
465 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
466 ) OR ttrss_feeds.last_updated IS NULL
467 OR last_updated = '1970-01-01 00:00:00')";
469 $update_limit_qpart = "AND ((
470 ttrss_feeds.update_interval = 0
471 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
473 ttrss_feeds.update_interval > 0
474 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
475 ) OR ttrss_feeds.last_updated IS NULL
476 OR last_updated = '1970-01-01 00:00:00')";
479 // Test if feed is currently being updated by another process.
480 if (DB_TYPE
== "pgsql") {
481 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
483 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
486 $random_qpart = sql_random_function();
490 // we could be invoked from public.php with no active session
491 if ($_SESSION["uid"]) {
492 $owner_check_qpart = "AND ttrss_feeds.owner_uid = ".$pdo->quote($_SESSION["uid"]);
494 $owner_check_qpart = "";
497 // We search for feed needing update.
498 $res = $pdo->query("SELECT ttrss_feeds.feed_url,ttrss_feeds.id
500 ttrss_feeds, ttrss_users, ttrss_user_prefs
502 ttrss_feeds.owner_uid = ttrss_users.id
503 AND ttrss_users.id = ttrss_user_prefs.owner_uid
504 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
507 $updstart_thresh_qpart
508 ORDER BY $random_qpart LIMIT 30");
514 while ($line = $res->fetch()) {
515 $feed_id = $line["id"];
517 if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
518 RSSUtils
::update_rss_feed($feed_id, true);
525 // Purge orphans and cleanup tags
526 Article
::purge_orphans();
527 //cleanup_tags(14, 50000);
529 if ($num_updated > 0) {
530 print json_encode(array("message" => "UPDATE_COUNTERS",
531 "num_updated" => $num_updated));
533 print json_encode(array("message" => "NOTHING_TO_UPDATE"));
538 function updaterandomfeed() {
539 RPC
::updaterandomfeed_real();
542 private function markArticlesById($ids, $cmode) {
544 $ids_qmarks = arr_qmarks($ids);
547 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
548 marked = false, last_marked = NOW()
549 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
550 } else if ($cmode == 1) {
551 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
552 marked = true, last_marked = NOW()
553 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
555 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
556 marked = NOT marked,last_marked = NOW()
557 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
560 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
563 private function publishArticlesById($ids, $cmode) {
565 $ids_qmarks = arr_qmarks($ids);
568 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
569 published = false, last_published = NOW()
570 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
571 } else if ($cmode == 1) {
572 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
573 published = true, last_published = NOW()
574 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
576 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
577 published = NOT published,last_published = NOW()
578 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
581 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
584 function getlinktitlebyid() {
585 $id = $_REQUEST['id'];
587 $sth = $this->pdo
->prepare("SELECT link, title FROM ttrss_entries, ttrss_user_entries
588 WHERE ref_id = ? AND ref_id = id AND owner_uid = ?");
589 $sth->execute([$id, $_SESSION['uid']]);
591 if ($row = $sth->fetch()) {
592 $link = $row['link'];
593 $title = $row['title'];
595 echo json_encode(array("link" => $link, "title" => $title));
597 echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));
602 $msg = $_REQUEST['msg'];
603 $file = basename($_REQUEST['file']);
604 $line = (int) $_REQUEST['line'];
605 $context = $_REQUEST['context'];
608 Logger
::get()->log_error(E_USER_WARNING
,
609 $msg, 'client-js:' . $file, $line, $context);
611 echo json_encode(array("message" => "HOST_ERROR_LOGGED"));
613 echo json_encode(array("error" => "MESSAGE_NOT_FOUND"));