]>
git.wh0rd.org - tt-rss.git/blob - classes/rpc.php
272c25ef4b74f8e2c381d15b3beb886cfb882661
2 class RPC
extends Protected_Handler
{
4 function setprofile() {
5 $id = db_escape_string($_REQUEST["id"]);
7 $_SESSION["profile"] = $id;
8 $_SESSION["prefs_cache"] = array();
11 function remprofiles() {
12 $ids = explode(",", db_escape_string(trim($_REQUEST["ids"])));
14 foreach ($ids as $id) {
15 if ($_SESSION["profile"] != $id) {
16 db_query($this->link
, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
17 owner_uid = " . $_SESSION["uid"]);
23 function addprofile() {
24 $title = db_escape_string(trim($_REQUEST["title"]));
26 db_query($this->link
, "BEGIN");
28 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles
29 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
31 if (db_num_rows($result) == 0) {
33 db_query($this->link
, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
34 VALUES ('$title', ".$_SESSION["uid"] .")");
36 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles WHERE
39 if (db_num_rows($result) != 0) {
40 $profile_id = db_fetch_result($result, 0, "id");
43 initialize_user_prefs($this->link
, $_SESSION["uid"], $profile_id);
48 db_query($this->link
, "COMMIT");
53 function saveprofile() {
54 $id = db_escape_string($_REQUEST["id"]);
55 $title = db_escape_string(trim($_REQUEST["value"]));
58 print __("Default profile");
63 db_query($this->link
, "BEGIN");
65 $result = db_query($this->link
, "SELECT id FROM ttrss_settings_profiles
66 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
68 if (db_num_rows($result) == 0) {
69 db_query($this->link
, "UPDATE ttrss_settings_profiles
70 SET title = '$title' WHERE id = '$id' AND
71 owner_uid = " . $_SESSION["uid"]);
74 $result = db_query($this->link
, "SELECT title FROM ttrss_settings_profiles
75 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
76 print db_fetch_result($result, 0, "title");
79 db_query($this->link
, "COMMIT");
84 function remarchive() {
85 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
87 foreach ($ids as $id) {
88 $result = db_query($this->link
, "DELETE FROM ttrss_archived_feeds WHERE
89 (SELECT COUNT(*) FROM ttrss_user_entries
90 WHERE orig_feed_id = '$id') = 0 AND
91 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
93 $rc = db_affected_rows($this->link
, $result);
98 $feed = db_escape_string($_REQUEST['feed']);
99 $cat = db_escape_string($_REQUEST['cat']);
100 $login = db_escape_string($_REQUEST['login']);
101 $pass = db_escape_string($_REQUEST['pass']);
103 $rc = subscribe_to_feed($this->link
, $feed, $cat, $login, $pass);
105 print json_encode(array("result" => $rc));
108 function extractfeedurls() {
109 $urls = get_feeds_from_html($_REQUEST['url']);
111 print json_encode(array("urls" => $urls));
114 function togglepref() {
115 $key = db_escape_string($_REQUEST["key"]);
116 set_pref($this->link
, $key, !get_pref($this->link
, $key));
117 $value = get_pref($this->link
, $key);
119 print json_encode(array("param" =>$key, "value" => $value));
123 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
125 $key = db_escape_string($_REQUEST["key"]);
126 $value = db_escape_string($value);
128 set_pref($this->link
, $key, $value);
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
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() {
276 $id = db_escape_string($_REQUEST["id"]);
278 $tags_str = db_escape_string($_REQUEST["tags_str"]);
279 $tags = array_unique(trim_array(explode(",", $tags_str)));
281 db_query($this->link
, "BEGIN");
283 $result = db_query($this->link
, "SELECT int_id FROM ttrss_user_entries WHERE
284 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
286 if (db_num_rows($result) == 1) {
288 $tags_to_cache = array();
290 $int_id = db_fetch_result($result, 0, "int_id");
292 db_query($this->link
, "DELETE FROM ttrss_tags WHERE
293 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
295 foreach ($tags as $tag) {
296 $tag = sanitize_tag($tag);
298 if (!tag_is_valid($tag)) {
302 if (preg_match("/^[0-9]*$/", $tag)) {
306 // print "<!-- $id : $int_id : $tag -->";
309 db_query($this->link
, "INSERT INTO ttrss_tags
310 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
313 array_push($tags_to_cache, $tag);
316 /* update tag cache */
318 sort($tags_to_cache);
319 $tags_str = join(",", $tags_to_cache);
321 db_query($this->link
, "UPDATE ttrss_user_entries
322 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
323 AND owner_uid = " . $_SESSION["uid"]);
326 db_query($this->link
, "COMMIT");
329 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
330 $memcache->delete($obj_id);
333 $tags = get_article_tags($this->link
, $id);
334 $tags_str = format_tags_string($tags, $id);
335 $tags_str_full = join(", ", $tags);
337 if (!$tags_str_full) $tags_str_full = __("no tags");
339 print json_encode(array("tags_str" => array("id" => $id,
340 "content" => $tags_str, "content_full" => $tags_str_full)));
343 function regenOPMLKey() {
344 update_feed_access_key($this->link
, 'OPML:Publish',
345 false, $_SESSION["uid"]);
347 $new_link = opml_publish_url($this->link
);
349 print json_encode(array("link" => $new_link));
352 function completeTags() {
353 $search = db_escape_string($_REQUEST["search"]);
355 $result = db_query($this->link
, "SELECT DISTINCT tag_name FROM ttrss_tags
356 WHERE owner_uid = '".$_SESSION["uid"]."' AND
357 tag_name LIKE '$search%' ORDER BY tag_name
361 while ($line = db_fetch_assoc($result)) {
362 print "<li>" . $line["tag_name"] . "</li>";
368 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
369 $days = sprintf("%d", $_REQUEST["days"]);
371 foreach ($ids as $id) {
373 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
374 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
376 if (db_num_rows($result) == 1) {
377 purge_feed($this->link
, $id, $days);
382 function getArticles() {
383 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
386 foreach ($ids as $id) {
388 array_push($articles, format_article($this->link
, $id, 0, false));
392 print json_encode($articles);
395 function checkDate() {
396 $date = db_escape_string($_REQUEST["date"]);
397 $date_parsed = strtotime($date);
399 print json_encode(array("result" => (bool)$date_parsed,
400 "date" => date("c", $date_parsed)));
403 function assigntolabel() {
404 return labelops(true);
407 function removefromlabel() {
408 return labelops(false);
411 function labelops($assign) {
414 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
415 $label_id = db_escape_string($_REQUEST["lid"]);
417 $label = db_escape_string(label_find_caption($this->link
, $label_id,
420 $reply["info-for-headlines"] = array();
424 foreach ($ids as $id) {
427 label_add_article($this->link
, $id, $label, $_SESSION["uid"]);
429 label_remove_article($this->link
, $id, $label, $_SESSION["uid"]);
431 $labels = get_article_labels($this->link
, $id, $_SESSION["uid"]);
433 array_push($reply["info-for-headlines"],
434 array("id" => $id, "labels" => format_article_labels($labels, $id)));
439 $reply["message"] = "UPDATE_COUNTERS";
441 print json_encode($reply);
444 function updateFeedBrowser() {
445 $search = db_escape_string($_REQUEST["search"]);
446 $limit = db_escape_string($_REQUEST["limit"]);
447 $mode = (int) db_escape_string($_REQUEST["mode"]);
449 print json_encode(array("content" =>
450 make_feed_browser($this->link
, $search, $limit, $mode),
455 function massSubscribe() {
457 $payload = json_decode($_REQUEST["payload"], false);
458 $mode = $_REQUEST["mode"];
460 if (!$payload ||
!is_array($payload)) return;
463 foreach ($payload as $feed) {
465 $title = db_escape_string($feed[0]);
466 $feed_url = db_escape_string($feed[1]);
468 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
469 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
471 if (db_num_rows($result) == 0) {
472 $result = db_query($this->link
, "INSERT INTO ttrss_feeds
473 (owner_uid,feed_url,title,cat_id,site_url)
474 VALUES ('".$_SESSION["uid"]."',
475 '$feed_url', '$title', NULL, '')");
478 } else if ($mode == 2) {
480 foreach ($payload as $id) {
481 $result = db_query($this->link
, "SELECT * FROM ttrss_archived_feeds
482 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
484 if (db_num_rows($result) != 0) {
485 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
486 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
487 $title = db_escape_string(db_fetch_result($result, 0, "title"));
489 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
490 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
492 if (db_num_rows($result) == 0) {
493 $result = db_query($this->link
, "INSERT INTO ttrss_feeds
494 (owner_uid,feed_url,title,cat_id,site_url)
495 VALUES ('$id','".$_SESSION["uid"]."',
496 '$feed_url', '$title', NULL, '$site_url')");
503 function digestgetcontents() {
504 $article_id = db_escape_string($_REQUEST['article_id']);
506 $result = db_query($this->link
, "SELECT content,title,link,marked,published
507 FROM ttrss_entries, ttrss_user_entries
508 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
510 $content = sanitize($this->link
, db_fetch_result($result, 0, "content"));
511 $title = strip_tags(db_fetch_result($result, 0, "title"));
512 $article_url = htmlspecialchars(db_fetch_result($result, 0, "link"));
513 $marked = sql_bool_to_bool(db_fetch_result($result, 0, "marked"));
514 $published = sql_bool_to_bool(db_fetch_result($result, 0, "published"));
516 print json_encode(array("article" =>
517 array("id" => $article_id, "url" => $article_url,
518 "tags" => get_article_tags($this->link
, $article_id),
519 "marked" => $marked, "published" => $published,
520 "title" => $title, "content" => $content)));
523 function digestupdate() {
524 $feed_id = db_escape_string($_REQUEST['feed_id']);
525 $offset = db_escape_string($_REQUEST['offset']);
526 $seq = db_escape_string($_REQUEST['seq']);
528 if (!$feed_id) $feed_id = -4;
529 if (!$offset) $offset = 0;
533 $reply['seq'] = $seq;
535 $headlines = api_get_headlines($this->link
, $feed_id, 30, $offset,
536 '', ($feed_id == -4), true, false, "unread", "updated DESC", 0, 0);
538 $reply['headlines'] = array();
539 $reply['headlines']['title'] = getFeedTitle($this->link
, $feed_id);
540 $reply['headlines']['content'] = $headlines;
542 print json_encode($reply);
545 function digestinit() {
546 $tmp_feeds = api_get_feeds($this->link
, -4, true, false, 0);
550 foreach ($tmp_feeds as $f) {
551 if ($f['id'] > 0 ||
$f['id'] == -4) array_push($feeds, $f);
554 print json_encode(array("feeds" => $feeds));
557 function catchupFeed() {
558 $feed_id = db_escape_string($_REQUEST['feed_id']);
559 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
561 catchup_feed($this->link
, $feed_id, $is_cat);
563 print json_encode(array("message" => "UPDATE_COUNTERS"));
566 function sendEmail() {
567 $secretkey = $_REQUEST['secretkey'];
569 require_once 'lib/phpmailer/class.phpmailer.php';
573 if ($_SESSION['email_secretkey'] &&
574 $secretkey == $_SESSION['email_secretkey']) {
576 $_SESSION['email_secretkey'] = '';
578 $destination = $_REQUEST['destination'];
579 $subject = $_REQUEST['subject'];
580 $content = $_REQUEST['content'];
582 $replyto = strip_tags($_SESSION['email_replyto']);
583 $fromname = strip_tags($_SESSION['email_fromname']);
585 $mail = new PHPMailer();
587 $mail->PluginDir
= "lib/phpmailer/";
588 $mail->SetLanguage("en", "lib/phpmailer/language/");
590 $mail->CharSet
= "UTF-8";
592 $mail->From
= $replyto;
593 $mail->FromName
= $fromname;
594 $mail->AddAddress($destination);
597 $mail->Host
= SMTP_HOST
;
598 $mail->Mailer
= "smtp";
599 $mail->SMTPAuth
= SMTP_LOGIN
!= '';
600 $mail->Username
= SMTP_LOGIN
;
601 $mail->Password
= SMTP_PASSWORD
;
604 $mail->IsHTML(false);
605 $mail->Subject
= $subject;
606 $mail->Body
= $content;
611 $reply['error'] = $mail->ErrorInfo
;
613 save_email_address($this->link
, db_escape_string($destination));
614 $reply['message'] = "UPDATE_COUNTERS";
618 $reply['error'] = "Not authorized.";
621 print json_encode($reply);
624 function completeEmails() {
625 $search = db_escape_string($_REQUEST["search"]);
629 foreach ($_SESSION['stored_emails'] as $email) {
630 if (strpos($email, $search) !== false) {
631 print "<li>$email</li>";
638 function quickAddCat() {
639 $cat = db_escape_string($_REQUEST["cat"]);
641 add_feed_category($this->link
, $cat);
643 $result = db_query($this->link
, "SELECT id FROM ttrss_feed_categories WHERE
644 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
646 if (db_num_rows($result) == 1) {
647 $id = db_fetch_result($result, 0, "id");
652 print_feed_cat_select($this->link
, "cat_id", $id);
655 function regenFeedKey() {
656 $feed_id = db_escape_string($_REQUEST['id']);
657 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
659 $new_key = update_feed_access_key($this->link
, $feed_id, $is_cat);
661 print json_encode(array("link" => $new_key));
665 function clearKeys() {
666 db_query($this->link
, "DELETE FROM ttrss_access_keys WHERE
667 owner_uid = " . $_SESSION["uid"]);
671 function clearArticleKeys() {
672 db_query($this->link
, "UPDATE ttrss_user_entries SET uuid = '' WHERE
673 owner_uid = " . $_SESSION["uid"]);
679 function verifyRegexp() {
680 $reg_exp = $_REQUEST["reg_exp"];
682 $status = @preg_match
("/$reg_exp/i", "TEST") !== false;
684 print json_encode(array("status" => $status));
687 // TODO: unify with digest-get-contents?
688 function cdmGetArticle() {
689 $ids = array(db_escape_string($_REQUEST["id"]));
690 $cids = explode(",", $_REQUEST["cids"]);
692 $ids = array_merge($ids, $cids);
696 foreach ($ids as $id) {
699 $result = db_query($this->link
, "SELECT content,
700 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
702 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
703 ttrss_entries.id = ref_id AND
704 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
706 if (db_num_rows($result) != 0) {
707 $line = db_fetch_assoc($result);
709 $article_content = sanitize($this->link
, $line["content"],
710 false, false, $line['site_url']);
713 array("id" => $id, "content" => $article_content));
717 print json_encode($rv);
720 function scheduleFeedUpdate() {
721 $feed_id = db_escape_string($_REQUEST["id"]);
722 $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
724 $message = __("Your request could not be completed.");
728 $message = __("Feed update has been scheduled.");
730 db_query($this->link
, "UPDATE ttrss_feeds SET
731 last_update_started = '1970-01-01',
732 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
733 owner_uid = ".$_SESSION["uid"]);
736 $message = __("Category update has been scheduled.");
739 $cat_query = "cat_id = '$feed_id'";
741 $cat_query = "cat_id IS NULL";
743 db_query($this->link
, "UPDATE ttrss_feeds SET
744 last_update_started = '1970-01-01',
745 last_updated = '1970-01-01' WHERE $cat_query AND
746 owner_uid = ".$_SESSION["uid"]);
749 $message = __("Can't update this kind of feed.");
752 print json_encode(array("message" => $message));
756 function getTweetInfo() {
757 $id = db_escape_string($_REQUEST['id']);
759 $result = db_query($this->link
, "SELECT title, link
760 FROM ttrss_entries, ttrss_user_entries
761 WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
763 if (db_num_rows($result) != 0) {
764 $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
766 $article_link = db_fetch_result($result, 0, 'link');
769 print json_encode(array("title" => $title, "link" => $article_link,
774 $id = db_escape_string($_REQUEST["id"]);
775 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
777 db_query($this->link
, "UPDATE ttrss_user_entries SET note = '$note'
778 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
780 $formatted_note = format_article_note($id, $note);
782 print json_encode(array("note" => $formatted_note,
783 "raw_length" => mb_strlen($note)));
787 $hash = sha1(uniqid(rand(), true));
789 print json_encode(array("hash" => $hash));