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