]> git.wh0rd.org - tt-rss.git/blob - classes/rpc.php
Merge branch 'master' of git.fakecake.org:tt-rss into pdo-experimental
[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", "completelabels", "saveprofile");
6
7 return array_search($method, $csrf_ignored) !== false;
8 }
9
10 function setprofile() {
11 $_SESSION["profile"] = $_REQUEST["id"];
12
13 // default value
14 if (!$_SESSION["profile"]) $_SESSION["profile"] = null;
15 }
16
17 function remprofiles() {
18 $ids = explode(",", trim($_REQUEST["ids"]));
19
20 foreach ($ids as $id) {
21 if ($_SESSION["profile"] != $id) {
22 $sth = $this->pdo->prepare("DELETE FROM ttrss_settings_profiles WHERE id = ? AND
23 owner_uid = ?");
24 $sth->execute([$id, $_SESSION['uid']]);
25 }
26 }
27 }
28
29 // Silent
30 function addprofile() {
31 $title = trim($_REQUEST["title"]);
32
33 if ($title) {
34 $this->pdo->beginTransaction();
35
36 $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles
37 WHERE title = ? AND owner_uid = ?");
38 $sth->execute([$title, $_SESSION['uid']]);
39
40 if (!$sth->fetch()) {
41
42 $sth = $this->pdo->prepare("INSERT INTO ttrss_settings_profiles (title, owner_uid)
43 VALUES (?, ?)");
44
45 $sth->execute([$title, $_SESSION['uid']]);
46
47 $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles WHERE
48 title = ? AND owner_uid = ?");
49 $sth->execute([$title, $_SESSION['uid']]);
50
51 if ($row = $sth->fetch()) {
52 $profile_id = $row['id'];
53
54 if ($profile_id) {
55 initialize_user_prefs($_SESSION["uid"], $profile_id);
56 }
57 }
58 }
59
60 $this->pdo->commit();
61 }
62 }
63
64 function saveprofile() {
65 $id = $_REQUEST["id"];
66 $title = trim($_REQUEST["value"]);
67
68 if ($id == 0) {
69 print __("Default profile");
70 return;
71 }
72
73 if ($title) {
74 $sth = $this->pdo->prepare("UPDATE ttrss_settings_profiles
75 SET title = ? WHERE id = ? AND
76 owner_uid = ?");
77
78 $sth->execute([$title, $id, $_SESSION['uid']]);
79 print $title;
80 }
81 }
82
83 // Silent
84 function remarchive() {
85 $ids = explode(",", $_REQUEST["ids"]);
86
87 $sth = $this->pdo->prepare("DELETE FROM ttrss_archived_feeds WHERE
88 (SELECT COUNT(*) FROM ttrss_user_entries
89 WHERE orig_feed_id = :id) = 0 AND
90 id = :id AND owner_uid = :uid");
91
92 foreach ($ids as $id) {
93 $sth->execute([":id" => $id, ":uid" => $_SESSION['uid']]);
94 }
95 }
96
97 function addfeed() {
98 $feed = $_REQUEST['feed'];
99 $cat = $_REQUEST['cat'];
100 $login = $_REQUEST['login'];
101 $pass = trim($_REQUEST['pass']);
102
103 $rc = Feeds::subscribe_to_feed($feed, $cat, $login, $pass);
104
105 print json_encode(array("result" => $rc));
106 }
107
108 function togglepref() {
109 $key = $_REQUEST["key"];
110 set_pref($key, !get_pref($key));
111 $value = get_pref($key);
112
113 print json_encode(array("param" =>$key, "value" => $value));
114 }
115
116 function setpref() {
117 // set_pref escapes input, so no need to double escape it here
118 $key = $_REQUEST['key'];
119 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
120
121 set_pref($key, $value, false, $key != 'USER_STYLESHEET');
122
123 print json_encode(array("param" =>$key, "value" => $value));
124 }
125
126 function mark() {
127 $mark = $_REQUEST["mark"];
128 $id = $_REQUEST["id"];
129
130 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET marked = ?,
131 last_marked = NOW()
132 WHERE ref_id = ? AND owner_uid = ?");
133
134 $sth->execute([$mark, $id, $_SESSION['uid']]);
135
136 print json_encode(array("message" => "UPDATE_COUNTERS"));
137 }
138
139 function delete() {
140 $ids = explode(",", $_REQUEST["ids"]);
141 $ids_qmarks = arr_qmarks($ids);
142
143 $sth = $this->pdo->prepare("DELETE FROM ttrss_user_entries
144 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
145 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
146
147 Article::purge_orphans();
148
149 print json_encode(array("message" => "UPDATE_COUNTERS"));
150 }
151
152 function unarchive() {
153 $ids = explode(",", $_REQUEST["ids"]);
154
155 foreach ($ids as $id) {
156 $this->pdo->beginTransaction();
157
158 $sth = $this->pdo->prepare("SELECT feed_url,site_url,title FROM ttrss_archived_feeds
159 WHERE id = (SELECT orig_feed_id FROM ttrss_user_entries WHERE ref_id = :id
160 AND owner_uid = :uid) AND owner_uid = :uid");
161 $sth->execute([":uid" => $_SESSION['uid'], ":id" => $id]);
162
163 if ($row = $sth->fetch()) {
164 $feed_url = $row['feed_url'];
165 $site_url = $row['site_url'];
166 $title = $row['title'];
167
168 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
169 AND owner_uid = ?");
170 $sth->execute([$feed_url, $_SESSION['uid']]);
171
172 if ($row = $sth->fetch()) {
173 $feed_id = $row["id"];
174 } else {
175 if (!$title) $title = '[Unknown]';
176
177 $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds
178 (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
179 VALUES (?, ?, ?, ?, NULL, '', '', 0)");
180 $sth->execute([$_SESSION['uid'], $feed_url, $site_url, $title]);
181
182 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
183 AND owner_uid = ?");
184 $sth->execute([$feed_url, $_SESSION['uid']]);
185
186 if ($row = $sth->fetch()) {
187 $feed_id = $row['id'];
188 }
189 }
190
191 if ($feed_id) {
192 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries
193 SET feed_id = ?, orig_feed_id = NULL
194 WHERE ref_id = ? AND owner_uid = ?");
195 $sth->execute([$feed_id, $id, $_SESSION['uid']]);
196 }
197 }
198
199 $this->pdo->commit();
200 }
201
202 print json_encode(array("message" => "UPDATE_COUNTERS"));
203 }
204
205 function archive() {
206 $ids = explode(",", $_REQUEST["ids"]);
207
208 foreach ($ids as $id) {
209 $this->archive_article($id, $_SESSION["uid"]);
210 }
211
212 print json_encode(array("message" => "UPDATE_COUNTERS"));
213 }
214
215 private function archive_article($id, $owner_uid) {
216 $this->pdo->beginTransaction();
217
218 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
219
220 $sth = $this->pdo->prepare("SELECT feed_id FROM ttrss_user_entries
221 WHERE ref_id = ? AND owner_uid = ?");
222 $sth->execute([$id, $owner_uid]);
223
224 if ($row = $sth->fetch()) {
225
226 /* prepare the archived table */
227
228 $feed_id = (int) $row['feed_id'];
229
230 if ($feed_id) {
231 $sth = $this->pdo->prepare("SELECT id FROM ttrss_archived_feeds
232 WHERE id = ? AND owner_uid = ?");
233 $sth->execute([$feed_id, $owner_uid]);
234
235 if ($row = $sth->fetch()) {
236 $new_feed_id = $row['id'];
237 } else {
238 $row = $this->pdo->query("SELECT MAX(id) AS id FROM ttrss_archived_feeds")->fetch();
239 $new_feed_id = (int)$row['id'] + 1;
240
241 $sth = $this->pdo->prepare("INSERT INTO ttrss_archived_feeds
242 (id, owner_uid, title, feed_url, site_url)
243 SELECT ?, owner_uid, title, feed_url, site_url from ttrss_feeds
244 WHERE id = ?");
245
246 $sth->execute([$new_feed_id, $feed_id]);
247 }
248
249 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries
250 SET orig_feed_id = ?, feed_id = NULL
251 WHERE ref_id = ? AND owner_uid = ?");
252 $sth->execute([$new_feed_id, $id, $owner_uid]);
253 }
254 }
255
256 $this->pdo->commit();
257 }
258
259 function publ() {
260 $pub = $_REQUEST["pub"];
261 $id = $_REQUEST["id"];
262
263 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
264 published = ?, last_published = NOW()
265 WHERE ref_id = ? AND owner_uid = ?");
266
267 $sth->execute([$pub, $id, $_SESSION['uid']]);
268
269 print json_encode(array("message" => "UPDATE_COUNTERS"));
270 }
271
272 function getAllCounters() {
273 $last_article_id = (int) $_REQUEST["last_article_id"];
274
275 $reply = array();
276
277 if (!empty($_REQUEST['seq'])) $reply['seq'] = (int) $_REQUEST['seq'];
278
279 if ($last_article_id != Article::getLastArticleId()) {
280 $reply['counters'] = Counters::getAllCounters();
281 }
282
283 $reply['runtime-info'] = make_runtime_info();
284
285 print json_encode($reply);
286 }
287
288 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
289 function catchupSelected() {
290 $ids = explode(",", $_REQUEST["ids"]);
291 $cmode = sprintf("%d", $_REQUEST["cmode"]);
292
293 Article::catchupArticlesById($ids, $cmode);
294
295 print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids));
296 }
297
298 function markSelected() {
299 $ids = explode(",", $_REQUEST["ids"]);
300 $cmode = (int)$_REQUEST["cmode"];
301
302 $this->markArticlesById($ids, $cmode);
303
304 print json_encode(array("message" => "UPDATE_COUNTERS"));
305 }
306
307 function publishSelected() {
308 $ids = explode(",", $_REQUEST["ids"]);
309 $cmode = (int)$_REQUEST["cmode"];
310
311 $this->publishArticlesById($ids, $cmode);
312
313 print json_encode(array("message" => "UPDATE_COUNTERS"));
314 }
315
316 function sanityCheck() {
317 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
318 $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true";
319 $_SESSION["hasMp3"] = $_REQUEST["hasMp3"] === "true";
320 $_SESSION["clientTzOffset"] = $_REQUEST["clientTzOffset"];
321
322 $reply = array();
323
324 $reply['error'] = sanity_check();
325
326 if ($reply['error']['code'] == 0) {
327 $reply['init-params'] = make_init_params();
328 $reply['runtime-info'] = make_runtime_info(true);
329 }
330
331 print json_encode($reply);
332 }
333
334 function completeLabels() {
335 $search = $_REQUEST["search"];
336
337 $sth = $this->pdo->query("SELECT DISTINCT caption FROM
338 ttrss_labels2
339 WHERE owner_uid = ? AND
340 LOWER(caption) LIKE LOWER(?) ORDER BY caption
341 LIMIT 5");
342 $sth->execute([$_SESSION['uid'], "%$search%"]);
343
344 print "<ul>";
345 while ($line = $sth->fetch()) {
346 print "<li>" . $line["caption"] . "</li>";
347 }
348 print "</ul>";
349 }
350
351 function updateFeedBrowser() {
352 if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER) return;
353
354 $search = $_REQUEST["search"];
355 $limit = $_REQUEST["limit"];
356 $mode = (int) $_REQUEST["mode"];
357
358 require_once "feedbrowser.php";
359
360 print json_encode(array("content" =>
361 make_feed_browser($search, $limit, $mode),
362 "mode" => $mode));
363 }
364
365 // Silent
366 function massSubscribe() {
367
368 $payload = json_decode($_REQUEST["payload"], false);
369 $mode = $_REQUEST["mode"];
370
371 if (!$payload || !is_array($payload)) return;
372
373 if ($mode == 1) {
374 foreach ($payload as $feed) {
375
376 $title = $feed[0];
377 $feed_url = $feed[1];
378
379 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE
380 feed_url = ? AND owner_uid = ?");
381 $sth->execute([$feed_url, $_SESSION['uid']]);
382
383 if (!$sth->fetch()) {
384 $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds
385 (owner_uid,feed_url,title,cat_id,site_url)
386 VALUES (?, ?, ?, NULL, '')");
387
388 $sth->execute([$_SESSION['uid'], $feed_url, $title]);
389 }
390 }
391 } else if ($mode == 2) {
392 // feed archive
393 foreach ($payload as $id) {
394 $sth = $this->pdo->prepare("SELECT * FROM ttrss_archived_feeds
395 WHERE id = ? AND owner_uid = ?");
396 $sth->execute([$id, $_SESSION['uid']]);
397
398 if ($row = $sth->fetch()) {
399 $site_url = $row['site_url'];
400 $feed_url = $row['feed_url'];
401 $title = $row['title'];
402
403 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE
404 feed_url = ? AND owner_uid = ?");
405 $sth->execute([$feed_url, $_SESSION['uid']]);
406
407 if (!$sth->fetch()) {
408 $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds
409 (owner_uid,feed_url,title,cat_id,site_url)
410 VALUES (?, ?, ?, NULL, ?)");
411
412 $sth->execute([$_SESSION['uid'], $feed_url, $title, $site_url]);
413 }
414 }
415 }
416 }
417 }
418
419 function catchupFeed() {
420 $feed_id = $_REQUEST['feed_id'];
421 $is_cat = $_REQUEST['is_cat'] == "true";
422 $mode = $_REQUEST['mode'];
423 $search_query = $_REQUEST['search_query'];
424 $search_lang = $_REQUEST['search_lang'];
425
426 Feeds::catchup_feed($feed_id, $is_cat, false, $mode, [$search_query, $search_lang]);
427
428 print json_encode(array("message" => "UPDATE_COUNTERS"));
429 }
430
431 function setpanelmode() {
432 $wide = (int) $_REQUEST["wide"];
433
434 setcookie("ttrss_widescreen", $wide,
435 time() + COOKIE_LIFETIME_LONG);
436
437 print json_encode(array("wide" => $wide));
438 }
439
440 static function updaterandomfeed_real() {
441
442 // Test if the feed need a update (update interval exceded).
443 if (DB_TYPE == "pgsql") {
444 $update_limit_qpart = "AND ((
445 ttrss_feeds.update_interval = 0
446 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
447 ) OR (
448 ttrss_feeds.update_interval > 0
449 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
450 ) OR ttrss_feeds.last_updated IS NULL
451 OR last_updated = '1970-01-01 00:00:00')";
452 } else {
453 $update_limit_qpart = "AND ((
454 ttrss_feeds.update_interval = 0
455 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
456 ) OR (
457 ttrss_feeds.update_interval > 0
458 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
459 ) OR ttrss_feeds.last_updated IS NULL
460 OR last_updated = '1970-01-01 00:00:00')";
461 }
462
463 // Test if feed is currently being updated by another process.
464 if (DB_TYPE == "pgsql") {
465 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
466 } else {
467 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
468 }
469
470 $random_qpart = sql_random_function();
471
472 $pdo = Db::pdo();
473
474 // we could be invoked from public.php with no active session
475 if ($_SESSION["uid"]) {
476 $owner_check_qpart = "AND ttrss_feeds.owner_uid = ".$pdo->quote($_SESSION["uid"]);
477 } else {
478 $owner_check_qpart = "";
479 }
480
481 // We search for feed needing update.
482 $res = $pdo->query("SELECT ttrss_feeds.feed_url,ttrss_feeds.id
483 FROM
484 ttrss_feeds, ttrss_users, ttrss_user_prefs
485 WHERE
486 ttrss_feeds.owner_uid = ttrss_users.id
487 AND ttrss_users.id = ttrss_user_prefs.owner_uid
488 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
489 $owner_check_qpart
490 $update_limit_qpart
491 $updstart_thresh_qpart
492 ORDER BY $random_qpart LIMIT 30");
493
494 $num_updated = 0;
495
496 $tstart = time();
497
498 while ($line = $res->fetch()) {
499 $feed_id = $line["id"];
500
501 if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
502 RSSUtils::update_rss_feed($feed_id, true);
503 ++$num_updated;
504 } else {
505 break;
506 }
507 }
508
509 // Purge orphans and cleanup tags
510 Article::purge_orphans();
511 //cleanup_tags(14, 50000);
512
513 if ($num_updated > 0) {
514 print json_encode(array("message" => "UPDATE_COUNTERS",
515 "num_updated" => $num_updated));
516 } else {
517 print json_encode(array("message" => "NOTHING_TO_UPDATE"));
518 }
519
520 }
521
522 function updaterandomfeed() {
523 RPC::updaterandomfeed_real();
524 }
525
526 private function markArticlesById($ids, $cmode) {
527
528 $ids_qmarks = arr_qmarks($ids);
529
530 if ($cmode == 0) {
531 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
532 marked = false, last_marked = NOW()
533 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
534 } else if ($cmode == 1) {
535 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
536 marked = true, last_marked = NOW()
537 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
538 } else {
539 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
540 marked = NOT marked,last_marked = NOW()
541 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
542 }
543
544 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
545 }
546
547 private function publishArticlesById($ids, $cmode) {
548
549 $ids_qmarks = arr_qmarks($ids);
550
551 if ($cmode == 0) {
552 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
553 published = false, last_published = NOW()
554 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
555 } else if ($cmode == 1) {
556 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
557 published = true, last_published = NOW()
558 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
559 } else {
560 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
561 published = NOT published,last_published = NOW()
562 WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
563 }
564
565 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
566 }
567
568 function getlinktitlebyid() {
569 $id = $_REQUEST['id'];
570
571 $sth = $this->pdo->prepare("SELECT link, title FROM ttrss_entries, ttrss_user_entries
572 WHERE ref_id = ? AND ref_id = id AND owner_uid = ?");
573 $sth->execute([$id, $_SESSION['uid']]);
574
575 if ($row = $sth->fetch()) {
576 $link = $row['link'];
577 $title = $row['title'];
578
579 echo json_encode(array("link" => $link, "title" => $title));
580 } else {
581 echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));
582 }
583 }
584
585 function log() {
586 $msg = $_REQUEST['msg'];
587 $file = basename($_REQUEST['file']);
588 $line = (int) $_REQUEST['line'];
589 $context = $_REQUEST['context'];
590
591 if ($msg) {
592 Logger::get()->log_error(E_USER_WARNING,
593 $msg, 'client-js:' . $file, $line, $context);
594
595 echo json_encode(array("message" => "HOST_ERROR_LOGGED"));
596 } else {
597 echo json_encode(array("error" => "MESSAGE_NOT_FOUND"));
598 }
599
600 }
601 }