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