]>
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 | 10 | function setprofile() { |
3972bf59 | 11 | $id = db_escape_string($this->link, $_REQUEST["id"]); |
46da73c2 | 12 | |
d5112468 AD |
13 | $_SESSION["profile"] = $id; |
14 | $_SESSION["prefs_cache"] = array(); | |
15 | } | |
16 | ||
17 | function remprofiles() { | |
3972bf59 | 18 | $ids = explode(",", db_escape_string($this->link, trim($_REQUEST["ids"]))); |
d5112468 AD |
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() { | |
3972bf59 | 30 | $title = db_escape_string($this->link, trim($_REQUEST["title"])); |
d5112468 AD |
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() { | |
3972bf59 AD |
60 | $id = db_escape_string($this->link, $_REQUEST["id"]); |
61 | $title = db_escape_string($this->link, trim($_REQUEST["value"])); | |
d5112468 AD |
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() { | |
3972bf59 | 91 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
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() { | |
3972bf59 AD |
104 | $feed = db_escape_string($this->link, $_REQUEST['feed']); |
105 | $cat = db_escape_string($this->link, $_REQUEST['cat']); | |
106 | $login = db_escape_string($this->link, $_REQUEST['login']); | |
107 | $pass = db_escape_string($this->link, $_REQUEST['pass']); | |
d5112468 | 108 | |
efc6553d | 109 | $rc = subscribe_to_feed($this->link, $feed, $cat, $login, $pass); |
d5112468 AD |
110 | |
111 | print json_encode(array("result" => $rc)); | |
112 | } | |
113 | ||
d5112468 | 114 | function togglepref() { |
3972bf59 | 115 | $key = db_escape_string($this->link, $_REQUEST["key"]); |
d5112468 AD |
116 | set_pref($this->link, $key, !get_pref($this->link, $key)); |
117 | $value = get_pref($this->link, $key); | |
118 | ||
119 | print json_encode(array("param" =>$key, "value" => $value)); | |
120 | } | |
121 | ||
122 | function setpref() { | |
0380cfa9 | 123 | // set_pref escapes input, so no need to double escape it here |
74eef4fc AD |
124 | $key = $_REQUEST['key']; |
125 | $value = str_replace("\n", "<br/>", $_REQUEST['value']); | |
d5112468 | 126 | |
7e431502 | 127 | set_pref($this->link, $key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET'); |
d5112468 AD |
128 | |
129 | print json_encode(array("param" =>$key, "value" => $value)); | |
130 | } | |
131 | ||
132 | function mark() { | |
133 | $mark = $_REQUEST["mark"]; | |
3972bf59 | 134 | $id = db_escape_string($this->link, $_REQUEST["id"]); |
d5112468 AD |
135 | |
136 | if ($mark == "1") { | |
137 | $mark = "true"; | |
138 | } else { | |
139 | $mark = "false"; | |
140 | } | |
141 | ||
7873d588 AD |
142 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET marked = $mark, |
143 | last_marked = NOW() | |
d5112468 AD |
144 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); |
145 | ||
146 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
147 | } | |
148 | ||
149 | function delete() { | |
3972bf59 | 150 | $ids = db_escape_string($this->link, $_REQUEST["ids"]); |
d5112468 AD |
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() { | |
3972bf59 | 159 | $ids = db_escape_string($this->link, $_REQUEST["ids"]); |
d5112468 AD |
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() { | |
3972bf59 | 169 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
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"]; | |
3972bf59 AD |
212 | $id = db_escape_string($this->link, $_REQUEST["id"]); |
213 | $note = trim(strip_tags(db_escape_string($this->link, $_REQUEST["note"]))); | |
d5112468 AD |
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 | |
7873d588 | 222 | published = $pub, last_published = 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 | ||
5b55e9e2 | 246 | if ($seq) $reply['seq'] = $seq; |
d5112468 | 247 | |
5b55e9e2 AD |
248 | if ($last_article_id != getLastArticleId($this->link)) { |
249 | $reply['counters'] = getAllCounters($this->link); | |
250 | } | |
d5112468 | 251 | |
5b55e9e2 | 252 | $reply['runtime-info'] = make_runtime_info($this->link); |
d5112468 | 253 | |
5b55e9e2 | 254 | print json_encode($reply); |
d5112468 AD |
255 | } |
256 | ||
257 | /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */ | |
258 | function catchupSelected() { | |
3972bf59 | 259 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
260 | $cmode = sprintf("%d", $_REQUEST["cmode"]); |
261 | ||
262 | catchupArticlesById($this->link, $ids, $cmode); | |
263 | ||
264 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
265 | } | |
266 | ||
267 | function markSelected() { | |
3972bf59 | 268 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
269 | $cmode = sprintf("%d", $_REQUEST["cmode"]); |
270 | ||
87d7e850 | 271 | $this->markArticlesById($this->link, $ids, $cmode); |
d5112468 AD |
272 | |
273 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
274 | } | |
275 | ||
276 | function publishSelected() { | |
3972bf59 | 277 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
278 | $cmode = sprintf("%d", $_REQUEST["cmode"]); |
279 | ||
87d7e850 | 280 | $this->publishArticlesById($this->link, $ids, $cmode); |
d5112468 AD |
281 | |
282 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
283 | } | |
284 | ||
285 | function sanityCheck() { | |
286 | $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true"; | |
4f7d69e1 | 287 | $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true"; |
d5112468 AD |
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 | 302 | |
3972bf59 | 303 | $id = db_escape_string($this->link, $_REQUEST["id"]); |
d5112468 | 304 | |
3972bf59 | 305 | $tags_str = db_escape_string($this->link, $_REQUEST["tags_str"]); |
d5112468 AD |
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 | 374 | function completeLabels() { |
3972bf59 | 375 | $search = db_escape_string($this->link, $_REQUEST["search"]); |
1b4d1a6b AD |
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 | 391 | function completeTags() { |
3972bf59 | 392 | $search = db_escape_string($this->link, $_REQUEST["search"]); |
d5112468 AD |
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() { | |
3972bf59 | 407 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
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() { | |
3972bf59 | 422 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
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() { | |
3972bf59 | 435 | $date = db_escape_string($this->link, $_REQUEST["date"]); |
d5112468 AD |
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 | ||
3972bf59 AD |
453 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
454 | $label_id = db_escape_string($this->link, $_REQUEST["lid"]); | |
d5112468 | 455 | |
3972bf59 | 456 | $label = db_escape_string($this->link, label_find_caption($this->link, $label_id, |
d5112468 AD |
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() { | |
3972bf59 AD |
484 | $search = db_escape_string($this->link, $_REQUEST["search"]); |
485 | $limit = db_escape_string($this->link, $_REQUEST["limit"]); | |
486 | $mode = (int) db_escape_string($this->link, $_REQUEST["mode"]); | |
d5112468 | 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 | ||
3972bf59 AD |
506 | $title = db_escape_string($this->link, $feed[0]); |
507 | $feed_url = db_escape_string($this->link, $feed[1]); | |
d5112468 AD |
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) { | |
3972bf59 AD |
526 | $site_url = db_escape_string($this->link, db_fetch_result($result, 0, "site_url")); |
527 | $feed_url = db_escape_string($this->link, db_fetch_result($result, 0, "feed_url")); | |
528 | $title = db_escape_string($this->link, db_fetch_result($result, 0, "title")); | |
d5112468 AD |
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 | 544 | function catchupFeed() { |
3972bf59 AD |
545 | $feed_id = db_escape_string($this->link, $_REQUEST['feed_id']); |
546 | $is_cat = db_escape_string($this->link, $_REQUEST['is_cat']) == "true"; | |
d5112468 | 547 | |
b9a06a0e | 548 | catchup_feed($this->link, $feed_id, $is_cat, false); |
d5112468 AD |
549 | |
550 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
551 | } | |
552 | ||
d5112468 | 553 | function quickAddCat() { |
3972bf59 | 554 | $cat = db_escape_string($this->link, $_REQUEST["cat"]); |
d5112468 AD |
555 | |
556 | add_feed_category($this->link, $cat); | |
557 | ||
558 | $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE | |
559 | title = '$cat' AND owner_uid = " . $_SESSION["uid"]); | |
560 | ||
561 | if (db_num_rows($result) == 1) { | |
562 | $id = db_fetch_result($result, 0, "id"); | |
563 | } else { | |
564 | $id = 0; | |
565 | } | |
566 | ||
567 | print_feed_cat_select($this->link, "cat_id", $id); | |
568 | } | |
569 | ||
570 | function regenFeedKey() { | |
3972bf59 AD |
571 | $feed_id = db_escape_string($this->link, $_REQUEST['id']); |
572 | $is_cat = db_escape_string($this->link, $_REQUEST['is_cat']) == "true"; | |
d5112468 | 573 | |
87d7e850 | 574 | $new_key = $this->update_feed_access_key($this->link, $feed_id, $is_cat); |
d5112468 AD |
575 | |
576 | print json_encode(array("link" => $new_key)); | |
577 | } | |
578 | ||
579 | // Silent | |
580 | function clearKeys() { | |
581 | db_query($this->link, "DELETE FROM ttrss_access_keys WHERE | |
582 | owner_uid = " . $_SESSION["uid"]); | |
583 | } | |
584 | ||
585 | // Silent | |
586 | function clearArticleKeys() { | |
587 | db_query($this->link, "UPDATE ttrss_user_entries SET uuid = '' WHERE | |
588 | owner_uid = " . $_SESSION["uid"]); | |
589 | ||
590 | return; | |
591 | } | |
592 | ||
d5112468 AD |
593 | function verifyRegexp() { |
594 | $reg_exp = $_REQUEST["reg_exp"]; | |
595 | ||
596 | $status = @preg_match("/$reg_exp/i", "TEST") !== false; | |
597 | ||
598 | print json_encode(array("status" => $status)); | |
599 | } | |
600 | ||
19c73507 | 601 | /* function buttonPlugin() { |
369dbc19 | 602 | $pclass = "button_" . basename($_REQUEST['plugin']); |
f9ac31d6 AD |
603 | $method = $_REQUEST['plugin_method']; |
604 | ||
605 | if (class_exists($pclass)) { | |
606 | $plugin = new $pclass($this->link); | |
607 | if (method_exists($plugin, $method)) { | |
608 | return $plugin->$method(); | |
609 | } | |
d5112468 | 610 | } |
19c73507 | 611 | } */ |
d5112468 | 612 | |
d5112468 AD |
613 | function genHash() { |
614 | $hash = sha1(uniqid(rand(), true)); | |
615 | ||
616 | print json_encode(array("hash" => $hash)); | |
617 | } | |
33f0fdd0 AD |
618 | |
619 | function batchAddFeeds() { | |
3972bf59 AD |
620 | $cat_id = db_escape_string($this->link, $_REQUEST['cat']); |
621 | $feeds = explode("\n", db_escape_string($this->link, $_REQUEST['feeds'])); | |
622 | $login = db_escape_string($this->link, $_REQUEST['login']); | |
623 | $pass = db_escape_string($this->link, $_REQUEST['pass']); | |
33f0fdd0 | 624 | |
33f0fdd0 AD |
625 | foreach ($feeds as $feed) { |
626 | $feed = trim($feed); | |
627 | ||
628 | if (validate_feed_url($feed)) { | |
629 | ||
630 | db_query($this->link, "BEGIN"); | |
631 | ||
33f0fdd0 AD |
632 | if ($cat_id == "0" || !$cat_id) { |
633 | $cat_qpart = "NULL"; | |
634 | } else { | |
635 | $cat_qpart = "'$cat_id'"; | |
636 | } | |
637 | ||
638 | $result = db_query($this->link, | |
639 | "SELECT id FROM ttrss_feeds | |
640 | WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]); | |
641 | ||
642 | if (db_num_rows($result) == 0) { | |
643 | $result = db_query($this->link, | |
644 | "INSERT INTO ttrss_feeds | |
645 | (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method) | |
646 | VALUES ('".$_SESSION["uid"]."', '$feed', | |
19b3992b | 647 | '[Unknown]', $cat_qpart, '$login', '$pass', 0)"); |
33f0fdd0 AD |
648 | } |
649 | ||
650 | db_query($this->link, "COMMIT"); | |
651 | } | |
652 | } | |
653 | } | |
654 | ||
beb6ce27 | 655 | function setScore() { |
3972bf59 AD |
656 | $ids = db_escape_string($this->link, $_REQUEST['id']); |
657 | $score = (int)db_escape_string($this->link, $_REQUEST['score']); | |
beb6ce27 AD |
658 | |
659 | db_query($this->link, "UPDATE ttrss_user_entries SET | |
29064218 | 660 | score = '$score' WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]); |
beb6ce27 AD |
661 | |
662 | print json_encode(array("id" => $id, | |
2a3b6de0 | 663 | "score_pic" => get_score_pic($score))); |
beb6ce27 AD |
664 | } |
665 | ||
7d8f5657 AD |
666 | function setpanelmode() { |
667 | $wide = (int) $_REQUEST["wide"]; | |
668 | ||
f03701fe AD |
669 | setcookie("ttrss_widescreen", $wide, |
670 | time() + SESSION_COOKIE_LIFETIME); | |
7d8f5657 AD |
671 | |
672 | print json_encode(array("wide" => $wide)); | |
673 | } | |
674 | ||
8b83bf5f AD |
675 | function updaterandomfeed() { |
676 | // Test if the feed need a update (update interval exceded). | |
677 | if (DB_TYPE == "pgsql") { | |
678 | $update_limit_qpart = "AND (( | |
679 | ttrss_feeds.update_interval = 0 | |
680 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL) | |
681 | ) OR ( | |
682 | ttrss_feeds.update_interval > 0 | |
683 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL) | |
684 | ) OR ttrss_feeds.last_updated IS NULL | |
685 | OR last_updated = '1970-01-01 00:00:00')"; | |
686 | } else { | |
687 | $update_limit_qpart = "AND (( | |
688 | ttrss_feeds.update_interval = 0 | |
689 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE) | |
690 | ) OR ( | |
691 | ttrss_feeds.update_interval > 0 | |
692 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE) | |
693 | ) OR ttrss_feeds.last_updated IS NULL | |
694 | OR last_updated = '1970-01-01 00:00:00')"; | |
695 | } | |
696 | ||
697 | // Test if feed is currently being updated by another process. | |
698 | if (DB_TYPE == "pgsql") { | |
699 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')"; | |
700 | } else { | |
701 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))"; | |
702 | } | |
703 | ||
704 | $random_qpart = sql_random_function(); | |
705 | ||
706 | // We search for feed needing update. | |
707 | $result = db_query($this->link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id | |
708 | FROM | |
709 | ttrss_feeds, ttrss_users, ttrss_user_prefs | |
710 | WHERE | |
711 | ttrss_feeds.owner_uid = ttrss_users.id | |
712 | AND ttrss_users.id = ttrss_user_prefs.owner_uid | |
713 | AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' | |
714 | AND ttrss_feeds.owner_uid = ".$_SESSION["uid"]." | |
715 | $update_limit_qpart $updstart_thresh_qpart | |
716 | ORDER BY $random_qpart LIMIT 30"); | |
717 | ||
718 | $feed_id = -1; | |
719 | ||
720 | require_once "rssfuncs.php"; | |
721 | ||
722 | $num_updated = 0; | |
723 | ||
724 | $tstart = time(); | |
725 | ||
726 | while ($line = db_fetch_assoc($result)) { | |
727 | $feed_id = $line["id"]; | |
728 | ||
6b1a4ecd | 729 | if (time() - $tstart < ini_get("max_execution_time") * 0.7) { |
8b83bf5f AD |
730 | update_rss_feed($this->link, $feed_id, true); |
731 | ++$num_updated; | |
732 | } else { | |
733 | break; | |
734 | } | |
735 | } | |
736 | ||
cda55d67 AD |
737 | // Purge orphans and cleanup tags |
738 | purge_orphans($this->link); | |
739 | cleanup_tags($this->link, 14, 50000); | |
740 | ||
8b83bf5f AD |
741 | if ($num_updated > 0) { |
742 | print json_encode(array("message" => "UPDATE_COUNTERS", | |
743 | "num_updated" => $num_updated)); | |
744 | } else { | |
745 | print json_encode(array("message" => "NOTHING_TO_UPDATE")); | |
746 | } | |
747 | ||
748 | } | |
749 | ||
87d7e850 AD |
750 | function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) { |
751 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
752 | ||
753 | $sql_is_cat = bool_to_sql_bool($is_cat); | |
754 | ||
755 | $result = db_query($link, "SELECT access_key FROM ttrss_access_keys | |
756 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
757 | AND owner_uid = " . $owner_uid); | |
758 | ||
759 | if (db_num_rows($result) == 1) { | |
3972bf59 | 760 | $key = db_escape_string($this->link, sha1(uniqid(rand(), true))); |
87d7e850 AD |
761 | |
762 | db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key' | |
763 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
764 | AND owner_uid = " . $owner_uid); | |
765 | ||
766 | return $key; | |
767 | ||
768 | } else { | |
769 | return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid); | |
770 | } | |
771 | } | |
772 | ||
773 | private function markArticlesById($link, $ids, $cmode) { | |
774 | ||
775 | $tmp_ids = array(); | |
776 | ||
777 | foreach ($ids as $id) { | |
778 | array_push($tmp_ids, "ref_id = '$id'"); | |
779 | } | |
780 | ||
781 | $ids_qpart = join(" OR ", $tmp_ids); | |
782 | ||
783 | if ($cmode == 0) { | |
784 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 785 | marked = false, last_marked = NOW() |
87d7e850 AD |
786 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
787 | } else if ($cmode == 1) { | |
788 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 789 | marked = true, last_marked = NOW() |
87d7e850 AD |
790 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
791 | } else { | |
792 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 793 | marked = NOT marked,last_marked = NOW() |
87d7e850 AD |
794 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
795 | } | |
796 | } | |
797 | ||
798 | private function publishArticlesById($link, $ids, $cmode) { | |
799 | ||
800 | $tmp_ids = array(); | |
801 | ||
802 | foreach ($ids as $id) { | |
803 | array_push($tmp_ids, "ref_id = '$id'"); | |
804 | } | |
805 | ||
806 | $ids_qpart = join(" OR ", $tmp_ids); | |
807 | ||
808 | if ($cmode == 0) { | |
809 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 810 | published = false,last_published = NOW() |
87d7e850 AD |
811 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
812 | } else if ($cmode == 1) { | |
813 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 814 | published = true,last_published = NOW() |
87d7e850 AD |
815 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
816 | } else { | |
817 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 818 | published = NOT published,last_published = NOW() |
87d7e850 AD |
819 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
820 | } | |
821 | ||
822 | if (PUBSUBHUBBUB_HUB) { | |
823 | $rss_link = get_self_url_prefix() . | |
824 | "/public.php?op=rss&id=-2&key=" . | |
825 | get_feed_access_key($link, -2, false); | |
826 | ||
827 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
828 | ||
829 | $pubsub_result = $p->publish_update($rss_link); | |
830 | } | |
831 | } | |
832 | ||
5defc29f | 833 | function getlinktitlebyid() { |
3972bf59 | 834 | $id = db_escape_string($this->link, $_REQUEST['id']); |
7fc2e87e | 835 | |
5defc29f | 836 | $result = db_query($this->link, "SELECT link, title FROM ttrss_entries, ttrss_user_entries |
7fc2e87e AD |
837 | WHERE ref_id = '$id' AND ref_id = id AND owner_uid = ". $_SESSION["uid"]); |
838 | ||
839 | if (db_num_rows($result) != 0) { | |
840 | $link = db_fetch_result($result, 0, "link"); | |
5defc29f | 841 | $title = db_fetch_result($result, 0, "title"); |
7fc2e87e | 842 | |
5defc29f | 843 | echo json_encode(array("link" => $link, "title" => $title)); |
7fc2e87e AD |
844 | } else { |
845 | echo json_encode(array("error" => "ARTICLE_NOT_FOUND")); | |
846 | } | |
847 | } | |
848 | ||
d2f3467b AD |
849 | function cdmArticlePreview() { |
850 | $id = db_escape_string($this->link, $_REQUEST['id']); | |
851 | ||
852 | $result = db_query($this->link, "SELECT link, | |
853 | ttrss_entries.title, content, feed_url | |
854 | FROM | |
855 | ttrss_entries, ttrss_user_entries | |
856 | LEFT JOIN ttrss_feeds ON (ttrss_user_entries.feed_id = ttrss_feeds.id) | |
857 | WHERE ref_id = '$id' AND ref_id = ttrss_entries.id AND | |
858 | ttrss_user_entries.owner_uid = ". $_SESSION["uid"]); | |
859 | ||
860 | if (db_num_rows($result) != 0) { | |
861 | $link = db_fetch_result($result, 0, "link"); | |
862 | $title = db_fetch_result($result, 0, "title"); | |
863 | $feed_url = db_fetch_result($result, 0, "feed_url"); | |
864 | ||
865 | $content = sanitize($this->link, | |
866 | db_fetch_result($result, 0, "content"), false, false, $feed_url); | |
867 | ||
868 | print "<div class='content'>".$content."</content>"; | |
869 | ||
870 | } else { | |
871 | print "Article not found."; | |
872 | } | |
873 | ||
874 | } | |
875 | ||
d5112468 AD |
876 | } |
877 | ?> |