]> git.wh0rd.org - tt-rss.git/blob - modules/backend-rpc.php
tag editor: autocomplete
[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 }
23
24 if ($subop == "getLabelCounters") {
25 $aid = $_GET["aid"];
26 print "<rpc-reply>";
27 print "<counters>";
28 getLabelCounters($link);
29 if ($aid) {
30 getFeedCounter($link, $aid);
31 }
32 print "</counters>";
33 print "</rpc-reply>";
34 }
35
36 if ($subop == "getFeedCounters") {
37 print "<rpc-reply>";
38 print "<counters>";
39 getFeedCounters($link);
40 print "</counters>";
41 print "</rpc-reply>";
42 }
43
44 if ($subop == "getAllCounters") {
45 print "<rpc-reply>";
46 print "<counters>";
47
48 $omode = $_GET["omode"];
49
50 getAllCounters($link, $omode);
51 print "</counters>";
52 print_runtime_info($link);
53 print "</rpc-reply>";
54 }
55
56 if ($subop == "mark") {
57 $mark = $_GET["mark"];
58 $id = db_escape_string($_GET["id"]);
59
60 if ($mark == "1") {
61 $mark = "true";
62 } else {
63 $mark = "false";
64 }
65
66 // FIXME this needs collision testing
67
68 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
69 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
70
71 print "<rpc-reply><mark>$id</mark></rpc-reply>";
72
73 }
74
75 if ($subop == "publ") {
76 $pub = $_GET["pub"];
77 $id = db_escape_string($_GET["id"]);
78
79 if ($pub == "1") {
80 $pub = "true";
81 } else {
82 $pub = "false";
83 }
84
85 // FIXME this needs collision testing
86
87 $result = db_query($link, "UPDATE ttrss_user_entries SET published = $pub
88 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
89
90 print "<rpc-reply><pub>$id</pub></rpc-reply>";
91
92 }
93
94 if ($subop == "updateFeed") {
95 $feed_id = db_escape_string($_GET["feed"]);
96
97 $result = db_query($link,
98 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
99 AND owner_uid = " . $_SESSION["uid"]);
100
101 if (db_num_rows($result) > 0) {
102 $feed_url = db_fetch_result($result, 0, "feed_url");
103 update_rss_feed($link, $feed_url, $feed_id);
104 }
105
106 print "<rpc-reply>";
107 print "<counters>";
108 getFeedCounter($link, $feed_id);
109 print "</counters>";
110 print "</rpc-reply>";
111
112 return;
113 }
114
115 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
116
117 if (ENABLE_UPDATE_DAEMON) {
118
119 if ($subop == "forceUpdateAllFeeds") {
120
121 $result = db_query($link, "SELECT count(id) AS cid FROM
122 ttrss_scheduled_updates WHERE feed_id IS NULL AND
123 owner_uid = " . $_SESSION["uid"]);
124
125 $cid = db_fetch_result($result, 0, "cid");
126
127 if ($cid == 0) {
128
129 db_query($link, "INSERT INTO ttrss_scheduled_updates
130 (owner_uid, feed_id, entered) VALUES
131 (".$_SESSION["uid"].", NULL, NOW())");
132
133 }
134 }
135
136 } else {
137 update_all_feeds($link, $subop == "forceUpdateAllFeeds");
138 }
139
140 $global_unread_caller = sprintf("%d", $_GET["uctr"]);
141 $global_unread = getGlobalUnread($link);
142
143 print "<rpc-reply>";
144
145 print "<counters>";
146
147 if ($global_unread_caller != $global_unread) {
148
149 $omode = $_GET["omode"];
150
151 if (!$omode) $omode = "tflc";
152
153 if (strchr($omode, "l")) getLabelCounters($link);
154 if (strchr($omode, "f")) getFeedCounters($link);
155 if (strchr($omode, "t")) getTagCounters($link);
156 if (strchr($omode, "c")) {
157 if (get_pref($link, 'ENABLE_FEED_CATS')) {
158 getCategoryCounters($link);
159 }
160 }
161 }
162
163 getGlobalCounters($link, $global_unread);
164
165 print "</counters>";
166
167 print_runtime_info($link);
168
169 print "</rpc-reply>";
170
171 }
172
173 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
174 if ($subop == "catchupSelected") {
175
176 $ids = split(",", db_escape_string($_GET["ids"]));
177 $cmode = sprintf("%d", $_GET["cmode"]);
178
179 catchupArticlesById($link, $ids, $cmode);
180
181 print "<rpc-reply>";
182 print "<counters>";
183 getAllCounters($link, $_GET["omode"]);
184 print "</counters>";
185 print_runtime_info($link);
186 print "</rpc-reply>";
187 }
188
189 if ($subop == "markSelected") {
190
191 $ids = split(",", db_escape_string($_GET["ids"]));
192 $cmode = sprintf("%d", $_GET["cmode"]);
193
194 markArticlesById($link, $ids, $cmode);
195
196 print "<rpc-reply>";
197 print "<counters>";
198 getAllCounters($link, $_GET["omode"]);
199 print "</counters>";
200 print_runtime_info($link);
201 print "</rpc-reply>";
202 }
203
204 if ($subop == "publishSelected") {
205
206 $ids = split(",", db_escape_string($_GET["ids"]));
207 $cmode = sprintf("%d", $_GET["cmode"]);
208
209 publishArticlesById($link, $ids, $cmode);
210
211 print "<rpc-reply>";
212 print "<counters>";
213 getAllCounters($link, $_GET["omode"]);
214 print "</counters>";
215 print_runtime_info($link);
216 print "</rpc-reply>";
217 }
218
219 if ($subop == "sanityCheck") {
220 print "<rpc-reply>";
221 if (sanity_check($link)) {
222 print "<error error-code=\"0\"/>";
223 print_init_params($link);
224 print_runtime_info($link);
225
226 # assign client-passed params to session
227 $_SESSION["client.userAgent"] = $_GET["ua"];
228
229 }
230 print "</rpc-reply>";
231 }
232
233 if ($subop == "globalPurge") {
234
235 print "<rpc-reply>";
236 global_purge_old_posts($link, true);
237 print "</rpc-reply>";
238
239 }
240
241 if ($subop == "getArticleLink") {
242
243 $id = db_escape_string($_GET["id"]);
244
245 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
246 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
247
248 if (db_num_rows($result) == 1) {
249 $link = strip_tags(db_fetch_result($result, 0, "link"));
250 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
251 } else {
252 print "<rpc-reply><error>Article not found</error></rpc-reply>";
253 }
254 }
255
256 if ($subop == "setArticleTags") {
257
258 $id = db_escape_string($_GET["id"]);
259
260 $tags_str = db_escape_string($_GET["tags_str"]);
261
262 $tags = array_unique(trim_array(split(",", $tags_str)));
263
264 db_query($link, "BEGIN");
265
266 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
267 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
268
269 if (db_num_rows($result) == 1) {
270
271 $int_id = db_fetch_result($result, 0, "int_id");
272
273 db_query($link, "DELETE FROM ttrss_tags WHERE
274 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
275
276 foreach ($tags as $tag) {
277 $tag = sanitize_tag($tag);
278
279 if (!tag_is_valid($tag)) {
280 continue;
281 }
282
283 if (preg_match("/^[0-9]*$/", $tag)) {
284 continue;
285 }
286
287 print "<!-- $id : $int_id : $tag -->";
288
289 if ($tag != '') {
290 db_query($link, "INSERT INTO ttrss_tags
291 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
292 }
293 }
294 }
295
296 db_query($link, "COMMIT");
297
298 print "<rpc-reply>
299 <message>$id</message>
300 </rpc-reply>";
301
302 }
303
304 if ($subop == "regenPubKey") {
305
306 print "<rpc-reply>";
307
308 set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
309
310 $new_link = article_publish_url($link);
311
312 print "<link><![CDATA[$new_link]]></link>";
313
314 print "</rpc-reply>";
315
316 }
317
318 if ($subop == "logout") {
319 logout_user();
320 print_error_xml(6);
321 }
322
323 if ($subop == "completeTags") {
324
325 $search = db_escape_string($_REQUEST["search"]);
326
327 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
328 WHERE owner_uid = '".$_SESSION["uid"]."' AND
329 tag_name LIKE '$search%' ORDER BY tag_name
330 LIMIT 10");
331
332 print "<ul>";
333 while ($line = db_fetch_assoc($result)) {
334 print "<li>" . $line["tag_name"] . "</li>";
335 }
336 print "</ul>";
337
338 }
339
340 }
341 ?>