]> git.wh0rd.org - tt-rss.git/blob - classes/rpc.php
4cdaef93582a6467c8cf7b22cb31601242552bff
[tt-rss.git] / classes / rpc.php
1 <?php
2 class RPC extends Protected_Handler {
3
4 function csrf_ignore($method) {
5 $csrf_ignored = array("sanitycheck", "buttonplugin");
6
7 return array_search($method, $csrf_ignored) !== false;
8 }
9
10 function setprofile() {
11 $id = db_escape_string($_REQUEST["id"]);
12
13 $_SESSION["profile"] = $id;
14 $_SESSION["prefs_cache"] = array();
15 }
16
17 function remprofiles() {
18 $ids = explode(",", db_escape_string(trim($_REQUEST["ids"])));
19
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"]);
24 }
25 }
26 }
27
28 // Silent
29 function addprofile() {
30 $title = db_escape_string(trim($_REQUEST["title"]));
31 if ($title) {
32 db_query($this->link, "BEGIN");
33
34 $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles
35 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
36
37 if (db_num_rows($result) == 0) {
38
39 db_query($this->link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
40 VALUES ('$title', ".$_SESSION["uid"] .")");
41
42 $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles WHERE
43 title = '$title'");
44
45 if (db_num_rows($result) != 0) {
46 $profile_id = db_fetch_result($result, 0, "id");
47
48 if ($profile_id) {
49 initialize_user_prefs($this->link, $_SESSION["uid"], $profile_id);
50 }
51 }
52 }
53
54 db_query($this->link, "COMMIT");
55 }
56 }
57
58 // Silent
59 function saveprofile() {
60 $id = db_escape_string($_REQUEST["id"]);
61 $title = db_escape_string(trim($_REQUEST["value"]));
62
63 if ($id == 0) {
64 print __("Default profile");
65 return;
66 }
67
68 if ($title) {
69 db_query($this->link, "BEGIN");
70
71 $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles
72 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
73
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"]);
78 print $title;
79 } else {
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");
83 }
84
85 db_query($this->link, "COMMIT");
86 }
87 }
88
89 // Silent
90 function remarchive() {
91 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
92
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"]);
98
99 $rc = db_affected_rows($this->link, $result);
100 }
101 }
102
103 function addfeed() {
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
109 $rc = subscribe_to_feed($this->link, $feed, $cat, $login, $pass);
110
111 print json_encode(array("result" => $rc));
112 }
113
114 function extractfeedurls() {
115 $urls = get_feeds_from_html($_REQUEST['url']);
116
117 print json_encode(array("urls" => $urls));
118 }
119
120 function togglepref() {
121 $key = db_escape_string($_REQUEST["key"]);
122 set_pref($this->link, $key, !get_pref($this->link, $key));
123 $value = get_pref($this->link, $key);
124
125 print json_encode(array("param" =>$key, "value" => $value));
126 }
127
128 function setpref() {
129 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
130
131 $key = db_escape_string($_REQUEST["key"]);
132 $value = db_escape_string($value);
133
134 set_pref($this->link, $key, $value);
135
136 print json_encode(array("param" =>$key, "value" => $value));
137 }
138
139 function mark() {
140 $mark = $_REQUEST["mark"];
141 $id = db_escape_string($_REQUEST["id"]);
142
143 if ($mark == "1") {
144 $mark = "true";
145 } else {
146 $mark = "false";
147 }
148
149 $result = db_query($this->link, "UPDATE ttrss_user_entries SET marked = $mark
150 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
151
152 print json_encode(array("message" => "UPDATE_COUNTERS"));
153 }
154
155 function delete() {
156 $ids = db_escape_string($_REQUEST["ids"]);
157
158 $result = db_query($this->link, "DELETE FROM ttrss_user_entries
159 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
160
161 print json_encode(array("message" => "UPDATE_COUNTERS"));
162 }
163
164 function unarchive() {
165 $ids = db_escape_string($_REQUEST["ids"]);
166
167 $result = db_query($this->link, "UPDATE ttrss_user_entries
168 SET feed_id = orig_feed_id, orig_feed_id = NULL
169 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
170
171 print json_encode(array("message" => "UPDATE_COUNTERS"));
172 }
173
174 function archive() {
175 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
176
177 foreach ($ids as $id) {
178 archive_article($this->link, $id, $_SESSION["uid"]);
179 }
180
181 print json_encode(array("message" => "UPDATE_COUNTERS"));
182 }
183
184 function publ() {
185 $pub = $_REQUEST["pub"];
186 $id = db_escape_string($_REQUEST["id"]);
187 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
188
189 if ($pub == "1") {
190 $pub = "true";
191 } else {
192 $pub = "false";
193 }
194
195 $result = db_query($this->link, "UPDATE ttrss_user_entries SET
196 published = $pub
197 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
198
199 $pubsub_result = false;
200
201 if (PUBSUBHUBBUB_HUB) {
202 $rss_link = get_self_url_prefix() .
203 "/public.php?op=rss&id=-2&key=" .
204 get_feed_access_key($this->link, -2, false);
205
206 $p = new Publisher(PUBSUBHUBBUB_HUB);
207
208 $pubsub_result = $p->publish_update($rss_link);
209 }
210
211 print json_encode(array("message" => "UPDATE_COUNTERS",
212 "pubsub_result" => $pubsub_result));
213 }
214
215 function getAllCounters() {
216 $last_article_id = (int) $_REQUEST["last_article_id"];
217
218 $reply = array();
219
220 if ($seq) $reply['seq'] = $seq;
221
222 if ($last_article_id != getLastArticleId($this->link)) {
223 $omode = $_REQUEST["omode"];
224
225 if ($omode != "T")
226 $reply['counters'] = getAllCounters($this->link, $omode);
227 else
228 $reply['counters'] = getGlobalCounters($this->link);
229 }
230
231 $reply['runtime-info'] = make_runtime_info($this->link);
232
233 print json_encode($reply);
234 }
235
236 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
237 function catchupSelected() {
238 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
239 $cmode = sprintf("%d", $_REQUEST["cmode"]);
240
241 catchupArticlesById($this->link, $ids, $cmode);
242
243 print json_encode(array("message" => "UPDATE_COUNTERS"));
244 }
245
246 function markSelected() {
247 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
248 $cmode = sprintf("%d", $_REQUEST["cmode"]);
249
250 markArticlesById($this->link, $ids, $cmode);
251
252 print json_encode(array("message" => "UPDATE_COUNTERS"));
253 }
254
255 function publishSelected() {
256 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
257 $cmode = sprintf("%d", $_REQUEST["cmode"]);
258
259 publishArticlesById($this->link, $ids, $cmode);
260
261 print json_encode(array("message" => "UPDATE_COUNTERS"));
262 }
263
264 function sanityCheck() {
265 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
266
267 $reply = array();
268
269 $reply['error'] = sanity_check($this->link);
270
271 if ($reply['error']['code'] == 0) {
272 $reply['init-params'] = make_init_params($this->link);
273 $reply['runtime-info'] = make_runtime_info($this->link);
274 }
275
276 print json_encode($reply);
277 }
278
279 function setArticleTags() {
280 global $memcache;
281
282 $id = db_escape_string($_REQUEST["id"]);
283
284 $tags_str = db_escape_string($_REQUEST["tags_str"]);
285 $tags = array_unique(trim_array(explode(",", $tags_str)));
286
287 db_query($this->link, "BEGIN");
288
289 $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries WHERE
290 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
291
292 if (db_num_rows($result) == 1) {
293
294 $tags_to_cache = array();
295
296 $int_id = db_fetch_result($result, 0, "int_id");
297
298 db_query($this->link, "DELETE FROM ttrss_tags WHERE
299 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
300
301 foreach ($tags as $tag) {
302 $tag = sanitize_tag($tag);
303
304 if (!tag_is_valid($tag)) {
305 continue;
306 }
307
308 if (preg_match("/^[0-9]*$/", $tag)) {
309 continue;
310 }
311
312 // print "<!-- $id : $int_id : $tag -->";
313
314 if ($tag != '') {
315 db_query($this->link, "INSERT INTO ttrss_tags
316 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
317 }
318
319 array_push($tags_to_cache, $tag);
320 }
321
322 /* update tag cache */
323
324 sort($tags_to_cache);
325 $tags_str = join(",", $tags_to_cache);
326
327 db_query($this->link, "UPDATE ttrss_user_entries
328 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
329 AND owner_uid = " . $_SESSION["uid"]);
330 }
331
332 db_query($this->link, "COMMIT");
333
334 if ($memcache) {
335 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
336 $memcache->delete($obj_id);
337 }
338
339 $tags = get_article_tags($this->link, $id);
340 $tags_str = format_tags_string($tags, $id);
341 $tags_str_full = join(", ", $tags);
342
343 if (!$tags_str_full) $tags_str_full = __("no tags");
344
345 print json_encode(array("tags_str" => array("id" => $id,
346 "content" => $tags_str, "content_full" => $tags_str_full)));
347 }
348
349 function regenOPMLKey() {
350 update_feed_access_key($this->link, 'OPML:Publish',
351 false, $_SESSION["uid"]);
352
353 $new_link = opml_publish_url($this->link);
354
355 print json_encode(array("link" => $new_link));
356 }
357
358 function completeTags() {
359 $search = db_escape_string($_REQUEST["search"]);
360
361 $result = db_query($this->link, "SELECT DISTINCT tag_name FROM ttrss_tags
362 WHERE owner_uid = '".$_SESSION["uid"]."' AND
363 tag_name LIKE '$search%' ORDER BY tag_name
364 LIMIT 10");
365
366 print "<ul>";
367 while ($line = db_fetch_assoc($result)) {
368 print "<li>" . $line["tag_name"] . "</li>";
369 }
370 print "</ul>";
371 }
372
373 function purge() {
374 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
375 $days = sprintf("%d", $_REQUEST["days"]);
376
377 foreach ($ids as $id) {
378
379 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
380 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
381
382 if (db_num_rows($result) == 1) {
383 purge_feed($this->link, $id, $days);
384 }
385 }
386 }
387
388 function getArticles() {
389 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
390 $articles = array();
391
392 foreach ($ids as $id) {
393 if ($id) {
394 array_push($articles, format_article($this->link, $id, 0, false));
395 }
396 }
397
398 print json_encode($articles);
399 }
400
401 function checkDate() {
402 $date = db_escape_string($_REQUEST["date"]);
403 $date_parsed = strtotime($date);
404
405 print json_encode(array("result" => (bool)$date_parsed,
406 "date" => date("c", $date_parsed)));
407 }
408
409 function assigntolabel() {
410 return $this->labelops(true);
411 }
412
413 function removefromlabel() {
414 return $this->labelops(false);
415 }
416
417 function labelops($assign) {
418 $reply = array();
419
420 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
421 $label_id = db_escape_string($_REQUEST["lid"]);
422
423 $label = db_escape_string(label_find_caption($this->link, $label_id,
424 $_SESSION["uid"]));
425
426 $reply["info-for-headlines"] = array();
427
428 if ($label) {
429
430 foreach ($ids as $id) {
431
432 if ($assign)
433 label_add_article($this->link, $id, $label, $_SESSION["uid"]);
434 else
435 label_remove_article($this->link, $id, $label, $_SESSION["uid"]);
436
437 $labels = get_article_labels($this->link, $id, $_SESSION["uid"]);
438
439 array_push($reply["info-for-headlines"],
440 array("id" => $id, "labels" => format_article_labels($labels, $id)));
441
442 }
443 }
444
445 $reply["message"] = "UPDATE_COUNTERS";
446
447 print json_encode($reply);
448 }
449
450 function updateFeedBrowser() {
451 $search = db_escape_string($_REQUEST["search"]);
452 $limit = db_escape_string($_REQUEST["limit"]);
453 $mode = (int) db_escape_string($_REQUEST["mode"]);
454
455 print json_encode(array("content" =>
456 make_feed_browser($this->link, $search, $limit, $mode),
457 "mode" => $mode));
458 }
459
460 // Silent
461 function massSubscribe() {
462
463 $payload = json_decode($_REQUEST["payload"], false);
464 $mode = $_REQUEST["mode"];
465
466 if (!$payload || !is_array($payload)) return;
467
468 if ($mode == 1) {
469 foreach ($payload as $feed) {
470
471 $title = db_escape_string($feed[0]);
472 $feed_url = db_escape_string($feed[1]);
473
474 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
475 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
476
477 if (db_num_rows($result) == 0) {
478 $result = db_query($this->link, "INSERT INTO ttrss_feeds
479 (owner_uid,feed_url,title,cat_id,site_url)
480 VALUES ('".$_SESSION["uid"]."',
481 '$feed_url', '$title', NULL, '')");
482 }
483 }
484 } else if ($mode == 2) {
485 // feed archive
486 foreach ($payload as $id) {
487 $result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds
488 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
489
490 if (db_num_rows($result) != 0) {
491 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
492 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
493 $title = db_escape_string(db_fetch_result($result, 0, "title"));
494
495 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
496 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
497
498 if (db_num_rows($result) == 0) {
499 $result = db_query($this->link, "INSERT INTO ttrss_feeds
500 (owner_uid,feed_url,title,cat_id,site_url)
501 VALUES ('$id','".$_SESSION["uid"]."',
502 '$feed_url', '$title', NULL, '$site_url')");
503 }
504 }
505 }
506 }
507 }
508
509 function digestgetcontents() {
510 $article_id = db_escape_string($_REQUEST['article_id']);
511
512 $result = db_query($this->link, "SELECT content,title,link,marked,published
513 FROM ttrss_entries, ttrss_user_entries
514 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
515
516 $content = sanitize($this->link, db_fetch_result($result, 0, "content"));
517 $title = strip_tags(db_fetch_result($result, 0, "title"));
518 $article_url = htmlspecialchars(db_fetch_result($result, 0, "link"));
519 $marked = sql_bool_to_bool(db_fetch_result($result, 0, "marked"));
520 $published = sql_bool_to_bool(db_fetch_result($result, 0, "published"));
521
522 print json_encode(array("article" =>
523 array("id" => $article_id, "url" => $article_url,
524 "tags" => get_article_tags($this->link, $article_id),
525 "marked" => $marked, "published" => $published,
526 "title" => $title, "content" => $content)));
527 }
528
529 function digestupdate() {
530 $feed_id = db_escape_string($_REQUEST['feed_id']);
531 $offset = db_escape_string($_REQUEST['offset']);
532 $seq = db_escape_string($_REQUEST['seq']);
533
534 if (!$feed_id) $feed_id = -4;
535 if (!$offset) $offset = 0;
536
537 $reply = array();
538
539 $reply['seq'] = $seq;
540
541 $headlines = api_get_headlines($this->link, $feed_id, 30, $offset,
542 '', ($feed_id == -4), true, false, "unread", "updated DESC", 0, 0);
543
544 $reply['headlines'] = array();
545 $reply['headlines']['title'] = getFeedTitle($this->link, $feed_id);
546 $reply['headlines']['content'] = $headlines;
547
548 print json_encode($reply);
549 }
550
551 function digestinit() {
552 $tmp_feeds = api_get_feeds($this->link, -4, true, false, 0);
553
554 $feeds = array();
555
556 foreach ($tmp_feeds as $f) {
557 if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
558 }
559
560 print json_encode(array("feeds" => $feeds));
561 }
562
563 function catchupFeed() {
564 $feed_id = db_escape_string($_REQUEST['feed_id']);
565 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
566
567 catchup_feed($this->link, $feed_id, $is_cat);
568
569 print json_encode(array("message" => "UPDATE_COUNTERS"));
570 }
571
572 function quickAddCat() {
573 $cat = db_escape_string($_REQUEST["cat"]);
574
575 add_feed_category($this->link, $cat);
576
577 $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE
578 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
579
580 if (db_num_rows($result) == 1) {
581 $id = db_fetch_result($result, 0, "id");
582 } else {
583 $id = 0;
584 }
585
586 print_feed_cat_select($this->link, "cat_id", $id);
587 }
588
589 function regenFeedKey() {
590 $feed_id = db_escape_string($_REQUEST['id']);
591 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
592
593 $new_key = update_feed_access_key($this->link, $feed_id, $is_cat);
594
595 print json_encode(array("link" => $new_key));
596 }
597
598 // Silent
599 function clearKeys() {
600 db_query($this->link, "DELETE FROM ttrss_access_keys WHERE
601 owner_uid = " . $_SESSION["uid"]);
602 }
603
604 // Silent
605 function clearArticleKeys() {
606 db_query($this->link, "UPDATE ttrss_user_entries SET uuid = '' WHERE
607 owner_uid = " . $_SESSION["uid"]);
608
609 return;
610 }
611
612
613 function verifyRegexp() {
614 $reg_exp = $_REQUEST["reg_exp"];
615
616 $status = @preg_match("/$reg_exp/i", "TEST") !== false;
617
618 print json_encode(array("status" => $status));
619 }
620
621 // TODO: unify with digest-get-contents?
622 function cdmGetArticle() {
623 $ids = array(db_escape_string($_REQUEST["id"]));
624 $cids = explode(",", $_REQUEST["cids"]);
625
626 $ids = array_merge($ids, $cids);
627
628 $rv = array();
629
630 foreach ($ids as $id) {
631 $id = (int)$id;
632
633 $result = db_query($this->link, "SELECT content,
634 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
635 ttrss_entries
636 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
637 ttrss_entries.id = ref_id AND
638 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
639
640 if (db_num_rows($result) != 0) {
641 $line = db_fetch_assoc($result);
642
643 $article_content = sanitize($this->link, $line["content"],
644 false, false, $line['site_url']);
645
646 array_push($rv,
647 array("id" => $id, "content" => $article_content));
648 }
649 }
650
651 print json_encode($rv);
652 }
653
654 function scheduleFeedUpdate() {
655 $feed_id = db_escape_string($_REQUEST["id"]);
656 $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
657
658 $message = __("Your request could not be completed.");
659
660 if ($feed_id >= 0) {
661 if (!$is_cat) {
662 $message = __("Feed update has been scheduled.");
663
664 db_query($this->link, "UPDATE ttrss_feeds SET
665 last_update_started = '1970-01-01',
666 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
667 owner_uid = ".$_SESSION["uid"]);
668
669 } else {
670 $message = __("Category update has been scheduled.");
671
672 if ($feed_id)
673 $cat_query = "cat_id = '$feed_id'";
674 else
675 $cat_query = "cat_id IS NULL";
676
677 db_query($this->link, "UPDATE ttrss_feeds SET
678 last_update_started = '1970-01-01',
679 last_updated = '1970-01-01' WHERE $cat_query AND
680 owner_uid = ".$_SESSION["uid"]);
681 }
682 } else {
683 $message = __("Can't update this kind of feed.");
684 }
685
686 print json_encode(array("message" => $message));
687 return;
688 }
689
690 function buttonPlugin() {
691 $pclass = basename($_REQUEST['plugin']) . "_button";
692 $method = $_REQUEST['plugin_method'];
693
694 if (class_exists($pclass)) {
695 $plugin = new $pclass($this->link);
696 if (method_exists($plugin, $method)) {
697 return $plugin->$method();
698 }
699 }
700 }
701
702 function genHash() {
703 $hash = sha1(uniqid(rand(), true));
704
705 print json_encode(array("hash" => $hash));
706 }
707 }
708 ?>