]> git.wh0rd.org Git - tt-rss.git/blob - classes/rpc.php
bf693d2a2049bc3d61fe68a1c14401213f88c374
[tt-rss.git] / classes / rpc.php
1 <?php
2 class RPC extends Protected_Handler {
3
4         function csrf_ignore($method) {
5                 $csrf_ignored = array("sanitycheck", "buttonplugin", "exportget");
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 extractfeedurls() {
199                 $urls = get_feeds_from_html($_REQUEST['url']);
200
201                 print json_encode(array("urls" => $urls));
202         }
203
204         function togglepref() {
205                 $key = db_escape_string($_REQUEST["key"]);
206                 set_pref($this->link, $key, !get_pref($this->link, $key));
207                 $value = get_pref($this->link, $key);
208
209                 print json_encode(array("param" =>$key, "value" => $value));
210         }
211
212         function setpref() {
213                 // set_pref escapes input, so no need to double escape it here
214                 $key = $_REQUEST['key'];
215                 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
216
217                 set_pref($this->link, $key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET');
218
219                 print json_encode(array("param" =>$key, "value" => $value));
220         }
221
222         function mark() {
223                 $mark = $_REQUEST["mark"];
224                 $id = db_escape_string($_REQUEST["id"]);
225
226                 if ($mark == "1") {
227                         $mark = "true";
228                 } else {
229                         $mark = "false";
230                 }
231
232                 $result = db_query($this->link, "UPDATE ttrss_user_entries SET marked = $mark
233                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
234
235                 print json_encode(array("message" => "UPDATE_COUNTERS"));
236         }
237
238         function delete() {
239                 $ids = db_escape_string($_REQUEST["ids"]);
240
241                 $result = db_query($this->link, "DELETE FROM ttrss_user_entries
242                 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
243
244                 print json_encode(array("message" => "UPDATE_COUNTERS"));
245         }
246
247         function unarchive() {
248                 $ids = db_escape_string($_REQUEST["ids"]);
249
250                 $result = db_query($this->link, "UPDATE ttrss_user_entries
251                                         SET feed_id = orig_feed_id, orig_feed_id = NULL
252                                         WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
253
254                 print json_encode(array("message" => "UPDATE_COUNTERS"));
255         }
256
257         function archive() {
258                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
259
260                 foreach ($ids as $id) {
261                         archive_article($this->link, $id, $_SESSION["uid"]);
262                 }
263
264                 print json_encode(array("message" => "UPDATE_COUNTERS"));
265         }
266
267         function publ() {
268                 $pub = $_REQUEST["pub"];
269                 $id = db_escape_string($_REQUEST["id"]);
270                 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
271
272                 if ($pub == "1") {
273                         $pub = "true";
274                 } else {
275                         $pub = "false";
276                 }
277
278                 $result = db_query($this->link, "UPDATE ttrss_user_entries SET
279                         published = $pub
280                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
281
282                 $pubsub_result = false;
283
284                 if (PUBSUBHUBBUB_HUB) {
285                         $rss_link = get_self_url_prefix() .
286                                 "/public.php?op=rss&id=-2&key=" .
287                                 get_feed_access_key($this->link, -2, false);
288
289                         $p = new Publisher(PUBSUBHUBBUB_HUB);
290
291                         $pubsub_result = $p->publish_update($rss_link);
292                 }
293
294                 print json_encode(array("message" => "UPDATE_COUNTERS",
295                         "pubsub_result" => $pubsub_result));
296         }
297
298         function getAllCounters() {
299                 $last_article_id = (int) $_REQUEST["last_article_id"];
300
301                 $reply = array();
302
303                  if ($seq) $reply['seq'] = $seq;
304
305                  if ($last_article_id != getLastArticleId($this->link)) {
306                                 $omode = $_REQUEST["omode"];
307
308                         if ($omode != "T")
309                                 $reply['counters'] = getAllCounters($this->link, $omode);
310                         else
311                                 $reply['counters'] = getGlobalCounters($this->link);
312                  }
313
314                  $reply['runtime-info'] = make_runtime_info($this->link);
315
316                  print json_encode($reply);
317         }
318
319         /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
320         function catchupSelected() {
321                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
322                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
323
324                 catchupArticlesById($this->link, $ids, $cmode);
325
326                 print json_encode(array("message" => "UPDATE_COUNTERS"));
327         }
328
329         function markSelected() {
330                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
331                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
332
333                 markArticlesById($this->link, $ids, $cmode);
334
335                 print json_encode(array("message" => "UPDATE_COUNTERS"));
336         }
337
338         function publishSelected() {
339                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
340                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
341
342                 publishArticlesById($this->link, $ids, $cmode);
343
344                 print json_encode(array("message" => "UPDATE_COUNTERS"));
345         }
346
347         function sanityCheck() {
348                 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
349
350                 $reply = array();
351
352                 $reply['error'] = sanity_check($this->link);
353
354                 if ($reply['error']['code'] == 0) {
355                         $reply['init-params'] = make_init_params($this->link);
356                         $reply['runtime-info'] = make_runtime_info($this->link);
357                 }
358
359                 print json_encode($reply);
360         }
361
362         function setArticleTags() {
363
364                 $id = db_escape_string($_REQUEST["id"]);
365
366                 $tags_str = db_escape_string($_REQUEST["tags_str"]);
367                 $tags = array_unique(trim_array(explode(",", $tags_str)));
368
369                 db_query($this->link, "BEGIN");
370
371                 $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries WHERE
372                                 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
373
374                 if (db_num_rows($result) == 1) {
375
376                         $tags_to_cache = array();
377
378                         $int_id = db_fetch_result($result, 0, "int_id");
379
380                         db_query($this->link, "DELETE FROM ttrss_tags WHERE
381                                 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
382
383                         foreach ($tags as $tag) {
384                                 $tag = sanitize_tag($tag);
385
386                                 if (!tag_is_valid($tag)) {
387                                         continue;
388                                 }
389
390                                 if (preg_match("/^[0-9]*$/", $tag)) {
391                                         continue;
392                                 }
393
394                                 //                                      print "<!-- $id : $int_id : $tag -->";
395
396                                 if ($tag != '') {
397                                         db_query($this->link, "INSERT INTO ttrss_tags
398                                                                 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
399                                 }
400
401                                 array_push($tags_to_cache, $tag);
402                         }
403
404                         /* update tag cache */
405
406                         sort($tags_to_cache);
407                         $tags_str = join(",", $tags_to_cache);
408
409                         db_query($this->link, "UPDATE ttrss_user_entries
410                                 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
411                                                 AND owner_uid = " . $_SESSION["uid"]);
412                 }
413
414                 db_query($this->link, "COMMIT");
415
416                 $tags = get_article_tags($this->link, $id);
417                 $tags_str = format_tags_string($tags, $id);
418                 $tags_str_full = join(", ", $tags);
419
420                 if (!$tags_str_full) $tags_str_full = __("no tags");
421
422                 print json_encode(array("tags_str" => array("id" => $id,
423                                 "content" => $tags_str, "content_full" => $tags_str_full)));
424         }
425
426         function regenOPMLKey() {
427                 update_feed_access_key($this->link, 'OPML:Publish',
428                 false, $_SESSION["uid"]);
429
430                 $new_link = opml_publish_url($this->link);
431
432                 print json_encode(array("link" => $new_link));
433         }
434
435         function completeTags() {
436                 $search = db_escape_string($_REQUEST["search"]);
437
438                 $result = db_query($this->link, "SELECT DISTINCT tag_name FROM ttrss_tags
439                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
440                                 tag_name LIKE '$search%' ORDER BY tag_name
441                                 LIMIT 10");
442
443                 print "<ul>";
444                 while ($line = db_fetch_assoc($result)) {
445                         print "<li>" . $line["tag_name"] . "</li>";
446                 }
447                 print "</ul>";
448         }
449
450         function purge() {
451                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
452                 $days = sprintf("%d", $_REQUEST["days"]);
453
454                 foreach ($ids as $id) {
455
456                         $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
457                                 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
458
459                         if (db_num_rows($result) == 1) {
460                                 purge_feed($this->link, $id, $days);
461                         }
462                 }
463         }
464
465         function getArticles() {
466                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
467                 $articles = array();
468
469                 foreach ($ids as $id) {
470                         if ($id) {
471                                 array_push($articles, format_article($this->link, $id, 0, false));
472                         }
473                 }
474
475                 print json_encode($articles);
476         }
477
478         function checkDate() {
479                 $date = db_escape_string($_REQUEST["date"]);
480                 $date_parsed = strtotime($date);
481
482                 print json_encode(array("result" => (bool)$date_parsed,
483                         "date" => date("c", $date_parsed)));
484         }
485
486         function assigntolabel() {
487                 return $this->labelops(true);
488         }
489
490         function removefromlabel() {
491                 return $this->labelops(false);
492         }
493
494         function labelops($assign) {
495                 $reply = array();
496
497                 $ids = explode(",", db_escape_string($_REQUEST["ids"]));
498                 $label_id = db_escape_string($_REQUEST["lid"]);
499
500                 $label = db_escape_string(label_find_caption($this->link, $label_id,
501                 $_SESSION["uid"]));
502
503                 $reply["info-for-headlines"] = array();
504
505                 if ($label) {
506
507                         foreach ($ids as $id) {
508
509                                 if ($assign)
510                                         label_add_article($this->link, $id, $label, $_SESSION["uid"]);
511                                 else
512                                         label_remove_article($this->link, $id, $label, $_SESSION["uid"]);
513
514                                 $labels = get_article_labels($this->link, $id, $_SESSION["uid"]);
515
516                                 array_push($reply["info-for-headlines"],
517                                 array("id" => $id, "labels" => format_article_labels($labels, $id)));
518
519                         }
520                 }
521
522                 $reply["message"] = "UPDATE_COUNTERS";
523
524                 print json_encode($reply);
525         }
526
527         function updateFeedBrowser() {
528                 $search = db_escape_string($_REQUEST["search"]);
529                 $limit = db_escape_string($_REQUEST["limit"]);
530                 $mode = (int) db_escape_string($_REQUEST["mode"]);
531
532                 print json_encode(array("content" =>
533                         make_feed_browser($this->link, $search, $limit, $mode),
534                                 "mode" => $mode));
535         }
536
537         // Silent
538         function massSubscribe() {
539
540                 $payload = json_decode($_REQUEST["payload"], false);
541                 $mode = $_REQUEST["mode"];
542
543                 if (!$payload || !is_array($payload)) return;
544
545                 if ($mode == 1) {
546                         foreach ($payload as $feed) {
547
548                                 $title = db_escape_string($feed[0]);
549                                 $feed_url = db_escape_string($feed[1]);
550
551                                 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
552                                         feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
553
554                                 if (db_num_rows($result) == 0) {
555                                         $result = db_query($this->link, "INSERT INTO ttrss_feeds
556                                                                         (owner_uid,feed_url,title,cat_id,site_url)
557                                                                         VALUES ('".$_SESSION["uid"]."',
558                                                                         '$feed_url', '$title', NULL, '')");
559                                 }
560                         }
561                 } else if ($mode == 2) {
562                         // feed archive
563                         foreach ($payload as $id) {
564                                 $result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds
565                                         WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
566
567                                 if (db_num_rows($result) != 0) {
568                                         $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
569                                         $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
570                                         $title = db_escape_string(db_fetch_result($result, 0, "title"));
571
572                                         $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
573                                                 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
574
575                                         if (db_num_rows($result) == 0) {
576                                                 $result = db_query($this->link, "INSERT INTO ttrss_feeds
577                                                                                 (owner_uid,feed_url,title,cat_id,site_url)
578                                                                         VALUES ('$id','".$_SESSION["uid"]."',
579                                                                         '$feed_url', '$title', NULL, '$site_url')");
580                                         }
581                                 }
582                         }
583                 }
584         }
585
586         function digestgetcontents() {
587                 $article_id = db_escape_string($_REQUEST['article_id']);
588
589                 $result = db_query($this->link, "SELECT content,title,link,marked,published
590                         FROM ttrss_entries, ttrss_user_entries
591                         WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
592
593                 $content = sanitize($this->link, db_fetch_result($result, 0, "content"));
594                 $title = strip_tags(db_fetch_result($result, 0, "title"));
595                 $article_url = htmlspecialchars(db_fetch_result($result, 0, "link"));
596                 $marked = sql_bool_to_bool(db_fetch_result($result, 0, "marked"));
597                 $published = sql_bool_to_bool(db_fetch_result($result, 0, "published"));
598
599                 print json_encode(array("article" =>
600                         array("id" => $article_id, "url" => $article_url,
601                                 "tags" => get_article_tags($this->link, $article_id),
602                                 "marked" => $marked, "published" => $published,
603                                 "title" => $title, "content" => $content)));
604         }
605
606         function digestupdate() {
607                 $feed_id = db_escape_string($_REQUEST['feed_id']);
608                 $offset = db_escape_string($_REQUEST['offset']);
609                 $seq = db_escape_string($_REQUEST['seq']);
610
611                 if (!$feed_id) $feed_id = -4;
612                 if (!$offset) $offset = 0;
613
614                 $reply = array();
615
616                 $reply['seq'] = $seq;
617
618                 $headlines = api_get_headlines($this->link, $feed_id, 30, $offset,
619                                 '', ($feed_id == -4), true, false, "unread", "updated DESC", 0, 0);
620
621                 $reply['headlines'] = array();
622                 $reply['headlines']['title'] = getFeedTitle($this->link, $feed_id);
623                 $reply['headlines']['content'] = $headlines;
624
625                 print json_encode($reply);
626         }
627
628         function digestinit() {
629                 $tmp_feeds = api_get_feeds($this->link, -4, true, false, 0);
630
631                 $feeds = array();
632
633                 foreach ($tmp_feeds as $f) {
634                         if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
635                 }
636
637                 print json_encode(array("feeds" => $feeds));
638         }
639
640         function catchupFeed() {
641                 $feed_id = db_escape_string($_REQUEST['feed_id']);
642                 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
643                 $max_id = (int) db_escape_string($_REQUEST['max_id']);
644
645                 catchup_feed($this->link, $feed_id, $is_cat, false, $max_id);
646
647                 print json_encode(array("message" => "UPDATE_COUNTERS"));
648         }
649
650         function quickAddCat() {
651                 $cat = db_escape_string($_REQUEST["cat"]);
652
653                 add_feed_category($this->link, $cat);
654
655                 $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE
656                         title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
657
658                 if (db_num_rows($result) == 1) {
659                         $id = db_fetch_result($result, 0, "id");
660                 } else {
661                         $id = 0;
662                 }
663
664                 print_feed_cat_select($this->link, "cat_id", $id);
665         }
666
667         function regenFeedKey() {
668                 $feed_id = db_escape_string($_REQUEST['id']);
669                 $is_cat = db_escape_string($_REQUEST['is_cat']) == "true";
670
671                 $new_key = update_feed_access_key($this->link, $feed_id, $is_cat);
672
673                 print json_encode(array("link" => $new_key));
674         }
675
676         // Silent
677         function clearKeys() {
678                 db_query($this->link, "DELETE FROM ttrss_access_keys WHERE
679                         owner_uid = " . $_SESSION["uid"]);
680         }
681
682         // Silent
683         function clearArticleKeys() {
684                 db_query($this->link, "UPDATE ttrss_user_entries SET uuid = '' WHERE
685                         owner_uid = " . $_SESSION["uid"]);
686
687                 return;
688         }
689
690
691         function verifyRegexp() {
692                 $reg_exp = $_REQUEST["reg_exp"];
693
694                 $status = @preg_match("/$reg_exp/i", "TEST") !== false;
695
696                 print json_encode(array("status" => $status));
697         }
698
699         // TODO: unify with digest-get-contents?
700         function cdmGetArticle() {
701                 $ids = array(db_escape_string($_REQUEST["id"]));
702                 $cids = explode(",", $_REQUEST["cids"]);
703
704                 $ids = array_merge($ids, $cids);
705
706                 $rv = array();
707
708                 foreach ($ids as $id) {
709                         $id = (int)$id;
710
711                         $result = db_query($this->link, "SELECT content,
712                                         ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
713                                         ttrss_entries
714                                         WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
715                                         ttrss_entries.id = ref_id AND
716                                         ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
717
718                         if (db_num_rows($result) != 0) {
719                                 $line = db_fetch_assoc($result);
720
721                                 $article_content = sanitize($this->link, $line["content"],
722                                         false, false, $line['site_url']);
723
724                                 array_push($rv,
725                                         array("id" => $id, "content" => $article_content));
726                         }
727                 }
728
729                 print json_encode($rv);
730         }
731
732         function scheduleFeedUpdate() {
733                 $feed_id = db_escape_string($_REQUEST["id"]);
734                 $is_cat =  db_escape_string($_REQUEST['is_cat']) == 'true';
735
736                 $message = __("Your request could not be completed.");
737
738                 if ($feed_id >= 0) {
739                         if (!$is_cat) {
740                                 $message = __("Feed update has been scheduled.");
741
742                                 db_query($this->link, "UPDATE ttrss_feeds SET
743                                         last_update_started = '1970-01-01',
744                                         last_updated = '1970-01-01' WHERE id = '$feed_id' AND
745                                         owner_uid = ".$_SESSION["uid"]);
746
747                         } else {
748                                 $message = __("Category update has been scheduled.");
749
750                                 if ($feed_id)
751                                 $cat_query = "cat_id = '$feed_id'";
752                                 else
753                                 $cat_query = "cat_id IS NULL";
754
755                                 db_query($this->link, "UPDATE ttrss_feeds SET
756                                                 last_update_started = '1970-01-01',
757                                                 last_updated = '1970-01-01' WHERE $cat_query AND
758                                                 owner_uid = ".$_SESSION["uid"]);
759                         }
760                 } else {
761                         $message = __("Can't update this kind of feed.");
762                 }
763
764                 print json_encode(array("message" => $message));
765                 return;
766         }
767
768         function buttonPlugin() {
769                 $pclass = basename($_REQUEST['plugin']) . "_button";
770                 $method = $_REQUEST['plugin_method'];
771
772                 if (class_exists($pclass)) {
773                         $plugin = new $pclass($this->link);
774                         if (method_exists($plugin, $method)) {
775                                 return $plugin->$method();
776                         }
777                 }
778         }
779
780         function genHash() {
781                 $hash = sha1(uniqid(rand(), true));
782
783                 print json_encode(array("hash" => $hash));
784         }
785
786         function batchAddFeeds() {
787                 $cat_id = db_escape_string($_REQUEST['cat']);
788                 $feeds = explode("\n", db_escape_string($_REQUEST['feeds']));
789                 $login = db_escape_string($_REQUEST['login']);
790                 $pass = db_escape_string($_REQUEST['pass']);
791                 $need_auth = db_escape_string($_REQUEST['need_auth']) != "";
792
793                 $result = db_query($this->link, "SELECT twitter_oauth FROM ttrss_users
794 WHERE id = ".$_SESSION['uid']);
795                 $has_oauth = db_fetch_result($result, 0, 'twitter_oauth') != "";
796
797                 foreach ($feeds as $feed) {
798                         $feed = trim($feed);
799
800                         if (validate_feed_url($feed)) {
801
802                                 db_query($this->link, "BEGIN");
803
804                                 if (!$need_auth || !$has_oauth || strpos($url, '://api.twitter.com')
805                                                                                                                                 === false) {
806                                         $update_method = 0;
807                                 } else {
808                                         $update_method = 3;
809                                 }
810
811                                 if ($cat_id == "0" || !$cat_id) {
812                                         $cat_qpart = "NULL";
813                                 } else {
814                                         $cat_qpart = "'$cat_id'";
815                                 }
816
817                                 $result = db_query($this->link,
818                                         "SELECT id FROM ttrss_feeds
819                                         WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]);
820
821                                 if (db_num_rows($result) == 0) {
822                                         $result = db_query($this->link,
823                                                 "INSERT INTO ttrss_feeds
824                                                         (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method)
825                                                 VALUES ('".$_SESSION["uid"]."', '$feed',
826                                                         '[Unknown]', $cat_qpart, '$login', '$pass', '$update_method')");
827                                 }
828
829                                 db_query($this->link, "COMMIT");
830                         }
831                 }
832         }
833
834 }
835 ?>