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