]> git.wh0rd.org - tt-rss.git/blame - modules/backend-rpc.php
add hotkey (f x) to reverse headlines order (closes #262)
[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
01a87dff
AD
519 if ($subop == "logout") {
520 logout_user();
521 print_error_xml(6);
85bd574b 522 return;
01a87dff
AD
523 }
524
05fcdf52
AD
525 if ($subop == "completeTags") {
526
527 $search = db_escape_string($_REQUEST["search"]);
528
529 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
530 WHERE owner_uid = '".$_SESSION["uid"]."' AND
531 tag_name LIKE '$search%' ORDER BY tag_name
532 LIMIT 10");
533
534 print "<ul>";
535 while ($line = db_fetch_assoc($result)) {
536 print "<li>" . $line["tag_name"] . "</li>";
537 }
538 print "</ul>";
539
85bd574b 540 return;
05fcdf52
AD
541 }
542
81cd6cac 543 if ($subop == "purge") {
b4e75b2a
AD
544 $ids = split(",", db_escape_string($_REQUEST["ids"]));
545 $days = sprintf("%d", $_REQUEST["days"]);
81cd6cac
AD
546
547 print "<rpc-reply>";
548
549 print "<message><![CDATA[";
550
551 foreach ($ids as $id) {
552
553 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
554 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
555
556 if (db_num_rows($result) == 1) {
557 purge_feed($link, $id, $days, true);
558 }
559 }
560
561 print "]]></message>";
562
563 print "</rpc-reply>";
564
565 return;
566 }
567
9bf3f101 568/* if ($subop == "setScore") {
546499a9
AD
569 $id = db_escape_string($_REQUEST["id"]);
570 $score = sprintf("%d", $_REQUEST["score"]);
571
572 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
573 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
574
575 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
576
577 return;
578
9bf3f101 579 } */
546499a9 580
aa0fa9df
AD
581 if ($subop == "getArticles") {
582 $ids = split(",", db_escape_string($_REQUEST["ids"]));
583
584 print "<rpc-reply>";
585
586 foreach ($ids as $id) {
587 if ($id) {
588 outputArticleXML($link, $id, 0, false);
589 }
590 }
591 print "</rpc-reply>";
592
593 return;
594 }
595
d0da85c2
AD
596 if ($subop == "checkDate") {
597
598 $date = db_escape_string($_REQUEST["date"]);
599 $date_parsed = strtotime($date);
600
601 print "<rpc-reply>";
602
603 if ($date_parsed) {
604 print "<result>1</result>";
605 } else {
606 print "<result>0</result>";
607 }
608
609 print "</rpc-reply>";
610
611 return;
612 }
613
933ba4ee
AD
614 if ($subop == "removeFromLabel") {
615
616 $ids = split(",", db_escape_string($_REQUEST["ids"]));
617 $label_id = db_escape_string($_REQUEST["lid"]);
618
7a13338b
AD
619 $label = db_escape_string(label_find_caption($link, $label_id,
620 $_SESSION["uid"]));
933ba4ee 621
1c9c6025
AD
622 print "<rpc-reply>";
623 print "<info-for-headlines>";
624
933ba4ee
AD
625 if ($label) {
626
627 foreach ($ids as $id) {
628 label_remove_article($link, $id, $label, $_SESSION["uid"]);
1c9c6025
AD
629
630 print "<entry id=\"$id\"><![CDATA[";
631
632 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
2eb9c95c 633 print format_article_labels($labels, $id);
1c9c6025
AD
634
635 print "]]></entry>";
636
933ba4ee
AD
637 }
638 }
639
1c9c6025
AD
640 print "</info-for-headlines>";
641
642 print "<counters>";
643 getAllCounters($link, $omode);
644 print "</counters>";
645 print "</rpc-reply>";
933ba4ee
AD
646
647 return;
648 }
649
b8a637f3
AD
650 if ($subop == "assignToLabel") {
651
652 $ids = split(",", db_escape_string($_REQUEST["ids"]));
653 $label_id = db_escape_string($_REQUEST["lid"]);
654
7a13338b
AD
655 $label = db_escape_string(label_find_caption($link, $label_id,
656 $_SESSION["uid"]));
b8a637f3 657
f9247195
AD
658 print "<rpc-reply>";
659
660 print "<info-for-headlines>";
661
b8a637f3
AD
662 if ($label) {
663
664 foreach ($ids as $id) {
665 label_add_article($link, $id, $label, $_SESSION["uid"]);
f9247195
AD
666
667 print "<entry id=\"$id\"><![CDATA[";
668
669 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
2eb9c95c 670 print format_article_labels($labels, $id);
f9247195
AD
671
672 print "]]></entry>";
673
b8a637f3
AD
674 }
675 }
676
f9247195
AD
677 print "</info-for-headlines>";
678
679 print "<counters>";
680 getAllCounters($link, $omode);
681 print "</counters>";
682 print "</rpc-reply>";
b8a637f3
AD
683
684 return;
685 }
686
ef88b1cc 687 if ($subop == "updateFeedBrowser") {
c2913898
AD
688
689 $search = db_escape_string($_REQUEST["search"]);
690 $limit = db_escape_string($_REQUEST["limit"]);
082ae95b 691 $mode = db_escape_string($_REQUEST["mode"]);
c2913898
AD
692
693 print "<rpc-reply>";
694 print "<content>";
695 print "<![CDATA[";
082ae95b 696 $ctr = print_feed_browser($link, $search, $limit, $mode);
c2913898
AD
697 print "]]>";
698 print "</content>";
699 print "<num-results value=\"$ctr\"/>";
ef88b1cc 700 print "<mode value=\"$mode\"/>";
c2913898
AD
701 print "</rpc-reply>";
702
703 return;
704 }
705
ef88b1cc
AD
706
707 if ($subop == "massSubscribe") {
708
709 $ids = split(",", db_escape_string($_REQUEST["ids"]));
710 $mode = $_REQUEST["mode"];
711
712 $subscribed = array();
713
714 foreach ($ids as $id) {
715
716 if ($mode == 1) {
717 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
718 WHERE id = '$id'");
719 } else if ($mode == 2) {
720 $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
721 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
722 $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
723 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
724 }
725
726 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
727 $title = db_escape_string(db_fetch_result($result, 0, "title"));
728
729 $title_orig = db_fetch_result($result, 0, "title");
730
731 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
732 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
733
734 if (db_num_rows($result) == 0) {
735 if ($mode == 1) {
736 $result = db_query($link,
737 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
738 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
739 } else if ($mode == 2) {
740 $result = db_query($link,
741 "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
742 VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
743 }
744 array_push($subscribed, $title_orig);
745 }
746 }
747
748 $num_feeds = count($subscribed);
749
750 print "<rpc-reply>";
751 print "<num-feeds value='$num_feeds'/>";
752 print "</rpc-reply>";
753
754 return;
755 }
756
87b16a0a
AD
757 if ($subop == "download") {
758 $stage = (int) $_REQUEST["stage"];
04870193
AD
759 $cidt = (int)db_escape_string($_REQUEST["cidt"]);
760 $cidb = (int)db_escape_string($_REQUEST["cidb"]);
badac687 761 $sync = db_escape_string($_REQUEST["sync"]);
51f6f917
AD
762 //$amount = (int) $_REQUEST["amount"];
763 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
764 //if (!$amount) $amount = 50;
6a1cd591 765
d9447516
AD
766 /* Amount is not used by the frontend offline.js anymore, it goes by
767 * date_qpart below + cidb/cidt IDs */
768
04870193 769 $amount = 2000;
51f6f917 770 $unread_only = true;
87b16a0a
AD
771
772 print "<rpc-reply>";
773
badac687
AD
774 $sync = split(";", $sync);
775
776 print "<sync>";
777
778 if (count($sync) > 0) {
779 if (strtotime($sync[0])) {
780 $last_online = db_escape_string($sync[0]);
781
782 print "<sync-point><![CDATA[$last_online]]></sync-point>";
783
784 for ($i = 1; $i < count($sync); $i++) {
785 $e = split(",", $sync[$i]);
786
787 if (count($e) == 3) {
788
789 $id = (int) $e[0];
790 $unread = bool_to_sql_bool((bool) $e[1]);
5b8444d3
AD
791 $marked = (bool)$e[2];
792
793 if ($marked) {
794 $marked = bool_to_sql_bool($marked);
795 $marked_qpart = "marked = $marked,";
796 }
badac687 797
badac687 798 $query = "UPDATE ttrss_user_entries SET
5b8444d3 799 $marked_qpart
badac687
AD
800 unread = $unread,
801 last_read = '$last_online'
802 WHERE ref_id = '$id' AND
803 (last_read IS NULL OR last_read < '$last_online') AND
804 owner_uid = ".$_SESSION["uid"];
805
806 $result = db_query($link, $query);
807
492a4a6a 808 print "<sync-ok id=\"$id\"/>";
badac687
AD
809
810 }
811 }
7f4f9f4e
AD
812
813 /* Maybe we need to further update local DB for this client */
814
492a4a6a 815 $query = "SELECT ref_id,unread,marked FROM ttrss_user_entries
7f4f9f4e 816 WHERE last_read >= '$last_online' AND
e6c611c5 817 owner_uid = ".$_SESSION["uid"] . " LIMIT 1000";
7f4f9f4e
AD
818
819 $result = db_query($link, $query);
820
821 while ($line = db_fetch_assoc($result)) {
492a4a6a
AD
822 $unread = (int) sql_bool_to_bool($line["unread"]);
823 $marked = (int) sql_bool_to_bool($line["marked"]);
824
825 print "<sync-ok unread=\"$unread\" marked=\"$marked\"
826 id=\"".$line["ref_id"]."\"/>";
7f4f9f4e
AD
827 }
828
badac687
AD
829 }
830 }
831
832 print "</sync>";
833
87b16a0a
AD
834 if ($stage == 0) {
835 print "<feeds>";
836
d8781c91 837 $result = db_query($link, "SELECT id, title, cat_id FROM
117335bf 838 ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
87b16a0a
AD
839
840 while ($line = db_fetch_assoc($result)) {
36f78797
AD
841
842 $has_icon = (int) feed_has_icon($line["id"]);
843
d8781c91
AD
844 print "<feed has_icon=\"$has_icon\"
845 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
87b16a0a
AD
846 print $line["title"];
847 print "]]></feed>";
848 }
849
850 print "</feeds>";
851
d8781c91
AD
852 print "<feed-categories>";
853
75aa83ec 854 $result = db_query($link, "SELECT id, title, collapsed FROM
d8781c91
AD
855 ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
856
75aa83ec 857 print "<category id=\"0\" collapsed=\"".
57937c42 858 (int)get_pref($link, "_COLLAPSED_UNCAT")."\"><![CDATA[";
d8781c91
AD
859 print __("Uncategorized");
860 print "]]></category>";
861
d6416405 862 print "<category id=\"-1\" collapsed=\"".
57937c42 863 (int)get_pref($link, "_COLLAPSED_SPECIAL")."\"><![CDATA[";
d6416405
AD
864 print __("Special");
865 print "]]></category>";
866
c2726c96 867 print "<category id=\"-2\" collapsed=\"".
57937c42 868 (int)get_pref($link, "_COLLAPSED_LABELS")."\"><![CDATA[";
c2726c96
AD
869 print __("Labels");
870 print "]]></category>";
871
d8781c91 872 while ($line = db_fetch_assoc($result)) {
75aa83ec
AD
873 print "<category
874 id=\"".$line["id"]."\"
875 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
d8781c91
AD
876 print $line["title"];
877 print "]]></category>";
878 }
879
880 print "</feed-categories>";
881
ed22888b
AD
882 print "<labels>";
883
884 $result = db_query($link, "SELECT * FROM
885 ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
886
887 while ($line = db_fetch_assoc($result)) {
888 print "<label
889 id=\"".$line["id"]."\"
890 fg_color=\"".$line["fg_color"]."\"
891 bg_color=\"".$line["bg_color"]."\"
892 ><![CDATA[";
893 print $line["caption"];
894 print "]]></label>";
895 }
896
897
898 print "</labels>";
d8781c91 899
87b16a0a
AD
900 }
901
6a1cd591 902 if ($stage > 0) {
6a1cd591
AD
903 print "<articles>";
904
d9447516 905 $limit = 10;
6a1cd591
AD
906 $skip = $limit*($stage-1);
907
3e52ab08
AD
908 print "<limit value=\"$limit\"/>";
909
6a1cd591
AD
910 if ($amount > 0) $amount -= $skip;
911
912 if ($amount > 0) {
913
914 $limit = min($limit, $amount);
915
916 if ($unread_only) {
a400a562 917 $unread_qpart = "(unread = true OR marked = true) AND ";
6a1cd591
AD
918 }
919
67eb2531 920 if ($cidt && $cidb) {
04870193 921 $cid_qpart = "(ttrss_entries.id > $cidt OR ttrss_entries.id < $cidb) AND ";
95f0c2c5
AD
922 }
923
67eb2531 924 if (DB_TYPE == "pgsql") {
d9447516 925 $date_qpart = "updated >= NOW() - INTERVAL '1 week' AND";
67eb2531 926 } else {
d9447516 927 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 WEEK) AND";
67eb2531
AD
928 }
929
6a1cd591 930 $result = db_query($link,
c1a0541a
AD
931 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
932 guid,link,comments,
933 feed_id,content,updated,unread,marked FROM
934 ttrss_user_entries,ttrss_entries,ttrss_feeds
935 WHERE $unread_qpart $cid_qpart $date_qpart
c1a0541a
AD
936 ttrss_feeds.id = feed_id AND
937 ref_id = ttrss_entries.id AND
938 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
6a1cd591 939 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
fe8f2f0c 940
3034277a 941 if (function_exists('json_encode')) {
6a2034f9 942
3034277a
AD
943 while ($line = db_fetch_assoc($result)) {
944 print "<article><![CDATA[";
945
946 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
947 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
3ab18266 948
c2726c96
AD
949 $line["labels"] = get_article_labels($link, $line["id"]);
950
3ab18266
AD
951// too slow :(
952// $line["tags"] = format_tags_string(
953// get_article_tags($link, $line["id"]), $line["id"]);
3034277a
AD
954
955 print json_encode($line);
956 print "]]></article>";
957 }
6a1cd591
AD
958 }
959
960 }
961
962 print "</articles>";
963
964 }
965
87b16a0a
AD
966 print "</rpc-reply>";
967
968 return;
969 }
970
85bd574b 971 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
01b3e191
AD
972 }
973?>