]> git.wh0rd.org Git - tt-rss.git/blob - modules/backend-rpc.php
assign/remove to label rpc: use JSON
[tt-rss.git] / modules / backend-rpc.php
1 <?php
2         function handle_rpc_request($link) {
3
4                 $subop = $_REQUEST["subop"];
5                 $seq = (int) $_REQUEST["seq"];
6
7                 if ($subop == "setprofile") {
8                         $id = db_escape_string($_REQUEST["id"]);
9
10                         $_SESSION["profile"] = $id;
11                         $_SESSION["prefs_cache"] = array();
12                         return;
13                 }
14
15                 if ($subop == "remprofiles") {
16                         $ids = split(",", db_escape_string(trim($_REQUEST["ids"])));
17
18                         foreach ($ids as $id) {
19                                 if ($_SESSION["profile"] != $id) {
20                                         db_query($link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
21                                                 owner_uid = " . $_SESSION["uid"]);
22                                 }
23                         }
24                         return;
25                 }
26
27                 if ($subop == "addprofile") {
28                         $title = db_escape_string(trim($_REQUEST["title"]));
29                         if ($title) {
30                                 db_query($link, "BEGIN");
31
32                                 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
33                                         WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
34
35                                 if (db_num_rows($result) == 0) {
36
37                                         db_query($link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
38                                                 VALUES ('$title', ".$_SESSION["uid"] .")");
39         
40                                         $result = db_query($link, "SELECT id FROM ttrss_settings_profiles WHERE
41                                                 title = '$title'");
42         
43                                         if (db_num_rows($result) != 0) {
44                                                 $profile_id = db_fetch_result($result, 0, "id");
45         
46                                                 if ($profile_id) {
47                                                         initialize_user_prefs($link, $_SESSION["uid"], $profile_id); 
48                                                 }
49                                         }
50                                 }
51
52                                 db_query($link, "COMMIT");
53                         }
54                         return;
55                 }
56
57                 if ($subop == "saveprofile") {
58                         $id = db_escape_string($_REQUEST["id"]);
59                         $title = db_escape_string(trim($_REQUEST["value"]));
60
61                         if ($id == 0) {
62                                 print __("Default profile");
63                                 return;
64                         }
65
66                         if ($title) {
67                                 db_query($link, "BEGIN");
68
69                                 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
70                                         WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
71
72                                 if (db_num_rows($result) == 0) {
73                                         db_query($link, "UPDATE ttrss_settings_profiles
74                                                 SET title = '$title' WHERE id = '$id' AND
75                                                 owner_uid = " . $_SESSION["uid"]);
76                                         print $title;
77                                 } else {
78                                         $result = db_query($link, "SELECT title FROM ttrss_settings_profiles
79                                                 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
80                                         print db_fetch_result($result, 0, "title");
81                                 }
82
83                                 db_query($link, "COMMIT");
84                         }                       
85                         return;
86                 }
87
88                 if ($subop == "remarchive") {
89                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
90
91                         print "<rpc-reply>";
92
93                         foreach ($ids as $id) {
94                                 $result = db_query($link, "DELETE FROM ttrss_archived_feeds WHERE
95                                         (SELECT COUNT(*) FROM ttrss_user_entries 
96                                                 WHERE orig_feed_id = '$id') = 0 AND
97                                                 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
98
99                                 $rc = db_affected_rows($link, $result);
100
101                                 print "<feed id='$id' rc='$rc'/>";
102
103                         }
104
105                         print "</rpc-reply>";
106
107                         return;
108                 }
109
110                 if ($subop == "addfeed") {
111
112                         $feed = db_escape_string($_REQUEST['feed']);
113                         $cat = db_escape_string($_REQUEST['cat']);
114                         $login = db_escape_string($_REQUEST['login']);
115                         $pass = db_escape_string($_REQUEST['pass']);
116
117                         $rc = subscribe_to_feed($link, $feed, $cat, $login, $pass);
118
119                         print "<rpc-reply>";
120                         print "<result code='$rc'/>";
121                         print "</rpc-reply>";
122
123                         return;
124
125                 }
126
127                 if ($subop == "extractfeedurls") {
128                         print "<rpc-reply>";
129
130                         $urls = get_feeds_from_html($_REQUEST['url']);
131                         print "<urls><![CDATA[" . json_encode($urls) . "]]></urls>";
132
133                         print "</rpc-reply>";
134                         return;
135                 }
136
137                 if ($subop == "togglepref") {
138                         header("Content-Type: text/plain");
139
140                         $key = db_escape_string($_REQUEST["key"]);
141                         set_pref($link, $key, !get_pref($link, $key));
142                         $value = get_pref($link, $key);
143
144                         print json_encode(array("param" =>$key, "value" => $value));
145                         return;
146                 }
147
148                 if ($subop == "setpref") {
149                         print "<rpc-reply>";
150
151                         $key = db_escape_string($_REQUEST["key"]);
152                         $value = db_escape_string($_REQUEST["value"]);
153
154                         set_pref($link, $key, $value);
155
156                         print "<param-set key=\"$key\" value=\"$value\"/>";
157
158                         print "</rpc-reply>";
159
160                         return;
161                 }
162
163                 if ($subop == "mark") {
164                         header("Content-Type: text/plain");
165
166                         $mark = $_REQUEST["mark"];
167                         $id = db_escape_string($_REQUEST["id"]);
168
169                         if ($mark == "1") {
170                                 $mark = "true";
171                         } else {
172                                 $mark = "false";
173                         }
174
175                         // FIXME this needs collision testing
176
177                         $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
178                                 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
179
180                         print json_encode(array("message" => "UPDATE_COUNTERS"));
181                         return;
182                 }
183
184                 if ($subop == "delete") {
185                         header("Content-Type: text/plain");
186
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 json_encode(array("message" => "UPDATE_COUNTERS"));
193                         return;
194                 }
195
196                 if ($subop == "unarchive") {
197                         header("Content-Type: text/plain");
198
199                         $ids = db_escape_string($_REQUEST["ids"]);
200
201                         $result = db_query($link, "UPDATE ttrss_user_entries 
202                                 SET feed_id = orig_feed_id, orig_feed_id = NULL
203                                 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
204
205                         print json_encode(array("message" => "UPDATE_COUNTERS"));
206                         return;
207                 }
208
209                 if ($subop == "archive") {
210                         header("Content-Type: text/plain");
211
212                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
213
214                         foreach ($ids as $id) {
215                                 archive_article($link, $id, $_SESSION["uid"]);
216                         }
217
218                         print json_encode(array("message" => "UPDATE_COUNTERS"));
219                         return;
220                 }
221
222
223                 if ($subop == "publ") {
224                         $pub = $_REQUEST["pub"];
225                         $id = db_escape_string($_REQUEST["id"]);
226                         $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
227
228                         if ($pub == "1") {
229                                 $pub = "true";
230                         } else {
231                                 $pub = "false";
232                         }
233
234                         if ($note != 'undefined') {
235                                 $note_qpart = "note = '$note',";
236                         }
237
238                         // FIXME this needs collision testing
239
240                         $result = db_query($link, "UPDATE ttrss_user_entries SET 
241                                 $note_qpart
242                                 published = $pub
243                                 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
244
245
246                         print "<rpc-reply>";
247                         
248                         if ($note != 'undefined') {
249                                 $note_size = strlen($note);
250                                 print "<note id=\"$id\" size=\"$note_size\">";
251                                 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
252                                 print "</note>";
253                         }
254
255                         print "<message>UPDATE_COUNTERS</message>";
256
257                         print "</rpc-reply>";
258
259                         return;
260                 }
261
262 /*              if ($subop == "updateFeed") {
263                         $feed_id = db_escape_string($_REQUEST["feed"]);
264
265                         update_rss_feed($link, $feed_id);
266
267                         print "<rpc-reply>";
268                         print "<message>UPDATE_COUNTERS</message>";
269                         print "</rpc-reply>";
270
271                         return;
272                 } */
273
274                 if ($subop == "updateAllFeeds" || $subop == "getAllCounters") {
275
276                         header("Content-Type: text/plain");
277
278                         $last_article_id = (int) $_REQUEST["last_article_id"];  
279
280                         $reply = array();
281
282                         if ($seq) $reply['seq'] = $seq;
283
284                         if ($last_article_id != getLastArticleId($link)) {
285                                 $omode = $_REQUEST["omode"];
286
287                                 if ($omode != "T") 
288                                         $reply['counters'] = getAllCounters($link, $omode);
289                                 else
290                                         $reply['counters'] = getGlobalCounters($link);
291                         }
292  
293                         $reply['runtime-info'] = make_runtime_info($link);
294
295
296                         print json_encode($reply);
297                         return;
298                 }
299
300                 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
301                 if ($subop == "catchupSelected") {
302                         header("Content-Type: text/plain");
303
304                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
305                         $cmode = sprintf("%d", $_REQUEST["cmode"]);
306
307                         catchupArticlesById($link, $ids, $cmode);
308
309                         print json_encode(array("message" => "UPDATE_COUNTERS"));
310                         return;
311                 }
312
313                 if ($subop == "markSelected") {
314                         header("Content-Type: text/plain");
315
316                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
317                         $cmode = sprintf("%d", $_REQUEST["cmode"]);
318
319                         markArticlesById($link, $ids, $cmode);
320
321                         print json_encode(array("message" => "UPDATE_COUNTERS"));
322                         return;
323                 }
324
325                 if ($subop == "publishSelected") {
326                         header("Content-Type: text/plain");
327
328                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
329                         $cmode = sprintf("%d", $_REQUEST["cmode"]);
330
331                         publishArticlesById($link, $ids, $cmode);
332
333                         print json_encode(array("message" => "UPDATE_COUNTERS"));
334                         return;
335                 }
336
337                 if ($subop == "sanityCheck") {
338                         print "<rpc-reply>";
339                         if (sanity_check($link)) {
340                                 print "<error error-code=\"0\"/>";
341
342                                 print "<init-params><![CDATA[";
343                                 print json_encode(make_init_params($link));
344                                 print "]]></init-params>";
345
346                                 print_runtime_info($link);
347
348                                 # assign client-passed params to session
349                                 $_SESSION["client.userAgent"] = $_REQUEST["ua"];
350
351                         }
352                         print "</rpc-reply>";
353
354                         return;
355                 }               
356
357                 if ($subop == "globalPurge") {
358
359                         print "<rpc-reply>";
360                         global_purge_old_posts($link, true);
361                         print "</rpc-reply>";
362
363                         return;
364                 }
365
366                 if ($subop == "setArticleTags") {
367
368                         global $memcache;
369
370                         $id = db_escape_string($_REQUEST["id"]);
371
372                         $tags_str = db_escape_string($_REQUEST["tags_str"]);
373                         $tags = array_unique(trim_array(split(",", $tags_str)));
374
375                         db_query($link, "BEGIN");
376
377                         $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
378                                 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
379
380                         if (db_num_rows($result) == 1) {
381
382                                 $tags_to_cache = array();
383
384                                 $int_id = db_fetch_result($result, 0, "int_id");
385
386                                 db_query($link, "DELETE FROM ttrss_tags WHERE 
387                                         post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
388
389                                 foreach ($tags as $tag) {
390                                         $tag = sanitize_tag($tag);      
391
392                                         if (!tag_is_valid($tag)) {
393                                                 continue;
394                                         }
395
396                                         if (preg_match("/^[0-9]*$/", $tag)) {
397                                                 continue;
398                                         }
399
400 //                                      print "<!-- $id : $int_id : $tag -->";
401                                         
402                                         if ($tag != '') {
403                                                 db_query($link, "INSERT INTO ttrss_tags 
404                                                         (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
405                                         }
406
407                                         array_push($tags_to_cache, $tag);
408                                 }
409
410                                 /* update tag cache */
411
412                                 $tags_str = join(",", $tags_to_cache);
413
414                                 db_query($link, "UPDATE ttrss_user_entries 
415                                         SET tag_cache = '$tags_str' WHERE ref_id = '$id'
416                                         AND owner_uid = " . $_SESSION["uid"]);
417                         }
418
419                         db_query($link, "COMMIT");
420
421                         if ($memcache) {
422                                 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
423                                 $memcache->delete($obj_id);
424                         }
425
426                         $tags_str = format_tags_string(get_article_tags($link, $id), $id);
427
428                         print "<rpc-reply>
429                                 <tags-str id=\"$id\"><![CDATA[$tags_str]]></tags-str>
430                                 </rpc-reply>";
431
432                         return;
433                 }
434
435                 if ($subop == "regenOPMLKey") {
436
437                         print "<rpc-reply>";
438
439                         update_feed_access_key($link, 'OPML:Publish', 
440                                 false, $_SESSION["uid"]);
441
442                         $new_link = opml_publish_url($link);            
443                         print "<link><![CDATA[$new_link]]></link>";
444                         print "</rpc-reply>";
445                         return;
446                 }
447
448                 if ($subop == "logout") {
449                         logout_user();
450                         print_error_xml(6);
451                         return;
452                 }
453
454                 if ($subop == "completeTags") {
455                         header("Content-Type: text/plain");
456
457                         $search = db_escape_string($_REQUEST["search"]);
458
459                         $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags 
460                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
461                                 tag_name LIKE '$search%' ORDER BY tag_name
462                                 LIMIT 10");
463
464                         print "<ul>";
465                         while ($line = db_fetch_assoc($result)) {
466                                 print "<li>" . $line["tag_name"] . "</li>";
467                         }
468                         print "</ul>";
469
470                         return;
471                 }
472
473                 if ($subop == "purge") {
474                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
475                         $days = sprintf("%d", $_REQUEST["days"]);
476
477                         foreach ($ids as $id) {
478
479                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
480                                         id = '$id' AND owner_uid = ".$_SESSION["uid"]);
481
482                                 if (db_num_rows($result) == 1) {
483                                         purge_feed($link, $id, $days);
484                                 }
485                         }
486
487                         return;
488                 }
489
490 /*              if ($subop == "setScore") {
491                         $id = db_escape_string($_REQUEST["id"]);
492                         $score = sprintf("%d", $_REQUEST["score"]);
493
494                         $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
495                                 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
496
497                         print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
498
499                         return;
500
501                 } */
502
503                 if ($subop == "getArticles") {
504                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
505
506                         print "<rpc-reply>";
507
508                         foreach ($ids as $id) {
509                                 if ($id) {
510                                         outputArticleXML($link, $id, 0, false);
511                                 }
512                         }
513                         print "</rpc-reply>";
514
515                         return;
516                 }
517
518                 if ($subop == "checkDate") {
519
520                         $date = db_escape_string($_REQUEST["date"]);
521                         $date_parsed = strtotime($date);
522
523                         print "<rpc-reply>";
524
525                         if ($date_parsed) {
526                                 print "<result>1</result>";
527                         } else {
528                                 print "<result>0</result>";
529                         }
530
531                         print "</rpc-reply>";
532
533                         return;
534                 }
535
536                 if ($subop == "assignToLabel" || $subop == "removeFromLabel") {
537                         header("Content-Type: text/plain");
538
539                         $reply = array();
540
541                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
542                         $label_id = db_escape_string($_REQUEST["lid"]);
543
544                         $label = db_escape_string(label_find_caption($link, $label_id, 
545                                 $_SESSION["uid"]));
546
547                         $reply["info-for-headlines"] = array();
548
549                         if ($label) {
550
551                                 foreach ($ids as $id) {
552
553                                         if ($subop == "assignToLabel")
554                                                 label_add_article($link, $id, $label, $_SESSION["uid"]);
555                                         else
556                                                 label_remove_article($link, $id, $label, $_SESSION["uid"]);
557
558                                         $labels = get_article_labels($link, $id, $_SESSION["uid"]);
559                                   
560                                         array_push($reply["info-for-headlines"],
561                                           array("id" => $id, "labels" => format_article_labels($labels, $id)));
562
563                                 }
564                         }
565
566                         $reply["message"] = "UPDATE_COUNTERS";
567
568                         print json_encode($reply);
569
570                         return;
571                 }
572
573                 if ($subop == "updateFeedBrowser") {
574
575                         $search = db_escape_string($_REQUEST["search"]);
576                         $limit = db_escape_string($_REQUEST["limit"]);
577                         $mode = db_escape_string($_REQUEST["mode"]);
578
579                         print "<rpc-reply>";
580                         print "<content>";
581                         print "<![CDATA[";
582                         $ctr = print_feed_browser($link, $search, $limit, $mode);
583                         print "]]>";
584                         print "</content>";
585                         print "<num-results value=\"$ctr\"/>";
586                         print "<mode value=\"$mode\"/>";
587                         print "</rpc-reply>";
588
589                         return;
590                 }
591
592
593                 if ($subop == "massSubscribe") {
594
595                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
596                         $mode = $_REQUEST["mode"];
597
598                         $subscribed = array();
599
600                         foreach ($ids as $id) {
601
602                                 if ($mode == 1) {
603                                         $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
604                                                 WHERE id = '$id'");
605                                 } else if ($mode == 2) {
606                                         $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
607                                                 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
608                                         $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
609                                         $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
610                                 }
611         
612                                 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
613                                 $title = db_escape_string(db_fetch_result($result, 0, "title"));
614         
615                                 $title_orig = db_fetch_result($result, 0, "title");
616         
617                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
618                                                 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
619         
620                                 if (db_num_rows($result) == 0) {                        
621                                         if ($mode == 1) {
622                                                 $result = db_query($link,
623                                                         "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) 
624                                                         VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
625                                         } else if ($mode == 2) {
626                                                 $result = db_query($link,
627                                                         "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url) 
628                                                         VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
629                                         }
630                                         array_push($subscribed, $title_orig);
631                                 }
632                         }
633
634                         $num_feeds = count($subscribed);
635
636                         print "<rpc-reply>";
637                         print "<num-feeds value='$num_feeds'/>";
638                         print "</rpc-reply>";
639
640                         return;
641                 } 
642
643                 if ($subop == "digest-get-contents") {
644                         $article_id = db_escape_string($_REQUEST['article_id']);
645
646                         $result = db_query($link, "SELECT content 
647                                 FROM ttrss_entries, ttrss_user_entries
648                                 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
649
650                         print "<rpc-reply>";
651
652                         print "<article id=\"$article_id\"><![CDATA[";
653
654                         $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
655
656                         print $content;
657
658                         print "]]></article>";
659
660                         print "</rpc-reply>";
661
662                         return;
663                 }
664
665                 if ($subop == "digest-update") {
666                         header("Content-Type: text/plain");
667
668                         $feed_id = db_escape_string($_REQUEST['feed_id']);
669                         $offset = db_escape_string($_REQUEST['offset']);
670                         $seq = db_escape_string($_REQUEST['seq']);
671                 
672                         if (!$feed_id) $feed_id = -4;
673                         if (!$offset) $offset = 0;
674
675                         $reply = array();
676
677                         $reply['seq'] = $seq;
678
679                         $headlines = api_get_headlines($link, $feed_id, 10, $offset,
680                                 '', ($feed_id == -4), true, false, "unread", "updated DESC");
681
682                         //function api_get_headlines($link, $feed_id, $limit, $offset,
683                         //              $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
684
685                         $reply['headlines'] = array();
686                         $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
687                         $reply['headlines']['content'] = $headlines;
688
689                         print json_encode($reply);
690                         return;
691                 }
692
693                 if ($subop == "digest-init") {
694                         header("Content-Type: text/plain");
695
696                         $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
697
698                         $feeds = array();
699
700                         foreach ($tmp_feeds as $f) {
701                                 if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
702                         }
703
704                         print json_encode(array("feeds" => $feeds));
705
706                         return;
707                 }
708
709                 if ($subop == "catchupFeed") {
710
711                         $feed_id = db_escape_string($_REQUEST['feed_id']);
712                         $is_cat = db_escape_string($_REQUEST['is_cat']);
713
714                         print "<rpc-reply>";
715
716                         catchup_feed($link, $feed_id, $is_cat);
717
718                         print "</rpc-reply>";
719
720                         return;
721                 }
722
723                 if ($subop == "sendEmail") {
724                         $secretkey = $_REQUEST['secretkey'];
725
726                         print "<rpc-reply>";
727
728                         if (DIGEST_ENABLE && $_SESSION['email_secretkey'] && 
729                                                 $secretkey == $_SESSION['email_secretkey']) {
730
731                                 $_SESSION['email_secretkey'] = '';
732
733                                 $destination = $_REQUEST['destination'];
734                                 $subject = $_REQUEST['subject'];
735                                 $content = $_REQUEST['content'];
736
737                                 $replyto = strip_tags($_SESSION['email_replyto']);
738                                 $fromname = strip_tags($_SESSION['email_fromname']);
739
740                                 $mail = new PHPMailer();
741
742                                 $mail->PluginDir = "lib/phpmailer/";
743                                 $mail->SetLanguage("en", "lib/phpmailer/language/");
744
745                                 $mail->CharSet = "UTF-8";
746
747                                 $mail->From = $replyto;
748                                 $mail->FromName = $fromname;
749                                 $mail->AddAddress($destination);
750
751                                 if (DIGEST_SMTP_HOST) {
752                                         $mail->Host = DIGEST_SMTP_HOST;
753                                         $mail->Mailer = "smtp";
754                                         $mail->SMTPAuth = DIGEST_SMTP_LOGIN != '';
755                                         $mail->Username = DIGEST_SMTP_LOGIN;
756                                         $mail->Password = DIGEST_SMTP_PASSWORD;
757                                 }
758
759                                 $mail->IsHTML(false);
760                                 $mail->Subject = $subject;
761                                 $mail->Body = $content;
762
763                                 $rc = $mail->Send();
764
765                                 if (!$rc) {
766                                         print "<error><![CDATA[" . $mail->ErrorInfo . "]]></error>";
767                                 } else {
768                                         save_email_address($link, db_escape_string($destination));
769                                         print "<message>UPDATE_COUNTERS</message>";
770                                 }
771
772                         } else {
773                                 print "<error>Not authorized.</error>";
774                         }
775
776                         print "</rpc-reply>";
777
778                         return;
779                 }
780
781                 if ($subop == "completeEmails") {
782
783                         $search = db_escape_string($_REQUEST["search"]);
784
785                         print "<ul>";
786
787                         foreach ($_SESSION['stored_emails'] as $email) {
788                                 if (strpos($email, $search) !== false) {
789                                         print "<li>$email</li>";
790                                 }
791                         }
792
793                         print "</ul>";
794
795                         return;
796                 }
797
798                 if ($subop == "quickAddCat") {
799                         print "<rpc-reply>";    
800
801                         $cat = db_escape_string($_REQUEST["cat"]);
802
803                         add_feed_category($link, $cat);
804
805                         $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
806                                 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
807
808                         if (db_num_rows($result) == 1) {
809                                 $id = db_fetch_result($result, 0, "id");
810                         } else {
811                                 $id = 0;
812                         }
813
814                         print_feed_cat_select($link, "cat_id", $id);
815
816                         print "</rpc-reply>";
817
818                         return;
819                 }
820
821                 if ($subop == "regenFeedKey") {
822                         $feed_id = db_escape_string($_REQUEST['id']);
823                         $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
824
825                         print "<rpc-reply>";
826
827                         $new_key = update_feed_access_key($link, $feed_id, $is_cat);
828
829                         print "<link><![CDATA[$new_key]]></link>";
830
831                         print "</rpc-reply>";
832
833                         return;
834                 }
835
836                 if ($subop == "clearKeys") {
837
838                         db_query($link, "DELETE FROM ttrss_access_keys WHERE
839                                 owner_uid = " . $_SESSION["uid"]);
840
841                         print "<rpc-reply>";
842                         print "<message>UPDATE_COUNTERS</message>";
843                         print "</rpc-reply>";
844
845                         return;
846                 }
847
848                 if ($subop == "verifyRegexp") {
849                         $reg_exp = $_REQUEST["reg_exp"];
850
851                         print "<rpc-reply><status>";
852
853                         if (@preg_match("/$reg_exp/i", "TEST") === false) {
854                                 print "INVALID";
855                         } else {
856                                 print "OK";
857                         }
858
859                         print "</status></rpc-reply>";
860
861                         return;
862                 }
863
864                 if ($subop == "cdmGetArticle") {
865                         header("Content-Type: text/plain");
866
867                         $id = db_escape_string($_REQUEST["id"]);
868
869                         $result = db_query($link, "SELECT content, 
870                                 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds, 
871                                 ttrss_entries
872                                 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND 
873                                 ttrss_entries.id = ref_id AND
874                                 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
875
876                         if (db_num_rows($result) != 0) {
877                                 $line = db_fetch_assoc($result);
878
879                                 $article_content = sanitize_rss($link, $line["content"], 
880                                         false, false, $line['site_url']);
881                         
882                         } else {
883                                 $article_content = '';
884                         }
885
886                         print json_encode(array("article" =>
887                                 array("id" => $id, "content" => $article_content)));
888
889                         return;
890                 }
891
892                 if ($subop == "scheduleFeedUpdate") {
893                         header("Content-Type: text/plain");
894
895                         $feed_id = db_escape_string($_REQUEST["id"]);
896                         $is_cat =  db_escape_string($_REQUEST['is_cat']) == 'true';
897
898                         $message = __("Your request could not be completed.");
899
900                         if ($feed_id >= 0) {
901                                 if (!$is_cat) {
902                                         $message = __("Feed update has been scheduled.");
903
904                                         db_query($link, "UPDATE ttrss_feeds SET
905                                                 last_update_started = '1970-01-01',
906                                                 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
907                                                 owner_uid = ".$_SESSION["uid"]);
908
909                                 } else {
910                                         $message = __("Category update has been scheduled.");
911
912                                         if ($feed_id) 
913                                                 $cat_query = "cat_id = '$feed_id'";
914                                         else
915                                                 $cat_query = "cat_id IS NULL";
916
917                                         db_query($link, "UPDATE ttrss_feeds SET
918                                                 last_update_started = '1970-01-01',
919                                                 last_updated = '1970-01-01' WHERE $cat_query AND
920                                                 owner_uid = ".$_SESSION["uid"]);
921                                 }
922                         } else {
923                                 $message = __("Can't update this kind of feed.");
924                         }
925
926                         print json_encode(array("message" => $message));
927                         return;
928                 }
929
930                 if ($subop == "getTweetInfo") {
931                         header("Content-Type: text/plain");
932                         $id = db_escape_string($_REQUEST['id']);
933
934                         $result = db_query($link, "SELECT title, link 
935                                 FROM ttrss_entries, ttrss_user_entries
936                                 WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
937
938                         if (db_num_rows($result) != 0) {
939                                 $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
940                                         100, '...');
941                                 $article_link = db_fetch_result($result, 0, 'link');
942                         }
943
944                         print json_encode(array("title" => $title, "link" => $article_link,
945                                 "id" => $id));
946
947                         return;
948                 }
949
950                 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
951         }
952 ?>