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