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