]> git.wh0rd.org - tt-rss.git/blame - modules/backend-rpc.php
rpc/addfeed: use JSON
[tt-rss.git] / modules / backend-rpc.php
CommitLineData
1d3a17c7 1<?php
01b3e191
AD
2 function handle_rpc_request($link) {
3
b4e75b2a 4 $subop = $_REQUEST["subop"];
6237ea05 5 $seq = (int) $_REQUEST["seq"];
01b3e191 6
d9084cf2
AD
7 if ($subop == "setprofile") {
8 $id = db_escape_string($_REQUEST["id"]);
9
10 $_SESSION["profile"] = $id;
11 $_SESSION["prefs_cache"] = array();
12 return;
13 }
14
15 if ($subop == "remprofiles") {
16 $ids = split(",", db_escape_string(trim($_REQUEST["ids"])));
17
18 foreach ($ids as $id) {
19 if ($_SESSION["profile"] != $id) {
20 db_query($link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
21 owner_uid = " . $_SESSION["uid"]);
22 }
23 }
24 return;
25 }
26
27 if ($subop == "addprofile") {
28 $title = db_escape_string(trim($_REQUEST["title"]));
29 if ($title) {
30 db_query($link, "BEGIN");
31
32 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
33 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
34
35 if (db_num_rows($result) == 0) {
36
37 db_query($link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
38 VALUES ('$title', ".$_SESSION["uid"] .")");
39
40 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles WHERE
41 title = '$title'");
42
43 if (db_num_rows($result) != 0) {
44 $profile_id = db_fetch_result($result, 0, "id");
45
46 if ($profile_id) {
47 initialize_user_prefs($link, $_SESSION["uid"], $profile_id);
48 }
49 }
50 }
51
52 db_query($link, "COMMIT");
53 }
54 return;
55 }
56
57 if ($subop == "saveprofile") {
58 $id = db_escape_string($_REQUEST["id"]);
59 $title = db_escape_string(trim($_REQUEST["value"]));
60
61 if ($id == 0) {
62 print __("Default profile");
63 return;
64 }
65
66 if ($title) {
67 db_query($link, "BEGIN");
68
69 $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
70 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
71
72 if (db_num_rows($result) == 0) {
73 db_query($link, "UPDATE ttrss_settings_profiles
74 SET title = '$title' WHERE id = '$id' AND
75 owner_uid = " . $_SESSION["uid"]);
76 print $title;
77 } else {
78 $result = db_query($link, "SELECT title FROM ttrss_settings_profiles
79 WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
80 print db_fetch_result($result, 0, "title");
81 }
82
83 db_query($link, "COMMIT");
84 }
85 return;
86 }
87
ef88b1cc
AD
88 if ($subop == "remarchive") {
89 $ids = split(",", db_escape_string($_REQUEST["ids"]));
90
ef88b1cc
AD
91 foreach ($ids as $id) {
92 $result = db_query($link, "DELETE FROM ttrss_archived_feeds WHERE
93 (SELECT COUNT(*) FROM ttrss_user_entries
94 WHERE orig_feed_id = '$id') = 0 AND
95 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
96
97 $rc = db_affected_rows($link, $result);
ef88b1cc
AD
98 }
99
ef88b1cc
AD
100 return;
101 }
102
a5819bb3 103 if ($subop == "addfeed") {
e9175d13 104 header("Content-Type: text/plain");
a5819bb3
AD
105
106 $feed = db_escape_string($_REQUEST['feed']);
107 $cat = db_escape_string($_REQUEST['cat']);
108 $login = db_escape_string($_REQUEST['login']);
109 $pass = db_escape_string($_REQUEST['pass']);
110
111 $rc = subscribe_to_feed($link, $feed, $cat, $login, $pass);
112
e9175d13 113 print json_encode(array("result" => $rc));
a5819bb3
AD
114
115 return;
116
117 }
118
f0266f51 119 if ($subop == "extractfeedurls") {
e9175d13 120 header("Content-Type: text/plain");
f0266f51
CW
121
122 $urls = get_feeds_from_html($_REQUEST['url']);
f0266f51 123
e9175d13 124 print json_encode(array("urls" => $urls));
f0266f51
CW
125 return;
126 }
127
8a3e0b1a 128 if ($subop == "togglepref") {
74d12bab 129 header("Content-Type: text/plain");
8a3e0b1a
AD
130
131 $key = db_escape_string($_REQUEST["key"]);
8a3e0b1a 132 set_pref($link, $key, !get_pref($link, $key));
8a3e0b1a 133 $value = get_pref($link, $key);
01b3e191 134
74d12bab 135 print json_encode(array("param" =>$key, "value" => $value));
8a3e0b1a
AD
136 return;
137 }
138
139 if ($subop == "setpref") {
01b3e191
AD
140 print "<rpc-reply>";
141
b4e75b2a
AD
142 $key = db_escape_string($_REQUEST["key"]);
143 $value = db_escape_string($_REQUEST["value"]);
01b3e191
AD
144
145 set_pref($link, $key, $value);
146
147 print "<param-set key=\"$key\" value=\"$value\"/>";
148
149 print "</rpc-reply>";
150
85bd574b 151 return;
01b3e191
AD
152 }
153
01b3e191 154 if ($subop == "mark") {
74d12bab
AD
155 header("Content-Type: text/plain");
156
b4e75b2a
AD
157 $mark = $_REQUEST["mark"];
158 $id = db_escape_string($_REQUEST["id"]);
01b3e191
AD
159
160 if ($mark == "1") {
161 $mark = "true";
162 } else {
163 $mark = "false";
164 }
165
166 // FIXME this needs collision testing
167
168 $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
169 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
49aa6de9 170
74d12bab 171 print json_encode(array("message" => "UPDATE_COUNTERS"));
85bd574b 172 return;
01b3e191
AD
173 }
174
e04c18a2 175 if ($subop == "delete") {
74d12bab
AD
176 header("Content-Type: text/plain");
177
b4e75b2a 178 $ids = db_escape_string($_REQUEST["ids"]);
e04c18a2
AD
179
180 $result = db_query($link, "DELETE FROM ttrss_user_entries
181 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
182
74d12bab 183 print json_encode(array("message" => "UPDATE_COUNTERS"));
e04c18a2
AD
184 return;
185 }
186
187 if ($subop == "unarchive") {
74d12bab
AD
188 header("Content-Type: text/plain");
189
b4e75b2a 190 $ids = db_escape_string($_REQUEST["ids"]);
e04c18a2
AD
191
192 $result = db_query($link, "UPDATE ttrss_user_entries
ef83538d 193 SET feed_id = orig_feed_id, orig_feed_id = NULL
e04c18a2
AD
194 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
195
74d12bab 196 print json_encode(array("message" => "UPDATE_COUNTERS"));
e04c18a2
AD
197 return;
198 }
199
200 if ($subop == "archive") {
74d12bab
AD
201 header("Content-Type: text/plain");
202
b4e75b2a 203 $ids = split(",", db_escape_string($_REQUEST["ids"]));
e04c18a2 204
16fdac16
AD
205 foreach ($ids as $id) {
206 archive_article($link, $id, $_SESSION["uid"]);
207 }
e04c18a2 208
74d12bab 209 print json_encode(array("message" => "UPDATE_COUNTERS"));
e04c18a2
AD
210 return;
211 }
212
213
e4f4b46f 214 if ($subop == "publ") {
c7e51de1
AD
215 $pub = $_REQUEST["pub"];
216 $id = db_escape_string($_REQUEST["id"]);
217 $note = trim(strip_tags(db_escape_string($_REQUEST["note"])));
e4f4b46f
AD
218
219 if ($pub == "1") {
0a8011eb 220 $pub = "true";
e4f4b46f
AD
221 } else {
222 $pub = "false";
223 }
224
c7e51de1
AD
225 if ($note != 'undefined') {
226 $note_qpart = "note = '$note',";
227 }
228
e4f4b46f
AD
229 // FIXME this needs collision testing
230
c7e51de1
AD
231 $result = db_query($link, "UPDATE ttrss_user_entries SET
232 $note_qpart
233 published = $pub
e4f4b46f 234 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
49aa6de9 235
c7e51de1
AD
236
237 print "<rpc-reply>";
238
c7e51de1
AD
239 if ($note != 'undefined') {
240 $note_size = strlen($note);
241 print "<note id=\"$id\" size=\"$note_size\">";
242 print "<![CDATA[" . format_article_note($id, $note) . "]]>";
243 print "</note>";
244 }
245
f8fb4498
AD
246 print "<message>UPDATE_COUNTERS</message>";
247
c7e51de1 248 print "</rpc-reply>";
49aa6de9 249
85bd574b 250 return;
e4f4b46f
AD
251 }
252
74d12bab 253/* if ($subop == "updateFeed") {
b4e75b2a 254 $feed_id = db_escape_string($_REQUEST["feed"]);
01b3e191 255
c633e370 256 update_rss_feed($link, $feed_id);
01b3e191 257
f8fb4498
AD
258 print "<rpc-reply>";
259 print "<message>UPDATE_COUNTERS</message>";
01b3e191 260 print "</rpc-reply>";
f8fb4498 261
01b3e191 262 return;
74d12bab 263 } */
01b3e191 264
773adf8b 265 if ($subop == "updateAllFeeds" || $subop == "getAllCounters") {
f8fb4498 266
563b9c78
AD
267 header("Content-Type: text/plain");
268
f8fb4498 269 $last_article_id = (int) $_REQUEST["last_article_id"];
01b3e191 270
563b9c78 271 $reply = array();
01b3e191 272
563b9c78 273 if ($seq) $reply['seq'] = $seq;
6237ea05 274
f8fb4498 275 if ($last_article_id != getLastArticleId($link)) {
6a7817c1 276 $omode = $_REQUEST["omode"];
f8fb4498
AD
277
278 if ($omode != "T")
563b9c78 279 $reply['counters'] = getAllCounters($link, $omode);
f8fb4498 280 else
563b9c78 281 $reply['counters'] = getGlobalCounters($link);
a06d0e5a
AD
282 }
283
563b9c78 284 $reply['runtime-info'] = make_runtime_info($link);
f54f515f 285
01b3e191 286
563b9c78 287 print json_encode($reply);
85bd574b 288 return;
01b3e191 289 }
472782e8 290
01b3e191
AD
291 /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
292 if ($subop == "catchupSelected") {
74d12bab 293 header("Content-Type: text/plain");
01b3e191 294
2855ee88
AD
295 $ids = split(",", db_escape_string($_REQUEST["ids"]));
296 $cmode = sprintf("%d", $_REQUEST["cmode"]);
01b3e191 297
472782e8 298 catchupArticlesById($link, $ids, $cmode);
01b3e191 299
74d12bab 300 print json_encode(array("message" => "UPDATE_COUNTERS"));
85bd574b 301 return;
01b3e191
AD
302 }
303
304 if ($subop == "markSelected") {
74d12bab 305 header("Content-Type: text/plain");
01b3e191 306
b4e75b2a
AD
307 $ids = split(",", db_escape_string($_REQUEST["ids"]));
308 $cmode = sprintf("%d", $_REQUEST["cmode"]);
01b3e191 309
18eddb2c
AD
310 markArticlesById($link, $ids, $cmode);
311
74d12bab 312 print json_encode(array("message" => "UPDATE_COUNTERS"));
85bd574b 313 return;
01b3e191
AD
314 }
315
e4f4b46f 316 if ($subop == "publishSelected") {
74d12bab 317 header("Content-Type: text/plain");
e4f4b46f 318
b4e75b2a
AD
319 $ids = split(",", db_escape_string($_REQUEST["ids"]));
320 $cmode = sprintf("%d", $_REQUEST["cmode"]);
e4f4b46f
AD
321
322 publishArticlesById($link, $ids, $cmode);
323
74d12bab 324 print json_encode(array("message" => "UPDATE_COUNTERS"));
85bd574b 325 return;
e4f4b46f
AD
326 }
327
01b3e191 328 if ($subop == "sanityCheck") {
3ac2b520 329 print "<rpc-reply>";
01b3e191
AD
330 if (sanity_check($link)) {
331 print "<error error-code=\"0\"/>";
d8221301
AD
332
333 print "<init-params><![CDATA[";
334 print json_encode(make_init_params($link));
335 print "]]></init-params>";
336
f54f515f 337 print_runtime_info($link);
4220d6b0
AD
338
339 # assign client-passed params to session
b4e75b2a 340 $_SESSION["client.userAgent"] = $_REQUEST["ua"];
4220d6b0 341
01b3e191 342 }
3ac2b520 343 print "</rpc-reply>";
85bd574b
AD
344
345 return;
3ac2b520 346 }
01b3e191
AD
347
348 if ($subop == "globalPurge") {
349
350 print "<rpc-reply>";
351 global_purge_old_posts($link, true);
352 print "</rpc-reply>";
353
85bd574b 354 return;
01b3e191 355 }
3ac2b520 356
0b126ac2 357 if ($subop == "setArticleTags") {
ddcbbea2 358 header("Content-Type: text/plain");
14b6c54b 359
bd3f2ade
AD
360 global $memcache;
361
b4e75b2a 362 $id = db_escape_string($_REQUEST["id"]);
14b6c54b 363
b4e75b2a 364 $tags_str = db_escape_string($_REQUEST["tags_str"]);
d62a3b63 365 $tags = array_unique(trim_array(split(",", $tags_str)));
0b126ac2
AD
366
367 db_query($link, "BEGIN");
368
369 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries WHERE
370 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
371
372 if (db_num_rows($result) == 1) {
373
779560b7
AD
374 $tags_to_cache = array();
375
0b126ac2
AD
376 $int_id = db_fetch_result($result, 0, "int_id");
377
378 db_query($link, "DELETE FROM ttrss_tags WHERE
379 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
380
381 foreach ($tags as $tag) {
14b6c54b 382 $tag = sanitize_tag($tag);
0b126ac2 383
ef063748
AD
384 if (!tag_is_valid($tag)) {
385 continue;
386 }
387
0b126ac2
AD
388 if (preg_match("/^[0-9]*$/", $tag)) {
389 continue;
390 }
14b6c54b 391
307d187c 392// print "<!-- $id : $int_id : $tag -->";
0b126ac2
AD
393
394 if ($tag != '') {
395 db_query($link, "INSERT INTO ttrss_tags
396 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
397 }
779560b7
AD
398
399 array_push($tags_to_cache, $tag);
0b126ac2 400 }
0b126ac2 401
779560b7
AD
402 /* update tag cache */
403
404 $tags_str = join(",", $tags_to_cache);
405
406 db_query($link, "UPDATE ttrss_user_entries
407 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
408 AND owner_uid = " . $_SESSION["uid"]);
409 }
490c366d 410
0b126ac2
AD
411 db_query($link, "COMMIT");
412
bd3f2ade
AD
413 if ($memcache) {
414 $obj_id = md5("TAGS:".$_SESSION["uid"].":$id");
415 $memcache->delete($obj_id);
416 }
417
307d187c
AD
418 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
419
ddcbbea2
AD
420 print json_encode(array("tags_str" => array("id" => $id,
421 "content" => $tags_str)));
0b126ac2 422
85bd574b 423 return;
0b126ac2 424 }
01a87dff 425
ef7b7bbd
MK
426 if ($subop == "regenOPMLKey") {
427
428 print "<rpc-reply>";
2e7f046f
AD
429
430 update_feed_access_key($link, 'OPML:Publish',
431 false, $_SESSION["uid"]);
432
ef7b7bbd
MK
433 $new_link = opml_publish_url($link);
434 print "<link><![CDATA[$new_link]]></link>";
435 print "</rpc-reply>";
436 return;
437 }
438
01a87dff
AD
439 if ($subop == "logout") {
440 logout_user();
441 print_error_xml(6);
85bd574b 442 return;
01a87dff
AD
443 }
444
05fcdf52 445 if ($subop == "completeTags") {
74d12bab 446 header("Content-Type: text/plain");
05fcdf52
AD
447
448 $search = db_escape_string($_REQUEST["search"]);
449
450 $result = db_query($link, "SELECT DISTINCT tag_name FROM ttrss_tags
451 WHERE owner_uid = '".$_SESSION["uid"]."' AND
452 tag_name LIKE '$search%' ORDER BY tag_name
453 LIMIT 10");
454
455 print "<ul>";
456 while ($line = db_fetch_assoc($result)) {
457 print "<li>" . $line["tag_name"] . "</li>";
458 }
459 print "</ul>";
460
85bd574b 461 return;
05fcdf52
AD
462 }
463
81cd6cac 464 if ($subop == "purge") {
b4e75b2a
AD
465 $ids = split(",", db_escape_string($_REQUEST["ids"]));
466 $days = sprintf("%d", $_REQUEST["days"]);
81cd6cac 467
81cd6cac
AD
468 foreach ($ids as $id) {
469
470 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
471 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
472
473 if (db_num_rows($result) == 1) {
74d12bab 474 purge_feed($link, $id, $days);
81cd6cac
AD
475 }
476 }
477
81cd6cac
AD
478 return;
479 }
480
9bf3f101 481/* if ($subop == "setScore") {
546499a9
AD
482 $id = db_escape_string($_REQUEST["id"]);
483 $score = sprintf("%d", $_REQUEST["score"]);
484
485 $result = db_query($link, "UPDATE ttrss_user_entries SET score = '$score'
486 WHERE ref_id = '$id' AND owner_uid = ".$_SESSION["uid"]);
487
488 print "<rpc-reply><message>Acknowledged.</message></rpc-reply>";
489
490 return;
491
9bf3f101 492 } */
546499a9 493
aa0fa9df
AD
494 if ($subop == "getArticles") {
495 $ids = split(",", db_escape_string($_REQUEST["ids"]));
496
497 print "<rpc-reply>";
498
499 foreach ($ids as $id) {
500 if ($id) {
501 outputArticleXML($link, $id, 0, false);
502 }
503 }
504 print "</rpc-reply>";
505
506 return;
507 }
508
d0da85c2
AD
509 if ($subop == "checkDate") {
510
511 $date = db_escape_string($_REQUEST["date"]);
512 $date_parsed = strtotime($date);
513
514 print "<rpc-reply>";
515
516 if ($date_parsed) {
517 print "<result>1</result>";
518 } else {
519 print "<result>0</result>";
520 }
521
522 print "</rpc-reply>";
523
524 return;
525 }
526
8eb592ec
AD
527 if ($subop == "assignToLabel" || $subop == "removeFromLabel") {
528 header("Content-Type: text/plain");
933ba4ee 529
8eb592ec 530 $reply = array();
b8a637f3
AD
531
532 $ids = split(",", db_escape_string($_REQUEST["ids"]));
533 $label_id = db_escape_string($_REQUEST["lid"]);
534
7a13338b
AD
535 $label = db_escape_string(label_find_caption($link, $label_id,
536 $_SESSION["uid"]));
b8a637f3 537
8eb592ec 538 $reply["info-for-headlines"] = array();
f9247195 539
b8a637f3
AD
540 if ($label) {
541
542 foreach ($ids as $id) {
f9247195 543
8eb592ec
AD
544 if ($subop == "assignToLabel")
545 label_add_article($link, $id, $label, $_SESSION["uid"]);
546 else
547 label_remove_article($link, $id, $label, $_SESSION["uid"]);
f9247195
AD
548
549 $labels = get_article_labels($link, $id, $_SESSION["uid"]);
8eb592ec
AD
550
551 array_push($reply["info-for-headlines"],
552 array("id" => $id, "labels" => format_article_labels($labels, $id)));
f9247195 553
b8a637f3
AD
554 }
555 }
556
8eb592ec 557 $reply["message"] = "UPDATE_COUNTERS";
f9247195 558
8eb592ec 559 print json_encode($reply);
b8a637f3
AD
560
561 return;
562 }
563
ef88b1cc 564 if ($subop == "updateFeedBrowser") {
c2913898
AD
565
566 $search = db_escape_string($_REQUEST["search"]);
567 $limit = db_escape_string($_REQUEST["limit"]);
082ae95b 568 $mode = db_escape_string($_REQUEST["mode"]);
c2913898
AD
569
570 print "<rpc-reply>";
571 print "<content>";
572 print "<![CDATA[";
082ae95b 573 $ctr = print_feed_browser($link, $search, $limit, $mode);
c2913898
AD
574 print "]]>";
575 print "</content>";
576 print "<num-results value=\"$ctr\"/>";
ef88b1cc 577 print "<mode value=\"$mode\"/>";
c2913898
AD
578 print "</rpc-reply>";
579
580 return;
581 }
582
ef88b1cc
AD
583
584 if ($subop == "massSubscribe") {
585
586 $ids = split(",", db_escape_string($_REQUEST["ids"]));
587 $mode = $_REQUEST["mode"];
588
589 $subscribed = array();
590
591 foreach ($ids as $id) {
592
593 if ($mode == 1) {
594 $result = db_query($link, "SELECT feed_url,title FROM ttrss_feeds
595 WHERE id = '$id'");
596 } else if ($mode == 2) {
597 $result = db_query($link, "SELECT * FROM ttrss_archived_feeds
598 WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
599 $orig_id = db_escape_string(db_fetch_result($result, 0, "id"));
600 $site_url = db_escape_string(db_fetch_result($result, 0, "site_url"));
601 }
602
603 $feed_url = db_escape_string(db_fetch_result($result, 0, "feed_url"));
604 $title = db_escape_string(db_fetch_result($result, 0, "title"));
605
606 $title_orig = db_fetch_result($result, 0, "title");
607
608 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
609 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
610
611 if (db_num_rows($result) == 0) {
612 if ($mode == 1) {
613 $result = db_query($link,
614 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
615 VALUES ('".$_SESSION["uid"]."', '$feed_url', '$title', NULL)");
616 } else if ($mode == 2) {
617 $result = db_query($link,
618 "INSERT INTO ttrss_feeds (id,owner_uid,feed_url,title,cat_id,site_url)
619 VALUES ('$orig_id','".$_SESSION["uid"]."', '$feed_url', '$title', NULL, '$site_url')");
620 }
621 array_push($subscribed, $title_orig);
622 }
623 }
624
625 $num_feeds = count($subscribed);
626
627 print "<rpc-reply>";
628 print "<num-feeds value='$num_feeds'/>";
629 print "</rpc-reply>";
630
631 return;
632 }
633
d8ea9902
AD
634 if ($subop == "digest-get-contents") {
635 $article_id = db_escape_string($_REQUEST['article_id']);
636
637 $result = db_query($link, "SELECT content
638 FROM ttrss_entries, ttrss_user_entries
639 WHERE id = '$article_id' AND ref_id = id AND owner_uid = ".$_SESSION['uid']);
640
641 print "<rpc-reply>";
642
643 print "<article id=\"$article_id\"><![CDATA[";
644
645 $content = sanitize_rss($link, db_fetch_result($result, 0, "content"));
646
647 print $content;
648
649 print "]]></article>";
650
651 print "</rpc-reply>";
652
653 return;
654 }
655
1ca8997b 656 if ($subop == "digest-update") {
126cb765
AD
657 header("Content-Type: text/plain");
658
b41c2549 659 $feed_id = db_escape_string($_REQUEST['feed_id']);
1ca8997b 660 $offset = db_escape_string($_REQUEST['offset']);
e4c530dc 661 $seq = db_escape_string($_REQUEST['seq']);
1ca8997b 662
b41c2549 663 if (!$feed_id) $feed_id = -4;
1ca8997b 664 if (!$offset) $offset = 0;
1ca8997b 665
126cb765
AD
666 $reply = array();
667
668 $reply['seq'] = $seq;
e4c530dc 669
1ca8997b 670 $headlines = api_get_headlines($link, $feed_id, 10, $offset,
d8ea9902 671 '', ($feed_id == -4), true, false, "unread", "updated DESC");
1ca8997b
AD
672
673 //function api_get_headlines($link, $feed_id, $limit, $offset,
674 // $filter, $is_cat, $show_excerpt, $show_content, $view_mode) {
675
126cb765
AD
676 $reply['headlines'] = array();
677 $reply['headlines']['title'] = getFeedTitle($link, $feed_id);
678 $reply['headlines']['content'] = $headlines;
1ca8997b 679
126cb765 680 print json_encode($reply);
1ca8997b
AD
681 return;
682 }
683
684 if ($subop == "digest-init") {
126cb765
AD
685 header("Content-Type: text/plain");
686
a17d7219 687 $tmp_feeds = api_get_feeds($link, -3, true, false, 0);
911d4c08 688
911d4c08
AD
689 $feeds = array();
690
691 foreach ($tmp_feeds as $f) {
b41c2549 692 if ($f['id'] > 0 || $f['id'] == -4) array_push($feeds, $f);
911d4c08
AD
693 }
694
126cb765 695 print json_encode(array("feeds" => $feeds));
911d4c08 696
911d4c08
AD
697 return;
698 }
699
c1b5cd23
AD
700 if ($subop == "catchupFeed") {
701
702 $feed_id = db_escape_string($_REQUEST['feed_id']);
703 $is_cat = db_escape_string($_REQUEST['is_cat']);
704
705 print "<rpc-reply>";
706
707 catchup_feed($link, $feed_id, $is_cat);
708
709 print "</rpc-reply>";
710
711 return;
712 }
713
31a53903
AD
714 if ($subop == "sendEmail") {
715 $secretkey = $_REQUEST['secretkey'];
716
717 print "<rpc-reply>";
718
719 if (DIGEST_ENABLE && $_SESSION['email_secretkey'] &&
720 $secretkey == $_SESSION['email_secretkey']) {
721
722 $_SESSION['email_secretkey'] = '';
723
724 $destination = $_REQUEST['destination'];
725 $subject = $_REQUEST['subject'];
726 $content = $_REQUEST['content'];
727
728 $replyto = strip_tags($_SESSION['email_replyto']);
729 $fromname = strip_tags($_SESSION['email_fromname']);
730
731 $mail = new PHPMailer();
732
733 $mail->PluginDir = "lib/phpmailer/";
734 $mail->SetLanguage("en", "lib/phpmailer/language/");
735
736 $mail->CharSet = "UTF-8";
737
738 $mail->From = $replyto;
739 $mail->FromName = $fromname;
740 $mail->AddAddress($destination);
741
742 if (DIGEST_SMTP_HOST) {
743 $mail->Host = DIGEST_SMTP_HOST;
744 $mail->Mailer = "smtp";
745 $mail->SMTPAuth = DIGEST_SMTP_LOGIN != '';
746 $mail->Username = DIGEST_SMTP_LOGIN;
747 $mail->Password = DIGEST_SMTP_PASSWORD;
748 }
749
750 $mail->IsHTML(false);
751 $mail->Subject = $subject;
752 $mail->Body = $content;
753
754 $rc = $mail->Send();
755
756 if (!$rc) {
757 print "<error><![CDATA[" . $mail->ErrorInfo . "]]></error>";
758 } else {
759 save_email_address($link, db_escape_string($destination));
f8fb4498 760 print "<message>UPDATE_COUNTERS</message>";
31a53903
AD
761 }
762
763 } else {
764 print "<error>Not authorized.</error>";
765 }
766
767 print "</rpc-reply>";
768
769 return;
770 }
771
772 if ($subop == "completeEmails") {
773
774 $search = db_escape_string($_REQUEST["search"]);
775
776 print "<ul>";
777
778 foreach ($_SESSION['stored_emails'] as $email) {
779 if (strpos($email, $search) !== false) {
780 print "<li>$email</li>";
781 }
782 }
783
784 print "</ul>";
785
786 return;
787 }
788
5c7c7da9
AD
789 if ($subop == "quickAddCat") {
790 print "<rpc-reply>";
791
792 $cat = db_escape_string($_REQUEST["cat"]);
793
794 add_feed_category($link, $cat);
795
796 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
797 title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
798
799 if (db_num_rows($result) == 1) {
800 $id = db_fetch_result($result, 0, "id");
801 } else {
802 $id = 0;
803 }
804
5c7c7da9 805 print_feed_cat_select($link, "cat_id", $id);
5c7c7da9
AD
806
807 print "</rpc-reply>";
808
809 return;
810 }
811
8801fb01
AD
812 if ($subop == "regenFeedKey") {
813 $feed_id = db_escape_string($_REQUEST['id']);
814 $is_cat = (bool) db_escape_string($_REQUEST['is_cat']);
815
816 print "<rpc-reply>";
817
818 $new_key = update_feed_access_key($link, $feed_id, $is_cat);
819
820 print "<link><![CDATA[$new_key]]></link>";
821
822 print "</rpc-reply>";
823
824 return;
825 }
826
8d86f15f
AD
827 if ($subop == "clearKeys") {
828
829 db_query($link, "DELETE FROM ttrss_access_keys WHERE
830 owner_uid = " . $_SESSION["uid"]);
831
f8fb4498
AD
832 print "<rpc-reply>";
833 print "<message>UPDATE_COUNTERS</message>";
834 print "</rpc-reply>";
8d86f15f
AD
835
836 return;
837 }
838
f705f206
AD
839 if ($subop == "verifyRegexp") {
840 $reg_exp = $_REQUEST["reg_exp"];
841
842 print "<rpc-reply><status>";
843
844 if (@preg_match("/$reg_exp/i", "TEST") === false) {
845 print "INVALID";
846 } else {
847 print "OK";
848 }
849
850 print "</status></rpc-reply>";
851
852 return;
853 }
854
dd1c0680 855 if ($subop == "cdmGetArticle") {
74d12bab
AD
856 header("Content-Type: text/plain");
857
dd1c0680
AD
858 $id = db_escape_string($_REQUEST["id"]);
859
860 $result = db_query($link, "SELECT content,
861 ttrss_feeds.site_url AS site_url FROM ttrss_user_entries, ttrss_feeds,
862 ttrss_entries
863 WHERE feed_id = ttrss_feeds.id AND ref_id = '$id' AND
864 ttrss_entries.id = ref_id AND
865 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
866
867 if (db_num_rows($result) != 0) {
868 $line = db_fetch_assoc($result);
869
870 $article_content = sanitize_rss($link, $line["content"],
871 false, false, $line['site_url']);
872
873 } else {
874 $article_content = '';
875 }
876
74d12bab
AD
877 print json_encode(array("article" =>
878 array("id" => $id, "content" => $article_content)));
dd1c0680
AD
879
880 return;
881 }
882
428b704d 883 if ($subop == "scheduleFeedUpdate") {
74d12bab
AD
884 header("Content-Type: text/plain");
885
428b704d 886 $feed_id = db_escape_string($_REQUEST["id"]);
997429c2 887 $is_cat = db_escape_string($_REQUEST['is_cat']) == 'true';
428b704d
AD
888
889 $message = __("Your request could not be completed.");
890
891 if ($feed_id >= 0) {
892 if (!$is_cat) {
893 $message = __("Feed update has been scheduled.");
894
895 db_query($link, "UPDATE ttrss_feeds SET
896 last_update_started = '1970-01-01',
897 last_updated = '1970-01-01' WHERE id = '$feed_id' AND
898 owner_uid = ".$_SESSION["uid"]);
899
900 } else {
901 $message = __("Category update has been scheduled.");
902
903 if ($feed_id)
904 $cat_query = "cat_id = '$feed_id'";
905 else
906 $cat_query = "cat_id IS NULL";
907
908 db_query($link, "UPDATE ttrss_feeds SET
909 last_update_started = '1970-01-01',
910 last_updated = '1970-01-01' WHERE $cat_query AND
911 owner_uid = ".$_SESSION["uid"]);
912 }
913 } else {
914 $message = __("Can't update this kind of feed.");
915 }
916
74d12bab 917 print json_encode(array("message" => $message));
428b704d
AD
918 return;
919 }
920
ba7e88e5 921 if ($subop == "getTweetInfo") {
563b9c78 922 header("Content-Type: text/plain");
ba7e88e5
AD
923 $id = db_escape_string($_REQUEST['id']);
924
925 $result = db_query($link, "SELECT title, link
926 FROM ttrss_entries, ttrss_user_entries
927 WHERE id = '$id' AND ref_id = id AND owner_uid = " .$_SESSION['uid']);
928
929 if (db_num_rows($result) != 0) {
930 $title = truncate_string(strip_tags(db_fetch_result($result, 0, 'title')),
931 100, '...');
932 $article_link = db_fetch_result($result, 0, 'link');
933 }
934
935 print json_encode(array("title" => $title, "link" => $article_link,
936 "id" => $id));
937
938 return;
939 }
940
85bd574b 941 print "<rpc-reply><error>Unknown method: $subop</error></rpc-reply>";
01b3e191
AD
942 }
943?>