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