]>
git.wh0rd.org - tt-rss.git/blob - classes/rpc.php
db34a6c9d4a78bf70502a2842ee3048d473fd4fa
2 class RPC
extends Handler_Protected
{
4 function csrf_ignore($method) {
5 $csrf_ignored = array("sanitycheck", "buttonplugin", "exportget");
7 return array_search($method, $csrf_ignored) !== false;
10 function setprofile() {
11 $id = db_escape_string($_REQUEST["id"]);
13 $_SESSION["profile"] = $id;
14 $_SESSION["prefs_cache"] = array();
17 function exportget() {
18 $exportname = CACHE_DIR
. "/export/" .
19 sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
21 if (file_exists($exportname)) {
22 header("Content-type: text/xml");
24 if (function_exists('gzencode')) {
25 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz");
26 echo gzencode(file_get_contents($exportname));
28 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml");
29 echo file_get_contents($exportname);
32 echo "File not found.";
36 function exportrun() {
37 $offset = (int) db_escape_string($_REQUEST['offset']);
41 if ($offset < 10000 && is_writable(CACHE_DIR
. "/export")) {
42 $result = db_query($this->link
, "SELECT
53 ttrss_feeds.title AS feed_title,
54 ttrss_feeds.feed_url AS feed_url,
57 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
60 (marked = true OR feed_id IS NULL) AND
61 ref_id = ttrss_entries.id AND
62 ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . "
63 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
65 $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
68 $fp = fopen(CACHE_DIR
. "/export/$exportname.xml", "w");
69 fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION
."\">");
71 $fp = fopen(CACHE_DIR
. "/export/$exportname.xml", "a");
76 while ($line = db_fetch_assoc($result)) {
77 fputs($fp, "<article>");
79 foreach ($line as $k => $v) {
80 fputs($fp, "<$k><![CDATA[$v]]></$k>");
83 fputs($fp, "</article>");
86 $exported = db_num_rows($result);
88 if ($exported < $limit && $exported > 0) {
89 fputs($fp, "</articles>");
97 print json_encode(array("exported" => $exported));
100 function remprofiles() {
101 $ids = explode(",", db_escape_string(trim($_REQUEST["ids"])));
103 foreach ($ids as $id) {
104 if ($_SESSION["profile"] != $id) {
105 db_query($this->link
, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
106 owner_uid = " . $_SESSION["uid"]);
112 function addprofile() {
113 $title = db_escape_string(trim($_REQUEST["title"]));
115 db_query($this->link
, "BEGIN");
117 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles
118 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
120 if (db_num_rows($result) == 0) {
122 db_query($this->link
, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
123 VALUES ('$title', ".$_SESSION["uid"] .")");
125 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles WHERE
128 if (db_num_rows($result) != 0) {
129 $profile_id = db_fetch_result($result, 0, "id");
132 initialize_user_prefs($this->link
, $_SESSION["uid"], $profile_id);
137 db_query($this->link
, "COMMIT");
142 function saveprofile() {
143 $id = db_escape_string($_REQUEST["id"]);
144 $title = db_escape_string(trim($_REQUEST["value"]));
147 print __("Default profile");
152 db_query($this->link
, "BEGIN");
154 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles
155 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
157 if (db_num_rows($result) == 0) {
158 db_query($this->link
, "UPDATE ttrss_settings_profiles
159 SET title = '$title' WHERE id = '$id' AND
160 owner_uid = " . $_SESSION["uid"]);
163 $result = db_query($this->link
, "SELECT title FROM ttrss_settings_profiles
164 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
165 print db_fetch_result($result, 0, "title");
168 db_query($this->link
, "COMMIT");
173 function remarchive() {
174 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
176 foreach ($ids as $id) {
177 $result = db_query($this->link
, "DELETE FROM ttrss_archived_feeds WHERE
178 (SELECT COUNT(*) FROM ttrss_user_entries
179 WHERE orig_feed_id = '$id') = 0 AND
180 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
182 $rc = db_affected_rows($this->link
, $result);
187 $feed = db_escape_string($_REQUEST['feed']);
188 $cat = db_escape_string($_REQUEST['cat']);
189 $login = db_escape_string($_REQUEST['login']);
190 $pass = db_escape_string($_REQUEST['pass']);
191 $need_auth = db_escape_string($_REQUEST['need_auth']) != "";
193 $rc = subscribe_to_feed($this->link
, $feed, $cat, $login, $pass, $need_auth);
195 print json_encode(array("result" => $rc));
198 function extractfeedurls() {
199 $urls = get_feeds_from_html($_REQUEST['url']);
201 print json_encode(array("urls" => $urls));
204 function togglepref() {
205 $key = db_escape_string($_REQUEST["key"]);
206 set_pref($this->link
, $key, !get_pref($this->link
, $key));
207 $value = get_pref($this->link
, $key);
209 print json_encode(array("param" =>$key, "value" => $value));
213 // set_pref escapes input, so no need to double escape it here
214 $key = $_REQUEST['key'];
215 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
217 set_pref($this->link
, $key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET');
219 print json_encode(array("param" =>$key, "value" => $value));
223 $mark = $_REQUEST["mark"];
224 $id = db_escape_string($_REQUEST["id"]);
232 $result = db_query($this->link
, "UPDATE ttrss_user_entries SET marked = $mark
233 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
235 print json_encode(array("message" => "UPDATE_COUNTERS"));
239 $ids = db_escape_string($_REQUEST["ids"]);
241 $result = db_query($this->link
, "DELETE FROM ttrss_user_entries
242 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
244 print json_encode(array("message" => "UPDATE_COUNTERS"));
247 function unarchive() {
248 $ids = db_escape_string($_REQUEST["ids"]);
250 $result = db_query($this->link
, "UPDATE ttrss_user_entries
251 SET feed_id = orig_feed_id, orig_feed_id = NULL
252 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
254 print json_encode(array("message" => "UPDATE_COUNTERS"));
258 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
260 foreach ($ids as $id) {
261 archive_article($this->link
, $id, $_SESSION["uid"]);
264 print json_encode(array("message" => "UPDATE_COUNTERS"));
268 $pub = $_REQUEST["pub"];
269 $id = db_escape_string($_REQUEST["id"]);
270 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
278 $result = db_query($this->link
, "UPDATE ttrss_user_entries SET
280 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
282 $pubsub_result = false;
284 if (PUBSUBHUBBUB_HUB
) {
285 $rss_link = get_self_url_prefix() .
286 "/public.php?op=rss&id=-2&key=" .
287 get_feed_access_key($this->link
, -2, false);
289 $p = new Publisher(PUBSUBHUBBUB_HUB
);
291 $pubsub_result = $p->publish_update($rss_link);
294 print json_encode(array("message" => "UPDATE_COUNTERS",
295 "pubsub_result" => $pubsub_result));
298 function getAllCounters() {
299 $last_article_id = (int) $_REQUEST["last_article_id"];
303 if ($seq) $reply['seq'] = $seq;
305 if ($last_article_id != getLastArticleId($this->link
)) {
306 $omode = $_REQUEST["omode"];
309 $reply['counters'] = getAllCounters($this->link
, $omode);
311 $reply['counters'] = getGlobalCounters($this->link
);
314 $reply['runtime-info'] = make_runtime_info($this->link
);
316 print json_encode($reply);
319 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
320 function catchupSelected() {
321 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
322 $cmode = sprintf("%d", $_REQUEST["cmode"]);
324 catchupArticlesById($this->link
, $ids, $cmode);
326 print json_encode(array("message" => "UPDATE_COUNTERS"));
329 function markSelected() {
330 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
331 $cmode = sprintf("%d", $_REQUEST["cmode"]);
333 markArticlesById($this->link
, $ids, $cmode);
335 print json_encode(array("message" => "UPDATE_COUNTERS"));
338 function publishSelected() {
339 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
340 $cmode = sprintf("%d", $_REQUEST["cmode"]);
342 publishArticlesById($this->link
, $ids, $cmode);
344 print json_encode(array("message" => "UPDATE_COUNTERS"));
347 function sanityCheck() {
348 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
352 $reply['error'] = sanity_check($this->link
);
354 if ($reply['error']['code'] == 0) {
355 $reply['init-params'] = make_init_params($this->link
);
356 $reply['runtime-info'] = make_runtime_info($this->link
);
359 print json_encode($reply);
362 function setArticleTags() {
364 $id = db_escape_string($_REQUEST["id"]);
366 $tags_str = db_escape_string($_REQUEST["tags_str"]);
367 $tags = array_unique(trim_array(explode(",", $tags_str)));
369 db_query($this->link
, "BEGIN");
371 $result = db_query($this->link
, "SELECT int_id FROM ttrss_user_entries WHERE
372 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
374 if (db_num_rows($result) == 1) {
376 $tags_to_cache = array();
378 $int_id = db_fetch_result($result, 0, "int_id");
380 db_query($this->link
, "DELETE FROM ttrss_tags WHERE
381 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
383 foreach ($tags as $tag) {
384 $tag = sanitize_tag($tag);
386 if (!tag_is_valid($tag)) {
390 if (preg_match("/^[0-9]*$/", $tag)) {
394 // print "<!-- $id : $int_id : $tag -->";
397 db_query($this->link
, "INSERT INTO ttrss_tags
398 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
401 array_push($tags_to_cache, $tag);
404 /* update tag cache */
406 sort($tags_to_cache);
407 $tags_str = join(",", $tags_to_cache);
409 db_query($this->link
, "UPDATE ttrss_user_entries
410 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
411 AND owner_uid = " . $_SESSION["uid"]);
414 db_query($this->link
, "COMMIT");
416 $tags = get_article_tags($this->link
, $id);
417 $tags_str = format_tags_string($tags, $id);
418 $tags_str_full = join(", ", $tags);
420 if (!$tags_str_full) $tags_str_full = __("no tags");
422 print json_encode(array("tags_str" => array("id" => $id,
423 "content" => $tags_str, "content_full" => $tags_str_full)));
426 function regenOPMLKey() {
427 update_feed_access_key($this->link
, 'OPML:Publish',
428 false, $_SESSION["uid"]);
430 $new_link = opml_publish_url($this->link
);
432 print json_encode(array("link" => $new_link));
435 function completeTags() {
436 $search = db_escape_string($_REQUEST["search"]);
438 $result = db_query($this->link
, "SELECT DISTINCT tag_name FROM ttrss_tags
439 WHERE owner_uid = '".$_SESSION["uid"]."' AND
440 tag_name LIKE '$search%' ORDER BY tag_name
444 while ($line = db_fetch_assoc($result)) {
445 print "<li>" . $line["tag_name"] . "</li>";
451 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
452 $days = sprintf("%d", $_REQUEST["days"]);
454 foreach ($ids as $id) {
456 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
457 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
459 if (db_num_rows($result) == 1) {
460 purge_feed($this->link
, $id, $days);
465 function getArticles() {
466 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
469 foreach ($ids as $id) {
471 array_push($articles, format_article($this->link
, $id, 0, false));
475 print json_encode($articles);
478 function checkDate() {
479 $date = db_escape_string($_REQUEST["date"]);
480 $date_parsed = strtotime($date);
482 print json_encode(array("result" => (bool)$date_parsed,
483 "date" => date("c", $date_parsed)));
486 function assigntolabel() {
487 return $this->labelops(true);
490 function removefromlabel() {
491 return $this->labelops(false);
494 function labelops($assign) {
497 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
498 $label_id = db_escape_string($_REQUEST["lid"]);
500 $label = db_escape_string(label_find_caption($this->link
, $label_id,
503 $reply["info-for-headlines"] = array();
507 foreach ($ids as $id) {
510 label_add_article($this->link
, $id, $label, $_SESSION["uid"]);
512 label_remove_article($this->link
, $id, $label, $_SESSION["uid"]);
514 $labels = get_article_labels($this->link
, $id, $_SESSION["uid"]);
516 array_push($reply["info-for-headlines"],
517 array("id" => $id, "labels" => format_article_labels($labels, $id)));
522 $reply["message"] = "UPDATE_COUNTERS";
524 print json_encode($reply);
527 function updateFeedBrowser() {
528 $search = db_escape_string($_REQUEST["search"]);
529 $limit = db_escape_string($_REQUEST["limit"]);
530 $mode = (int) db_escape_string($_REQUEST["mode"]);
532 print json_encode(array("content" =>
533 make_feed_browser($this->link
, $search, $limit, $mode),
538 function massSubscribe() {
540 $payload = json_decode($_REQUEST["payload"], false);
541 $mode = $_REQUEST["mode"];
543 if (!$payload ||
!is_array($payload)) return;
546 foreach ($payload as $feed) {
548 $title = db_escape_string($feed[0]);
549 $feed_url = db_escape_string($feed[1]);
551 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
552 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
554 if (db_num_rows($result) == 0) {
555 $result = db_query($this->link
, "INSERT INTO ttrss_feeds
556 (owner_uid,feed_url,title,cat_id,site_url)
557 VALUES ('".$_SESSION["uid"]."',
558 '$feed_url', '$title', NULL, '')");
561 } else if ($mode == 2) {
563 foreach ($payload as $id) {
564 $result = db_query($this->link
, "SELECT * FROM ttrss_archived_feeds
565 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
567 if (db_num_rows($result) != 0) {
568 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
569 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
570 $title = db_escape_string(db_fetch_result($result, 0, "title"));
572 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
573 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
575 if (db_num_rows($result) == 0) {
576 $result = db_query($this->link
, "INSERT INTO ttrss_feeds
577 (owner_uid,feed_url,title,cat_id,site_url)
578 VALUES ('$id','".$_SESSION["uid"]."',
579 '$feed_url', '$title', NULL, '$site_url')");
586 function digestgetcontents() {
587 $article_id = db_escape_string($_REQUEST['article_id']);
589 $result = db_query($this->link
, "SELECT content,title,link,marked,published
590 FROM ttrss_entries, ttrss_user_entries
591 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
593 $content = sanitize($this->link
, db_fetch_result($result, 0, "content"));
594 $title = strip_tags(db_fetch_result($result, 0, "title"));
595 $article_url = htmlspecialchars(db_fetch_result($result, 0, "link"));
596 $marked = sql_bool_to_bool(db_fetch_result($result, 0, "marked"));
597 $published = sql_bool_to_bool(db_fetch_result($result, 0, "published"));
599 print json_encode(array("article" =>
600 array("id" => $article_id, "url" => $article_url,
601 "tags" => get_article_tags($this->link
, $article_id),
602 "marked" => $marked, "published" => $published,
603 "title" => $title, "content" => $content)));
606 function digestupdate() {
607 $feed_id = db_escape_string($_REQUEST['feed_id']);
608 $offset = db_escape_string($_REQUEST['offset']);
609 $seq = db_escape_string($_REQUEST['seq']);
611 if (!$feed_id) $feed_id = -4;
612 if (!$offset) $offset = 0;
616 $reply['seq'] = $seq;
618 $headlines = api_get_headlines($this->link
, $feed_id, 30, $offset,
619 '', ($feed_id == -4), true, false, "unread", "updated DESC", 0, 0);
621 $reply['headlines'] = array();
622 $reply['headlines']['title'] = getFeedTitle($this->link
, $feed_id);
623 $reply['headlines']['content'] = $headlines;
625 print json_encode($reply);
628 function digestinit() {
629 $tmp_feeds = api_get_feeds($this->link
, -4, true, false, 0);
633 foreach ($tmp_feeds as $f) {
634 if ($f['id'] > 0 ||
$f['id'] == -4) array_push($feeds, $f);
637 print json_encode(array("feeds" => $feeds));
640 function catchupFeed() {
641 $feed_id = db_escape_string($_REQUEST['feed_id']);
642 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
643 $max_id = (int) db_escape_string($_REQUEST['max_id']);
645 catchup_feed($this->link
, $feed_id, $is_cat, false, $max_id);
647 print json_encode(array("message" => "UPDATE_COUNTERS"));
650 function quickAddCat() {
651 $cat = db_escape_string($_REQUEST["cat"]);
653 add_feed_category($this->link
, $cat);
655 $result = db_query($this->link
, "SELECT id FROM ttrss_feed_categories WHERE
656 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
658 if (db_num_rows($result) == 1) {
659 $id = db_fetch_result($result, 0, "id");
664 print_feed_cat_select($this->link
, "cat_id", $id);
667 function regenFeedKey() {
668 $feed_id = db_escape_string($_REQUEST['id']);
669 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
671 $new_key = update_feed_access_key($this->link
, $feed_id, $is_cat);
673 print json_encode(array("link" => $new_key));
677 function clearKeys() {
678 db_query($this->link
, "DELETE FROM ttrss_access_keys WHERE
679 owner_uid = " . $_SESSION["uid"]);
683 function clearArticleKeys() {
684 db_query($this->link
, "UPDATE ttrss_user_entries SET uuid = '' WHERE
685 owner_uid = " . $_SESSION["uid"]);
691 function verifyRegexp() {
692 $reg_exp = $_REQUEST["reg_exp"];
694 $status = @preg_match
("/$reg_exp/i", "TEST") !== false;
696 print json_encode(array("status" => $status));
699 // TODO: unify with digest-get-contents?
700 function cdmGetArticle() {
701 $ids = array(db_escape_string($_REQUEST["id"]));
702 $cids = explode(",", $_REQUEST["cids"]);
704 $ids = array_merge($ids, $cids);
708 foreach ($ids as $id) {
711 $result = db_query($this->link
, "SELECT content,
712 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
714 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
715 ttrss_entries.id = ref_id AND
716 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
718 if (db_num_rows($result) != 0) {
719 $line = db_fetch_assoc($result);
721 $article_content = sanitize($this->link
, $line["content"],
722 false, false, $line['site_url']);
725 array("id" => $id, "content" => $article_content));
729 print json_encode($rv);
732 function scheduleFeedUpdate() {
733 $feed_id = db_escape_string($_REQUEST["id"]);
734 $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
736 $message = __("Your request could not be completed.");
740 $message = __("Feed update has been scheduled.");
742 db_query($this->link
, "UPDATE ttrss_feeds SET
743 last_update_started = '1970-01-01',
744 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
745 owner_uid = ".$_SESSION["uid"]);
748 $message = __("Category update has been scheduled.");
751 $cat_query = "cat_id = '$feed_id'";
753 $cat_query = "cat_id IS NULL";
755 db_query($this->link
, "UPDATE ttrss_feeds SET
756 last_update_started = '1970-01-01',
757 last_updated = '1970-01-01' WHERE $cat_query AND
758 owner_uid = ".$_SESSION["uid"]);
761 $message = __("Can't update this kind of feed.");
764 print json_encode(array("message" => $message));
768 function buttonPlugin() {
769 $pclass = "button_" . basename($_REQUEST['plugin']);
770 $method = $_REQUEST['plugin_method'];
772 if (class_exists($pclass)) {
773 $plugin = new $pclass($this->link
);
774 if (method_exists($plugin, $method)) {
775 return $plugin->$method();
781 $hash = sha1(uniqid(rand(), true));
783 print json_encode(array("hash" => $hash));
786 function batchAddFeeds() {
787 $cat_id = db_escape_string($_REQUEST['cat']);
788 $feeds = explode("\n", db_escape_string($_REQUEST['feeds']));
789 $login = db_escape_string($_REQUEST['login']);
790 $pass = db_escape_string($_REQUEST['pass']);
791 $need_auth = db_escape_string($_REQUEST['need_auth']) != "";
793 foreach ($feeds as $feed) {
796 if (validate_feed_url($feed)) {
798 db_query($this->link
, "BEGIN");
802 if ($cat_id == "0" ||
!$cat_id) {
805 $cat_qpart = "'$cat_id'";
808 $result = db_query($this->link
,
809 "SELECT id FROM ttrss_feeds
810 WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]);
812 if (db_num_rows($result) == 0) {
813 $result = db_query($this->link
,
814 "INSERT INTO ttrss_feeds
815 (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method)
816 VALUES ('".$_SESSION["uid"]."', '$feed',
817 '[Unknown]', $cat_qpart, '$login', '$pass', '$update_method')");
820 db_query($this->link
, "COMMIT");