]>
git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
2 function handle_rpc_request($link) {
4 $subop = $_REQUEST["subop"];
5 $seq = (int) $_REQUEST["seq"];
8 if ($subop == "setprofile") {
9 $id = db_escape_string($_REQUEST["id"]);
11 $_SESSION["profile"] = $id;
12 $_SESSION["prefs_cache"] = array();
17 if ($subop == "remprofiles") {
18 $ids = split(",", db_escape_string(trim($_REQUEST["ids"])));
20 foreach ($ids as $id) {
21 if ($_SESSION["profile"] != $id) {
22 db_query($link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
23 owner_uid = " . $_SESSION["uid"]);
30 if ($subop == "addprofile") {
31 $title = db_escape_string(trim($_REQUEST["title"]));
33 db_query($link, "BEGIN");
35 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
36 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
38 if (db_num_rows($result) == 0) {
40 db_query($link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
41 VALUES ('$title', ".$_SESSION["uid"] .")");
43 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles WHERE
46 if (db_num_rows($result) != 0) {
47 $profile_id = db_fetch_result($result, 0, "id");
50 initialize_user_prefs($link, $_SESSION["uid"], $profile_id);
55 db_query($link, "COMMIT");
61 if ($subop == "saveprofile") {
62 $id = db_escape_string($_REQUEST["id"]);
63 $title = db_escape_string(trim($_REQUEST["value"]));
66 print __("Default profile");
71 db_query($link, "BEGIN");
73 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
74 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
76 if (db_num_rows($result) == 0) {
77 db_query($link, "UPDATE ttrss_settings_profiles
78 SET title = '$title' WHERE id = '$id' AND
79 owner_uid = " . $_SESSION["uid"]);
82 $result = db_query($link, "SELECT title FROM ttrss_settings_profiles
83 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
84 print db_fetch_result($result, 0, "title");
87 db_query($link, "COMMIT");
93 if ($subop == "remarchive") {
94 $ids = split(",", db_escape_string($_REQUEST["ids"]));
96 foreach ($ids as $id) {
97 $result = db_query($link, "DELETE FROM ttrss_archived_feeds WHERE
98 (SELECT COUNT(*) FROM ttrss_user_entries
99 WHERE orig_feed_id = '$id') = 0 AND
100 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
102 $rc = db_affected_rows($link, $result);
107 if ($subop == "addfeed") {
108 header("Content-Type: text/plain");
110 $feed = db_escape_string($_REQUEST['feed']);
111 $cat = db_escape_string($_REQUEST['cat']);
112 $login = db_escape_string($_REQUEST['login']);
113 $pass = db_escape_string($_REQUEST['pass']);
115 $rc = subscribe_to_feed($link, $feed, $cat, $login, $pass);
117 print json_encode(array("result" => $rc));
123 if ($subop == "extractfeedurls") {
124 header("Content-Type: text/plain");
126 $urls = get_feeds_from_html($_REQUEST['url']);
128 print json_encode(array("urls" => $urls));
132 if ($subop == "togglepref") {
133 header("Content-Type: text/plain");
135 $key = db_escape_string($_REQUEST["key"]);
136 set_pref($link, $key, !get_pref($link, $key));
137 $value = get_pref($link, $key);
139 print json_encode(array("param" =>$key, "value" => $value));
143 if ($subop == "setpref") {
144 header("Content-Type: text/plain");
146 $key = db_escape_string($_REQUEST["key"]);
147 $value = db_escape_string($_REQUEST["value"]);
149 set_pref($link, $key, $value);
151 print json_encode(array("param" =>$key, "value" => $value));
155 if ($subop == "mark") {
156 header("Content-Type: text/plain");
158 $mark = $_REQUEST["mark"];
159 $id = db_escape_string($_REQUEST["id"]);
167 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
168 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
170 print json_encode(array("message" => "UPDATE_COUNTERS"));
174 if ($subop == "delete") {
175 header("Content-Type: text/plain");
177 $ids = db_escape_string($_REQUEST["ids"]);
179 $result = db_query($link, "DELETE FROM ttrss_user_entries
180 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
182 print json_encode(array("message" => "UPDATE_COUNTERS"));
186 if ($subop == "unarchive") {
187 header("Content-Type: text/plain");
189 $ids = db_escape_string($_REQUEST["ids"]);
191 $result = db_query($link, "UPDATE ttrss_user_entries
192 SET feed_id = orig_feed_id, orig_feed_id = NULL
193 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
195 print json_encode(array("message" => "UPDATE_COUNTERS"));
199 if ($subop == "archive") {
200 header("Content-Type: text/plain");
202 $ids = split(",", db_escape_string($_REQUEST["ids"]));
204 foreach ($ids as $id) {
205 archive_article($link, $id, $_SESSION["uid"]);
208 print json_encode(array("message" => "UPDATE_COUNTERS"));
212 if ($subop == "publ") {
213 header("Content-Type: text/plain");
215 $pub = $_REQUEST["pub"];
216 $id = db_escape_string($_REQUEST["id"]);
217 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
225 $result = db_query($link, "UPDATE ttrss_user_entries SET
227 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
229 print json_encode(array("message" => "UPDATE_COUNTERS"));
233 /* if ($subop == "updateFeed") {
234 $feed_id = db_escape_string($_REQUEST["feed"]);
236 update_rss_feed($link, $feed_id);
239 print "<message>UPDATE_COUNTERS</message>";
240 print "</rpc-reply>";
245 if ($subop == "updateAllFeeds" ||
$subop == "getAllCounters") {
247 header("Content-Type: text/plain");
249 $last_article_id = (int) $_REQUEST["last_article_id"];
253 if ($seq) $reply['seq'] = $seq;
255 if ($last_article_id != getLastArticleId($link)) {
256 $omode = $_REQUEST["omode"];
259 $reply['counters'] = getAllCounters($link, $omode);
261 $reply['counters'] = getGlobalCounters($link);
264 $reply['runtime-info'] = make_runtime_info($link);
267 print json_encode($reply);
271 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
272 if ($subop == "catchupSelected") {
273 header("Content-Type: text/plain");
275 $ids = split(",", db_escape_string($_REQUEST["ids"]));
276 $cmode = sprintf("%d", $_REQUEST["cmode"]);
278 catchupArticlesById($link, $ids, $cmode);
280 print json_encode(array("message" => "UPDATE_COUNTERS"));
284 if ($subop == "markSelected") {
285 header("Content-Type: text/plain");
287 $ids = split(",", db_escape_string($_REQUEST["ids"]));
288 $cmode = sprintf("%d", $_REQUEST["cmode"]);
290 markArticlesById($link, $ids, $cmode);
292 print json_encode(array("message" => "UPDATE_COUNTERS"));
296 if ($subop == "publishSelected") {
297 header("Content-Type: text/plain");
299 $ids = split(",", db_escape_string($_REQUEST["ids"]));
300 $cmode = sprintf("%d", $_REQUEST["cmode"]);
302 publishArticlesById($link, $ids, $cmode);
304 print json_encode(array("message" => "UPDATE_COUNTERS"));
309 if ($subop == "sanityCheck") {
311 if (sanity_check($link)) {
312 print "<error error-code=\"0\"/>";
314 print "<init-params><![CDATA[";
315 print json_encode(make_init_params($link));
316 print "]]></init-params>";
318 print_runtime_info($link);
320 print "</rpc-reply>";
325 /* if ($subop == "globalPurge") {
328 global_purge_old_posts($link, true);
329 print "</rpc-reply>";
334 if ($subop == "setArticleTags") {
335 header("Content-Type: text/plain");
339 $id = db_escape_string($_REQUEST["id"]);
341 $tags_str = db_escape_string($_REQUEST["tags_str"]);
342 $tags = array_unique(trim_array(split(",", $tags_str)));
344 db_query($link, "BEGIN");
346 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
347 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
349 if (db_num_rows($result) == 1) {
351 $tags_to_cache = array();
353 $int_id = db_fetch_result($result, 0, "int_id");
355 db_query($link, "DELETE FROM ttrss_tags WHERE
356 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
358 foreach ($tags as $tag) {
359 $tag = sanitize_tag($tag);
361 if (!tag_is_valid($tag)) {
365 if (preg_match("/^[0-9]*$/", $tag)) {
369 // print "<!-- $id : $int_id : $tag -->";
372 db_query($link, "INSERT INTO ttrss_tags
373 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
376 array_push($tags_to_cache, $tag);
379 /* update tag cache */
381 $tags_str = join(",", $tags_to_cache);
383 db_query($link, "UPDATE ttrss_user_entries
384 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
385 AND owner_uid = " . $_SESSION["uid"]);
388 db_query($link, "COMMIT");
391 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
392 $memcache->delete($obj_id);
395 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
397 print json_encode(array("tags_str" => array("id" => $id,
398 "content" => $tags_str)));
403 if ($subop == "regenOPMLKey") {
404 header("Content-Type: text/plain");
406 update_feed_access_key($link, 'OPML:Publish',
407 false, $_SESSION["uid"]);
409 $new_link = opml_publish_url($link);
411 print json_encode(array("link" => $new_link));
416 if ($subop == "logout") {
422 if ($subop == "completeTags") {
423 header("Content-Type: text/plain");
425 $search = db_escape_string($_REQUEST["search"]);
427 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
428 WHERE owner_uid = '".$_SESSION["uid"]."' AND
429 tag_name LIKE '$search%' ORDER BY tag_name
433 while ($line = db_fetch_assoc($result)) {
434 print "<li>" . $line["tag_name"] . "</li>";
441 if ($subop == "purge") {
442 $ids = split(",", db_escape_string($_REQUEST["ids"]));
443 $days = sprintf("%d", $_REQUEST["days"]);
445 foreach ($ids as $id) {
447 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
448 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
450 if (db_num_rows($result) == 1) {
451 purge_feed($link, $id, $days);
458 /* if ($subop == "setScore") {
459 $id = db_escape_string($_REQUEST["id"]);
460 $score = sprintf("%d", $_REQUEST["score"]);
462 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
463 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
465 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
472 if ($subop == "getArticles") {
473 $ids = split(",", db_escape_string($_REQUEST["ids"]));
477 foreach ($ids as $id) {
479 outputArticleXML($link, $id, 0, false);
482 print "</rpc-reply>";
487 if ($subop == "checkDate") {
488 header("Content-Type: text/plain");
490 $date = db_escape_string($_REQUEST["date"]);
491 $date_parsed = strtotime($date);
493 print json_encode(array("result" => (bool)$date_parsed));
497 if ($subop == "assignToLabel" ||
$subop == "removeFromLabel") {
498 header("Content-Type: text/plain");
502 $ids = split(",", db_escape_string($_REQUEST["ids"]));
503 $label_id = db_escape_string($_REQUEST["lid"]);
505 $label = db_escape_string(label_find_caption($link, $label_id,
508 $reply["info-for-headlines"] = array();
512 foreach ($ids as $id) {
514 if ($subop == "assignToLabel")
515 label_add_article($link, $id, $label, $_SESSION["uid"]);
517 label_remove_article($link, $id, $label, $_SESSION["uid"]);
519 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
521 array_push($reply["info-for-headlines"],
522 array("id" => $id, "labels" => format_article_labels($labels, $id)));
527 $reply["message"] = "UPDATE_COUNTERS";
529 print json_encode($reply);
534 if ($subop == "updateFeedBrowser") {
535 header("Content-Type: text/plain");
537 $search = db_escape_string($_REQUEST["search"]);
538 $limit = db_escape_string($_REQUEST["limit"]);
539 $mode = (int) db_escape_string($_REQUEST["mode"]);
541 print json_encode(array("content" =>
542 make_feed_browser($link, $search, $limit, $mode),
548 if ($subop == "massSubscribe") {
550 $ids = split(",", db_escape_string($_REQUEST["ids"]));
551 $mode = $_REQUEST["mode"];
553 $subscribed = array();
555 foreach ($ids as $id) {
558 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
560 } else if ($mode == 2) {
561 $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
562 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
563 $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
564 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
567 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
568 $title = db_escape_string(db_fetch_result($result, 0, "title"));
570 $title_orig = db_fetch_result($result, 0, "title");
572 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
573 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
575 if (db_num_rows($result) == 0) {
577 $result = db_query($link,
578 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
579 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
580 } else if ($mode == 2) {
581 $result = db_query($link,
582 "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
583 VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
585 array_push($subscribed, $title_orig);
592 if ($subop == "digest-get-contents") {
593 header("Content-Type: text/plain");
595 $article_id = db_escape_string($_REQUEST['article_id']);
597 $result = db_query($link, "SELECT content
598 FROM ttrss_entries, ttrss_user_entries
599 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
601 $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
603 print json_encode(array("article" =>
604 array("id" => $id, "content" => $content)));
608 if ($subop == "digest-update") {
609 header("Content-Type: text/plain");
611 $feed_id = db_escape_string($_REQUEST['feed_id']);
612 $offset = db_escape_string($_REQUEST['offset']);
613 $seq = db_escape_string($_REQUEST['seq']);
615 if (!$feed_id) $feed_id = -4;
616 if (!$offset) $offset = 0;
620 $reply['seq'] = $seq;
622 $headlines = api_get_headlines($link, $feed_id, 10, $offset,
623 '', ($feed_id == -4), true, false, "unread", "updated DESC");
625 //function api_get_headlines($link, $feed_id, $limit, $offset,
626 // $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
628 $reply['headlines'] = array();
629 $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
630 $reply['headlines']['content'] = $headlines;
632 print json_encode($reply);
636 if ($subop == "digest-init") {
637 header("Content-Type: text/plain");
639 $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
643 foreach ($tmp_feeds as $f) {
644 if ($f['id'] > 0 ||
$f['id'] == -4) array_push($feeds, $f);
647 print json_encode(array("feeds" => $feeds));
652 if ($subop == "catchupFeed") {
653 $feed_id = db_escape_string($_REQUEST['feed_id']);
654 $is_cat = db_escape_string($_REQUEST['is_cat']);
656 catchup_feed($link, $feed_id, $is_cat);
661 if ($subop == "sendEmail") {
662 header("Content-Type: text/plain");
664 $secretkey = $_REQUEST['secretkey'];
668 if (DIGEST_ENABLE
&& $_SESSION['email_secretkey'] &&
669 $secretkey == $_SESSION['email_secretkey']) {
671 $_SESSION['email_secretkey'] = '';
673 $destination = $_REQUEST['destination'];
674 $subject = $_REQUEST['subject'];
675 $content = $_REQUEST['content'];
677 $replyto = strip_tags($_SESSION['email_replyto']);
678 $fromname = strip_tags($_SESSION['email_fromname']);
680 $mail = new PHPMailer();
682 $mail->PluginDir
= "lib/phpmailer/";
683 $mail->SetLanguage("en", "lib/phpmailer/language/");
685 $mail->CharSet
= "UTF-8";
687 $mail->From
= $replyto;
688 $mail->FromName
= $fromname;
689 $mail->AddAddress($destination);
691 if (DIGEST_SMTP_HOST
) {
692 $mail->Host
= DIGEST_SMTP_HOST
;
693 $mail->Mailer
= "smtp";
694 $mail->SMTPAuth
= DIGEST_SMTP_LOGIN
!= '';
695 $mail->Username
= DIGEST_SMTP_LOGIN
;
696 $mail->Password
= DIGEST_SMTP_PASSWORD
;
699 $mail->IsHTML(false);
700 $mail->Subject
= $subject;
701 $mail->Body
= $content;
706 $reply['error'] = $mail->ErrorInfo
;
708 save_email_address($link, db_escape_string($destination));
709 $reply['message'] = "UPDATE_COUNTERS";
713 $reply['error'] = "Not authorized.";
716 print json_encode($reply);
720 if ($subop == "completeEmails") {
721 header("Content-Type: text/plain");
723 $search = db_escape_string($_REQUEST["search"]);
727 foreach ($_SESSION['stored_emails'] as $email) {
728 if (strpos($email, $search) !== false) {
729 print "<li>$email</li>";
738 if ($subop == "quickAddCat") {
739 header("Content-Type: text/plain");
741 $cat = db_escape_string($_REQUEST["cat"]);
743 add_feed_category($link, $cat);
745 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
746 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
748 if (db_num_rows($result) == 1) {
749 $id = db_fetch_result($result, 0, "id");
754 print_feed_cat_select($link, "cat_id", $id);
759 if ($subop == "regenFeedKey") {
760 header("Content-Type: text/plain");
762 $feed_id = db_escape_string($_REQUEST['id']);
763 $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
765 $new_key = update_feed_access_key($link, $feed_id, $is_cat);
767 print json_encode(array("link" => $new_key));
772 if ($subop == "clearKeys") {
773 db_query($link, "DELETE FROM ttrss_access_keys WHERE
774 owner_uid = " . $_SESSION["uid"]);
779 if ($subop == "verifyRegexp") {
780 header("Content-Type: text/plain");
782 $reg_exp = $_REQUEST["reg_exp"];
784 $status = @preg_match
("/$reg_exp/i", "TEST") !== false;
786 print json_encode(array("status" => $status));
790 // TODO: unify with digest-get-contents?
791 if ($subop == "cdmGetArticle") {
792 header("Content-Type: text/plain");
794 $id = db_escape_string($_REQUEST["id"]);
796 $result = db_query($link, "SELECT content,
797 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
799 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
800 ttrss_entries.id = ref_id AND
801 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
803 if (db_num_rows($result) != 0) {
804 $line = db_fetch_assoc($result);
806 $article_content = sanitize_rss($link, $line["content"],
807 false, false, $line['site_url']);
810 $article_content = '';
813 print json_encode(array("article" =>
814 array("id" => $id, "content" => $article_content)));
819 if ($subop == "scheduleFeedUpdate") {
820 header("Content-Type: text/plain");
822 $feed_id = db_escape_string($_REQUEST["id"]);
823 $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
825 $message = __("Your request could not be completed.");
829 $message = __("Feed update has been scheduled.");
831 db_query($link, "UPDATE ttrss_feeds SET
832 last_update_started = '1970-01-01',
833 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
834 owner_uid = ".$_SESSION["uid"]);
837 $message = __("Category update has been scheduled.");
840 $cat_query = "cat_id = '$feed_id'";
842 $cat_query = "cat_id IS NULL";
844 db_query($link, "UPDATE ttrss_feeds SET
845 last_update_started = '1970-01-01',
846 last_updated = '1970-01-01' WHERE $cat_query AND
847 owner_uid = ".$_SESSION["uid"]);
850 $message = __("Can't update this kind of feed.");
853 print json_encode(array("message" => $message));
857 if ($subop == "getTweetInfo") {
858 header("Content-Type: text/plain");
859 $id = db_escape_string($_REQUEST['id']);
861 $result = db_query($link, "SELECT title, link
862 FROM ttrss_entries, ttrss_user_entries
863 WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
865 if (db_num_rows($result) != 0) {
866 $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
868 $article_link = db_fetch_result($result, 0, 'link');
871 print json_encode(array("title" => $title, "link" => $article_link,
877 if ($subop == "setNote") {
878 header("Content-Type: text/plain");
880 $id = db_escape_string($_REQUEST["id"]);
881 $note = strip_tags(db_escape_string($_REQUEST["note"]));
883 db_query($link, "UPDATE ttrss_user_entries SET note = '$note'
884 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
886 $formatted_note = format_article_note($id, $note);
888 print json_encode(array("note" => $formatted_note));
892 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";