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