]> git.wh0rd.org - tt-rss.git/blame - backend.php
rpc: no longer use XML
[tt-rss.git] / backend.php
CommitLineData
1d3a17c7 1<?php
ce885e21
AD
2 /* remove ill effects of magic quotes */
3
5fa17372 4 if (get_magic_quotes_gpc()) {
c0793f64 5 function stripslashes_deep($value) {
009646d2 6 $value = is_array($value) ?
c0793f64
AD
7 array_map('stripslashes_deep', $value) : stripslashes($value);
8 return $value;
9 }
10
11 $_POST = array_map('stripslashes_deep', $_POST);
12 $_GET = array_map('stripslashes_deep', $_GET);
13 $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
14 $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
ce885e21
AD
15 }
16
fb074239 17 require_once "functions.php";
36bfab86 18 require_once "sessions.php";
c339343b 19 require_once "modules/backend-rpc.php";
657770a0
AD
20 require_once "sanity_check.php";
21 require_once "config.php";
af106b0e
AD
22 require_once "db.php";
23 require_once "db-prefs.php";
657770a0 24
dc56b3b7 25 no_cache_incantation();
8d039718 26
009646d2 27 if (ENABLE_TRANSLATIONS == true) {
8d039718
AD
28 startup_gettext();
29 }
dc56b3b7 30
7f0acba7
AD
31 $script_started = getmicrotime();
32
009646d2 33 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
7f0acba7
AD
34
35 if (!$link) {
36 if (DB_TYPE == "mysql") {
37 print mysql_error();
38 }
009646d2 39 // PG seems to display its own errors just fine by default.
7f0acba7
AD
40 return;
41 }
42
f29ba148 43 init_connection($link);
7f0acba7
AD
44
45 $op = $_REQUEST["op"];
d9084cf2 46 $subop = $_REQUEST["subop"];
fac5e7d1 47 $mode = $_REQUEST["mode"];
7f0acba7 48
c339343b 49 $print_exec_time = false;
7e3634d9 50
565ca565
AD
51 if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
52 header("Content-Type: application/xml; charset=utf-8");
53 } else {
9fdf7824 54 header("Content-Type: text/plain; charset=utf-8");
262bd8ea
AD
55 }
56
bd202c3f
AD
57 if (ENABLE_GZIP_OUTPUT) {
58 ob_start("ob_gzhandler");
59 }
60
9c883208
AD
61 if (SINGLE_USER_MODE) {
62 authenticate_user($link, "admin", null);
63 }
64
009646d2
AD
65 if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
66 $op != "rss" && $op != "getUnread" && $op != "getProfiles") {
67
68 header("Content-Type: text/plain");
69 print json_encode(array("error" => array("code" => 6)));
70 return;
262bd8ea 71 }
1c7f75ed 72
ad815c71 73 $purge_intervals = array(
d1db26aa
AD
74 0 => __("Use default"),
75 -1 => __("Never purge"),
76 5 => __("1 week old"),
77 14 => __("2 weeks old"),
78 31 => __("1 month old"),
79 60 => __("2 months old"),
80 90 => __("3 months old"));
ad815c71
AD
81
82 $update_intervals = array(
ecace165 83 0 => __("Default interval"),
d1db26aa 84 -1 => __("Disable updates"),
1e7cbe16 85 15 => __("Each 15 minutes"),
d1db26aa 86 30 => __("Each 30 minutes"),
505f2f0e
AD
87 60 => __("Hourly"),
88 240 => __("Each 4 hours"),
89 720 => __("Each 12 hours"),
90 1440 => __("Daily"),
91 10080 => __("Weekly"));
92
93 $update_intervals_nodefault = array(
94 -1 => __("Disable updates"),
95 15 => __("Each 15 minutes"),
96 30 => __("Each 30 minutes"),
d1db26aa
AD
97 60 => __("Hourly"),
98 240 => __("Each 4 hours"),
99 720 => __("Each 12 hours"),
100 1440 => __("Daily"),
101 10080 => __("Weekly"));
ad815c71 102
16211ddb 103 $update_methods = array(
ecace165 104 0 => __("Default"),
0dd9c0cf 105 1 => __("Magpie"),
009646d2 106 2 => __("SimplePie"),
b3009bcd 107 3 => __("Twitter OAuth"));
16211ddb 108
78a5c296 109 if (DEFAULT_UPDATE_METHOD == "1") {
16211ddb
AD
110 $update_methods[0] .= ' (SimplePie)';
111 } else {
112 $update_methods[0] .= ' (Magpie)';
113 }
114
3c5783b7 115 $access_level_names = array(
009646d2 116 0 => __("User"),
a88d37e5 117 5 => __("Power User"),
d1db26aa 118 10 => __("Administrator"));
3c5783b7 119
f27d955a 120 require_once "modules/pref-prefs.php";
ef8be8ea
AD
121 require_once "modules/popup-dialog.php";
122 require_once "modules/help.php";
123 require_once "modules/pref-feeds.php";
124 require_once "modules/pref-filters.php";
125 require_once "modules/pref-labels.php";
126 require_once "modules/pref-users.php";
ef8be8ea 127
ebb948c2
AD
128 $error = sanity_check($link);
129
130 if ($error['code'] != 0) {
131 print json_encode(array("error" => $error));
132 return;
133 }
023fe037 134
45004d43
AD
135 switch($op) { // Select action according to $op value.
136 case "rpc":
137 // Handle remote procedure calls.
138 handle_rpc_request($link);
139 break; // rpc
140
141 case "feeds":
b4e75b2a 142 $subop = $_REQUEST["subop"];
1985a5e0 143 $root = (bool)$_REQUEST["root"];
45004d43
AD
144
145 switch($subop) {
146 case "catchupAll":
009646d2 147 db_query($link, "UPDATE ttrss_user_entries SET
45004d43 148 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
ad0056a8
AD
149 ccache_zero_all($link, $_SESSION["uid"]);
150
45004d43
AD
151 break;
152
153 case "collapse":
b4e75b2a 154 $cat_id = db_escape_string($_REQUEST["cid"]);
fcf70c51
AD
155 $mode = (int) db_escape_string($_REQUEST['mode']);
156 toggle_collapse_cat($link, $cat_id, $mode);
45004d43
AD
157 return;
158 break;
159 }
c3b81db0 160
1985a5e0
AD
161 if (!$root) {
162 print json_encode(outputFeedList($link));
163 } else {
164
165 $feeds = outputFeedList($link, false);
166
167 $root = array();
168 $root['id'] = 'root';
169 $root['name'] = __('Feeds');
170 $root['items'] = $feeds['items'];
171
172 $fl = array();
173 $fl['identifier'] = 'id';
174 $fl['label'] = 'name';
175 $fl['items'] = array($root);
176
177 print json_encode($fl);
178 }
13e785e0 179
45004d43 180 break; // feeds
1cd17194 181
b509d64e
AD
182 case "la":
183 $id = db_escape_string($_REQUEST['id']);
184
185 $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
186 WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
187
188 if (db_num_rows($result) == 1) {
189 $article_url = db_fetch_result($result, 0, 'link');
d4dcf8e9 190 $article_url = str_replace("\n", "", $article_url);
b509d64e
AD
191
192 header("Location: $article_url");
193 return;
194
195 } else {
196 print_error(__("Article not found."));
197 }
198 break;
199
45004d43 200 case "view":
e097e8be 201
b4e75b2a
AD
202 $id = db_escape_string($_REQUEST["id"]);
203 $cids = split(",", db_escape_string($_REQUEST["cids"]));
204 $mode = db_escape_string($_REQUEST["mode"]);
205 $omode = db_escape_string($_REQUEST["omode"]);
e097e8be 206
009646d2 207 // in prefetch mode we only output requested cids, main article
45004d43 208 // just gets marked as read (it already exists in client cache)
e097e8be 209
009646d2
AD
210 $articles = array();
211
45004d43 212 if ($mode == "") {
009646d2 213 array_push($articles, format_article($link, $id, false));
eedfb635 214 } else if ($mode == "zoom") {
009646d2 215 array_push($articles, format_article($link, $id, false, true, true));
45004d43
AD
216 } else {
217 catchupArticleById($link, $id, 0);
218 }
e097e8be 219
a598370d
AD
220 if (!$_SESSION["bw_limit"]) {
221 foreach ($cids as $cid) {
222 if ($cid) {
009646d2 223 array_push($articles, format_article($link, $cid, false, false));
a598370d 224 }
45004d43 225 }
e097e8be 226 }
e097e8be 227
009646d2 228 print json_encode($articles);
5a94a953 229
45004d43 230 break; // view
1cd17194 231
45004d43 232 case "viewfeed":
1cd17194 233
45004d43 234 $timing_info = getmicrotime();
46921916 235
bd202c3f 236 $reply = array();
3de0261a 237
b4e75b2a 238 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
46921916 239
b4e75b2a 240 $omode = db_escape_string($_REQUEST["omode"]);
3de0261a 241
b4e75b2a
AD
242 $feed = db_escape_string($_REQUEST["feed"]);
243 $subop = db_escape_string($_REQUEST["subop"]);
244 $view_mode = db_escape_string($_REQUEST["view_mode"]);
6f068202 245 $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
c3fddd05
AD
246 @$cat_view = db_escape_string($_REQUEST["cat"]);
247 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
248 @$offset = db_escape_string($_REQUEST["skip"]);
249 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
b4e75b2a 250 $order_by = db_escape_string($_REQUEST["order_by"]);
203de776 251
fe1087fb
AD
252 /* Feed -5 is a special case: it is used to display auxiliary information
253 * when there's nothing to load - e.g. no stuff in fresh feed */
254
255 if ($feed == -5) {
bd202c3f
AD
256 print json_encode(generate_dashboard_feed($link));
257 return;
258 }
259
260 $result = false;
261
262 if ($feed < -10) {
81f6deea 263 $label_feed = -11-$feed;
bd202c3f
AD
264 $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
265 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
266 } else if (!$cat_view && $feed > 0) {
267 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
268 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
81f6deea 269 } else if ($cat_view && $feed > 0) {
bd202c3f
AD
270 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
271 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
272 }
273
274 if ($result && db_num_rows($result) == 0) {
275 print json_encode(generate_error_feed($link, __("Feed not found.")));
fe1087fb
AD
276 return;
277 }
278
51e196de
AD
279 /* Updating a label ccache means recalculating all of the caches
280 * so for performance reasons we don't do that here */
281
5225d420
AD
282 if ($feed >= 0) {
283 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
284 }
0737b95a 285
45004d43
AD
286 set_pref($link, "_DEFAULT_VIEW_MODE", $view_mode);
287 set_pref($link, "_DEFAULT_VIEW_LIMIT", $limit);
7b4d02a8 288 set_pref($link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
40496720 289
45004d43
AD
290 if (!$cat_view && preg_match("/^[0-9][0-9]*$/", $feed)) {
291 db_query($link, "UPDATE ttrss_feeds SET last_viewed = NOW()
292 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
293 }
7466dc6a 294
bd202c3f
AD
295 $reply['headlines'] = array();
296
297 if (!$next_unread_feed)
298 $reply['headlines']['id'] = $feed;
299 else
300 $reply['headlines']['id'] = $next_unread_feed;
301
302 $reply['headlines']['is_cat'] = (bool) $cat_view;
009646d2 303
a5c815d3
AD
304 $override_order = false;
305
b3990c92
AD
306 if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
307 $date_sort_field = "updated";
308 } else {
309 $date_sort_field = "date_entered";
310 }
311
a5c815d3
AD
312 switch ($order_by) {
313 case "date":
314 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 315 $override_order = "$date_sort_field";
009646d2 316 } else {
b3990c92 317 $override_order = "$date_sort_field DESC";
a5c815d3
AD
318 }
319 break;
c50e2b30 320
a5c815d3 321 case "title":
7b20c977 322 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 323 $override_order = "title DESC, $date_sort_field";
7b20c977 324 } else {
b3990c92 325 $override_order = "title, $date_sort_field DESC";
7b20c977 326 }
a5c815d3 327 break;
d76a3b03 328
a5c815d3 329 case "score":
7b20c977 330 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
b3990c92 331 $override_order = "score, $date_sort_field";
7b20c977 332 } else {
b3990c92 333 $override_order = "score DESC, $date_sort_field DESC";
7b20c977 334 }
a5c815d3
AD
335 break;
336 }
e19c1824 337
905ff52a
AD
338 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
339
bd202c3f 340 $ret = format_headlines_list($link, $feed, $subop,
009646d2 341 $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
a5c815d3 342 $vgroup_last_feed, $override_order);
7b4d02a8 343
a5c815d3
AD
344 $topmost_article_ids = $ret[0];
345 $headlines_count = $ret[1];
346 $returned_feed = $ret[2];
347 $disable_cache = $ret[3];
348 $vgroup_last_feed = $ret[4];
7b4d02a8 349
bd202c3f
AD
350 $reply['headlines']['content'] = $ret[5];
351 $reply['headlines']['toolbar'] = $ret[6];
46921916 352
905ff52a
AD
353 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
354
cfcb7d42 355 $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
696a6850
AD
356 $cat_view, true);
357
358 if ($headlines_unread == -1) {
359 $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
696a6850 360 }
a5c815d3 361
bd202c3f 362 $reply['headlines-info'] = array("count" => (int) $headlines_count,
ffbe082d
AD
363 "vgroup_last_feed" => $vgroup_last_feed,
364 "unread" => (int) $headlines_unread,
365 "disable_cache" => (bool) $disable_cache);
366
bd202c3f 367 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
009646d2 368
bd202c3f 369 if (is_array($topmost_article_ids) && !get_pref($link, 'COMBINED_DISPLAY_MODE') && !$_SESSION["bw_limit"]) {
009646d2
AD
370 $articles = array();
371
a5c815d3 372 foreach ($topmost_article_ids as $id) {
009646d2 373 array_push($articles, format_article($link, $id, $feed, false));
45004d43 374 }
009646d2 375
bd202c3f
AD
376 $reply['articles'] = $articles;
377 }
46921916 378
dd25b0c6 379 if ($subop) {
bd202c3f 380 $reply['counters'] = getAllCounters($link, $omode, $feed);
009646d2 381 }
98bea1b1 382
b4e75b2a 383 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
46921916 384
bd202c3f
AD
385 $reply['runtime-info'] = make_runtime_info($link);
386
387 print json_encode($reply);
03c88bdd 388
45004d43 389 break; // viewfeed
1cd17194 390
45004d43
AD
391 case "pref-feeds":
392 module_pref_feeds($link);
393 break; // pref-feeds
a7f22b70 394
45004d43
AD
395 case "pref-filters":
396 module_pref_filters($link);
397 break; // pref-filters
a0476535 398
45004d43
AD
399 case "pref-labels":
400 module_pref_labels($link);
401 break; // pref-labels
d0000401 402
45004d43
AD
403 case "pref-prefs":
404 module_pref_prefs($link);
405 break; // pref-prefs
f27d955a 406
45004d43
AD
407 case "pref-users":
408 module_pref_users($link);
409 break; // prefs-users
a0476535 410
45004d43
AD
411 case "help":
412 module_help($link);
413 break; // help
a0476535 414
45004d43
AD
415 case "dlg":
416 module_popup_dialog($link);
417 break; // dlg
a7f22b70 418
45004d43
AD
419 case "pref-pub-items":
420 module_pref_pub_items($link);
421 break; // pref-pub-items
e4f4b46f 422
45004d43 423 case "globalUpdateFeeds":
45004d43 424 // Update all feeds needing a update.
5cbaf873 425 update_daemon_common($link, 0, true, true);
45004d43 426 break; // globalUpdateFeeds
e5d758e3 427
45004d43
AD
428 case "pref-feed-browser":
429 module_pref_feed_browser($link);
430 break; // pref-feed-browser
c6232e43 431
45004d43 432 case "rss":
b4e75b2a 433 $feed = db_escape_string($_REQUEST["id"]);
8801fb01 434 $key = db_escape_string($_REQUEST["key"]);
b4e75b2a
AD
435 $is_cat = $_REQUEST["is_cat"] != false;
436 $limit = (int)db_escape_string($_REQUEST["limit"]);
e1eb2147 437
b4e75b2a
AD
438 $search = db_escape_string($_REQUEST["q"]);
439 $match_on = db_escape_string($_REQUEST["m"]);
440 $search_mode = db_escape_string($_REQUEST["smode"]);
2f2bd1b3 441 $view_mode = db_escape_string($_REQUEST["view-mode"]);
18664970 442
b4798282
AD
443 if (SINGLE_USER_MODE) {
444 authenticate_user($link, "admin", null);
445 }
446
8e57cfbe 447 $owner_id = false;
19039fd0 448
8e57cfbe 449 if ($key) {
8801fb01
AD
450 $result = db_query($link, "SELECT owner_uid FROM
451 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
452
453 if (db_num_rows($result) == 1)
8e57cfbe 454 $owner_id = db_fetch_result($result, 0, "owner_uid");
45004d43 455 }
18664970 456
8e57cfbe
AD
457 if ($owner_id) {
458 $_SESSION['uid'] = $owner_id;
459
23d72f39
AD
460 generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
461 $search, $search_mode, $match_on, $view_mode);
19039fd0
AD
462 } else {
463 header('HTTP/1.1 403 Forbidden');
45004d43
AD
464 }
465 break; // rss
18664970 466
45004d43 467 case "getUnread":
b4e75b2a 468 $login = db_escape_string($_REQUEST["login"]);
1f7b77d1 469 $fresh = $_REQUEST["fresh"] == "1";
f3acc32e 470
45004d43 471 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
7e3634d9 472
45004d43
AD
473 if (db_num_rows($result) == 1) {
474 $uid = db_fetch_result($result, 0, "id");
1f7b77d1 475
45004d43 476 print getGlobalUnread($link, $uid);
1f7b77d1
AD
477
478 if ($fresh) {
479 print ";";
480 print getFeedArticles($link, -3, false, true, $uid);
481 }
482
45004d43
AD
483 } else {
484 print "-1;User not found";
485 }
7e3634d9 486
45004d43
AD
487 $print_exec_time = false;
488 break; // getUnread
9cd7c995 489
45004d43 490 case "digestTest":
45004d43
AD
491 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
492 $print_exec_time = false;
493 break; // digestTest
448b0abd 494
45004d43 495 case "digestSend":
45004d43
AD
496 send_headlines_digests($link);
497 $print_exec_time = false;
498 break; // digestSend
7e3634d9 499
ba7e88e5 500 case "loading":
009646d2 501 print __("Loading, please wait...") . " " .
ba7e88e5
AD
502 "<img src='images/indicator_tiny.gif'>";
503
d9084cf2
AD
504 case "getProfiles":
505 $login = db_escape_string($_REQUEST["login"]);
506 $password = db_escape_string($_REQUEST["password"]);
507
508 if (authenticate_user($link, $login, $password)) {
509 $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
510 WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
511
512 print "<select style='width: 100%' name='profile'>";
513
514 print "<option value='0'>" . __("Default profile") . "</option>";
515
516 while ($line = db_fetch_assoc($result)) {
517 $id = $line["id"];
518 $title = $line["title"];
519
520 print "<option value='$id'>$title</option>";
521 }
522
523 print "</select>";
524
525 $_SESSION = array();
0456176a 526 }
d9084cf2 527 break;
d9084cf2 528
45004d43 529 } // Select action according to $op value.
f3acc32e 530
ba7e88e5 531
45004d43 532 // We close the connection to database.
4b3dff6e 533 db_close($link);
1cd17194 534?>
406d9489 535
7e3634d9 536<?php if ($print_exec_time) { ?>
1d3a17c7 537<!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
f3acc32e 538<?php } ?>