]> git.wh0rd.org Git - tt-rss.git/blob - modules/backend-rpc.php
Merge branch 'master' of madoka.volgo-balt.ru:public_html/testbox/tt-rss into digest...
[tt-rss.git] / modules / backend-rpc.php
1 <?php
2         function handle_rpc_request($link) {
3
4                 $subop = $_REQUEST["subop"];
5
6                 if ($subop == "setprofile") {
7                         $id = db_escape_string($_REQUEST["id"]);
8
9                         $_SESSION["profile"] = $id;
10                         $_SESSION["prefs_cache"] = array();
11                         return;
12                 }
13
14                 if ($subop == "remprofiles") {
15                         $ids = split(",", db_escape_string(trim($_REQUEST["ids"])));
16
17                         foreach ($ids as $id) {
18                                 if ($_SESSION["profile"] != $id) {
19                                         db_query($link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
20                                                 owner_uid = " . $_SESSION["uid"]);
21                                 }
22                         }
23                         return;
24                 }
25
26                 if ($subop == "addprofile") {
27                         $title = db_escape_string(trim($_REQUEST["title"]));
28                         if ($title) {
29                                 db_query($link, "BEGIN");
30
31                                 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
32                                         WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
33
34                                 if (db_num_rows($result) == 0) {
35
36                                         db_query($link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
37                                                 VALUES ('$title', ".$_SESSION["uid"] .")");
38         
39                                         $result = db_query($link, "SELECT id FROM ttrss_settings_profiles WHERE
40                                                 title = '$title'");
41         
42                                         if (db_num_rows($result) != 0) {
43                                                 $profile_id = db_fetch_result($result, 0, "id");
44         
45                                                 if ($profile_id) {
46                                                         initialize_user_prefs($link, $_SESSION["uid"], $profile_id); 
47                                                 }
48                                         }
49                                 }
50
51                                 db_query($link, "COMMIT");
52                         }
53                         return;
54                 }
55
56                 if ($subop == "saveprofile") {
57                         $id = db_escape_string($_REQUEST["id"]);
58                         $title = db_escape_string(trim($_REQUEST["value"]));
59
60                         if ($id == 0) {
61                                 print __("Default profile");
62                                 return;
63                         }
64
65                         if ($title) {
66                                 db_query($link, "BEGIN");
67
68                                 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
69                                         WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
70
71                                 if (db_num_rows($result) == 0) {
72                                         db_query($link, "UPDATE ttrss_settings_profiles
73                                                 SET title = '$title' WHERE id = '$id' AND
74                                                 owner_uid = " . $_SESSION["uid"]);
75                                         print $title;
76                                 } else {
77                                         $result = db_query($link, "SELECT title FROM ttrss_settings_profiles
78                                                 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
79                                         print db_fetch_result($result, 0, "title");
80                                 }
81
82                                 db_query($link, "COMMIT");
83                         }                       
84                         return;
85                 }
86
87                 if ($subop == "remarchive") {
88                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
89
90                         print "<rpc-reply>";
91
92                         foreach ($ids as $id) {
93                                 $result = db_query($link, "DELETE FROM ttrss_archived_feeds WHERE
94                                         (SELECT COUNT(*) FROM ttrss_user_entries 
95                                                 WHERE orig_feed_id = '$id') = 0 AND
96                                                 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
97
98                                 $rc = db_affected_rows($link, $result);
99
100                                 print "<feed id='$id' rc='$rc'/>";
101
102                         }
103
104                         print "</rpc-reply>";
105
106                         return;
107                 }
108
109                 if ($subop == "addfeed") {
110
111                         $feed = db_escape_string($_REQUEST['feed']);
112                         $cat = db_escape_string($_REQUEST['cat']);
113                         $login = db_escape_string($_REQUEST['login']);
114                         $pass = db_escape_string($_REQUEST['pass']);
115
116                         $rc = subscribe_to_feed($link, $feed, $cat, $login, $pass);
117
118                         print "<rpc-reply>";
119                         print "<result code='$rc'/>";
120                         print "</rpc-reply>";
121
122                         return;
123
124                 }
125
126                 if ($subop == "togglepref") {
127                         print "<rpc-reply>";
128
129                         $key = db_escape_string($_REQUEST["key"]);
130
131                         set_pref($link, $key, !get_pref($link, $key));
132
133                         $value = get_pref($link, $key);
134
135                         print "<param-set key=\"$key\" value=\"$value\"/>";
136
137                         print "</rpc-reply>";
138
139                         return;
140                 }
141
142                 if ($subop == "setpref") {
143                         print "<rpc-reply>";
144
145                         $key = db_escape_string($_REQUEST["key"]);
146                         $value = db_escape_string($_REQUEST["value"]);
147
148                         set_pref($link, $key, $value);
149
150                         print "<param-set key=\"$key\" value=\"$value\"/>";
151
152                         print "</rpc-reply>";
153
154                         return;
155                 }
156
157                 if ($subop == "getAllCounters") {
158                         print "<rpc-reply>";                    
159                         print "<counters>";
160
161                         $omode = $_REQUEST["omode"];
162
163                         getAllCounters($link, $omode);
164                         print "</counters>";
165                         print_runtime_info($link);
166                         print "</rpc-reply>";
167
168                         return;
169                 }
170
171                 if ($subop == "mark") {
172                         $mark = $_REQUEST["mark"];
173                         $id = db_escape_string($_REQUEST["id"]);
174
175                         if ($mark == "1") {
176                                 $mark = "true";
177                         } else {
178                                 $mark = "false";
179                         }
180
181                         // FIXME this needs collision testing
182
183                         $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
184                                 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
185
186                         print "<rpc-reply><counters>";
187                         getGlobalCounters($link);
188                         getVirtCounters($link);
189                         getLabelCounters($link);
190                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
191                                 getCategoryCounters($link);
192                         }
193                         print "</counters></rpc-reply>";
194
195                         return;
196                 }
197
198                 if ($subop == "delete") {
199                         $ids = db_escape_string($_REQUEST["ids"]);
200
201                         $result = db_query($link, "DELETE FROM ttrss_user_entries                               
202                                 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
203
204                         print "<rpc-reply><counters>";
205                         getGlobalCounters($link);
206                         getVirtCounters($link);
207                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
208                                 getCategoryCounters($link);
209                         }
210                         print "</counters></rpc-reply>";
211
212                         return;
213                 }
214
215                 if ($subop == "unarchive") {
216                         $ids = db_escape_string($_REQUEST["ids"]);
217
218                         $result = db_query($link, "UPDATE ttrss_user_entries 
219                                 SET feed_id = orig_feed_id, orig_feed_id = NULL
220                                 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
221
222                         print "<rpc-reply><counters>";
223                         getGlobalCounters($link);
224                         getVirtCounters($link);
225                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
226                                 getCategoryCounters($link);
227                         }
228                         print "</counters></rpc-reply>";
229
230                         return;
231                 }
232
233                 if ($subop == "archive") {
234                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
235
236                         foreach ($ids as $id) {
237                                 archive_article($link, $id, $_SESSION["uid"]);
238                         }
239
240                         print "<rpc-reply><counters>";
241                         getGlobalCounters($link);
242                         getVirtCounters($link);
243                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
244                                 getCategoryCounters($link);
245                         }
246                         print "</counters></rpc-reply>";
247
248                         return;
249                 }
250
251
252                 if ($subop == "publ") {
253                         $pub = $_REQUEST["pub"];
254                         $id = db_escape_string($_REQUEST["id"]);
255                         $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
256
257                         if ($pub == "1") {
258                                 $pub = "true";
259                         } else {
260                                 $pub = "false";
261                         }
262
263                         if ($note != 'undefined') {
264                                 $note_qpart = "note = '$note',";
265                         }
266
267                         // FIXME this needs collision testing
268
269                         $result = db_query($link, "UPDATE ttrss_user_entries SET 
270                                 $note_qpart
271                                 published = $pub
272                                 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
273
274
275                         print "<rpc-reply>";
276                         
277                         print "<counters>";
278                         getGlobalCounters($link);
279                         getVirtCounters($link);
280                         getLabelCounters($link);
281                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
282                                 getCategoryCounters($link);
283                         }
284                         print "</counters>";
285
286                         if ($note != 'undefined') {
287                                 $note_size = strlen($note);
288                                 print "<note id=\"$id\" size=\"$note_size\">";
289                                 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
290                                 print "</note>";
291                         }
292
293                         print "</rpc-reply>";
294
295                         return;
296                 }
297
298                 if ($subop == "updateFeed") {
299                         $feed_id = db_escape_string($_REQUEST["feed"]);
300
301                         update_rss_feed($link, $feed_id);
302
303                         print "<rpc-reply>";    
304                         print "<counters>";
305                         getFeedCounter($link, $feed_id);
306                         print "</counters>";
307                         print "</rpc-reply>";
308                         
309                         return;
310                 }
311
312                 if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
313         
314                         $global_unread_caller = sprintf("%d", $_REQUEST["uctr"]);
315                         $global_unread = getGlobalUnread($link);
316
317                         print "<rpc-reply>";
318
319                         print "<counters>";
320
321                         if ($global_unread_caller != $global_unread) {
322
323                                 $omode = $_REQUEST["omode"];
324          
325                                 if (!$omode) $omode = "tflc";
326
327                                 getVirtCounters($link);
328
329                                 if (strchr($omode, "l")) getLabelCounters($link);
330
331                                 if (strchr($omode, "c")) {                      
332                                         if (get_pref($link, 'ENABLE_FEED_CATS')) {
333                                                 getCategoryCounters($link);
334                                         }
335                                 }
336
337                                 if (strchr($omode, "f")) getFeedCounters($link);
338                                 if (strchr($omode, "t")) getTagCounters($link);
339
340                                 getGlobalCounters($link, $global_unread);
341                         }
342  
343                         print "</counters>";
344
345                         print_runtime_info($link);
346
347                         print "</rpc-reply>";
348
349                         return;
350                 }
351
352                 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
353                 if ($subop == "catchupSelected") {
354
355                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
356                         $cmode = sprintf("%d", $_REQUEST["cmode"]);
357
358                         catchupArticlesById($link, $ids, $cmode);
359
360                         print "<rpc-reply>";
361                         print "<counters>";
362                         getAllCounters($link, $_REQUEST["omode"]);
363                         print "</counters>";
364                         print_runtime_info($link);
365                         print "</rpc-reply>";
366
367                         return;
368                 }
369
370                 if ($subop == "markSelected") {
371
372                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
373                         $cmode = sprintf("%d", $_REQUEST["cmode"]);
374
375                         markArticlesById($link, $ids, $cmode);
376
377                         print "<rpc-reply>";
378                         print "<counters>";
379                         getAllCounters($link, $_REQUEST["omode"]);
380                         print "</counters>";
381                         print_runtime_info($link);
382                         print "</rpc-reply>";
383
384                         return;
385                 }
386
387                 if ($subop == "publishSelected") {
388
389                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
390                         $cmode = sprintf("%d", $_REQUEST["cmode"]);
391
392                         publishArticlesById($link, $ids, $cmode);
393
394                         print "<rpc-reply>";
395                         print "<counters>";
396                         getAllCounters($link, $_REQUEST["omode"]);
397                         print "</counters>";
398                         print_runtime_info($link);
399                         print "</rpc-reply>";
400
401                         return;
402                 }
403
404                 if ($subop == "sanityCheck") {
405                         print "<rpc-reply>";
406                         if (sanity_check($link)) {
407                                 print "<error error-code=\"0\"/>";
408                                 print_init_params($link);
409                                 print_runtime_info($link);
410
411                                 # assign client-passed params to session
412                                 $_SESSION["client.userAgent"] = $_REQUEST["ua"];
413
414                         }
415                         print "</rpc-reply>";
416
417                         return;
418                 }               
419
420                 if ($subop == "globalPurge") {
421
422                         print "<rpc-reply>";
423                         global_purge_old_posts($link, true);
424                         print "</rpc-reply>";
425
426                         return;
427                 }
428
429                 if ($subop == "getArticleLink") {
430
431                         $id = db_escape_string($_REQUEST["id"]);
432
433                         $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
434                                 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
435
436                         if (db_num_rows($result) == 1) {
437                                 $link = htmlspecialchars(strip_tags(db_fetch_result($result, 0, "link")));
438                                 print "<rpc-reply><link>$link</link><id>$id</id></rpc-reply>";
439                         } else {
440                                 print "<rpc-reply><error>Article not found</error></rpc-reply>";
441                         }
442
443                         return;
444                 }
445
446                 if ($subop == "setArticleTags") {
447
448                         global $memcache;
449
450                         $id = db_escape_string($_REQUEST["id"]);
451
452                         $tags_str = db_escape_string($_REQUEST["tags_str"]);
453
454                         $tags = array_unique(trim_array(split(",", $tags_str)));
455
456                         db_query($link, "BEGIN");
457
458                         $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
459                                 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
460
461                         if (db_num_rows($result) == 1) {
462
463                                 $int_id = db_fetch_result($result, 0, "int_id");
464
465                                 db_query($link, "DELETE FROM ttrss_tags WHERE 
466                                         post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
467
468                                 foreach ($tags as $tag) {
469                                         $tag = sanitize_tag($tag);      
470
471                                         if (!tag_is_valid($tag)) {
472                                                 continue;
473                                         }
474
475                                         if (preg_match("/^[0-9]*$/", $tag)) {
476                                                 continue;
477                                         }
478
479 //                                      print "<!-- $id : $int_id : $tag -->";
480                                         
481                                         if ($tag != '') {
482                                                 db_query($link, "INSERT INTO ttrss_tags 
483                                                         (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
484                                         }
485                                 }
486                         }
487
488                         db_query($link, "COMMIT");
489
490                         if ($memcache) {
491                                 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
492                                 $memcache->delete($obj_id);
493                         }
494
495                         $tags_str = format_tags_string(get_article_tags($link, $id), $id);
496
497                         print "<rpc-reply>
498                                 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
499                                 </rpc-reply>";
500
501                         return;
502                 }
503
504                 if ($subop == "regenPubKey") {
505
506                         print "<rpc-reply>";
507
508                         set_pref($link, "_PREFS_PUBLISH_KEY", generate_publish_key(), $_SESSION["uid"]);
509
510                         $new_link = article_publish_url($link);         
511
512                         print "<link><![CDATA[$new_link]]></link>";
513
514                         print "</rpc-reply>";
515
516                         return;
517                 }
518
519                 if ($subop == "regenOPMLKey") {
520
521                         print "<rpc-reply>";
522                         set_pref($link, " _PREFS_OPML_PUBLISH_KEY", generate_publish_key(), $_SESSION["uid"]);
523                         $new_link = opml_publish_url($link);            
524                         print "<link><![CDATA[$new_link]]></link>";
525                         print "</rpc-reply>";
526                         return;
527                 }
528
529                 if ($subop == "logout") {
530                         logout_user();
531                         print_error_xml(6);
532                         return;
533                 }
534
535                 if ($subop == "completeTags") {
536
537                         $search = db_escape_string($_REQUEST["search"]);
538
539                         $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags 
540                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
541                                 tag_name LIKE '$search%' ORDER BY tag_name
542                                 LIMIT 10");
543
544                         print "<ul>";
545                         while ($line = db_fetch_assoc($result)) {
546                                 print "<li>" . $line["tag_name"] . "</li>";
547                         }
548                         print "</ul>";
549
550                         return;
551                 }
552
553                 if ($subop == "purge") {
554                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
555                         $days = sprintf("%d", $_REQUEST["days"]);
556
557                         print "<rpc-reply>";
558
559                         print "<message><![CDATA[";
560
561                         foreach ($ids as $id) {
562
563                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
564                                         id = '$id' AND owner_uid = ".$_SESSION["uid"]);
565
566                                 if (db_num_rows($result) == 1) {
567                                         purge_feed($link, $id, $days, true);
568                                 }
569                         }
570
571                         print "]]></message>";
572
573                         print "</rpc-reply>";
574
575                         return;
576                 }
577
578 /*              if ($subop == "setScore") {
579                         $id = db_escape_string($_REQUEST["id"]);
580                         $score = sprintf("%d", $_REQUEST["score"]);
581
582                         $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
583                                 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
584
585                         print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
586
587                         return;
588
589                 } */
590
591                 if ($subop == "getArticles") {
592                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
593
594                         print "<rpc-reply>";
595
596                         foreach ($ids as $id) {
597                                 if ($id) {
598                                         outputArticleXML($link, $id, 0, false);
599                                 }
600                         }
601                         print "</rpc-reply>";
602
603                         return;
604                 }
605
606                 if ($subop == "checkDate") {
607
608                         $date = db_escape_string($_REQUEST["date"]);
609                         $date_parsed = strtotime($date);
610
611                         print "<rpc-reply>";
612
613                         if ($date_parsed) {
614                                 print "<result>1</result>";
615                         } else {
616                                 print "<result>0</result>";
617                         }
618
619                         print "</rpc-reply>";
620
621                         return;
622                 }
623
624                 if ($subop == "removeFromLabel") {
625
626                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
627                         $label_id = db_escape_string($_REQUEST["lid"]);
628
629                         $label = db_escape_string(label_find_caption($link, $label_id, 
630                                 $_SESSION["uid"]));
631
632                         print "<rpc-reply>";
633                         print "<info-for-headlines>";
634
635                         if ($label) {
636
637                                 foreach ($ids as $id) {
638                                         label_remove_article($link, $id, $label, $_SESSION["uid"]);
639
640                                         print "<entry id=\"$id\"><![CDATA[";
641
642                                         $labels = get_article_labels($link, $id, $_SESSION["uid"]);
643                                         print format_article_labels($labels, $id);
644
645                                         print "]]></entry>";
646
647                                 }
648                         }
649
650                         print "</info-for-headlines>";
651
652                         print "<counters>";
653                         getAllCounters($link, $omode);
654                         print "</counters>";
655                         print "</rpc-reply>";
656
657                         return;
658                 }
659
660                 if ($subop == "assignToLabel") {
661
662                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
663                         $label_id = db_escape_string($_REQUEST["lid"]);
664
665                         $label = db_escape_string(label_find_caption($link, $label_id, 
666                                 $_SESSION["uid"]));
667
668                         print "<rpc-reply>";                    
669
670                         print "<info-for-headlines>";
671
672                         if ($label) {
673
674                                 foreach ($ids as $id) {
675                                         label_add_article($link, $id, $label, $_SESSION["uid"]);
676
677                                         print "<entry id=\"$id\"><![CDATA[";
678
679                                         $labels = get_article_labels($link, $id, $_SESSION["uid"]);
680                                         print format_article_labels($labels, $id);
681
682                                         print "]]></entry>";
683
684                                 }
685                         }
686
687                         print "</info-for-headlines>";
688
689                         print "<counters>";
690                         getAllCounters($link, $omode);
691                         print "</counters>";
692                         print "</rpc-reply>";
693
694                         return;
695                 }
696
697                 if ($subop == "updateFeedBrowser") {
698
699                         $search = db_escape_string($_REQUEST["search"]);
700                         $limit = db_escape_string($_REQUEST["limit"]);
701                         $mode = db_escape_string($_REQUEST["mode"]);
702
703                         print "<rpc-reply>";
704                         print "<content>";
705                         print "<![CDATA[";
706                         $ctr = print_feed_browser($link, $search, $limit, $mode);
707                         print "]]>";
708                         print "</content>";
709                         print "<num-results value=\"$ctr\"/>";
710                         print "<mode value=\"$mode\"/>";
711                         print "</rpc-reply>";
712
713                         return;
714                 }
715
716
717                 if ($subop == "massSubscribe") {
718
719                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
720                         $mode = $_REQUEST["mode"];
721
722                         $subscribed = array();
723
724                         foreach ($ids as $id) {
725
726                                 if ($mode == 1) {
727                                         $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
728                                                 WHERE id = '$id'");
729                                 } else if ($mode == 2) {
730                                         $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
731                                                 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
732                                         $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
733                                         $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
734                                 }
735         
736                                 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
737                                 $title = db_escape_string(db_fetch_result($result, 0, "title"));
738         
739                                 $title_orig = db_fetch_result($result, 0, "title");
740         
741                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
742                                                 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
743         
744                                 if (db_num_rows($result) == 0) {                        
745                                         if ($mode == 1) {
746                                                 $result = db_query($link,
747                                                         "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) 
748                                                         VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
749                                         } else if ($mode == 2) {
750                                                 $result = db_query($link,
751                                                         "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url) 
752                                                         VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
753                                         }
754                                         array_push($subscribed, $title_orig);
755                                 }
756                         }
757
758                         $num_feeds = count($subscribed);
759
760                         print "<rpc-reply>";
761                         print "<num-feeds value='$num_feeds'/>";
762                         print "</rpc-reply>";
763
764                         return;
765                 } 
766
767                 if ($subop == "download") {
768                         $stage = (int) $_REQUEST["stage"];
769                         $cidt = (int)db_escape_string($_REQUEST["cidt"]);
770                         $cidb = (int)db_escape_string($_REQUEST["cidb"]);
771                         $sync = db_escape_string($_REQUEST["sync"]);
772                         //$amount = (int) $_REQUEST["amount"];
773                         //$unread_only = db_escape_string($_REQUEST["unread_only"]);
774                         //if (!$amount) $amount = 50;
775
776                         /* Amount is not used by the frontend offline.js anymore, it goes by
777                          * date_qpart below + cidb/cidt IDs */
778
779                         $amount = 2000;
780                         $unread_only = true;
781
782                         print "<rpc-reply>";
783
784                         $sync = split(";", $sync);
785
786                         print "<sync>";
787
788                         if (count($sync) > 0) {
789                                 if (strtotime($sync[0])) {
790                                         $last_online = db_escape_string($sync[0]);
791
792                                         print "<sync-point><![CDATA[$last_online]]></sync-point>";
793                                         
794                                         for ($i = 1; $i < count($sync); $i++) {
795                                                 $e = split(",", $sync[$i]);
796
797                                                 if (count($e) == 3) {
798
799                                                         $id = (int) $e[0];
800                                                         $unread = bool_to_sql_bool((bool) $e[1]);
801                                                         $marked = (bool)$e[2];
802
803                                                         if ($marked) {
804                                                                 $marked = bool_to_sql_bool($marked);
805                                                                 $marked_qpart = "marked = $marked,";
806                                                         }
807
808                                                         $query = "UPDATE ttrss_user_entries SET 
809                                                                 $marked_qpart
810                                                                 unread = $unread, 
811                                                                 last_read = '$last_online' 
812                                                         WHERE ref_id = '$id' AND 
813                                                                 (last_read IS NULL OR last_read < '$last_online') AND
814                                                                 owner_uid = ".$_SESSION["uid"];
815
816                                                         $result = db_query($link, $query);
817
818                                                         print "<sync-ok id=\"$id\"/>";
819
820                                                 }
821                                         }
822
823                                         /* Maybe we need to further update local DB for this client */
824
825                                         $query = "SELECT ref_id,unread,marked FROM ttrss_user_entries
826                                                 WHERE last_read >= '$last_online' AND
827                                                                 owner_uid = ".$_SESSION["uid"] . " LIMIT 1000";
828
829                                         $result = db_query($link, $query);
830
831                                         while ($line = db_fetch_assoc($result)) {
832                                                 $unread = (int) sql_bool_to_bool($line["unread"]);
833                                                 $marked = (int) sql_bool_to_bool($line["marked"]);
834
835                                                 print "<sync-ok unread=\"$unread\" marked=\"$marked\" 
836                                                         id=\"".$line["ref_id"]."\"/>";
837                                         }
838
839                                 }
840                         }
841
842                         print "</sync>";
843
844                         if ($stage == 0) {
845                                 print "<feeds>";
846
847                                 $result = db_query($link, "SELECT id, title, cat_id FROM
848                                         ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"]);
849
850                                 while ($line = db_fetch_assoc($result)) {
851
852                                         $has_icon = (int) feed_has_icon($line["id"]);
853
854                                         print "<feed has_icon=\"$has_icon\" 
855                                                 cat_id=\"".(int)$line["cat_id"]."\" id=\"".$line["id"]."\"><![CDATA[";
856                                         print $line["title"];
857                                         print "]]></feed>";
858                                 }
859
860                                 print "</feeds>";
861
862                                 print "<feed-categories>";
863
864                                 $result = db_query($link, "SELECT id, title, collapsed FROM
865                                         ttrss_feed_categories WHERE owner_uid = ".$_SESSION["uid"]);
866
867                                         print "<category id=\"0\" collapsed=\"".
868                                                 (int)get_pref($link, "_COLLAPSED_UNCAT")."\"><![CDATA[";
869                                         print __("Uncategorized");
870                                         print "]]></category>";
871
872                                         print "<category id=\"-1\" collapsed=\"".
873                                                 (int)get_pref($link, "_COLLAPSED_SPECIAL")."\"><![CDATA[";
874                                         print __("Special");
875                                         print "]]></category>";
876
877                                         print "<category id=\"-2\" collapsed=\"".
878                                                 (int)get_pref($link, "_COLLAPSED_LABELS")."\"><![CDATA[";
879                                         print __("Labels");
880                                         print "]]></category>";
881
882                                 while ($line = db_fetch_assoc($result)) {
883                                         print "<category 
884                                                 id=\"".$line["id"]."\"
885                                                 collapsed=\"".(int)sql_bool_to_bool($line["collapsed"])."\"><![CDATA[";
886                                         print $line["title"];
887                                         print "]]></category>";
888                                 }
889
890                                 print "</feed-categories>";
891
892                                 print "<labels>";
893
894                                 $result = db_query($link, "SELECT * FROM
895                                         ttrss_labels2 WHERE owner_uid = ".$_SESSION["uid"]);
896
897                                 while ($line = db_fetch_assoc($result)) {
898                                         print "<label
899                                                 id=\"".$line["id"]."\"
900                                                 fg_color=\"".$line["fg_color"]."\"
901                                                 bg_color=\"".$line["bg_color"]."\"
902                                                 ><![CDATA[";
903                                         print $line["caption"];
904                                         print "]]></label>";
905                                 }
906
907
908                                 print "</labels>";
909
910                         }
911
912                         if ($stage > 0) {
913                                 print "<articles>";
914
915                                 $limit = 10;
916                                 $skip = $limit*($stage-1);
917
918                                 print "<limit value=\"$limit\"/>";
919
920                                 if ($amount > 0) $amount -= $skip;
921
922                                 if ($amount > 0) {
923
924                                         $limit = min($limit, $amount);
925
926                                         if ($unread_only) {
927                                                 $unread_qpart = "(unread = true OR marked = true) AND ";
928                                         }
929
930                                         if ($cidt && $cidb) {
931                                                 $cid_qpart =  "(ttrss_entries.id > $cidt OR ttrss_entries.id < $cidb) AND ";
932                                         }
933
934                                         if (DB_TYPE == "pgsql") {
935                                                 $date_qpart = "updated >= NOW() - INTERVAL '1 week' AND";
936                                         } else {
937                                                 $date_qpart = "updated >= DATE_SUB(NOW(), INTERVAL 1 WEEK) AND";
938                                         }                       
939
940                                         $result = db_query($link,
941                                                 "SELECT DISTINCT ttrss_entries.id,ttrss_entries.title,
942                                                         guid,link,comments,
943                                                         feed_id,content,updated,unread,marked FROM
944                                                         ttrss_user_entries,ttrss_entries,ttrss_feeds
945                                                 WHERE $unread_qpart $cid_qpart $date_qpart
946                                                         ttrss_feeds.id = feed_id AND
947                                                         ref_id = ttrss_entries.id AND 
948                                                         ttrss_user_entries.owner_uid = ".$_SESSION["uid"]."
949                                                         ORDER BY updated DESC LIMIT $limit OFFSET $skip");
950
951                                         if (function_exists('json_encode')) {
952
953                                                 while ($line = db_fetch_assoc($result)) {
954                                                         print "<article><![CDATA[";
955         
956                                                         $line["marked"] = (int)sql_bool_to_bool($line["marked"]);
957                                                         $line["unread"] = (int)sql_bool_to_bool($line["unread"]);
958
959                                                         $line["labels"] = get_article_labels($link, $line["id"]);
960
961 //                                                      too slow :(                                                     
962 //                                                      $line["tags"] = format_tags_string(
963 //                                                              get_article_tags($link, $line["id"]), $line["id"]);
964         
965                                                         print json_encode($line);
966                                                         print "]]></article>";
967                                                 }       
968                                         }
969
970                                 }
971
972                                 print "</articles>";
973
974                         }
975
976                         print "</rpc-reply>";
977
978                         return;
979                 }
980
981                 if ($subop == "digest-get-contents") {
982                         $article_id = db_escape_string($_REQUEST['article_id']);
983
984                         $result = db_query($link, "SELECT content 
985                                 FROM ttrss_entries, ttrss_user_entries
986                                 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
987
988                         print "<rpc-reply>";
989
990                         print "<article id=\"$article_id\"><![CDATA[";
991
992                         $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
993
994                         print $content;
995
996                         print "]]></article>";
997
998                         print "</rpc-reply>";
999
1000                         return;
1001                 }
1002
1003                 if ($subop == "digest-update") {
1004                         $feed_id = db_escape_string($_REQUEST['feed_id']);
1005                         $offset = db_escape_string($_REQUEST['offset']);
1006                         $seq = db_escape_string($_REQUEST['seq']);
1007                 
1008                         if (!$feed_id) $feed_id = -4;
1009                         if (!$offset) $offset = 0;
1010                         print "<rpc-reply>";
1011
1012                         print "<seq>$seq</seq>";
1013
1014                         $headlines = api_get_headlines($link, $feed_id, 10, $offset,
1015                                 '', ($feed_id == -4), true, false, "unread", "updated DESC");
1016
1017                         //function api_get_headlines($link, $feed_id, $limit, $offset,
1018                         //              $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
1019
1020                         print "<headlines-title><![CDATA[" . getFeedTitle($link, $feed_id) . 
1021                                 "]]></headlines-title>";
1022
1023                         print "<headlines><![CDATA[" . json_encode($headlines) . "]]></headlines>";
1024
1025                         print "</rpc-reply>";
1026                         return;
1027                 }
1028
1029                 if ($subop == "digest-init") {
1030                         print "<rpc-reply>";
1031
1032                         $tmp_feeds = api_get_feeds($link, false, true, false, 0);
1033                         $feeds = array();
1034
1035                         foreach ($tmp_feeds as $f) {
1036                                 if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
1037                         }
1038
1039                         print "<feeds><![CDATA[" . json_encode($feeds) . "]]></feeds>";
1040
1041                         print "</rpc-reply>";
1042                         return;
1043                 }
1044
1045                 if ($subop == "catchupFeed") {
1046
1047                         $feed_id = db_escape_string($_REQUEST['feed_id']);
1048                         $is_cat = db_escape_string($_REQUEST['is_cat']);
1049
1050                         print "<rpc-reply>";
1051
1052                         catchup_feed($link, $feed_id, $is_cat);
1053
1054                         print "</rpc-reply>";
1055
1056                         return;
1057                 }
1058
1059                 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
1060         }
1061 ?>