]> git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
offline: made synchronization less aggressive
[tt-rss.git] / modules / backend-rpc.php
1 <?php
2 function handle_rpc_request($link) {
3
4 $subop = $_REQUEST["subop"];
5
6 if ($subop == "setpref") {
7 if (WEB_DEMO_MODE) {
8 return;
9 }
10
11 print "<rpc-reply>";
12
13 $key = db_escape_string($_REQUEST["key"]);
14 $value = db_escape_string($_REQUEST["value"]);
15
16 set_pref($link, $key, $value);
17
18 print "<param-set key=\"$key\" value=\"$value\"/>";
19
20 print "</rpc-reply>";
21
22 return;
23 }
24
25 if ($subop == "getLabelCounters") {
26 $aid = $_REQUEST["aid"];
27 print "<rpc-reply>";
28 print "<counters>";
29 getLabelCounters($link);
30 if ($aid) {
31 getFeedCounter($link, $aid);
32 }
33 print "</counters>";
34 print "</rpc-reply>";
35
36 return;
37 }
38
39 if ($subop == "getFeedCounters") {
40 print "<rpc-reply>";
41 print "<counters>";
42 getFeedCounters($link);
43 print "</counters>";
44 print "</rpc-reply>";
45
46 return;
47 }
48
49 if ($subop == "getAllCounters") {
50 print "<rpc-reply>";
51 print "<counters>";
52
53 $omode = $_REQUEST["omode"];
54
55 getAllCounters($link, $omode);
56 print "</counters>";
57 print_runtime_info($link);
58 print "</rpc-reply>";
59
60 return;
61 }
62
63 if ($subop == "mark") {
64 $mark = $_REQUEST["mark"];
65 $id = db_escape_string($_REQUEST["id"]);
66
67 if ($mark == "1") {
68 $mark = "true";
69 } else {
70 $mark = "false";
71 }
72
73 // FIXME this needs collision testing
74
75 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
76 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
77
78 print "<rpc-reply><counters>";
79 getGlobalCounters($link);
80 getLabelCounters($link);
81 if (get_pref($link, 'ENABLE_FEED_CATS')) {
82 getCategoryCounters($link);
83 }
84 print "</counters></rpc-reply>";
85
86 return;
87 }
88
89 if ($subop == "delete") {
90 $ids = db_escape_string($_REQUEST["ids"]);
91
92 $result = db_query($link, "DELETE FROM ttrss_user_entries
93 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
94
95 print "<rpc-reply><counters>";
96 getGlobalCounters($link);
97 if (get_pref($link, 'ENABLE_FEED_CATS')) {
98 getCategoryCounters($link);
99 }
100 print "</counters></rpc-reply>";
101
102 return;
103 }
104
105 if ($subop == "unarchive") {
106 $ids = db_escape_string($_REQUEST["ids"]);
107
108 $result = db_query($link, "UPDATE ttrss_user_entries
109 SET feed_id = orig_feed_id, orig_feed_id = NULL
110 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
111
112 print "<rpc-reply><counters>";
113 getGlobalCounters($link);
114 if (get_pref($link, 'ENABLE_FEED_CATS')) {
115 getCategoryCounters($link);
116 }
117 print "</counters></rpc-reply>";
118
119 return;
120 }
121
122 if ($subop == "archive") {
123 $ids = split(",", db_escape_string($_REQUEST["ids"]));
124
125 foreach ($ids as $id) {
126 archive_article($link, $id, $_SESSION["uid"]);
127 }
128
129 print "<rpc-reply><counters>";
130 getGlobalCounters($link);
131 if (get_pref($link, 'ENABLE_FEED_CATS')) {
132 getCategoryCounters($link);
133 }
134 print "</counters></rpc-reply>";
135
136 return;
137 }
138
139
140 if ($subop == "publ") {
141 $pub = $_REQUEST["pub"];
142 $id = db_escape_string($_REQUEST["id"]);
143 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
144
145 if ($pub == "1") {
146 $pub = "true";
147 } else {
148 $pub = "false";
149 }
150
151 if ($note != 'undefined') {
152 $note_qpart = "note = '$note',";
153 }
154
155 // FIXME this needs collision testing
156
157 $result = db_query($link, "UPDATE ttrss_user_entries SET
158 $note_qpart
159 published = $pub
160 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
161
162
163 print "<rpc-reply>";
164
165 print "<counters>";
166 getGlobalCounters($link);
167 getLabelCounters($link);
168 if (get_pref($link, 'ENABLE_FEED_CATS')) {
169 getCategoryCounters($link);
170 }
171 print "</counters>";
172
173 if ($note != 'undefined') {
174 $note_size = strlen($note);
175 print "<note id=\"$id\" size=\"$note_size\">";
176 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
177 print "</note>";
178 }
179
180 print "</rpc-reply>";
181
182 return;
183 }
184
185 if ($subop == "updateFeed") {
186 $feed_id = db_escape_string($_REQUEST["feed"]);
187
188 $result = db_query($link,
189 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
190 AND owner_uid = " . $_SESSION["uid"]);
191
192 if (db_num_rows($result) > 0) {
193 $feed_url = db_fetch_result($result, 0, "feed_url");
194 update_rss_feed($link, $feed_url, $feed_id);
195 }
196
197 print "<rpc-reply>";
198 print "<counters>";
199 getFeedCounter($link, $feed_id);
200 print "</counters>";
201 print "</rpc-reply>";
202
203 return;
204 }
205
206 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
207
208 $global_unread_caller = sprintf("%d", $_REQUEST["uctr"]);
209 $global_unread = getGlobalUnread($link);
210
211 print "<rpc-reply>";
212
213 print "<counters>";
214
215 if ($global_unread_caller != $global_unread) {
216
217 $omode = $_REQUEST["omode"];
218
219 if (!$omode) $omode = "tflc";
220
221 if (strchr($omode, "l")) getLabelCounters($link);
222
223 if (strchr($omode, "c")) {
224 if (get_pref($link, 'ENABLE_FEED_CATS')) {
225 getCategoryCounters($link);
226 }
227 }
228
229 if (strchr($omode, "f")) getFeedCounters($link);
230 if (strchr($omode, "t")) getTagCounters($link);
231
232 getGlobalCounters($link, $global_unread);
233 }
234
235 print "</counters>";
236
237 print_runtime_info($link);
238
239 print "</rpc-reply>";
240
241 return;
242 }
243
244 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
245 if ($subop == "catchupSelected") {
246
247 $ids = split(",", db_escape_string($_REQUEST["ids"]));
248 $cmode = sprintf("%d", $_REQUEST["cmode"]);
249
250 catchupArticlesById($link, $ids, $cmode);
251
252 print "<rpc-reply>";
253 print "<counters>";
254 getAllCounters($link, $_REQUEST["omode"]);
255 print "</counters>";
256 print_runtime_info($link);
257 print "</rpc-reply>";
258
259 return;
260 }
261
262 if ($subop == "markSelected") {
263
264 $ids = split(",", db_escape_string($_REQUEST["ids"]));
265 $cmode = sprintf("%d", $_REQUEST["cmode"]);
266
267 markArticlesById($link, $ids, $cmode);
268
269 print "<rpc-reply>";
270 print "<counters>";
271 getAllCounters($link, $_REQUEST["omode"]);
272 print "</counters>";
273 print_runtime_info($link);
274 print "</rpc-reply>";
275
276 return;
277 }
278
279 if ($subop == "publishSelected") {
280
281 $ids = split(",", db_escape_string($_REQUEST["ids"]));
282 $cmode = sprintf("%d", $_REQUEST["cmode"]);
283
284 publishArticlesById($link, $ids, $cmode);
285
286 print "<rpc-reply>";
287 print "<counters>";
288 getAllCounters($link, $_REQUEST["omode"]);
289 print "</counters>";
290 print_runtime_info($link);
291 print "</rpc-reply>";
292
293 return;
294 }
295
296 if ($subop == "sanityCheck") {
297 print "<rpc-reply>";
298 if (sanity_check($link)) {
299 print "<error error-code=\"0\"/>";
300 print_init_params($link);
301 print_runtime_info($link);
302
303 # assign client-passed params to session
304 $_SESSION["client.userAgent"] = $_REQUEST["ua"];
305
306 }
307 print "</rpc-reply>";
308
309 return;
310 }
311
312 if ($subop == "globalPurge") {
313
314 print "<rpc-reply>";
315 global_purge_old_posts($link, true);
316 print "</rpc-reply>";
317
318 return;
319 }
320
321 if ($subop == "getArticleLink") {
322
323 $id = db_escape_string($_REQUEST["id"]);
324
325 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
326 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
327
328 if (db_num_rows($result) == 1) {
329 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
330 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
331 } else {
332 print "<rpc-reply><error>Article not found</error></rpc-reply>";
333 }
334
335 return;
336 }
337
338 if ($subop == "setArticleTags") {
339
340 $id = db_escape_string($_REQUEST["id"]);
341
342 $tags_str = db_escape_string($_REQUEST["tags_str"]);
343
344 $tags = array_unique(trim_array(split(",", $tags_str)));
345
346 db_query($link, "BEGIN");
347
348 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
349 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
350
351 if (db_num_rows($result) == 1) {
352
353 $int_id = db_fetch_result($result, 0, "int_id");
354
355 db_query($link, "DELETE FROM ttrss_tags WHERE
356 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
357
358 foreach ($tags as $tag) {
359 $tag = sanitize_tag($tag);
360
361 if (!tag_is_valid($tag)) {
362 continue;
363 }
364
365 if (preg_match("/^[0-9]*$/", $tag)) {
366 continue;
367 }
368
369 // print "<!-- $id : $int_id : $tag -->";
370
371 if ($tag != '') {
372 db_query($link, "INSERT INTO ttrss_tags
373 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
374 }
375 }
376 }
377
378 db_query($link, "COMMIT");
379
380 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
381
382 print "<rpc-reply>
383 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
384 </rpc-reply>";
385
386 return;
387 }
388
389 if ($subop == "regenPubKey") {
390
391 print "<rpc-reply>";
392
393 set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
394
395 $new_link = article_publish_url($link);
396
397 print "<link><![CDATA[$new_link]]></link>";
398
399 print "</rpc-reply>";
400
401 return;
402 }
403
404 if ($subop == "logout") {
405 logout_user();
406 print_error_xml(6);
407 return;
408 }
409
410 if ($subop == "completeTags") {
411
412 $search = db_escape_string($_REQUEST["search"]);
413
414 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
415 WHERE owner_uid = '".$_SESSION["uid"]."' AND
416 tag_name LIKE '$search%' ORDER BY tag_name
417 LIMIT 10");
418
419 print "<ul>";
420 while ($line = db_fetch_assoc($result)) {
421 print "<li>" . $line["tag_name"] . "</li>";
422 }
423 print "</ul>";
424
425 return;
426 }
427
428 if ($subop == "purge") {
429 $ids = split(",", db_escape_string($_REQUEST["ids"]));
430 $days = sprintf("%d", $_REQUEST["days"]);
431
432 print "<rpc-reply>";
433
434 print "<message><![CDATA[";
435
436 foreach ($ids as $id) {
437
438 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
439 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
440
441 if (db_num_rows($result) == 1) {
442 purge_feed($link, $id, $days, true);
443 }
444 }
445
446 print "]]></message>";
447
448 print "</rpc-reply>";
449
450 return;
451 }
452
453 /* if ($subop == "setScore") {
454 $id = db_escape_string($_REQUEST["id"]);
455 $score = sprintf("%d", $_REQUEST["score"]);
456
457 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
458 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
459
460 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
461
462 return;
463
464 } */
465
466 if ($subop == "getArticles") {
467 $ids = split(",", db_escape_string($_REQUEST["ids"]));
468
469 print "<rpc-reply>";
470
471 foreach ($ids as $id) {
472 if ($id) {
473 outputArticleXML($link, $id, 0, false);
474 }
475 }
476 print "</rpc-reply>";
477
478 return;
479 }
480
481 if ($subop == "checkDate") {
482
483 $date = db_escape_string($_REQUEST["date"]);
484 $date_parsed = strtotime($date);
485
486 print "<rpc-reply>";
487
488 if ($date_parsed) {
489 print "<result>1</result>";
490 } else {
491 print "<result>0</result>";
492 }
493
494 print "</rpc-reply>";
495
496 return;
497 }
498
499 if ($subop == "removeFromLabel") {
500
501 $ids = split(",", db_escape_string($_REQUEST["ids"]));
502 $label_id = db_escape_string($_REQUEST["lid"]);
503
504 $label = db_escape_string(label_find_caption($link, $label_id,
505 $_SESSION["uid"]));
506
507 print "<rpc-reply>";
508 print "<info-for-headlines>";
509
510 if ($label) {
511
512 foreach ($ids as $id) {
513 label_remove_article($link, $id, $label, $_SESSION["uid"]);
514
515 print "<entry id=\"$id\"><![CDATA[";
516
517 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
518 print format_article_labels($labels, $id);
519
520 print "]]></entry>";
521
522 }
523 }
524
525 print "</info-for-headlines>";
526
527 print "<counters>";
528 getAllCounters($link, $omode);
529 print "</counters>";
530 print "</rpc-reply>";
531
532 return;
533 }
534
535 if ($subop == "assignToLabel") {
536
537 $ids = split(",", db_escape_string($_REQUEST["ids"]));
538 $label_id = db_escape_string($_REQUEST["lid"]);
539
540 $label = db_escape_string(label_find_caption($link, $label_id,
541 $_SESSION["uid"]));
542
543 print "<rpc-reply>";
544
545 print "<info-for-headlines>";
546
547 if ($label) {
548
549 foreach ($ids as $id) {
550 label_add_article($link, $id, $label, $_SESSION["uid"]);
551
552 print "<entry id=\"$id\"><![CDATA[";
553
554 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
555 print format_article_labels($labels, $id);
556
557 print "]]></entry>";
558
559 }
560 }
561
562 print "</info-for-headlines>";
563
564 print "<counters>";
565 getAllCounters($link, $omode);
566 print "</counters>";
567 print "</rpc-reply>";
568
569 return;
570 }
571
572 if ($subop == "feedBrowser") {
573
574 $search = db_escape_string($_REQUEST["search"]);
575 $limit = db_escape_string($_REQUEST["limit"]);
576
577 print "<rpc-reply>";
578 print "<content>";
579 print "<![CDATA[";
580 $ctr = print_feed_browser($link, $search, $limit);
581 print "]]>";
582 print "</content>";
583 print "<num-results value=\"$ctr\"/>";
584 print "</rpc-reply>";
585
586 return;
587 }
588
589 if ($subop == "download") {
590 $stage = (int) $_REQUEST["stage"];
591 $cidt = (int)db_escape_string($_REQUEST["cidt"]);
592 $cidb = (int)db_escape_string($_REQUEST["cidb"]);
593 $sync = db_escape_string($_REQUEST["sync"]);
594 //$amount = (int) $_REQUEST["amount"];
595 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
596 //if (!$amount) $amount = 50;
597
598 /* Amount is not used by the frontend offline.js anymore, it goes by
599 * date_qpart below + cidb/cidt IDs */
600
601 $amount = 2000;
602 $unread_only = true;
603
604 print "<rpc-reply>";
605
606 $sync = split(";", $sync);
607
608 print "<sync>";
609
610 if (count($sync) > 0) {
611 if (strtotime($sync[0])) {
612 $last_online = db_escape_string($sync[0]);
613
614 print "<sync-point><![CDATA[$last_online]]></sync-point>";
615
616 for ($i = 1; $i < count($sync); $i++) {
617 $e = split(",", $sync[$i]);
618
619 if (count($e) == 3) {
620
621 $id = (int) $e[0];
622 $unread = bool_to_sql_bool((bool) $e[1]);
623 $marked = (bool)$e[2];
624
625 if ($marked) {
626 $marked = bool_to_sql_bool($marked);
627 $marked_qpart = "marked = $marked,";
628 }
629
630 $query = "UPDATE ttrss_user_entries SET
631 $marked_qpart
632 unread = $unread,
633 last_read = '$last_online'
634 WHERE ref_id = '$id' AND
635 (last_read IS NULL OR last_read < '$last_online') AND
636 owner_uid = ".$_SESSION["uid"];
637
638 $result = db_query($link, $query);
639
640 print "<sync-ok id=\"$id\"/>";
641
642 }
643 }
644
645 /* Maybe we need to further update local DB for this client */
646
647 $query = "SELECT ref_id,unread,marked FROM ttrss_user_entries
648 WHERE last_read >= '$last_online' AND
649 owner_uid = ".$_SESSION["uid"] . " LIMIT 1000";
650
651 $result = db_query($link, $query);
652
653 while ($line = db_fetch_assoc($result)) {
654 $unread = (int) sql_bool_to_bool($line["unread"]);
655 $marked = (int) sql_bool_to_bool($line["marked"]);
656
657 print "<sync-ok unread=\"$unread\" marked=\"$marked\"
658 id=\"".$line["ref_id"]."\"/>";
659 }
660
661 }
662 }
663
664 print "</sync>";
665
666 if ($stage == 0) {
667 print "<feeds>";
668
669 $result = db_query($link, "SELECT id, title, cat_id FROM
670 ttrss_feeds WHERE hidden = false AND owner_uid = ".$_SESSION["uid"]);
671
672 while ($line = db_fetch_assoc($result)) {
673
674 $has_icon = (int) feed_has_icon($line["id"]);
675
676 print "<feed has_icon=\"$has_icon\"
677 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
678 print $line["title"];
679 print "]]></feed>";
680 }
681
682 print "</feeds>";
683
684 print "<feed-categories>";
685
686 $result = db_query($link, "SELECT id, title, collapsed FROM
687 ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
688
689 print "<category id=\"0\" collapsed=\"".
690 (int)get_pref($link, "_COLLAPSED_UNCAT")."\"><![CDATA[";
691 print __("Uncategorized");
692 print "]]></category>";
693
694 print "<category id=\"-1\" collapsed=\"".
695 (int)get_pref($link, "_COLLAPSED_SPECIAL")."\"><![CDATA[";
696 print __("Special");
697 print "]]></category>";
698
699 print "<category id=\"-2\" collapsed=\"".
700 (int)get_pref($link, "_COLLAPSED_LABELS")."\"><![CDATA[";
701 print __("Labels");
702 print "]]></category>";
703
704 while ($line = db_fetch_assoc($result)) {
705 print "<category
706 id=\"".$line["id"]."\"
707 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
708 print $line["title"];
709 print "]]></category>";
710 }
711
712 print "</feed-categories>";
713
714 print "<labels>";
715
716 $result = db_query($link, "SELECT * FROM
717 ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
718
719 while ($line = db_fetch_assoc($result)) {
720 print "<label
721 id=\"".$line["id"]."\"
722 fg_color=\"".$line["fg_color"]."\"
723 bg_color=\"".$line["bg_color"]."\"
724 ><![CDATA[";
725 print $line["caption"];
726 print "]]></label>";
727 }
728
729
730 print "</labels>";
731
732 }
733
734 if ($stage > 0) {
735 print "<articles>";
736
737 $limit = 10;
738 $skip = $limit*($stage-1);
739
740 print "<limit value=\"$limit\"/>";
741
742 if ($amount > 0) $amount -= $skip;
743
744 if ($amount > 0) {
745
746 $limit = min($limit, $amount);
747
748 if ($unread_only) {
749 $unread_qpart = "(unread = true OR marked = true) AND ";
750 }
751
752 if ($cidt && $cidb) {
753 $cid_qpart = "(ttrss_entries.id > $cidt OR ttrss_entries.id < $cidb) AND ";
754 }
755
756 if (DB_TYPE == "pgsql") {
757 $date_qpart = "updated >= NOW() - INTERVAL '1 week' AND";
758 } else {
759 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 WEEK) AND";
760 }
761
762 $result = db_query($link,
763 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
764 guid,link,comments,
765 feed_id,content,updated,unread,marked FROM
766 ttrss_user_entries,ttrss_entries,ttrss_feeds
767 WHERE $unread_qpart $cid_qpart $date_qpart
768 hidden = false AND
769 ttrss_feeds.id = feed_id AND
770 ref_id = ttrss_entries.id AND
771 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
772 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
773
774 if (function_exists('json_encode')) {
775
776 while ($line = db_fetch_assoc($result)) {
777 print "<article><![CDATA[";
778
779 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
780 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
781
782 $line["labels"] = get_article_labels($link, $line["id"]);
783
784 // too slow :(
785 // $line["tags"] = format_tags_string(
786 // get_article_tags($link, $line["id"]), $line["id"]);
787
788 print json_encode($line);
789 print "]]></article>";
790 }
791 }
792
793 }
794
795 print "</articles>";
796
797 }
798
799 print "</rpc-reply>";
800
801 return;
802 }
803
804 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
805 }
806 ?>