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