]>
git.wh0rd.org - tt-rss.git/blob - classes/rpc.php
33e500edfe72eb12d6e94d0ab6dd31c86b1c7622
2 class RPC
extends Handler_Protected
{
4 function csrf_ignore($method) {
5 $csrf_ignored = array("sanitycheck", "completelabels");
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 remprofiles() {
18 $ids = explode(",", db_escape_string(trim($_REQUEST["ids"])));
20 foreach ($ids as $id) {
21 if ($_SESSION["profile"] != $id) {
22 db_query($this->link
, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
23 owner_uid = " . $_SESSION["uid"]);
29 function addprofile() {
30 $title = db_escape_string(trim($_REQUEST["title"]));
32 db_query($this->link
, "BEGIN");
34 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles
35 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
37 if (db_num_rows($result) == 0) {
39 db_query($this->link
, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
40 VALUES ('$title', ".$_SESSION["uid"] .")");
42 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles WHERE
45 if (db_num_rows($result) != 0) {
46 $profile_id = db_fetch_result($result, 0, "id");
49 initialize_user_prefs($this->link
, $_SESSION["uid"], $profile_id);
54 db_query($this->link
, "COMMIT");
59 function saveprofile() {
60 $id = db_escape_string($_REQUEST["id"]);
61 $title = db_escape_string(trim($_REQUEST["value"]));
64 print __("Default profile");
69 db_query($this->link
, "BEGIN");
71 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles
72 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
74 if (db_num_rows($result) == 0) {
75 db_query($this->link
, "UPDATE ttrss_settings_profiles
76 SET title = '$title' WHERE id = '$id' AND
77 owner_uid = " . $_SESSION["uid"]);
80 $result = db_query($this->link
, "SELECT title FROM ttrss_settings_profiles
81 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
82 print db_fetch_result($result, 0, "title");
85 db_query($this->link
, "COMMIT");
90 function remarchive() {
91 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
93 foreach ($ids as $id) {
94 $result = db_query($this->link
, "DELETE FROM ttrss_archived_feeds WHERE
95 (SELECT COUNT(*) FROM ttrss_user_entries
96 WHERE orig_feed_id = '$id') = 0 AND
97 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
99 $rc = db_affected_rows($this->link
, $result);
104 $feed = db_escape_string($_REQUEST['feed']);
105 $cat = db_escape_string($_REQUEST['cat']);
106 $login = db_escape_string($_REQUEST['login']);
107 $pass = db_escape_string($_REQUEST['pass']);
108 $need_auth = db_escape_string($_REQUEST['need_auth']) != "";
110 $rc = subscribe_to_feed($this->link
, $feed, $cat, $login, $pass, $need_auth);
112 print json_encode(array("result" => $rc));
115 function togglepref() {
116 $key = db_escape_string($_REQUEST["key"]);
117 set_pref($this->link
, $key, !get_pref($this->link
, $key));
118 $value = get_pref($this->link
, $key);
120 print json_encode(array("param" =>$key, "value" => $value));
124 // set_pref escapes input, so no need to double escape it here
125 $key = $_REQUEST['key'];
126 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
128 set_pref($this->link
, $key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET');
130 print json_encode(array("param" =>$key, "value" => $value));
134 $mark = $_REQUEST["mark"];
135 $id = db_escape_string($_REQUEST["id"]);
143 $result = db_query($this->link
, "UPDATE ttrss_user_entries SET marked = $mark
144 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
146 print json_encode(array("message" => "UPDATE_COUNTERS"));
150 $ids = db_escape_string($_REQUEST["ids"]);
152 $result = db_query($this->link
, "DELETE FROM ttrss_user_entries
153 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
155 print json_encode(array("message" => "UPDATE_COUNTERS"));
158 function unarchive() {
159 $ids = db_escape_string($_REQUEST["ids"]);
161 $result = db_query($this->link
, "UPDATE ttrss_user_entries
162 SET feed_id = orig_feed_id, orig_feed_id = NULL
163 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
165 print json_encode(array("message" => "UPDATE_COUNTERS"));
169 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
171 foreach ($ids as $id) {
172 archive_article($this->link
, $id, $_SESSION["uid"]);
175 print json_encode(array("message" => "UPDATE_COUNTERS"));
179 $pub = $_REQUEST["pub"];
180 $id = db_escape_string($_REQUEST["id"]);
181 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
189 $result = db_query($this->link
, "UPDATE ttrss_user_entries SET
190 published = $pub, last_read = NOW()
191 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
193 $pubsub_result = false;
195 if (PUBSUBHUBBUB_HUB
) {
196 $rss_link = get_self_url_prefix() .
197 "/public.php?op=rss&id=-2&key=" .
198 get_feed_access_key($this->link
, -2, false);
200 $p = new Publisher(PUBSUBHUBBUB_HUB
);
202 $pubsub_result = $p->publish_update($rss_link);
205 print json_encode(array("message" => "UPDATE_COUNTERS",
206 "pubsub_result" => $pubsub_result));
209 function getAllCounters() {
210 $last_article_id = (int) $_REQUEST["last_article_id"];
214 if ($seq) $reply['seq'] = $seq;
216 if ($last_article_id != getLastArticleId($this->link
)) {
217 $omode = $_REQUEST["omode"];
220 $reply['counters'] = getAllCounters($this->link
, $omode);
222 $reply['counters'] = getGlobalCounters($this->link
);
225 $reply['runtime-info'] = make_runtime_info($this->link
);
227 print json_encode($reply);
230 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
231 function catchupSelected() {
232 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
233 $cmode = sprintf("%d", $_REQUEST["cmode"]);
235 catchupArticlesById($this->link
, $ids, $cmode);
237 print json_encode(array("message" => "UPDATE_COUNTERS"));
240 function markSelected() {
241 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
242 $cmode = sprintf("%d", $_REQUEST["cmode"]);
244 markArticlesById($this->link
, $ids, $cmode);
246 print json_encode(array("message" => "UPDATE_COUNTERS"));
249 function publishSelected() {
250 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
251 $cmode = sprintf("%d", $_REQUEST["cmode"]);
253 publishArticlesById($this->link
, $ids, $cmode);
255 print json_encode(array("message" => "UPDATE_COUNTERS"));
258 function sanityCheck() {
259 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
263 $reply['error'] = sanity_check($this->link
);
265 if ($reply['error']['code'] == 0) {
266 $reply['init-params'] = make_init_params($this->link
);
267 $reply['runtime-info'] = make_runtime_info($this->link
);
270 print json_encode($reply);
273 function setArticleTags() {
275 $id = db_escape_string($_REQUEST["id"]);
277 $tags_str = db_escape_string($_REQUEST["tags_str"]);
278 $tags = array_unique(trim_array(explode(",", $tags_str)));
280 db_query($this->link
, "BEGIN");
282 $result = db_query($this->link
, "SELECT int_id FROM ttrss_user_entries WHERE
283 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
285 if (db_num_rows($result) == 1) {
287 $tags_to_cache = array();
289 $int_id = db_fetch_result($result, 0, "int_id");
291 db_query($this->link
, "DELETE FROM ttrss_tags WHERE
292 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
294 foreach ($tags as $tag) {
295 $tag = sanitize_tag($tag);
297 if (!tag_is_valid($tag)) {
301 if (preg_match("/^[0-9]*$/", $tag)) {
305 // print "<!-- $id : $int_id : $tag -->";
308 db_query($this->link
, "INSERT INTO ttrss_tags
309 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
312 array_push($tags_to_cache, $tag);
315 /* update tag cache */
317 sort($tags_to_cache);
318 $tags_str = join(",", $tags_to_cache);
320 db_query($this->link
, "UPDATE ttrss_user_entries
321 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
322 AND owner_uid = " . $_SESSION["uid"]);
325 db_query($this->link
, "COMMIT");
327 $tags = get_article_tags($this->link
, $id);
328 $tags_str = format_tags_string($tags, $id);
329 $tags_str_full = join(", ", $tags);
331 if (!$tags_str_full) $tags_str_full = __("no tags");
333 print json_encode(array("tags_str" => array("id" => $id,
334 "content" => $tags_str, "content_full" => $tags_str_full)));
337 function regenOPMLKey() {
338 update_feed_access_key($this->link
, 'OPML:Publish',
339 false, $_SESSION["uid"]);
341 $new_link = opml_publish_url($this->link
);
343 print json_encode(array("link" => $new_link));
346 function completeLabels() {
347 $search = db_escape_string($_REQUEST["search"]);
349 $result = db_query($this->link
, "SELECT DISTINCT caption FROM
351 WHERE owner_uid = '".$_SESSION["uid"]."' AND
352 LOWER(caption) LIKE LOWER('$search%') ORDER BY caption
356 while ($line = db_fetch_assoc($result)) {
357 print "<li>" . $line["caption"] . "</li>";
363 function completeTags() {
364 $search = db_escape_string($_REQUEST["search"]);
366 $result = db_query($this->link
, "SELECT DISTINCT tag_name FROM ttrss_tags
367 WHERE owner_uid = '".$_SESSION["uid"]."' AND
368 tag_name LIKE '$search%' ORDER BY tag_name
372 while ($line = db_fetch_assoc($result)) {
373 print "<li>" . $line["tag_name"] . "</li>";
379 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
380 $days = sprintf("%d", $_REQUEST["days"]);
382 foreach ($ids as $id) {
384 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
385 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
387 if (db_num_rows($result) == 1) {
388 purge_feed($this->link
, $id, $days);
393 function getArticles() {
394 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
397 foreach ($ids as $id) {
399 array_push($articles, format_article($this->link
, $id, 0, false));
403 print json_encode($articles);
406 function checkDate() {
407 $date = db_escape_string($_REQUEST["date"]);
408 $date_parsed = strtotime($date);
410 print json_encode(array("result" => (bool)$date_parsed,
411 "date" => date("c", $date_parsed)));
414 function assigntolabel() {
415 return $this->labelops(true);
418 function removefromlabel() {
419 return $this->labelops(false);
422 function labelops($assign) {
425 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
426 $label_id = db_escape_string($_REQUEST["lid"]);
428 $label = db_escape_string(label_find_caption($this->link
, $label_id,
431 $reply["info-for-headlines"] = array();
435 foreach ($ids as $id) {
438 label_add_article($this->link
, $id, $label, $_SESSION["uid"]);
440 label_remove_article($this->link
, $id, $label, $_SESSION["uid"]);
442 $labels = get_article_labels($this->link
, $id, $_SESSION["uid"]);
444 array_push($reply["info-for-headlines"],
445 array("id" => $id, "labels" => format_article_labels($labels, $id)));
450 $reply["message"] = "UPDATE_COUNTERS";
452 print json_encode($reply);
455 function updateFeedBrowser() {
456 $search = db_escape_string($_REQUEST["search"]);
457 $limit = db_escape_string($_REQUEST["limit"]);
458 $mode = (int) db_escape_string($_REQUEST["mode"]);
460 require_once "feedbrowser.php";
462 print json_encode(array("content" =>
463 make_feed_browser($this->link
, $search, $limit, $mode),
468 function massSubscribe() {
470 $payload = json_decode($_REQUEST["payload"], false);
471 $mode = $_REQUEST["mode"];
473 if (!$payload ||
!is_array($payload)) return;
476 foreach ($payload as $feed) {
478 $title = db_escape_string($feed[0]);
479 $feed_url = db_escape_string($feed[1]);
481 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
482 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
484 if (db_num_rows($result) == 0) {
485 $result = db_query($this->link
, "INSERT INTO ttrss_feeds
486 (owner_uid,feed_url,title,cat_id,site_url)
487 VALUES ('".$_SESSION["uid"]."',
488 '$feed_url', '$title', NULL, '')");
491 } else if ($mode == 2) {
493 foreach ($payload as $id) {
494 $result = db_query($this->link
, "SELECT * FROM ttrss_archived_feeds
495 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
497 if (db_num_rows($result) != 0) {
498 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
499 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
500 $title = db_escape_string(db_fetch_result($result, 0, "title"));
502 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
503 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
505 if (db_num_rows($result) == 0) {
506 $result = db_query($this->link
, "INSERT INTO ttrss_feeds
507 (owner_uid,feed_url,title,cat_id,site_url)
508 VALUES ('$id','".$_SESSION["uid"]."',
509 '$feed_url', '$title', NULL, '$site_url')");
516 function catchupFeed() {
517 $feed_id = db_escape_string($_REQUEST['feed_id']);
518 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
519 $max_id = (int) db_escape_string($_REQUEST['max_id']);
521 catchup_feed($this->link
, $feed_id, $is_cat, false, $max_id);
523 print json_encode(array("message" => "UPDATE_COUNTERS"));
526 function quickAddCat() {
527 $cat = db_escape_string($_REQUEST["cat"]);
529 add_feed_category($this->link
, $cat);
531 $result = db_query($this->link
, "SELECT id FROM ttrss_feed_categories WHERE
532 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
534 if (db_num_rows($result) == 1) {
535 $id = db_fetch_result($result, 0, "id");
540 print_feed_cat_select($this->link
, "cat_id", $id);
543 function regenFeedKey() {
544 $feed_id = db_escape_string($_REQUEST['id']);
545 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
547 $new_key = update_feed_access_key($this->link
, $feed_id, $is_cat);
549 print json_encode(array("link" => $new_key));
553 function clearKeys() {
554 db_query($this->link
, "DELETE FROM ttrss_access_keys WHERE
555 owner_uid = " . $_SESSION["uid"]);
559 function clearArticleKeys() {
560 db_query($this->link
, "UPDATE ttrss_user_entries SET uuid = '' WHERE
561 owner_uid = " . $_SESSION["uid"]);
566 function verifyRegexp() {
567 $reg_exp = $_REQUEST["reg_exp"];
569 $status = @preg_match
("/$reg_exp/i", "TEST") !== false;
571 print json_encode(array("status" => $status));
574 /* function buttonPlugin() {
575 $pclass = "button_" . basename($_REQUEST['plugin']);
576 $method = $_REQUEST['plugin_method'];
578 if (class_exists($pclass)) {
579 $plugin = new $pclass($this->link);
580 if (method_exists($plugin, $method)) {
581 return $plugin->$method();
587 $hash = sha1(uniqid(rand(), true));
589 print json_encode(array("hash" => $hash));
592 function batchAddFeeds() {
593 $cat_id = db_escape_string($_REQUEST['cat']);
594 $feeds = explode("\n", db_escape_string($_REQUEST['feeds']));
595 $login = db_escape_string($_REQUEST['login']);
596 $pass = db_escape_string($_REQUEST['pass']);
597 $need_auth = db_escape_string($_REQUEST['need_auth']) != "";
599 foreach ($feeds as $feed) {
602 if (validate_feed_url($feed)) {
604 db_query($this->link
, "BEGIN");
606 if ($cat_id == "0" ||
!$cat_id) {
609 $cat_qpart = "'$cat_id'";
612 $result = db_query($this->link
,
613 "SELECT id FROM ttrss_feeds
614 WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]);
616 if (db_num_rows($result) == 0) {
617 $result = db_query($this->link
,
618 "INSERT INTO ttrss_feeds
619 (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method)
620 VALUES ('".$_SESSION["uid"]."', '$feed',
621 '[Unknown]', $cat_qpart, '$login', '$pass', 0)");
624 db_query($this->link
, "COMMIT");
629 function setScore() {
630 $ids = db_escape_string($_REQUEST['id']);
631 $score = (int)db_escape_string($_REQUEST['score']);
633 db_query($this->link
, "UPDATE ttrss_user_entries SET
634 score = '$score' WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
636 print json_encode(array("id" => $id,
637 "score_pic" => theme_image($link, get_score_pic($score))));
640 function setpanelmode() {
641 $wide = (int) $_REQUEST["wide"];
643 $_SESSION["widescreen"] = $wide;
645 print json_encode(array("wide" => $wide));