]> git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
54bf4a326a8e212e57edc9050c019cefd92b0d34
[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 = $_GET["pub"];
91 $id = db_escape_string($_GET["id"]);
92
93 if ($pub == "1") {
94 $pub = "true";
95 } else {
96 $pub = "false";
97 }
98
99 // FIXME this needs collision testing
100
101 $result = db_query($link, "UPDATE ttrss_user_entries SET published = $pub
102 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
103
104 print "<rpc-reply><counters>";
105 getGlobalCounters($link);
106 getLabelCounters($link);
107 if (get_pref($link, 'ENABLE_FEED_CATS')) {
108 getCategoryCounters($link);
109 }
110 print "</counters></rpc-reply>";
111
112 return;
113 }
114
115 if ($subop == "updateFeed") {
116 $feed_id = db_escape_string($_GET["feed"]);
117
118 $result = db_query($link,
119 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
120 AND owner_uid = " . $_SESSION["uid"]);
121
122 if (db_num_rows($result) > 0) {
123 $feed_url = db_fetch_result($result, 0, "feed_url");
124 update_rss_feed($link, $feed_url, $feed_id);
125 }
126
127 print "<rpc-reply>";
128 print "<counters>";
129 getFeedCounter($link, $feed_id);
130 print "</counters>";
131 print "</rpc-reply>";
132
133 return;
134 }
135
136 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
137
138 $global_unread_caller = sprintf("%d", $_GET["uctr"]);
139 $global_unread = getGlobalUnread($link);
140
141 print "<rpc-reply>";
142
143 print "<counters>";
144
145 if ($global_unread_caller != $global_unread) {
146
147 $omode = $_GET["omode"];
148
149 if (!$omode) $omode = "tflc";
150
151 if (strchr($omode, "l")) getLabelCounters($link);
152
153 if (strchr($omode, "c")) {
154 if (get_pref($link, 'ENABLE_FEED_CATS')) {
155 getCategoryCounters($link);
156 }
157 }
158
159 if (strchr($omode, "f")) getFeedCounters($link);
160 if (strchr($omode, "t")) getTagCounters($link);
161
162 getGlobalCounters($link, $global_unread);
163 }
164
165 print "</counters>";
166
167 print_runtime_info($link);
168
169 print "</rpc-reply>";
170
171 return;
172 }
173
174 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
175 if ($subop == "catchupSelected") {
176
177 $ids = split(",", db_escape_string($_GET["ids"]));
178 $cmode = sprintf("%d", $_GET["cmode"]);
179
180 catchupArticlesById($link, $ids, $cmode);
181
182 print "<rpc-reply>";
183 print "<counters>";
184 getAllCounters($link, $_GET["omode"]);
185 print "</counters>";
186 print_runtime_info($link);
187 print "</rpc-reply>";
188
189 return;
190 }
191
192 if ($subop == "markSelected") {
193
194 $ids = split(",", db_escape_string($_GET["ids"]));
195 $cmode = sprintf("%d", $_GET["cmode"]);
196
197 markArticlesById($link, $ids, $cmode);
198
199 print "<rpc-reply>";
200 print "<counters>";
201 getAllCounters($link, $_GET["omode"]);
202 print "</counters>";
203 print_runtime_info($link);
204 print "</rpc-reply>";
205
206 return;
207 }
208
209 if ($subop == "publishSelected") {
210
211 $ids = split(",", db_escape_string($_GET["ids"]));
212 $cmode = sprintf("%d", $_GET["cmode"]);
213
214 publishArticlesById($link, $ids, $cmode);
215
216 print "<rpc-reply>";
217 print "<counters>";
218 getAllCounters($link, $_GET["omode"]);
219 print "</counters>";
220 print_runtime_info($link);
221 print "</rpc-reply>";
222
223 return;
224 }
225
226 if ($subop == "sanityCheck") {
227 print "<rpc-reply>";
228 if (sanity_check($link)) {
229 print "<error error-code=\"0\"/>";
230 print_init_params($link);
231 print_runtime_info($link);
232
233 # assign client-passed params to session
234 $_SESSION["client.userAgent"] = $_GET["ua"];
235
236 }
237 print "</rpc-reply>";
238
239 return;
240 }
241
242 if ($subop == "globalPurge") {
243
244 print "<rpc-reply>";
245 global_purge_old_posts($link, true);
246 print "</rpc-reply>";
247
248 return;
249 }
250
251 if ($subop == "getArticleLink") {
252
253 $id = db_escape_string($_GET["id"]);
254
255 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
256 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
257
258 if (db_num_rows($result) == 1) {
259 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
260 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
261 } else {
262 print "<rpc-reply><error>Article not found</error></rpc-reply>";
263 }
264
265 return;
266 }
267
268 if ($subop == "setArticleTags") {
269
270 $id = db_escape_string($_GET["id"]);
271
272 $tags_str = db_escape_string($_GET["tags_str"]);
273
274 $tags = array_unique(trim_array(split(",", $tags_str)));
275
276 db_query($link, "BEGIN");
277
278 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
279 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
280
281 if (db_num_rows($result) == 1) {
282
283 $int_id = db_fetch_result($result, 0, "int_id");
284
285 db_query($link, "DELETE FROM ttrss_tags WHERE
286 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
287
288 foreach ($tags as $tag) {
289 $tag = sanitize_tag($tag);
290
291 if (!tag_is_valid($tag)) {
292 continue;
293 }
294
295 if (preg_match("/^[0-9]*$/", $tag)) {
296 continue;
297 }
298
299 // print "<!-- $id : $int_id : $tag -->";
300
301 if ($tag != '') {
302 db_query($link, "INSERT INTO ttrss_tags
303 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
304 }
305 }
306 }
307
308 db_query($link, "COMMIT");
309
310 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
311
312 print "<rpc-reply>
313 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
314 </rpc-reply>";
315
316 return;
317 }
318
319 if ($subop == "regenPubKey") {
320
321 print "<rpc-reply>";
322
323 set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
324
325 $new_link = article_publish_url($link);
326
327 print "<link><![CDATA[$new_link]]></link>";
328
329 print "</rpc-reply>";
330
331 return;
332 }
333
334 if ($subop == "logout") {
335 logout_user();
336 print_error_xml(6);
337 return;
338 }
339
340 if ($subop == "completeTags") {
341
342 $search = db_escape_string($_REQUEST["search"]);
343
344 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
345 WHERE owner_uid = '".$_SESSION["uid"]."' AND
346 tag_name LIKE '$search%' ORDER BY tag_name
347 LIMIT 10");
348
349 print "<ul>";
350 while ($line = db_fetch_assoc($result)) {
351 print "<li>" . $line["tag_name"] . "</li>";
352 }
353 print "</ul>";
354
355 return;
356 }
357
358 if ($subop == "purge") {
359 $ids = split(",", db_escape_string($_GET["ids"]));
360 $days = sprintf("%d", $_GET["days"]);
361
362 print "<rpc-reply>";
363
364 print "<message><![CDATA[";
365
366 foreach ($ids as $id) {
367
368 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
369 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
370
371 if (db_num_rows($result) == 1) {
372 purge_feed($link, $id, $days, true);
373 }
374 }
375
376 print "]]></message>";
377
378 print "</rpc-reply>";
379
380 return;
381 }
382
383 /* if ($subop == "setScore") {
384 $id = db_escape_string($_REQUEST["id"]);
385 $score = sprintf("%d", $_REQUEST["score"]);
386
387 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
388 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
389
390 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
391
392 return;
393
394 } */
395
396 if ($subop == "getArticles") {
397 $ids = split(",", db_escape_string($_REQUEST["ids"]));
398
399 print "<rpc-reply>";
400
401 foreach ($ids as $id) {
402 if ($id) {
403 outputArticleXML($link, $id, 0, false);
404 }
405 }
406 print "</rpc-reply>";
407
408 return;
409 }
410
411 if ($subop == "checkDate") {
412
413 $date = db_escape_string($_REQUEST["date"]);
414 $date_parsed = strtotime($date);
415
416 print "<rpc-reply>";
417
418 if ($date_parsed) {
419 print "<result>1</result>";
420 } else {
421 print "<result>0</result>";
422 }
423
424 print "</rpc-reply>";
425
426 return;
427 }
428
429 if ($subop == "removeFromLabel") {
430
431 $ids = split(",", db_escape_string($_REQUEST["ids"]));
432 $label_id = db_escape_string($_REQUEST["lid"]);
433
434 $label = label_find_caption($link, $label_id, $_SESSION["uid"]);
435
436 print "<rpc-reply>";
437 print "<info-for-headlines>";
438
439 if ($label) {
440
441 foreach ($ids as $id) {
442 label_remove_article($link, $id, $label, $_SESSION["uid"]);
443
444 print "<entry id=\"$id\"><![CDATA[";
445
446 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
447 print format_article_labels($labels, $id);
448
449 print "]]></entry>";
450
451 }
452 }
453
454 print "</info-for-headlines>";
455
456 print "<counters>";
457 getAllCounters($link, $omode);
458 print "</counters>";
459 print "</rpc-reply>";
460
461 return;
462 }
463
464 if ($subop == "assignToLabel") {
465
466 $ids = split(",", db_escape_string($_REQUEST["ids"]));
467 $label_id = db_escape_string($_REQUEST["lid"]);
468
469 $label = label_find_caption($link, $label_id, $_SESSION["uid"]);
470
471 print "<rpc-reply>";
472
473 print "<info-for-headlines>";
474
475 if ($label) {
476
477 foreach ($ids as $id) {
478 label_add_article($link, $id, $label, $_SESSION["uid"]);
479
480 print "<entry id=\"$id\"><![CDATA[";
481
482 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
483 print format_article_labels($labels, $id);
484
485 print "]]></entry>";
486
487 }
488 }
489
490 print "</info-for-headlines>";
491
492 print "<counters>";
493 getAllCounters($link, $omode);
494 print "</counters>";
495 print "</rpc-reply>";
496
497 return;
498 }
499
500 if ($subop == "feedBrowser") {
501
502 $search = db_escape_string($_REQUEST["search"]);
503 $limit = db_escape_string($_REQUEST["limit"]);
504
505 print "<rpc-reply>";
506 print "<content>";
507 print "<![CDATA[";
508 $ctr = print_feed_browser($link, $search, $limit);
509 print "]]>";
510 print "</content>";
511 print "<num-results value=\"$ctr\"/>";
512 print "</rpc-reply>";
513
514 return;
515 }
516
517 if ($subop == "download") {
518 $stage = (int) $_REQUEST["stage"];
519 $cidt = db_escape_string($_REQUEST["cidt"]);
520 $cidb = db_escape_string($_REQUEST["cidb"]);
521 //$amount = (int) $_REQUEST["amount"];
522 //$unread_only = db_escape_string($_REQUEST["unread_only"]);
523 //if (!$amount) $amount = 50;
524
525 $amount = 100;
526 $unread_only = true;
527
528 print "<rpc-reply>";
529
530 if ($stage == 0) {
531 print "<feeds>";
532
533 $reply = array();
534
535 $result = db_query($link, "SELECT id, title FROM
536 ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
537
538 while ($line = db_fetch_assoc($result)) {
539
540 $has_icon = (int) feed_has_icon($line["id"]);
541
542 print "<feed has_icon=\"$has_icon\" id=\"".$line["id"]."\"><![CDATA[";
543 print $line["title"];
544 print "]]></feed>";
545 }
546
547 print "</feeds>";
548
549 }
550
551 if ($stage > 0) {
552
553 print "<articles>";
554
555 $limit = 50;
556 $skip = $limit*($stage-1);
557
558 if ($amount > 0) $amount -= $skip;
559
560 if ($amount > 0) {
561
562 $limit = min($limit, $amount);
563
564 if ($unread_only) {
565 $unread_qpart = "unread = true AND ";
566 }
567
568 if ($cidt && $cidb) {
569 $cid_qpart = "(id > $cidt OR id < $cidb) AND ";
570 }
571
572 if (DB_TYPE == "pgsql") {
573 $date_qpart = "updated >= NOW() - INTERVAL '1 month' AND";
574 } else {
575 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 MONTH) AND";
576 }
577
578 $result = db_query($link,
579 "SELECT DISTINCT id,title,guid,link,comments,
580 feed_id,content,updated,unread,marked FROM
581 ttrss_user_entries,ttrss_entries
582 WHERE $unread_qpart $cid_qpart $date_qpart
583 ref_id = id AND owner_uid = ".$_SESSION["uid"]."
584 ORDER BY updated DESC LIMIT $limit OFFSET $skip");
585
586 if (function_exists('json_encode')) {
587
588 while ($line = db_fetch_assoc($result)) {
589 print "<article><![CDATA[";
590
591 $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
592 $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
593 $line["tags"] = format_tags_string(get_article_tags($link, $id), $id);
594
595 print json_encode($line);
596 print "]]></article>";
597 }
598 }
599
600 }
601
602 print "</articles>";
603
604 }
605
606 print "</rpc-reply>";
607
608 return;
609 }
610
611 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
612 }
613 ?>