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