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