]>
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 | ||
f0d3c94a AD |
155 | purge_orphans($this->link); |
156 | ||
d5112468 AD |
157 | print json_encode(array("message" => "UPDATE_COUNTERS")); |
158 | } | |
159 | ||
160 | function unarchive() { | |
3972bf59 | 161 | $ids = db_escape_string($this->link, $_REQUEST["ids"]); |
d5112468 AD |
162 | |
163 | $result = db_query($this->link, "UPDATE ttrss_user_entries | |
164 | SET feed_id = orig_feed_id, orig_feed_id = NULL | |
165 | WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]); | |
166 | ||
167 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
168 | } | |
169 | ||
170 | function archive() { | |
3972bf59 | 171 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
172 | |
173 | foreach ($ids as $id) { | |
87d7e850 | 174 | $this->archive_article($this->link, $id, $_SESSION["uid"]); |
d5112468 AD |
175 | } |
176 | ||
177 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
178 | } | |
179 | ||
87d7e850 AD |
180 | private function archive_article($link, $id, $owner_uid) { |
181 | db_query($link, "BEGIN"); | |
182 | ||
183 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
184 | WHERE ref_id = '$id' AND owner_uid = $owner_uid"); | |
185 | ||
186 | if (db_num_rows($result) != 0) { | |
187 | ||
188 | /* prepare the archived table */ | |
189 | ||
190 | $feed_id = (int) db_fetch_result($result, 0, "feed_id"); | |
191 | ||
192 | if ($feed_id) { | |
193 | $result = db_query($link, "SELECT id FROM ttrss_archived_feeds | |
194 | WHERE id = '$feed_id'"); | |
195 | ||
196 | if (db_num_rows($result) == 0) { | |
197 | db_query($link, "INSERT INTO ttrss_archived_feeds | |
198 | (id, owner_uid, title, feed_url, site_url) | |
199 | SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds | |
200 | WHERE id = '$feed_id'"); | |
201 | } | |
202 | ||
203 | db_query($link, "UPDATE ttrss_user_entries | |
204 | SET orig_feed_id = feed_id, feed_id = NULL | |
205 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
206 | } | |
207 | } | |
208 | ||
209 | db_query($link, "COMMIT"); | |
210 | } | |
211 | ||
d5112468 AD |
212 | function publ() { |
213 | $pub = $_REQUEST["pub"]; | |
3972bf59 AD |
214 | $id = db_escape_string($this->link, $_REQUEST["id"]); |
215 | $note = trim(strip_tags(db_escape_string($this->link, $_REQUEST["note"]))); | |
d5112468 AD |
216 | |
217 | if ($pub == "1") { | |
218 | $pub = "true"; | |
219 | } else { | |
220 | $pub = "false"; | |
221 | } | |
222 | ||
223 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET | |
7873d588 | 224 | published = $pub, last_published = NOW() |
d5112468 AD |
225 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); |
226 | ||
227 | $pubsub_result = false; | |
228 | ||
229 | if (PUBSUBHUBBUB_HUB) { | |
230 | $rss_link = get_self_url_prefix() . | |
231 | "/public.php?op=rss&id=-2&key=" . | |
232 | get_feed_access_key($this->link, -2, false); | |
233 | ||
234 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
235 | ||
236 | $pubsub_result = $p->publish_update($rss_link); | |
237 | } | |
238 | ||
239 | print json_encode(array("message" => "UPDATE_COUNTERS", | |
240 | "pubsub_result" => $pubsub_result)); | |
241 | } | |
242 | ||
243 | function getAllCounters() { | |
244 | $last_article_id = (int) $_REQUEST["last_article_id"]; | |
245 | ||
246 | $reply = array(); | |
247 | ||
5b55e9e2 | 248 | if ($seq) $reply['seq'] = $seq; |
d5112468 | 249 | |
5b55e9e2 AD |
250 | if ($last_article_id != getLastArticleId($this->link)) { |
251 | $reply['counters'] = getAllCounters($this->link); | |
252 | } | |
d5112468 | 253 | |
5b55e9e2 | 254 | $reply['runtime-info'] = make_runtime_info($this->link); |
d5112468 | 255 | |
5b55e9e2 | 256 | print json_encode($reply); |
d5112468 AD |
257 | } |
258 | ||
259 | /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */ | |
260 | function catchupSelected() { | |
3972bf59 | 261 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
262 | $cmode = sprintf("%d", $_REQUEST["cmode"]); |
263 | ||
264 | catchupArticlesById($this->link, $ids, $cmode); | |
265 | ||
266 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
267 | } | |
268 | ||
269 | function markSelected() { | |
3972bf59 | 270 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
271 | $cmode = sprintf("%d", $_REQUEST["cmode"]); |
272 | ||
87d7e850 | 273 | $this->markArticlesById($this->link, $ids, $cmode); |
d5112468 AD |
274 | |
275 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
276 | } | |
277 | ||
278 | function publishSelected() { | |
3972bf59 | 279 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
280 | $cmode = sprintf("%d", $_REQUEST["cmode"]); |
281 | ||
87d7e850 | 282 | $this->publishArticlesById($this->link, $ids, $cmode); |
d5112468 AD |
283 | |
284 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
285 | } | |
286 | ||
287 | function sanityCheck() { | |
288 | $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true"; | |
4f7d69e1 | 289 | $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true"; |
d5112468 AD |
290 | |
291 | $reply = array(); | |
292 | ||
293 | $reply['error'] = sanity_check($this->link); | |
294 | ||
295 | if ($reply['error']['code'] == 0) { | |
296 | $reply['init-params'] = make_init_params($this->link); | |
297 | $reply['runtime-info'] = make_runtime_info($this->link); | |
298 | } | |
299 | ||
300 | print json_encode($reply); | |
301 | } | |
302 | ||
303 | function setArticleTags() { | |
d5112468 | 304 | |
3972bf59 | 305 | $id = db_escape_string($this->link, $_REQUEST["id"]); |
d5112468 | 306 | |
3972bf59 | 307 | $tags_str = db_escape_string($this->link, $_REQUEST["tags_str"]); |
d5112468 AD |
308 | $tags = array_unique(trim_array(explode(",", $tags_str))); |
309 | ||
310 | db_query($this->link, "BEGIN"); | |
311 | ||
312 | $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries WHERE | |
313 | ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1"); | |
314 | ||
315 | if (db_num_rows($result) == 1) { | |
316 | ||
317 | $tags_to_cache = array(); | |
318 | ||
319 | $int_id = db_fetch_result($result, 0, "int_id"); | |
320 | ||
321 | db_query($this->link, "DELETE FROM ttrss_tags WHERE | |
322 | post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'"); | |
323 | ||
324 | foreach ($tags as $tag) { | |
325 | $tag = sanitize_tag($tag); | |
326 | ||
327 | if (!tag_is_valid($tag)) { | |
328 | continue; | |
329 | } | |
330 | ||
331 | if (preg_match("/^[0-9]*$/", $tag)) { | |
332 | continue; | |
333 | } | |
334 | ||
335 | // print "<!-- $id : $int_id : $tag -->"; | |
336 | ||
337 | if ($tag != '') { | |
338 | db_query($this->link, "INSERT INTO ttrss_tags | |
339 | (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')"); | |
340 | } | |
341 | ||
342 | array_push($tags_to_cache, $tag); | |
343 | } | |
344 | ||
345 | /* update tag cache */ | |
346 | ||
347 | sort($tags_to_cache); | |
348 | $tags_str = join(",", $tags_to_cache); | |
349 | ||
350 | db_query($this->link, "UPDATE ttrss_user_entries | |
351 | SET tag_cache = '$tags_str' WHERE ref_id = '$id' | |
352 | AND owner_uid = " . $_SESSION["uid"]); | |
353 | } | |
354 | ||
355 | db_query($this->link, "COMMIT"); | |
356 | ||
d5112468 AD |
357 | $tags = get_article_tags($this->link, $id); |
358 | $tags_str = format_tags_string($tags, $id); | |
359 | $tags_str_full = join(", ", $tags); | |
360 | ||
361 | if (!$tags_str_full) $tags_str_full = __("no tags"); | |
362 | ||
363 | print json_encode(array("tags_str" => array("id" => $id, | |
364 | "content" => $tags_str, "content_full" => $tags_str_full))); | |
365 | } | |
366 | ||
367 | function regenOPMLKey() { | |
87d7e850 | 368 | $this->update_feed_access_key($this->link, 'OPML:Publish', |
d5112468 AD |
369 | false, $_SESSION["uid"]); |
370 | ||
50832719 | 371 | $new_link = Opml::opml_publish_url($this->link); |
d5112468 AD |
372 | |
373 | print json_encode(array("link" => $new_link)); | |
374 | } | |
375 | ||
1b4d1a6b | 376 | function completeLabels() { |
3972bf59 | 377 | $search = db_escape_string($this->link, $_REQUEST["search"]); |
1b4d1a6b AD |
378 | |
379 | $result = db_query($this->link, "SELECT DISTINCT caption FROM | |
380 | ttrss_labels2 | |
381 | WHERE owner_uid = '".$_SESSION["uid"]."' AND | |
382 | LOWER(caption) LIKE LOWER('$search%') ORDER BY caption | |
383 | LIMIT 5"); | |
384 | ||
385 | print "<ul>"; | |
386 | while ($line = db_fetch_assoc($result)) { | |
387 | print "<li>" . $line["caption"] . "</li>"; | |
388 | } | |
389 | print "</ul>"; | |
390 | } | |
391 | ||
392 | ||
d5112468 | 393 | function completeTags() { |
3972bf59 | 394 | $search = db_escape_string($this->link, $_REQUEST["search"]); |
d5112468 AD |
395 | |
396 | $result = db_query($this->link, "SELECT DISTINCT tag_name FROM ttrss_tags | |
397 | WHERE owner_uid = '".$_SESSION["uid"]."' AND | |
398 | tag_name LIKE '$search%' ORDER BY tag_name | |
399 | LIMIT 10"); | |
400 | ||
401 | print "<ul>"; | |
402 | while ($line = db_fetch_assoc($result)) { | |
403 | print "<li>" . $line["tag_name"] . "</li>"; | |
404 | } | |
405 | print "</ul>"; | |
406 | } | |
407 | ||
408 | function purge() { | |
3972bf59 | 409 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
410 | $days = sprintf("%d", $_REQUEST["days"]); |
411 | ||
412 | foreach ($ids as $id) { | |
413 | ||
414 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
415 | id = '$id' AND owner_uid = ".$_SESSION["uid"]); | |
416 | ||
417 | if (db_num_rows($result) == 1) { | |
418 | purge_feed($this->link, $id, $days); | |
419 | } | |
420 | } | |
421 | } | |
422 | ||
423 | function getArticles() { | |
3972bf59 | 424 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
d5112468 AD |
425 | $articles = array(); |
426 | ||
427 | foreach ($ids as $id) { | |
428 | if ($id) { | |
429 | array_push($articles, format_article($this->link, $id, 0, false)); | |
430 | } | |
431 | } | |
432 | ||
433 | print json_encode($articles); | |
434 | } | |
435 | ||
436 | function checkDate() { | |
3972bf59 | 437 | $date = db_escape_string($this->link, $_REQUEST["date"]); |
d5112468 AD |
438 | $date_parsed = strtotime($date); |
439 | ||
440 | print json_encode(array("result" => (bool)$date_parsed, | |
441 | "date" => date("c", $date_parsed))); | |
442 | } | |
443 | ||
444 | function assigntolabel() { | |
4fdb8759 | 445 | return $this->labelops(true); |
d5112468 | 446 | } |
46da73c2 | 447 | |
d5112468 | 448 | function removefromlabel() { |
4fdb8759 | 449 | return $this->labelops(false); |
d5112468 | 450 | } |
46da73c2 | 451 | |
d5112468 AD |
452 | function labelops($assign) { |
453 | $reply = array(); | |
454 | ||
3972bf59 AD |
455 | $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"])); |
456 | $label_id = db_escape_string($this->link, $_REQUEST["lid"]); | |
d5112468 | 457 | |
3972bf59 | 458 | $label = db_escape_string($this->link, label_find_caption($this->link, $label_id, |
d5112468 AD |
459 | $_SESSION["uid"])); |
460 | ||
461 | $reply["info-for-headlines"] = array(); | |
462 | ||
463 | if ($label) { | |
464 | ||
465 | foreach ($ids as $id) { | |
466 | ||
467 | if ($assign) | |
468 | label_add_article($this->link, $id, $label, $_SESSION["uid"]); | |
469 | else | |
470 | label_remove_article($this->link, $id, $label, $_SESSION["uid"]); | |
471 | ||
472 | $labels = get_article_labels($this->link, $id, $_SESSION["uid"]); | |
473 | ||
474 | array_push($reply["info-for-headlines"], | |
475 | array("id" => $id, "labels" => format_article_labels($labels, $id))); | |
476 | ||
477 | } | |
478 | } | |
479 | ||
480 | $reply["message"] = "UPDATE_COUNTERS"; | |
481 | ||
482 | print json_encode($reply); | |
483 | } | |
484 | ||
485 | function updateFeedBrowser() { | |
3972bf59 AD |
486 | $search = db_escape_string($this->link, $_REQUEST["search"]); |
487 | $limit = db_escape_string($this->link, $_REQUEST["limit"]); | |
488 | $mode = (int) db_escape_string($this->link, $_REQUEST["mode"]); | |
d5112468 | 489 | |
55c7f092 AD |
490 | require_once "feedbrowser.php"; |
491 | ||
d5112468 AD |
492 | print json_encode(array("content" => |
493 | make_feed_browser($this->link, $search, $limit, $mode), | |
494 | "mode" => $mode)); | |
495 | } | |
496 | ||
497 | // Silent | |
498 | function massSubscribe() { | |
499 | ||
500 | $payload = json_decode($_REQUEST["payload"], false); | |
501 | $mode = $_REQUEST["mode"]; | |
502 | ||
503 | if (!$payload || !is_array($payload)) return; | |
504 | ||
505 | if ($mode == 1) { | |
506 | foreach ($payload as $feed) { | |
507 | ||
3972bf59 AD |
508 | $title = db_escape_string($this->link, $feed[0]); |
509 | $feed_url = db_escape_string($this->link, $feed[1]); | |
d5112468 AD |
510 | |
511 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
512 | feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]); | |
513 | ||
514 | if (db_num_rows($result) == 0) { | |
515 | $result = db_query($this->link, "INSERT INTO ttrss_feeds | |
516 | (owner_uid,feed_url,title,cat_id,site_url) | |
517 | VALUES ('".$_SESSION["uid"]."', | |
518 | '$feed_url', '$title', NULL, '')"); | |
519 | } | |
520 | } | |
521 | } else if ($mode == 2) { | |
522 | // feed archive | |
523 | foreach ($payload as $id) { | |
524 | $result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds | |
525 | WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
526 | ||
527 | if (db_num_rows($result) != 0) { | |
3972bf59 AD |
528 | $site_url = db_escape_string($this->link, db_fetch_result($result, 0, "site_url")); |
529 | $feed_url = db_escape_string($this->link, db_fetch_result($result, 0, "feed_url")); | |
530 | $title = db_escape_string($this->link, db_fetch_result($result, 0, "title")); | |
d5112468 AD |
531 | |
532 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
533 | feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]); | |
534 | ||
535 | if (db_num_rows($result) == 0) { | |
536 | $result = db_query($this->link, "INSERT INTO ttrss_feeds | |
537 | (owner_uid,feed_url,title,cat_id,site_url) | |
538 | VALUES ('$id','".$_SESSION["uid"]."', | |
539 | '$feed_url', '$title', NULL, '$site_url')"); | |
540 | } | |
541 | } | |
542 | } | |
543 | } | |
544 | } | |
545 | ||
d5112468 | 546 | function catchupFeed() { |
3972bf59 AD |
547 | $feed_id = db_escape_string($this->link, $_REQUEST['feed_id']); |
548 | $is_cat = db_escape_string($this->link, $_REQUEST['is_cat']) == "true"; | |
d5112468 | 549 | |
b9a06a0e | 550 | catchup_feed($this->link, $feed_id, $is_cat, false); |
d5112468 AD |
551 | |
552 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
553 | } | |
554 | ||
d5112468 | 555 | function quickAddCat() { |
3972bf59 | 556 | $cat = db_escape_string($this->link, $_REQUEST["cat"]); |
d5112468 AD |
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() { | |
3972bf59 AD |
573 | $feed_id = db_escape_string($this->link, $_REQUEST['id']); |
574 | $is_cat = db_escape_string($this->link, $_REQUEST['is_cat']) == "true"; | |
d5112468 | 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() { | |
3972bf59 AD |
622 | $cat_id = db_escape_string($this->link, $_REQUEST['cat']); |
623 | $feeds = explode("\n", db_escape_string($this->link, $_REQUEST['feeds'])); | |
624 | $login = db_escape_string($this->link, $_REQUEST['login']); | |
625 | $pass = db_escape_string($this->link, $_REQUEST['pass']); | |
33f0fdd0 | 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() { |
3972bf59 AD |
658 | $ids = db_escape_string($this->link, $_REQUEST['id']); |
659 | $score = (int)db_escape_string($this->link, $_REQUEST['score']); | |
beb6ce27 AD |
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, | |
2a3b6de0 | 665 | "score_pic" => get_score_pic($score))); |
beb6ce27 AD |
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 | ||
cda55d67 AD |
739 | // Purge orphans and cleanup tags |
740 | purge_orphans($this->link); | |
741 | cleanup_tags($this->link, 14, 50000); | |
742 | ||
8b83bf5f AD |
743 | if ($num_updated > 0) { |
744 | print json_encode(array("message" => "UPDATE_COUNTERS", | |
745 | "num_updated" => $num_updated)); | |
746 | } else { | |
747 | print json_encode(array("message" => "NOTHING_TO_UPDATE")); | |
748 | } | |
749 | ||
750 | } | |
751 | ||
87d7e850 AD |
752 | function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) { |
753 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
754 | ||
755 | $sql_is_cat = bool_to_sql_bool($is_cat); | |
756 | ||
757 | $result = db_query($link, "SELECT access_key FROM ttrss_access_keys | |
758 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
759 | AND owner_uid = " . $owner_uid); | |
760 | ||
761 | if (db_num_rows($result) == 1) { | |
3972bf59 | 762 | $key = db_escape_string($this->link, sha1(uniqid(rand(), true))); |
87d7e850 AD |
763 | |
764 | db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key' | |
765 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
766 | AND owner_uid = " . $owner_uid); | |
767 | ||
768 | return $key; | |
769 | ||
770 | } else { | |
771 | return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid); | |
772 | } | |
773 | } | |
774 | ||
775 | private function markArticlesById($link, $ids, $cmode) { | |
776 | ||
777 | $tmp_ids = array(); | |
778 | ||
779 | foreach ($ids as $id) { | |
780 | array_push($tmp_ids, "ref_id = '$id'"); | |
781 | } | |
782 | ||
783 | $ids_qpart = join(" OR ", $tmp_ids); | |
784 | ||
785 | if ($cmode == 0) { | |
786 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 787 | marked = false, last_marked = NOW() |
87d7e850 AD |
788 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
789 | } else if ($cmode == 1) { | |
790 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 791 | marked = true, last_marked = NOW() |
87d7e850 AD |
792 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
793 | } else { | |
794 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 795 | marked = NOT marked,last_marked = NOW() |
87d7e850 AD |
796 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
797 | } | |
798 | } | |
799 | ||
800 | private function publishArticlesById($link, $ids, $cmode) { | |
801 | ||
802 | $tmp_ids = array(); | |
803 | ||
804 | foreach ($ids as $id) { | |
805 | array_push($tmp_ids, "ref_id = '$id'"); | |
806 | } | |
807 | ||
808 | $ids_qpart = join(" OR ", $tmp_ids); | |
809 | ||
810 | if ($cmode == 0) { | |
811 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 812 | published = false,last_published = NOW() |
87d7e850 AD |
813 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
814 | } else if ($cmode == 1) { | |
815 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 816 | published = true,last_published = NOW() |
87d7e850 AD |
817 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
818 | } else { | |
819 | db_query($link, "UPDATE ttrss_user_entries SET | |
7873d588 | 820 | published = NOT published,last_published = NOW() |
87d7e850 AD |
821 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); |
822 | } | |
823 | ||
824 | if (PUBSUBHUBBUB_HUB) { | |
825 | $rss_link = get_self_url_prefix() . | |
826 | "/public.php?op=rss&id=-2&key=" . | |
827 | get_feed_access_key($link, -2, false); | |
828 | ||
829 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
830 | ||
831 | $pubsub_result = $p->publish_update($rss_link); | |
832 | } | |
833 | } | |
834 | ||
5defc29f | 835 | function getlinktitlebyid() { |
3972bf59 | 836 | $id = db_escape_string($this->link, $_REQUEST['id']); |
7fc2e87e | 837 | |
5defc29f | 838 | $result = db_query($this->link, "SELECT link, title FROM ttrss_entries, ttrss_user_entries |
7fc2e87e AD |
839 | WHERE ref_id = '$id' AND ref_id = id AND owner_uid = ". $_SESSION["uid"]); |
840 | ||
841 | if (db_num_rows($result) != 0) { | |
842 | $link = db_fetch_result($result, 0, "link"); | |
5defc29f | 843 | $title = db_fetch_result($result, 0, "title"); |
7fc2e87e | 844 | |
5defc29f | 845 | echo json_encode(array("link" => $link, "title" => $title)); |
7fc2e87e AD |
846 | } else { |
847 | echo json_encode(array("error" => "ARTICLE_NOT_FOUND")); | |
848 | } | |
849 | } | |
850 | ||
d2f3467b AD |
851 | function cdmArticlePreview() { |
852 | $id = db_escape_string($this->link, $_REQUEST['id']); | |
853 | ||
854 | $result = db_query($this->link, "SELECT link, | |
855 | ttrss_entries.title, content, feed_url | |
856 | FROM | |
857 | ttrss_entries, ttrss_user_entries | |
858 | LEFT JOIN ttrss_feeds ON (ttrss_user_entries.feed_id = ttrss_feeds.id) | |
859 | WHERE ref_id = '$id' AND ref_id = ttrss_entries.id AND | |
860 | ttrss_user_entries.owner_uid = ". $_SESSION["uid"]); | |
861 | ||
862 | if (db_num_rows($result) != 0) { | |
863 | $link = db_fetch_result($result, 0, "link"); | |
864 | $title = db_fetch_result($result, 0, "title"); | |
865 | $feed_url = db_fetch_result($result, 0, "feed_url"); | |
866 | ||
867 | $content = sanitize($this->link, | |
868 | db_fetch_result($result, 0, "content"), false, false, $feed_url); | |
869 | ||
870 | print "<div class='content'>".$content."</content>"; | |
871 | ||
872 | } else { | |
873 | print "Article not found."; | |
874 | } | |
875 | ||
876 | } | |
877 | ||
d5112468 AD |
878 | } |
879 | ?> |