]> git.wh0rd.org Git - tt-rss.git/blob - classes/rpc.php
sharepopup: implement assigning labels while sharing
[tt-rss.git] / classes / rpc.php
1 <?php
2 class RPC extends Handler_Protected {
3
4         function csrf_ignore($method) {
5                 $csrf_ignored = array("sanitycheck", "buttonplugin", "exportget", "completelabels");
6
7                 return array_search($method, $csrf_ignored) !== false;
8         }
9
10         function setprofile() {
11                 $id = db_escape_string($_REQUEST["id"]);
12
13                 $_SESSION["profile"] = $id;
14                 $_SESSION["prefs_cache"] = array();
15         }
16
17         function exportget() {
18                 $exportname = CACHE_DIR . "/export/" .
19                         sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
20
21                 if (file_exists($exportname)) {
22                         header("Content-type: text/xml");
23
24                         if (function_exists('gzencode')) {
25                                 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz");
26                                 echo gzencode(file_get_contents($exportname));
27                         } else {
28                                 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml");
29                                 echo file_get_contents($exportname);
30                         }
31                 } else {
32                         echo "File not found.";
33                 }
34         }
35
36         function exportrun() {
37                 $offset = (int) db_escape_string($_REQUEST['offset']);
38                 $exported = 0;
39                 $limit = 250;
40
41                 if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) {
42                         $result = db_query($this->link, "SELECT
43                                         ttrss_entries.guid,
44                                         ttrss_entries.title,
45                                         content,
46                                         marked,
47                                         published,
48                                         score,
49                                         note,
50                                         link,
51                                         tag_cache,
52                                         label_cache,
53                                         ttrss_feeds.title AS feed_title,
54                                         ttrss_feeds.feed_url AS feed_url,
55                                         ttrss_entries.updated
56                                 FROM
57                                         ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
58                                         ttrss_entries
59                                 WHERE
60                                         (marked = true OR feed_id IS NULL) AND
61                                         ref_id = ttrss_entries.id AND
62                                         ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . "
63                                 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
64
65                         $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
66
67                         if ($offset == 0) {
68                                 $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "w");
69                                 fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION."\">");
70                         } else {
71                                 $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "a");
72                         }
73
74                         if ($fp) {
75
76                                 while ($line = db_fetch_assoc($result)) {
77                                         fputs($fp, "<article>");
78
79                                         foreach ($line as $k => $v) {
80                                                 fputs($fp, "<$k><![CDATA[$v]]></$k>");
81                                         }
82
83                                         fputs($fp, "</article>");
84                                 }
85
86                                 $exported = db_num_rows($result);
87
88                                 if ($exported < $limit && $exported > 0) {
89                                         fputs($fp, "</articles>");
90                                 }
91
92                                 fclose($fp);
93                         }
94
95                 }
96
97                 print json_encode(array("exported" => $exported));
98         }
99
100         function remprofiles() {
101                 $ids = explode(",", db_escape_string(trim($_REQUEST["ids"])));
102
103                 foreach ($ids as $id) {
104                         if ($_SESSION["profile"] != $id) {
105                                 db_query($this->link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
106                                                         owner_uid = " . $_SESSION["uid"]);
107                         }
108                 }
109         }
110
111         // Silent
112         function addprofile() {
113                 $title = db_escape_string(trim($_REQUEST["title"]));
114                 if ($title) {
115                         db_query($this->link, "BEGIN");
116
117                         $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles
118                                 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
119
120                         if (db_num_rows($result) == 0) {
121
122                                 db_query($this->link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
123                                                         VALUES ('$title', ".$_SESSION["uid"] .")");
124
125                                 $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles WHERE
126                                         title = '$title'");
127
128                                 if (db_num_rows($result) != 0) {
129                                         $profile_id = db_fetch_result($result, 0, "id");
130
131                                         if ($profile_id) {
132                                                 initialize_user_prefs($this->link, $_SESSION["uid"], $profile_id);
133                                         }
134                                 }
135                         }
136
137                         db_query($this->link, "COMMIT");
138                 }
139         }
140
141         // Silent
142         function saveprofile() {
143                 $id = db_escape_string($_REQUEST["id"]);
144                 $title = db_escape_string(trim($_REQUEST["value"]));
145
146                 if ($id == 0) {
147                         print __("Default profile");
148                         return;
149                 }
150
151                 if ($title) {
152                         db_query($this->link, "BEGIN");
153
154                         $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles
155                                 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
156
157                         if (db_num_rows($result) == 0) {
158                                 db_query($this->link, "UPDATE ttrss_settings_profiles
159                                                         SET title = '$title' WHERE id = '$id' AND
160                                                         owner_uid = " . $_SESSION["uid"]);
161                                 print $title;
162                         } else {
163                                 $result = db_query($this->link, "SELECT title FROM ttrss_settings_profiles
164                                                         WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
165                                 print db_fetch_result($result, 0, "title");
166                         }
167
168                         db_query($this->link, "COMMIT");
169                 }
170         }
171
172         // Silent
173         function remarchive() {
174                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
175
176                 foreach ($ids as $id) {
177                         $result = db_query($this->link, "DELETE FROM ttrss_archived_feeds WHERE
178                 (SELECT COUNT(*) FROM ttrss_user_entries
179                                                         WHERE orig_feed_id = '$id') = 0 AND
180                 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
181
182                         $rc = db_affected_rows($this->link, $result);
183                 }
184         }
185
186         function addfeed() {
187                 $feed = db_escape_string($_REQUEST['feed']);
188                 $cat = db_escape_string($_REQUEST['cat']);
189                 $login = db_escape_string($_REQUEST['login']);
190                 $pass = db_escape_string($_REQUEST['pass']);
191                 $need_auth = db_escape_string($_REQUEST['need_auth']) != "";
192
193                 $rc = subscribe_to_feed($this->link, $feed, $cat, $login, $pass, $need_auth);
194
195                 print json_encode(array("result" => $rc));
196         }
197
198         function togglepref() {
199                 $key = db_escape_string($_REQUEST["key"]);
200                 set_pref($this->link, $key, !get_pref($this->link, $key));
201                 $value = get_pref($this->link, $key);
202
203                 print json_encode(array("param" =>$key, "value" => $value));
204         }
205
206         function setpref() {
207                 // set_pref escapes input, so no need to double escape it here
208                 $key = $_REQUEST['key'];
209                 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
210
211                 set_pref($this->link, $key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET');
212
213                 print json_encode(array("param" =>$key, "value" => $value));
214         }
215
216         function mark() {
217                 $mark = $_REQUEST["mark"];
218                 $id = db_escape_string($_REQUEST["id"]);
219
220                 if ($mark == "1") {
221                         $mark = "true";
222                 } else {
223                         $mark = "false";
224                 }
225
226                 $result = db_query($this->link, "UPDATE ttrss_user_entries SET marked = $mark
227                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
228
229                 print json_encode(array("message" => "UPDATE_COUNTERS"));
230         }
231
232         function delete() {
233                 $ids = db_escape_string($_REQUEST["ids"]);
234
235                 $result = db_query($this->link, "DELETE FROM ttrss_user_entries
236                 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
237
238                 print json_encode(array("message" => "UPDATE_COUNTERS"));
239         }
240
241         function unarchive() {
242                 $ids = db_escape_string($_REQUEST["ids"]);
243
244                 $result = db_query($this->link, "UPDATE ttrss_user_entries
245                                         SET feed_id = orig_feed_id, orig_feed_id = NULL
246                                         WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
247
248                 print json_encode(array("message" => "UPDATE_COUNTERS"));
249         }
250
251         function archive() {
252                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
253
254                 foreach ($ids as $id) {
255                         archive_article($this->link, $id, $_SESSION["uid"]);
256                 }
257
258                 print json_encode(array("message" => "UPDATE_COUNTERS"));
259         }
260
261         function publ() {
262                 $pub = $_REQUEST["pub"];
263                 $id = db_escape_string($_REQUEST["id"]);
264                 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
265
266                 if ($pub == "1") {
267                         $pub = "true";
268                 } else {
269                         $pub = "false";
270                 }
271
272                 $result = db_query($this->link, "UPDATE ttrss_user_entries SET
273                         published = $pub, last_read = NOW()
274                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
275
276                 $pubsub_result = false;
277
278                 if (PUBSUBHUBBUB_HUB) {
279                         $rss_link = get_self_url_prefix() .
280                                 "/public.php?op=rss&id=-2&key=" .
281                                 get_feed_access_key($this->link, -2, false);
282
283                         $p = new Publisher(PUBSUBHUBBUB_HUB);
284
285                         $pubsub_result = $p->publish_update($rss_link);
286                 }
287
288                 print json_encode(array("message" => "UPDATE_COUNTERS",
289                         "pubsub_result" => $pubsub_result));
290         }
291
292         function getAllCounters() {
293                 $last_article_id = (int) $_REQUEST["last_article_id"];
294
295                 $reply = array();
296
297                  if ($seq) $reply['seq'] = $seq;
298
299                  if ($last_article_id != getLastArticleId($this->link)) {
300                                 $omode = $_REQUEST["omode"];
301
302                         if ($omode != "T")
303                                 $reply['counters'] = getAllCounters($this->link, $omode);
304                         else
305                                 $reply['counters'] = getGlobalCounters($this->link);
306                  }
307
308                  $reply['runtime-info'] = make_runtime_info($this->link);
309
310                  print json_encode($reply);
311         }
312
313         /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
314         function catchupSelected() {
315                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
316                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
317
318                 catchupArticlesById($this->link, $ids, $cmode);
319
320                 print json_encode(array("message" => "UPDATE_COUNTERS"));
321         }
322
323         function markSelected() {
324                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
325                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
326
327                 markArticlesById($this->link, $ids, $cmode);
328
329                 print json_encode(array("message" => "UPDATE_COUNTERS"));
330         }
331
332         function publishSelected() {
333                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
334                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
335
336                 publishArticlesById($this->link, $ids, $cmode);
337
338                 print json_encode(array("message" => "UPDATE_COUNTERS"));
339         }
340
341         function sanityCheck() {
342                 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
343
344                 $reply = array();
345
346                 $reply['error'] = sanity_check($this->link);
347
348                 if ($reply['error']['code'] == 0) {
349                         $reply['init-params'] = make_init_params($this->link);
350                         $reply['runtime-info'] = make_runtime_info($this->link);
351                 }
352
353                 print json_encode($reply);
354         }
355
356         function setArticleTags() {
357
358                 $id = db_escape_string($_REQUEST["id"]);
359
360                 $tags_str = db_escape_string($_REQUEST["tags_str"]);
361                 $tags = array_unique(trim_array(explode(",", $tags_str)));
362
363                 db_query($this->link, "BEGIN");
364
365                 $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries WHERE
366                                 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
367
368                 if (db_num_rows($result) == 1) {
369
370                         $tags_to_cache = array();
371
372                         $int_id = db_fetch_result($result, 0, "int_id");
373
374                         db_query($this->link, "DELETE FROM ttrss_tags WHERE
375                                 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
376
377                         foreach ($tags as $tag) {
378                                 $tag = sanitize_tag($tag);
379
380                                 if (!tag_is_valid($tag)) {
381                                         continue;
382                                 }
383
384                                 if (preg_match("/^[0-9]*$/", $tag)) {
385                                         continue;
386                                 }
387
388                                 //                                      print "<!-- $id : $int_id : $tag -->";
389
390                                 if ($tag != '') {
391                                         db_query($this->link, "INSERT INTO ttrss_tags
392                                                                 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
393                                 }
394
395                                 array_push($tags_to_cache, $tag);
396                         }
397
398                         /* update tag cache */
399
400                         sort($tags_to_cache);
401                         $tags_str = join(",", $tags_to_cache);
402
403                         db_query($this->link, "UPDATE ttrss_user_entries
404                                 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
405                                                 AND owner_uid = " . $_SESSION["uid"]);
406                 }
407
408                 db_query($this->link, "COMMIT");
409
410                 $tags = get_article_tags($this->link, $id);
411                 $tags_str = format_tags_string($tags, $id);
412                 $tags_str_full = join(", ", $tags);
413
414                 if (!$tags_str_full) $tags_str_full = __("no tags");
415
416                 print json_encode(array("tags_str" => array("id" => $id,
417                                 "content" => $tags_str, "content_full" => $tags_str_full)));
418         }
419
420         function regenOPMLKey() {
421                 update_feed_access_key($this->link, 'OPML:Publish',
422                 false, $_SESSION["uid"]);
423
424                 $new_link = opml_publish_url($this->link);
425
426                 print json_encode(array("link" => $new_link));
427         }
428
429         function completeLabels() {
430                 $search = db_escape_string($_REQUEST["search"]);
431
432                 $result = db_query($this->link, "SELECT DISTINCT caption FROM
433                                 ttrss_labels2
434                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
435                                 LOWER(caption) LIKE LOWER('$search%') ORDER BY caption
436                                 LIMIT 5");
437
438                 print "<ul>";
439                 while ($line = db_fetch_assoc($result)) {
440                         print "<li>" . $line["caption"] . "</li>";
441                 }
442                 print "</ul>";
443         }
444
445
446         function completeTags() {
447                 $search = db_escape_string($_REQUEST["search"]);
448
449                 $result = db_query($this->link, "SELECT DISTINCT tag_name FROM ttrss_tags
450                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
451                                 tag_name LIKE '$search%' ORDER BY tag_name
452                                 LIMIT 10");
453
454                 print "<ul>";
455                 while ($line = db_fetch_assoc($result)) {
456                         print "<li>" . $line["tag_name"] . "</li>";
457                 }
458                 print "</ul>";
459         }
460
461         function purge() {
462                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
463                 $days = sprintf("%d", $_REQUEST["days"]);
464
465                 foreach ($ids as $id) {
466
467                         $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
468                                 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
469
470                         if (db_num_rows($result) == 1) {
471                                 purge_feed($this->link, $id, $days);
472                         }
473                 }
474         }
475
476         function getArticles() {
477                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
478                 $articles = array();
479
480                 foreach ($ids as $id) {
481                         if ($id) {
482                                 array_push($articles, format_article($this->link, $id, 0, false));
483                         }
484                 }
485
486                 print json_encode($articles);
487         }
488
489         function checkDate() {
490                 $date = db_escape_string($_REQUEST["date"]);
491                 $date_parsed = strtotime($date);
492
493                 print json_encode(array("result" => (bool)$date_parsed,
494                         "date" => date("c", $date_parsed)));
495         }
496
497         function assigntolabel() {
498                 return $this->labelops(true);
499         }
500
501         function removefromlabel() {
502                 return $this->labelops(false);
503         }
504
505         function labelops($assign) {
506                 $reply = array();
507
508                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
509                 $label_id = db_escape_string($_REQUEST["lid"]);
510
511                 $label = db_escape_string(label_find_caption($this->link, $label_id,
512                 $_SESSION["uid"]));
513
514                 $reply["info-for-headlines"] = array();
515
516                 if ($label) {
517
518                         foreach ($ids as $id) {
519
520                                 if ($assign)
521                                         label_add_article($this->link, $id, $label, $_SESSION["uid"]);
522                                 else
523                                         label_remove_article($this->link, $id, $label, $_SESSION["uid"]);
524
525                                 $labels = get_article_labels($this->link, $id, $_SESSION["uid"]);
526
527                                 array_push($reply["info-for-headlines"],
528                                 array("id" => $id, "labels" => format_article_labels($labels, $id)));
529
530                         }
531                 }
532
533                 $reply["message"] = "UPDATE_COUNTERS";
534
535                 print json_encode($reply);
536         }
537
538         function updateFeedBrowser() {
539                 $search = db_escape_string($_REQUEST["search"]);
540                 $limit = db_escape_string($_REQUEST["limit"]);
541                 $mode = (int) db_escape_string($_REQUEST["mode"]);
542
543                 print json_encode(array("content" =>
544                         make_feed_browser($this->link, $search, $limit, $mode),
545                                 "mode" => $mode));
546         }
547
548         // Silent
549         function massSubscribe() {
550
551                 $payload = json_decode($_REQUEST["payload"], false);
552                 $mode = $_REQUEST["mode"];
553
554                 if (!$payload || !is_array($payload)) return;
555
556                 if ($mode == 1) {
557                         foreach ($payload as $feed) {
558
559                                 $title = db_escape_string($feed[0]);
560                                 $feed_url = db_escape_string($feed[1]);
561
562                                 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
563                                         feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
564
565                                 if (db_num_rows($result) == 0) {
566                                         $result = db_query($this->link, "INSERT INTO ttrss_feeds
567                                                                         (owner_uid,feed_url,title,cat_id,site_url)
568                                                                         VALUES ('".$_SESSION["uid"]."',
569                                                                         '$feed_url', '$title', NULL, '')");
570                                 }
571                         }
572                 } else if ($mode == 2) {
573                         // feed archive
574                         foreach ($payload as $id) {
575                                 $result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds
576                                         WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
577
578                                 if (db_num_rows($result) != 0) {
579                                         $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
580                                         $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
581                                         $title = db_escape_string(db_fetch_result($result, 0, "title"));
582
583                                         $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
584                                                 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
585
586                                         if (db_num_rows($result) == 0) {
587                                                 $result = db_query($this->link, "INSERT INTO ttrss_feeds
588                                                                                 (owner_uid,feed_url,title,cat_id,site_url)
589                                                                         VALUES ('$id','".$_SESSION["uid"]."',
590                                                                         '$feed_url', '$title', NULL, '$site_url')");
591                                         }
592                                 }
593                         }
594                 }
595         }
596
597         function digestgetcontents() {
598                 $article_id = db_escape_string($_REQUEST['article_id']);
599
600                 $result = db_query($this->link, "SELECT content,title,link,marked,published
601                         FROM ttrss_entries, ttrss_user_entries
602                         WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
603
604                 $content = sanitize($this->link, db_fetch_result($result, 0, "content"));
605                 $title = strip_tags(db_fetch_result($result, 0, "title"));
606                 $article_url = htmlspecialchars(db_fetch_result($result, 0, "link"));
607                 $marked = sql_bool_to_bool(db_fetch_result($result, 0, "marked"));
608                 $published = sql_bool_to_bool(db_fetch_result($result, 0, "published"));
609
610                 print json_encode(array("article" =>
611                         array("id" => $article_id, "url" => $article_url,
612                                 "tags" => get_article_tags($this->link, $article_id),
613                                 "marked" => $marked, "published" => $published,
614                                 "title" => $title, "content" => $content)));
615         }
616
617         function digestupdate() {
618                 $feed_id = db_escape_string($_REQUEST['feed_id']);
619                 $offset = db_escape_string($_REQUEST['offset']);
620                 $seq = db_escape_string($_REQUEST['seq']);
621
622                 if (!$feed_id) $feed_id = -4;
623                 if (!$offset) $offset = 0;
624
625                 $reply = array();
626
627                 $reply['seq'] = $seq;
628
629                 $headlines = api_get_headlines($this->link, $feed_id, 30, $offset,
630                                 '', ($feed_id == -4), true, false, "unread", "updated DESC", 0, 0);
631
632                 $reply['headlines'] = array();
633                 $reply['headlines']['title'] = getFeedTitle($this->link, $feed_id);
634                 $reply['headlines']['content'] = $headlines;
635
636                 print json_encode($reply);
637         }
638
639         function digestinit() {
640                 $tmp_feeds = api_get_feeds($this->link, -4, true, false, 0);
641
642                 $feeds = array();
643
644                 foreach ($tmp_feeds as $f) {
645                         if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
646                 }
647
648                 print json_encode(array("feeds" => $feeds));
649         }
650
651         function catchupFeed() {
652                 $feed_id = db_escape_string($_REQUEST['feed_id']);
653                 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
654                 $max_id = (int) db_escape_string($_REQUEST['max_id']);
655
656                 catchup_feed($this->link, $feed_id, $is_cat, false, $max_id);
657
658                 print json_encode(array("message" => "UPDATE_COUNTERS"));
659         }
660
661         function quickAddCat() {
662                 $cat = db_escape_string($_REQUEST["cat"]);
663
664                 add_feed_category($this->link, $cat);
665
666                 $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE
667                         title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
668
669                 if (db_num_rows($result) == 1) {
670                         $id = db_fetch_result($result, 0, "id");
671                 } else {
672                         $id = 0;
673                 }
674
675                 print_feed_cat_select($this->link, "cat_id", $id);
676         }
677
678         function regenFeedKey() {
679                 $feed_id = db_escape_string($_REQUEST['id']);
680                 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
681
682                 $new_key = update_feed_access_key($this->link, $feed_id, $is_cat);
683
684                 print json_encode(array("link" => $new_key));
685         }
686
687         // Silent
688         function clearKeys() {
689                 db_query($this->link, "DELETE FROM ttrss_access_keys WHERE
690                         owner_uid = " . $_SESSION["uid"]);
691         }
692
693         // Silent
694         function clearArticleKeys() {
695                 db_query($this->link, "UPDATE ttrss_user_entries SET uuid = '' WHERE
696                         owner_uid = " . $_SESSION["uid"]);
697
698                 return;
699         }
700
701         function verifyRegexp() {
702                 $reg_exp = $_REQUEST["reg_exp"];
703
704                 $status = @preg_match("/$reg_exp/i", "TEST") !== false;
705
706                 print json_encode(array("status" => $status));
707         }
708
709         function buttonPlugin() {
710                 $pclass = "button_" . basename($_REQUEST['plugin']);
711                 $method = $_REQUEST['plugin_method'];
712
713                 if (class_exists($pclass)) {
714                         $plugin = new $pclass($this->link);
715                         if (method_exists($plugin, $method)) {
716                                 return $plugin->$method();
717                         }
718                 }
719         }
720
721         function genHash() {
722                 $hash = sha1(uniqid(rand(), true));
723
724                 print json_encode(array("hash" => $hash));
725         }
726
727         function batchAddFeeds() {
728                 $cat_id = db_escape_string($_REQUEST['cat']);
729                 $feeds = explode("\n", db_escape_string($_REQUEST['feeds']));
730                 $login = db_escape_string($_REQUEST['login']);
731                 $pass = db_escape_string($_REQUEST['pass']);
732                 $need_auth = db_escape_string($_REQUEST['need_auth']) != "";
733
734                 foreach ($feeds as $feed) {
735                         $feed = trim($feed);
736
737                         if (validate_feed_url($feed)) {
738
739                                 db_query($this->link, "BEGIN");
740
741                                 $update_method = 0;
742
743                                 if ($cat_id == "0" || !$cat_id) {
744                                         $cat_qpart = "NULL";
745                                 } else {
746                                         $cat_qpart = "'$cat_id'";
747                                 }
748
749                                 $result = db_query($this->link,
750                                         "SELECT id FROM ttrss_feeds
751                                         WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]);
752
753                                 if (db_num_rows($result) == 0) {
754                                         $result = db_query($this->link,
755                                                 "INSERT INTO ttrss_feeds
756                                                         (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method)
757                                                 VALUES ('".$_SESSION["uid"]."', '$feed',
758                                                         '[Unknown]', $cat_qpart, '$login', '$pass', '$update_method')");
759                                 }
760
761                                 db_query($this->link, "COMMIT");
762                         }
763                 }
764         }
765
766 }
767 ?>