]> git.wh0rd.org Git - tt-rss.git/blob - modules/backend-rpc.php
implement pubsubhubbub subscriber (update schema)
[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 = split(",", 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 = split(",", 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 = split(",", 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 = split(",", 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 = split(",", 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 = split(",", 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(split(",", $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                                 $tags_str = join(",", $tags_to_cache);
363
364                                 db_query($link, "UPDATE ttrss_user_entries
365                                         SET tag_cache = '$tags_str' WHERE ref_id = '$id'
366                                         AND owner_uid = " . $_SESSION["uid"]);
367                         }
368
369                         db_query($link, "COMMIT");
370
371                         if ($memcache) {
372                                 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
373                                 $memcache->delete($obj_id);
374                         }
375
376                         $tags_str = format_tags_string(get_article_tags($link, $id), $id);
377
378                         print json_encode(array("tags_str" => array("id" => $id,
379                                 "content" => $tags_str)));
380
381                         return;
382                 }
383
384                 if ($subop == "regenOPMLKey") {
385                         update_feed_access_key($link, 'OPML:Publish',
386                                 false, $_SESSION["uid"]);
387
388                         $new_link = opml_publish_url($link);
389
390                         print json_encode(array("link" => $new_link));
391                         return;
392                 }
393
394                 if ($subop == "completeTags") {
395                         $search = db_escape_string($_REQUEST["search"]);
396
397                         $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
398                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
399                                 tag_name LIKE '$search%' ORDER BY tag_name
400                                 LIMIT 10");
401
402                         print "<ul>";
403                         while ($line = db_fetch_assoc($result)) {
404                                 print "<li>" . $line["tag_name"] . "</li>";
405                         }
406                         print "</ul>";
407
408                         return;
409                 }
410
411                 if ($subop == "purge") {
412                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
413                         $days = sprintf("%d", $_REQUEST["days"]);
414
415                         foreach ($ids as $id) {
416
417                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
418                                         id = '$id' AND owner_uid = ".$_SESSION["uid"]);
419
420                                 if (db_num_rows($result) == 1) {
421                                         purge_feed($link, $id, $days);
422                                 }
423                         }
424
425                         return;
426                 }
427
428 /*              if ($subop == "setScore") {
429                         $id = db_escape_string($_REQUEST["id"]);
430                         $score = sprintf("%d", $_REQUEST["score"]);
431
432                         $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
433                                 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
434
435                         print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
436
437                         return;
438
439                 } */
440
441                 if ($subop == "getArticles") {
442                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
443                         $articles = array();
444
445                         foreach ($ids as $id) {
446                                 if ($id) {
447                                         array_push($articles, format_article($link, $id, 0, false));
448                                 }
449                         }
450
451                         print json_encode($articles);
452                         return;
453                 }
454
455                 if ($subop == "checkDate") {
456                         $date = db_escape_string($_REQUEST["date"]);
457                         $date_parsed = strtotime($date);
458
459                         print json_encode(array("result" => (bool)$date_parsed));
460                         return;
461                 }
462
463                 if ($subop == "assignToLabel" || $subop == "removeFromLabel") {
464                         $reply = array();
465
466                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
467                         $label_id = db_escape_string($_REQUEST["lid"]);
468
469                         $label = db_escape_string(label_find_caption($link, $label_id,
470                                 $_SESSION["uid"]));
471
472                         $reply["info-for-headlines"] = array();
473
474                         if ($label) {
475
476                                 foreach ($ids as $id) {
477
478                                         if ($subop == "assignToLabel")
479                                                 label_add_article($link, $id, $label, $_SESSION["uid"]);
480                                         else
481                                                 label_remove_article($link, $id, $label, $_SESSION["uid"]);
482
483                                         $labels = get_article_labels($link, $id, $_SESSION["uid"]);
484
485                                         array_push($reply["info-for-headlines"],
486                                           array("id" => $id, "labels" => format_article_labels($labels, $id)));
487
488                                 }
489                         }
490
491                         $reply["message"] = "UPDATE_COUNTERS";
492
493                         print json_encode($reply);
494
495                         return;
496                 }
497
498                 if ($subop == "updateFeedBrowser") {
499                         $search = db_escape_string($_REQUEST["search"]);
500                         $limit = db_escape_string($_REQUEST["limit"]);
501                         $mode = (int) db_escape_string($_REQUEST["mode"]);
502
503                         print json_encode(array("content" =>
504                                 make_feed_browser($link, $search, $limit, $mode),
505                                 "mode" => $mode));
506                         return;
507                 }
508
509                 // Silent
510                 if ($subop == "massSubscribe") {
511
512                         $ids = split(",", db_escape_string($_REQUEST["ids"]));
513                         $mode = $_REQUEST["mode"];
514
515                         $subscribed = array();
516
517                         foreach ($ids as $id) {
518
519                                 if ($mode == 1) {
520                                         $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
521                                                 WHERE id = '$id'");
522                                 } else if ($mode == 2) {
523                                         $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
524                                                 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
525                                         $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
526                                         $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
527                                 }
528
529                                 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
530                                 $title = db_escape_string(db_fetch_result($result, 0, "title"));
531
532                                 $title_orig = db_fetch_result($result, 0, "title");
533
534                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
535                                                 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
536
537                                 if (db_num_rows($result) == 0) {
538                                         if ($mode == 1) {
539                                                 $result = db_query($link,
540                                                         "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
541                                                         VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
542                                         } else if ($mode == 2) {
543                                                 $result = db_query($link,
544                                                         "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
545                                                         VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
546                                         }
547                                         array_push($subscribed, $title_orig);
548                                 }
549                         }
550
551                         return;
552                 }
553
554                 if ($subop == "digest-get-contents") {
555                         $article_id = db_escape_string($_REQUEST['article_id']);
556
557                         $result = db_query($link, "SELECT content
558                                 FROM ttrss_entries, ttrss_user_entries
559                                 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
560
561                         $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
562
563                         print json_encode(array("article" =>
564                                 array("id" => $id, "content" => $content)));
565                         return;
566                 }
567
568                 if ($subop == "digest-update") {
569                         $feed_id = db_escape_string($_REQUEST['feed_id']);
570                         $offset = db_escape_string($_REQUEST['offset']);
571                         $seq = db_escape_string($_REQUEST['seq']);
572
573                         if (!$feed_id) $feed_id = -4;
574                         if (!$offset) $offset = 0;
575
576                         $reply = array();
577
578                         $reply['seq'] = $seq;
579
580                         $headlines = api_get_headlines($link, $feed_id, 10, $offset,
581                                 '', ($feed_id == -4), true, false, "unread", "updated DESC");
582
583                         //function api_get_headlines($link, $feed_id, $limit, $offset,
584                         //              $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
585
586                         $reply['headlines'] = array();
587                         $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
588                         $reply['headlines']['content'] = $headlines;
589
590                         print json_encode($reply);
591                         return;
592                 }
593
594                 if ($subop == "digest-init") {
595                         $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
596
597                         $feeds = array();
598
599                         foreach ($tmp_feeds as $f) {
600                                 if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
601                         }
602
603                         print json_encode(array("feeds" => $feeds));
604
605                         return;
606                 }
607
608                 if ($subop == "catchupFeed") {
609                         $feed_id = db_escape_string($_REQUEST['feed_id']);
610                         $is_cat = db_escape_string($_REQUEST['is_cat']);
611
612                         catchup_feed($link, $feed_id, $is_cat);
613
614                         return;
615                 }
616
617                 if ($subop == "sendEmail") {
618                         $secretkey = $_REQUEST['secretkey'];
619
620                         $reply = array();
621
622                         if (DIGEST_ENABLE && $_SESSION['email_secretkey'] &&
623                                                 $secretkey == $_SESSION['email_secretkey']) {
624
625                                 $_SESSION['email_secretkey'] = '';
626
627                                 $destination = $_REQUEST['destination'];
628                                 $subject = $_REQUEST['subject'];
629                                 $content = $_REQUEST['content'];
630
631                                 $replyto = strip_tags($_SESSION['email_replyto']);
632                                 $fromname = strip_tags($_SESSION['email_fromname']);
633
634                                 $mail = new PHPMailer();
635
636                                 $mail->PluginDir = "lib/phpmailer/";
637                                 $mail->SetLanguage("en", "lib/phpmailer/language/");
638
639                                 $mail->CharSet = "UTF-8";
640
641                                 $mail->From = $replyto;
642                                 $mail->FromName = $fromname;
643                                 $mail->AddAddress($destination);
644
645                                 if (DIGEST_SMTP_HOST) {
646                                         $mail->Host = DIGEST_SMTP_HOST;
647                                         $mail->Mailer = "smtp";
648                                         $mail->SMTPAuth = DIGEST_SMTP_LOGIN != '';
649                                         $mail->Username = DIGEST_SMTP_LOGIN;
650                                         $mail->Password = DIGEST_SMTP_PASSWORD;
651                                 }
652
653                                 $mail->IsHTML(false);
654                                 $mail->Subject = $subject;
655                                 $mail->Body = $content;
656
657                                 $rc = $mail->Send();
658
659                                 if (!$rc) {
660                                         $reply['error'] =  $mail->ErrorInfo;
661                                 } else {
662                                         save_email_address($link, db_escape_string($destination));
663                                         $reply['message'] = "UPDATE_COUNTERS";
664                                 }
665
666                         } else {
667                                 $reply['error'] = "Not authorized.";
668                         }
669
670                         print json_encode($reply);
671                         return;
672                 }
673
674                 if ($subop == "completeEmails") {
675                         $search = db_escape_string($_REQUEST["search"]);
676
677                         print "<ul>";
678
679                         foreach ($_SESSION['stored_emails'] as $email) {
680                                 if (strpos($email, $search) !== false) {
681                                         print "<li>$email</li>";
682                                 }
683                         }
684
685                         print "</ul>";
686
687                         return;
688                 }
689
690                 if ($subop == "quickAddCat") {
691                         $cat = db_escape_string($_REQUEST["cat"]);
692
693                         add_feed_category($link, $cat);
694
695                         $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
696                                 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
697
698                         if (db_num_rows($result) == 1) {
699                                 $id = db_fetch_result($result, 0, "id");
700                         } else {
701                                 $id = 0;
702                         }
703
704                         print_feed_cat_select($link, "cat_id", $id);
705
706                         return;
707                 }
708
709                 if ($subop == "regenFeedKey") {
710                         $feed_id = db_escape_string($_REQUEST['id']);
711                         $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
712
713                         $new_key = update_feed_access_key($link, $feed_id, $is_cat);
714
715                         print json_encode(array("link" => $new_key));
716                         return;
717                 }
718
719                 // Silent
720                 if ($subop == "clearKeys") {
721                         db_query($link, "DELETE FROM ttrss_access_keys WHERE
722                                 owner_uid = " . $_SESSION["uid"]);
723
724                         return;
725                 }
726
727                 if ($subop == "verifyRegexp") {
728                         $reg_exp = $_REQUEST["reg_exp"];
729
730                         $status = @preg_match("/$reg_exp/i", "TEST") !== false;
731
732                         print json_encode(array("status" => $status));
733                         return;
734                 }
735
736                 // TODO: unify with digest-get-contents?
737                 if ($subop == "cdmGetArticle") {
738                         $id = db_escape_string($_REQUEST["id"]);
739
740                         $result = db_query($link, "SELECT content,
741                                 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
742                                 ttrss_entries
743                                 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
744                                 ttrss_entries.id = ref_id AND
745                                 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
746
747                         if (db_num_rows($result) != 0) {
748                                 $line = db_fetch_assoc($result);
749
750                                 $article_content = sanitize_rss($link, $line["content"],
751                                         false, false, $line['site_url']);
752
753                         } else {
754                                 $article_content = '';
755                         }
756
757                         print json_encode(array("article" =>
758                                 array("id" => $id, "content" => $article_content)));
759
760                         return;
761                 }
762
763                 if ($subop == "scheduleFeedUpdate") {
764                         $feed_id = db_escape_string($_REQUEST["id"]);
765                         $is_cat =  db_escape_string($_REQUEST['is_cat']) == 'true';
766
767                         $message = __("Your request could not be completed.");
768
769                         if ($feed_id >= 0) {
770                                 if (!$is_cat) {
771                                         $message = __("Feed update has been scheduled.");
772
773                                         db_query($link, "UPDATE ttrss_feeds SET
774                                                 last_update_started = '1970-01-01',
775                                                 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
776                                                 owner_uid = ".$_SESSION["uid"]);
777
778                                 } else {
779                                         $message = __("Category update has been scheduled.");
780
781                                         if ($feed_id)
782                                                 $cat_query = "cat_id = '$feed_id'";
783                                         else
784                                                 $cat_query = "cat_id IS NULL";
785
786                                         db_query($link, "UPDATE ttrss_feeds SET
787                                                 last_update_started = '1970-01-01',
788                                                 last_updated = '1970-01-01' WHERE $cat_query AND
789                                                 owner_uid = ".$_SESSION["uid"]);
790                                 }
791                         } else {
792                                 $message = __("Can't update this kind of feed.");
793                         }
794
795                         print json_encode(array("message" => $message));
796                         return;
797                 }
798
799                 if ($subop == "getTweetInfo") {
800                         $id = db_escape_string($_REQUEST['id']);
801
802                         $result = db_query($link, "SELECT title, link
803                                 FROM ttrss_entries, ttrss_user_entries
804                                 WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
805
806                         if (db_num_rows($result) != 0) {
807                                 $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
808                                         100, '...');
809                                 $article_link = db_fetch_result($result, 0, 'link');
810                         }
811
812                         print json_encode(array("title" => $title, "link" => $article_link,
813                                 "id" => $id));
814
815                         return;
816                 }
817
818                 if ($subop == "setNote") {
819                         $id = db_escape_string($_REQUEST["id"]);
820                         $note = strip_tags(db_escape_string($_REQUEST["note"]));
821
822                         db_query($link, "UPDATE ttrss_user_entries SET note = '$note'
823                                 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
824
825                         $formatted_note = format_article_note($id, $note);
826
827                         print json_encode(array("note" => $formatted_note));
828                         return;
829                 }
830
831                 print json_encode(array("error" => array("code" => 7,
832                         "message" => "Unknown method: $subop")));
833         }
834 ?>