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