]>
git.wh0rd.org - tt-rss.git/blob - classes/rpc.php
d8ab5d37f1f939e8e0f3702d7d9feaaa2937216b
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 function remprofiles() {
15 $ids = explode(",", trim($_REQUEST["ids"]));
17 foreach ($ids as $id) {
18 if ($_SESSION["profile"] != $id) {
19 $sth = $this->pdo
->prepare("DELETE FROM ttrss_settings_profiles WHERE id = ? AND
21 $sth->execute([$id, $_SESSION['uid']]);
27 function addprofile() {
28 $title = trim($_REQUEST["title"]);
31 $this->pdo
->beginTransaction();
33 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_settings_profiles
34 WHERE title = ? AND owner_uid = ?");
35 $sth->execute([$title, $_SESSION['uid']]);
39 $sth = $this->pdo
->prepare("INSERT INTO ttrss_settings_profiles (title, owner_uid)
42 $sth->execute([$title, $_SESSION['uid']]);
44 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_settings_profiles WHERE
45 title = ? AND owner_uid = ?");
46 $sth->execute([$title, $_SESSION['uid']]);
48 if ($row = $sth->fetch()) {
49 $profile_id = $row['id'];
52 initialize_user_prefs($_SESSION["uid"], $profile_id);
61 function saveprofile() {
62 $id = $_REQUEST["id"];
63 $title = trim($_REQUEST["value"]);
66 print __("Default profile");
71 $sth = $this->pdo
->prepare("UPDATE ttrss_settings_profiles
72 SET title = ? WHERE id = ? AND
75 $sth->execute([$title, $id, $_SESSION['uid']]);
81 function remarchive() {
82 $ids = explode(",", $_REQUEST["ids"]);
84 $sth = $this->pdo
->prepare("DELETE FROM ttrss_archived_feeds WHERE
85 (SELECT COUNT(*) FROM ttrss_user_entries
86 WHERE orig_feed_id = :id) = 0 AND
87 id = :id AND owner_uid = :uid");
89 foreach ($ids as $id) {
90 $sth->execute([":id" => $id, ":uid" => $_SESSION['uid']]);
95 $feed = $_REQUEST['feed'];
96 $cat = $_REQUEST['cat'];
97 $login = $_REQUEST['login'];
98 $pass = trim($_REQUEST['pass']);
100 $rc = Feeds
::subscribe_to_feed($feed, $cat, $login, $pass);
102 print json_encode(array("result" => $rc));
105 function togglepref() {
106 $key = $_REQUEST["key"];
107 set_pref($key, !get_pref($key));
108 $value = get_pref($key);
110 print json_encode(array("param" =>$key, "value" => $value));
114 // set_pref escapes input, so no need to double escape it here
115 $key = $_REQUEST['key'];
116 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
118 set_pref($key, $value, false, $key != 'USER_STYLESHEET');
120 print json_encode(array("param" =>$key, "value" => $value));
124 $mark = $_REQUEST["mark"];
125 $id = $_REQUEST["id"];
127 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET marked = ?,
129 WHERE ref_id = ? AND owner_uid = ?");
131 $sth->execute([$mark, $id, $_SESSION['uid']]);
133 print json_encode(array("message" => "UPDATE_COUNTERS"));
137 $ids = explode(",", $_REQUEST["ids"]);
138 $ids_qmarks = arr_qmarks($ids);
140 $sth = $this->pdo
->prepare("DELETE FROM ttrss_user_entries
141 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
142 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
144 Article
::purge_orphans();
146 print json_encode(array("message" => "UPDATE_COUNTERS"));
149 function unarchive() {
150 $ids = explode(",", $_REQUEST["ids"]);
152 foreach ($ids as $id) {
153 $this->pdo
->beginTransaction();
155 $sth = $this->pdo
->prepare("SELECT feed_url,site_url,title FROM ttrss_archived_feeds
156 WHERE id = (SELECT orig_feed_id FROM ttrss_user_entries WHERE ref_id = :id
157 AND owner_uid = :uid) AND owner_uid = :uid");
158 $sth->execute([":uid" => $_SESSION['uid'], ":id" => $id]);
160 if ($row = $sth->fetch()) {
161 $feed_url = $row['feed_url'];
162 $site_url = $row['site_url'];
163 $title = $row['title'];
165 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
167 $sth->execute([$feed_url, $_SESSION['uid']]);
169 if ($row = $sth->fetch()) {
170 $feed_id = $row["id"];
172 if (!$title) $title = '[Unknown]';
174 $sth = $this->pdo
->prepare("INSERT INTO ttrss_feeds
175 (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
176 VALUES (?, ?, ?, ?, NULL, '', '', 0)");
177 $sth->execute([$_SESSION['uid'], $feed_url, $site_url, $title]);
179 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
181 $sth->execute([$feed_url, $_SESSION['uid']]);
183 if ($row = $sth->fetch()) {
184 $feed_id = $row['id'];
189 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries
190 SET feed_id = ?, orig_feed_id = NULL
191 WHERE ref_id = ? AND owner_uid = ?");
192 $sth->execute([$feed_id, $id, $_SESSION['uid']]);
196 $this->pdo
->commit();
199 print json_encode(array("message" => "UPDATE_COUNTERS"));
203 $ids = explode(",", $_REQUEST["ids"]);
205 foreach ($ids as $id) {
206 $this->archive_article($id, $_SESSION["uid"]);
209 print json_encode(array("message" => "UPDATE_COUNTERS"));
212 private function archive_article($id, $owner_uid) {
213 $this->pdo
->beginTransaction();
215 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
217 $sth = $this->pdo
->prepare("SELECT feed_id FROM ttrss_user_entries
218 WHERE ref_id = ? AND owner_uid = ?");
219 $sth->execute([$id, $owner_uid]);
221 if ($row = $sth->fetch()) {
223 /* prepare the archived table */
225 $feed_id = (int) $row['feed_id'];
228 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_archived_feeds
229 WHERE id = ? AND owner_uid = ?");
230 $sth->execute([$feed_id, $owner_uid]);
232 if ($row = $sth->fetch()) {
233 $new_feed_id = $row['id'];
235 $row = $this->pdo
->query("SELECT MAX(id) AS id FROM ttrss_archived_feeds")->fetch();
236 $new_feed_id = (int)$row['id'] +
1;
238 $sth = $this->pdo
->prepare("INSERT INTO ttrss_archived_feeds
239 (id, owner_uid, title, feed_url, site_url)
240 SELECT ?, owner_uid, title, feed_url, site_url from ttrss_feeds
243 $sth->execute([$new_feed_id, $feed_id]);
246 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries
247 SET orig_feed_id = ?, feed_id = NULL
248 WHERE ref_id = ? AND owner_uid = ?");
249 $sth->execute([$new_feed_id, $id, $owner_uid]);
253 $this->pdo
->commit();
257 $pub = $_REQUEST["pub"];
258 $id = $_REQUEST["id"];
260 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
261 published = ?, last_published = NOW()
262 WHERE ref_id = ? AND owner_uid = ?");
264 $sth->execute([$pub, $id, $_SESSION['uid']]);
266 print json_encode(array("message" => "UPDATE_COUNTERS"));
269 function getAllCounters() {
270 $last_article_id = (int) $_REQUEST["last_article_id"];
274 if (!empty($_REQUEST['seq'])) $reply['seq'] = (int) $_REQUEST['seq'];
276 if ($last_article_id != Article
::getLastArticleId()) {
277 $reply['counters'] = Counters
::getAllCounters();
280 $reply['runtime-info'] = make_runtime_info();
282 print json_encode($reply);
285 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
286 function catchupSelected() {
287 $ids = explode(",", $_REQUEST["ids"]);
288 $cmode = sprintf("%d", $_REQUEST["cmode"]);
290 Article
::catchupArticlesById($ids, $cmode);
292 print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids));
295 function markSelected() {
296 $ids = explode(",", $_REQUEST["ids"]);
297 $cmode = (int)$_REQUEST["cmode"];
299 $this->markArticlesById($ids, $cmode);
301 print json_encode(array("message" => "UPDATE_COUNTERS"));
304 function publishSelected() {
305 $ids = explode(",", $_REQUEST["ids"]);
306 $cmode = (int)$_REQUEST["cmode"];
308 $this->publishArticlesById($ids, $cmode);
310 print json_encode(array("message" => "UPDATE_COUNTERS"));
313 function sanityCheck() {
314 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
315 $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true";
316 $_SESSION["hasMp3"] = $_REQUEST["hasMp3"] === "true";
317 $_SESSION["clientTzOffset"] = $_REQUEST["clientTzOffset"];
321 $reply['error'] = sanity_check();
323 if ($reply['error']['code'] == 0) {
324 $reply['init-params'] = make_init_params();
325 $reply['runtime-info'] = make_runtime_info(true);
328 print json_encode($reply);
331 function completeLabels() {
332 $search = $_REQUEST["search"];
334 $sth = $this->pdo
->query("SELECT DISTINCT caption FROM
336 WHERE owner_uid = ? AND
337 LOWER(caption) LIKE LOWER(?) ORDER BY caption
339 $sth->execute([$_SESSION['uid'], "%$search%"]);
342 while ($line = $sth->fetch()) {
343 print "<li>" . $line["caption"] . "</li>";
349 $ids = explode(",", $_REQUEST["ids"]);
350 $days = (int) $_REQUEST["days"];
352 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE
353 id = ? AND owner_uid = ?");
355 foreach ($ids as $id) {
356 $sth->execute([$id, $_SESSION['uid']]);
359 purge_feed($id, $days);
364 function updateFeedBrowser() {
365 if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER
) return;
367 $search = $_REQUEST["search"];
368 $limit = $_REQUEST["limit"];
369 $mode = (int) $_REQUEST["mode"];
371 require_once "feedbrowser.php";
373 print json_encode(array("content" =>
374 make_feed_browser($search, $limit, $mode),
379 function massSubscribe() {
381 $payload = json_decode($_REQUEST["payload"], false);
382 $mode = $_REQUEST["mode"];
384 if (!$payload ||
!is_array($payload)) return;
387 foreach ($payload as $feed) {
390 $feed_url = $feed[1];
392 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE
393 feed_url = ? AND owner_uid = ?");
394 $sth->execute([$feed_url, $_SESSION['uid']]);
396 if (!$sth->fetch()) {
397 $sth = $this->pdo
->prepare("INSERT INTO ttrss_feeds
398 (owner_uid,feed_url,title,cat_id,site_url)
399 VALUES (?, ?, ?, NULL, '')");
401 $sth->execute([$_SESSION['uid'], $feed_url, $title]);
404 } else if ($mode == 2) {
406 foreach ($payload as $id) {
407 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_archived_feeds
408 WHERE id = ? AND owner_uid = ?");
409 $sth->execute([$id, $_SESSION['uid']]);
411 if ($row = $sth->fetch()) {
412 $site_url = $row['site_url'];
413 $feed_url = $row['feed_url'];
414 $title = $row['title'];
416 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE
417 feed_url = ? AND owner_uid = ?");
418 $sth->execute([$feed_url, $_SESSION['uid']]);
420 if (!$sth->fetch()) {
421 $sth = $this->pdo
->prepare("INSERT INTO ttrss_feeds
422 (owner_uid,feed_url,title,cat_id,site_url)
423 VALUES (?, ?, ?, NULL, ?)");
425 $sth->execute([$_SESSION['uid'], $feed_url, $title, $site_url]);
432 function catchupFeed() {
433 $feed_id = $_REQUEST['feed_id'];
434 $is_cat = $_REQUEST['is_cat'] == "true";
435 $mode = $_REQUEST['mode'];
436 $search_query = $_REQUEST['search_query'];
437 $search_lang = $_REQUEST['search_lang'];
439 Feeds
::catchup_feed($feed_id, $is_cat, false, $mode, [$search_query, $search_lang]);
441 print json_encode(array("message" => "UPDATE_COUNTERS"));
444 function setpanelmode() {
445 $wide = (int) $_REQUEST["wide"];
447 setcookie("ttrss_widescreen", $wide,
448 time() + COOKIE_LIFETIME_LONG
);
450 print json_encode(array("wide" => $wide));
453 static function updaterandomfeed_real() {
455 // Test if the feed need a update (update interval exceded).
456 if (DB_TYPE
== "pgsql") {
457 $update_limit_qpart = "AND ((
458 ttrss_feeds.update_interval = 0
459 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
461 ttrss_feeds.update_interval > 0
462 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
463 ) OR ttrss_feeds.last_updated IS NULL
464 OR last_updated = '1970-01-01 00:00:00')";
466 $update_limit_qpart = "AND ((
467 ttrss_feeds.update_interval = 0
468 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
470 ttrss_feeds.update_interval > 0
471 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
472 ) OR ttrss_feeds.last_updated IS NULL
473 OR last_updated = '1970-01-01 00:00:00')";
476 // Test if feed is currently being updated by another process.
477 if (DB_TYPE
== "pgsql") {
478 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
480 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
483 $random_qpart = sql_random_function();
487 // we could be invoked from public.php with no active session
488 if ($_SESSION["uid"]) {
489 $owner_check_qpart = "AND ttrss_feeds.owner_uid = ".$pdo->quote($_SESSION["uid"]);
491 $owner_check_qpart = "";
494 // We search for feed needing update.
495 $res = $pdo->query("SELECT ttrss_feeds.feed_url,ttrss_feeds.id
497 ttrss_feeds, ttrss_users, ttrss_user_prefs
499 ttrss_feeds.owner_uid = ttrss_users.id
500 AND ttrss_users.id = ttrss_user_prefs.owner_uid
501 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
504 $updstart_thresh_qpart
505 ORDER BY $random_qpart LIMIT 30");
511 while ($line = $res->fetch()) {
512 $feed_id = $line["id"];
514 if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
515 RSSUtils
::update_rss_feed($feed_id, true);
522 // Purge orphans and cleanup tags
523 Article
::purge_orphans();
524 //cleanup_tags(14, 50000);
526 if ($num_updated > 0) {
527 print json_encode(array("message" => "UPDATE_COUNTERS",
528 "num_updated" => $num_updated));
530 print json_encode(array("message" => "NOTHING_TO_UPDATE"));
535 function updaterandomfeed() {
536 RPC
::updaterandomfeed_real();
539 private function markArticlesById($ids, $cmode) {
541 $ids_qmarks = arr_qmarks($ids);
544 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
545 marked = false, last_marked = NOW()
546 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
547 } else if ($cmode == 1) {
548 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
549 marked = true, last_marked = NOW()
550 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
552 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
553 marked = NOT marked,last_marked = NOW()
554 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
557 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
560 private function publishArticlesById($ids, $cmode) {
562 $ids_qmarks = arr_qmarks($ids);
565 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
566 published = false, last_published = NOW()
567 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
568 } else if ($cmode == 1) {
569 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
570 published = true, last_published = NOW()
571 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
573 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
574 published = NOT published,last_published = NOW()
575 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
578 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
581 function getlinktitlebyid() {
582 $id = $_REQUEST['id'];
584 $sth = $this->pdo
->prepare("SELECT link, title FROM ttrss_entries, ttrss_user_entries
585 WHERE ref_id = ? AND ref_id = id AND owner_uid = ?");
586 $sth->execute([$id, $_SESSION['uid']]);
588 if ($row = $sth->fetch()) {
589 $link = $row['link'];
590 $title = $row['title'];
592 echo json_encode(array("link" => $link, "title" => $title));
594 echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));
599 $msg = $_REQUEST['msg'];
600 $file = basename($_REQUEST['file']);
601 $line = (int) $_REQUEST['line'];
602 $context = $_REQUEST['context'];
605 Logger
::get()->log_error(E_USER_WARNING
,
606 $msg, 'client-js:' . $file, $line, $context);
608 echo json_encode(array("message" => "HOST_ERROR_LOGGED"));
610 echo json_encode(array("error" => "MESSAGE_NOT_FOUND"));