]> git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
properly handle on-the-fly adding of categories
[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 == "extractfeedurls") {
127 print "<rpc-reply>";
128
129 $urls = get_feeds_from_html($_REQUEST['url']);
130 print "<urls><![CDATA[" . json_encode($urls) . "]]></urls>";
131
132 print "</rpc-reply>";
133 return;
134 }
135
136 if ($subop == "togglepref") {
137 print "<rpc-reply>";
138
139 $key = db_escape_string($_REQUEST["key"]);
140
141 set_pref($link, $key, !get_pref($link, $key));
142
143 $value = get_pref($link, $key);
144
145 print "<param-set key=\"$key\" value=\"$value\"/>";
146
147 print "</rpc-reply>";
148
149 return;
150 }
151
152 if ($subop == "setpref") {
153 print "<rpc-reply>";
154
155 $key = db_escape_string($_REQUEST["key"]);
156 $value = db_escape_string($_REQUEST["value"]);
157
158 set_pref($link, $key, $value);
159
160 print "<param-set key=\"$key\" value=\"$value\"/>";
161
162 print "</rpc-reply>";
163
164 return;
165 }
166
167 if ($subop == "getAllCounters") {
168 print "<rpc-reply>";
169 print "<counters><![CDATA[";
170
171 print json_encode(getAllCounters($link, $_REQUEST['omode']));
172
173 print "]]></counters>";
174 print_runtime_info($link);
175 print "</rpc-reply>";
176
177 return;
178 }
179
180 if ($subop == "mark") {
181 $mark = $_REQUEST["mark"];
182 $id = db_escape_string($_REQUEST["id"]);
183
184 if ($mark == "1") {
185 $mark = "true";
186 } else {
187 $mark = "false";
188 }
189
190 // FIXME this needs collision testing
191
192 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
193 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
194
195 print "<rpc-reply><counters><![CDATA[";
196 print json_encode(getAllCounters($link));
197 print "]]></counters></rpc-reply>";
198
199 return;
200 }
201
202 if ($subop == "delete") {
203 $ids = db_escape_string($_REQUEST["ids"]);
204
205 $result = db_query($link, "DELETE FROM ttrss_user_entries
206 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
207
208 print "<rpc-reply><counters><![CDATA[";
209 print json_encode(getAllCounters($link));
210 print "]]></counters></rpc-reply>";
211
212 return;
213 }
214
215 if ($subop == "unarchive") {
216 $ids = db_escape_string($_REQUEST["ids"]);
217
218 $result = db_query($link, "UPDATE ttrss_user_entries
219 SET feed_id = orig_feed_id, orig_feed_id = NULL
220 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
221
222 print "<rpc-reply><counters><![CDATA[";
223 print json_encode(getAllCounters($link));
224 print "]]></counters></rpc-reply>";
225
226 return;
227 }
228
229 if ($subop == "archive") {
230 $ids = split(",", db_escape_string($_REQUEST["ids"]));
231
232 foreach ($ids as $id) {
233 archive_article($link, $id, $_SESSION["uid"]);
234 }
235
236 print "<rpc-reply><counters><![CDATA[";
237 print json_encode(getAllCounters($link));
238 print "]]></counters></rpc-reply>";
239
240 return;
241 }
242
243
244 if ($subop == "publ") {
245 $pub = $_REQUEST["pub"];
246 $id = db_escape_string($_REQUEST["id"]);
247 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
248
249 if ($pub == "1") {
250 $pub = "true";
251 } else {
252 $pub = "false";
253 }
254
255 if ($note != 'undefined') {
256 $note_qpart = "note = '$note',";
257 }
258
259 // FIXME this needs collision testing
260
261 $result = db_query($link, "UPDATE ttrss_user_entries SET
262 $note_qpart
263 published = $pub
264 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
265
266
267 print "<rpc-reply>";
268
269 print "<counters><![CDATA[";
270 print json_encode(getAllCounters($link));
271 print "]]></counters>";
272
273 if ($note != 'undefined') {
274 $note_size = strlen($note);
275 print "<note id=\"$id\" size=\"$note_size\">";
276 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
277 print "</note>";
278 }
279
280 print "</rpc-reply>";
281
282 return;
283 }
284
285 if ($subop == "updateFeed") {
286 $feed_id = db_escape_string($_REQUEST["feed"]);
287
288 update_rss_feed($link, $feed_id);
289
290 print "<rpc-reply>";
291 print "<counters><![CDATA[";
292 print json_encode(getFeedCounters($link, $feed_id));
293 print "]]></counters>";
294 print "</rpc-reply>";
295
296 return;
297 }
298
299 if ($subop == "updateAllFeeds") {
300
301 $global_unread_caller = sprintf("%d", $_REQUEST["uctr"]);
302 $global_unread = getGlobalUnread($link);
303
304 print "<rpc-reply>";
305
306 if ($global_unread_caller != $global_unread) {
307 print "<counters><![CDATA[";
308 $omode = $_REQUEST["omode"];
309 print json_encode(getAllCounters($link, $omode));
310 print "]]></counters>";
311 }
312
313 print_runtime_info($link);
314
315 print "</rpc-reply>";
316
317 return;
318 }
319
320 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
321 if ($subop == "catchupSelected") {
322
323 $ids = split(",", db_escape_string($_REQUEST["ids"]));
324 $cmode = sprintf("%d", $_REQUEST["cmode"]);
325
326 catchupArticlesById($link, $ids, $cmode);
327
328 print "<rpc-reply>";
329 print "<counters><![CDATA[";
330 print json_encode(getAllCounters($link, $_REQUEST['omode']));
331 print "]]></counters>";
332
333 print_runtime_info($link);
334 print "</rpc-reply>";
335
336 return;
337 }
338
339 if ($subop == "markSelected") {
340
341 $ids = split(",", db_escape_string($_REQUEST["ids"]));
342 $cmode = sprintf("%d", $_REQUEST["cmode"]);
343
344 markArticlesById($link, $ids, $cmode);
345
346 print "<rpc-reply>";
347 print "<counters><![CDATA[";
348 print json_encode(getAllCounters($link, $_REQUEST['omode']));
349 print "]]></counters>";
350 print_runtime_info($link);
351 print "</rpc-reply>";
352
353 return;
354 }
355
356 if ($subop == "publishSelected") {
357
358 $ids = split(",", db_escape_string($_REQUEST["ids"]));
359 $cmode = sprintf("%d", $_REQUEST["cmode"]);
360
361 publishArticlesById($link, $ids, $cmode);
362
363 print "<rpc-reply>";
364 print "<counters><![CDATA[";
365 print json_encode(getAllCounters($link, $_REQUEST['omode']));
366 print "]]></counters>";
367 print_runtime_info($link);
368 print "</rpc-reply>";
369
370 return;
371 }
372
373 if ($subop == "sanityCheck") {
374 print "<rpc-reply>";
375 if (sanity_check($link)) {
376 print "<error error-code=\"0\"/>";
377
378 print "<init-params><![CDATA[";
379 print json_encode(make_init_params($link));
380 print "]]></init-params>";
381
382 print_runtime_info($link);
383
384 # assign client-passed params to session
385 $_SESSION["client.userAgent"] = $_REQUEST["ua"];
386
387 }
388 print "</rpc-reply>";
389
390 return;
391 }
392
393 if ($subop == "globalPurge") {
394
395 print "<rpc-reply>";
396 global_purge_old_posts($link, true);
397 print "</rpc-reply>";
398
399 return;
400 }
401
402 if ($subop == "getArticleLink") {
403
404 $id = db_escape_string($_REQUEST["id"]);
405
406 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
407 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
408
409 if (db_num_rows($result) == 1) {
410 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
411 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
412 } else {
413 print "<rpc-reply><error>Article not found</error></rpc-reply>";
414 }
415
416 return;
417 }
418
419 if ($subop == "setArticleTags") {
420
421 global $memcache;
422
423 $id = db_escape_string($_REQUEST["id"]);
424
425 $tags_str = db_escape_string($_REQUEST["tags_str"]);
426
427 $tags = array_unique(trim_array(split(",", $tags_str)));
428
429 db_query($link, "BEGIN");
430
431 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
432 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
433
434 if (db_num_rows($result) == 1) {
435
436 $int_id = db_fetch_result($result, 0, "int_id");
437
438 db_query($link, "DELETE FROM ttrss_tags WHERE
439 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
440
441 foreach ($tags as $tag) {
442 $tag = sanitize_tag($tag);
443
444 if (!tag_is_valid($tag)) {
445 continue;
446 }
447
448 if (preg_match("/^[0-9]*$/", $tag)) {
449 continue;
450 }
451
452 // print "<!-- $id : $int_id : $tag -->";
453
454 if ($tag != '') {
455 db_query($link, "INSERT INTO ttrss_tags
456 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
457 }
458 }
459 }
460
461 db_query($link, "COMMIT");
462
463 if ($memcache) {
464 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
465 $memcache->delete($obj_id);
466 }
467
468 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
469
470 print "<rpc-reply>
471 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
472 </rpc-reply>";
473
474 return;
475 }
476
477 if ($subop == "regenOPMLKey") {
478
479 print "<rpc-reply>";
480
481 update_feed_access_key($link, 'OPML:Publish',
482 false, $_SESSION["uid"]);
483
484 $new_link = opml_publish_url($link);
485 print "<link><![CDATA[$new_link]]></link>";
486 print "</rpc-reply>";
487 return;
488 }
489
490 if ($subop == "logout") {
491 logout_user();
492 print_error_xml(6);
493 return;
494 }
495
496 if ($subop == "completeTags") {
497
498 $search = db_escape_string($_REQUEST["search"]);
499
500 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
501 WHERE owner_uid = '".$_SESSION["uid"]."' AND
502 tag_name LIKE '$search%' ORDER BY tag_name
503 LIMIT 10");
504
505 print "<ul>";
506 while ($line = db_fetch_assoc($result)) {
507 print "<li>" . $line["tag_name"] . "</li>";
508 }
509 print "</ul>";
510
511 return;
512 }
513
514 if ($subop == "purge") {
515 $ids = split(",", db_escape_string($_REQUEST["ids"]));
516 $days = sprintf("%d", $_REQUEST["days"]);
517
518 print "<rpc-reply>";
519
520 print "<message><![CDATA[";
521
522 foreach ($ids as $id) {
523
524 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
525 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
526
527 if (db_num_rows($result) == 1) {
528 purge_feed($link, $id, $days, true);
529 }
530 }
531
532 print "]]></message>";
533
534 print "</rpc-reply>";
535
536 return;
537 }
538
539 /* if ($subop == "setScore") {
540 $id = db_escape_string($_REQUEST["id"]);
541 $score = sprintf("%d", $_REQUEST["score"]);
542
543 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
544 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
545
546 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
547
548 return;
549
550 } */
551
552 if ($subop == "getArticles") {
553 $ids = split(",", db_escape_string($_REQUEST["ids"]));
554
555 print "<rpc-reply>";
556
557 foreach ($ids as $id) {
558 if ($id) {
559 outputArticleXML($link, $id, 0, false);
560 }
561 }
562 print "</rpc-reply>";
563
564 return;
565 }
566
567 if ($subop == "checkDate") {
568
569 $date = db_escape_string($_REQUEST["date"]);
570 $date_parsed = strtotime($date);
571
572 print "<rpc-reply>";
573
574 if ($date_parsed) {
575 print "<result>1</result>";
576 } else {
577 print "<result>0</result>";
578 }
579
580 print "</rpc-reply>";
581
582 return;
583 }
584
585 if ($subop == "removeFromLabel") {
586
587 $ids = split(",", db_escape_string($_REQUEST["ids"]));
588 $label_id = db_escape_string($_REQUEST["lid"]);
589
590 $label = db_escape_string(label_find_caption($link, $label_id,
591 $_SESSION["uid"]));
592
593 print "<rpc-reply>";
594 print "<info-for-headlines>";
595
596 if ($label) {
597
598 foreach ($ids as $id) {
599 label_remove_article($link, $id, $label, $_SESSION["uid"]);
600
601 print "<entry id=\"$id\"><![CDATA[";
602
603 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
604 print format_article_labels($labels, $id);
605
606 print "]]></entry>";
607
608 }
609 }
610
611 print "</info-for-headlines>";
612
613 print "<counters><![CDATA[";
614 print json_encode(getAllCounters($link, $_REQUEST['omode']));
615 print "]]></counters>";
616 print "</rpc-reply>";
617
618 return;
619 }
620
621 if ($subop == "assignToLabel") {
622
623 $ids = split(",", db_escape_string($_REQUEST["ids"]));
624 $label_id = db_escape_string($_REQUEST["lid"]);
625
626 $label = db_escape_string(label_find_caption($link, $label_id,
627 $_SESSION["uid"]));
628
629 print "<rpc-reply>";
630
631 print "<info-for-headlines>";
632
633 if ($label) {
634
635 foreach ($ids as $id) {
636 label_add_article($link, $id, $label, $_SESSION["uid"]);
637
638 print "<entry id=\"$id\"><![CDATA[";
639
640 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
641 print format_article_labels($labels, $id);
642
643 print "]]></entry>";
644
645 }
646 }
647
648 print "</info-for-headlines>";
649
650 print "<counters><![CDATA[";
651 print json_encode(getAllCounters($link, $_REQUEST['omode']));
652 print "]]></counters>";
653 print "</rpc-reply>";
654
655 return;
656 }
657
658 if ($subop == "updateFeedBrowser") {
659
660 $search = db_escape_string($_REQUEST["search"]);
661 $limit = db_escape_string($_REQUEST["limit"]);
662 $mode = db_escape_string($_REQUEST["mode"]);
663
664 print "<rpc-reply>";
665 print "<content>";
666 print "<![CDATA[";
667 $ctr = print_feed_browser($link, $search, $limit, $mode);
668 print "]]>";
669 print "</content>";
670 print "<num-results value=\"$ctr\"/>";
671 print "<mode value=\"$mode\"/>";
672 print "</rpc-reply>";
673
674 return;
675 }
676
677
678 if ($subop == "massSubscribe") {
679
680 $ids = split(",", db_escape_string($_REQUEST["ids"]));
681 $mode = $_REQUEST["mode"];
682
683 $subscribed = array();
684
685 foreach ($ids as $id) {
686
687 if ($mode == 1) {
688 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
689 WHERE id = '$id'");
690 } else if ($mode == 2) {
691 $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
692 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
693 $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
694 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
695 }
696
697 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
698 $title = db_escape_string(db_fetch_result($result, 0, "title"));
699
700 $title_orig = db_fetch_result($result, 0, "title");
701
702 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
703 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
704
705 if (db_num_rows($result) == 0) {
706 if ($mode == 1) {
707 $result = db_query($link,
708 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
709 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
710 } else if ($mode == 2) {
711 $result = db_query($link,
712 "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
713 VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
714 }
715 array_push($subscribed, $title_orig);
716 }
717 }
718
719 $num_feeds = count($subscribed);
720
721 print "<rpc-reply>";
722 print "<num-feeds value='$num_feeds'/>";
723 print "</rpc-reply>";
724
725 return;
726 }
727
728 if ($subop == "download") {
729 $stage = (int) $_REQUEST["stage"];
730 $cidt = (int)db_escape_string($_REQUEST["cidt"]);
731 $cidb = (int)db_escape_string($_REQUEST["cidb"]);
732 $sync = db_escape_string($_REQUEST["sync"]);
733 //$amount = (int) $_REQUEST["amount"];
734 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
735 //if (!$amount) $amount = 50;
736
737 /* Amount is not used by the frontend offline.js anymore, it goes by
738 * date_qpart below + cidb/cidt IDs */
739
740 $amount = 2000;
741 $unread_only = true;
742
743 print "<rpc-reply>";
744
745 $sync = split(";", $sync);
746
747 print "<sync>";
748
749 if (count($sync) > 0) {
750 if (strtotime($sync[0])) {
751 $last_online = db_escape_string($sync[0]);
752
753 print "<sync-point><![CDATA[$last_online]]></sync-point>";
754
755 for ($i = 1; $i < count($sync); $i++) {
756 $e = split(",", $sync[$i]);
757
758 if (count($e) == 3) {
759
760 $id = (int) $e[0];
761 $unread = bool_to_sql_bool((bool) $e[1]);
762 $marked = (bool)$e[2];
763
764 if ($marked) {
765 $marked = bool_to_sql_bool($marked);
766 $marked_qpart = "marked = $marked,";
767 }
768
769 $query = "UPDATE ttrss_user_entries SET
770 $marked_qpart
771 unread = $unread,
772 last_read = '$last_online'
773 WHERE ref_id = '$id' AND
774 (last_read IS NULL OR last_read < '$last_online') AND
775 owner_uid = ".$_SESSION["uid"];
776
777 $result = db_query($link, $query);
778
779 print "<sync-ok id=\"$id\"/>";
780
781 }
782 }
783
784 /* Maybe we need to further update local DB for this client */
785
786 $query = "SELECT ref_id,unread,marked FROM ttrss_user_entries
787 WHERE last_read >= '$last_online' AND
788 owner_uid = ".$_SESSION["uid"] . " LIMIT 1000";
789
790 $result = db_query($link, $query);
791
792 while ($line = db_fetch_assoc($result)) {
793 $unread = (int) sql_bool_to_bool($line["unread"]);
794 $marked = (int) sql_bool_to_bool($line["marked"]);
795
796 print "<sync-ok unread=\"$unread\" marked=\"$marked\"
797 id=\"".$line["ref_id"]."\"/>";
798 }
799
800 }
801 }
802
803 print "</sync>";
804
805 if ($stage == 0) {
806 print "<feeds>";
807
808 $result = db_query($link, "SELECT id, title, cat_id FROM
809 ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
810
811 while ($line = db_fetch_assoc($result)) {
812
813 $has_icon = (int) feed_has_icon($line["id"]);
814
815 print "<feed has_icon=\"$has_icon\"
816 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
817 print $line["title"];
818 print "]]></feed>";
819 }
820
821 print "</feeds>";
822
823 print "<feed-categories>";
824
825 $result = db_query($link, "SELECT id, title, collapsed FROM
826 ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
827
828 print "<category id=\"0\" collapsed=\"".
829 (int)get_pref($link, "_COLLAPSED_UNCAT")."\"><![CDATA[";
830 print __("Uncategorized");
831 print "]]></category>";
832
833 print "<category id=\"-1\" collapsed=\"".
834 (int)get_pref($link, "_COLLAPSED_SPECIAL")."\"><![CDATA[";
835 print __("Special");
836 print "]]></category>";
837
838 print "<category id=\"-2\" collapsed=\"".
839 (int)get_pref($link, "_COLLAPSED_LABELS")."\"><![CDATA[";
840 print __("Labels");
841 print "]]></category>";
842
843 while ($line = db_fetch_assoc($result)) {
844 print "<category
845 id=\"".$line["id"]."\"
846 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
847 print $line["title"];
848 print "]]></category>";
849 }
850
851 print "</feed-categories>";
852
853 print "<labels>";
854
855 $result = db_query($link, "SELECT * FROM
856 ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
857
858 while ($line = db_fetch_assoc($result)) {
859 print "<label
860 id=\"".$line["id"]."\"
861 fg_color=\"".$line["fg_color"]."\"
862 bg_color=\"".$line["bg_color"]."\"
863 ><![CDATA[";
864 print $line["caption"];
865 print "]]></label>";
866 }
867
868
869 print "</labels>";
870
871 }
872
873 if ($stage > 0) {
874 print "<articles>";
875
876 $limit = 10;
877 $skip = $limit*($stage-1);
878
879 print "<limit value=\"$limit\"/>";
880
881 if ($amount > 0) $amount -= $skip;
882
883 if ($amount > 0) {
884
885 $limit = min($limit, $amount);
886
887 if ($unread_only) {
888 $unread_qpart = "(unread = true OR marked = true) AND ";
889 }
890
891 if ($cidt && $cidb) {
892 $cid_qpart = "(ttrss_entries.id > $cidt OR ttrss_entries.id < $cidb) AND ";
893 }
894
895 if (DB_TYPE == "pgsql") {
896 $date_qpart = "updated >= NOW() - INTERVAL '1 week' AND";
897 } else {
898 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 WEEK) AND";
899 }
900
901 $result = db_query($link,
902 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
903 guid,link,comments,
904 feed_id,content,updated,unread,marked FROM
905 ttrss_user_entries,ttrss_entries,ttrss_feeds
906 WHERE $unread_qpart $cid_qpart $date_qpart
907 ttrss_feeds.id = feed_id AND
908 ref_id = ttrss_entries.id AND
909 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
910 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
911
912 if (function_exists('json_encode')) {
913
914 while ($line = db_fetch_assoc($result)) {
915 print "<article><![CDATA[";
916
917 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
918 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
919
920 $line["labels"] = get_article_labels($link, $line["id"]);
921
922 // too slow :(
923 // $line["tags"] = format_tags_string(
924 // get_article_tags($link, $line["id"]), $line["id"]);
925
926 print json_encode($line);
927 print "]]></article>";
928 }
929 }
930
931 }
932
933 print "</articles>";
934
935 }
936
937 print "</rpc-reply>";
938
939 return;
940 }
941
942 if ($subop == "digest-get-contents") {
943 $article_id = db_escape_string($_REQUEST['article_id']);
944
945 $result = db_query($link, "SELECT content
946 FROM ttrss_entries, ttrss_user_entries
947 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
948
949 print "<rpc-reply>";
950
951 print "<article id=\"$article_id\"><![CDATA[";
952
953 $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
954
955 print $content;
956
957 print "]]></article>";
958
959 print "</rpc-reply>";
960
961 return;
962 }
963
964 if ($subop == "digest-update") {
965 $feed_id = db_escape_string($_REQUEST['feed_id']);
966 $offset = db_escape_string($_REQUEST['offset']);
967 $seq = db_escape_string($_REQUEST['seq']);
968
969 if (!$feed_id) $feed_id = -4;
970 if (!$offset) $offset = 0;
971 print "<rpc-reply>";
972
973 print "<seq>$seq</seq>";
974
975 $headlines = api_get_headlines($link, $feed_id, 10, $offset,
976 '', ($feed_id == -4), true, false, "unread", "updated DESC");
977
978 //function api_get_headlines($link, $feed_id, $limit, $offset,
979 // $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
980
981 print "<headlines-title><![CDATA[" . getFeedTitle($link, $feed_id) .
982 "]]></headlines-title>";
983
984 print "<headlines><![CDATA[" . json_encode($headlines) . "]]></headlines>";
985
986 print "</rpc-reply>";
987 return;
988 }
989
990 if ($subop == "digest-init") {
991 print "<rpc-reply>";
992
993 $tmp_feeds = api_get_feeds($link, false, true, false, 0);
994 $feeds = array();
995
996 foreach ($tmp_feeds as $f) {
997 if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
998 }
999
1000 print "<feeds><![CDATA[" . json_encode($feeds) . "]]></feeds>";
1001
1002 print "</rpc-reply>";
1003 return;
1004 }
1005
1006 if ($subop == "catchupFeed") {
1007
1008 $feed_id = db_escape_string($_REQUEST['feed_id']);
1009 $is_cat = db_escape_string($_REQUEST['is_cat']);
1010
1011 print "<rpc-reply>";
1012
1013 catchup_feed($link, $feed_id, $is_cat);
1014
1015 print "</rpc-reply>";
1016
1017 return;
1018 }
1019
1020 if ($subop == "sendEmail") {
1021 $secretkey = $_REQUEST['secretkey'];
1022
1023 print "<rpc-reply>";
1024
1025 if (DIGEST_ENABLE && $_SESSION['email_secretkey'] &&
1026 $secretkey == $_SESSION['email_secretkey']) {
1027
1028 $_SESSION['email_secretkey'] = '';
1029
1030 $destination = $_REQUEST['destination'];
1031 $subject = $_REQUEST['subject'];
1032 $content = $_REQUEST['content'];
1033
1034 $replyto = strip_tags($_SESSION['email_replyto']);
1035 $fromname = strip_tags($_SESSION['email_fromname']);
1036
1037 $mail = new PHPMailer();
1038
1039 $mail->PluginDir = "lib/phpmailer/";
1040 $mail->SetLanguage("en", "lib/phpmailer/language/");
1041
1042 $mail->CharSet = "UTF-8";
1043
1044 $mail->From = $replyto;
1045 $mail->FromName = $fromname;
1046 $mail->AddAddress($destination);
1047
1048 if (DIGEST_SMTP_HOST) {
1049 $mail->Host = DIGEST_SMTP_HOST;
1050 $mail->Mailer = "smtp";
1051 $mail->SMTPAuth = DIGEST_SMTP_LOGIN != '';
1052 $mail->Username = DIGEST_SMTP_LOGIN;
1053 $mail->Password = DIGEST_SMTP_PASSWORD;
1054 }
1055
1056 $mail->IsHTML(false);
1057 $mail->Subject = $subject;
1058 $mail->Body = $content;
1059
1060 $rc = $mail->Send();
1061
1062 if (!$rc) {
1063 print "<error><![CDATA[" . $mail->ErrorInfo . "]]></error>";
1064 } else {
1065 save_email_address($link, db_escape_string($destination));
1066 print "<message>OK</message>";
1067 }
1068
1069 } else {
1070 print "<error>Not authorized.</error>";
1071 }
1072
1073 print "</rpc-reply>";
1074
1075 return;
1076 }
1077
1078 if ($subop == "completeEmails") {
1079
1080 $search = db_escape_string($_REQUEST["search"]);
1081
1082 print "<ul>";
1083
1084 foreach ($_SESSION['stored_emails'] as $email) {
1085 if (strpos($email, $search) !== false) {
1086 print "<li>$email</li>";
1087 }
1088 }
1089
1090 print "</ul>";
1091
1092 return;
1093 }
1094
1095 if ($subop == "quickAddCat") {
1096 print "<rpc-reply>";
1097
1098 $cat = db_escape_string($_REQUEST["cat"]);
1099
1100 add_feed_category($link, $cat);
1101
1102 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
1103 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
1104
1105 if (db_num_rows($result) == 1) {
1106 $id = db_fetch_result($result, 0, "id");
1107 } else {
1108 $id = 0;
1109 }
1110
1111 print_feed_cat_select($link, "cat_id", $id);
1112
1113 print "</rpc-reply>";
1114
1115 return;
1116 }
1117
1118 if ($subop == "regenFeedKey") {
1119 $feed_id = db_escape_string($_REQUEST['id']);
1120 $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
1121
1122 print "<rpc-reply>";
1123
1124 $new_key = update_feed_access_key($link, $feed_id, $is_cat);
1125
1126 print "<link><![CDATA[$new_key]]></link>";
1127
1128 print "</rpc-reply>";
1129
1130 return;
1131 }
1132
1133 if ($subop == "clearKeys") {
1134
1135 db_query($link, "DELETE FROM ttrss_access_keys WHERE
1136 owner_uid = " . $_SESSION["uid"]);
1137
1138 print "<rpc-reply><message>OK</message></rpc-reply>";
1139
1140 return;
1141 }
1142
1143 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
1144 }
1145 ?>