]>
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"));
213 if ($subop == "publ") {
214 $pub = $_REQUEST["pub"];
215 $id = db_escape_string($_REQUEST["id"]);
216 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
224 if ($note != 'undefined') {
225 $note_qpart = "note = '$note',";
228 // FIXME this needs collision testing
230 $result = db_query($link, "UPDATE ttrss_user_entries SET
233 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
238 if ($note != 'undefined') {
239 $note_size = strlen($note);
240 print "<note id=\"$id\" size=\"$note_size\">";
241 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
245 print "<message>UPDATE_COUNTERS</message>";
247 print "</rpc-reply>";
252 /* if ($subop == "updateFeed") {
253 $feed_id = db_escape_string($_REQUEST["feed"]);
255 update_rss_feed($link, $feed_id);
258 print "<message>UPDATE_COUNTERS</message>";
259 print "</rpc-reply>";
264 if ($subop == "updateAllFeeds" ||
$subop == "getAllCounters") {
266 header("Content-Type: text/plain");
268 $last_article_id = (int) $_REQUEST["last_article_id"];
272 if ($seq) $reply['seq'] = $seq;
274 if ($last_article_id != getLastArticleId($link)) {
275 $omode = $_REQUEST["omode"];
278 $reply['counters'] = getAllCounters($link, $omode);
280 $reply['counters'] = getGlobalCounters($link);
283 $reply['runtime-info'] = make_runtime_info($link);
286 print json_encode($reply);
290 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
291 if ($subop == "catchupSelected") {
292 header("Content-Type: text/plain");
294 $ids = split(",", db_escape_string($_REQUEST["ids"]));
295 $cmode = sprintf("%d", $_REQUEST["cmode"]);
297 catchupArticlesById($link, $ids, $cmode);
299 print json_encode(array("message" => "UPDATE_COUNTERS"));
303 if ($subop == "markSelected") {
304 header("Content-Type: text/plain");
306 $ids = split(",", db_escape_string($_REQUEST["ids"]));
307 $cmode = sprintf("%d", $_REQUEST["cmode"]);
309 markArticlesById($link, $ids, $cmode);
311 print json_encode(array("message" => "UPDATE_COUNTERS"));
315 if ($subop == "publishSelected") {
316 header("Content-Type: text/plain");
318 $ids = split(",", db_escape_string($_REQUEST["ids"]));
319 $cmode = sprintf("%d", $_REQUEST["cmode"]);
321 publishArticlesById($link, $ids, $cmode);
323 print json_encode(array("message" => "UPDATE_COUNTERS"));
328 if ($subop == "sanityCheck") {
330 if (sanity_check($link)) {
331 print "<error error-code=\"0\"/>";
333 print "<init-params><![CDATA[";
334 print json_encode(make_init_params($link));
335 print "]]></init-params>";
337 print_runtime_info($link);
339 print "</rpc-reply>";
344 /* if ($subop == "globalPurge") {
347 global_purge_old_posts($link, true);
348 print "</rpc-reply>";
353 if ($subop == "setArticleTags") {
354 header("Content-Type: text/plain");
358 $id = db_escape_string($_REQUEST["id"]);
360 $tags_str = db_escape_string($_REQUEST["tags_str"]);
361 $tags = array_unique(trim_array(split(",", $tags_str)));
363 db_query($link, "BEGIN");
365 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
366 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
368 if (db_num_rows($result) == 1) {
370 $tags_to_cache = array();
372 $int_id = db_fetch_result($result, 0, "int_id");
374 db_query($link, "DELETE FROM ttrss_tags WHERE
375 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
377 foreach ($tags as $tag) {
378 $tag = sanitize_tag($tag);
380 if (!tag_is_valid($tag)) {
384 if (preg_match("/^[0-9]*$/", $tag)) {
388 // print "<!-- $id : $int_id : $tag -->";
391 db_query($link, "INSERT INTO ttrss_tags
392 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
395 array_push($tags_to_cache, $tag);
398 /* update tag cache */
400 $tags_str = join(",", $tags_to_cache);
402 db_query($link, "UPDATE ttrss_user_entries
403 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
404 AND owner_uid = " . $_SESSION["uid"]);
407 db_query($link, "COMMIT");
410 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
411 $memcache->delete($obj_id);
414 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
416 print json_encode(array("tags_str" => array("id" => $id,
417 "content" => $tags_str)));
422 if ($subop == "regenOPMLKey") {
423 header("Content-Type: text/plain");
425 update_feed_access_key($link, 'OPML:Publish',
426 false, $_SESSION["uid"]);
428 $new_link = opml_publish_url($link);
430 print json_encode(array("link" => $new_link));
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),
567 if ($subop == "massSubscribe") {
569 $ids = split(",", db_escape_string($_REQUEST["ids"]));
570 $mode = $_REQUEST["mode"];
572 $subscribed = array();
574 foreach ($ids as $id) {
577 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
579 } else if ($mode == 2) {
580 $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
581 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
582 $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
583 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
586 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
587 $title = db_escape_string(db_fetch_result($result, 0, "title"));
589 $title_orig = db_fetch_result($result, 0, "title");
591 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
592 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
594 if (db_num_rows($result) == 0) {
596 $result = db_query($link,
597 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
598 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
599 } else if ($mode == 2) {
600 $result = db_query($link,
601 "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
602 VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
604 array_push($subscribed, $title_orig);
611 if ($subop == "digest-get-contents") {
612 header("Content-Type: text/plain");
614 $article_id = db_escape_string($_REQUEST['article_id']);
616 $result = db_query($link, "SELECT content
617 FROM ttrss_entries, ttrss_user_entries
618 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
620 $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
622 print json_encode(array("article" =>
623 array("id" => $id, "content" => $content)));
627 if ($subop == "digest-update") {
628 header("Content-Type: text/plain");
630 $feed_id = db_escape_string($_REQUEST['feed_id']);
631 $offset = db_escape_string($_REQUEST['offset']);
632 $seq = db_escape_string($_REQUEST['seq']);
634 if (!$feed_id) $feed_id = -4;
635 if (!$offset) $offset = 0;
639 $reply['seq'] = $seq;
641 $headlines = api_get_headlines($link, $feed_id, 10, $offset,
642 '', ($feed_id == -4), true, false, "unread", "updated DESC");
644 //function api_get_headlines($link, $feed_id, $limit, $offset,
645 // $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
647 $reply['headlines'] = array();
648 $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
649 $reply['headlines']['content'] = $headlines;
651 print json_encode($reply);
655 if ($subop == "digest-init") {
656 header("Content-Type: text/plain");
658 $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
662 foreach ($tmp_feeds as $f) {
663 if ($f['id'] > 0 ||
$f['id'] == -4) array_push($feeds, $f);
666 print json_encode(array("feeds" => $feeds));
671 if ($subop == "catchupFeed") {
672 $feed_id = db_escape_string($_REQUEST['feed_id']);
673 $is_cat = db_escape_string($_REQUEST['is_cat']);
675 catchup_feed($link, $feed_id, $is_cat);
680 if ($subop == "sendEmail") {
681 header("Content-Type: text/plain");
683 $secretkey = $_REQUEST['secretkey'];
687 if (DIGEST_ENABLE
&& $_SESSION['email_secretkey'] &&
688 $secretkey == $_SESSION['email_secretkey']) {
690 $_SESSION['email_secretkey'] = '';
692 $destination = $_REQUEST['destination'];
693 $subject = $_REQUEST['subject'];
694 $content = $_REQUEST['content'];
696 $replyto = strip_tags($_SESSION['email_replyto']);
697 $fromname = strip_tags($_SESSION['email_fromname']);
699 $mail = new PHPMailer();
701 $mail->PluginDir
= "lib/phpmailer/";
702 $mail->SetLanguage("en", "lib/phpmailer/language/");
704 $mail->CharSet
= "UTF-8";
706 $mail->From
= $replyto;
707 $mail->FromName
= $fromname;
708 $mail->AddAddress($destination);
710 if (DIGEST_SMTP_HOST
) {
711 $mail->Host
= DIGEST_SMTP_HOST
;
712 $mail->Mailer
= "smtp";
713 $mail->SMTPAuth
= DIGEST_SMTP_LOGIN
!= '';
714 $mail->Username
= DIGEST_SMTP_LOGIN
;
715 $mail->Password
= DIGEST_SMTP_PASSWORD
;
718 $mail->IsHTML(false);
719 $mail->Subject
= $subject;
720 $mail->Body
= $content;
725 $reply['error'] = $mail->ErrorInfo
;
727 save_email_address($link, db_escape_string($destination));
728 $reply['message'] = "UPDATE_COUNTERS";
732 $reply['error'] = "Not authorized.";
735 print json_encode($reply);
739 if ($subop == "completeEmails") {
740 header("Content-Type: text/plain");
742 $search = db_escape_string($_REQUEST["search"]);
746 foreach ($_SESSION['stored_emails'] as $email) {
747 if (strpos($email, $search) !== false) {
748 print "<li>$email</li>";
757 if ($subop == "quickAddCat") {
758 header("Content-Type: text/plain");
760 $cat = db_escape_string($_REQUEST["cat"]);
762 add_feed_category($link, $cat);
764 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
765 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
767 if (db_num_rows($result) == 1) {
768 $id = db_fetch_result($result, 0, "id");
773 print_feed_cat_select($link, "cat_id", $id);
778 if ($subop == "regenFeedKey") {
779 header("Content-Type: text/plain");
781 $feed_id = db_escape_string($_REQUEST['id']);
782 $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
784 $new_key = update_feed_access_key($link, $feed_id, $is_cat);
786 print json_encode(array("link" => $new_key));
791 if ($subop == "clearKeys") {
792 db_query($link, "DELETE FROM ttrss_access_keys WHERE
793 owner_uid = " . $_SESSION["uid"]);
798 if ($subop == "verifyRegexp") {
799 header("Content-Type: text/plain");
801 $reg_exp = $_REQUEST["reg_exp"];
803 $status = @preg_match
("/$reg_exp/i", "TEST") !== false;
805 print json_encode(array("status" => $status));
809 // TODO: unify with digest-get-contents?
810 if ($subop == "cdmGetArticle") {
811 header("Content-Type: text/plain");
813 $id = db_escape_string($_REQUEST["id"]);
815 $result = db_query($link, "SELECT content,
816 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
818 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
819 ttrss_entries.id = ref_id AND
820 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
822 if (db_num_rows($result) != 0) {
823 $line = db_fetch_assoc($result);
825 $article_content = sanitize_rss($link, $line["content"],
826 false, false, $line['site_url']);
829 $article_content = '';
832 print json_encode(array("article" =>
833 array("id" => $id, "content" => $article_content)));
838 if ($subop == "scheduleFeedUpdate") {
839 header("Content-Type: text/plain");
841 $feed_id = db_escape_string($_REQUEST["id"]);
842 $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
844 $message = __("Your request could not be completed.");
848 $message = __("Feed update has been scheduled.");
850 db_query($link, "UPDATE ttrss_feeds SET
851 last_update_started = '1970-01-01',
852 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
853 owner_uid = ".$_SESSION["uid"]);
856 $message = __("Category update has been scheduled.");
859 $cat_query = "cat_id = '$feed_id'";
861 $cat_query = "cat_id IS NULL";
863 db_query($link, "UPDATE ttrss_feeds SET
864 last_update_started = '1970-01-01',
865 last_updated = '1970-01-01' WHERE $cat_query AND
866 owner_uid = ".$_SESSION["uid"]);
869 $message = __("Can't update this kind of feed.");
872 print json_encode(array("message" => $message));
876 if ($subop == "getTweetInfo") {
877 header("Content-Type: text/plain");
878 $id = db_escape_string($_REQUEST['id']);
880 $result = db_query($link, "SELECT title, link
881 FROM ttrss_entries, ttrss_user_entries
882 WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
884 if (db_num_rows($result) != 0) {
885 $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
887 $article_link = db_fetch_result($result, 0, 'link');
890 print json_encode(array("title" => $title, "link" => $article_link,
896 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";