]>
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"];
7 if ($subop == "setprofile") {
8 $id = db_escape_string($_REQUEST["id"]);
10 $_SESSION["profile"] = $id;
11 $_SESSION["prefs_cache"] = array();
15 if ($subop == "remprofiles") {
16 $ids = split(",", db_escape_string(trim($_REQUEST["ids"])));
18 foreach ($ids as $id) {
19 if ($_SESSION["profile"] != $id) {
20 db_query($link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
21 owner_uid = " . $_SESSION["uid"]);
27 if ($subop == "addprofile") {
28 $title = db_escape_string(trim($_REQUEST["title"]));
30 db_query($link, "BEGIN");
32 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
33 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
35 if (db_num_rows($result) == 0) {
37 db_query($link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
38 VALUES ('$title', ".$_SESSION["uid"] .")");
40 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles WHERE
43 if (db_num_rows($result) != 0) {
44 $profile_id = db_fetch_result($result, 0, "id");
47 initialize_user_prefs($link, $_SESSION["uid"], $profile_id);
52 db_query($link, "COMMIT");
57 if ($subop == "saveprofile") {
58 $id = db_escape_string($_REQUEST["id"]);
59 $title = db_escape_string(trim($_REQUEST["value"]));
62 print __("Default profile");
67 db_query($link, "BEGIN");
69 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
70 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
72 if (db_num_rows($result) == 0) {
73 db_query($link, "UPDATE ttrss_settings_profiles
74 SET title = '$title' WHERE id = '$id' AND
75 owner_uid = " . $_SESSION["uid"]);
78 $result = db_query($link, "SELECT title FROM ttrss_settings_profiles
79 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
80 print db_fetch_result($result, 0, "title");
83 db_query($link, "COMMIT");
88 if ($subop == "remarchive") {
89 $ids = split(",", db_escape_string($_REQUEST["ids"]));
91 foreach ($ids as $id) {
92 $result = db_query($link, "DELETE FROM ttrss_archived_feeds WHERE
93 (SELECT COUNT(*) FROM ttrss_user_entries
94 WHERE orig_feed_id = '$id') = 0 AND
95 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
97 $rc = db_affected_rows($link, $result);
103 if ($subop == "addfeed") {
104 header("Content-Type: text/plain");
106 $feed = db_escape_string($_REQUEST['feed']);
107 $cat = db_escape_string($_REQUEST['cat']);
108 $login = db_escape_string($_REQUEST['login']);
109 $pass = db_escape_string($_REQUEST['pass']);
111 $rc = subscribe_to_feed($link, $feed, $cat, $login, $pass);
113 print json_encode(array("result" => $rc));
119 if ($subop == "extractfeedurls") {
120 header("Content-Type: text/plain");
122 $urls = get_feeds_from_html($_REQUEST['url']);
124 print json_encode(array("urls" => $urls));
128 if ($subop == "togglepref") {
129 header("Content-Type: text/plain");
131 $key = db_escape_string($_REQUEST["key"]);
132 set_pref($link, $key, !get_pref($link, $key));
133 $value = get_pref($link, $key);
135 print json_encode(array("param" =>$key, "value" => $value));
139 if ($subop == "setpref") {
140 header("Content-Type: text/plain");
142 $key = db_escape_string($_REQUEST["key"]);
143 $value = db_escape_string($_REQUEST["value"]);
145 set_pref($link, $key, $value);
147 print json_encode(array("param" =>$key, "value" => $value));
151 if ($subop == "mark") {
152 header("Content-Type: text/plain");
154 $mark = $_REQUEST["mark"];
155 $id = db_escape_string($_REQUEST["id"]);
163 // FIXME this needs collision testing
165 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
166 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
168 print json_encode(array("message" => "UPDATE_COUNTERS"));
172 if ($subop == "delete") {
173 header("Content-Type: text/plain");
175 $ids = db_escape_string($_REQUEST["ids"]);
177 $result = db_query($link, "DELETE FROM ttrss_user_entries
178 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
180 print json_encode(array("message" => "UPDATE_COUNTERS"));
184 if ($subop == "unarchive") {
185 header("Content-Type: text/plain");
187 $ids = db_escape_string($_REQUEST["ids"]);
189 $result = db_query($link, "UPDATE ttrss_user_entries
190 SET feed_id = orig_feed_id, orig_feed_id = NULL
191 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
193 print json_encode(array("message" => "UPDATE_COUNTERS"));
197 if ($subop == "archive") {
198 header("Content-Type: text/plain");
200 $ids = split(",", db_escape_string($_REQUEST["ids"]));
202 foreach ($ids as $id) {
203 archive_article($link, $id, $_SESSION["uid"]);
206 print json_encode(array("message" => "UPDATE_COUNTERS"));
211 if ($subop == "publ") {
212 $pub = $_REQUEST["pub"];
213 $id = db_escape_string($_REQUEST["id"]);
214 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
222 if ($note != 'undefined') {
223 $note_qpart = "note = '$note',";
226 // FIXME this needs collision testing
228 $result = db_query($link, "UPDATE ttrss_user_entries SET
231 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
236 if ($note != 'undefined') {
237 $note_size = strlen($note);
238 print "<note id=\"$id\" size=\"$note_size\">";
239 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
243 print "<message>UPDATE_COUNTERS</message>";
245 print "</rpc-reply>";
250 /* if ($subop == "updateFeed") {
251 $feed_id = db_escape_string($_REQUEST["feed"]);
253 update_rss_feed($link, $feed_id);
256 print "<message>UPDATE_COUNTERS</message>";
257 print "</rpc-reply>";
262 if ($subop == "updateAllFeeds" ||
$subop == "getAllCounters") {
264 header("Content-Type: text/plain");
266 $last_article_id = (int) $_REQUEST["last_article_id"];
270 if ($seq) $reply['seq'] = $seq;
272 if ($last_article_id != getLastArticleId($link)) {
273 $omode = $_REQUEST["omode"];
276 $reply['counters'] = getAllCounters($link, $omode);
278 $reply['counters'] = getGlobalCounters($link);
281 $reply['runtime-info'] = make_runtime_info($link);
284 print json_encode($reply);
288 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
289 if ($subop == "catchupSelected") {
290 header("Content-Type: text/plain");
292 $ids = split(",", db_escape_string($_REQUEST["ids"]));
293 $cmode = sprintf("%d", $_REQUEST["cmode"]);
295 catchupArticlesById($link, $ids, $cmode);
297 print json_encode(array("message" => "UPDATE_COUNTERS"));
301 if ($subop == "markSelected") {
302 header("Content-Type: text/plain");
304 $ids = split(",", db_escape_string($_REQUEST["ids"]));
305 $cmode = sprintf("%d", $_REQUEST["cmode"]);
307 markArticlesById($link, $ids, $cmode);
309 print json_encode(array("message" => "UPDATE_COUNTERS"));
313 if ($subop == "publishSelected") {
314 header("Content-Type: text/plain");
316 $ids = split(",", db_escape_string($_REQUEST["ids"]));
317 $cmode = sprintf("%d", $_REQUEST["cmode"]);
319 publishArticlesById($link, $ids, $cmode);
321 print json_encode(array("message" => "UPDATE_COUNTERS"));
326 if ($subop == "sanityCheck") {
328 if (sanity_check($link)) {
329 print "<error error-code=\"0\"/>";
331 print "<init-params><![CDATA[";
332 print json_encode(make_init_params($link));
333 print "]]></init-params>";
335 print_runtime_info($link);
337 print "</rpc-reply>";
342 /* if ($subop == "globalPurge") {
345 global_purge_old_posts($link, true);
346 print "</rpc-reply>";
351 if ($subop == "setArticleTags") {
352 header("Content-Type: text/plain");
356 $id = db_escape_string($_REQUEST["id"]);
358 $tags_str = db_escape_string($_REQUEST["tags_str"]);
359 $tags = array_unique(trim_array(split(",", $tags_str)));
361 db_query($link, "BEGIN");
363 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
364 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
366 if (db_num_rows($result) == 1) {
368 $tags_to_cache = array();
370 $int_id = db_fetch_result($result, 0, "int_id");
372 db_query($link, "DELETE FROM ttrss_tags WHERE
373 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
375 foreach ($tags as $tag) {
376 $tag = sanitize_tag($tag);
378 if (!tag_is_valid($tag)) {
382 if (preg_match("/^[0-9]*$/", $tag)) {
386 // print "<!-- $id : $int_id : $tag -->";
389 db_query($link, "INSERT INTO ttrss_tags
390 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
393 array_push($tags_to_cache, $tag);
396 /* update tag cache */
398 $tags_str = join(",", $tags_to_cache);
400 db_query($link, "UPDATE ttrss_user_entries
401 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
402 AND owner_uid = " . $_SESSION["uid"]);
405 db_query($link, "COMMIT");
408 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
409 $memcache->delete($obj_id);
412 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
414 print json_encode(array("tags_str" => array("id" => $id,
415 "content" => $tags_str)));
421 if ($subop == "regenOPMLKey") {
425 update_feed_access_key($link, 'OPML:Publish',
426 false, $_SESSION["uid"]);
428 $new_link = opml_publish_url($link);
429 print "<link><![CDATA[$new_link]]></link>";
430 print "</rpc-reply>";
435 if ($subop == "logout") {
441 if ($subop == "completeTags") {
442 header("Content-Type: text/plain");
444 $search = db_escape_string($_REQUEST["search"]);
446 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
447 WHERE owner_uid = '".$_SESSION["uid"]."' AND
448 tag_name LIKE '$search%' ORDER BY tag_name
452 while ($line = db_fetch_assoc($result)) {
453 print "<li>" . $line["tag_name"] . "</li>";
460 if ($subop == "purge") {
461 $ids = split(",", db_escape_string($_REQUEST["ids"]));
462 $days = sprintf("%d", $_REQUEST["days"]);
464 foreach ($ids as $id) {
466 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
467 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
469 if (db_num_rows($result) == 1) {
470 purge_feed($link, $id, $days);
477 /* if ($subop == "setScore") {
478 $id = db_escape_string($_REQUEST["id"]);
479 $score = sprintf("%d", $_REQUEST["score"]);
481 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
482 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
484 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
491 if ($subop == "getArticles") {
492 $ids = split(",", db_escape_string($_REQUEST["ids"]));
496 foreach ($ids as $id) {
498 outputArticleXML($link, $id, 0, false);
501 print "</rpc-reply>";
506 if ($subop == "checkDate") {
507 header("Content-Type: text/plain");
509 $date = db_escape_string($_REQUEST["date"]);
510 $date_parsed = strtotime($date);
512 print json_encode(array("result" => (bool)$date_parsed));
516 if ($subop == "assignToLabel" ||
$subop == "removeFromLabel") {
517 header("Content-Type: text/plain");
521 $ids = split(",", db_escape_string($_REQUEST["ids"]));
522 $label_id = db_escape_string($_REQUEST["lid"]);
524 $label = db_escape_string(label_find_caption($link, $label_id,
527 $reply["info-for-headlines"] = array();
531 foreach ($ids as $id) {
533 if ($subop == "assignToLabel")
534 label_add_article($link, $id, $label, $_SESSION["uid"]);
536 label_remove_article($link, $id, $label, $_SESSION["uid"]);
538 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
540 array_push($reply["info-for-headlines"],
541 array("id" => $id, "labels" => format_article_labels($labels, $id)));
546 $reply["message"] = "UPDATE_COUNTERS";
548 print json_encode($reply);
553 if ($subop == "updateFeedBrowser") {
554 header("Content-Type: text/plain");
556 $search = db_escape_string($_REQUEST["search"]);
557 $limit = db_escape_string($_REQUEST["limit"]);
558 $mode = (int) db_escape_string($_REQUEST["mode"]);
560 print json_encode(array("content" =>
561 make_feed_browser($link, $search, $limit, $mode),
566 if ($subop == "massSubscribe") {
568 $ids = split(",", db_escape_string($_REQUEST["ids"]));
569 $mode = $_REQUEST["mode"];
571 $subscribed = array();
573 foreach ($ids as $id) {
576 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
578 } else if ($mode == 2) {
579 $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
580 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
581 $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
582 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
585 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
586 $title = db_escape_string(db_fetch_result($result, 0, "title"));
588 $title_orig = db_fetch_result($result, 0, "title");
590 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
591 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
593 if (db_num_rows($result) == 0) {
595 $result = db_query($link,
596 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
597 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
598 } else if ($mode == 2) {
599 $result = db_query($link,
600 "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
601 VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
603 array_push($subscribed, $title_orig);
610 if ($subop == "digest-get-contents") {
611 header("Content-Type: text/plain");
613 $article_id = db_escape_string($_REQUEST['article_id']);
615 $result = db_query($link, "SELECT content
616 FROM ttrss_entries, ttrss_user_entries
617 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
619 $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
621 print json_encode(array("article" =>
622 array("id" => $id, "content" => $content)));
626 if ($subop == "digest-update") {
627 header("Content-Type: text/plain");
629 $feed_id = db_escape_string($_REQUEST['feed_id']);
630 $offset = db_escape_string($_REQUEST['offset']);
631 $seq = db_escape_string($_REQUEST['seq']);
633 if (!$feed_id) $feed_id = -4;
634 if (!$offset) $offset = 0;
638 $reply['seq'] = $seq;
640 $headlines = api_get_headlines($link, $feed_id, 10, $offset,
641 '', ($feed_id == -4), true, false, "unread", "updated DESC");
643 //function api_get_headlines($link, $feed_id, $limit, $offset,
644 // $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
646 $reply['headlines'] = array();
647 $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
648 $reply['headlines']['content'] = $headlines;
650 print json_encode($reply);
654 if ($subop == "digest-init") {
655 header("Content-Type: text/plain");
657 $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
661 foreach ($tmp_feeds as $f) {
662 if ($f['id'] > 0 ||
$f['id'] == -4) array_push($feeds, $f);
665 print json_encode(array("feeds" => $feeds));
670 if ($subop == "catchupFeed") {
671 $feed_id = db_escape_string($_REQUEST['feed_id']);
672 $is_cat = db_escape_string($_REQUEST['is_cat']);
674 catchup_feed($link, $feed_id, $is_cat);
679 if ($subop == "sendEmail") {
680 header("Content-Type: text/plain");
682 $secretkey = $_REQUEST['secretkey'];
686 if (DIGEST_ENABLE
&& $_SESSION['email_secretkey'] &&
687 $secretkey == $_SESSION['email_secretkey']) {
689 $_SESSION['email_secretkey'] = '';
691 $destination = $_REQUEST['destination'];
692 $subject = $_REQUEST['subject'];
693 $content = $_REQUEST['content'];
695 $replyto = strip_tags($_SESSION['email_replyto']);
696 $fromname = strip_tags($_SESSION['email_fromname']);
698 $mail = new PHPMailer();
700 $mail->PluginDir
= "lib/phpmailer/";
701 $mail->SetLanguage("en", "lib/phpmailer/language/");
703 $mail->CharSet
= "UTF-8";
705 $mail->From
= $replyto;
706 $mail->FromName
= $fromname;
707 $mail->AddAddress($destination);
709 if (DIGEST_SMTP_HOST
) {
710 $mail->Host
= DIGEST_SMTP_HOST
;
711 $mail->Mailer
= "smtp";
712 $mail->SMTPAuth
= DIGEST_SMTP_LOGIN
!= '';
713 $mail->Username
= DIGEST_SMTP_LOGIN
;
714 $mail->Password
= DIGEST_SMTP_PASSWORD
;
717 $mail->IsHTML(false);
718 $mail->Subject
= $subject;
719 $mail->Body
= $content;
724 $reply['error'] = $mail->ErrorInfo
;
726 save_email_address($link, db_escape_string($destination));
727 $reply['message'] = "UPDATE_COUNTERS";
731 $reply['error'] = "Not authorized.";
734 print json_encode($reply);
738 if ($subop == "completeEmails") {
740 $search = db_escape_string($_REQUEST["search"]);
744 foreach ($_SESSION['stored_emails'] as $email) {
745 if (strpos($email, $search) !== false) {
746 print "<li>$email</li>";
755 if ($subop == "quickAddCat") {
758 $cat = db_escape_string($_REQUEST["cat"]);
760 add_feed_category($link, $cat);
762 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
763 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
765 if (db_num_rows($result) == 1) {
766 $id = db_fetch_result($result, 0, "id");
771 print_feed_cat_select($link, "cat_id", $id);
773 print "</rpc-reply>";
778 if ($subop == "regenFeedKey") {
779 $feed_id = db_escape_string($_REQUEST['id']);
780 $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
784 $new_key = update_feed_access_key($link, $feed_id, $is_cat);
786 print "<link><![CDATA[$new_key]]></link>";
788 print "</rpc-reply>";
793 if ($subop == "clearKeys") {
795 db_query($link, "DELETE FROM ttrss_access_keys WHERE
796 owner_uid = " . $_SESSION["uid"]);
799 print "<message>UPDATE_COUNTERS</message>";
800 print "</rpc-reply>";
805 if ($subop == "verifyRegexp") {
806 $reg_exp = $_REQUEST["reg_exp"];
808 print "<rpc-reply><status>";
810 if (@preg_match
("/$reg_exp/i", "TEST") === false) {
816 print "</status></rpc-reply>";
821 if ($subop == "cdmGetArticle") {
822 header("Content-Type: text/plain");
824 $id = db_escape_string($_REQUEST["id"]);
826 $result = db_query($link, "SELECT content,
827 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
829 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
830 ttrss_entries.id = ref_id AND
831 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
833 if (db_num_rows($result) != 0) {
834 $line = db_fetch_assoc($result);
836 $article_content = sanitize_rss($link, $line["content"],
837 false, false, $line['site_url']);
840 $article_content = '';
843 print json_encode(array("article" =>
844 array("id" => $id, "content" => $article_content)));
849 if ($subop == "scheduleFeedUpdate") {
850 header("Content-Type: text/plain");
852 $feed_id = db_escape_string($_REQUEST["id"]);
853 $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
855 $message = __("Your request could not be completed.");
859 $message = __("Feed update has been scheduled.");
861 db_query($link, "UPDATE ttrss_feeds SET
862 last_update_started = '1970-01-01',
863 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
864 owner_uid = ".$_SESSION["uid"]);
867 $message = __("Category update has been scheduled.");
870 $cat_query = "cat_id = '$feed_id'";
872 $cat_query = "cat_id IS NULL";
874 db_query($link, "UPDATE ttrss_feeds SET
875 last_update_started = '1970-01-01',
876 last_updated = '1970-01-01' WHERE $cat_query AND
877 owner_uid = ".$_SESSION["uid"]);
880 $message = __("Can't update this kind of feed.");
883 print json_encode(array("message" => $message));
887 if ($subop == "getTweetInfo") {
888 header("Content-Type: text/plain");
889 $id = db_escape_string($_REQUEST['id']);
891 $result = db_query($link, "SELECT title, link
892 FROM ttrss_entries, ttrss_user_entries
893 WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
895 if (db_num_rows($result) != 0) {
896 $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
898 $article_link = db_fetch_result($result, 0, 'link');
901 print json_encode(array("title" => $title, "link" => $article_link,
907 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";