]>
Commit | Line | Data |
---|---|---|
d5112468 | 1 | <?php |
369dbc19 | 2 | class RPC extends Handler_Protected { |
d5112468 | 3 | |
8484ce22 | 4 | function csrf_ignore($method) { |
6c2637d9 | 5 | $csrf_ignored = array("sanitycheck", "completelabels"); |
8484ce22 AD |
6 | |
7 | return array_search($method, $csrf_ignored) !== false; | |
8 | } | |
9 | ||
d5112468 AD |
10 | function setprofile() { |
11 | $id = db_escape_string($_REQUEST["id"]); | |
46da73c2 | 12 | |
d5112468 AD |
13 | $_SESSION["profile"] = $id; |
14 | $_SESSION["prefs_cache"] = array(); | |
15 | } | |
16 | ||
17 | function remprofiles() { | |
18 | $ids = explode(",", db_escape_string(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(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($_REQUEST["id"]); | |
61 | $title = db_escape_string(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($_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($_REQUEST['feed']); | |
105 | $cat = db_escape_string($_REQUEST['cat']); | |
106 | $login = db_escape_string($_REQUEST['login']); | |
107 | $pass = db_escape_string($_REQUEST['pass']); | |
aa60999b | 108 | $need_auth = db_escape_string($_REQUEST['need_auth']) != ""; |
d5112468 | 109 | |
aa60999b | 110 | $rc = subscribe_to_feed($this->link, $feed, $cat, $login, $pass, $need_auth); |
d5112468 AD |
111 | |
112 | print json_encode(array("result" => $rc)); | |
113 | } | |
114 | ||
d5112468 AD |
115 | function togglepref() { |
116 | $key = db_escape_string($_REQUEST["key"]); | |
117 | set_pref($this->link, $key, !get_pref($this->link, $key)); | |
118 | $value = get_pref($this->link, $key); | |
119 | ||
120 | print json_encode(array("param" =>$key, "value" => $value)); | |
121 | } | |
122 | ||
123 | function setpref() { | |
0380cfa9 | 124 | // set_pref escapes input, so no need to double escape it here |
74eef4fc AD |
125 | $key = $_REQUEST['key']; |
126 | $value = str_replace("\n", "<br/>", $_REQUEST['value']); | |
d5112468 | 127 | |
7e431502 | 128 | set_pref($this->link, $key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET'); |
d5112468 AD |
129 | |
130 | print json_encode(array("param" =>$key, "value" => $value)); | |
131 | } | |
132 | ||
133 | function mark() { | |
134 | $mark = $_REQUEST["mark"]; | |
135 | $id = db_escape_string($_REQUEST["id"]); | |
136 | ||
137 | if ($mark == "1") { | |
138 | $mark = "true"; | |
139 | } else { | |
140 | $mark = "false"; | |
141 | } | |
142 | ||
143 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET marked = $mark | |
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($_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 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
156 | } | |
157 | ||
158 | function unarchive() { | |
159 | $ids = db_escape_string($_REQUEST["ids"]); | |
160 | ||
161 | $result = db_query($this->link, "UPDATE ttrss_user_entries | |
162 | SET feed_id = orig_feed_id, orig_feed_id = NULL | |
163 | WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]); | |
164 | ||
165 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
166 | } | |
167 | ||
168 | function archive() { | |
169 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
170 | ||
171 | foreach ($ids as $id) { | |
87d7e850 | 172 | $this->archive_article($this->link, $id, $_SESSION["uid"]); |
d5112468 AD |
173 | } |
174 | ||
175 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
176 | } | |
177 | ||
87d7e850 AD |
178 | private function archive_article($link, $id, $owner_uid) { |
179 | db_query($link, "BEGIN"); | |
180 | ||
181 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
182 | WHERE ref_id = '$id' AND owner_uid = $owner_uid"); | |
183 | ||
184 | if (db_num_rows($result) != 0) { | |
185 | ||
186 | /* prepare the archived table */ | |
187 | ||
188 | $feed_id = (int) db_fetch_result($result, 0, "feed_id"); | |
189 | ||
190 | if ($feed_id) { | |
191 | $result = db_query($link, "SELECT id FROM ttrss_archived_feeds | |
192 | WHERE id = '$feed_id'"); | |
193 | ||
194 | if (db_num_rows($result) == 0) { | |
195 | db_query($link, "INSERT INTO ttrss_archived_feeds | |
196 | (id, owner_uid, title, feed_url, site_url) | |
197 | SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds | |
198 | WHERE id = '$feed_id'"); | |
199 | } | |
200 | ||
201 | db_query($link, "UPDATE ttrss_user_entries | |
202 | SET orig_feed_id = feed_id, feed_id = NULL | |
203 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
204 | } | |
205 | } | |
206 | ||
207 | db_query($link, "COMMIT"); | |
208 | } | |
209 | ||
d5112468 AD |
210 | function publ() { |
211 | $pub = $_REQUEST["pub"]; | |
212 | $id = db_escape_string($_REQUEST["id"]); | |
213 | $note = trim(strip_tags(db_escape_string($_REQUEST["note"]))); | |
214 | ||
215 | if ($pub == "1") { | |
216 | $pub = "true"; | |
217 | } else { | |
218 | $pub = "false"; | |
219 | } | |
220 | ||
221 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET | |
46b78149 | 222 | published = $pub, last_read = NOW() |
d5112468 AD |
223 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); |
224 | ||
225 | $pubsub_result = false; | |
226 | ||
227 | if (PUBSUBHUBBUB_HUB) { | |
228 | $rss_link = get_self_url_prefix() . | |
229 | "/public.php?op=rss&id=-2&key=" . | |
230 | get_feed_access_key($this->link, -2, false); | |
231 | ||
232 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
233 | ||
234 | $pubsub_result = $p->publish_update($rss_link); | |
235 | } | |
236 | ||
237 | print json_encode(array("message" => "UPDATE_COUNTERS", | |
238 | "pubsub_result" => $pubsub_result)); | |
239 | } | |
240 | ||
241 | function getAllCounters() { | |
242 | $last_article_id = (int) $_REQUEST["last_article_id"]; | |
243 | ||
244 | $reply = array(); | |
245 | ||
246 | if ($seq) $reply['seq'] = $seq; | |
247 | ||
248 | if ($last_article_id != getLastArticleId($this->link)) { | |
249 | $omode = $_REQUEST["omode"]; | |
46da73c2 | 250 | |
d5112468 AD |
251 | if ($omode != "T") |
252 | $reply['counters'] = getAllCounters($this->link, $omode); | |
253 | else | |
254 | $reply['counters'] = getGlobalCounters($this->link); | |
255 | } | |
256 | ||
257 | $reply['runtime-info'] = make_runtime_info($this->link); | |
258 | ||
259 | print json_encode($reply); | |
260 | } | |
261 | ||
262 | /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */ | |
263 | function catchupSelected() { | |
264 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
265 | $cmode = sprintf("%d", $_REQUEST["cmode"]); | |
266 | ||
267 | catchupArticlesById($this->link, $ids, $cmode); | |
268 | ||
269 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
270 | } | |
271 | ||
272 | function markSelected() { | |
273 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
274 | $cmode = sprintf("%d", $_REQUEST["cmode"]); | |
275 | ||
87d7e850 | 276 | $this->markArticlesById($this->link, $ids, $cmode); |
d5112468 AD |
277 | |
278 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
279 | } | |
280 | ||
281 | function publishSelected() { | |
282 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
283 | $cmode = sprintf("%d", $_REQUEST["cmode"]); | |
284 | ||
87d7e850 | 285 | $this->publishArticlesById($this->link, $ids, $cmode); |
d5112468 AD |
286 | |
287 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
288 | } | |
289 | ||
290 | function sanityCheck() { | |
291 | $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true"; | |
292 | ||
293 | $reply = array(); | |
294 | ||
295 | $reply['error'] = sanity_check($this->link); | |
296 | ||
297 | if ($reply['error']['code'] == 0) { | |
298 | $reply['init-params'] = make_init_params($this->link); | |
299 | $reply['runtime-info'] = make_runtime_info($this->link); | |
300 | } | |
301 | ||
302 | print json_encode($reply); | |
303 | } | |
304 | ||
305 | function setArticleTags() { | |
d5112468 AD |
306 | |
307 | $id = db_escape_string($_REQUEST["id"]); | |
308 | ||
309 | $tags_str = db_escape_string($_REQUEST["tags_str"]); | |
310 | $tags = array_unique(trim_array(explode(",", $tags_str))); | |
311 | ||
312 | db_query($this->link, "BEGIN"); | |
313 | ||
314 | $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries WHERE | |
315 | ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1"); | |
316 | ||
317 | if (db_num_rows($result) == 1) { | |
318 | ||
319 | $tags_to_cache = array(); | |
320 | ||
321 | $int_id = db_fetch_result($result, 0, "int_id"); | |
322 | ||
323 | db_query($this->link, "DELETE FROM ttrss_tags WHERE | |
324 | post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'"); | |
325 | ||
326 | foreach ($tags as $tag) { | |
327 | $tag = sanitize_tag($tag); | |
328 | ||
329 | if (!tag_is_valid($tag)) { | |
330 | continue; | |
331 | } | |
332 | ||
333 | if (preg_match("/^[0-9]*$/", $tag)) { | |
334 | continue; | |
335 | } | |
336 | ||
337 | // print "<!-- $id : $int_id : $tag -->"; | |
338 | ||
339 | if ($tag != '') { | |
340 | db_query($this->link, "INSERT INTO ttrss_tags | |
341 | (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')"); | |
342 | } | |
343 | ||
344 | array_push($tags_to_cache, $tag); | |
345 | } | |
346 | ||
347 | /* update tag cache */ | |
348 | ||
349 | sort($tags_to_cache); | |
350 | $tags_str = join(",", $tags_to_cache); | |
351 | ||
352 | db_query($this->link, "UPDATE ttrss_user_entries | |
353 | SET tag_cache = '$tags_str' WHERE ref_id = '$id' | |
354 | AND owner_uid = " . $_SESSION["uid"]); | |
355 | } | |
356 | ||
357 | db_query($this->link, "COMMIT"); | |
358 | ||
d5112468 AD |
359 | $tags = get_article_tags($this->link, $id); |
360 | $tags_str = format_tags_string($tags, $id); | |
361 | $tags_str_full = join(", ", $tags); | |
362 | ||
363 | if (!$tags_str_full) $tags_str_full = __("no tags"); | |
364 | ||
365 | print json_encode(array("tags_str" => array("id" => $id, | |
366 | "content" => $tags_str, "content_full" => $tags_str_full))); | |
367 | } | |
368 | ||
369 | function regenOPMLKey() { | |
87d7e850 | 370 | $this->update_feed_access_key($this->link, 'OPML:Publish', |
d5112468 AD |
371 | false, $_SESSION["uid"]); |
372 | ||
373 | $new_link = opml_publish_url($this->link); | |
374 | ||
375 | print json_encode(array("link" => $new_link)); | |
376 | } | |
377 | ||
1b4d1a6b AD |
378 | function completeLabels() { |
379 | $search = db_escape_string($_REQUEST["search"]); | |
380 | ||
381 | $result = db_query($this->link, "SELECT DISTINCT caption FROM | |
382 | ttrss_labels2 | |
383 | WHERE owner_uid = '".$_SESSION["uid"]."' AND | |
384 | LOWER(caption) LIKE LOWER('$search%') ORDER BY caption | |
385 | LIMIT 5"); | |
386 | ||
387 | print "<ul>"; | |
388 | while ($line = db_fetch_assoc($result)) { | |
389 | print "<li>" . $line["caption"] . "</li>"; | |
390 | } | |
391 | print "</ul>"; | |
392 | } | |
393 | ||
394 | ||
d5112468 AD |
395 | function completeTags() { |
396 | $search = db_escape_string($_REQUEST["search"]); | |
397 | ||
398 | $result = db_query($this->link, "SELECT DISTINCT tag_name FROM ttrss_tags | |
399 | WHERE owner_uid = '".$_SESSION["uid"]."' AND | |
400 | tag_name LIKE '$search%' ORDER BY tag_name | |
401 | LIMIT 10"); | |
402 | ||
403 | print "<ul>"; | |
404 | while ($line = db_fetch_assoc($result)) { | |
405 | print "<li>" . $line["tag_name"] . "</li>"; | |
406 | } | |
407 | print "</ul>"; | |
408 | } | |
409 | ||
410 | function purge() { | |
411 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
412 | $days = sprintf("%d", $_REQUEST["days"]); | |
413 | ||
414 | foreach ($ids as $id) { | |
415 | ||
416 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
417 | id = '$id' AND owner_uid = ".$_SESSION["uid"]); | |
418 | ||
419 | if (db_num_rows($result) == 1) { | |
420 | purge_feed($this->link, $id, $days); | |
421 | } | |
422 | } | |
423 | } | |
424 | ||
425 | function getArticles() { | |
426 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
427 | $articles = array(); | |
428 | ||
429 | foreach ($ids as $id) { | |
430 | if ($id) { | |
431 | array_push($articles, format_article($this->link, $id, 0, false)); | |
432 | } | |
433 | } | |
434 | ||
435 | print json_encode($articles); | |
436 | } | |
437 | ||
438 | function checkDate() { | |
439 | $date = db_escape_string($_REQUEST["date"]); | |
440 | $date_parsed = strtotime($date); | |
441 | ||
442 | print json_encode(array("result" => (bool)$date_parsed, | |
443 | "date" => date("c", $date_parsed))); | |
444 | } | |
445 | ||
446 | function assigntolabel() { | |
4fdb8759 | 447 | return $this->labelops(true); |
d5112468 | 448 | } |
46da73c2 | 449 | |
d5112468 | 450 | function removefromlabel() { |
4fdb8759 | 451 | return $this->labelops(false); |
d5112468 | 452 | } |
46da73c2 | 453 | |
d5112468 AD |
454 | function labelops($assign) { |
455 | $reply = array(); | |
456 | ||
457 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
458 | $label_id = db_escape_string($_REQUEST["lid"]); | |
459 | ||
460 | $label = db_escape_string(label_find_caption($this->link, $label_id, | |
461 | $_SESSION["uid"])); | |
462 | ||
463 | $reply["info-for-headlines"] = array(); | |
464 | ||
465 | if ($label) { | |
466 | ||
467 | foreach ($ids as $id) { | |
468 | ||
469 | if ($assign) | |
470 | label_add_article($this->link, $id, $label, $_SESSION["uid"]); | |
471 | else | |
472 | label_remove_article($this->link, $id, $label, $_SESSION["uid"]); | |
473 | ||
474 | $labels = get_article_labels($this->link, $id, $_SESSION["uid"]); | |
475 | ||
476 | array_push($reply["info-for-headlines"], | |
477 | array("id" => $id, "labels" => format_article_labels($labels, $id))); | |
478 | ||
479 | } | |
480 | } | |
481 | ||
482 | $reply["message"] = "UPDATE_COUNTERS"; | |
483 | ||
484 | print json_encode($reply); | |
485 | } | |
486 | ||
487 | function updateFeedBrowser() { | |
488 | $search = db_escape_string($_REQUEST["search"]); | |
489 | $limit = db_escape_string($_REQUEST["limit"]); | |
490 | $mode = (int) db_escape_string($_REQUEST["mode"]); | |
491 | ||
55c7f092 AD |
492 | require_once "feedbrowser.php"; |
493 | ||
d5112468 AD |
494 | print json_encode(array("content" => |
495 | make_feed_browser($this->link, $search, $limit, $mode), | |
496 | "mode" => $mode)); | |
497 | } | |
498 | ||
499 | // Silent | |
500 | function massSubscribe() { | |
501 | ||
502 | $payload = json_decode($_REQUEST["payload"], false); | |
503 | $mode = $_REQUEST["mode"]; | |
504 | ||
505 | if (!$payload || !is_array($payload)) return; | |
506 | ||
507 | if ($mode == 1) { | |
508 | foreach ($payload as $feed) { | |
509 | ||
510 | $title = db_escape_string($feed[0]); | |
511 | $feed_url = db_escape_string($feed[1]); | |
512 | ||
513 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
514 | feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]); | |
515 | ||
516 | if (db_num_rows($result) == 0) { | |
517 | $result = db_query($this->link, "INSERT INTO ttrss_feeds | |
518 | (owner_uid,feed_url,title,cat_id,site_url) | |
519 | VALUES ('".$_SESSION["uid"]."', | |
520 | '$feed_url', '$title', NULL, '')"); | |
521 | } | |
522 | } | |
523 | } else if ($mode == 2) { | |
524 | // feed archive | |
525 | foreach ($payload as $id) { | |
526 | $result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds | |
527 | WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
528 | ||
529 | if (db_num_rows($result) != 0) { | |
530 | $site_url = db_escape_string(db_fetch_result($result, 0, "site_url")); | |
531 | $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url")); | |
532 | $title = db_escape_string(db_fetch_result($result, 0, "title")); | |
533 | ||
534 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
535 | feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]); | |
536 | ||
537 | if (db_num_rows($result) == 0) { | |
538 | $result = db_query($this->link, "INSERT INTO ttrss_feeds | |
539 | (owner_uid,feed_url,title,cat_id,site_url) | |
540 | VALUES ('$id','".$_SESSION["uid"]."', | |
541 | '$feed_url', '$title', NULL, '$site_url')"); | |
542 | } | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
d5112468 AD |
548 | function catchupFeed() { |
549 | $feed_id = db_escape_string($_REQUEST['feed_id']); | |
550 | $is_cat = db_escape_string($_REQUEST['is_cat']) == "true"; | |
184f5195 | 551 | $max_id = (int) db_escape_string($_REQUEST['max_id']); |
d5112468 | 552 | |
184f5195 | 553 | catchup_feed($this->link, $feed_id, $is_cat, false, $max_id); |
d5112468 AD |
554 | |
555 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
556 | } | |
557 | ||
d5112468 AD |
558 | function quickAddCat() { |
559 | $cat = db_escape_string($_REQUEST["cat"]); | |
560 | ||
561 | add_feed_category($this->link, $cat); | |
562 | ||
563 | $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE | |
564 | title = '$cat' AND owner_uid = " . $_SESSION["uid"]); | |
565 | ||
566 | if (db_num_rows($result) == 1) { | |
567 | $id = db_fetch_result($result, 0, "id"); | |
568 | } else { | |
569 | $id = 0; | |
570 | } | |
571 | ||
572 | print_feed_cat_select($this->link, "cat_id", $id); | |
573 | } | |
574 | ||
575 | function regenFeedKey() { | |
576 | $feed_id = db_escape_string($_REQUEST['id']); | |
577 | $is_cat = db_escape_string($_REQUEST['is_cat']) == "true"; | |
578 | ||
87d7e850 | 579 | $new_key = $this->update_feed_access_key($this->link, $feed_id, $is_cat); |
d5112468 AD |
580 | |
581 | print json_encode(array("link" => $new_key)); | |
582 | } | |
583 | ||
584 | // Silent | |
585 | function clearKeys() { | |
586 | db_query($this->link, "DELETE FROM ttrss_access_keys WHERE | |
587 | owner_uid = " . $_SESSION["uid"]); | |
588 | } | |
589 | ||
590 | // Silent | |
591 | function clearArticleKeys() { | |
592 | db_query($this->link, "UPDATE ttrss_user_entries SET uuid = '' WHERE | |
593 | owner_uid = " . $_SESSION["uid"]); | |
594 | ||
595 | return; | |
596 | } | |
597 | ||
d5112468 AD |
598 | function verifyRegexp() { |
599 | $reg_exp = $_REQUEST["reg_exp"]; | |
600 | ||
601 | $status = @preg_match("/$reg_exp/i", "TEST") !== false; | |
602 | ||
603 | print json_encode(array("status" => $status)); | |
604 | } | |
605 | ||
19c73507 | 606 | /* function buttonPlugin() { |
369dbc19 | 607 | $pclass = "button_" . basename($_REQUEST['plugin']); |
f9ac31d6 AD |
608 | $method = $_REQUEST['plugin_method']; |
609 | ||
610 | if (class_exists($pclass)) { | |
611 | $plugin = new $pclass($this->link); | |
612 | if (method_exists($plugin, $method)) { | |
613 | return $plugin->$method(); | |
614 | } | |
d5112468 | 615 | } |
19c73507 | 616 | } */ |
d5112468 | 617 | |
d5112468 AD |
618 | function genHash() { |
619 | $hash = sha1(uniqid(rand(), true)); | |
620 | ||
621 | print json_encode(array("hash" => $hash)); | |
622 | } | |
33f0fdd0 AD |
623 | |
624 | function batchAddFeeds() { | |
625 | $cat_id = db_escape_string($_REQUEST['cat']); | |
626 | $feeds = explode("\n", db_escape_string($_REQUEST['feeds'])); | |
627 | $login = db_escape_string($_REQUEST['login']); | |
628 | $pass = db_escape_string($_REQUEST['pass']); | |
629 | $need_auth = db_escape_string($_REQUEST['need_auth']) != ""; | |
630 | ||
33f0fdd0 AD |
631 | foreach ($feeds as $feed) { |
632 | $feed = trim($feed); | |
633 | ||
634 | if (validate_feed_url($feed)) { | |
635 | ||
636 | db_query($this->link, "BEGIN"); | |
637 | ||
33f0fdd0 AD |
638 | if ($cat_id == "0" || !$cat_id) { |
639 | $cat_qpart = "NULL"; | |
640 | } else { | |
641 | $cat_qpart = "'$cat_id'"; | |
642 | } | |
643 | ||
644 | $result = db_query($this->link, | |
645 | "SELECT id FROM ttrss_feeds | |
646 | WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]); | |
647 | ||
648 | if (db_num_rows($result) == 0) { | |
649 | $result = db_query($this->link, | |
650 | "INSERT INTO ttrss_feeds | |
651 | (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method) | |
652 | VALUES ('".$_SESSION["uid"]."', '$feed', | |
19b3992b | 653 | '[Unknown]', $cat_qpart, '$login', '$pass', 0)"); |
33f0fdd0 AD |
654 | } |
655 | ||
656 | db_query($this->link, "COMMIT"); | |
657 | } | |
658 | } | |
659 | } | |
660 | ||
beb6ce27 | 661 | function setScore() { |
29064218 | 662 | $ids = db_escape_string($_REQUEST['id']); |
beb6ce27 AD |
663 | $score = (int)db_escape_string($_REQUEST['score']); |
664 | ||
665 | db_query($this->link, "UPDATE ttrss_user_entries SET | |
29064218 | 666 | score = '$score' WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]); |
beb6ce27 AD |
667 | |
668 | print json_encode(array("id" => $id, | |
669 | "score_pic" => theme_image($link, get_score_pic($score)))); | |
670 | } | |
671 | ||
7d8f5657 AD |
672 | function setpanelmode() { |
673 | $wide = (int) $_REQUEST["wide"]; | |
674 | ||
675 | $_SESSION["widescreen"] = $wide; | |
676 | ||
677 | print json_encode(array("wide" => $wide)); | |
678 | } | |
679 | ||
8b83bf5f AD |
680 | function updaterandomfeed() { |
681 | // Test if the feed need a update (update interval exceded). | |
682 | if (DB_TYPE == "pgsql") { | |
683 | $update_limit_qpart = "AND (( | |
684 | ttrss_feeds.update_interval = 0 | |
685 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL) | |
686 | ) OR ( | |
687 | ttrss_feeds.update_interval > 0 | |
688 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL) | |
689 | ) OR ttrss_feeds.last_updated IS NULL | |
690 | OR last_updated = '1970-01-01 00:00:00')"; | |
691 | } else { | |
692 | $update_limit_qpart = "AND (( | |
693 | ttrss_feeds.update_interval = 0 | |
694 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE) | |
695 | ) OR ( | |
696 | ttrss_feeds.update_interval > 0 | |
697 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE) | |
698 | ) OR ttrss_feeds.last_updated IS NULL | |
699 | OR last_updated = '1970-01-01 00:00:00')"; | |
700 | } | |
701 | ||
702 | // Test if feed is currently being updated by another process. | |
703 | if (DB_TYPE == "pgsql") { | |
704 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')"; | |
705 | } else { | |
706 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))"; | |
707 | } | |
708 | ||
709 | $random_qpart = sql_random_function(); | |
710 | ||
711 | // We search for feed needing update. | |
712 | $result = db_query($this->link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id | |
713 | FROM | |
714 | ttrss_feeds, ttrss_users, ttrss_user_prefs | |
715 | WHERE | |
716 | ttrss_feeds.owner_uid = ttrss_users.id | |
717 | AND ttrss_users.id = ttrss_user_prefs.owner_uid | |
718 | AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' | |
719 | AND ttrss_feeds.owner_uid = ".$_SESSION["uid"]." | |
720 | $update_limit_qpart $updstart_thresh_qpart | |
721 | ORDER BY $random_qpart LIMIT 30"); | |
722 | ||
723 | $feed_id = -1; | |
724 | ||
725 | require_once "rssfuncs.php"; | |
726 | ||
727 | $num_updated = 0; | |
728 | ||
729 | $tstart = time(); | |
730 | ||
731 | while ($line = db_fetch_assoc($result)) { | |
732 | $feed_id = $line["id"]; | |
733 | ||
6b1a4ecd | 734 | if (time() - $tstart < ini_get("max_execution_time") * 0.7) { |
8b83bf5f AD |
735 | update_rss_feed($this->link, $feed_id, true); |
736 | ++$num_updated; | |
737 | } else { | |
738 | break; | |
739 | } | |
740 | } | |
741 | ||
742 | if ($num_updated > 0) { | |
743 | print json_encode(array("message" => "UPDATE_COUNTERS", | |
744 | "num_updated" => $num_updated)); | |
745 | } else { | |
746 | print json_encode(array("message" => "NOTHING_TO_UPDATE")); | |
747 | } | |
748 | ||
749 | } | |
750 | ||
87d7e850 AD |
751 | function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) { |
752 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
753 | ||
754 | $sql_is_cat = bool_to_sql_bool($is_cat); | |
755 | ||
756 | $result = db_query($link, "SELECT access_key FROM ttrss_access_keys | |
757 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
758 | AND owner_uid = " . $owner_uid); | |
759 | ||
760 | if (db_num_rows($result) == 1) { | |
761 | $key = db_escape_string(sha1(uniqid(rand(), true))); | |
762 | ||
763 | db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key' | |
764 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
765 | AND owner_uid = " . $owner_uid); | |
766 | ||
767 | return $key; | |
768 | ||
769 | } else { | |
770 | return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid); | |
771 | } | |
772 | } | |
773 | ||
774 | private function markArticlesById($link, $ids, $cmode) { | |
775 | ||
776 | $tmp_ids = array(); | |
777 | ||
778 | foreach ($ids as $id) { | |
779 | array_push($tmp_ids, "ref_id = '$id'"); | |
780 | } | |
781 | ||
782 | $ids_qpart = join(" OR ", $tmp_ids); | |
783 | ||
784 | if ($cmode == 0) { | |
785 | db_query($link, "UPDATE ttrss_user_entries SET | |
786 | marked = false,last_read = NOW() | |
787 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
788 | } else if ($cmode == 1) { | |
789 | db_query($link, "UPDATE ttrss_user_entries SET | |
790 | marked = true | |
791 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
792 | } else { | |
793 | db_query($link, "UPDATE ttrss_user_entries SET | |
794 | marked = NOT marked,last_read = NOW() | |
795 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
796 | } | |
797 | } | |
798 | ||
799 | private function publishArticlesById($link, $ids, $cmode) { | |
800 | ||
801 | $tmp_ids = array(); | |
802 | ||
803 | foreach ($ids as $id) { | |
804 | array_push($tmp_ids, "ref_id = '$id'"); | |
805 | } | |
806 | ||
807 | $ids_qpart = join(" OR ", $tmp_ids); | |
808 | ||
809 | if ($cmode == 0) { | |
810 | db_query($link, "UPDATE ttrss_user_entries SET | |
811 | published = false,last_read = NOW() | |
812 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
813 | } else if ($cmode == 1) { | |
814 | db_query($link, "UPDATE ttrss_user_entries SET | |
815 | published = true,last_read = NOW() | |
816 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
817 | } else { | |
818 | db_query($link, "UPDATE ttrss_user_entries SET | |
819 | published = NOT published,last_read = NOW() | |
820 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
821 | } | |
822 | ||
823 | if (PUBSUBHUBBUB_HUB) { | |
824 | $rss_link = get_self_url_prefix() . | |
825 | "/public.php?op=rss&id=-2&key=" . | |
826 | get_feed_access_key($link, -2, false); | |
827 | ||
828 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
829 | ||
830 | $pubsub_result = $p->publish_update($rss_link); | |
831 | } | |
832 | } | |
833 | ||
d5112468 AD |
834 | } |
835 | ?> |