]> git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
fix background refresh being broken when no counter data is sent
[tt-rss.git] / modules / backend-rpc.php
1 <?php
2 function handle_rpc_request($link) {
3
4 $subop = $_REQUEST["subop"];
5
6 if ($subop == "setprofile") {
7 $id = db_escape_string($_REQUEST["id"]);
8
9 $_SESSION["profile"] = $id;
10 $_SESSION["prefs_cache"] = array();
11 return;
12 }
13
14 if ($subop == "remprofiles") {
15 $ids = split(",", db_escape_string(trim($_REQUEST["ids"])));
16
17 foreach ($ids as $id) {
18 if ($_SESSION["profile"] != $id) {
19 db_query($link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
20 owner_uid = " . $_SESSION["uid"]);
21 }
22 }
23 return;
24 }
25
26 if ($subop == "addprofile") {
27 $title = db_escape_string(trim($_REQUEST["title"]));
28 if ($title) {
29 db_query($link, "BEGIN");
30
31 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
32 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
33
34 if (db_num_rows($result) == 0) {
35
36 db_query($link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
37 VALUES ('$title', ".$_SESSION["uid"] .")");
38
39 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles WHERE
40 title = '$title'");
41
42 if (db_num_rows($result) != 0) {
43 $profile_id = db_fetch_result($result, 0, "id");
44
45 if ($profile_id) {
46 initialize_user_prefs($link, $_SESSION["uid"], $profile_id);
47 }
48 }
49 }
50
51 db_query($link, "COMMIT");
52 }
53 return;
54 }
55
56 if ($subop == "saveprofile") {
57 $id = db_escape_string($_REQUEST["id"]);
58 $title = db_escape_string(trim($_REQUEST["value"]));
59
60 if ($id == 0) {
61 print __("Default profile");
62 return;
63 }
64
65 if ($title) {
66 db_query($link, "BEGIN");
67
68 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
69 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
70
71 if (db_num_rows($result) == 0) {
72 db_query($link, "UPDATE ttrss_settings_profiles
73 SET title = '$title' WHERE id = '$id' AND
74 owner_uid = " . $_SESSION["uid"]);
75 print $title;
76 } else {
77 $result = db_query($link, "SELECT title FROM ttrss_settings_profiles
78 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
79 print db_fetch_result($result, 0, "title");
80 }
81
82 db_query($link, "COMMIT");
83 }
84 return;
85 }
86
87 if ($subop == "remarchive") {
88 $ids = split(",", db_escape_string($_REQUEST["ids"]));
89
90 print "<rpc-reply>";
91
92 foreach ($ids as $id) {
93 $result = db_query($link, "DELETE FROM ttrss_archived_feeds WHERE
94 (SELECT COUNT(*) FROM ttrss_user_entries
95 WHERE orig_feed_id = '$id') = 0 AND
96 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
97
98 $rc = db_affected_rows($link, $result);
99
100 print "<feed id='$id' rc='$rc'/>";
101
102 }
103
104 print "</rpc-reply>";
105
106 return;
107 }
108
109 if ($subop == "addfeed") {
110
111 $feed = db_escape_string($_REQUEST['feed']);
112 $cat = db_escape_string($_REQUEST['cat']);
113 $login = db_escape_string($_REQUEST['login']);
114 $pass = db_escape_string($_REQUEST['pass']);
115
116 $rc = subscribe_to_feed($link, $feed, $cat, $login, $pass);
117
118 print "<rpc-reply>";
119 print "<result code='$rc'/>";
120 print "</rpc-reply>";
121
122 return;
123
124 }
125
126 if ($subop == "togglepref") {
127 print "<rpc-reply>";
128
129 $key = db_escape_string($_REQUEST["key"]);
130
131 set_pref($link, $key, !get_pref($link, $key));
132
133 $value = get_pref($link, $key);
134
135 print "<param-set key=\"$key\" value=\"$value\"/>";
136
137 print "</rpc-reply>";
138
139 return;
140 }
141
142 if ($subop == "setpref") {
143 print "<rpc-reply>";
144
145 $key = db_escape_string($_REQUEST["key"]);
146 $value = db_escape_string($_REQUEST["value"]);
147
148 set_pref($link, $key, $value);
149
150 print "<param-set key=\"$key\" value=\"$value\"/>";
151
152 print "</rpc-reply>";
153
154 return;
155 }
156
157 if ($subop == "getAllCounters") {
158 print "<rpc-reply>";
159 print "<counters><![CDATA[";
160
161 print json_encode(getAllCounters($link, $_REQUEST['omode']));
162
163 print "]]></counters>";
164 print_runtime_info($link);
165 print "</rpc-reply>";
166
167 return;
168 }
169
170 if ($subop == "mark") {
171 $mark = $_REQUEST["mark"];
172 $id = db_escape_string($_REQUEST["id"]);
173
174 if ($mark == "1") {
175 $mark = "true";
176 } else {
177 $mark = "false";
178 }
179
180 // FIXME this needs collision testing
181
182 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
183 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
184
185 print "<rpc-reply><counters><![CDATA[";
186 print json_encode(getAllCounters($link));
187 print "]]></counters></rpc-reply>";
188
189 return;
190 }
191
192 if ($subop == "delete") {
193 $ids = db_escape_string($_REQUEST["ids"]);
194
195 $result = db_query($link, "DELETE FROM ttrss_user_entries
196 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
197
198 print "<rpc-reply><counters><![CDATA[";
199 print json_encode(getAllCounters($link));
200 print "]]></counters></rpc-reply>";
201
202 return;
203 }
204
205 if ($subop == "unarchive") {
206 $ids = db_escape_string($_REQUEST["ids"]);
207
208 $result = db_query($link, "UPDATE ttrss_user_entries
209 SET feed_id = orig_feed_id, orig_feed_id = NULL
210 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
211
212 print "<rpc-reply><counters><![CDATA[";
213 print json_encode(getAllCounters($link));
214 print "]]></counters></rpc-reply>";
215
216 return;
217 }
218
219 if ($subop == "archive") {
220 $ids = split(",", db_escape_string($_REQUEST["ids"]));
221
222 foreach ($ids as $id) {
223 archive_article($link, $id, $_SESSION["uid"]);
224 }
225
226 print "<rpc-reply><counters><![CDATA[";
227 print json_encode(getAllCounters($link));
228 print "]]></counters></rpc-reply>";
229
230 return;
231 }
232
233
234 if ($subop == "publ") {
235 $pub = $_REQUEST["pub"];
236 $id = db_escape_string($_REQUEST["id"]);
237 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
238
239 if ($pub == "1") {
240 $pub = "true";
241 } else {
242 $pub = "false";
243 }
244
245 if ($note != 'undefined') {
246 $note_qpart = "note = '$note',";
247 }
248
249 // FIXME this needs collision testing
250
251 $result = db_query($link, "UPDATE ttrss_user_entries SET
252 $note_qpart
253 published = $pub
254 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
255
256
257 print "<rpc-reply>";
258
259 print "<counters><![CDATA[";
260 print json_encode(getAllCounters($link));
261 print "]]></counters>";
262
263 if ($note != 'undefined') {
264 $note_size = strlen($note);
265 print "<note id=\"$id\" size=\"$note_size\">";
266 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
267 print "</note>";
268 }
269
270 print "</rpc-reply>";
271
272 return;
273 }
274
275 if ($subop == "updateFeed") {
276 $feed_id = db_escape_string($_REQUEST["feed"]);
277
278 update_rss_feed($link, $feed_id);
279
280 print "<rpc-reply>";
281 print "<counters><![CDATA[";
282 print json_encode(getFeedCounters($link, $feed_id));
283 print "]]></counters>";
284 print "</rpc-reply>";
285
286 return;
287 }
288
289 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
290
291 $global_unread_caller = sprintf("%d", $_REQUEST["uctr"]);
292 $global_unread = getGlobalUnread($link);
293
294 print "<rpc-reply>";
295
296 if ($global_unread_caller != $global_unread) {
297 print "<counters><![CDATA[";
298 $omode = $_REQUEST["omode"];
299 print json_encode(getAllCounters($link, $omode));
300 print "]]></counters>";
301 }
302
303 print_runtime_info($link);
304
305 print "</rpc-reply>";
306
307 return;
308 }
309
310 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
311 if ($subop == "catchupSelected") {
312
313 $ids = split(",", db_escape_string($_REQUEST["ids"]));
314 $cmode = sprintf("%d", $_REQUEST["cmode"]);
315
316 catchupArticlesById($link, $ids, $cmode);
317
318 print "<rpc-reply>";
319 print "<counters><![CDATA[";
320 print json_encode(getAllCounters($link, $_REQUEST['omode']));
321 print "]]></counters>";
322
323 print_runtime_info($link);
324 print "</rpc-reply>";
325
326 return;
327 }
328
329 if ($subop == "markSelected") {
330
331 $ids = split(",", db_escape_string($_REQUEST["ids"]));
332 $cmode = sprintf("%d", $_REQUEST["cmode"]);
333
334 markArticlesById($link, $ids, $cmode);
335
336 print "<rpc-reply>";
337 print "<counters><![CDATA[";
338 print json_encode(getAllCounters($link, $_REQUEST['omode']));
339 print "]]></counters>";
340 print_runtime_info($link);
341 print "</rpc-reply>";
342
343 return;
344 }
345
346 if ($subop == "publishSelected") {
347
348 $ids = split(",", db_escape_string($_REQUEST["ids"]));
349 $cmode = sprintf("%d", $_REQUEST["cmode"]);
350
351 publishArticlesById($link, $ids, $cmode);
352
353 print "<rpc-reply>";
354 print "<counters><![CDATA[";
355 print json_encode(getAllCounters($link, $_REQUEST['omode']));
356 print "]]></counters>";
357 print_runtime_info($link);
358 print "</rpc-reply>";
359
360 return;
361 }
362
363 if ($subop == "sanityCheck") {
364 print "<rpc-reply>";
365 if (sanity_check($link)) {
366 print "<error error-code=\"0\"/>";
367 print_init_params($link);
368 print_runtime_info($link);
369
370 # assign client-passed params to session
371 $_SESSION["client.userAgent"] = $_REQUEST["ua"];
372
373 }
374 print "</rpc-reply>";
375
376 return;
377 }
378
379 if ($subop == "globalPurge") {
380
381 print "<rpc-reply>";
382 global_purge_old_posts($link, true);
383 print "</rpc-reply>";
384
385 return;
386 }
387
388 if ($subop == "getArticleLink") {
389
390 $id = db_escape_string($_REQUEST["id"]);
391
392 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
393 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
394
395 if (db_num_rows($result) == 1) {
396 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
397 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
398 } else {
399 print "<rpc-reply><error>Article not found</error></rpc-reply>";
400 }
401
402 return;
403 }
404
405 if ($subop == "setArticleTags") {
406
407 global $memcache;
408
409 $id = db_escape_string($_REQUEST["id"]);
410
411 $tags_str = db_escape_string($_REQUEST["tags_str"]);
412
413 $tags = array_unique(trim_array(split(",", $tags_str)));
414
415 db_query($link, "BEGIN");
416
417 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
418 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
419
420 if (db_num_rows($result) == 1) {
421
422 $int_id = db_fetch_result($result, 0, "int_id");
423
424 db_query($link, "DELETE FROM ttrss_tags WHERE
425 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
426
427 foreach ($tags as $tag) {
428 $tag = sanitize_tag($tag);
429
430 if (!tag_is_valid($tag)) {
431 continue;
432 }
433
434 if (preg_match("/^[0-9]*$/", $tag)) {
435 continue;
436 }
437
438 // print "<!-- $id : $int_id : $tag -->";
439
440 if ($tag != '') {
441 db_query($link, "INSERT INTO ttrss_tags
442 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
443 }
444 }
445 }
446
447 db_query($link, "COMMIT");
448
449 if ($memcache) {
450 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
451 $memcache->delete($obj_id);
452 }
453
454 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
455
456 print "<rpc-reply>
457 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
458 </rpc-reply>";
459
460 return;
461 }
462
463 if ($subop == "regenPubKey") {
464
465 print "<rpc-reply>";
466
467 set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key(), $_SESSION["uid"]);
468
469 $new_link = article_publish_url($link);
470
471 print "<link><![CDATA[$new_link]]></link>";
472
473 print "</rpc-reply>";
474
475 return;
476 }
477
478 if ($subop == "regenOPMLKey") {
479
480 print "<rpc-reply>";
481 set_pref($link, " _PREFS_OPML_PUBLISH_KEY", generate_publish_key(), $_SESSION["uid"]);
482 $new_link = opml_publish_url($link);
483 print "<link><![CDATA[$new_link]]></link>";
484 print "</rpc-reply>";
485 return;
486 }
487
488 if ($subop == "logout") {
489 logout_user();
490 print_error_xml(6);
491 return;
492 }
493
494 if ($subop == "completeTags") {
495
496 $search = db_escape_string($_REQUEST["search"]);
497
498 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
499 WHERE owner_uid = '".$_SESSION["uid"]."' AND
500 tag_name LIKE '$search%' ORDER BY tag_name
501 LIMIT 10");
502
503 print "<ul>";
504 while ($line = db_fetch_assoc($result)) {
505 print "<li>" . $line["tag_name"] . "</li>";
506 }
507 print "</ul>";
508
509 return;
510 }
511
512 if ($subop == "purge") {
513 $ids = split(",", db_escape_string($_REQUEST["ids"]));
514 $days = sprintf("%d", $_REQUEST["days"]);
515
516 print "<rpc-reply>";
517
518 print "<message><![CDATA[";
519
520 foreach ($ids as $id) {
521
522 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
523 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
524
525 if (db_num_rows($result) == 1) {
526 purge_feed($link, $id, $days, true);
527 }
528 }
529
530 print "]]></message>";
531
532 print "</rpc-reply>";
533
534 return;
535 }
536
537 /* if ($subop == "setScore") {
538 $id = db_escape_string($_REQUEST["id"]);
539 $score = sprintf("%d", $_REQUEST["score"]);
540
541 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
542 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
543
544 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
545
546 return;
547
548 } */
549
550 if ($subop == "getArticles") {
551 $ids = split(",", db_escape_string($_REQUEST["ids"]));
552
553 print "<rpc-reply>";
554
555 foreach ($ids as $id) {
556 if ($id) {
557 outputArticleXML($link, $id, 0, false);
558 }
559 }
560 print "</rpc-reply>";
561
562 return;
563 }
564
565 if ($subop == "checkDate") {
566
567 $date = db_escape_string($_REQUEST["date"]);
568 $date_parsed = strtotime($date);
569
570 print "<rpc-reply>";
571
572 if ($date_parsed) {
573 print "<result>1</result>";
574 } else {
575 print "<result>0</result>";
576 }
577
578 print "</rpc-reply>";
579
580 return;
581 }
582
583 if ($subop == "removeFromLabel") {
584
585 $ids = split(",", db_escape_string($_REQUEST["ids"]));
586 $label_id = db_escape_string($_REQUEST["lid"]);
587
588 $label = db_escape_string(label_find_caption($link, $label_id,
589 $_SESSION["uid"]));
590
591 print "<rpc-reply>";
592 print "<info-for-headlines>";
593
594 if ($label) {
595
596 foreach ($ids as $id) {
597 label_remove_article($link, $id, $label, $_SESSION["uid"]);
598
599 print "<entry id=\"$id\"><![CDATA[";
600
601 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
602 print format_article_labels($labels, $id);
603
604 print "]]></entry>";
605
606 }
607 }
608
609 print "</info-for-headlines>";
610
611 print "<counters><![CDATA[";
612 print json_encode(getAllCounters($link, $_REQUEST['omode']));
613 print "]]></counters>";
614 print "</rpc-reply>";
615
616 return;
617 }
618
619 if ($subop == "assignToLabel") {
620
621 $ids = split(",", db_escape_string($_REQUEST["ids"]));
622 $label_id = db_escape_string($_REQUEST["lid"]);
623
624 $label = db_escape_string(label_find_caption($link, $label_id,
625 $_SESSION["uid"]));
626
627 print "<rpc-reply>";
628
629 print "<info-for-headlines>";
630
631 if ($label) {
632
633 foreach ($ids as $id) {
634 label_add_article($link, $id, $label, $_SESSION["uid"]);
635
636 print "<entry id=\"$id\"><![CDATA[";
637
638 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
639 print format_article_labels($labels, $id);
640
641 print "]]></entry>";
642
643 }
644 }
645
646 print "</info-for-headlines>";
647
648 print "<counters><![CDATA[";
649 print json_encode(getAllCounters($link, $_REQUEST['omode']));
650 print "]]></counters>";
651 print "</rpc-reply>";
652
653 return;
654 }
655
656 if ($subop == "updateFeedBrowser") {
657
658 $search = db_escape_string($_REQUEST["search"]);
659 $limit = db_escape_string($_REQUEST["limit"]);
660 $mode = db_escape_string($_REQUEST["mode"]);
661
662 print "<rpc-reply>";
663 print "<content>";
664 print "<![CDATA[";
665 $ctr = print_feed_browser($link, $search, $limit, $mode);
666 print "]]>";
667 print "</content>";
668 print "<num-results value=\"$ctr\"/>";
669 print "<mode value=\"$mode\"/>";
670 print "</rpc-reply>";
671
672 return;
673 }
674
675
676 if ($subop == "massSubscribe") {
677
678 $ids = split(",", db_escape_string($_REQUEST["ids"]));
679 $mode = $_REQUEST["mode"];
680
681 $subscribed = array();
682
683 foreach ($ids as $id) {
684
685 if ($mode == 1) {
686 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
687 WHERE id = '$id'");
688 } else if ($mode == 2) {
689 $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
690 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
691 $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
692 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
693 }
694
695 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
696 $title = db_escape_string(db_fetch_result($result, 0, "title"));
697
698 $title_orig = db_fetch_result($result, 0, "title");
699
700 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
701 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
702
703 if (db_num_rows($result) == 0) {
704 if ($mode == 1) {
705 $result = db_query($link,
706 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
707 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
708 } else if ($mode == 2) {
709 $result = db_query($link,
710 "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
711 VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
712 }
713 array_push($subscribed, $title_orig);
714 }
715 }
716
717 $num_feeds = count($subscribed);
718
719 print "<rpc-reply>";
720 print "<num-feeds value='$num_feeds'/>";
721 print "</rpc-reply>";
722
723 return;
724 }
725
726 if ($subop == "download") {
727 $stage = (int) $_REQUEST["stage"];
728 $cidt = (int)db_escape_string($_REQUEST["cidt"]);
729 $cidb = (int)db_escape_string($_REQUEST["cidb"]);
730 $sync = db_escape_string($_REQUEST["sync"]);
731 //$amount = (int) $_REQUEST["amount"];
732 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
733 //if (!$amount) $amount = 50;
734
735 /* Amount is not used by the frontend offline.js anymore, it goes by
736 * date_qpart below + cidb/cidt IDs */
737
738 $amount = 2000;
739 $unread_only = true;
740
741 print "<rpc-reply>";
742
743 $sync = split(";", $sync);
744
745 print "<sync>";
746
747 if (count($sync) > 0) {
748 if (strtotime($sync[0])) {
749 $last_online = db_escape_string($sync[0]);
750
751 print "<sync-point><![CDATA[$last_online]]></sync-point>";
752
753 for ($i = 1; $i < count($sync); $i++) {
754 $e = split(",", $sync[$i]);
755
756 if (count($e) == 3) {
757
758 $id = (int) $e[0];
759 $unread = bool_to_sql_bool((bool) $e[1]);
760 $marked = (bool)$e[2];
761
762 if ($marked) {
763 $marked = bool_to_sql_bool($marked);
764 $marked_qpart = "marked = $marked,";
765 }
766
767 $query = "UPDATE ttrss_user_entries SET
768 $marked_qpart
769 unread = $unread,
770 last_read = '$last_online'
771 WHERE ref_id = '$id' AND
772 (last_read IS NULL OR last_read < '$last_online') AND
773 owner_uid = ".$_SESSION["uid"];
774
775 $result = db_query($link, $query);
776
777 print "<sync-ok id=\"$id\"/>";
778
779 }
780 }
781
782 /* Maybe we need to further update local DB for this client */
783
784 $query = "SELECT ref_id,unread,marked FROM ttrss_user_entries
785 WHERE last_read >= '$last_online' AND
786 owner_uid = ".$_SESSION["uid"] . " LIMIT 1000";
787
788 $result = db_query($link, $query);
789
790 while ($line = db_fetch_assoc($result)) {
791 $unread = (int) sql_bool_to_bool($line["unread"]);
792 $marked = (int) sql_bool_to_bool($line["marked"]);
793
794 print "<sync-ok unread=\"$unread\" marked=\"$marked\"
795 id=\"".$line["ref_id"]."\"/>";
796 }
797
798 }
799 }
800
801 print "</sync>";
802
803 if ($stage == 0) {
804 print "<feeds>";
805
806 $result = db_query($link, "SELECT id, title, cat_id FROM
807 ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
808
809 while ($line = db_fetch_assoc($result)) {
810
811 $has_icon = (int) feed_has_icon($line["id"]);
812
813 print "<feed has_icon=\"$has_icon\"
814 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
815 print $line["title"];
816 print "]]></feed>";
817 }
818
819 print "</feeds>";
820
821 print "<feed-categories>";
822
823 $result = db_query($link, "SELECT id, title, collapsed FROM
824 ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
825
826 print "<category id=\"0\" collapsed=\"".
827 (int)get_pref($link, "_COLLAPSED_UNCAT")."\"><![CDATA[";
828 print __("Uncategorized");
829 print "]]></category>";
830
831 print "<category id=\"-1\" collapsed=\"".
832 (int)get_pref($link, "_COLLAPSED_SPECIAL")."\"><![CDATA[";
833 print __("Special");
834 print "]]></category>";
835
836 print "<category id=\"-2\" collapsed=\"".
837 (int)get_pref($link, "_COLLAPSED_LABELS")."\"><![CDATA[";
838 print __("Labels");
839 print "]]></category>";
840
841 while ($line = db_fetch_assoc($result)) {
842 print "<category
843 id=\"".$line["id"]."\"
844 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
845 print $line["title"];
846 print "]]></category>";
847 }
848
849 print "</feed-categories>";
850
851 print "<labels>";
852
853 $result = db_query($link, "SELECT * FROM
854 ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
855
856 while ($line = db_fetch_assoc($result)) {
857 print "<label
858 id=\"".$line["id"]."\"
859 fg_color=\"".$line["fg_color"]."\"
860 bg_color=\"".$line["bg_color"]."\"
861 ><![CDATA[";
862 print $line["caption"];
863 print "]]></label>";
864 }
865
866
867 print "</labels>";
868
869 }
870
871 if ($stage > 0) {
872 print "<articles>";
873
874 $limit = 10;
875 $skip = $limit*($stage-1);
876
877 print "<limit value=\"$limit\"/>";
878
879 if ($amount > 0) $amount -= $skip;
880
881 if ($amount > 0) {
882
883 $limit = min($limit, $amount);
884
885 if ($unread_only) {
886 $unread_qpart = "(unread = true OR marked = true) AND ";
887 }
888
889 if ($cidt && $cidb) {
890 $cid_qpart = "(ttrss_entries.id > $cidt OR ttrss_entries.id < $cidb) AND ";
891 }
892
893 if (DB_TYPE == "pgsql") {
894 $date_qpart = "updated >= NOW() - INTERVAL '1 week' AND";
895 } else {
896 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 WEEK) AND";
897 }
898
899 $result = db_query($link,
900 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
901 guid,link,comments,
902 feed_id,content,updated,unread,marked FROM
903 ttrss_user_entries,ttrss_entries,ttrss_feeds
904 WHERE $unread_qpart $cid_qpart $date_qpart
905 ttrss_feeds.id = feed_id AND
906 ref_id = ttrss_entries.id AND
907 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
908 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
909
910 if (function_exists('json_encode')) {
911
912 while ($line = db_fetch_assoc($result)) {
913 print "<article><![CDATA[";
914
915 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
916 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
917
918 $line["labels"] = get_article_labels($link, $line["id"]);
919
920 // too slow :(
921 // $line["tags"] = format_tags_string(
922 // get_article_tags($link, $line["id"]), $line["id"]);
923
924 print json_encode($line);
925 print "]]></article>";
926 }
927 }
928
929 }
930
931 print "</articles>";
932
933 }
934
935 print "</rpc-reply>";
936
937 return;
938 }
939
940 if ($subop == "digest-get-contents") {
941 $article_id = db_escape_string($_REQUEST['article_id']);
942
943 $result = db_query($link, "SELECT content
944 FROM ttrss_entries, ttrss_user_entries
945 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
946
947 print "<rpc-reply>";
948
949 print "<article id=\"$article_id\"><![CDATA[";
950
951 $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
952
953 print $content;
954
955 print "]]></article>";
956
957 print "</rpc-reply>";
958
959 return;
960 }
961
962 if ($subop == "digest-update") {
963 $feed_id = db_escape_string($_REQUEST['feed_id']);
964 $offset = db_escape_string($_REQUEST['offset']);
965 $seq = db_escape_string($_REQUEST['seq']);
966
967 if (!$feed_id) $feed_id = -4;
968 if (!$offset) $offset = 0;
969 print "<rpc-reply>";
970
971 print "<seq>$seq</seq>";
972
973 $headlines = api_get_headlines($link, $feed_id, 10, $offset,
974 '', ($feed_id == -4), true, false, "unread", "updated DESC");
975
976 //function api_get_headlines($link, $feed_id, $limit, $offset,
977 // $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
978
979 print "<headlines-title><![CDATA[" . getFeedTitle($link, $feed_id) .
980 "]]></headlines-title>";
981
982 print "<headlines><![CDATA[" . json_encode($headlines) . "]]></headlines>";
983
984 print "</rpc-reply>";
985 return;
986 }
987
988 if ($subop == "digest-init") {
989 print "<rpc-reply>";
990
991 $tmp_feeds = api_get_feeds($link, false, true, false, 0);
992 $feeds = array();
993
994 foreach ($tmp_feeds as $f) {
995 if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
996 }
997
998 print "<feeds><![CDATA[" . json_encode($feeds) . "]]></feeds>";
999
1000 print "</rpc-reply>";
1001 return;
1002 }
1003
1004 if ($subop == "catchupFeed") {
1005
1006 $feed_id = db_escape_string($_REQUEST['feed_id']);
1007 $is_cat = db_escape_string($_REQUEST['is_cat']);
1008
1009 print "<rpc-reply>";
1010
1011 catchup_feed($link, $feed_id, $is_cat);
1012
1013 print "</rpc-reply>";
1014
1015 return;
1016 }
1017
1018 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
1019 }
1020 ?>