]> git.wh0rd.org - tt-rss.git/blame - modules/backend-rpc.php
allow adding/removing labels from frontend
[tt-rss.git] / modules / backend-rpc.php
CommitLineData
1d3a17c7 1<?php
01b3e191
AD
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
85bd574b 22 return;
01b3e191
AD
23 }
24
25 if ($subop == "getLabelCounters") {
26 $aid = $_GET["aid"];
27 print "<rpc-reply>";
f54f515f 28 print "<counters>";
01b3e191
AD
29 getLabelCounters($link);
30 if ($aid) {
31 getFeedCounter($link, $aid);
32 }
f54f515f 33 print "</counters>";
01b3e191 34 print "</rpc-reply>";
85bd574b
AD
35
36 return;
01b3e191
AD
37 }
38
39 if ($subop == "getFeedCounters") {
40 print "<rpc-reply>";
f54f515f 41 print "<counters>";
01b3e191 42 getFeedCounters($link);
f54f515f 43 print "</counters>";
01b3e191 44 print "</rpc-reply>";
85bd574b
AD
45
46 return;
01b3e191
AD
47 }
48
49 if ($subop == "getAllCounters") {
cf4d339c 50 print "<rpc-reply>";
f54f515f 51 print "<counters>";
cf4d339c
AD
52
53 $omode = $_GET["omode"];
54
55 getAllCounters($link, $omode);
f54f515f
AD
56 print "</counters>";
57 print_runtime_info($link);
01b3e191 58 print "</rpc-reply>";
85bd574b
AD
59
60 return;
01b3e191
AD
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"]);
49aa6de9 77
36e05046
AD
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>";
49aa6de9 85
85bd574b 86 return;
01b3e191
AD
87 }
88
e4f4b46f
AD
89 if ($subop == "publ") {
90 $pub = $_GET["pub"];
91 $id = db_escape_string($_GET["id"]);
92
93 if ($pub == "1") {
0a8011eb 94 $pub = "true";
e4f4b46f
AD
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"]);
49aa6de9 103
36e05046
AD
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>";
49aa6de9 111
85bd574b 112 return;
e4f4b46f
AD
113 }
114
01b3e191
AD
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
f54f515f
AD
127 print "<rpc-reply>";
128 print "<counters>";
01b3e191 129 getFeedCounter($link, $feed_id);
f54f515f 130 print "</counters>";
01b3e191
AD
131 print "</rpc-reply>";
132
133 return;
134 }
135
136 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
137
01b3e191
AD
138 $global_unread_caller = sprintf("%d", $_GET["uctr"]);
139 $global_unread = getGlobalUnread($link);
140
141 print "<rpc-reply>";
142
f54f515f
AD
143 print "<counters>";
144
a06d0e5a 145 if ($global_unread_caller != $global_unread) {
1341ea0d 146
a06d0e5a
AD
147 $omode = $_GET["omode"];
148
149 if (!$omode) $omode = "tflc";
1341ea0d 150
a06d0e5a 151 if (strchr($omode, "l")) getLabelCounters($link);
1341ea0d 152
a06d0e5a
AD
153 if (strchr($omode, "c")) {
154 if (get_pref($link, 'ENABLE_FEED_CATS')) {
155 getCategoryCounters($link);
156 }
157 }
01b3e191 158
01b3e191 159 if (strchr($omode, "f")) getFeedCounters($link);
a06d0e5a 160 if (strchr($omode, "t")) getTagCounters($link);
01b3e191 161
a06d0e5a
AD
162 getGlobalCounters($link, $global_unread);
163 }
164
f54f515f
AD
165 print "</counters>";
166
167 print_runtime_info($link);
168
01b3e191
AD
169 print "</rpc-reply>";
170
85bd574b 171 return;
01b3e191 172 }
472782e8 173
01b3e191
AD
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"]));
01b3e191
AD
178 $cmode = sprintf("%d", $_GET["cmode"]);
179
472782e8 180 catchupArticlesById($link, $ids, $cmode);
01b3e191 181
01b3e191 182 print "<rpc-reply>";
f54f515f 183 print "<counters>";
2bc2147f 184 getAllCounters($link, $_GET["omode"]);
f54f515f
AD
185 print "</counters>";
186 print_runtime_info($link);
01b3e191 187 print "</rpc-reply>";
85bd574b
AD
188
189 return;
01b3e191
AD
190 }
191
192 if ($subop == "markSelected") {
193
194 $ids = split(",", db_escape_string($_GET["ids"]));
01b3e191
AD
195 $cmode = sprintf("%d", $_GET["cmode"]);
196
18eddb2c
AD
197 markArticlesById($link, $ids, $cmode);
198
01b3e191 199 print "<rpc-reply>";
f54f515f 200 print "<counters>";
2bc2147f 201 getAllCounters($link, $_GET["omode"]);
f54f515f
AD
202 print "</counters>";
203 print_runtime_info($link);
01b3e191 204 print "</rpc-reply>";
85bd574b
AD
205
206 return;
01b3e191
AD
207 }
208
e4f4b46f
AD
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>";
2bc2147f 218 getAllCounters($link, $_GET["omode"]);
e4f4b46f
AD
219 print "</counters>";
220 print_runtime_info($link);
221 print "</rpc-reply>";
85bd574b
AD
222
223 return;
e4f4b46f
AD
224 }
225
01b3e191 226 if ($subop == "sanityCheck") {
3ac2b520 227 print "<rpc-reply>";
01b3e191
AD
228 if (sanity_check($link)) {
229 print "<error error-code=\"0\"/>";
3ac2b520 230 print_init_params($link);
f54f515f 231 print_runtime_info($link);
4220d6b0
AD
232
233 # assign client-passed params to session
234 $_SESSION["client.userAgent"] = $_GET["ua"];
235
01b3e191 236 }
3ac2b520 237 print "</rpc-reply>";
85bd574b
AD
238
239 return;
3ac2b520 240 }
01b3e191
AD
241
242 if ($subop == "globalPurge") {
243
244 print "<rpc-reply>";
245 global_purge_old_posts($link, true);
246 print "</rpc-reply>";
247
85bd574b 248 return;
01b3e191 249 }
3ac2b520 250
298f3f78
AD
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) {
06925d9e 259 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
e2ccbfab 260 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
298f3f78
AD
261 } else {
262 print "<rpc-reply><error>Article not found</error></rpc-reply>";
263 }
85bd574b
AD
264
265 return;
298f3f78
AD
266 }
267
0b126ac2 268 if ($subop == "setArticleTags") {
14b6c54b 269
0b126ac2 270 $id = db_escape_string($_GET["id"]);
14b6c54b 271
0b126ac2
AD
272 $tags_str = db_escape_string($_GET["tags_str"]);
273
d62a3b63 274 $tags = array_unique(trim_array(split(",", $tags_str)));
0b126ac2
AD
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) {
14b6c54b 289 $tag = sanitize_tag($tag);
0b126ac2 290
ef063748
AD
291 if (!tag_is_valid($tag)) {
292 continue;
293 }
294
0b126ac2
AD
295 if (preg_match("/^[0-9]*$/", $tag)) {
296 continue;
297 }
14b6c54b 298
9cfd409d 299 print "<!-- $id : $int_id : $tag -->";
0b126ac2
AD
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 print "<rpc-reply>
311 <message>$id</message>
312 </rpc-reply>";
313
85bd574b 314 return;
0b126ac2 315 }
01a87dff 316
945c243e
AD
317 if ($subop == "regenPubKey") {
318
319 print "<rpc-reply>";
320
321 set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
945c243e 322
f56e3080 323 $new_link = article_publish_url($link);
945c243e
AD
324
325 print "<link><![CDATA[$new_link]]></link>";
326
327 print "</rpc-reply>";
328
85bd574b 329 return;
945c243e
AD
330 }
331
01a87dff
AD
332 if ($subop == "logout") {
333 logout_user();
334 print_error_xml(6);
85bd574b 335 return;
01a87dff
AD
336 }
337
05fcdf52
AD
338 if ($subop == "completeTags") {
339
340 $search = db_escape_string($_REQUEST["search"]);
341
342 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
343 WHERE owner_uid = '".$_SESSION["uid"]."' AND
344 tag_name LIKE '$search%' ORDER BY tag_name
345 LIMIT 10");
346
347 print "<ul>";
348 while ($line = db_fetch_assoc($result)) {
349 print "<li>" . $line["tag_name"] . "</li>";
350 }
351 print "</ul>";
352
85bd574b 353 return;
05fcdf52
AD
354 }
355
81cd6cac
AD
356 if ($subop == "purge") {
357 $ids = split(",", db_escape_string($_GET["ids"]));
358 $days = sprintf("%d", $_GET["days"]);
359
360 print "<rpc-reply>";
361
362 print "<message><![CDATA[";
363
364 foreach ($ids as $id) {
365
366 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
367 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
368
369 if (db_num_rows($result) == 1) {
370 purge_feed($link, $id, $days, true);
371 }
372 }
373
374 print "]]></message>";
375
376 print "</rpc-reply>";
377
378 return;
379 }
380
546499a9
AD
381 if ($subop == "setScore") {
382 $id = db_escape_string($_REQUEST["id"]);
383 $score = sprintf("%d", $_REQUEST["score"]);
384
385 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
386 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
387
388 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
389
390 return;
391
392 }
393
aa0fa9df
AD
394 if ($subop == "getArticles") {
395 $ids = split(",", db_escape_string($_REQUEST["ids"]));
396
397 print "<rpc-reply>";
398
399 foreach ($ids as $id) {
400 if ($id) {
401 outputArticleXML($link, $id, 0, false);
402 }
403 }
404 print "</rpc-reply>";
405
406 return;
407 }
408
d0da85c2
AD
409 if ($subop == "checkDate") {
410
411 $date = db_escape_string($_REQUEST["date"]);
412 $date_parsed = strtotime($date);
413
414 print "<rpc-reply>";
415
416 if ($date_parsed) {
417 print "<result>1</result>";
418 } else {
419 print "<result>0</result>";
420 }
421
422 print "</rpc-reply>";
423
424 return;
425 }
426
933ba4ee
AD
427 if ($subop == "removeFromLabel") {
428
429 $ids = split(",", db_escape_string($_REQUEST["ids"]));
430 $label_id = db_escape_string($_REQUEST["lid"]);
431
432 $label = label_find_caption($link, $label_id, $_SESSION["uid"]);
433
434 if ($label) {
435
436 foreach ($ids as $id) {
437 label_remove_article($link, $id, $label, $_SESSION["uid"]);
438 }
439 }
440
441 print "<rpc-reply>OK</rpc-reply>";
442
443 return;
444 }
445
b8a637f3
AD
446 if ($subop == "assignToLabel") {
447
448 $ids = split(",", db_escape_string($_REQUEST["ids"]));
449 $label_id = db_escape_string($_REQUEST["lid"]);
450
451 $label = label_find_caption($link, $label_id, $_SESSION["uid"]);
452
453 if ($label) {
454
455 foreach ($ids as $id) {
456 label_add_article($link, $id, $label, $_SESSION["uid"]);
457 }
458 }
459
e2549229 460 print "<rpc-reply>OK</rpc-reply>";
b8a637f3
AD
461
462 return;
463 }
464
85bd574b 465 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
01b3e191
AD
466 }
467?>