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