rpc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <?php
  2. class RPC extends Handler_Protected {
  3. function csrf_ignore($method) {
  4. $csrf_ignored = array("sanitycheck", "completelabels", "saveprofile");
  5. return array_search($method, $csrf_ignored) !== false;
  6. }
  7. function setprofile() {
  8. $_SESSION["profile"] = $_REQUEST["id"];
  9. }
  10. function remprofiles() {
  11. $ids = explode(",", trim($_REQUEST["ids"]));
  12. foreach ($ids as $id) {
  13. if ($_SESSION["profile"] != $id) {
  14. $sth = $this->pdo->prepare("DELETE FROM ttrss_settings_profiles WHERE id = ? AND
  15. owner_uid = ?");
  16. $sth->execute([$id, $_SESSION['uid']]);
  17. }
  18. }
  19. }
  20. // Silent
  21. function addprofile() {
  22. $title = trim($_REQUEST["title"]);
  23. if ($title) {
  24. $this->pdo->beginTransaction();
  25. $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles
  26. WHERE title = ? AND owner_uid = ?");
  27. $sth->execute([$title, $_SESSION['uid']]);
  28. if (!$sth->fetch()) {
  29. $sth = $this->pdo->prepare("INSERT INTO ttrss_settings_profiles (title, owner_uid)
  30. VALUES (?, ?)");
  31. $sth->execute([$title, $_SESSION['uid']]);
  32. $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles WHERE
  33. title = ? AND owner_uid = ?");
  34. $sth->execute([$title, $_SESSION['uid']]);
  35. if ($row = $sth->fetch()) {
  36. $profile_id = $row['id'];
  37. if ($profile_id) {
  38. initialize_user_prefs($_SESSION["uid"], $profile_id);
  39. }
  40. }
  41. }
  42. $this->pdo->commit();
  43. }
  44. }
  45. function saveprofile() {
  46. $id = $_REQUEST["id"];
  47. $title = trim($_REQUEST["value"]);
  48. if ($id == 0) {
  49. print __("Default profile");
  50. return;
  51. }
  52. if ($title) {
  53. $sth = $this->pdo->prepare("UPDATE ttrss_settings_profiles
  54. SET title = ? WHERE id = ? AND
  55. owner_uid = ?");
  56. $sth->execute([$title, $id, $_SESSION['uid']]);
  57. print $title;
  58. }
  59. }
  60. // Silent
  61. function remarchive() {
  62. $ids = explode(",", $_REQUEST["ids"]);
  63. $sth = $this->pdo->prepare("DELETE FROM ttrss_archived_feeds WHERE
  64. (SELECT COUNT(*) FROM ttrss_user_entries
  65. WHERE orig_feed_id = :id) = 0 AND
  66. id = :id AND owner_uid = :uid");
  67. foreach ($ids as $id) {
  68. $sth->execute([":id" => $id, ":uid" => $_SESSION['uid']]);
  69. }
  70. }
  71. function addfeed() {
  72. $feed = $_REQUEST['feed'];
  73. $cat = $_REQUEST['cat'];
  74. $login = $_REQUEST['login'];
  75. $pass = trim($_REQUEST['pass']);
  76. $rc = Feeds::subscribe_to_feed($feed, $cat, $login, $pass);
  77. print json_encode(array("result" => $rc));
  78. }
  79. function togglepref() {
  80. $key = $_REQUEST["key"];
  81. set_pref($key, !get_pref($key));
  82. $value = get_pref($key);
  83. print json_encode(array("param" =>$key, "value" => $value));
  84. }
  85. function setpref() {
  86. // set_pref escapes input, so no need to double escape it here
  87. $key = $_REQUEST['key'];
  88. $value = str_replace("\n", "<br/>", $_REQUEST['value']);
  89. set_pref($key, $value, false, $key != 'USER_STYLESHEET');
  90. print json_encode(array("param" =>$key, "value" => $value));
  91. }
  92. function mark() {
  93. $mark = $_REQUEST["mark"];
  94. $id = $_REQUEST["id"];
  95. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET marked = ?,
  96. last_marked = NOW()
  97. WHERE ref_id = ? AND owner_uid = ?");
  98. $sth->execute([$mark, $id, $_SESSION['uid']]);
  99. print json_encode(array("message" => "UPDATE_COUNTERS"));
  100. }
  101. function delete() {
  102. $ids = explode(",", $_REQUEST["ids"]);
  103. $ids_qmarks = arr_qmarks($ids);
  104. $sth = $this->pdo->prepare("DELETE FROM ttrss_user_entries
  105. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  106. $sth->execute(array_merge($ids, [$_SESSION['uid']]));
  107. Article::purge_orphans();
  108. print json_encode(array("message" => "UPDATE_COUNTERS"));
  109. }
  110. function unarchive() {
  111. $ids = explode(",", $_REQUEST["ids"]);
  112. foreach ($ids as $id) {
  113. $this->pdo->beginTransaction();
  114. $sth = $this->pdo->prepare("SELECT feed_url,site_url,title FROM ttrss_archived_feeds
  115. WHERE id = (SELECT orig_feed_id FROM ttrss_user_entries WHERE ref_id = :id
  116. AND owner_uid = :uid) AND owner_uid = :uid");
  117. $sth->execute([":uid" => $_SESSION['uid'], ":id" => $id]);
  118. if ($row = $sth->fetch()) {
  119. $feed_url = $row['feed_url'];
  120. $site_url = $row['site_url'];
  121. $title = $row['title'];
  122. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
  123. AND owner_uid = ?");
  124. $sth->execute([$feed_url, $_SESSION['uid']]);
  125. if ($row = $sth->fetch()) {
  126. $feed_id = $row["id"];
  127. } else {
  128. if (!$title) $title = '[Unknown]';
  129. $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds
  130. (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
  131. VALUES (?, ?, ?, ?, NULL, '', '', 0)");
  132. $sth->execute([$_SESSION['uid'], $feed_url, $site_url, $title]);
  133. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
  134. AND owner_uid = ?");
  135. $sth->execute([$feed_url, $_SESSION['uid']]);
  136. if ($row = $sth->fetch()) {
  137. $feed_id = $row['id'];
  138. }
  139. }
  140. if ($feed_id) {
  141. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries
  142. SET feed_id = ?, orig_feed_id = NULL
  143. WHERE ref_id = ? AND owner_uid = ?");
  144. $sth->execute([$feed_id, $id, $_SESSION['uid']]);
  145. }
  146. }
  147. $this->pdo->commit();
  148. }
  149. print json_encode(array("message" => "UPDATE_COUNTERS"));
  150. }
  151. function archive() {
  152. $ids = explode(",", $_REQUEST["ids"]);
  153. foreach ($ids as $id) {
  154. $this->archive_article($id, $_SESSION["uid"]);
  155. }
  156. print json_encode(array("message" => "UPDATE_COUNTERS"));
  157. }
  158. private function archive_article($id, $owner_uid) {
  159. $this->pdo->beginTransaction();
  160. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  161. $sth = $this->pdo->prepare("SELECT feed_id FROM ttrss_user_entries
  162. WHERE ref_id = ? AND owner_uid = ?");
  163. $sth->execute([$id, $owner_uid]);
  164. if ($row = $sth->fetch()) {
  165. /* prepare the archived table */
  166. $feed_id = (int) $row['feed_id'];
  167. if ($feed_id) {
  168. $sth = $this->pdo->prepare("SELECT id FROM ttrss_archived_feeds
  169. WHERE id = ? AND owner_uid = ?");
  170. $sth->execute([$feed_id, $owner_uid]);
  171. if ($row = $sth->fetch()) {
  172. $new_feed_id = $row['id'];
  173. } else {
  174. $row = $this->pdo->query("SELECT MAX(id) AS id FROM ttrss_archived_feeds")->fetch();
  175. $new_feed_id = (int)$row['id'] + 1;
  176. $sth = $this->pdo->prepare("INSERT INTO ttrss_archived_feeds
  177. (id, owner_uid, title, feed_url, site_url)
  178. SELECT ?, owner_uid, title, feed_url, site_url from ttrss_feeds
  179. WHERE id = ?");
  180. $sth->execute([$new_feed_id, $feed_id]);
  181. }
  182. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries
  183. SET orig_feed_id = ?, feed_id = NULL
  184. WHERE ref_id = ? AND owner_uid = ?");
  185. $sth->execute([$new_feed_id, $id, $owner_uid]);
  186. }
  187. }
  188. $this->pdo->commit();
  189. }
  190. function publ() {
  191. $pub = $_REQUEST["pub"];
  192. $id = $_REQUEST["id"];
  193. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  194. published = ?, last_published = NOW()
  195. WHERE ref_id = ? AND owner_uid = ?");
  196. $sth->execute([$pub, $id, $_SESSION['uid']]);
  197. print json_encode(array("message" => "UPDATE_COUNTERS"));
  198. }
  199. function getAllCounters() {
  200. $last_article_id = (int) $_REQUEST["last_article_id"];
  201. $reply = array();
  202. if (!empty($_REQUEST['seq'])) $reply['seq'] = (int) $_REQUEST['seq'];
  203. if ($last_article_id != Article::getLastArticleId()) {
  204. $reply['counters'] = Counters::getAllCounters();
  205. }
  206. $reply['runtime-info'] = make_runtime_info();
  207. print json_encode($reply);
  208. }
  209. /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
  210. function catchupSelected() {
  211. $ids = explode(",", $_REQUEST["ids"]);
  212. $cmode = sprintf("%d", $_REQUEST["cmode"]);
  213. Article::catchupArticlesById($ids, $cmode);
  214. print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids));
  215. }
  216. function markSelected() {
  217. $ids = explode(",", $_REQUEST["ids"]);
  218. $cmode = (int)$_REQUEST["cmode"];
  219. $this->markArticlesById($ids, $cmode);
  220. print json_encode(array("message" => "UPDATE_COUNTERS"));
  221. }
  222. function publishSelected() {
  223. $ids = explode(",", $_REQUEST["ids"]);
  224. $cmode = (int)$_REQUEST["cmode"];
  225. $this->publishArticlesById($ids, $cmode);
  226. print json_encode(array("message" => "UPDATE_COUNTERS"));
  227. }
  228. function sanityCheck() {
  229. $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
  230. $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true";
  231. $_SESSION["hasMp3"] = $_REQUEST["hasMp3"] === "true";
  232. $_SESSION["clientTzOffset"] = $_REQUEST["clientTzOffset"];
  233. $reply = array();
  234. $reply['error'] = sanity_check();
  235. if ($reply['error']['code'] == 0) {
  236. $reply['init-params'] = make_init_params();
  237. $reply['runtime-info'] = make_runtime_info(true);
  238. }
  239. print json_encode($reply);
  240. }
  241. function completeLabels() {
  242. $search = $_REQUEST["search"];
  243. $sth = $this->pdo->query("SELECT DISTINCT caption FROM
  244. ttrss_labels2
  245. WHERE owner_uid = ? AND
  246. LOWER(caption) LIKE LOWER(?) ORDER BY caption
  247. LIMIT 5");
  248. $sth->execute([$_SESSION['uid'], "%$search%"]);
  249. print "<ul>";
  250. while ($line = $sth->fetch()) {
  251. print "<li>" . $line["caption"] . "</li>";
  252. }
  253. print "</ul>";
  254. }
  255. function purge() {
  256. $ids = explode(",", $_REQUEST["ids"]);
  257. $days = (int) $_REQUEST["days"];
  258. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE
  259. id = ? AND owner_uid = ?");
  260. foreach ($ids as $id) {
  261. $sth->execute([$id, $_SESSION['uid']]);
  262. if ($sth->fetch()) {
  263. purge_feed($id, $days);
  264. }
  265. }
  266. }
  267. function updateFeedBrowser() {
  268. if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER) return;
  269. $search = $_REQUEST["search"];
  270. $limit = $_REQUEST["limit"];
  271. $mode = (int) $_REQUEST["mode"];
  272. require_once "feedbrowser.php";
  273. print json_encode(array("content" =>
  274. make_feed_browser($search, $limit, $mode),
  275. "mode" => $mode));
  276. }
  277. // Silent
  278. function massSubscribe() {
  279. $payload = json_decode($_REQUEST["payload"], false);
  280. $mode = $_REQUEST["mode"];
  281. if (!$payload || !is_array($payload)) return;
  282. if ($mode == 1) {
  283. foreach ($payload as $feed) {
  284. $title = $feed[0];
  285. $feed_url = $feed[1];
  286. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE
  287. feed_url = ? AND owner_uid = ?");
  288. $sth->execute([$feed_url, $_SESSION['uid']]);
  289. if (!$sth->fetch()) {
  290. $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds
  291. (owner_uid,feed_url,title,cat_id,site_url)
  292. VALUES (?, ?, ?, NULL, '')");
  293. $sth->execute([$_SESSION['uid'], $feed_url, $title]);
  294. }
  295. }
  296. } else if ($mode == 2) {
  297. // feed archive
  298. foreach ($payload as $id) {
  299. $sth = $this->pdo->prepare("SELECT * FROM ttrss_archived_feeds
  300. WHERE id = ? AND owner_uid = ?");
  301. $sth->execute([$id, $_SESSION['uid']]);
  302. if ($row = $sth->fetch()) {
  303. $site_url = $row['site_url'];
  304. $feed_url = $row['feed_url'];
  305. $title = $row['title'];
  306. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE
  307. feed_url = ? AND owner_uid = ?");
  308. $sth->execute([$feed_url, $_SESSION['uid']]);
  309. if (!$sth->fetch()) {
  310. $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds
  311. (owner_uid,feed_url,title,cat_id,site_url)
  312. VALUES (?, ?, ?, NULL, ?)");
  313. $sth->execute([$_SESSION['uid'], $feed_url, $title, $site_url]);
  314. }
  315. }
  316. }
  317. }
  318. }
  319. function catchupFeed() {
  320. $feed_id = $_REQUEST['feed_id'];
  321. $is_cat = $_REQUEST['is_cat'] == "true";
  322. $mode = $_REQUEST['mode'];
  323. $search_query = $_REQUEST['search_query'];
  324. $search_lang = $_REQUEST['search_lang'];
  325. Feeds::catchup_feed($feed_id, $is_cat, false, $mode, [$search_query, $search_lang]);
  326. print json_encode(array("message" => "UPDATE_COUNTERS"));
  327. }
  328. function setpanelmode() {
  329. $wide = (int) $_REQUEST["wide"];
  330. setcookie("ttrss_widescreen", $wide,
  331. time() + COOKIE_LIFETIME_LONG);
  332. print json_encode(array("wide" => $wide));
  333. }
  334. static function updaterandomfeed_real() {
  335. // Test if the feed need a update (update interval exceded).
  336. if (DB_TYPE == "pgsql") {
  337. $update_limit_qpart = "AND ((
  338. ttrss_feeds.update_interval = 0
  339. AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
  340. ) OR (
  341. ttrss_feeds.update_interval > 0
  342. AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
  343. ) OR ttrss_feeds.last_updated IS NULL
  344. OR last_updated = '1970-01-01 00:00:00')";
  345. } else {
  346. $update_limit_qpart = "AND ((
  347. ttrss_feeds.update_interval = 0
  348. AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
  349. ) OR (
  350. ttrss_feeds.update_interval > 0
  351. AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
  352. ) OR ttrss_feeds.last_updated IS NULL
  353. OR last_updated = '1970-01-01 00:00:00')";
  354. }
  355. // Test if feed is currently being updated by another process.
  356. if (DB_TYPE == "pgsql") {
  357. $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
  358. } else {
  359. $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
  360. }
  361. $random_qpart = sql_random_function();
  362. $pdo = Db::pdo();
  363. // we could be invoked from public.php with no active session
  364. if ($_SESSION["uid"]) {
  365. $owner_check_qpart = "AND ttrss_feeds.owner_uid = ".$pdo->quote($_SESSION["uid"]);
  366. } else {
  367. $owner_check_qpart = "";
  368. }
  369. // We search for feed needing update.
  370. $res = $pdo->query("SELECT ttrss_feeds.feed_url,ttrss_feeds.id
  371. FROM
  372. ttrss_feeds, ttrss_users, ttrss_user_prefs
  373. WHERE
  374. ttrss_feeds.owner_uid = ttrss_users.id
  375. AND ttrss_users.id = ttrss_user_prefs.owner_uid
  376. AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
  377. $owner_check_qpart
  378. $update_limit_qpart
  379. $updstart_thresh_qpart
  380. ORDER BY $random_qpart LIMIT 30");
  381. $num_updated = 0;
  382. $tstart = time();
  383. while ($line = $res->fetch()) {
  384. $feed_id = $line["id"];
  385. if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
  386. RSSUtils::update_rss_feed($feed_id, true);
  387. ++$num_updated;
  388. } else {
  389. break;
  390. }
  391. }
  392. // Purge orphans and cleanup tags
  393. Article::purge_orphans();
  394. //cleanup_tags(14, 50000);
  395. if ($num_updated > 0) {
  396. print json_encode(array("message" => "UPDATE_COUNTERS",
  397. "num_updated" => $num_updated));
  398. } else {
  399. print json_encode(array("message" => "NOTHING_TO_UPDATE"));
  400. }
  401. }
  402. function updaterandomfeed() {
  403. RPC::updaterandomfeed_real();
  404. }
  405. private function markArticlesById($ids, $cmode) {
  406. $ids_qmarks = arr_qmarks($ids);
  407. if ($cmode == 0) {
  408. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  409. marked = false, last_marked = NOW()
  410. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  411. } else if ($cmode == 1) {
  412. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  413. marked = true, last_marked = NOW()
  414. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  415. } else {
  416. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  417. marked = NOT marked,last_marked = NOW()
  418. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  419. }
  420. $sth->execute(array_merge($ids, [$_SESSION['uid']]));
  421. }
  422. private function publishArticlesById($ids, $cmode) {
  423. $ids_qmarks = arr_qmarks($ids);
  424. if ($cmode == 0) {
  425. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  426. published = false, last_published = NOW()
  427. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  428. } else if ($cmode == 1) {
  429. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  430. published = true, last_published = NOW()
  431. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  432. } else {
  433. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  434. published = NOT published,last_published = NOW()
  435. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  436. }
  437. $sth->execute(array_merge($ids, [$_SESSION['uid']]));
  438. }
  439. function getlinktitlebyid() {
  440. $id = $_REQUEST['id'];
  441. $sth = $this->pdo->prepare("SELECT link, title FROM ttrss_entries, ttrss_user_entries
  442. WHERE ref_id = ? AND ref_id = id AND owner_uid = ?");
  443. $sth->execute([$id, $_SESSION['uid']]);
  444. if ($row = $sth->fetch()) {
  445. $link = $row['link'];
  446. $title = $row['title'];
  447. echo json_encode(array("link" => $link, "title" => $title));
  448. } else {
  449. echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));
  450. }
  451. }
  452. function log() {
  453. $msg = $_REQUEST['msg'];
  454. $file = basename($_REQUEST['file']);
  455. $line = (int) $_REQUEST['line'];
  456. $context = $_REQUEST['context'];
  457. if ($msg) {
  458. Logger::get()->log_error(E_USER_WARNING,
  459. $msg, 'client-js:' . $file, $line, $context);
  460. echo json_encode(array("message" => "HOST_ERROR_LOGGED"));
  461. } else {
  462. echo json_encode(array("error" => "MESSAGE_NOT_FOUND"));
  463. }
  464. }
  465. }