]>
Commit | Line | Data |
---|---|---|
d5112468 | 1 | <?php |
46da73c2 | 2 | class RPC extends Protected_Handler { |
d5112468 | 3 | |
8484ce22 | 4 | function csrf_ignore($method) { |
566faa14 | 5 | $csrf_ignored = array("sanitycheck", "buttonplugin", "exportget"); |
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 | ||
566faa14 AD |
17 | function exportget() { |
18 | $exportname = CACHE_DIR . "/export/" . | |
19 | sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml"; | |
20 | ||
21 | if (file_exists($exportname)) { | |
22 | header("Content-type: text/xml"); | |
566faa14 | 23 | |
dddd80cf AD |
24 | if (function_exists('gzencode')) { |
25 | header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz"); | |
26 | echo gzencode(file_get_contents($exportname)); | |
27 | } else { | |
28 | header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml"); | |
29 | echo file_get_contents($exportname); | |
30 | } | |
566faa14 AD |
31 | } else { |
32 | echo "File not found."; | |
33 | } | |
34 | } | |
35 | ||
36 | function exportrun() { | |
37 | $offset = (int) db_escape_string($_REQUEST['offset']); | |
38 | $exported = 0; | |
39 | $limit = 250; | |
40 | ||
41 | if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) { | |
42 | $result = db_query($this->link, "SELECT | |
43 | ttrss_entries.guid, | |
44 | ttrss_entries.title, | |
45 | content, | |
46 | marked, | |
47 | published, | |
48 | score, | |
49 | note, | |
2c43770f | 50 | link, |
566faa14 AD |
51 | tag_cache, |
52 | label_cache, | |
53 | ttrss_feeds.title AS feed_title, | |
54 | ttrss_feeds.feed_url AS feed_url, | |
55 | ttrss_entries.updated | |
56 | FROM | |
57 | ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id), | |
58 | ttrss_entries | |
59 | WHERE | |
60 | (marked = true OR feed_id IS NULL) AND | |
61 | ref_id = ttrss_entries.id AND | |
62 | ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . " | |
63 | ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset"); | |
64 | ||
65 | $exportname = sha1($_SESSION['uid'] . $_SESSION['login']); | |
66 | ||
67 | if ($offset == 0) { | |
68 | $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "w"); | |
69 | fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION."\">"); | |
70 | } else { | |
71 | $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "a"); | |
72 | } | |
73 | ||
74 | if ($fp) { | |
75 | ||
76 | while ($line = db_fetch_assoc($result)) { | |
77 | fputs($fp, "<article>"); | |
78 | ||
79 | foreach ($line as $k => $v) { | |
80 | fputs($fp, "<$k><![CDATA[$v]]></$k>"); | |
81 | } | |
82 | ||
83 | fputs($fp, "</article>"); | |
84 | } | |
85 | ||
86 | $exported = db_num_rows($result); | |
87 | ||
88 | if ($exported < $limit && $exported > 0) { | |
89 | fputs($fp, "</articles>"); | |
90 | } | |
91 | ||
92 | fclose($fp); | |
93 | } | |
94 | ||
95 | } | |
96 | ||
97 | print json_encode(array("exported" => $exported)); | |
98 | } | |
99 | ||
d5112468 AD |
100 | function remprofiles() { |
101 | $ids = explode(",", db_escape_string(trim($_REQUEST["ids"]))); | |
102 | ||
103 | foreach ($ids as $id) { | |
104 | if ($_SESSION["profile"] != $id) { | |
105 | db_query($this->link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND | |
106 | owner_uid = " . $_SESSION["uid"]); | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | // Silent | |
112 | function addprofile() { | |
113 | $title = db_escape_string(trim($_REQUEST["title"])); | |
114 | if ($title) { | |
115 | db_query($this->link, "BEGIN"); | |
116 | ||
117 | $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles | |
118 | WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]); | |
119 | ||
120 | if (db_num_rows($result) == 0) { | |
121 | ||
122 | db_query($this->link, "INSERT INTO ttrss_settings_profiles (title, owner_uid) | |
123 | VALUES ('$title', ".$_SESSION["uid"] .")"); | |
124 | ||
125 | $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles WHERE | |
126 | title = '$title'"); | |
127 | ||
128 | if (db_num_rows($result) != 0) { | |
129 | $profile_id = db_fetch_result($result, 0, "id"); | |
130 | ||
131 | if ($profile_id) { | |
132 | initialize_user_prefs($this->link, $_SESSION["uid"], $profile_id); | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | db_query($this->link, "COMMIT"); | |
138 | } | |
139 | } | |
140 | ||
141 | // Silent | |
142 | function saveprofile() { | |
143 | $id = db_escape_string($_REQUEST["id"]); | |
144 | $title = db_escape_string(trim($_REQUEST["value"])); | |
145 | ||
146 | if ($id == 0) { | |
147 | print __("Default profile"); | |
148 | return; | |
149 | } | |
150 | ||
151 | if ($title) { | |
152 | db_query($this->link, "BEGIN"); | |
153 | ||
154 | $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles | |
155 | WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]); | |
156 | ||
157 | if (db_num_rows($result) == 0) { | |
158 | db_query($this->link, "UPDATE ttrss_settings_profiles | |
159 | SET title = '$title' WHERE id = '$id' AND | |
160 | owner_uid = " . $_SESSION["uid"]); | |
161 | print $title; | |
162 | } else { | |
163 | $result = db_query($this->link, "SELECT title FROM ttrss_settings_profiles | |
164 | WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]); | |
165 | print db_fetch_result($result, 0, "title"); | |
166 | } | |
167 | ||
168 | db_query($this->link, "COMMIT"); | |
169 | } | |
170 | } | |
171 | ||
172 | // Silent | |
173 | function remarchive() { | |
174 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
175 | ||
176 | foreach ($ids as $id) { | |
177 | $result = db_query($this->link, "DELETE FROM ttrss_archived_feeds WHERE | |
178 | (SELECT COUNT(*) FROM ttrss_user_entries | |
179 | WHERE orig_feed_id = '$id') = 0 AND | |
180 | id = '$id' AND owner_uid = ".$_SESSION["uid"]); | |
181 | ||
182 | $rc = db_affected_rows($this->link, $result); | |
183 | } | |
184 | } | |
185 | ||
186 | function addfeed() { | |
187 | $feed = db_escape_string($_REQUEST['feed']); | |
188 | $cat = db_escape_string($_REQUEST['cat']); | |
189 | $login = db_escape_string($_REQUEST['login']); | |
190 | $pass = db_escape_string($_REQUEST['pass']); | |
aa60999b | 191 | $need_auth = db_escape_string($_REQUEST['need_auth']) != ""; |
d5112468 | 192 | |
aa60999b | 193 | $rc = subscribe_to_feed($this->link, $feed, $cat, $login, $pass, $need_auth); |
d5112468 AD |
194 | |
195 | print json_encode(array("result" => $rc)); | |
196 | } | |
197 | ||
198 | function extractfeedurls() { | |
199 | $urls = get_feeds_from_html($_REQUEST['url']); | |
200 | ||
201 | print json_encode(array("urls" => $urls)); | |
202 | } | |
203 | ||
204 | function togglepref() { | |
205 | $key = db_escape_string($_REQUEST["key"]); | |
206 | set_pref($this->link, $key, !get_pref($this->link, $key)); | |
207 | $value = get_pref($this->link, $key); | |
208 | ||
209 | print json_encode(array("param" =>$key, "value" => $value)); | |
210 | } | |
211 | ||
212 | function setpref() { | |
213 | $value = str_replace("\n", "<br/>", $_REQUEST['value']); | |
214 | ||
0380cfa9 | 215 | // set_pref escapes input, so no need to double escape it here |
d5112468 | 216 | |
0380cfa9 | 217 | set_pref($this->link, $key, $value, $_SESSION['uid'], false); |
d5112468 AD |
218 | |
219 | print json_encode(array("param" =>$key, "value" => $value)); | |
220 | } | |
221 | ||
222 | function mark() { | |
223 | $mark = $_REQUEST["mark"]; | |
224 | $id = db_escape_string($_REQUEST["id"]); | |
225 | ||
226 | if ($mark == "1") { | |
227 | $mark = "true"; | |
228 | } else { | |
229 | $mark = "false"; | |
230 | } | |
231 | ||
232 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET marked = $mark | |
233 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
234 | ||
235 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
236 | } | |
237 | ||
238 | function delete() { | |
239 | $ids = db_escape_string($_REQUEST["ids"]); | |
240 | ||
241 | $result = db_query($this->link, "DELETE FROM ttrss_user_entries | |
242 | WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]); | |
243 | ||
244 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
245 | } | |
246 | ||
247 | function unarchive() { | |
248 | $ids = db_escape_string($_REQUEST["ids"]); | |
249 | ||
250 | $result = db_query($this->link, "UPDATE ttrss_user_entries | |
251 | SET feed_id = orig_feed_id, orig_feed_id = NULL | |
252 | WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]); | |
253 | ||
254 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
255 | } | |
256 | ||
257 | function archive() { | |
258 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
259 | ||
260 | foreach ($ids as $id) { | |
261 | archive_article($this->link, $id, $_SESSION["uid"]); | |
262 | } | |
263 | ||
264 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
265 | } | |
266 | ||
267 | function publ() { | |
268 | $pub = $_REQUEST["pub"]; | |
269 | $id = db_escape_string($_REQUEST["id"]); | |
270 | $note = trim(strip_tags(db_escape_string($_REQUEST["note"]))); | |
271 | ||
272 | if ($pub == "1") { | |
273 | $pub = "true"; | |
274 | } else { | |
275 | $pub = "false"; | |
276 | } | |
277 | ||
278 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET | |
279 | published = $pub | |
280 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
281 | ||
282 | $pubsub_result = false; | |
283 | ||
284 | if (PUBSUBHUBBUB_HUB) { | |
285 | $rss_link = get_self_url_prefix() . | |
286 | "/public.php?op=rss&id=-2&key=" . | |
287 | get_feed_access_key($this->link, -2, false); | |
288 | ||
289 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
290 | ||
291 | $pubsub_result = $p->publish_update($rss_link); | |
292 | } | |
293 | ||
294 | print json_encode(array("message" => "UPDATE_COUNTERS", | |
295 | "pubsub_result" => $pubsub_result)); | |
296 | } | |
297 | ||
298 | function getAllCounters() { | |
299 | $last_article_id = (int) $_REQUEST["last_article_id"]; | |
300 | ||
301 | $reply = array(); | |
302 | ||
303 | if ($seq) $reply['seq'] = $seq; | |
304 | ||
305 | if ($last_article_id != getLastArticleId($this->link)) { | |
306 | $omode = $_REQUEST["omode"]; | |
46da73c2 | 307 | |
d5112468 AD |
308 | if ($omode != "T") |
309 | $reply['counters'] = getAllCounters($this->link, $omode); | |
310 | else | |
311 | $reply['counters'] = getGlobalCounters($this->link); | |
312 | } | |
313 | ||
314 | $reply['runtime-info'] = make_runtime_info($this->link); | |
315 | ||
316 | print json_encode($reply); | |
317 | } | |
318 | ||
319 | /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */ | |
320 | function catchupSelected() { | |
321 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
322 | $cmode = sprintf("%d", $_REQUEST["cmode"]); | |
323 | ||
324 | catchupArticlesById($this->link, $ids, $cmode); | |
325 | ||
326 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
327 | } | |
328 | ||
329 | function markSelected() { | |
330 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
331 | $cmode = sprintf("%d", $_REQUEST["cmode"]); | |
332 | ||
333 | markArticlesById($this->link, $ids, $cmode); | |
334 | ||
335 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
336 | } | |
337 | ||
338 | function publishSelected() { | |
339 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
340 | $cmode = sprintf("%d", $_REQUEST["cmode"]); | |
341 | ||
342 | publishArticlesById($this->link, $ids, $cmode); | |
343 | ||
344 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
345 | } | |
346 | ||
347 | function sanityCheck() { | |
348 | $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true"; | |
349 | ||
350 | $reply = array(); | |
351 | ||
352 | $reply['error'] = sanity_check($this->link); | |
353 | ||
354 | if ($reply['error']['code'] == 0) { | |
355 | $reply['init-params'] = make_init_params($this->link); | |
356 | $reply['runtime-info'] = make_runtime_info($this->link); | |
357 | } | |
358 | ||
359 | print json_encode($reply); | |
360 | } | |
361 | ||
362 | function setArticleTags() { | |
363 | global $memcache; | |
364 | ||
365 | $id = db_escape_string($_REQUEST["id"]); | |
366 | ||
367 | $tags_str = db_escape_string($_REQUEST["tags_str"]); | |
368 | $tags = array_unique(trim_array(explode(",", $tags_str))); | |
369 | ||
370 | db_query($this->link, "BEGIN"); | |
371 | ||
372 | $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries WHERE | |
373 | ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1"); | |
374 | ||
375 | if (db_num_rows($result) == 1) { | |
376 | ||
377 | $tags_to_cache = array(); | |
378 | ||
379 | $int_id = db_fetch_result($result, 0, "int_id"); | |
380 | ||
381 | db_query($this->link, "DELETE FROM ttrss_tags WHERE | |
382 | post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'"); | |
383 | ||
384 | foreach ($tags as $tag) { | |
385 | $tag = sanitize_tag($tag); | |
386 | ||
387 | if (!tag_is_valid($tag)) { | |
388 | continue; | |
389 | } | |
390 | ||
391 | if (preg_match("/^[0-9]*$/", $tag)) { | |
392 | continue; | |
393 | } | |
394 | ||
395 | // print "<!-- $id : $int_id : $tag -->"; | |
396 | ||
397 | if ($tag != '') { | |
398 | db_query($this->link, "INSERT INTO ttrss_tags | |
399 | (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')"); | |
400 | } | |
401 | ||
402 | array_push($tags_to_cache, $tag); | |
403 | } | |
404 | ||
405 | /* update tag cache */ | |
406 | ||
407 | sort($tags_to_cache); | |
408 | $tags_str = join(",", $tags_to_cache); | |
409 | ||
410 | db_query($this->link, "UPDATE ttrss_user_entries | |
411 | SET tag_cache = '$tags_str' WHERE ref_id = '$id' | |
412 | AND owner_uid = " . $_SESSION["uid"]); | |
413 | } | |
414 | ||
415 | db_query($this->link, "COMMIT"); | |
416 | ||
417 | if ($memcache) { | |
418 | $obj_id = md5("TAGS:".$_SESSION["uid"].":$id"); | |
419 | $memcache->delete($obj_id); | |
420 | } | |
421 | ||
422 | $tags = get_article_tags($this->link, $id); | |
423 | $tags_str = format_tags_string($tags, $id); | |
424 | $tags_str_full = join(", ", $tags); | |
425 | ||
426 | if (!$tags_str_full) $tags_str_full = __("no tags"); | |
427 | ||
428 | print json_encode(array("tags_str" => array("id" => $id, | |
429 | "content" => $tags_str, "content_full" => $tags_str_full))); | |
430 | } | |
431 | ||
432 | function regenOPMLKey() { | |
433 | update_feed_access_key($this->link, 'OPML:Publish', | |
434 | false, $_SESSION["uid"]); | |
435 | ||
436 | $new_link = opml_publish_url($this->link); | |
437 | ||
438 | print json_encode(array("link" => $new_link)); | |
439 | } | |
440 | ||
441 | function completeTags() { | |
442 | $search = db_escape_string($_REQUEST["search"]); | |
443 | ||
444 | $result = db_query($this->link, "SELECT DISTINCT tag_name FROM ttrss_tags | |
445 | WHERE owner_uid = '".$_SESSION["uid"]."' AND | |
446 | tag_name LIKE '$search%' ORDER BY tag_name | |
447 | LIMIT 10"); | |
448 | ||
449 | print "<ul>"; | |
450 | while ($line = db_fetch_assoc($result)) { | |
451 | print "<li>" . $line["tag_name"] . "</li>"; | |
452 | } | |
453 | print "</ul>"; | |
454 | } | |
455 | ||
456 | function purge() { | |
457 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
458 | $days = sprintf("%d", $_REQUEST["days"]); | |
459 | ||
460 | foreach ($ids as $id) { | |
461 | ||
462 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
463 | id = '$id' AND owner_uid = ".$_SESSION["uid"]); | |
464 | ||
465 | if (db_num_rows($result) == 1) { | |
466 | purge_feed($this->link, $id, $days); | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
471 | function getArticles() { | |
472 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
473 | $articles = array(); | |
474 | ||
475 | foreach ($ids as $id) { | |
476 | if ($id) { | |
477 | array_push($articles, format_article($this->link, $id, 0, false)); | |
478 | } | |
479 | } | |
480 | ||
481 | print json_encode($articles); | |
482 | } | |
483 | ||
484 | function checkDate() { | |
485 | $date = db_escape_string($_REQUEST["date"]); | |
486 | $date_parsed = strtotime($date); | |
487 | ||
488 | print json_encode(array("result" => (bool)$date_parsed, | |
489 | "date" => date("c", $date_parsed))); | |
490 | } | |
491 | ||
492 | function assigntolabel() { | |
4fdb8759 | 493 | return $this->labelops(true); |
d5112468 | 494 | } |
46da73c2 | 495 | |
d5112468 | 496 | function removefromlabel() { |
4fdb8759 | 497 | return $this->labelops(false); |
d5112468 | 498 | } |
46da73c2 | 499 | |
d5112468 AD |
500 | function labelops($assign) { |
501 | $reply = array(); | |
502 | ||
503 | $ids = explode(",", db_escape_string($_REQUEST["ids"])); | |
504 | $label_id = db_escape_string($_REQUEST["lid"]); | |
505 | ||
506 | $label = db_escape_string(label_find_caption($this->link, $label_id, | |
507 | $_SESSION["uid"])); | |
508 | ||
509 | $reply["info-for-headlines"] = array(); | |
510 | ||
511 | if ($label) { | |
512 | ||
513 | foreach ($ids as $id) { | |
514 | ||
515 | if ($assign) | |
516 | label_add_article($this->link, $id, $label, $_SESSION["uid"]); | |
517 | else | |
518 | label_remove_article($this->link, $id, $label, $_SESSION["uid"]); | |
519 | ||
520 | $labels = get_article_labels($this->link, $id, $_SESSION["uid"]); | |
521 | ||
522 | array_push($reply["info-for-headlines"], | |
523 | array("id" => $id, "labels" => format_article_labels($labels, $id))); | |
524 | ||
525 | } | |
526 | } | |
527 | ||
528 | $reply["message"] = "UPDATE_COUNTERS"; | |
529 | ||
530 | print json_encode($reply); | |
531 | } | |
532 | ||
533 | function updateFeedBrowser() { | |
534 | $search = db_escape_string($_REQUEST["search"]); | |
535 | $limit = db_escape_string($_REQUEST["limit"]); | |
536 | $mode = (int) db_escape_string($_REQUEST["mode"]); | |
537 | ||
538 | print json_encode(array("content" => | |
539 | make_feed_browser($this->link, $search, $limit, $mode), | |
540 | "mode" => $mode)); | |
541 | } | |
542 | ||
543 | // Silent | |
544 | function massSubscribe() { | |
545 | ||
546 | $payload = json_decode($_REQUEST["payload"], false); | |
547 | $mode = $_REQUEST["mode"]; | |
548 | ||
549 | if (!$payload || !is_array($payload)) return; | |
550 | ||
551 | if ($mode == 1) { | |
552 | foreach ($payload as $feed) { | |
553 | ||
554 | $title = db_escape_string($feed[0]); | |
555 | $feed_url = db_escape_string($feed[1]); | |
556 | ||
557 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
558 | feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]); | |
559 | ||
560 | if (db_num_rows($result) == 0) { | |
561 | $result = db_query($this->link, "INSERT INTO ttrss_feeds | |
562 | (owner_uid,feed_url,title,cat_id,site_url) | |
563 | VALUES ('".$_SESSION["uid"]."', | |
564 | '$feed_url', '$title', NULL, '')"); | |
565 | } | |
566 | } | |
567 | } else if ($mode == 2) { | |
568 | // feed archive | |
569 | foreach ($payload as $id) { | |
570 | $result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds | |
571 | WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
572 | ||
573 | if (db_num_rows($result) != 0) { | |
574 | $site_url = db_escape_string(db_fetch_result($result, 0, "site_url")); | |
575 | $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url")); | |
576 | $title = db_escape_string(db_fetch_result($result, 0, "title")); | |
577 | ||
578 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
579 | feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]); | |
580 | ||
581 | if (db_num_rows($result) == 0) { | |
582 | $result = db_query($this->link, "INSERT INTO ttrss_feeds | |
583 | (owner_uid,feed_url,title,cat_id,site_url) | |
584 | VALUES ('$id','".$_SESSION["uid"]."', | |
585 | '$feed_url', '$title', NULL, '$site_url')"); | |
586 | } | |
587 | } | |
588 | } | |
589 | } | |
590 | } | |
591 | ||
592 | function digestgetcontents() { | |
593 | $article_id = db_escape_string($_REQUEST['article_id']); | |
594 | ||
595 | $result = db_query($this->link, "SELECT content,title,link,marked,published | |
596 | FROM ttrss_entries, ttrss_user_entries | |
597 | WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']); | |
598 | ||
183ff16c | 599 | $content = sanitize($this->link, db_fetch_result($result, 0, "content")); |
d5112468 AD |
600 | $title = strip_tags(db_fetch_result($result, 0, "title")); |
601 | $article_url = htmlspecialchars(db_fetch_result($result, 0, "link")); | |
602 | $marked = sql_bool_to_bool(db_fetch_result($result, 0, "marked")); | |
603 | $published = sql_bool_to_bool(db_fetch_result($result, 0, "published")); | |
604 | ||
605 | print json_encode(array("article" => | |
606 | array("id" => $article_id, "url" => $article_url, | |
607 | "tags" => get_article_tags($this->link, $article_id), | |
608 | "marked" => $marked, "published" => $published, | |
609 | "title" => $title, "content" => $content))); | |
610 | } | |
611 | ||
612 | function digestupdate() { | |
613 | $feed_id = db_escape_string($_REQUEST['feed_id']); | |
614 | $offset = db_escape_string($_REQUEST['offset']); | |
615 | $seq = db_escape_string($_REQUEST['seq']); | |
616 | ||
617 | if (!$feed_id) $feed_id = -4; | |
618 | if (!$offset) $offset = 0; | |
619 | ||
620 | $reply = array(); | |
621 | ||
622 | $reply['seq'] = $seq; | |
623 | ||
624 | $headlines = api_get_headlines($this->link, $feed_id, 30, $offset, | |
625 | '', ($feed_id == -4), true, false, "unread", "updated DESC", 0, 0); | |
626 | ||
627 | $reply['headlines'] = array(); | |
628 | $reply['headlines']['title'] = getFeedTitle($this->link, $feed_id); | |
629 | $reply['headlines']['content'] = $headlines; | |
630 | ||
631 | print json_encode($reply); | |
632 | } | |
633 | ||
634 | function digestinit() { | |
635 | $tmp_feeds = api_get_feeds($this->link, -4, true, false, 0); | |
636 | ||
637 | $feeds = array(); | |
638 | ||
639 | foreach ($tmp_feeds as $f) { | |
640 | if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f); | |
641 | } | |
642 | ||
643 | print json_encode(array("feeds" => $feeds)); | |
644 | } | |
645 | ||
646 | function catchupFeed() { | |
647 | $feed_id = db_escape_string($_REQUEST['feed_id']); | |
648 | $is_cat = db_escape_string($_REQUEST['is_cat']) == "true"; | |
649 | ||
650 | catchup_feed($this->link, $feed_id, $is_cat); | |
651 | ||
652 | print json_encode(array("message" => "UPDATE_COUNTERS")); | |
653 | } | |
654 | ||
d5112468 AD |
655 | function quickAddCat() { |
656 | $cat = db_escape_string($_REQUEST["cat"]); | |
657 | ||
658 | add_feed_category($this->link, $cat); | |
659 | ||
660 | $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE | |
661 | title = '$cat' AND owner_uid = " . $_SESSION["uid"]); | |
662 | ||
663 | if (db_num_rows($result) == 1) { | |
664 | $id = db_fetch_result($result, 0, "id"); | |
665 | } else { | |
666 | $id = 0; | |
667 | } | |
668 | ||
669 | print_feed_cat_select($this->link, "cat_id", $id); | |
670 | } | |
671 | ||
672 | function regenFeedKey() { | |
673 | $feed_id = db_escape_string($_REQUEST['id']); | |
674 | $is_cat = db_escape_string($_REQUEST['is_cat']) == "true"; | |
675 | ||
676 | $new_key = update_feed_access_key($this->link, $feed_id, $is_cat); | |
677 | ||
678 | print json_encode(array("link" => $new_key)); | |
679 | } | |
680 | ||
681 | // Silent | |
682 | function clearKeys() { | |
683 | db_query($this->link, "DELETE FROM ttrss_access_keys WHERE | |
684 | owner_uid = " . $_SESSION["uid"]); | |
685 | } | |
686 | ||
687 | // Silent | |
688 | function clearArticleKeys() { | |
689 | db_query($this->link, "UPDATE ttrss_user_entries SET uuid = '' WHERE | |
690 | owner_uid = " . $_SESSION["uid"]); | |
691 | ||
692 | return; | |
693 | } | |
694 | ||
695 | ||
696 | function verifyRegexp() { | |
697 | $reg_exp = $_REQUEST["reg_exp"]; | |
698 | ||
699 | $status = @preg_match("/$reg_exp/i", "TEST") !== false; | |
700 | ||
701 | print json_encode(array("status" => $status)); | |
702 | } | |
703 | ||
704 | // TODO: unify with digest-get-contents? | |
705 | function cdmGetArticle() { | |
706 | $ids = array(db_escape_string($_REQUEST["id"])); | |
707 | $cids = explode(",", $_REQUEST["cids"]); | |
708 | ||
709 | $ids = array_merge($ids, $cids); | |
710 | ||
711 | $rv = array(); | |
712 | ||
713 | foreach ($ids as $id) { | |
714 | $id = (int)$id; | |
715 | ||
716 | $result = db_query($this->link, "SELECT content, | |
717 | ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds, | |
718 | ttrss_entries | |
719 | WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND | |
720 | ttrss_entries.id = ref_id AND | |
721 | ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
722 | ||
723 | if (db_num_rows($result) != 0) { | |
724 | $line = db_fetch_assoc($result); | |
725 | ||
183ff16c | 726 | $article_content = sanitize($this->link, $line["content"], |
d5112468 AD |
727 | false, false, $line['site_url']); |
728 | ||
729 | array_push($rv, | |
730 | array("id" => $id, "content" => $article_content)); | |
731 | } | |
732 | } | |
733 | ||
734 | print json_encode($rv); | |
735 | } | |
736 | ||
737 | function scheduleFeedUpdate() { | |
738 | $feed_id = db_escape_string($_REQUEST["id"]); | |
739 | $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true'; | |
740 | ||
741 | $message = __("Your request could not be completed."); | |
742 | ||
743 | if ($feed_id >= 0) { | |
744 | if (!$is_cat) { | |
745 | $message = __("Feed update has been scheduled."); | |
746 | ||
747 | db_query($this->link, "UPDATE ttrss_feeds SET | |
748 | last_update_started = '1970-01-01', | |
749 | last_updated = '1970-01-01' WHERE id = '$feed_id' AND | |
750 | owner_uid = ".$_SESSION["uid"]); | |
751 | ||
752 | } else { | |
753 | $message = __("Category update has been scheduled."); | |
754 | ||
755 | if ($feed_id) | |
756 | $cat_query = "cat_id = '$feed_id'"; | |
757 | else | |
758 | $cat_query = "cat_id IS NULL"; | |
759 | ||
760 | db_query($this->link, "UPDATE ttrss_feeds SET | |
761 | last_update_started = '1970-01-01', | |
762 | last_updated = '1970-01-01' WHERE $cat_query AND | |
763 | owner_uid = ".$_SESSION["uid"]); | |
764 | } | |
765 | } else { | |
766 | $message = __("Can't update this kind of feed."); | |
767 | } | |
768 | ||
769 | print json_encode(array("message" => $message)); | |
770 | return; | |
771 | } | |
772 | ||
f9ac31d6 AD |
773 | function buttonPlugin() { |
774 | $pclass = basename($_REQUEST['plugin']) . "_button"; | |
775 | $method = $_REQUEST['plugin_method']; | |
776 | ||
777 | if (class_exists($pclass)) { | |
778 | $plugin = new $pclass($this->link); | |
779 | if (method_exists($plugin, $method)) { | |
780 | return $plugin->$method(); | |
781 | } | |
d5112468 | 782 | } |
d5112468 AD |
783 | } |
784 | ||
d5112468 AD |
785 | function genHash() { |
786 | $hash = sha1(uniqid(rand(), true)); | |
787 | ||
788 | print json_encode(array("hash" => $hash)); | |
789 | } | |
33f0fdd0 AD |
790 | |
791 | function batchAddFeeds() { | |
792 | $cat_id = db_escape_string($_REQUEST['cat']); | |
793 | $feeds = explode("\n", db_escape_string($_REQUEST['feeds'])); | |
794 | $login = db_escape_string($_REQUEST['login']); | |
795 | $pass = db_escape_string($_REQUEST['pass']); | |
796 | $need_auth = db_escape_string($_REQUEST['need_auth']) != ""; | |
797 | ||
798 | $result = db_query($this->link, "SELECT twitter_oauth FROM ttrss_users | |
799 | WHERE id = ".$_SESSION['uid']); | |
800 | $has_oauth = db_fetch_result($result, 0, 'twitter_oauth') != ""; | |
801 | ||
802 | foreach ($feeds as $feed) { | |
803 | $feed = trim($feed); | |
804 | ||
805 | if (validate_feed_url($feed)) { | |
806 | ||
807 | db_query($this->link, "BEGIN"); | |
808 | ||
809 | if (!$need_auth || !$has_oauth || strpos($url, '://api.twitter.com') | |
810 | === false) { | |
811 | $update_method = 0; | |
812 | } else { | |
813 | $update_method = 3; | |
814 | } | |
815 | ||
816 | if ($cat_id == "0" || !$cat_id) { | |
817 | $cat_qpart = "NULL"; | |
818 | } else { | |
819 | $cat_qpart = "'$cat_id'"; | |
820 | } | |
821 | ||
822 | $result = db_query($this->link, | |
823 | "SELECT id FROM ttrss_feeds | |
824 | WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]); | |
825 | ||
826 | if (db_num_rows($result) == 0) { | |
827 | $result = db_query($this->link, | |
828 | "INSERT INTO ttrss_feeds | |
829 | (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method) | |
830 | VALUES ('".$_SESSION["uid"]."', '$feed', | |
831 | '[Unknown]', $cat_qpart, '$login', '$pass', '$update_method')"); | |
832 | } | |
833 | ||
834 | db_query($this->link, "COMMIT"); | |
835 | } | |
836 | } | |
837 | } | |
838 | ||
d5112468 AD |
839 | } |
840 | ?> |