]>
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 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
148 $key = db_escape_string($_REQUEST["key"]);
149 $value = db_escape_string($value);
151 set_pref($link, $key, $value);
153 print json_encode(array("param" =>$key, "value" => $value));
157 if ($subop == "mark") {
158 header("Content-Type: text/plain");
160 $mark = $_REQUEST["mark"];
161 $id = db_escape_string($_REQUEST["id"]);
169 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
170 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
172 print json_encode(array("message" => "UPDATE_COUNTERS"));
176 if ($subop == "delete") {
177 header("Content-Type: text/plain");
179 $ids = db_escape_string($_REQUEST["ids"]);
181 $result = db_query($link, "DELETE FROM ttrss_user_entries
182 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
184 print json_encode(array("message" => "UPDATE_COUNTERS"));
188 if ($subop == "unarchive") {
189 header("Content-Type: text/plain");
191 $ids = db_escape_string($_REQUEST["ids"]);
193 $result = db_query($link, "UPDATE ttrss_user_entries
194 SET feed_id = orig_feed_id, orig_feed_id = NULL
195 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
197 print json_encode(array("message" => "UPDATE_COUNTERS"));
201 if ($subop == "archive") {
202 header("Content-Type: text/plain");
204 $ids = split(",", db_escape_string($_REQUEST["ids"]));
206 foreach ($ids as $id) {
207 archive_article($link, $id, $_SESSION["uid"]);
210 print json_encode(array("message" => "UPDATE_COUNTERS"));
214 if ($subop == "publ") {
215 header("Content-Type: text/plain");
217 $pub = $_REQUEST["pub"];
218 $id = db_escape_string($_REQUEST["id"]);
219 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
227 $result = db_query($link, "UPDATE ttrss_user_entries SET
229 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
231 print json_encode(array("message" => "UPDATE_COUNTERS"));
235 /* if ($subop == "updateFeed") {
236 $feed_id = db_escape_string($_REQUEST["feed"]);
238 update_rss_feed($link, $feed_id);
241 print "<message>UPDATE_COUNTERS</message>";
242 print "</rpc-reply>";
247 if ($subop == "updateAllFeeds" ||
$subop == "getAllCounters") {
249 header("Content-Type: text/plain");
251 $last_article_id = (int) $_REQUEST["last_article_id"];
255 if ($seq) $reply['seq'] = $seq;
257 if ($last_article_id != getLastArticleId($link)) {
258 $omode = $_REQUEST["omode"];
261 $reply['counters'] = getAllCounters($link, $omode);
263 $reply['counters'] = getGlobalCounters($link);
266 $reply['runtime-info'] = make_runtime_info($link);
269 print json_encode($reply);
273 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
274 if ($subop == "catchupSelected") {
275 header("Content-Type: text/plain");
277 $ids = split(",", db_escape_string($_REQUEST["ids"]));
278 $cmode = sprintf("%d", $_REQUEST["cmode"]);
280 catchupArticlesById($link, $ids, $cmode);
282 print json_encode(array("message" => "UPDATE_COUNTERS"));
286 if ($subop == "markSelected") {
287 header("Content-Type: text/plain");
289 $ids = split(",", db_escape_string($_REQUEST["ids"]));
290 $cmode = sprintf("%d", $_REQUEST["cmode"]);
292 markArticlesById($link, $ids, $cmode);
294 print json_encode(array("message" => "UPDATE_COUNTERS"));
298 if ($subop == "publishSelected") {
299 header("Content-Type: text/plain");
301 $ids = split(",", db_escape_string($_REQUEST["ids"]));
302 $cmode = sprintf("%d", $_REQUEST["cmode"]);
304 publishArticlesById($link, $ids, $cmode);
306 print json_encode(array("message" => "UPDATE_COUNTERS"));
311 if ($subop == "sanityCheck") {
313 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
316 if (sanity_check($link)) {
317 print "<error error-code=\"0\"/>";
319 print "<init-params><![CDATA[";
320 print json_encode(make_init_params($link));
321 print "]]></init-params>";
323 print_runtime_info($link);
325 print "</rpc-reply>";
330 /* if ($subop == "globalPurge") {
333 global_purge_old_posts($link, true);
334 print "</rpc-reply>";
339 if ($subop == "setArticleTags") {
340 header("Content-Type: text/plain");
344 $id = db_escape_string($_REQUEST["id"]);
346 $tags_str = db_escape_string($_REQUEST["tags_str"]);
347 $tags = array_unique(trim_array(split(",", $tags_str)));
349 db_query($link, "BEGIN");
351 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
352 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
354 if (db_num_rows($result) == 1) {
356 $tags_to_cache = array();
358 $int_id = db_fetch_result($result, 0, "int_id");
360 db_query($link, "DELETE FROM ttrss_tags WHERE
361 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
363 foreach ($tags as $tag) {
364 $tag = sanitize_tag($tag);
366 if (!tag_is_valid($tag)) {
370 if (preg_match("/^[0-9]*$/", $tag)) {
374 // print "<!-- $id : $int_id : $tag -->";
377 db_query($link, "INSERT INTO ttrss_tags
378 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
381 array_push($tags_to_cache, $tag);
384 /* update tag cache */
386 $tags_str = join(",", $tags_to_cache);
388 db_query($link, "UPDATE ttrss_user_entries
389 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
390 AND owner_uid = " . $_SESSION["uid"]);
393 db_query($link, "COMMIT");
396 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
397 $memcache->delete($obj_id);
400 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
402 print json_encode(array("tags_str" => array("id" => $id,
403 "content" => $tags_str)));
408 if ($subop == "regenOPMLKey") {
409 header("Content-Type: text/plain");
411 update_feed_access_key($link, 'OPML:Publish',
412 false, $_SESSION["uid"]);
414 $new_link = opml_publish_url($link);
416 print json_encode(array("link" => $new_link));
421 if ($subop == "logout") {
427 if ($subop == "completeTags") {
428 header("Content-Type: text/plain");
430 $search = db_escape_string($_REQUEST["search"]);
432 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
433 WHERE owner_uid = '".$_SESSION["uid"]."' AND
434 tag_name LIKE '$search%' ORDER BY tag_name
438 while ($line = db_fetch_assoc($result)) {
439 print "<li>" . $line["tag_name"] . "</li>";
446 if ($subop == "purge") {
447 $ids = split(",", db_escape_string($_REQUEST["ids"]));
448 $days = sprintf("%d", $_REQUEST["days"]);
450 foreach ($ids as $id) {
452 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
453 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
455 if (db_num_rows($result) == 1) {
456 purge_feed($link, $id, $days);
463 /* if ($subop == "setScore") {
464 $id = db_escape_string($_REQUEST["id"]);
465 $score = sprintf("%d", $_REQUEST["score"]);
467 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
468 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
470 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
476 if ($subop == "getArticles") {
477 header("Content-Type: text/plain");
479 $ids = split(",", db_escape_string($_REQUEST["ids"]));
482 foreach ($ids as $id) {
484 array_push($articles, format_article($link, $id, 0, false));
488 print json_encode($articles);
492 if ($subop == "checkDate") {
493 header("Content-Type: text/plain");
495 $date = db_escape_string($_REQUEST["date"]);
496 $date_parsed = strtotime($date);
498 print json_encode(array("result" => (bool)$date_parsed));
502 if ($subop == "assignToLabel" ||
$subop == "removeFromLabel") {
503 header("Content-Type: text/plain");
507 $ids = split(",", db_escape_string($_REQUEST["ids"]));
508 $label_id = db_escape_string($_REQUEST["lid"]);
510 $label = db_escape_string(label_find_caption($link, $label_id,
513 $reply["info-for-headlines"] = array();
517 foreach ($ids as $id) {
519 if ($subop == "assignToLabel")
520 label_add_article($link, $id, $label, $_SESSION["uid"]);
522 label_remove_article($link, $id, $label, $_SESSION["uid"]);
524 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
526 array_push($reply["info-for-headlines"],
527 array("id" => $id, "labels" => format_article_labels($labels, $id)));
532 $reply["message"] = "UPDATE_COUNTERS";
534 print json_encode($reply);
539 if ($subop == "updateFeedBrowser") {
540 header("Content-Type: text/plain");
542 $search = db_escape_string($_REQUEST["search"]);
543 $limit = db_escape_string($_REQUEST["limit"]);
544 $mode = (int) db_escape_string($_REQUEST["mode"]);
546 print json_encode(array("content" =>
547 make_feed_browser($link, $search, $limit, $mode),
553 if ($subop == "massSubscribe") {
555 $ids = split(",", db_escape_string($_REQUEST["ids"]));
556 $mode = $_REQUEST["mode"];
558 $subscribed = array();
560 foreach ($ids as $id) {
563 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
565 } else if ($mode == 2) {
566 $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
567 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
568 $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
569 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
572 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
573 $title = db_escape_string(db_fetch_result($result, 0, "title"));
575 $title_orig = db_fetch_result($result, 0, "title");
577 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
578 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
580 if (db_num_rows($result) == 0) {
582 $result = db_query($link,
583 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
584 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
585 } else if ($mode == 2) {
586 $result = db_query($link,
587 "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
588 VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
590 array_push($subscribed, $title_orig);
597 if ($subop == "digest-get-contents") {
598 header("Content-Type: text/plain");
600 $article_id = db_escape_string($_REQUEST['article_id']);
602 $result = db_query($link, "SELECT content
603 FROM ttrss_entries, ttrss_user_entries
604 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
606 $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
608 print json_encode(array("article" =>
609 array("id" => $id, "content" => $content)));
613 if ($subop == "digest-update") {
614 header("Content-Type: text/plain");
616 $feed_id = db_escape_string($_REQUEST['feed_id']);
617 $offset = db_escape_string($_REQUEST['offset']);
618 $seq = db_escape_string($_REQUEST['seq']);
620 if (!$feed_id) $feed_id = -4;
621 if (!$offset) $offset = 0;
625 $reply['seq'] = $seq;
627 $headlines = api_get_headlines($link, $feed_id, 10, $offset,
628 '', ($feed_id == -4), true, false, "unread", "updated DESC");
630 //function api_get_headlines($link, $feed_id, $limit, $offset,
631 // $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
633 $reply['headlines'] = array();
634 $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
635 $reply['headlines']['content'] = $headlines;
637 print json_encode($reply);
641 if ($subop == "digest-init") {
642 header("Content-Type: text/plain");
644 $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
648 foreach ($tmp_feeds as $f) {
649 if ($f['id'] > 0 ||
$f['id'] == -4) array_push($feeds, $f);
652 print json_encode(array("feeds" => $feeds));
657 if ($subop == "catchupFeed") {
658 $feed_id = db_escape_string($_REQUEST['feed_id']);
659 $is_cat = db_escape_string($_REQUEST['is_cat']);
661 catchup_feed($link, $feed_id, $is_cat);
666 if ($subop == "sendEmail") {
667 header("Content-Type: text/plain");
669 $secretkey = $_REQUEST['secretkey'];
673 if (DIGEST_ENABLE
&& $_SESSION['email_secretkey'] &&
674 $secretkey == $_SESSION['email_secretkey']) {
676 $_SESSION['email_secretkey'] = '';
678 $destination = $_REQUEST['destination'];
679 $subject = $_REQUEST['subject'];
680 $content = $_REQUEST['content'];
682 $replyto = strip_tags($_SESSION['email_replyto']);
683 $fromname = strip_tags($_SESSION['email_fromname']);
685 $mail = new PHPMailer();
687 $mail->PluginDir
= "lib/phpmailer/";
688 $mail->SetLanguage("en", "lib/phpmailer/language/");
690 $mail->CharSet
= "UTF-8";
692 $mail->From
= $replyto;
693 $mail->FromName
= $fromname;
694 $mail->AddAddress($destination);
696 if (DIGEST_SMTP_HOST
) {
697 $mail->Host
= DIGEST_SMTP_HOST
;
698 $mail->Mailer
= "smtp";
699 $mail->SMTPAuth
= DIGEST_SMTP_LOGIN
!= '';
700 $mail->Username
= DIGEST_SMTP_LOGIN
;
701 $mail->Password
= DIGEST_SMTP_PASSWORD
;
704 $mail->IsHTML(false);
705 $mail->Subject
= $subject;
706 $mail->Body
= $content;
711 $reply['error'] = $mail->ErrorInfo
;
713 save_email_address($link, db_escape_string($destination));
714 $reply['message'] = "UPDATE_COUNTERS";
718 $reply['error'] = "Not authorized.";
721 print json_encode($reply);
725 if ($subop == "completeEmails") {
726 header("Content-Type: text/plain");
728 $search = db_escape_string($_REQUEST["search"]);
732 foreach ($_SESSION['stored_emails'] as $email) {
733 if (strpos($email, $search) !== false) {
734 print "<li>$email</li>";
743 if ($subop == "quickAddCat") {
744 header("Content-Type: text/plain");
746 $cat = db_escape_string($_REQUEST["cat"]);
748 add_feed_category($link, $cat);
750 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
751 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
753 if (db_num_rows($result) == 1) {
754 $id = db_fetch_result($result, 0, "id");
759 print_feed_cat_select($link, "cat_id", $id);
764 if ($subop == "regenFeedKey") {
765 header("Content-Type: text/plain");
767 $feed_id = db_escape_string($_REQUEST['id']);
768 $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
770 $new_key = update_feed_access_key($link, $feed_id, $is_cat);
772 print json_encode(array("link" => $new_key));
777 if ($subop == "clearKeys") {
778 db_query($link, "DELETE FROM ttrss_access_keys WHERE
779 owner_uid = " . $_SESSION["uid"]);
784 if ($subop == "verifyRegexp") {
785 header("Content-Type: text/plain");
787 $reg_exp = $_REQUEST["reg_exp"];
789 $status = @preg_match
("/$reg_exp/i", "TEST") !== false;
791 print json_encode(array("status" => $status));
795 // TODO: unify with digest-get-contents?
796 if ($subop == "cdmGetArticle") {
797 header("Content-Type: text/plain");
799 $id = db_escape_string($_REQUEST["id"]);
801 $result = db_query($link, "SELECT content,
802 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
804 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
805 ttrss_entries.id = ref_id AND
806 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
808 if (db_num_rows($result) != 0) {
809 $line = db_fetch_assoc($result);
811 $article_content = sanitize_rss($link, $line["content"],
812 false, false, $line['site_url']);
815 $article_content = '';
818 print json_encode(array("article" =>
819 array("id" => $id, "content" => $article_content)));
824 if ($subop == "scheduleFeedUpdate") {
825 header("Content-Type: text/plain");
827 $feed_id = db_escape_string($_REQUEST["id"]);
828 $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
830 $message = __("Your request could not be completed.");
834 $message = __("Feed update has been scheduled.");
836 db_query($link, "UPDATE ttrss_feeds SET
837 last_update_started = '1970-01-01',
838 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
839 owner_uid = ".$_SESSION["uid"]);
842 $message = __("Category update has been scheduled.");
845 $cat_query = "cat_id = '$feed_id'";
847 $cat_query = "cat_id IS NULL";
849 db_query($link, "UPDATE ttrss_feeds SET
850 last_update_started = '1970-01-01',
851 last_updated = '1970-01-01' WHERE $cat_query AND
852 owner_uid = ".$_SESSION["uid"]);
855 $message = __("Can't update this kind of feed.");
858 print json_encode(array("message" => $message));
862 if ($subop == "getTweetInfo") {
863 header("Content-Type: text/plain");
864 $id = db_escape_string($_REQUEST['id']);
866 $result = db_query($link, "SELECT title, link
867 FROM ttrss_entries, ttrss_user_entries
868 WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
870 if (db_num_rows($result) != 0) {
871 $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
873 $article_link = db_fetch_result($result, 0, 'link');
876 print json_encode(array("title" => $title, "link" => $article_link,
882 if ($subop == "setNote") {
883 header("Content-Type: text/plain");
885 $id = db_escape_string($_REQUEST["id"]);
886 $note = strip_tags(db_escape_string($_REQUEST["note"]));
888 db_query($link, "UPDATE ttrss_user_entries SET note = '$note'
889 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
891 $formatted_note = format_article_note($id, $note);
893 print json_encode(array("note" => $formatted_note));
897 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";