]> git.wh0rd.org Git - tt-rss.git/blob - modules/backend-rpc.php
assorted labels bugfixes and UI work
[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                         print "<rpc-reply>
311                                 <message>$id</message>
312                                 </rpc-reply>";
313
314                         return;
315                 }
316
317                 if ($subop == "regenPubKey") {
318
319                         print "<rpc-reply>";
320
321                         set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key());
322
323                         $new_link = article_publish_url($link);         
324
325                         print "<link><![CDATA[$new_link]]></link>";
326
327                         print "</rpc-reply>";
328
329                         return;
330                 }
331
332                 if ($subop == "logout") {
333                         logout_user();
334                         print_error_xml(6);
335                         return;
336                 }
337
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
353                         return;
354                 }
355
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
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
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
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
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
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
460                         print "<rpc-reply><counters>";
461
462                         if ($label) {
463                                 getGlobalCounters($link);
464                                 getLabelCounters($link);
465                                 if (get_pref($link, 'ENABLE_FEED_CATS')) {
466                                         getCategoryCounters($link);
467                                 }
468                         }
469
470                         print "</counters></rpc-reply>";
471
472                         return;
473                 }
474
475                 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
476         }
477 ?>