root/modules/backend-rpc.php

Revision 2451, 9.1 kB (checked in by www-ttrss, 6 months ago)

[project @ getNeighborIds: add scope, add automatic prefetch for articles under mouse]

Line 
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             if (ENABLE_UPDATE_DAEMON) {
139
140                 if ($subop == "forceUpdateAllFeeds") {
141
142                     $result = db_query($link, "SELECT count(id) AS cid FROM
143                         ttrss_scheduled_updates WHERE feed_id IS NULL AND
144                             owner_uid = " . $_SESSION["uid"]);
145     
146                     $cid = db_fetch_result($result, 0, "cid");
147
148                     if ($cid == 0) {
149     
150                         db_query($link, "INSERT INTO ttrss_scheduled_updates
151                             (owner_uid, feed_id, entered) VALUES
152                             (".$_SESSION["uid"].", NULL, NOW())");
153                     
154                     }
155                 }
156                 
157             } else {   
158                 update_all_feeds($link, $subop == "forceUpdateAllFeeds");
159             }
160
161             $global_unread_caller = sprintf("%d", $_GET["uctr"]);
162             $global_unread = getGlobalUnread($link);
163
164             print "<rpc-reply>";
165
166             print "<counters>";
167
168              $omode = $_GET["omode"];
169     
170              if (!$omode) $omode = "tflc";
171
172              if (strchr($omode, "l")) getLabelCounters($link);
173
174              if (strchr($omode, "c")) {           
175                  if (get_pref($link, 'ENABLE_FEED_CATS')) {
176                      getCategoryCounters($link);
177                  }
178             }
179
180             if ($global_unread_caller != $global_unread) {
181
182                  if (strchr($omode, "f")) getFeedCounters($link);
183                  if (strchr($omode, "t")) getTagCounters($link);
184             }
185
186              getGlobalCounters($link, $global_unread);
187
188             print "</counters>";
189
190             print_runtime_info($link);
191
192             print "</rpc-reply>";
193
194             return;
195         }
196
197         /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
198         if ($subop == "catchupSelected") {
199
200             $ids = split(",", db_escape_string($_GET["ids"]));
201             $cmode = sprintf("%d", $_GET["cmode"]);
202
203             catchupArticlesById($link, $ids, $cmode);
204
205             print "<rpc-reply>";
206             print "<counters>";
207             getAllCounters($link, $_GET["omode"]);
208             print "</counters>";
209             print_runtime_info($link);
210             print "</rpc-reply>";
211
212             return;
213         }
214
215         if ($subop == "markSelected") {
216
217             $ids = split(",", db_escape_string($_GET["ids"]));
218             $cmode = sprintf("%d", $_GET["cmode"]);
219
220             markArticlesById($link, $ids, $cmode);
221
222             print "<rpc-reply>";
223             print "<counters>";
224             getAllCounters($link, $_GET["omode"]);
225             print "</counters>";
226             print_runtime_info($link);
227             print "</rpc-reply>";
228
229             return;
230         }
231
232         if ($subop == "publishSelected") {
233
234             $ids = split(",", db_escape_string($_GET["ids"]));
235             $cmode = sprintf("%d", $_GET["cmode"]);
236
237             publishArticlesById($link, $ids, $cmode);
238
239             print "<rpc-reply>";
240             print "<counters>";
241             getAllCounters($link, $_GET["omode"]);
242             print "</counters>";
243             print_runtime_info($link);
244             print "</rpc-reply>";
245
246             return;
247         }
248
249         if ($subop == "sanityCheck") {
250             print "<rpc-reply>";
251             if (sanity_check($link)) {
252                 print "<error error-code=\"0\"/>";
253                 print_init_params($link);
254                 print_runtime_info($link);
255
256                 # assign client-passed params to session
257                 $_SESSION["client.userAgent"] = $_GET["ua"];
258
259             }
260             print "</rpc-reply>";
261
262             return;
263         }       
264
265         if ($subop == "globalPurge") {
266
267             print "<rpc-reply>";
268             global_purge_old_posts($link, true);
269             print "</rpc-reply>";
270
271             return;
272         }
273
274         if ($subop == "getArticleLink") {
275
276             $id = db_escape_string($_GET["id"]);
277
278             $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
279                 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
280
281             if (db_num_rows($result) == 1) {
282                 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
283                 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
284             } else {
285                 print "<rpc-reply><error>Article not found</error></rpc-reply>";
286             }
287
288             return;
289         }
290
291         if ($subop == "setArticleTags") {
292
293             $id = db_escape_string($_GET["id"]);
294
295             $tags_str = db_escape_string($_GET["tags_str"]);
296
297             $tags = array_unique(trim_array(split(",", $tags_str)));
298
299             db_query($link, "BEGIN");
300
301             $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
302                 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
303
304             if (db_num_rows($result) == 1) {
305
306                 $int_id = db_fetch_result($result, 0, "int_id");
307
308                 db_query($link, "DELETE FROM ttrss_tags WHERE
309                     post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
310
311                 foreach ($tags as $tag) {
312                     $tag = sanitize_tag($tag);   
313
314                     if (!tag_is_valid($tag)) {
315                         continue;
316                     }
317
318                     if (preg_match("/^[0-9]*$/", $tag)) {
319                         continue;
320                     }
321
322                     print "<!-- $id : $int_id : $tag -->";
323                     
324                     if ($tag != '') {
325                         db_query($link, "INSERT INTO ttrss_tags
326                             (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
327                     }
328                 }
329             }
330
331             db_query($link, "COMMIT");
332
333             print "<rpc-reply>
334                 <message>$id</message>
335                 </rpc-reply>";
336
337             return;
338         }
339
340         if ($subop == "regenPubKey") {
341
342             print "<rpc-reply>";
343
344             set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
345
346             $new_link = article_publish_url($link);       
347
348             print "<link><![CDATA[$new_link]]></link>";
349
350             print "</rpc-reply>";
351
352             return;
353         }
354
355         if ($subop == "logout") {
356             logout_user();
357             print_error_xml(6);
358             return;
359         }
360
361         if ($subop == "completeTags") {
362
363             $search = db_escape_string($_REQUEST["search"]);
364
365             $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
366                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
367                   tag_name LIKE '$search%' ORDER BY tag_name
368                 LIMIT 10");
369
370             print "<ul>";
371             while ($line = db_fetch_assoc($result)) {
372                 print "<li>" . $line["tag_name"] . "</li>";
373             }
374             print "</ul>";
375
376             return;
377         }
378
379         if ($subop == "purge") {
380             $ids = split(",", db_escape_string($_GET["ids"]));
381             $days = sprintf("%d", $_GET["days"]);
382
383             print "<rpc-reply>";
384
385             print "<message><![CDATA[";
386
387             foreach ($ids as $id) {
388
389                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
390                     id = '$id' AND owner_uid = ".$_SESSION["uid"]);
391
392                 if (db_num_rows($result) == 1) {
393                     purge_feed($link, $id, $days, true);
394                 }
395             }
396
397             print "]]></message>";
398
399             print "</rpc-reply>";
400
401             return;
402         }
403
404         if ($subop == "setScore") {
405             $id = db_escape_string($_REQUEST["id"]);
406             $score = sprintf("%d", $_REQUEST["score"]);
407
408             $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
409                 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
410
411             print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
412
413             return;
414
415         }
416
417         if ($subop == "getArticles") {
418             $ids = split(",", db_escape_string($_REQUEST["ids"]));
419
420             print "<rpc-reply>";
421
422             foreach ($ids as $id) {
423                 if ($id) {
424                     outputArticleXML($link, $id, 0, false);
425                 }
426             }
427             print "</rpc-reply>";
428
429             return;
430         }
431
432         print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
433     }
434 ?>
435
Note: See TracBrowser for help on using the browser.