backend-rpc.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. <?php
  2. function handle_rpc_request($link) {
  3. $subop = $_REQUEST["subop"];
  4. $seq = (int) $_REQUEST["seq"];
  5. // Silent
  6. if ($subop == "setprofile") {
  7. $id = db_escape_string($_REQUEST["id"]);
  8. $_SESSION["profile"] = $id;
  9. $_SESSION["prefs_cache"] = array();
  10. return;
  11. }
  12. // Silent
  13. if ($subop == "remprofiles") {
  14. $ids = split(",", db_escape_string(trim($_REQUEST["ids"])));
  15. foreach ($ids as $id) {
  16. if ($_SESSION["profile"] != $id) {
  17. db_query($link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
  18. owner_uid = " . $_SESSION["uid"]);
  19. }
  20. }
  21. return;
  22. }
  23. // Silent
  24. if ($subop == "addprofile") {
  25. $title = db_escape_string(trim($_REQUEST["title"]));
  26. if ($title) {
  27. db_query($link, "BEGIN");
  28. $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
  29. WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
  30. if (db_num_rows($result) == 0) {
  31. db_query($link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
  32. VALUES ('$title', ".$_SESSION["uid"] .")");
  33. $result = db_query($link, "SELECT id FROM ttrss_settings_profiles WHERE
  34. title = '$title'");
  35. if (db_num_rows($result) != 0) {
  36. $profile_id = db_fetch_result($result, 0, "id");
  37. if ($profile_id) {
  38. initialize_user_prefs($link, $_SESSION["uid"], $profile_id);
  39. }
  40. }
  41. }
  42. db_query($link, "COMMIT");
  43. }
  44. return;
  45. }
  46. // Silent
  47. if ($subop == "saveprofile") {
  48. $id = db_escape_string($_REQUEST["id"]);
  49. $title = db_escape_string(trim($_REQUEST["value"]));
  50. if ($id == 0) {
  51. print __("Default profile");
  52. return;
  53. }
  54. if ($title) {
  55. db_query($link, "BEGIN");
  56. $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
  57. WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
  58. if (db_num_rows($result) == 0) {
  59. db_query($link, "UPDATE ttrss_settings_profiles
  60. SET title = '$title' WHERE id = '$id' AND
  61. owner_uid = " . $_SESSION["uid"]);
  62. print $title;
  63. } else {
  64. $result = db_query($link, "SELECT title FROM ttrss_settings_profiles
  65. WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
  66. print db_fetch_result($result, 0, "title");
  67. }
  68. db_query($link, "COMMIT");
  69. }
  70. return;
  71. }
  72. // Silent
  73. if ($subop == "remarchive") {
  74. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  75. foreach ($ids as $id) {
  76. $result = db_query($link, "DELETE FROM ttrss_archived_feeds WHERE
  77. (SELECT COUNT(*) FROM ttrss_user_entries
  78. WHERE orig_feed_id = '$id') = 0 AND
  79. id = '$id' AND owner_uid = ".$_SESSION["uid"]);
  80. $rc = db_affected_rows($link, $result);
  81. }
  82. return;
  83. }
  84. if ($subop == "addfeed") {
  85. header("Content-Type: text/plain");
  86. $feed = db_escape_string($_REQUEST['feed']);
  87. $cat = db_escape_string($_REQUEST['cat']);
  88. $login = db_escape_string($_REQUEST['login']);
  89. $pass = db_escape_string($_REQUEST['pass']);
  90. $rc = subscribe_to_feed($link, $feed, $cat, $login, $pass);
  91. print json_encode(array("result" => $rc));
  92. return;
  93. }
  94. if ($subop == "extractfeedurls") {
  95. header("Content-Type: text/plain");
  96. $urls = get_feeds_from_html($_REQUEST['url']);
  97. print json_encode(array("urls" => $urls));
  98. return;
  99. }
  100. if ($subop == "togglepref") {
  101. header("Content-Type: text/plain");
  102. $key = db_escape_string($_REQUEST["key"]);
  103. set_pref($link, $key, !get_pref($link, $key));
  104. $value = get_pref($link, $key);
  105. print json_encode(array("param" =>$key, "value" => $value));
  106. return;
  107. }
  108. if ($subop == "setpref") {
  109. header("Content-Type: text/plain");
  110. $value = str_replace("\n", "<br/>", $_REQUEST['value']);
  111. $key = db_escape_string($_REQUEST["key"]);
  112. $value = db_escape_string($value);
  113. set_pref($link, $key, $value);
  114. print json_encode(array("param" =>$key, "value" => $value));
  115. return;
  116. }
  117. if ($subop == "mark") {
  118. header("Content-Type: text/plain");
  119. $mark = $_REQUEST["mark"];
  120. $id = db_escape_string($_REQUEST["id"]);
  121. if ($mark == "1") {
  122. $mark = "true";
  123. } else {
  124. $mark = "false";
  125. }
  126. $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
  127. WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
  128. print json_encode(array("message" => "UPDATE_COUNTERS"));
  129. return;
  130. }
  131. if ($subop == "delete") {
  132. header("Content-Type: text/plain");
  133. $ids = db_escape_string($_REQUEST["ids"]);
  134. $result = db_query($link, "DELETE FROM ttrss_user_entries
  135. WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
  136. print json_encode(array("message" => "UPDATE_COUNTERS"));
  137. return;
  138. }
  139. if ($subop == "unarchive") {
  140. header("Content-Type: text/plain");
  141. $ids = db_escape_string($_REQUEST["ids"]);
  142. $result = db_query($link, "UPDATE ttrss_user_entries
  143. SET feed_id = orig_feed_id, orig_feed_id = NULL
  144. WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
  145. print json_encode(array("message" => "UPDATE_COUNTERS"));
  146. return;
  147. }
  148. if ($subop == "archive") {
  149. header("Content-Type: text/plain");
  150. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  151. foreach ($ids as $id) {
  152. archive_article($link, $id, $_SESSION["uid"]);
  153. }
  154. print json_encode(array("message" => "UPDATE_COUNTERS"));
  155. return;
  156. }
  157. if ($subop == "publ") {
  158. header("Content-Type: text/plain");
  159. $pub = $_REQUEST["pub"];
  160. $id = db_escape_string($_REQUEST["id"]);
  161. $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
  162. if ($pub == "1") {
  163. $pub = "true";
  164. } else {
  165. $pub = "false";
  166. }
  167. $result = db_query($link, "UPDATE ttrss_user_entries SET
  168. published = $pub
  169. WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
  170. print json_encode(array("message" => "UPDATE_COUNTERS"));
  171. return;
  172. }
  173. /* if ($subop == "updateFeed") {
  174. $feed_id = db_escape_string($_REQUEST["feed"]);
  175. update_rss_feed($link, $feed_id);
  176. print "<rpc-reply>";
  177. print "<message>UPDATE_COUNTERS</message>";
  178. print "</rpc-reply>";
  179. return;
  180. } */
  181. if ($subop == "updateAllFeeds" || $subop == "getAllCounters") {
  182. header("Content-Type: text/plain");
  183. $last_article_id = (int) $_REQUEST["last_article_id"];
  184. $reply = array();
  185. if ($seq) $reply['seq'] = $seq;
  186. if ($last_article_id != getLastArticleId($link)) {
  187. $omode = $_REQUEST["omode"];
  188. if ($omode != "T")
  189. $reply['counters'] = getAllCounters($link, $omode);
  190. else
  191. $reply['counters'] = getGlobalCounters($link);
  192. }
  193. $reply['runtime-info'] = make_runtime_info($link);
  194. print json_encode($reply);
  195. return;
  196. }
  197. /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
  198. if ($subop == "catchupSelected") {
  199. header("Content-Type: text/plain");
  200. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  201. $cmode = sprintf("%d", $_REQUEST["cmode"]);
  202. catchupArticlesById($link, $ids, $cmode);
  203. print json_encode(array("message" => "UPDATE_COUNTERS"));
  204. return;
  205. }
  206. if ($subop == "markSelected") {
  207. header("Content-Type: text/plain");
  208. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  209. $cmode = sprintf("%d", $_REQUEST["cmode"]);
  210. markArticlesById($link, $ids, $cmode);
  211. print json_encode(array("message" => "UPDATE_COUNTERS"));
  212. return;
  213. }
  214. if ($subop == "publishSelected") {
  215. header("Content-Type: text/plain");
  216. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  217. $cmode = sprintf("%d", $_REQUEST["cmode"]);
  218. publishArticlesById($link, $ids, $cmode);
  219. print json_encode(array("message" => "UPDATE_COUNTERS"));
  220. return;
  221. }
  222. if ($subop == "sanityCheck") {
  223. header("Content-Type: text/plain");
  224. $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
  225. $reply = array();
  226. $reply['error'] = sanity_check($link);
  227. if ($reply['error']['code'] == 0) {
  228. $reply['init-params'] = make_init_params($link);
  229. $reply['runtime-info'] = make_runtime_info($link);
  230. }
  231. print json_encode($reply);
  232. return;
  233. }
  234. /* if ($subop == "globalPurge") {
  235. print "<rpc-reply>";
  236. global_purge_old_posts($link, true);
  237. print "</rpc-reply>";
  238. return;
  239. } */
  240. if ($subop == "setArticleTags") {
  241. header("Content-Type: text/plain");
  242. global $memcache;
  243. $id = db_escape_string($_REQUEST["id"]);
  244. $tags_str = db_escape_string($_REQUEST["tags_str"]);
  245. $tags = array_unique(trim_array(split(",", $tags_str)));
  246. db_query($link, "BEGIN");
  247. $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
  248. ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
  249. if (db_num_rows($result) == 1) {
  250. $tags_to_cache = array();
  251. $int_id = db_fetch_result($result, 0, "int_id");
  252. db_query($link, "DELETE FROM ttrss_tags WHERE
  253. post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
  254. foreach ($tags as $tag) {
  255. $tag = sanitize_tag($tag);
  256. if (!tag_is_valid($tag)) {
  257. continue;
  258. }
  259. if (preg_match("/^[0-9]*$/", $tag)) {
  260. continue;
  261. }
  262. // print "<!-- $id : $int_id : $tag -->";
  263. if ($tag != '') {
  264. db_query($link, "INSERT INTO ttrss_tags
  265. (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
  266. }
  267. array_push($tags_to_cache, $tag);
  268. }
  269. /* update tag cache */
  270. $tags_str = join(",", $tags_to_cache);
  271. db_query($link, "UPDATE ttrss_user_entries
  272. SET tag_cache = '$tags_str' WHERE ref_id = '$id'
  273. AND owner_uid = " . $_SESSION["uid"]);
  274. }
  275. db_query($link, "COMMIT");
  276. if ($memcache) {
  277. $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
  278. $memcache->delete($obj_id);
  279. }
  280. $tags_str = format_tags_string(get_article_tags($link, $id), $id);
  281. print json_encode(array("tags_str" => array("id" => $id,
  282. "content" => $tags_str)));
  283. return;
  284. }
  285. if ($subop == "regenOPMLKey") {
  286. header("Content-Type: text/plain");
  287. update_feed_access_key($link, 'OPML:Publish',
  288. false, $_SESSION["uid"]);
  289. $new_link = opml_publish_url($link);
  290. print json_encode(array("link" => $new_link));
  291. return;
  292. }
  293. // XML method
  294. if ($subop == "logout") {
  295. logout_user();
  296. print_error_xml(6);
  297. return;
  298. }
  299. if ($subop == "completeTags") {
  300. header("Content-Type: text/plain");
  301. $search = db_escape_string($_REQUEST["search"]);
  302. $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
  303. WHERE owner_uid = '".$_SESSION["uid"]."' AND
  304. tag_name LIKE '$search%' ORDER BY tag_name
  305. LIMIT 10");
  306. print "<ul>";
  307. while ($line = db_fetch_assoc($result)) {
  308. print "<li>" . $line["tag_name"] . "</li>";
  309. }
  310. print "</ul>";
  311. return;
  312. }
  313. if ($subop == "purge") {
  314. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  315. $days = sprintf("%d", $_REQUEST["days"]);
  316. foreach ($ids as $id) {
  317. $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
  318. id = '$id' AND owner_uid = ".$_SESSION["uid"]);
  319. if (db_num_rows($result) == 1) {
  320. purge_feed($link, $id, $days);
  321. }
  322. }
  323. return;
  324. }
  325. /* if ($subop == "setScore") {
  326. $id = db_escape_string($_REQUEST["id"]);
  327. $score = sprintf("%d", $_REQUEST["score"]);
  328. $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
  329. WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
  330. print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
  331. return;
  332. } */
  333. if ($subop == "getArticles") {
  334. header("Content-Type: text/plain");
  335. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  336. $articles = array();
  337. foreach ($ids as $id) {
  338. if ($id) {
  339. array_push($articles, format_article($link, $id, 0, false));
  340. }
  341. }
  342. print json_encode($articles);
  343. return;
  344. }
  345. if ($subop == "checkDate") {
  346. header("Content-Type: text/plain");
  347. $date = db_escape_string($_REQUEST["date"]);
  348. $date_parsed = strtotime($date);
  349. print json_encode(array("result" => (bool)$date_parsed));
  350. return;
  351. }
  352. if ($subop == "assignToLabel" || $subop == "removeFromLabel") {
  353. header("Content-Type: text/plain");
  354. $reply = array();
  355. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  356. $label_id = db_escape_string($_REQUEST["lid"]);
  357. $label = db_escape_string(label_find_caption($link, $label_id,
  358. $_SESSION["uid"]));
  359. $reply["info-for-headlines"] = array();
  360. if ($label) {
  361. foreach ($ids as $id) {
  362. if ($subop == "assignToLabel")
  363. label_add_article($link, $id, $label, $_SESSION["uid"]);
  364. else
  365. label_remove_article($link, $id, $label, $_SESSION["uid"]);
  366. $labels = get_article_labels($link, $id, $_SESSION["uid"]);
  367. array_push($reply["info-for-headlines"],
  368. array("id" => $id, "labels" => format_article_labels($labels, $id)));
  369. }
  370. }
  371. $reply["message"] = "UPDATE_COUNTERS";
  372. print json_encode($reply);
  373. return;
  374. }
  375. if ($subop == "updateFeedBrowser") {
  376. header("Content-Type: text/plain");
  377. $search = db_escape_string($_REQUEST["search"]);
  378. $limit = db_escape_string($_REQUEST["limit"]);
  379. $mode = (int) db_escape_string($_REQUEST["mode"]);
  380. print json_encode(array("content" =>
  381. make_feed_browser($link, $search, $limit, $mode),
  382. "mode" => $mode));
  383. return;
  384. }
  385. // Silent
  386. if ($subop == "massSubscribe") {
  387. $ids = split(",", db_escape_string($_REQUEST["ids"]));
  388. $mode = $_REQUEST["mode"];
  389. $subscribed = array();
  390. foreach ($ids as $id) {
  391. if ($mode == 1) {
  392. $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
  393. WHERE id = '$id'");
  394. } else if ($mode == 2) {
  395. $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
  396. WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
  397. $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
  398. $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
  399. }
  400. $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
  401. $title = db_escape_string(db_fetch_result($result, 0, "title"));
  402. $title_orig = db_fetch_result($result, 0, "title");
  403. $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
  404. feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
  405. if (db_num_rows($result) == 0) {
  406. if ($mode == 1) {
  407. $result = db_query($link,
  408. "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
  409. VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
  410. } else if ($mode == 2) {
  411. $result = db_query($link,
  412. "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
  413. VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
  414. }
  415. array_push($subscribed, $title_orig);
  416. }
  417. }
  418. return;
  419. }
  420. if ($subop == "digest-get-contents") {
  421. header("Content-Type: text/plain");
  422. $article_id = db_escape_string($_REQUEST['article_id']);
  423. $result = db_query($link, "SELECT content
  424. FROM ttrss_entries, ttrss_user_entries
  425. WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
  426. $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
  427. print json_encode(array("article" =>
  428. array("id" => $id, "content" => $content)));
  429. return;
  430. }
  431. if ($subop == "digest-update") {
  432. header("Content-Type: text/plain");
  433. $feed_id = db_escape_string($_REQUEST['feed_id']);
  434. $offset = db_escape_string($_REQUEST['offset']);
  435. $seq = db_escape_string($_REQUEST['seq']);
  436. if (!$feed_id) $feed_id = -4;
  437. if (!$offset) $offset = 0;
  438. $reply = array();
  439. $reply['seq'] = $seq;
  440. $headlines = api_get_headlines($link, $feed_id, 10, $offset,
  441. '', ($feed_id == -4), true, false, "unread", "updated DESC");
  442. //function api_get_headlines($link, $feed_id, $limit, $offset,
  443. // $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
  444. $reply['headlines'] = array();
  445. $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
  446. $reply['headlines']['content'] = $headlines;
  447. print json_encode($reply);
  448. return;
  449. }
  450. if ($subop == "digest-init") {
  451. header("Content-Type: text/plain");
  452. $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
  453. $feeds = array();
  454. foreach ($tmp_feeds as $f) {
  455. if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
  456. }
  457. print json_encode(array("feeds" => $feeds));
  458. return;
  459. }
  460. if ($subop == "catchupFeed") {
  461. $feed_id = db_escape_string($_REQUEST['feed_id']);
  462. $is_cat = db_escape_string($_REQUEST['is_cat']);
  463. catchup_feed($link, $feed_id, $is_cat);
  464. return;
  465. }
  466. if ($subop == "sendEmail") {
  467. header("Content-Type: text/plain");
  468. $secretkey = $_REQUEST['secretkey'];
  469. $reply = array();
  470. if (DIGEST_ENABLE && $_SESSION['email_secretkey'] &&
  471. $secretkey == $_SESSION['email_secretkey']) {
  472. $_SESSION['email_secretkey'] = '';
  473. $destination = $_REQUEST['destination'];
  474. $subject = $_REQUEST['subject'];
  475. $content = $_REQUEST['content'];
  476. $replyto = strip_tags($_SESSION['email_replyto']);
  477. $fromname = strip_tags($_SESSION['email_fromname']);
  478. $mail = new PHPMailer();
  479. $mail->PluginDir = "lib/phpmailer/";
  480. $mail->SetLanguage("en", "lib/phpmailer/language/");
  481. $mail->CharSet = "UTF-8";
  482. $mail->From = $replyto;
  483. $mail->FromName = $fromname;
  484. $mail->AddAddress($destination);
  485. if (DIGEST_SMTP_HOST) {
  486. $mail->Host = DIGEST_SMTP_HOST;
  487. $mail->Mailer = "smtp";
  488. $mail->SMTPAuth = DIGEST_SMTP_LOGIN != '';
  489. $mail->Username = DIGEST_SMTP_LOGIN;
  490. $mail->Password = DIGEST_SMTP_PASSWORD;
  491. }
  492. $mail->IsHTML(false);
  493. $mail->Subject = $subject;
  494. $mail->Body = $content;
  495. $rc = $mail->Send();
  496. if (!$rc) {
  497. $reply['error'] = $mail->ErrorInfo;
  498. } else {
  499. save_email_address($link, db_escape_string($destination));
  500. $reply['message'] = "UPDATE_COUNTERS";
  501. }
  502. } else {
  503. $reply['error'] = "Not authorized.";
  504. }
  505. print json_encode($reply);
  506. return;
  507. }
  508. if ($subop == "completeEmails") {
  509. header("Content-Type: text/plain");
  510. $search = db_escape_string($_REQUEST["search"]);
  511. print "<ul>";
  512. foreach ($_SESSION['stored_emails'] as $email) {
  513. if (strpos($email, $search) !== false) {
  514. print "<li>$email</li>";
  515. }
  516. }
  517. print "</ul>";
  518. return;
  519. }
  520. if ($subop == "quickAddCat") {
  521. header("Content-Type: text/plain");
  522. $cat = db_escape_string($_REQUEST["cat"]);
  523. add_feed_category($link, $cat);
  524. $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
  525. title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
  526. if (db_num_rows($result) == 1) {
  527. $id = db_fetch_result($result, 0, "id");
  528. } else {
  529. $id = 0;
  530. }
  531. print_feed_cat_select($link, "cat_id", $id);
  532. return;
  533. }
  534. if ($subop == "regenFeedKey") {
  535. header("Content-Type: text/plain");
  536. $feed_id = db_escape_string($_REQUEST['id']);
  537. $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
  538. $new_key = update_feed_access_key($link, $feed_id, $is_cat);
  539. print json_encode(array("link" => $new_key));
  540. return;
  541. }
  542. // Silent
  543. if ($subop == "clearKeys") {
  544. db_query($link, "DELETE FROM ttrss_access_keys WHERE
  545. owner_uid = " . $_SESSION["uid"]);
  546. return;
  547. }
  548. if ($subop == "verifyRegexp") {
  549. header("Content-Type: text/plain");
  550. $reg_exp = $_REQUEST["reg_exp"];
  551. $status = @preg_match("/$reg_exp/i", "TEST") !== false;
  552. print json_encode(array("status" => $status));
  553. return;
  554. }
  555. // TODO: unify with digest-get-contents?
  556. if ($subop == "cdmGetArticle") {
  557. header("Content-Type: text/plain");
  558. $id = db_escape_string($_REQUEST["id"]);
  559. $result = db_query($link, "SELECT content,
  560. ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
  561. ttrss_entries
  562. WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
  563. ttrss_entries.id = ref_id AND
  564. ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
  565. if (db_num_rows($result) != 0) {
  566. $line = db_fetch_assoc($result);
  567. $article_content = sanitize_rss($link, $line["content"],
  568. false, false, $line['site_url']);
  569. } else {
  570. $article_content = '';
  571. }
  572. print json_encode(array("article" =>
  573. array("id" => $id, "content" => $article_content)));
  574. return;
  575. }
  576. if ($subop == "scheduleFeedUpdate") {
  577. header("Content-Type: text/plain");
  578. $feed_id = db_escape_string($_REQUEST["id"]);
  579. $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
  580. $message = __("Your request could not be completed.");
  581. if ($feed_id >= 0) {
  582. if (!$is_cat) {
  583. $message = __("Feed update has been scheduled.");
  584. db_query($link, "UPDATE ttrss_feeds SET
  585. last_update_started = '1970-01-01',
  586. last_updated = '1970-01-01' WHERE id = '$feed_id' AND
  587. owner_uid = ".$_SESSION["uid"]);
  588. } else {
  589. $message = __("Category update has been scheduled.");
  590. if ($feed_id)
  591. $cat_query = "cat_id = '$feed_id'";
  592. else
  593. $cat_query = "cat_id IS NULL";
  594. db_query($link, "UPDATE ttrss_feeds SET
  595. last_update_started = '1970-01-01',
  596. last_updated = '1970-01-01' WHERE $cat_query AND
  597. owner_uid = ".$_SESSION["uid"]);
  598. }
  599. } else {
  600. $message = __("Can't update this kind of feed.");
  601. }
  602. print json_encode(array("message" => $message));
  603. return;
  604. }
  605. if ($subop == "getTweetInfo") {
  606. header("Content-Type: text/plain");
  607. $id = db_escape_string($_REQUEST['id']);
  608. $result = db_query($link, "SELECT title, link
  609. FROM ttrss_entries, ttrss_user_entries
  610. WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
  611. if (db_num_rows($result) != 0) {
  612. $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
  613. 100, '...');
  614. $article_link = db_fetch_result($result, 0, 'link');
  615. }
  616. print json_encode(array("title" => $title, "link" => $article_link,
  617. "id" => $id));
  618. return;
  619. }
  620. if ($subop == "setNote") {
  621. header("Content-Type: text/plain");
  622. $id = db_escape_string($_REQUEST["id"]);
  623. $note = strip_tags(db_escape_string($_REQUEST["note"]));
  624. db_query($link, "UPDATE ttrss_user_entries SET note = '$note'
  625. WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
  626. $formatted_note = format_article_note($id, $note);
  627. print json_encode(array("note" => $formatted_note));
  628. return;
  629. }
  630. print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
  631. }
  632. ?>